Debian Codegraph @

src/core/chuck_table.cpp

raw
1 /*----------------------------------------------------------------------------
2 ChucK Strongly-timed Audio Programming Language
3 Compiler, Virtual Machine, and Synthesis Engine
4
5 Copyright (c) 2003 Ge Wang and Perry R. Cook. All rights reserved.
6 http://chuck.stanford.edu/
7 http://chuck.cs.princeton.edu/
8
9 This program is free software; you can redistribute it and/or modify
10 it under the dual-license terms of EITHER the MIT License OR the GNU
11 General Public License (the latter as published by the Free Software
12 Foundation; either version 2 of the License or, at your option, any
13 later version).
14
15 This program is distributed in the hope that it will be useful and/or
16 interesting, but WITHOUT ANY WARRANTY; without even the implied warranty
17 of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 MIT Licence and/or the GNU General Public License for details.
19
20 You should have received a copy of the MIT License and the GNU General
21 Public License (GPL) along with this program; a copy of the GPL can also
22 be obtained by writing to the Free Software Foundation, Inc., 59 Temple
23 Place, Suite 330, Boston, MA 02111-1307 U.S.A.
24 -----------------------------------------------------------------------------*/
25
26 //-----------------------------------------------------------------------------
27 // file: chuck_table.cpp
28 // desc: chuck table implementation for parsers
29 //
30 // No algorithm should use these functions directly, because
31 // programming with void* is too error-prone. Instead,
32 // each module should make "wrapper" functions that take
33 // well-typed arguments and call the TAB_ functions.
34 //
35 // copyright (c) 1997 Andrew W. Appel.
36 //
37 // original author: Andrew Appel ([email protected])
38 // modified: Ge Wang ([email protected])
39 // date: Autumn 2002
40 //-----------------------------------------------------------------------------
41 #include "chuck_table.h"
42 #include "chuck_symbol.h"
43 #include <stdio.h>
44 #include <string.h>
45
46 #define TABSIZE 65437
47
48
49 typedef struct binder_ *binder;
50 struct binder_ {void *key; void *value; binder next; void *prevtop;};
51 struct TAB_table_ {
52 unsigned long size;
53 binder * table;
54 void * top;
55 TAB_eq_func eq_func;
56 TAB_hash_func hash_func;
57 };
58
59
60 static binder Binder(void *key, void *value, binder next, void *prevtop)
61 {
62 binder b = (binder)checked_malloc(sizeof(struct binder_));
63 b->key = key; b->value=value; b->next=next; b->prevtop = prevtop;
64 return b;
65 }
66
67
68 TAB_table TAB_empty(void)
69 {
70 return TAB_empty2(TABSIZE);
71 }
72
73
74 TAB_table TAB_empty2( unsigned long s )
75 {
76 TAB_table t = (TAB_table)checked_malloc(sizeof(struct TAB_table_));
77 unsigned long i;
78 t->table = (binder *)checked_malloc(sizeof(binder)*s);
79 t->size = s;
80 t->top = NULL;
81 t->eq_func = NULL;
82 t->hash_func = NULL;
83 for( i = 0; i < s; i++ )
84 t->table[i] = NULL;
85 return t;
86 }
87
88
89 TAB_table TAB_empty3( TAB_eq_func eq, TAB_hash_func hash, unsigned long s )
90 {
91 TAB_table t = TAB_empty2(s);
92 t->eq_func = eq;
93 t->hash_func = hash;
94 return t;
95 }
96
97
98 void TAB_delete( TAB_table t )
99 {
100 unsigned long i;
101 binder p = NULL, n = NULL;
102 for(i = 0; i < t->size; i++ )
103 {
104 p = t->table[i];
105 while( p )
106 {
107 n = p->next;
108 free(p);
109 p = n;
110 }
111 }
112
113 // free the table
114 free( t->table );
115 free( t );
116 }
117
118
119
120 /* The cast from pointer to integer in the expression
121 * ((unsigned)key) % TABSIZE
122 * may lead to a warning message. However, the code is safe,
123 * and will still operate correctly. This line is just hashing
124 * a pointer value into an integer value, and no matter how the
125 * conversion is done, as long as it is done consistently, a
126 * reasonable and repeatable index into the table will result.
127 */
128
129 void TAB_enter(TAB_table t, void *key, void *value)
130 {
131 t_CKINT index;
132 t_CKUINT hval = (t_CKUINT)key;
133 assert(t && key);
134 if( t->hash_func )
135 hval = (t_CKUINT)t->hash_func(key);
136 index = hval % t->size;
137 t->table[index] = Binder(key, value, t->table[index], t->top);
138 t->top = key;
139 }
140
141 void *TAB_look(TAB_table t, void *key)
142 {
143 t_CKINT index;
144 t_CKUINT hval = (t_CKUINT)key;
145 binder b;
146 assert(t && key);
147 if( t->hash_func )
148 hval = (t_CKUINT)t->hash_func(key);
149 index= hval % t->size;
150 if( !t->eq_func )
151 {
152 for(b=t->table[index]; b; b=b->next)
153 if (b->key==key) return b->value;
154 }
155 else
156 {
157 for(b=t->table[index]; b; b=b->next)
158 if (t->eq_func(b->key, key)) return b->value;
159 }
160
161 return NULL;
162 }
163
164 void *TAB_pop(TAB_table t)
165 {
166 void *k; binder b; long index;
167 t_CKUINT hval;
168 assert (t);
169 k = t->top;
170 assert (k);
171 hval = (t_CKUINT)k;
172 if(t->hash_func) hval = (t_CKUINT)t->hash_func(k);
173 index = hval % t->size;
174 b = t->table[index];
175 assert(b);
176 t->table[index] = b->next;
177 t->top=b->prevtop;
178 return b->key;
179 }
180
181 void *TAB_topv(TAB_table t)
182 {
183 void *k; binder b; t_CKINT index;
184 t_CKUINT hval;
185 assert(t);
186 k = t->top;
187 assert(k);
188 hval = (t_CKUINT)k;
189 if(t->hash_func) hval = (t_CKUINT)t->hash_func(k);
190 index = hval % t->size;
191 b = t->table[index];
192 assert(b);
193 return b->value;
194 }
195
196 void TAB_dump(TAB_table t, void (*show)(void *key, void *value))
197 {
198 void *k = t->top;
199 t_CKINT index = ((t_CKUINT)k) % t->size;
200 binder b = t->table[index];
201 if (b==NULL) return;
202 t->table[index]=b->next;
203 t->top=b->prevtop;
204 show(b->key,b->value);
205 TAB_dump(t,show);
206 assert(t->top == b->prevtop && t->table[index]==b->next);
207 t->top=k;
208 t->table[index]=b;
209 }
210
211 long str_eq( void * lhs, void * rhs )
212 {
213 return !strcmp( (char *)lhs, (char *)rhs );
214 }
215
216 long str_hash( void * str )
217 {
218 unsigned long h=0; char *s;
219 for(s=(char *)str; *s; s++)
220 h = h*65599 + *s;
221 return h;
222 }