This project implements a simple hashtable in C and includes unit tests to verify its functionality.
- GCC (GNU Compiler Collection) or any compatible C compiler
- Make (build automation tool)
hashtable.c: Implementation of the hashtable.hashtable.h: Header file for the hashtable.test_hashtable.c: Unit tests for the hashtable.Makefile: Build script for the project.
To build the project, run the following command:
makeThis will generate:
- A static library: libhashmap.a
- A dynamic library: libhashmap.dll (on Windows) or libhashmap.so (on Linux)
- A test executable: test_hashtable.exe (on Windows) or test_hashtable (on Linux)
make run_testThis will compile and run the test_hashtable.c file, which contains unit tests for the hashtable implementation.
- hash_table* hash_table_init(size_t capacity, hashfunc *hf): Creates a new hashtable.
- bool hash_table_insert(hash_table *ht, const char *key, void *obj): Inserts a key-value pair into the hashtable.
- void* hash_table_get(hash_table *ht, const char *key): Retrieves the value associated with a key.
- void* hash_table_delete(hash_table *ht, const char *key): Removes a key-value pair from the hashtable.
- void hash_table_destroy(hash_table *ht, cleanupfunc *cf): Destroys the hashtable and frees memory.
#include <stdio.h>
#include "hashtable.h"
// Simple hash function
unsigned int simple_hash(const char *key) {
unsigned int hash = 0;
while (*key) {
hash = (hash << 5) + *key++;
}
return hash;
}
int main() {
// Initialize the hashtable
hash_table *ht = hash_table_init(10, simple_hash);
// Insert key-value pairs
hash_table_insert(ht, "key1", "value1");
hash_table_insert(ht, "key2", "value2");
// Retrieve values
printf("key1: %s\n", (char *)hash_table_get(ht, "key1"));
printf("key2: %s\n", (char *)hash_table_get(ht, "key2"));
// Delete a key-value pair
hash_table_delete(ht, "key1");
printf("key1 after deletion: %s\n", (char *)hash_table_get(ht, "key1"));
// Destroy the hashtable
hash_table_destroy(ht, NULL);
return 0;
}To link your program with the static library, use the following command:
gcc -o your_program your_program.c -L. -lhashmapTo link your program with the dynamic library, use the following command:
gcc -o your_program your_program.c -L. -lhashmap -Wl,-rpath,.This project is licensed under the GPL License. See the License file for more details.