1 |
astrand |
930 |
/* HW: HenkJan Wolthuis, 1997 */ |
2 |
|
|
#ifndef HASH__H |
3 |
|
|
#define HASH__H |
4 |
|
|
|
5 |
|
|
#include <stddef.h> /* For size_t */ |
6 |
|
|
/* |
7 |
|
|
** A hash table consists of an array of these buckets. Each bucket |
8 |
|
|
** holds a copy of the key, a pointer to the data associated with the |
9 |
|
|
** key, and a pointer to the next bucket that collided with this one, |
10 |
|
|
** if there was one. |
11 |
|
|
*/ |
12 |
|
|
|
13 |
|
|
typedef struct bucket { |
14 |
|
|
char *key; |
15 |
|
|
void *data; |
16 |
|
|
struct bucket *next; |
17 |
|
|
} bucket; |
18 |
|
|
|
19 |
|
|
/* |
20 |
|
|
** This is what you actually declare an instance of to create a table. |
21 |
|
|
** You then call 'construct_table' with the address of this structure, |
22 |
|
|
** and a guess at the size of the table. Note that more nodes than this |
23 |
|
|
** can be inserted in the table, but performance degrades as this |
24 |
|
|
** happens. Performance should still be quite adequate until 2 or 3 |
25 |
|
|
** times as many nodes have been inserted as the table was created with. |
26 |
|
|
*/ |
27 |
|
|
|
28 |
|
|
typedef struct hash_table { |
29 |
|
|
size_t size; |
30 |
|
|
size_t count; /* HW */ |
31 |
|
|
bucket **table; |
32 |
|
|
} hash_table; |
33 |
|
|
|
34 |
|
|
/* |
35 |
|
|
** This is used to construct the table. If it doesn't succeed, it sets |
36 |
|
|
** the table's size to 0, and the pointer to the table to NULL. |
37 |
|
|
*/ |
38 |
|
|
/* HW: returns NULL if it fails */ |
39 |
|
|
hash_table *hash_construct_table(hash_table *table,size_t size); |
40 |
|
|
|
41 |
|
|
/* |
42 |
|
|
** Inserts a pointer to 'data' in the table, with a copy of 'key' as its |
43 |
|
|
** key. Note that this makes a copy of the key, but NOT of the |
44 |
|
|
** associated data. |
45 |
|
|
*/ |
46 |
|
|
|
47 |
|
|
void *hash_insert(char *key,void *data,struct hash_table *table); |
48 |
|
|
|
49 |
|
|
/* |
50 |
|
|
** Returns a pointer to the data associated with a key. If the key has |
51 |
|
|
** not been inserted in the table, returns NULL. |
52 |
|
|
*/ |
53 |
|
|
|
54 |
|
|
void *hash_lookup(char *key,struct hash_table *table); |
55 |
|
|
|
56 |
|
|
/* |
57 |
|
|
** Deletes an entry from the table. Returns a pointer to the data that |
58 |
|
|
** was associated with the key so the calling code can dispose of it |
59 |
|
|
** properly. |
60 |
|
|
*/ |
61 |
|
|
|
62 |
|
|
void *hash_del(char *key,struct hash_table *table); |
63 |
|
|
|
64 |
|
|
/* |
65 |
|
|
** Goes through a hash table and calls the function passed to it |
66 |
|
|
** for each node that has been inserted. The function is passed |
67 |
|
|
** a pointer to the key, and a pointer to the data associated |
68 |
|
|
** with it. |
69 |
|
|
*/ |
70 |
|
|
|
71 |
|
|
void hash_enumerate( hash_table *table,void (*func)(char *,void *)); |
72 |
|
|
|
73 |
|
|
/* HW: same as above, but sorted output ( sorted on 'key') */ |
74 |
|
|
int hash_sorted_enum( hash_table *table, void(*func)(char *, void*)); |
75 |
|
|
|
76 |
|
|
/* |
77 |
|
|
** Frees a hash table. For each node that was inserted in the table, |
78 |
|
|
** it calls the function whose address it was passed, with a pointer |
79 |
|
|
** to the data that was in the table. The function is expected to |
80 |
|
|
** free the data. Typical usage would be: |
81 |
|
|
** free_table(&table, free); |
82 |
|
|
** if the data placed in the table was dynamically allocated, or: |
83 |
|
|
** free_table(&table, NULL); |
84 |
|
|
** if not. ( If the parameter passed is NULL, it knows not to call |
85 |
|
|
** any function with the data. ) |
86 |
|
|
*/ |
87 |
|
|
|
88 |
|
|
void hash_free_table(hash_table *table, void (*func)(void *)); |
89 |
|
|
|
90 |
|
|
#endif /* HASH__H */ |