8.4. hash_map — Hash map

Source code: src/collections/hash_map.h, src/collections/hash_map.c

Test code: tst/collections/hash_map/main.c

Test coverage: src/collections/hash_map.c


Typedefs

typedef

Functions

int hash_map_init(struct hash_map_t *self_p, struct hash_map_bucket_t *buckets_p, size_t buckets_max, struct hash_map_entry_t *entries_p, size_t entries_max, hash_function_t hash)

Initialize hash map with given parameters.

Return
zero(0) or negative error code.
Parameters
  • self_p: Initialized hash map.
  • buckets_p: Array of buckets.
  • buckets_max: Number of entries in buckets_p.
  • entries_p: Array of empty entries.
  • entries_max: Number of entries in entries_p.
  • hash: Hash function.

int hash_map_add(struct hash_map_t *self_p, long key, void *value_p)

Add given key-value pair into hash map. Overwrites old value if the key is already present in map.

Return
zero(0) or negative error code.
Parameters
  • self_p: Initialized hash map.
  • key: Key to hash.
  • value_p: Value to insert for key.

int hash_map_remove(struct hash_map_t *self_p, long key)

Remove given key from hash map.

Return
zero(0) or negative error code.
Parameters
  • self_p: Initialized hash map.
  • key: Key to hash.

void *hash_map_get(struct hash_map_t *self_p, long key)

Get value for given key.

Return
Value for key or NULL if key was not found in the map.
Parameters
  • self_p: Initialized hash map.
  • key: Key to hash.

struct

Public Members

struct hash_map_entry_t *hash_map_entry_t::next_p
long hash_map_entry_t::key
void *hash_map_entry_t::value_p
struct

Public Members

struct hash_map_entry_t *hash_map_bucket_t::list_p
struct

Public Members

struct hash_map_bucket_t *hash_map_t::buckets_p
size_t hash_map_t::buckets_max
struct hash_map_entry_t *hash_map_t::entries_p
hash_function_t hash_map_t::hash