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.
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:
📝 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};
success =
th_put(&th,
"key_1", strlen(
"key_1"), &person);
if (success == false) {
fprintf(stderr, "Unable to insert\n");
return 1;
}
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));
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;
printf(
"[for] key -> %s, name -> %s\n", it->
key->
data, p->name);
}
printf(
"[while] key -> %s, name -> %s\n", it->
key->
data, p->name);
}
success =
th_delete(&th,
"key_1", strlen(
"key_1"));
if (success == false) {
fprintf(stderr, "Unable to delete\n");
return 1;
}
james =
th_get(&th,
"key_1", strlen(
"key_1"));
if (james != NULL) {
fprintf(stderr, "The entry still exists\n");
return 1;
}
return 0;
}
bool th_iterator_next(th_iterator_t **ptr)
Try to get the next element. Free the iterator if it reachs the end.
Represents an iterator that allow to iterate over a generic table.
Represent a hashmap controller.
th_t th_create(th_method_t method)
Allocate then initialize a hashmap controller. based on the given method.
th_iterator_t * th_empty_iterator(th_t *th)
Return an empty iterator.
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.
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.
int th_len(th_t *th)
Get the hashmap length (total amount of key value pairs).
void th_free(th_t *th)
Free a hashmap.
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.
th_iterator_t * th_begin_iterator(th_t *th)
Return a pointer on an iterator with the first element.
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[]) {
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.
void th_sc_table_init(th_sc_table_t *table)
Initialize a separate chaining table.
Represent a separate chaining table.
void * th_any_t
Represent any type of data.