hashtable v0.1.1
A lightweight separate-chaining arena-backed hashtable in C
hashtable_api.h
Go to the documentation of this file.
1 
112 #ifndef HASHTABLE_API_H
113 #define HASHTABLE_API_H
114 
115 #include <stdint.h>
116 
117 
118 #define HASHTABLE_LIB_VERSION "v0.1.1"
119 
120 
121 #if !defined(HASHTABLE_SIZE_T_UINT16) && !defined(HASHTABLE_SIZE_T_UINT32) && !defined(HASHTABLE_SIZE_T_SYS)
122 #define HASHTABLE_SIZE_T_SYS
123 #endif // if !defined..
124 
125 
129 #if defined(HASHTABLE_SIZE_T_UINT16)
130 typedef uint16_t hashtable_size_t;
131 #elif defined(HASHTABLE_SIZE_T_UINT32)
132 typedef uint32_t hashtable_size_t;
133 #elif defined(HASHTABLE_SIZE_T_SYS)
134 typedef size_t hashtable_size_t;
135 #else
136 #error("HASHTABLE_SIZE_T option not defined")
137 #endif // if defined....
138 
139 
143 #define HASHTABLE_MIN_ARRAY_COUNT (10u)
144 
145 
152 #define HASHTABLE_MIN_BUFFER_SIZE(array_count) \
153  (sizeof(_keyval_pair_table_data_t) + sizeof(_keyval_pair_list_table_t) + \
154  ((array_count) * sizeof(_keyval_pair_list_t)) + \
155  sizeof(_keyval_pair_data_block_t))
156 
157 
166 typedef uint32_t (*hashtable_hashfunc_t)(const char *data, const hashtable_size_t size);
167 
168 
172 typedef struct
173 {
175  uint32_t array_count;
177 
178 
182 typedef struct
183 {
185  uint32_t entry_count;
186  size_t data_size;
187  void *table_data;
188 } hashtable_t;
189 
190 
205  void *buffer, size_t buffer_size);
206 
207 
222 int hashtable_insert(hashtable_t *table, const char *key, const hashtable_size_t key_size,
223  const char *value, const hashtable_size_t value_size);
224 
225 
237 int hashtable_remove(hashtable_t *table, const char *key, const hashtable_size_t key_size);
238 
239 
252 int hashtable_retrieve(hashtable_t *table, const char *key, const hashtable_size_t key_size,
253  char **value, hashtable_size_t *value_size);
254 
255 
266 int hashtable_has_key(hashtable_t *table, const char *key, const hashtable_size_t key_size);
267 
268 
278 int hashtable_bytes_remaining(hashtable_t *table, size_t *bytes_remaining);
279 
280 
297 int hashtable_next_item(hashtable_t *table, char **key, hashtable_size_t *key_size,
298  char **value, hashtable_size_t *value_size);
299 
300 
312 
313 
323 
324 
335 int hashtable_default_config(hashtable_config_t *config, size_t buffer_size);
336 
337 
347 
348 
353 #ifdef HASHTABLE_PACKED_STRUCT
354 #define _HASHTABLE_PACKED __attribute__((packed))
355 #else
356 #define _HASHTABLE_PACKED
357 #endif // HASHTABLE_PACKED_STRUCT
358 
359 
364 typedef struct _keyval_pair
365 {
366  struct _keyval_pair *next;
367  hashtable_size_t key_size;
368  hashtable_size_t value_size;
369  uint8_t data[];
370 } _HASHTABLE_PACKED _keyval_pair_t;
371 
372 
376 typedef struct
377 {
378  _keyval_pair_t *head;
379  _keyval_pair_t *tail;
380 } _keyval_pair_list_t;
381 
382 
386 typedef struct
387 {
388  _keyval_pair_list_t freelist;
389  size_t total_bytes;
390  size_t bytes_used;
391  uint8_t data[];
392 } _keyval_pair_data_block_t;
393 
394 
398 typedef struct
399 {
400  uint32_t array_count;
401  _keyval_pair_list_t table[];
402 } _keyval_pair_list_table_t;
403 
404 
436 typedef struct
437 {
438  _keyval_pair_list_table_t *list_table;
439  _keyval_pair_data_block_t *data_block;
440  uint32_t cursor_array_index;
441  uint32_t cursor_items_traversed;
442  _keyval_pair_t *cursor_item;
443  uint8_t cursor_limit;
444 } _keyval_pair_table_data_t;
445 
446 #endif // HASHTABLE_API_H
int hashtable_default_config(hashtable_config_t *config, size_t buffer_size)
int hashtable_create(hashtable_t *table, const hashtable_config_t *config, void *buffer, size_t buffer_size)
int hashtable_next_item(hashtable_t *table, char **key, hashtable_size_t *key_size, char **value, hashtable_size_t *value_size)
char * hashtable_error_message(void)
int hashtable_remove(hashtable_t *table, const char *key, const hashtable_size_t key_size)
int hashtable_bytes_remaining(hashtable_t *table, size_t *bytes_remaining)
int hashtable_retrieve(hashtable_t *table, const char *key, const hashtable_size_t key_size, char **value, hashtable_size_t *value_size)
int hashtable_reset_cursor(hashtable_t *table)
int hashtable_clear(hashtable_t *table)
int hashtable_insert(hashtable_t *table, const char *key, const hashtable_size_t key_size, const char *value, const hashtable_size_t value_size)
uint32_t(* hashtable_hashfunc_t)(const char *data, const hashtable_size_t size)
Definition: hashtable_api.h:166
size_t hashtable_size_t
Defines the type used to represent the size of keys and values stored in the hashtable.
Definition: hashtable_api.h:134
int hashtable_has_key(hashtable_t *table, const char *key, const hashtable_size_t key_size)
Configuration data for a single hashtable instance.
Definition: hashtable_api.h:173
hashtable_hashfunc_t hash
Hash function to use, must not be NULL.
Definition: hashtable_api.h:174
uint32_t array_count
Number of table array slots, must not be 0.
Definition: hashtable_api.h:175
All data for a single hashtable instance.
Definition: hashtable_api.h:183
uint32_t entry_count
Number of entries in the table.
Definition: hashtable_api.h:185
size_t data_size
Size of data section.
Definition: hashtable_api.h:186
void * table_data
Pointer to buffer for data section.
Definition: hashtable_api.h:187
hashtable_config_t config
Hashtable config data.
Definition: hashtable_api.h:184