A more memory-efficient implementation of a hash table in C.
To include densehashtable and rustyc as libraries in your own CMake project, follow these steps:
-
Clone this repository:
git clone https://github.com/noahdominic/dense-hash-table.git -
Copy the
densehashtableandrustycdirectories into your project's directory structure. -
In your project's main CMakeLists.txt file, add the following lines:
add_subdirectory(densehashtable) add_subdirectory(rustyc) target_link_libraries(your_target densehashtable rustyc)
Replace
your_targetwith the name of your CMake target. -
Ensure that your project includes the necessary header files from
densehashtableandrustyc.
Here's a simple example demonstrating how to use the dense hash table:
#include <stdio.h>
#include "densehashtable.h"
int main() {
// Initialise a hash table with a capacity of 8 elements
struct DenseHashTable *hash_table = dense_hash_table_init();
// Insert key-value pairs
if (!dense_hash_table_insert(hash_table, "key1", 123).is_ok){
// Handle insertion failures here...
goto emergency_exit;
}
if (!dense_hash_table_insert(hash_table, "key2", 4635).is_ok){
// Handle insertion failures here...
goto emergency_exit;
}
// Retrieve values by keys
ResultOption result_handler = dense_hash_table_lookup(hash_table, "key1");
// Check for errors
if (!result_handler.is_ok) {
goto emergency_exit;
}
// Check if key is found
if (!result_handler.value.is_some) {
printf("Not found!\n");
}
const char value1 = result_handler.value.value;
result_handler = dense_hash_table_lookup(hash_table, "key2");
// Check for errors
if (!result_handler.is_ok) {
goto emergency_exit;
}
// Check if key is found
if (!result_handler.value.is_some) {
printf("Not found!\n");
}
const char value2 = result_handler.value.value;
printf("Value for key1: %s\n", value1);
printf("Value for key2: %s\n", value2);
emergency_exit:
// Clean up memory
dense_hash_table_destroy(hash_table);
return 0;
}At present, the dense hash table implementation provided in this repository has the following limitations:
-
Data Type Restriction: The hash table is designed to handle only
intdata types for keys and values. Using other data types may result in unexpected behavior. -
Dependency on a rudimentary replication of Rust's
ResultandOptiontypes: The library includes basic functionalities inspired by Rust'sResultandOptiontypes. Usage and behavior might differ from the original Rust features, and the addition of such may not be idiomatic C.
Contributions to enhance the implementation, add features, or fix issues are welcome! Please fork this repository and submit pull requests to contribute.
This project is licensed under the EU Public Licence v. 1.2. Please see the LICENCE file for details.