UYS  0.1
Useful Yet Simple (C libraries collection)
 All Data Structures Files Functions Typedefs Macros Pages
nhash.h
1 /*****************************************************************
2  *
3  * nhash.h
4  *****************************************************************/
5 
6 
7 #ifndef CHTBL_H
8 #define CHTBL_H
9 #include <stdlib.h>
10 #include <stdint.h>
11 #include "list.h"
12 
13 
14 /*************************************************************//**
15  * NHash, NHash_: structure for chained hash tables.
16  ****************************************************************/
17 typedef struct NHash_ {
18  int buckets;
19  void (*destroy)(void *data);
20  int size;
21  List *table;
22 } NHash;
23 
24 
25 typedef struct NHashEntry {
26  uint64_t key;
27  const void *val;
28 } NHashEntry;
29 
30 
31 
32 /*****************************************************************
33  *
34  * PUBLIC INTERFACE
35  *
36  *****************************************************************/
37 
38 
39 /*************************************************************//**
40  * nhash_init:
41  *
42  * Initializes the chained hash table specified by htbl. This operation must
43  * be called for a chained hash table before the hash table can be used
44  * with any other operation.
45  * Complexity: O(m), where m is the number of buckets in the hash table.
46  *
47  * \param buckets The number of buckets allocated in the hash table
48  * \param *h a user-defined hash function for hashing keys
49  * \param *match specifies a user-defined function to determine whether two
50  * keys match. It should return 1 if key1 is equal to key2, and 0
51  * otherwise.
52  * \param *destroy provides a way to free dynamically allocated data when
53  * nhash_destroy is called. For example, if the hash table contains data
54  * dynamically allocated using malloc, destroy should be set to free to
55  * free the data as the hash table is destroyed. For structured data
56  * containing several dynamically allocated members, destroy should be
57  * set to a user-defined function that calls free for each dynamically
58  * allocated member as well as for the structure itself. For a hash table
59  * containing data that should not be freed, destroy should be set to
60  * NULL.
61  *
62  * \return 0 if initializing the hash table is successful, or –1 otherwise.
63  ****************************************************************/
64 int nhash_init(NHash *htbl, int buckets,
65  void (*destroy)(void*data));
66 
67 
68 /*************************************************************//**
69  * nhash_destroy: Destroys the chained hash table specified by htbl.
70  *
71  * No other operations are permitted after calling nhash_destroy unless
72  * nhash_init is called again. The nhash_destroy operation removes all
73  * elements from a hash table and calls the function passed as destroy to
74  * nhash_init once for each element as it is removed,
75  * provided destroy was not set to NULL.
76  * Complexity: O(m), where m is the number of buckets in the hash table.
77  *
78  * \param *htbl pointer to a `NHash` struct
79  *
80  * \return None
81  ****************************************************************/
82 void nhash_destroy(NHash *htbl);
83 
84 
85 NHashEntry* nhash_get(NHash *htbl, size_t nk, ...);
86 
87 /*************************************************************//**
88  * nhash_insert: Inserts an element into the chained hash table `htbl`.
89  *
90  * The new element contains a pointer to data, so the memory referenced by
91  * data should remain valid as long as the element remains in the hash table.
92  * It is the responsibility of the caller to manage the storage associated
93  * with data.
94  * Complexity: O(1)
95  *
96  * \param *htbl pointer to a `NHash` struct
97  * \param *data pointer to the data that will be referenced by the new element
98  *
99  * \return 0 if inserting the element is successful, 1 if the element is
100  * already in the hash table, or –1 otherwise.
101  ****************************************************************/
102 int nhash_insert(NHash *htbl, const void *val, size_t nk, ...);
103 
104 
105 /*************************************************************//**
106  * nhash_remove: Removes the element matching data from `htbl`.
107  *
108  * Upon return, data points to the data stored in the element that
109  * was removed. It is the responsibility of the caller to manage the storage
110  * associated with the data.
111  * Complexity: O(1)
112  *
113  * \param *htbl pointer to a `NHash` struct
114  * \param **data pointer to the pointer where a reference to the data from
115  * the removed element will be stored.
116  *
117  * \return 0 if removing the element is successful, or –1 otherwise.
118  ****************************************************************/
119 void* nhash_remove(NHash *htbl, size_t nk, ...);
120 
121 
122 void* nhash_pop(NHash *htbl);
123 
124 /*************************************************************//**
125  * nhash_grow: Grow the chained hash table to twice its current size.
126  *
127  * In practice a new table is created, every element is removed from the
128  * former table and insert into the new one with a new hash fitting the
129  * current size.
130  * Complexity: O(n), where n is the number of elements in the hash table.
131  *
132  * \param *htbl pointer to a `NHash` struct
133  *
134  * \return 0 if the element is found in the hash table, or –1 otherwise.
135  ****************************************************************/
136 void nhash_grow(NHash *htbl);
137 
138 
139 /*************************************************************//**
140  * nhash_size: evaluates to the number of elements in `htbl`.
141  *
142  * Complexity: O(1)
143  *
144  * \param *htbl pointer to a `NHash` struct
145  *
146  * \return Number of elements in the hash table.
147  ****************************************************************/
148 #define nhash_size(htbl) ((htbl)->size)
149 
150 
151 /*************************************************************//**
152  * nhash_loadfactor: evaluates to the number of elements in `htbl`.
153  *
154  * The load factor of a hash table is defined as:
155  * α = n / m
156  * where n is the number of elements in the table and m is the number of
157  * positions into which elements may be hashed. The load factor of a chained
158  * hash table indicates the maximum number of elements we can expect to
159  * encounter in a bucket, assuming uniform hashing.
160  * Performance degrades significantly if we make the number of buckets in the
161  * table small relative to the number of elements we plan to insert.
162  * Python's dict implementation, for example, is said to grow when it's load
163  * factor reaches 2/3, to prevent performance degradation.
164  * Complexity: O(1)
165  *
166  * \param *htbl pointer to a `NHash` struct
167  *
168  * \return Load factor.
169  ****************************************************************/
170 #define nhash_loadfactor(htbl) (((float)((htbl)->size)) / ((htbl)->buckets))
171 
172 
173 #endif
Interface for the linked list implementation.
Definition: nhash.h:17
structure for linked lists.
Definition: list.h:56