-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathhashtable.c
185 lines (151 loc) · 3.95 KB
/
hashtable.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
/* Nice hash table for Apache runtime. */
/* rsr note: shouldn't this whole pile of code be replaced with the
hash implementation that is part of the Apache runtime? Does this
code provide features not present in the normal Apache APR hash
routines? */
#include <apr.h>
#include <httpd.h>
#include "hashtable.h"
typedef struct {
const char *key;
void *val;
} HashBucket;
struct _HashTable {
int n;
int n_max;
HashBucket **buckets;
};
struct _HashTableIter {
const HashTable *ht;
int index;
};
/* This hash function, the choice of 2^k for the size, and the linear
search are all relatively unsophisticated. It should be good enough
for government use, though. */
static unsigned int
hash_func (const char *string)
{
unsigned int result;
int c;
int i;
result = 0;
for (i = 0; (c = ((unsigned char *)string)[i]) != '\0'; i++)
result += (result << 3) + c;
return result;
}
HashTable *
virgule_hash_table_new (apr_pool_t *p)
{
HashTable *result;
result = (HashTable *)apr_palloc (p, sizeof(HashTable));
result->n = 0;
result->n_max = 4;
result->buckets = (HashBucket **)apr_palloc (p, sizeof(HashBucket *) * result->n_max);
memset (result->buckets, 0, sizeof(HashBucket *) * result->n_max);
return result;
}
void *
virgule_hash_table_get (const HashTable *ht, const char *key)
{
unsigned int hash;
hash = hash_func (key) % ht->n_max;
while (ht->buckets[hash] != NULL)
{
if (!strcmp (ht->buckets[hash]->key, key))
return ht->buckets[hash]->val;
hash = (hash + 1) % ht->n_max;
}
return NULL;
}
static void
hash_table_insert_bucket (apr_pool_t *p, HashBucket **buckets, int n_max,
HashBucket *bucket)
{
unsigned int hash;
hash = hash_func (bucket->key) % n_max;
while (buckets[hash] != NULL)
hash = (hash + 1) % n_max;
buckets[hash] = bucket;
}
/* Internal insert function. Assumes that key is not already present. */
static void
hash_table_insert (apr_pool_t *p, HashBucket **buckets, int n_max,
const char *key, void *val)
{
HashBucket *bucket = (HashBucket *)apr_palloc (p, sizeof(HashBucket));
bucket->key = key;
bucket->val = val;
hash_table_insert_bucket (p, buckets, n_max, bucket);
}
void
virgule_hash_table_set (apr_pool_t *p, HashTable *ht, const char *key, void *val)
{
unsigned int hash;
int n_max;
hash = hash_func (key) % ht->n_max;
while (ht->buckets[hash] != NULL)
{
if (!strcmp (ht->buckets[hash]->key, key))
{
ht->buckets[hash]->val = val;
return;
}
hash = (hash + 1) % ht->n_max;
}
ht->n++;
n_max = ht->n_max;
if (ht->n > (n_max >> 1))
{
/* rehash */
int new_n_max = n_max << 1;
HashBucket **old_buckets = ht->buckets;
HashBucket **new_buckets = (HashBucket **)apr_palloc (p, sizeof(HashBucket *) * new_n_max);
int i;
memset (new_buckets, 0, sizeof(HashBucket *) * new_n_max);
for (i = 0; i < n_max; i++)
{
if (old_buckets[i] != NULL)
hash_table_insert_bucket (p, new_buckets, new_n_max,
old_buckets[i]);
}
hash_table_insert (p, new_buckets, new_n_max, key, val);
ht->n_max = new_n_max;
ht->buckets = new_buckets;
}
else
{
HashBucket *bucket = (HashBucket *)apr_palloc (p, sizeof(HashBucket));
ht->buckets[hash] = bucket;
bucket->key = key;
bucket->val = val;
}
}
HashTableIter *
virgule_hash_table_iter (apr_pool_t *p, const HashTable *ht)
{
HashTableIter *result;
result = (HashTableIter *)apr_palloc (p, sizeof(HashTableIter));
result->ht = ht;
result->index = 0;
return result;
}
int
virgule_hash_table_iter_get (HashTableIter *iter, const char **pkey, void **pval)
{
const HashTable *ht = iter->ht;
for (; iter->index < ht->n_max; iter->index++)
{
if (ht->buckets[iter->index] != NULL)
{
*pkey = ht->buckets[iter->index]->key;
*pval = ht->buckets[iter->index]->val;
return 1;
}
}
return 0;
}
void
virgule_hash_table_iter_next (HashTableIter *iter)
{
iter->index++;
}