Tinyhash
This is a library containing multiple C implementations of hashmap.
Loading...
Searching...
No Matches
C hashmap implementations

build lint

This is a library containing multiple C implementations of hashmap. The public API is deliberately simple and user-friendly.

Here are the different methods implemented:

📖 Build and run

For the build, you only need the following requirements:

To build the library, you can run the following commands.

mkdir build
cd build
cmake ..
make

If you want to install the library, you can run the following command.

make install

The following CMake build arguments are available to enable or disable options.

Name Description Default value
-DBUILD_TESTS Compile the test files **ON**
-DBUILD_STATIC Link as a static library (instead of a shared library) **OFF**
-DBUILD_DOC Build the documentation **OFF**

🤝 Contribute

If you want to help the project, you can follow the guidelines in CONTRIBUTING.md.

🧪 Tests

Make sure you run CMake with the -DBUILD_TESTS=ON flag. The generated Makefile will contain a special test target, so you can run the tests with the following command:

make test

📝 Documentation

Just make sure you run CMake with the -DBUILD_DOC=ON flag.

The Makefile all target will automatically build the documentation.

📎 Some examples

Here is a basic example of how you could use the hashmap.

With the root controller (high level)

#include <stdint.h>
#include <stdio.h>
#include <string.h>
#include <tinyhash/tinyhash.h>
typedef struct {
char name[10];
bool is_hydrated;
} person_t;
int main(int argc, const char *argv[]) {
bool success;
person_t person = {"James", true};
// Create a controller with the open addressing method
// Insert a new key value pair
success = th_put(&th, "key_1", strlen("key_1"), &person);
if (success == false) {
fprintf(stderr, "Unable to insert\n");
return 1;
}
// Get the last inserted value
person_t *james;
james = th_get(&th, "key_1", strlen("key_1"));
if (success == false) {
fprintf(stderr, "It does not exist\n");
return 1;
}
printf("name -> %s, is_hydrated -> %d, hashmap length -> %d\n", james->name,
james->is_hydrated, th_len(&th));
// Quick multiple insert with the same value (not important here)
th_put(&th, "key_2", strlen("key_2"), &person);
th_put(&th, "key_3", strlen("key_3"), &person);
th_put(&th, "key_4", strlen("key_4"), &person);
printf("hashmap length -> %d\n", th_len(&th));
person_t *p;
// Iterate with a for loop
for (it = th_begin_iterator(&th); it != NULL; th_iterator_next(&it)) {
p = it->value;
printf("[for] key -> %s, name -> %s\n", it->key->data, p->name);
}
// Iterate with a while loop
it = th_empty_iterator(&th);
while (th_iterator_next(&it) == true) {
p = it->value;
printf("[while] key -> %s, name -> %s\n", it->key->data, p->name);
}
// Delete an entry
success = th_delete(&th, "key_1", strlen("key_1"));
if (success == false) {
fprintf(stderr, "Unable to delete\n");
return 1;
}
// Verify that it doesnt exist anymore
james = th_get(&th, "key_1", strlen("key_1"));
if (james != NULL) {
fprintf(stderr, "The entry still exists\n");
return 1;
}
// Free the allocated memory
th_free(&th);
return 0;
}
bool th_iterator_next(th_iterator_t **ptr)
Try to get the next element. Free the iterator if it reachs the end.
Definition iterator.c:35
Represents an iterator that allow to iterate over a generic table.
Definition iterator.h:11
th_any_t value
Definition iterator.h:15
th_key_t * key
Definition iterator.h:14
th_any_t data
Definition key.h:17
Represent a hashmap controller.
Definition tinyhash.h:71
th_t th_create(th_method_t method)
Allocate then initialize a hashmap controller. based on the given method.
Definition tinyhash.c:39
th_iterator_t * th_empty_iterator(th_t *th)
Return an empty iterator.
Definition tinyhash.c:77
th_any_t th_get(th_t *th, th_any_t data, size_t data_size)
Returns a value from a hashmap. Return NULL if it doest not exist.
Definition tinyhash.c:53
bool th_delete(th_t *th, th_any_t data, size_t data_size)
Delete a key value pair from a hashmap. Return true on success.
Definition tinyhash.c:61
int th_len(th_t *th)
Get the hashmap length (total amount of key value pairs).
Definition tinyhash.c:81
void th_free(th_t *th)
Free a hashmap.
Definition tinyhash.c:67
bool th_put(th_t *th, th_any_t data, size_t data_size, th_any_t value)
Insert element within the hashmap. Return true on success.
Definition tinyhash.c:57
th_iterator_t * th_begin_iterator(th_t *th)
Return a pointer on an iterator with the first element.
Definition tinyhash.c:73
@ TH_OPEN_ADRESSING
Definition tinyhash.h:16

Without the root controller (lower level)

This is exactly the same logic as using the root controller, although the prefix of functions and types will change.

For example, to use the ‘separate chaining’ method, the prefix will be th_sc_table.f

#include <string.h>
#include <tinyhash/separate_chaining/table.h>
int main(int argc, const char *argv[]) {
th_sc_table_put(&table, "hello", strlen("hello"), (th_any_t)0);
// etc..
return 0;
}
bool th_sc_table_put(th_generic_table_t generic_table, th_any_t data, size_t data_size, th_any_t value)
Insert a value within an separate chaining table. Return true on success.
Definition table.c:183
void th_sc_table_init(th_sc_table_t *table)
Initialize a separate chaining table.
Definition table.c:13
Represent a separate chaining table.
Definition table.h:16
void * th_any_t
Represent any type of data.
Definition types.h:8