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


Version
7.0.0

Typedefs

typedef int(* hash_function_t)(long key)

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 hash_map_entry_t

Public Members

struct hash_map_entry_t *next_p
long key
void *value_p
struct hash_map_bucket_t

Public Members

struct hash_map_entry_t *list_p
struct hash_map_t

Public Members

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