File: hash.c

package info (click to toggle)
rxp 1.2.3-1
  • links: PTS
  • area: main
  • in suites: woody
  • size: 436 kB
  • ctags: 966
  • sloc: ansic: 9,679; makefile: 127; sh: 3
file content (235 lines) | stat: -rw-r--r-- 4,573 bytes parent folder | download | duplicates (6)
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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
/* 
 * Copyright Richard Tobin 1995-9.
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>

#include "system.h"
#include "hash.h"

struct hash_table {
    int entries;
    int buckets;
    struct hash_entry **bucket;
};

static unsigned int hash(const void *key, int len);
static void rehash(HashTable table);
static HashEntry hash_lookup(HashTable table, const void *key, int key_len,
			     int *foundp, int add);
static int key_compare(const void *key1, int key1_len, 
		       const void *key2, int key2_len);
static void *key_copy(const void *key, int key_len);

/* 
 * Create a hash table.
 * init_size is the initial number of buckets, it doesn't have to be "right".
 */

HashTable create_hash_table(int init_size)
{
    int s, i;
    HashTable table;

    table = Malloc(sizeof(*table));
    if(!table)
	return 0;

    for(s = 256; s < init_size; s <<= 1)
	;

    table->entries = 0;
    table->buckets = s;
    table->bucket = Malloc(s * sizeof(*table->bucket));
    if(!table->bucket)
	return 0;

    for(i=0; i<s; i++)
	table->bucket[i] = 0;

    return table;
}

/* 
 * Free a hash table.
 */

void free_hash_table(HashTable table)
{
    int i;
    HashEntry entry, next;

    for(i=0; i<table->buckets; i++)
	for(entry = table->bucket[i]; entry; entry = next)
	{
	    next = entry->next;
	    Free(entry->key);
	    Free(entry);
	}

    Free(table->bucket);
    Free(table);
}

int hash_count(HashTable table)
{
    return table->entries;
}

HashEntry hash_find(HashTable table, const void *key, int key_len)
{
    return hash_lookup(table, key, key_len, 0, 0);
}

HashEntry hash_find_or_add(HashTable table, const void *key, int key_len,
			   int *foundp)
{
    return hash_lookup(table, key, key_len, foundp, 1);
}

static HashEntry hash_lookup(HashTable table, const void *key, int key_len,
			     int *foundp, int add)
{
    HashEntry *entry, new;
    unsigned int h = hash(key, key_len);

    for(entry = &table->bucket[h % table->buckets];
	*entry; 
	entry = &(*entry)->next)
	if(key_compare((*entry)->key, (*entry)->key_len, key, key_len) == 0)
	    break;

    if(foundp)
	*foundp = (*entry != 0);

    if(*entry == 0 && add == 0)
	return 0;

    if(*entry != 0)
	return *entry;

    if(table->entries > table->buckets)	/* XXX arbitrary! */
    {
	rehash(table);
	return hash_lookup(table, key, key_len, foundp, add);
    }

    new = Malloc(sizeof(*new));
    if(!new)
	return 0;

    new->key = key_copy(key, key_len);
    new->key_len = key_len;
    new->value = 0;
    new->next = 0;
    
    table->entries++;

    *entry = new;

    return new;
}

void hash_remove(HashTable table, HashEntry entry)
{
    unsigned int h = hash(entry->key, entry->key_len);
    HashEntry *e;

    for(e = &table->bucket[h % table->buckets]; *e; e = &(*e)->next)
	if(*e == entry)
	{
	    *e = entry->next;
	    Free(entry);
	    table->entries--;
	    return;
	}
    
    fprintf(stderr, "Attempt to remove non-existent entry from table\n");
    abort();
}

void hash_map(HashTable table, 
	      void (*function)(const HashEntryStruct *, void *), void *arg)
{
    int i;
    HashEntry entry;

    for(i=0; i<table->buckets; i++)
	for(entry = table->bucket[i]; entry; entry = entry->next)
	    (*function)(entry, arg);
}

static void rehash(HashTable table)
{
    HashTable new;
    unsigned h;
    int i;
    HashEntry entry, next, *chain;

    /* XXX Should collect some statistics here */

    new = create_hash_table(2 * table->buckets);
    if(!new)
	return;

    for(i=0; i<table->buckets; i++)
    {
	for(entry = table->bucket[i]; entry; entry = next)
	{
	    next = entry->next;
	    h = hash(entry->key, entry->key_len);
	    chain = &new->bucket[h % new->buckets];
	    entry->next = *chain;
	    *chain = entry;
	    new->entries++;
	}
    }

    assert(new->entries == table->entries);

    Free(table->bucket);
    *table = *new;
    Free(new);
}

/*
 * Chris Torek's hash function.  I don't know whether it's any good for
 * this...
 */

static unsigned int hash(const void *key, int len)
{
    const char *k = key;
    unsigned int h = 0;		/* should probably be 32 bits */
    int i;

    for(i=0; i<len; i++)
	h = (h << 5) + h + k[i];

    return h;
}

static int key_compare(const void *key1, int key1_len,
		       const void *key2, int key2_len)
{
    if(key1_len != key2_len)
	return -1;
    return memcmp(key1, key2, key1_len);
}

static void *key_copy(const void *key, int key_len)
{
    void *copy;

    copy = Malloc(key_len);
    if(!copy)
	return 0;

    memcpy(copy, key, key_len);

    return copy;
}