10 |
** if there was one. |
** if there was one. |
11 |
*/ |
*/ |
12 |
|
|
13 |
typedef struct bucket { |
typedef struct bucket |
14 |
char *key; |
{ |
15 |
void *data; |
char *key; |
16 |
struct bucket *next; |
void *data; |
17 |
|
struct bucket *next; |
18 |
} bucket; |
} bucket; |
19 |
|
|
20 |
/* |
/* |
26 |
** times as many nodes have been inserted as the table was created with. |
** times as many nodes have been inserted as the table was created with. |
27 |
*/ |
*/ |
28 |
|
|
29 |
typedef struct hash_table { |
typedef struct hash_table |
30 |
size_t size; |
{ |
31 |
size_t count; /* HW */ |
size_t size; |
32 |
bucket **table; |
size_t count; /* HW */ |
33 |
|
bucket **table; |
34 |
} hash_table; |
} hash_table; |
35 |
|
|
36 |
/* |
/* |
38 |
** the table's size to 0, and the pointer to the table to NULL. |
** the table's size to 0, and the pointer to the table to NULL. |
39 |
*/ |
*/ |
40 |
/* HW: returns NULL if it fails */ |
/* HW: returns NULL if it fails */ |
41 |
hash_table *hash_construct_table(hash_table *table,size_t size); |
hash_table *hash_construct_table(hash_table * table, size_t size); |
42 |
|
|
43 |
/* |
/* |
44 |
** Inserts a pointer to 'data' in the table, with a copy of 'key' as its |
** Inserts a pointer to 'data' in the table, with a copy of 'key' as its |
46 |
** associated data. |
** associated data. |
47 |
*/ |
*/ |
48 |
|
|
49 |
void *hash_insert(char *key,void *data,struct hash_table *table); |
void *hash_insert(char *key, void *data, struct hash_table *table); |
50 |
|
|
51 |
/* |
/* |
52 |
** Returns a pointer to the data associated with a key. If the key has |
** Returns a pointer to the data associated with a key. If the key has |
53 |
** not been inserted in the table, returns NULL. |
** not been inserted in the table, returns NULL. |
54 |
*/ |
*/ |
55 |
|
|
56 |
void *hash_lookup(char *key,struct hash_table *table); |
void *hash_lookup(char *key, struct hash_table *table); |
57 |
|
|
58 |
/* |
/* |
59 |
** Deletes an entry from the table. Returns a pointer to the data that |
** Deletes an entry from the table. Returns a pointer to the data that |
61 |
** properly. |
** properly. |
62 |
*/ |
*/ |
63 |
|
|
64 |
void *hash_del(char *key,struct hash_table *table); |
void *hash_del(char *key, struct hash_table *table); |
65 |
|
|
66 |
/* |
/* |
67 |
** Goes through a hash table and calls the function passed to it |
** Goes through a hash table and calls the function passed to it |
70 |
** with it. |
** with it. |
71 |
*/ |
*/ |
72 |
|
|
73 |
void hash_enumerate( hash_table *table,void (*func)(char *,void *)); |
void hash_enumerate(hash_table * table, void (*func) (char *, void *)); |
74 |
|
|
75 |
/* HW: same as above, but sorted output ( sorted on 'key') */ |
/* HW: same as above, but sorted output ( sorted on 'key') */ |
76 |
int hash_sorted_enum( hash_table *table, void(*func)(char *, void*)); |
int hash_sorted_enum(hash_table * table, void (*func) (char *, void *)); |
77 |
|
|
78 |
/* |
/* |
79 |
** Frees a hash table. For each node that was inserted in the table, |
** Frees a hash table. For each node that was inserted in the table, |
87 |
** any function with the data. ) |
** any function with the data. ) |
88 |
*/ |
*/ |
89 |
|
|
90 |
void hash_free_table(hash_table *table, void (*func)(void *)); |
void hash_free_table(hash_table * table, void (*func) (void *)); |
91 |
|
|
92 |
#endif /* HASH__H */ |
#endif /* HASH__H */ |