#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include <stdarg.h>
#include "coucal.h"
#include "murmurhash3.h"
#define coucal_assert | ( | HASHTABLE, | |||
EXP | ) | (void)( (EXP) || (coucal_assert_failed(HASHTABLE, #EXP, __FILE__, __LINE__), 0) ) |
#define coucal_debug coucal_nolog |
#define coucal_trace coucal_nolog |
#define DECLARE_LOG_FUNCTION | ( | NAME, | |||
LEVEL | ) |
static void NAME(const coucal hashtable, const char *format, ...) \ INTHASH_PRINTF_FUN(2, 3); \ static void NAME(const coucal hashtable, const char *format, ...) { \ va_list args; \ va_start(args, format); \ coucal_log(hashtable, LEVEL, format, args); \ va_end(args); \ }
#define HTS_INTHASH_USES_MURMUR 1 |
#define INTHASH_INLINE |
#define INTHASH_PRINTF_FUN | ( | FMT, | |||
ARGS | ) |
#define MIN_LG_SIZE 4 |
Minimum value for lg_size.
#define MIN_POOL_CAPACITY 256 |
Minimum value for pool.capacity.
#define RECOMPUTE_STRING | ( | S | ) |
#define RELOCATE_STRING | ( | S | ) |
do { \ if (S != NULL && S != the_empty_string) { \ const char *const src = (S); \ char *const dest = \ &hashtable->pool.buffer[hashtable->pool.size]; \ const size_t capacity = hashtable->pool.capacity; \ char *const max_dest = \ &hashtable->pool.buffer[capacity]; \ /* copy string */ \ coucal_assert(hashtable, dest < max_dest); \ dest[0] = src[0]; \ { \ size_t i; \ for(i = 1 ; src[i - 1] != '\0' ; i++) { \ coucal_assert(hashtable, &dest[i] < max_dest); \ dest[i] = src[i]; \ } \ /* update pool size */ \ hashtable->pool.size += i; \ coucal_assert(hashtable, \ hashtable->pool.size <= capacity); \ } \ /* update source */ \ S = dest; \ count++; \ } \ } while(0)
#define STASH_SIZE 16 |
Size of auxiliary stash.
#define UINT_64_FORMAT "lld" |
void coucal_add | ( | coucal | hashtable, | |
coucal_key_const | name, | |||
intptr_t | value | |||
) |
Alias to coucal_write()
void coucal_add_pvoid | ( | coucal | hashtable, | |
coucal_key_const | name, | |||
void * | value | |||
) |
INTHASH_INLINE coucal_hashkeys coucal_calc_hashes | ( | coucal | hashtable, | |
coucal_key_const | value | |||
) |
Compute a hash of a key for the hashtable 'hashtable'. The returned hash is suitable for use with coucal_fetch_value_hashes() Note: the default implementation uses coucal_hash_string()
int coucal_created | ( | coucal | hashtable | ) |
Was the hashtable successfully created ? Return non-zero value if the hashtable is valid.
int coucal_dec | ( | coucal | hashtable, | |
coucal_key_const | name | |||
) |
Decrement an entry value in the hashtable (or create a new entry with value -1 if it does not yet exist) Return non-zero value if the entry was added, zero if it was changed.
Delete a hashtable, freeing all entries.
struct_coucal_enum coucal_enum_new | ( | coucal | hashtable | ) |
Return a new enumerator. Note: deleting entries is safe while enumerating, but adding entries lead to undefined enumeration behavior (yet safe).
coucal_item* coucal_enum_next | ( | struct_coucal_enum * | e | ) |
Enumerate the next entry.
int coucal_exists | ( | coucal | hashtable, | |
coucal_key_const | name | |||
) |
Return non-zero value if the given entry exists.
INTHASH_INLINE coucal_value* coucal_fetch_value | ( | coucal | hashtable, | |
coucal_key_const | name | |||
) |
Fetch an entry value from the hashtable. Returns NULL if the entry could not be found. The returned pointer is only valid until next call to this library, and can be used for read or write operations.
coucal_value* coucal_fetch_value_hashes | ( | coucal | hashtable, | |
coucal_key_const | name, | |||
const coucal_hashkeys * | hashes | |||
) |
Fetch an entry value from the hashtable, given a name, and its hashes. Returns NULL if the entry could not be found. The returned pointer is only valid until next call to this library, and can be used for read or write operations. The hashes MUST have been computed using coucal_calc_hashes(), or by copying an existing hash during an enumeration. The use of a non-matching hash is safe, but will return NULL.
intptr_t coucal_get_intptr | ( | coucal | hashtable, | |
coucal_key_const | name | |||
) |
Read an integer entry from the hashtable. Return 0 if the entry could not be found.
const char* coucal_get_name | ( | coucal | hashtable | ) |
Get the hashtable name, for degugging purpose. Return NULL if no name was defined.
void* coucal_get_pvoid | ( | coucal | hashtable, | |
coucal_key_const | name | |||
) |
Read a pointer entry from the hashtable and returns its value. Return NULL if the entry could not be found.
coucal_hashkeys coucal_hash_data | ( | const void * | data, | |
size_t | size | |||
) |
Compute a hash, given an opaque buffer.
size_t coucal_hash_size | ( | void | ) |
Return the library hash size compiled. The returned value MUST match COUCAL_HASH_SIZE.
INTHASH_INLINE coucal_hashkeys coucal_hash_string | ( | const char * | value | ) |
Compute a hash, given a string. This is the default function used for hashing keys, which are by default strings. This function uses coucal_hash_data() as backend.
int coucal_inc | ( | coucal | hashtable, | |
coucal_key_const | name | |||
) |
Increment an entry value in the hashtable (or create a new entry with value 1 if it does not yet exist) Return non-zero value if the entry was added, zero if it was changed.
size_t coucal_memory_size | ( | coucal | hashtable | ) |
Return the memory size taken by the hashtable. (This does not take account of the possible memory taken by values)
coucal coucal_new | ( | size_t | size | ) |
Create a new hashtable, with initial bucket size of 'size'. If size is 0, use the default minimal bucket size. Return a non-NULL pointer upon success.
By default, keys are supposed to be ''-terminated strings, which are duplicated by the library (the passed pointer does not need to be persistent), and values are opaque pointers (or integers) which are copied "as is", without further processing. Use coucal_value_set_key_handler() and coucal_value_set_value_handler() to alter this default behavior.
size_t coucal_nitems | ( | coucal | hashtable | ) |
Return the number of items in the hashtable.
int coucal_read | ( | coucal | hashtable, | |
coucal_key_const | name, | |||
intptr_t * | intvalue | |||
) |
Read an integer entry from the hashtable. Return non-zero value upon success and sets intvalue.
int coucal_read_pvoid | ( | coucal | hashtable, | |
coucal_key_const | name, | |||
void ** | value | |||
) |
Read a pointer entry from the hashtable. Return non-zero value upon success and sets value.
int coucal_read_value | ( | coucal | hashtable, | |
coucal_key_const | name, | |||
coucal_value * | value | |||
) |
Read an entry from the hashtable. Return non-zero value upon success and sets value.
int coucal_readptr | ( | coucal | hashtable, | |
coucal_key_const | name, | |||
intptr_t * | intvalue | |||
) |
Same as coucal_read(), but return 0 is the value was zero.
int coucal_remove | ( | coucal | hashtable, | |
coucal_key_const | name | |||
) |
Remove an entry from the hashtable Return non-zero value if the entry was removed, zero otherwise.
void coucal_set_assert_handler | ( | coucal | hashtable, | |
t_coucal_loghandler | log, | |||
t_coucal_asserthandler | fatal, | |||
coucal_opaque | arg | |||
) |
Set assertion failure handler. log: handler called upon serious programming error fatal: handler called upon serious programming error
void coucal_set_global_assert_handler | ( | t_coucal_loghandler | log, | |
t_coucal_asserthandler | fatal | |||
) |
Set default global assertion failure handler. The handler will be used if no specific handler was defined in the hashtable itself. log: handler called upon serious error log (opaque argument is the hashtable itself) fatal: handler called upon serious programming error (opaque argument is the hashtable itself)
void coucal_set_name | ( | coucal | hashtable, | |
coucal_key_const | name | |||
) |
Set the hashtable name, for degugging purpose. name: the hashtable name (ASCII or UTF-8)
void coucal_set_print_handler | ( | coucal | hashtable, | |
t_coucal_printkeyhandler | key, | |||
t_coucal_printvaluehandler | value, | |||
coucal_opaque | arg | |||
) |
Set pretty print loggers (debug). Both handlers must return a string pointer which shall be valid until the next call. Both key and value pointers shall be valid at the same time. name: handler called to print the string representation of the name value: handler called to print the string representation of the value
If 'flag' is non-zero, calls coucal_value_set_value_handler() with default system free() handler function, otherwise, free the value handlers.
void coucal_value_set_key_handler | ( | coucal | hashtable, | |
t_coucal_duphandler | dup, | |||
t_coucal_key_freehandler | free, | |||
t_coucal_hasheshandler | hash, | |||
t_coucal_cmphandler | equals, | |||
coucal_opaque | arg | |||
) |
Set handlers for keys. dup: handler called to duplicate a key. if NULL, the internal pool is used. free: handler called to free a key. if NULL, the internal pool is used. hash: hashing handler, called to hash a key. if NULL, the default hash function is used. equals: comparison handler, returning non-zero value when two keys are identical. if NULL, the default comparison function is used. arg: opaque custom argument to be used by functions. Handler(s) MUST NOT be changed once elements have been added.
void coucal_value_set_value_handler | ( | coucal | hashtable, | |
t_coucal_value_freehandler | free, | |||
coucal_opaque | arg | |||
) |
Set handlers for values. free: this handler will be called when a value is to be removed from the hashtable. if NULL, values won't be free'd. arg: opaque custom argument to be used by functions. Handler(s) MUST NOT be changed once elements have been added.
int coucal_write | ( | coucal | hashtable, | |
coucal_key_const | name, | |||
intptr_t | value | |||
) |
Write an integer entry to the hashtable. Return non-zero value if the entry was added, zero if it was replaced.
int coucal_write_pvoid | ( | coucal | hashtable, | |
coucal_key_const | name, | |||
void * | value | |||
) |
Write a pointer entry to the hashtable. Return non-zero value if the entry was added, zero if it was replaced.
int coucal_write_value | ( | coucal | hashtable, | |
coucal_key_const | name, | |||
coucal_value_const | value | |||
) |
Write an entry to the hashtable. Return non-zero value if the entry was added, zero if it was replaced.