00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033 #include <string.h>
00034 #include <ctype.h>
00035 #include "sym.h"
00036
00037 static void _to_lower(char *s){
00038
00039 while( *s ){
00040 if( isupper(*s) )*s = tolower(*s);
00041 s++;
00042 }
00043 }
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060 #define PRIME 211
00061 #define MASK 0xf0000000l
00062
00063
00064 static int hashpjw(s)
00065 char *s;
00066 {
00067 char *p;
00068 unsigned long h = 0, g;
00069 for ( p = s; *p != '\0'; p = p+1 ) {
00070 h = (h << 4) + (*p);
00071 if (g = h&MASK) {
00072 h = h ^ (g >> 24);
00073 h = h ^ g;
00074 }
00075 }
00076 return h % PRIME;
00077 }
00078
00079
00080
00081
00082
00083
00084
00085 SymbolTable sym_NewSymbolTable(
00086 void* (*memory_allocating_function)(size_t,void *),
00087 void *pMemorySegment
00088 ){
00089
00090
00091
00092
00093
00094
00095
00096 SymbolTable t;
00097 int i;
00098
00099 if( ! (t = (SymbolTable)memory_allocating_function(PRIME*sizeof(pSymbol),pMemorySegment)) )return NULL;
00100 for( i=0 ; i<PRIME ; i++ )t[i]=NULL;
00101 return t;
00102 }
00103
00104
00105 static void sym_FreeSymbolSub(
00106 pSymbol table,
00107 void (*memory_releasing_function)(void *, void *),
00108 void *pMemorySegment
00109 ){
00110 if( ! table )return;
00111 if( table->small_son )
00112 sym_FreeSymbolSub(table->small_son,memory_releasing_function,pMemorySegment);
00113 if( table->big_son )
00114 sym_FreeSymbolSub(table->big_son,memory_releasing_function,pMemorySegment);
00115 if( table->name )
00116 memory_releasing_function(table->name,pMemorySegment);
00117 memory_releasing_function(table,pMemorySegment);
00118 }
00119
00120
00121
00122
00123
00124
00125
00126
00127
00128
00129
00130
00131 void sym_FreeSymbolTable(
00132 SymbolTable table,
00133 void (*memory_releasing_function)(void *,void *),
00134 void *pMemorySegment
00135 ){
00136
00137
00138 int i;
00139
00140 for( i=0 ; i<PRIME ; i++ )
00141 sym_FreeSymbolSub(table[i],memory_releasing_function,pMemorySegment);
00142
00143 memory_releasing_function(table,pMemorySegment);
00144 }
00145
00146 static void sym_TraverseSymbolTableSub(
00147 pSymbol table,
00148 void (*call_back_function)(char *SymbolName, void *SymbolValue, void *f),
00149 void *f
00150 ){
00151
00152 if( ! table )return;
00153 if( table->small_son )
00154 sym_TraverseSymbolTableSub(table->small_son,call_back_function,f);
00155 if( table->big_son )
00156 sym_TraverseSymbolTableSub(table->big_son,call_back_function,f);
00157 if( table->name )
00158 call_back_function(table->name,table->value,f);
00159 }
00160
00161
00162
00163
00164
00165
00166
00167
00168 void sym_TraverseSymbolTable(
00169 SymbolTable table,
00170 void (*call_back_function)(char *SymbolName, void *SymbolValue, void *f),
00171 void *f
00172 ){
00173
00174
00175
00176
00177
00178
00179
00180
00181
00182
00183
00184 int i;
00185
00186 if( !table ) return;
00187 for( i=0 ; i<PRIME ; i++ )
00188 sym_TraverseSymbolTableSub(table[i],call_back_function,f);
00189 }
00190
00191
00192
00193
00194
00195
00196
00197 void **sym_LookupSymbol(
00198 char *s,
00199 SymbolTable hashtable,
00200 int insert,
00201 void* (*memory_allocating_function)(size_t, void *),
00202 void (*memory_releasing_function)(void *, void *),
00203 void *pMemorySegment
00204 ){
00205
00206
00207
00208
00209
00210
00211
00212
00213
00214
00215
00216
00217
00218
00219
00220
00221
00222
00223
00224
00225
00226
00227
00228
00229
00230
00231
00232 pSymbol *work_pointer;
00233 int k;
00234
00235
00236 _to_lower(s);
00237
00238 k = hashpjw( s );
00239 work_pointer = &(hashtable[ k ]);
00240 while( *work_pointer && (k=strcmp(s,(*work_pointer)->name)) )
00241 work_pointer= k > 0 ? &((*work_pointer)->big_son) :
00242 &((*work_pointer)->small_son);
00243
00244 if( *work_pointer )return &((*work_pointer)->value);
00245
00246
00247 if( ! insert )return NULL;
00248
00249
00250
00251 *work_pointer = (pSymbol)memory_allocating_function( sizeof(Symbol),pMemorySegment);
00252 if( !*work_pointer )return NULL;
00253
00254 (*work_pointer)->name = (char *)memory_allocating_function( (strlen( s )+1)*sizeof(char),pMemorySegment );
00255 if( !(*work_pointer)->name ) {
00256 memory_releasing_function(*work_pointer,pMemorySegment);
00257 return NULL;
00258 }
00259 strcpy((*work_pointer)->name , s);
00260
00261
00262 (*work_pointer)->value = NULL;
00263
00264
00265 (*work_pointer)->big_son =
00266 (*work_pointer)->small_son = NULL;
00267
00268 return &((*work_pointer)->value);
00269 }
00270
00271
00272
00273
00274
00275
00276
00277 int sym_DeleteSymbol(
00278 char *s,
00279 SymbolTable hashtable,
00280 void (*memory_releasing_function)(void *, void *),
00281 void *pMemorySegment
00282 ){
00283
00284
00285
00286
00287
00288
00289
00290
00291
00292
00293
00294
00295 pSymbol *work_pointer,*swp;
00296 pSymbol sym;
00297 int k;
00298
00299
00300 _to_lower(s);
00301
00302 k = hashpjw( s );
00303 work_pointer = swp = &(hashtable[ k ]);
00304 while( *work_pointer && (k=strcmp(s,(*work_pointer)->name)) )
00305 work_pointer= k > 0 ? &((*work_pointer)->big_son) :
00306 &((*work_pointer)->small_son);
00307
00308
00309 if( *work_pointer == NULL )return 1;
00310 sym = *work_pointer;
00311
00312 *work_pointer = NULL;
00313
00314 if( sym->big_son ){
00315 *work_pointer = sym->big_son;
00316 if( sym->small_son == NULL ){
00317 memory_releasing_function(sym->name,pMemorySegment);
00318 memory_releasing_function(sym,pMemorySegment);
00319 return 0;
00320 }
00321 }else{
00322
00323 if( sym->small_son ){
00324 *work_pointer = sym->small_son;
00325 memory_releasing_function(sym->name,pMemorySegment);
00326 memory_releasing_function(sym,pMemorySegment);
00327 return 0;
00328 }
00329
00330 memory_releasing_function(sym->name,pMemorySegment);
00331 memory_releasing_function(sym,pMemorySegment);
00332 return 0;
00333 }
00334
00335
00336 work_pointer = swp;
00337 while( *work_pointer && (k=strcmp(sym->small_son->name,(*work_pointer)->name)) )
00338 work_pointer= k > 0 ? &((*work_pointer)->big_son) :
00339 &((*work_pointer)->small_son);
00340
00341
00342 *work_pointer = sym->small_son;
00343
00344
00345 memory_releasing_function(sym->name,pMemorySegment);
00346 memory_releasing_function(sym,pMemorySegment);
00347
00348 return 0;
00349 }