7 array_init(struct array *array)
9 memset(array, 0, sizeof *array);
13 array_release(struct array *array)
19 array_add(struct array *array, int size)
29 while (alloc < array->size + size)
32 if (array->alloc < alloc) {
33 data = realloc(array->data, alloc);
40 p = array->data + array->size;
48 hashtable_init(struct hashtable *table, struct array *pool)
50 array_init(&table->buckets);
55 hashtable_release(struct hashtable *table)
57 array_release(&table->buckets);
61 hash_string(const char *key)
64 unsigned int hash = 0;
66 for (p = key; *p; p++)
67 hash = (hash * 617) ^ *p;
73 hashtable_lookup(struct hashtable *table, const char *key)
75 unsigned int mask, start, i;
79 pool = table->pool->data;
80 mask = table->buckets.alloc - 1;
81 start = hash_string(key) * sizeof(uint32_t);
83 for (i = 0; i < table->buckets.alloc; i += sizeof *b) {
84 b = table->buckets.data + ((start + i) & mask);
89 if (strcmp(key, &pool[*b]) == 0)
97 do_insert(struct hashtable *table, uint32_t value)
99 unsigned int mask, start, i;
103 key = (char *) table->pool->data + value;
104 mask = table->buckets.alloc - 1;
105 start = hash_string(key) * sizeof(uint32_t);
107 for (i = 0; i < table->buckets.alloc; i += sizeof *b) {
108 b = table->buckets.data + ((start + i) & mask);
117 add_to_string_pool(struct hashtable *table, const char *key)
122 len = strlen(key) + 1;
123 p = array_add(table->pool, len);
126 return p - (char *) table->pool->data;
130 hashtable_insert(struct hashtable *table, const char *key)
132 uint32_t value, *buckets, *b, *end;
135 alloc = table->buckets.alloc;
136 array_add(&table->buckets, 4 * sizeof *buckets);
137 if (alloc != table->buckets.alloc) {
138 end = table->buckets.data + alloc;
139 memset(end, 0, table->buckets.alloc - alloc);
140 for (b = table->buckets.data; b < end; b++) {
144 do_insert(table, value);
149 value = add_to_string_pool(table, key);
150 do_insert (table, value);
156 hashtable_tokenize(struct hashtable *table, const char *string)
163 token = hashtable_lookup(table, string);
167 return hashtable_insert(table, string);