/
hashtab.h
92 lines (74 loc) · 2.89 KB
/
hashtab.h
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
/**
* This code was originally written by Christopher Wellons
* and was copied from his lisp implementation "wisp" which
* can be found here:
* http://git.nullprogram.com/?p=wisp.git;a=summary
*/
/* hashtab.h - generic hashtable implementation for use anywhere */
#ifndef HASHTAB_H
#define HASHTAB_H
#include <stdlib.h>
typedef struct hashtab_node_t
{
void *key; /* key for the node */
void *value; /* value for this node */
struct hashtab_node_t *next; /* next node (open hashtable) */
} hashtab_node_t;
typedef struct hashtab_t
{
hashtab_node_t **arr;
size_t size; /* size of the hash */
int count; /* number if items in this table */
int (*hash_func) (void *, size_t); /* hash function */
} hashtab_t;
/* Iterator type for iterating through the hashtable. */
typedef struct hashtab_iter_t
{
/* key and value of current item */
void *key;
void *value;
/* bookkeeping data */
struct hashtab_internal_t
{
hashtab_t *hashtable;
hashtab_node_t *node;
int index;
} internal;
} hashtab_iter_t;
/* Initialize a new hashtable (set bookingkeeping data) and return a
* pointer to the hashtable. A hash function may be provided. If no
* function pointer is given (a NULL pointer), then the built in hash
* function is used. A NULL pointer returned if the creation of the
* hashtable failed due to a failed malloc(). */
hashtab_t *htb_init (size_t size,
int (*hash_func)
(void *key, size_t htb_size));
/* Fetch a value from table matching the key. Returns a pointer to
* the value matching the given key. */
void *htb_search (hashtab_t * hashtable, void *key);
/* Put a value into the table with the given key. Returns NULL if
* malloc() fails to allocate memory for the new node. */
void *htb_insert (hashtab_t * hashtable,
void *key, void *value);
/* Delete the given key and value pair from the hashtable. If the key
* does not exist, no error is given. */
void htb_remove (hashtab_t * hashtable, void *key);
/* Change the size of the hashtable. It will allocate a new hashtable
* and move all keys and values over. The pointer to the new hashtable
* is returned. Will return NULL if the new hashtable fails to be
* allocated. If this happens, the old hashtable will not be altered
* in any way. The old hashtable is destroyed upon a successful
* grow. */
hashtab_t *htb_grow (hashtab_t * hashtable, size_t new_size);
/* Free all resources used by the hashtable. */
void htb_destroy (hashtab_t * hashtable);
/* Initialize the given iterator. It will point to the first element
* in the hashtable. */
void htb_iter_init (hashtab_t * hashtable, hashtab_iter_t * ii);
/* Increment the iterator to the next element. The iterator key and
* value will point to NULL values when the iterator has reached the
* end of the hashtable. */
void htb_iter_inc (hashtab_iter_t * ii);
/* Default hashtable hash function. */
int htb_hash (void *key, size_t hashtab_size);
#endif