3 typedef struct CHISE_HASH_TABLE_ENTRY
7 } CHISE_HASH_TABLE_ENTRY;
9 typedef struct CHISE_HASH_TABLE
12 CHISE_HASH_TABLE_ENTRY *data;
16 chise_make_hash_table (size_t size)
18 CHISE_HASH_TABLE* hash
19 = (CHISE_HASH_TABLE*)malloc (sizeof (CHISE_HASH_TABLE));
25 = (CHISE_HASH_TABLE_ENTRY*) malloc (sizeof (CHISE_HASH_TABLE_ENTRY)
27 if (hash->data == NULL)
34 memset (hash->data, 0, sizeof (CHISE_HASH_TABLE_ENTRY) * size);
39 chise_destroy_hash_table (CHISE_HASH_TABLE* hash)
48 /* derived from hashpjw, Dragon Book P436. */
50 chise_hash_c_string (const unsigned char *ptr)
57 hash = (hash << 4) + *ptr++;
58 g = hash & 0xf0000000;
60 hash = (hash ^ (g >> 24)) ^ g;
62 return hash & 07777777777;
66 typedef CHISE_HASH_TABLE CHISE_NAME_TABLE;
67 typedef CHISE_HASH_TABLE_ENTRY CHISE_NAME_TABLE_ENTRY;
70 chise_make_name_table ()
72 return chise_make_hash_table (32);
76 chise_destroy_name_table (CHISE_NAME_TABLE* table)
78 chise_destroy_hash_table (table);
82 chise_name_table_put (CHISE_NAME_TABLE* table,
83 unsigned char *key, void *value)
86 CHISE_NAME_TABLE_ENTRY* entry;
91 index = chise_hash_c_string (key) % table->size;
92 for (i = index; i < table->size; i++)
94 entry = &table->data[i];
95 if (entry->key == NULL)
97 size_t len = strlen (key);
99 entry->key = (unsigned char*)malloc (len + 1);
100 if (entry->key == NULL)
102 strcpy (entry->key, key);
103 entry->value = value;
106 else if (strcmp (entry->key, key) == 0)
108 entry->value = value;
112 if (chise_name_table_grow (table) == 0)
113 return chise_name_table_put (table, key, value);
118 chise_name_table_get (CHISE_NAME_TABLE* table,
122 CHISE_NAME_TABLE_ENTRY entry;
127 index = chise_hash_c_string (key) % table->size;
128 for (i = index; i < table->size; i++)
130 entry = table->data[i];
131 if (entry.key == NULL)
133 else if (strcmp (entry.key, key) == 0)
140 chise_name_table_grow (CHISE_NAME_TABLE* table)
142 CHISE_NAME_TABLE *new_table
143 = chise_make_hash_table (table->size * 2);
146 if (new_table == NULL)
149 for (i = 0; i < table->size; i++)
151 CHISE_NAME_TABLE_ENTRY entry = table->data[i];
152 if ( (entry.key != NULL) && (entry.value != NULL) )
155 = chise_name_table_put (new_table, entry.key, entry.value);
158 chise_destroy_hash_table (new_table);
163 table->size = new_table->size;
164 table->data = new_table->data;