Lib-Hash is a light-weight C-library for hashmap. It can be used to store any primitive or user-defined datas. It provides very easy and intuitive API for creating, deleting, reading, writing and iterating through the elements in hash-table/map.
Under the hood, It maps from char* to void*. The reason behind this is to be
able to store any data type in the hash-table by casting the reference to it into
void*.
The hashing algorithm used in it is FNV-1A.
In order to use it in your project, simply clone the repo inside your project and include
libhash.h header file into the c program you want to use.
-
Go to your project Directory
cd my-project/ -
Clone the library inside the project
git clone https://github.com/therealsunx/libhash
-
Simply include the header file wherever you want to use
libhash#include "libhash/libhash.h"
And you're good to go.
Note: the data-members are not to be modified directly. Use provided methods to operate on data.
typedef struct {
char* key;
void* value;
hash_t __hash__;
} hpair_t;
typedef struct {
hpair_t* entries;
size_t __capacity__, __size__;
} hashmap_t;
typedef struct {
char* key;
void* value;
hashmap_t* __map__;
size_t __index__;
} hm_iterator_t;- Create a new hashmap
hashmap_t* hashmap_create(void);- Destroy/Free the hashmap
void hashmap_destroy(hashmap_t* map);- Get the number of items in hashmap
size_t hashmap_getCount(hashmap_t *map);- Get the current total capacity of hashmap
size_t hashmap_getCapacity(hashmap_t *map);- Get the item using
key. ReturnsNULLpointer if key is not in the hashmap
void* hashmap_get(hashmap_t* map, char* key);- Add a key-value pair in the map. Returns 1 if successful and 0 if the operation fails.
uint32_t hashmap_set(hashmap_t* map, char* key, void* value);- Removes an entry from the hashmap. If key is not in map, then nothing's changes.
void hashmap_pop(hashmap_t* map, char* key);- Returns an iterator for the hashmap
hm_iterator_t hashmap_iterator(hashmap_t* map);- Iterates over to the next entry in the provided hashmap's iterator. Returns 0 if no more entry left.
uint32_t hashmap_next(hm_iterator_t* iterator);if it returns 1, then you can access the key and value from the iterator.
