Hash maps

Constants

HASH_MAP_DUPLICATE, HASH_MAP_NOT_FOUND, HASH_MAP_OK


hm_ functions return codes

VALUES

#define HASH_MAP_OK 0
#define HASH_MAP_DUPLICATE  -1
#define HASH_MAP_NOT_FOUND  -2

HM_WALKER_CONTINUE, HM_WALKER_REMOVE, HM_WALKER_STOP


Walker flags for hash_map_walk. Returned by walker func.

VALUES

#define HM_WALKER_CONTINUE  0
#define HM_WALKER_STOP  0x01
#define HM_WALKER_REMOVE    0x02

Functions

hm_item_ptr

public

STATIC_INLINE void* hm_item_ptr(hm_item_t* obj, ptrdiff_t off) 
STATIC_INLINE void* hm_item_ptr(hm_item_t* obj, ptrdiff_t off) 

hm_get_key

public

STATIC_INLINE hm_key_t* hm_get_key(hashmap_t* hm, hm_item_t* obj) 

hash_map_init

public


Initialize static hash map.

Because hash map usually a global object, it doesn't
support formatted names

ARGUMENTS

LIBEXPORT void hash_map_init(hashmap_t* hm, const char* name)

hash_map_create

public


Create dynamic hashmap

ARGUMENTS

LIBEXPORT hashmap_t* hash_map_create(hashmap_t* base, const char* namefmt, ...)

hash_map_destroy

public


Destroy hash map

It shouldn't contain objects (or assertion will rise)

LIBEXPORT void hash_map_destroy(hashmap_t* hm)

hash_map_insert

public


Insert element into hash map

ARGUMENTS

RETURN VALUES
HASH_MAP_OK if object was successfully inserted or HASH_MAP_DUPLICATE if object with same key (not hash!) already exists in hash map

LIBEXPORT int  hash_map_insert(hashmap_t* hm, hm_item_t* object)

hash_map_remove

public


Remove element from hash map

ARGUMENTS

RETURN VALUES
HASH_MAP_OK if object was successfully remove or HASH_MAP_NOT_FOUND if object is not

LIBEXPORT int  hash_map_remove(hashmap_t* hm, hm_item_t* object)

hash_map_find_nolock

public


Find object in hash map by key without locking hashmap
call it only from walker context!

LIBEXPORT void* hash_map_find_nolock(hashmap_t* hm, const hm_key_t* key)

hash_map_find

public


Find object in hash map by key

LIBEXPORT void* hash_map_find(hashmap_t* hm, const hm_key_t* key)

hash_map_walk

public


Walk over objects in hash map and execute func for each object.

Func proto: int (func)(void object, void* arg)
Function func may return HM_WALKER_CONTINUE or HM_WALKER_STOP.

For HM_WALKER_STOP, hash_map_walk will stop walking over hash map and return
current object

ARGUMENTS

RETURN VALUES
NULL or object where func returned STOP

NOTES
because of nature of hash maps, walking order is undefined

LIBEXPORT void* hash_map_walk(hashmap_t* hm, hm_walker_func func, void* arg)

hm_string_hash

public


Calculate hash for strings

LIBEXPORT unsigned hm_string_hash(const hm_key_t* str, unsigned mask)

DECLARE_HASH_MAP


Declare hash map

ARGUMENTS

#define DECLARE_HASH_MAP(name, is_indirect, type, size, key_field, next_field, hm_hash_body, hm_compare_body)

DECLARE_HASH_MAP_STRKEY


Same as DECLARE_HASH_MAP, but assumes that key field is string declared as char* (AAS)

#define DECLARE_HASH_MAP_STRKEY(name, type, size, key_field, next_field, mask)

Types

typedef void hm_item_t;

typedef void hm_key_t;

hashmap_t

typedef struct {
    size_t            hm_size;
    hm_item_t**     hm_heads;

    boolean_t        hm_indirect;
    int             hm_type;

    thread_mutex_t hm_mutex;

    ptrdiff_t hm_off_key;
    ptrdiff_t hm_off_next;

    char* hm_name;

    unsigned (*hm_hash_key)(const hm_key_t* key);
    boolean_t (*hm_compare)(const hm_key_t* key1, const hm_key_t* key2);
} hashmap_t;