12#define NB_HASH_ELEMENTS 24
26void destroy_data(
void* ptr) {
36 LIST* keys_list = new_generic_list(NB_HASH_ELEMENTS + 1);
40 n_log(
LOG_INFO,
"Filling HashTable with %d random elements", NB_HASH_ELEMENTS);
41 for (
int it = 0; it < NB_HASH_ELEMENTS; it++) {
43 int randomizator = rand() % 4;
44 nstrprintf(nkey,
"key%d_%d", it, randomizator);
45 switch (randomizator) {
48 ht_put_int(htable,
_nstr(nkey), 666);
65 snprintf(str, 64,
"%s%d",
_nstr(nkey), rand() % 10);
78 n_log(
LOG_INFO,
"HT_FOREACH hash: %u, key:%s", node->hash_value,
_str(node->key));
82 n_log(
LOG_INFO,
"HT_FOREACH_R hash: %u, key:%s", node->hash_value,
_str(node->key));
88 n_log(
LOG_INFO,
"table size: %ld , table optimal size: %ld", htable->
size, optimal_size);
94 n_log(
LOG_ERR,
"Error when optimizing table %p", htable);
108 if (strncasecmp(
"key", node->
key, 3) == 0)
112 results =
ht_search(htable, &matching_nodes);
128 ht_put_int(htable,
"TestInt", 1);
131 HASH_INT_TYPE ival = -1;
133 ht_get_int(htable,
"TestInt", &ival);
#define Malloc(__ptr, __struct, __size)
Malloc Handler to get errors and set to 0.
#define _str(__PTR)
define true
#define Free(__ptr)
Free Handler to get errors.
#define _nstr(__PTR)
N_STR or "NULL" string for logging purposes.
char * key
string key of the node if any, else NULL
size_t size
size of the hash table
LIST * ht_search(HASH_TABLE *table, int(*node_is_matching)(HASH_NODE *node))
seach table for matching nodes
int destroy_ht(HASH_TABLE **table)
empty a table and destroy it
#define HT_FOREACH_R(__ITEM_, __HASH_, __ITERATOR,...)
ForEach macro helper.
HASH_TABLE * new_ht(size_t size)
Create a hash table with the given size.
int ht_get_table_collision_percentage(HASH_TABLE *table)
get table collision percentage (HASH_CLASSIC mode only)
int ht_get_double(HASH_TABLE *table, const char *key, double *val)
get double at 'key' from 'table'
int empty_ht(HASH_TABLE *table)
empty a table
LIST * ht_get_completion_list(HASH_TABLE *table, const char *keybud, size_t max_results)
get next matching keys in table tree
int ht_put_double(HASH_TABLE *table, const char *key, double value)
put a double value with given key in the targeted hash table
size_t ht_get_optimal_size(HASH_TABLE *table)
get optimal array size based on nb=(number_of_key*1.3) && if( !isprime(nb) )nb=nextprime(nb) (HASH_CL...
int ht_get_string(HASH_TABLE *table, const char *key, char **val)
get string at 'key' from 'table'
int ht_put_string(HASH_TABLE *table, const char *key, char *string)
put a string value (copy/dup) with given key in the targeted hash table
int ht_resize(HASH_TABLE **table, size_t size)
rehash table according to size (HASH_CLASSIC mode only)
HASH_TABLE * new_ht_trie(size_t alphabet_length, size_t alphabet_offset)
create a TRIE hash table with the alphabet_size, each key value beeing decreased by alphabet_offset
int ht_put_ptr(HASH_TABLE *table, const char *key, void *ptr, void(*destructor)(void *ptr))
put a pointer to the string value with given key in the targeted hash table
int ht_optimize(HASH_TABLE **table)
try an automatic optimization of the table
#define HT_FOREACH(__ITEM_, __HASH_,...)
ForEach macro helper.
structure of a hash table node
structure of a hash table
int list_push(LIST *list, void *ptr, void(*destructor)(void *ptr))
Add a pointer to the end of the list.
#define list_foreach(__ITEM_, __LIST_)
ForEach macro helper.
int list_destroy(LIST **list)
Empty and Free a list container.
Structure of a generic LIST container.
#define n_log(__LEVEL__,...)
Logging function wrapper to get line and func.
#define LOG_DEBUG
debug-level messages
#define LOG_ERR
error conditions
void set_log_level(const int log_level)
Set the global log level value ( static int LOG_LEVEL )
#define LOG_INFO
informational
void free_nstr_ptr(void *ptr)
Free a N_STR pointer structure.
#define free_nstr(__ptr)
free a N_STR structure and set the pointer to NULL
#define nstrprintf(__nstr_var, __format,...)
Macro to quickly allocate and sprintf to N_STR.
A box including a string and his lenght.
Hash functions and table.
List structures and definitions.
N_STR and string function declaration.
N_STR * rnd_str
string holder