]> git.donarmstrong.com Git - lilypond.git/blob - lily/include/symbol-cache.hh
release: 1.3.93
[lilypond.git] / lily / include / symbol-cache.hh
1 /*   
2   symbol-cache.hh -- declare Symbol cacher.
3   
4   source file of the GNU LilyPond music typesetter
5   
6   (c) 2000 Han-Wen Nienhuys <hanwen@cs.uu.nl>
7   
8  */
9
10 #ifndef SYMBOL_CACHE_HH
11 #define SYMBOL_CACHE_HH
12
13 #if 1
14
15 /*
16   A per file cache: for each compilation unit, there is a separate
17   cache that maps the address of a string directly to a SCM value
18
19  */
20 struct Symbol_cache_pair{
21   const char * key;
22   SCM val ;
23 };
24
25 static Symbol_cache_pair *private_symbol_cache;
26 static Symbol_cache_pair *private_symbol_cache_end;
27
28 static SCM
29 symbol (const char *ch) __attribute__ ((unused));
30
31  SCM
32 symbol (const char *ch)
33 {
34   Symbol_cache_pair * lo = private_symbol_cache;
35   Symbol_cache_pair * hi = private_symbol_cache_end -1;
36
37   if (lo)
38     {
39       do
40         {
41           Symbol_cache_pair * mid = lo + (hi - lo) / 2 ;
42           if (mid->key > ch)
43             hi = mid;
44           else
45             lo = mid;
46         }
47       while ((hi - lo) > 1);
48       if (lo->key== ch)
49         return lo->val;
50     }
51
52   
53   Symbol_cache_pair * p = private_symbol_cache;
54   for (; p < private_symbol_cache_end
55          && p->key < ch ; p++)
56     ;
57
58   int idx = p - private_symbol_cache;
59   
60   SCM sym = gh_symbol2scm ((char*) ch);
61   scm_permanent_object (sym);
62   
63   int sz = private_symbol_cache_end - private_symbol_cache;
64   sz ++ ;
65   private_symbol_cache
66     = (Symbol_cache_pair*) realloc (private_symbol_cache,
67                                     sizeof (Symbol_cache_pair)* sz);
68   private_symbol_cache_end = private_symbol_cache + sz;
69   for (p = private_symbol_cache_end -1;
70        p != private_symbol_cache + idx; p --)
71     *p = *(p - 1);
72
73   p->key = ch;
74   p->val = sym;
75     
76   return sym;
77 }
78 #endif /* SYMBOL_CACHE_HH */
79 #endif