2
** hash.c - functions for a hash table.
4
** Copyright (c) 1996, 1997, 1999 Thorsten Kukuk
6
** This file is part of the NYS YP Server.
8
** The YP Server is free software; you can redistribute it and/or
9
** modify it under the terms of the GNU General Public License
10
** version 2 as published by the Free Software Foundation.
12
** The NYS YP Server is distributed in the hope that it will be useful,
13
** but WITHOUT ANY WARRANTY; without even the implied warranty of
14
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15
** General Public License for more details.
17
** You should have received a copy of the GNU General Public
18
** License along with the NYS YP Server; see the file COPYING. If
19
** not, write to the Free Software Foundation, Inc., 675 Mass Ave,
20
** Cambridge, MA 02139, USA.
22
** Author: Thorsten Kukuk <kukuk@suse.de>
36
#define TABLESIZE 997 /*Should be a prime */
41
* Initialize a new hash table.
49
work = malloc (sizeof (hash_t *) * TABLESIZE);
52
fprintf (stderr, "Out of memory.\n");
56
for (i = 0; i < TABLESIZE; i++)
63
* hash_calc_key(const char* key)
65
* Calculates the key, returns it.
68
hash_calc_key (const char *key)
71
int length = strlen (key);
74
for (i = 0; i < length; i++)
75
hkey = (256 * hkey + key[i]) % TABLESIZE;
77
assert (hkey < TABLESIZE);
83
* hash_insert(hash_t **table, const char*, const char*)
85
* Complete re-write, to insert item into head of list
86
* at it's entry in the table.
89
hash_insert (hash_t **table, const char *key, const char *val)
94
assert (table != NULL);
98
hkey = hash_calc_key (key);
100
/* look for the item */
104
if (strcmp (work->key, key) == 0)
111
/* insert into head of list */
112
work = malloc (sizeof (hash_t));
115
fprintf (stderr, "Out of Memory.\n");
119
/* setup the new node */
120
work->key = strdup (key);
121
work->val = strdup (val);
124
if (table[hkey] != NULL)
126
work->next = table[hkey];
136
* hash_free(hash_t**)
138
* Deallocates all the structures.
142
hash_free (hash_t **table UNUSED)
144
/* XXX Not implementet yet! */
151
* hash_search(hash_t**, const char*)
153
* Looks for specified key, returns value if found,
158
hash_search (hash_t **table, const char *key)
163
assert (table != NULL);
164
assert (key != NULL);
166
hkey = hash_calc_key (key);
168
/* look for the key in the list */
172
if (strcmp (work->key, key) == 0)
183
* hash_delkey(hash_t**, const char* )
185
* Delete the item from the table.
188
hash_delkey (hash_t **table, const char *key)
194
assert (table != NULL);
195
assert (key != NULL);
197
hkey = hash_calc_key (key);
204
if (strcmp (work->key, key) == 0)
207
/* delete this node, and return? */
208
if (work == table[hkey])
209
table[hkey] = work->next;
211
prev->next = work->next;
228
* hash_first(hash_t**)
230
* Returns the first item in the hash table.
233
hash_first (hash_t **table)
237
for (i = 0; i < TABLESIZE; i++)
239
if (table[i] != NULL)
247
* hash_next(hash_t**, const char*)
249
* Returns the next item in the cache.
252
hash_next (hash_t **table, const char *key)
257
assert (table != NULL);
258
assert (key != NULL);
260
hkey = hash_calc_key (key);
262
/* look for the item */
266
if (strcmp (work->key, key) == 0)
274
/* at this point, we have seen the key:
275
* starting from here, return the first
276
* valid pointer we find
281
/* work is NULL, increment to next list. */
283
while (hkey < TABLESIZE)
285
if (table[hkey] != NULL)