Previous: , Up: Data structures   [Contents][Index]


5.2.3.2 Hashtable

A hashtable associates keys of arbitrary size and content with values. This data structure is also called associative array sometimes because you use keys in order to access values instead of numbers. You cannot store two values associated with the same key. The values can have any simple C types like integers or pointers.

Function: svz_hash_t * svz_hash_create (size_t size, svz_free_func_t destroy)

Create a new hash table with an initial capacity size. Return a non-zero pointer to the newly created hash. The size is calculated down to a binary value. The destroy callback specifies an element destruction callback for use by svz_hash_clear and svz_hash_destroy for each value. If no such operation should be performed the argument must be NULL.

Function: svz_hash_t * svz_hash_configure (svz_hash_t *hash, size_t (* keylen) (const char *) , unsigned long (* code) (const char *) , int (* equals) (const char *, const char *)

Set the internal keylen, code and and equals functions for hash table hash. Return hash.

keylen takes const char *data and returns size_t, the number of bytes in data representing the key.

code takes const char *data and returns unsigned long.

equals takes const char *data1, const char *data2 and returns int, which should be non-zero if equal.

As a special case, a NULL value means don’t set that function, leaving it to its default value.

Function: void svz_hash_destroy (svz_hash_t *hash)

Destroy the existing hash table hash, svz_freeing all keys within the hash, the hash table and the hash itself. If a non-NULL element destruction callback was specified to svz_hash_create, that function is called on each value.

Function: void * svz_hash_delete (svz_hash_t *hash, const char *key)

Delete an existing entry accessed via a key from the hash table hash. Return NULL if there is no such key, otherwise the previous value.

Function: void * svz_hash_put (svz_hash_t *hash, const char *key, void *value)

Add a new element consisting of key and value to hash. When key already exists, replace and return the old value. Note: This is sometimes the source of memory leaks.

Function: void * svz_hash_get (const svz_hash_t *hash, const char *key)

Return the value associated with key in the hash table hash, or NULL if there is no such key.

Function: void svz_hash_foreach (svz_hash_do_t *func, svz_hash_t *hash, void *closure)

Iterate func over each key/value pair in hash. func is called with three void * args: the key, the value and the opaque (to svz_hash_foreach) closure.

Function: size_t svz_hash_size (const svz_hash_t *hash)

Return the number of keys in the hash table hash. If hash is NULL, return zero.

Function: char * svz_hash_contains (const svz_hash_t *hash, void *value)

Return the key associated with value in the hash table hash, or NULL if there is no such value.

Function: int svz_hash_exists (const svz_hash_t *hash, char *key)

Return non-zero if key is stored within the hash table hash, otherwise zero. This function is useful when you cannot tell whether the return value of svz_hash_get (== NULL) indicates a real value in the hash or a non-existing hash key.


Previous: Array, Up: Data structures   [Contents][Index]