#include <stdio.h>
#include <stdlib.h>
#include "st.h"
#include <malloc.h>
st.cのインクルード依存関係図
構成 | |
struct | st_table_entry |
マクロ定義 | |
#define | ST_DEFAULT_MAX_DENSITY 5 |
#define | ST_DEFAULT_INIT_TABLE_SIZE 11 |
#define | alloc(type) (type*)malloc((unsigned)sizeof(type)) |
#define | Calloc(n, s) (char*)calloc((n),(s)) |
#define | EQUAL(table, x, y) ((x)==(y) || (*table->type->compare)((x),(y)) == 0) |
#define | do_hash(key, table) (unsigned int)(*(table)->type->hash)((key)) |
#define | do_hash_bin(key, table) (do_hash(key, table)%(table)->num_bins) |
#define | MINSIZE 8 |
#define | PTR_NOT_EQUAL(table, ptr, hash_val, key) ((ptr) != 0 && (ptr->hash != (hash_val) || !EQUAL((table), (key), (ptr)->key))) |
#define | COLLISION |
#define | FIND_ENTRY(table, ptr, hash_val, bin_pos) |
#define | ADD_DIRECT(table, key, value, hash_val, bin_pos) |
型定義 | |
typedef st_table_entry | st_table_entry |
関数 | |
int | numcmp () |
int | numhash () |
int | strcmp () |
int | strhash () |
void | rehash () |
int | new_size (int size) |
st_table * | st_init_table_with_size (struct st_hash_type *type, int size) |
st_table * | st_init_table (struct st_hash_type *type) |
st_table * | st_init_numtable () |
st_table * | st_init_numtable_with_size (int size) |
st_table * | st_init_strtable () |
st_table * | st_init_strtable_with_size (int size) |
void | st_free_table (st_table *table) |
int | st_lookup (st_table *table, char *key, char **value) |
int | st_insert (st_table *table, char *key, char *value) |
void | st_add_direct (st_table *table, char *key, char *value) |
void | rehash (st_table *table) |
st_table * | st_copy (st_table *old_table) |
int | st_delete (st_table *table, char **key, char **value) |
int | st_delete_safe (st_table *table, char **key, char **value, char *never) |
int | delete_never (char *key, char *value, char *never) |
void | st_cleanup_safe (st_table *table, char *never) |
void | st_foreach (st_table *table, int(*func)(), char *arg) |
int | strhash (char *string) |
int | numcmp (long x, long y) |
int | numhash (long n) |
変数 | |
st_hash_type | type_numhash |
st_hash_type | type_strhash |
long | primes [] |
st.c で定義されています。
|
値: do {\ st_table_entry *entry;\ if (table->num_entries/(table->num_bins) > ST_DEFAULT_MAX_DENSITY) {\ rehash(table);\ bin_pos = hash_val % table->num_bins;\ }\ \ entry = alloc(st_table_entry);\ \ entry->hash = hash_val;\ entry->key = key;\ entry->record = value;\ entry->next = table->bins[bin_pos];\ table->bins[bin_pos] = entry;\ table->num_entries++;\ } while (0) 参照元 st_add_direct(), と st_insert(). |
|
参照元 st_copy(), と st_init_table_with_size(). |
|
参照元 rehash(), st_copy(), と st_init_table_with_size(). |
|
|
|
参照元 st_add_direct(), st_insert(), と st_lookup(). |
|
参照元 st_delete(), と st_delete_safe(). |
|
参照元 st_delete(), と st_delete_safe(). |
|
値: do {\ bin_pos = hash_val%(table)->num_bins;\ ptr = (table)->bins[bin_pos];\ if (PTR_NOT_EQUAL(table, ptr, hash_val, key)) {\ COLLISION;\ while (PTR_NOT_EQUAL(table, ptr->next, hash_val, key)) {\ ptr = ptr->next;\ }\ ptr = ptr->next;\ }\ } while (0) 参照元 st_insert(), と st_lookup(). |
|
参照元 new_size(). |
|
|
|
|
|
|
|
参照元 rehash(), st_copy(), st_delete(), st_delete_safe(), st_foreach(), st_free_table(), st_init_table_with_size(), st_insert(), と st_lookup(). |
|
参照先 ST_CONTINUE, と ST_DELETE. 参照元 st_cleanup_safe().
00467 { 00468 if (value == never) return ST_DELETE; 00469 return ST_CONTINUE; 00470 } |
|
参照元 rehash(), と st_init_table_with_size().
00121 { 00122 int i; 00123 00124 #if 0 00125 for (i=3; i<31; i++) { 00126 if ((1<<i) > size) return 1<<i; 00127 } 00128 return -1; 00129 #else 00130 int newsize; 00131 00132 for (i = 0, newsize = MINSIZE; 00133 i < sizeof(primes)/sizeof(primes[0]); 00134 i++, newsize <<= 1) 00135 { 00136 if (newsize > size) return primes[i]; 00137 } 00138 /* Ran out of polynomials */ 00139 return -1; /* should raise exception */ 00140 #endif 00141 } |
|
00553 {
00554 return x != y;
00555 }
|
|
|
|
00560 {
00561 return n;
00562 }
|
|
|
|
参照先 Calloc, st_table_entry::hash, new_size(), st_table_entry::next, と st_table_entry.
00327 { 00328 register st_table_entry *ptr, *next, **new_bins; 00329 int i, old_num_bins = table->num_bins, new_num_bins; 00330 unsigned int hash_val; 00331 00332 new_num_bins = new_size(old_num_bins+1); 00333 new_bins = (st_table_entry**)Calloc(new_num_bins, sizeof(st_table_entry*)); 00334 00335 for(i = 0; i < old_num_bins; i++) { 00336 ptr = table->bins[i]; 00337 while (ptr != 0) { 00338 next = ptr->next; 00339 hash_val = ptr->hash % new_num_bins; 00340 ptr->next = new_bins[hash_val]; 00341 new_bins[hash_val] = ptr; 00342 ptr = next; 00343 } 00344 } 00345 free(table->bins); 00346 table->num_bins = new_num_bins; 00347 table->bins = new_bins; 00348 } |
|
|
|
参照先 ADD_DIRECT, と do_hash.
00316 { 00317 unsigned int hash_val, bin_pos; 00318 00319 hash_val = do_hash(key, table); 00320 bin_pos = hash_val % table->num_bins; 00321 ADD_DIRECT(table, key, value, hash_val, bin_pos); 00322 } |
|
参照先 delete_never(), st_table::num_entries, と st_foreach().
00474 { 00475 int num_entries = table->num_entries; 00476 00477 st_foreach(table, delete_never, never); 00478 table->num_entries = num_entries; 00479 } |
|
参照先 alloc, st_table::bins, Calloc, st_table_entry::next, と st_table_entry.
00353 { 00354 st_table *new_table; 00355 st_table_entry *ptr, *entry; 00356 int i, num_bins = old_table->num_bins; 00357 00358 new_table = alloc(st_table); 00359 if (new_table == 0) { 00360 return 0; 00361 } 00362 00363 *new_table = *old_table; 00364 new_table->bins = (st_table_entry**) 00365 Calloc((unsigned)num_bins, sizeof(st_table_entry*)); 00366 00367 if (new_table->bins == 0) { 00368 free(new_table); 00369 return 0; 00370 } 00371 00372 for(i = 0; i < num_bins; i++) { 00373 new_table->bins[i] = 0; 00374 ptr = old_table->bins[i]; 00375 while (ptr != 0) { 00376 entry = alloc(st_table_entry); 00377 if (entry == 0) { 00378 free(new_table->bins); 00379 free(new_table); 00380 return 0; 00381 } 00382 *entry = *ptr; 00383 entry->next = new_table->bins[i]; 00384 new_table->bins[i] = entry; 00385 ptr = ptr->next; 00386 } 00387 } 00388 return new_table; 00389 } |
|
参照先 do_hash_bin, EQUAL, st_table_entry::key, st_table_entry::next, st_table_entry::record, と st_table_entry.
00396 { 00397 unsigned int hash_val; 00398 st_table_entry *tmp; 00399 register st_table_entry *ptr; 00400 00401 hash_val = do_hash_bin(*key, table); 00402 ptr = table->bins[hash_val]; 00403 00404 if (ptr == 0) { 00405 if (value != 0) *value = 0; 00406 return 0; 00407 } 00408 00409 if (EQUAL(table, *key, ptr->key)) { 00410 table->bins[hash_val] = ptr->next; 00411 table->num_entries--; 00412 if (value != 0) *value = ptr->record; 00413 *key = ptr->key; 00414 free(ptr); 00415 return 1; 00416 } 00417 00418 for(; ptr->next != 0; ptr = ptr->next) { 00419 if (EQUAL(table, ptr->next->key, *key)) { 00420 tmp = ptr->next; 00421 ptr->next = ptr->next->next; 00422 table->num_entries--; 00423 if (value != 0) *value = tmp->record; 00424 *key = tmp->key; 00425 free(tmp); 00426 return 1; 00427 } 00428 } 00429 00430 return 0; 00431 } |
|
参照先 do_hash_bin, EQUAL, st_table_entry::key, st_table_entry::next, st_table_entry::record, と st_table_entry.
00439 { 00440 unsigned int hash_val; 00441 register st_table_entry *ptr; 00442 00443 hash_val = do_hash_bin(*key, table); 00444 ptr = table->bins[hash_val]; 00445 00446 if (ptr == 0) { 00447 if (value != 0) *value = 0; 00448 return 0; 00449 } 00450 00451 for(; ptr != 0; ptr = ptr->next) { 00452 if ((ptr->key != never) && EQUAL(table, ptr->key, *key)) { 00453 table->num_entries--; 00454 *key = ptr->key; 00455 if (value != 0) *value = ptr->record; 00456 ptr->key = ptr->record = never; 00457 return 1; 00458 } 00459 } 00460 00461 return 0; 00462 } |
|
参照先 st_table::bins, st_table_entry::key, st_table_entry::next, st_table::num_bins, st_table::num_entries, st_table_entry::record, ST_CONTINUE, ST_DELETE, st_retval, ST_STOP, と st_table_entry. 参照元 st_cleanup_safe().
00483 { 00484 st_table_entry *ptr, *last, *tmp; 00485 enum st_retval retval; 00486 int i; 00487 00488 for(i = 0; i < table->num_bins; i++) { 00489 last = 0; 00490 for(ptr = table->bins[i]; ptr != 0;) { 00491 retval = (*func)(ptr->key, ptr->record, arg); 00492 switch (retval) { 00493 case ST_CONTINUE: 00494 last = ptr; 00495 ptr = ptr->next; 00496 break; 00497 case ST_STOP: 00498 return; 00499 case ST_DELETE: 00500 tmp = ptr; 00501 if (last == 0) { 00502 table->bins[i] = ptr->next; 00503 } 00504 else { 00505 last->next = ptr->next; 00506 } 00507 ptr = ptr->next; 00508 free(tmp); 00509 table->num_entries--; 00510 } 00511 } 00512 } 00513 } |
|
参照先 st_table_entry::next, と st_table_entry.
00213 { 00214 register st_table_entry *ptr, *next; 00215 int i; 00216 00217 for(i = 0; i < table->num_bins; i++) { 00218 ptr = table->bins[i]; 00219 while (ptr != 0) { 00220 next = ptr->next; 00221 free(ptr); 00222 ptr = next; 00223 } 00224 } 00225 free(table->bins); 00226 free(table); 00227 } |
|
参照先 st_init_table(), と type_numhash.
00186 { 00187 return st_init_table(&type_numhash); 00188 } |
|
参照先 st_init_table_with_size(), と type_numhash.
00193 { 00194 return st_init_table_with_size(&type_numhash, size); 00195 } |
|
参照先 st_init_table(), と type_strhash.
00199 { 00200 return st_init_table(&type_strhash); 00201 } |
|
参照先 st_init_table_with_size(), と type_strhash.
00206 { 00207 return st_init_table_with_size(&type_strhash, size); 00208 } |
|
参照先 st_init_table_with_size(). 参照元 st_init_numtable(), と st_init_strtable().
00180 { 00181 return st_init_table_with_size(type, 0); 00182 } |
|
参照先 alloc, st_table::bins, Calloc, new_size(), st_table::num_bins, st_table::num_entries, st_table_entry, と st_table::type. 参照元 st_init_numtable_with_size(), st_init_strtable_with_size(), と st_init_table().
00156 { 00157 st_table *tbl; 00158 00159 #ifdef HASH_LOG 00160 if (init_st == 0) { 00161 init_st = 1; 00162 atexit(stat_col); 00163 } 00164 #endif 00165 00166 size = new_size(size); /* round up to prime number */ 00167 00168 tbl = alloc(st_table); 00169 tbl->type = type; 00170 tbl->num_entries = 0; 00171 tbl->num_bins = size; 00172 tbl->bins = (st_table_entry **)Calloc(size, sizeof(st_table_entry*)); 00173 00174 return tbl; 00175 } |
|
参照先 ADD_DIRECT, do_hash, FIND_ENTRY, st_table_entry::record, と st_table_entry.
00294 { 00295 unsigned int hash_val, bin_pos; 00296 register st_table_entry *ptr; 00297 00298 hash_val = do_hash(key, table); 00299 FIND_ENTRY(table, ptr, hash_val, bin_pos); 00300 00301 if (ptr == 0) { 00302 ADD_DIRECT(table, key, value, hash_val, bin_pos); 00303 return 0; 00304 } 00305 else { 00306 ptr->record = value; 00307 return 1; 00308 } 00309 } |
|
参照先 do_hash, FIND_ENTRY, st_table_entry::record, と st_table_entry.
00255 { 00256 unsigned int hash_val, bin_pos; 00257 register st_table_entry *ptr; 00258 00259 hash_val = do_hash(key, table); 00260 FIND_ENTRY(table, ptr, hash_val, bin_pos); 00261 00262 if (ptr == 0) { 00263 return 0; 00264 } 00265 else { 00266 if (value != 0) *value = ptr->record; 00267 return 1; 00268 } 00269 } |
|
|
|
00518 { 00519 register int c; 00520 00521 #ifdef HASH_ELFHASH 00522 register unsigned int h = 0, g; 00523 00524 while ((c = *string++) != '\0') { 00525 h = ( h << 4 ) + c; 00526 if ( g = h & 0xF0000000 ) 00527 h ^= g >> 24; 00528 h &= ~g; 00529 } 00530 return h; 00531 #elif HASH_PERL 00532 register int val = 0; 00533 00534 while ((c = *string++) != '\0') { 00535 val = val*33 + c; 00536 } 00537 00538 return val + (val>>5); 00539 #else 00540 register int val = 0; 00541 00542 while ((c = *string++) != '\0') { 00543 val = val*997 + c; 00544 } 00545 00546 return val + (val>>5); 00547 #endif 00548 } |
|
|
|
参照元 new_size(). |
|
初期値: { numcmp, numhash, } |
|
初期値: { strcmp, strhash, } |