summary refs log tree commit diff
diff options
context:
space:
mode:
authorBaitinq <manuelpalenzuelamerino@gmail.com>2025-01-04 00:26:39 +0100
committerBaitinq <manuelpalenzuelamerino@gmail.com>2025-01-04 00:26:39 +0100
commit8225f75cdc56f8efe61efa78237ce3d06f10d3a1 (patch)
tree9ffd30c56c53a6cae9af6dbb09ce2e493c4400c2
parentanother test (diff)
downloadc-hashtable-8225f75cdc56f8efe61efa78237ce3d06f10d3a1.tar.gz
c-hashtable-8225f75cdc56f8efe61efa78237ce3d06f10d3a1.tar.bz2
c-hashtable-8225f75cdc56f8efe61efa78237ce3d06f10d3a1.zip
take initial capacity in init function
-rw-r--r--lib/hashtable.c3
-rw-r--r--lib/hashtable.h2
-rw-r--r--src/main.zig6
3 files changed, 5 insertions, 6 deletions
diff --git a/lib/hashtable.c b/lib/hashtable.c
index 00e05d7..2015b9c 100644
--- a/lib/hashtable.c
+++ b/lib/hashtable.c
@@ -30,10 +30,9 @@ static int hash(char* key, size_t bucket_len) {
 	return sum % bucket_len;
 }
 
-HashTable hashtable_init() {
+HashTable hashtable_init(size_t capacity) {
 	HashTableImpl* ht = (HashTableImpl*) malloc(sizeof(HashTableImpl));
 
-	int capacity = 8; 
 	ht->buckets = (HashTableBucket*) calloc(sizeof(HashTableBucket), capacity);
 	ht->capacity = capacity;
 	
diff --git a/lib/hashtable.h b/lib/hashtable.h
index 5bdcb08..3f4be0e 100644
--- a/lib/hashtable.h
+++ b/lib/hashtable.h
@@ -3,7 +3,7 @@
 
 void typedef *HashTable;
 
-HashTable hashtable_init();
+HashTable hashtable_init(size_t);
 
 int hashtable_deinit(HashTable*);
 
diff --git a/src/main.zig b/src/main.zig
index a49db18..3a94dee 100644
--- a/src/main.zig
+++ b/src/main.zig
@@ -7,7 +7,7 @@ const hashtable = @cImport({
 pub fn main() !void {
     std.debug.print("Testing hashmap!\n", .{});
 
-    var ht = hashtable.hashtable_init();
+    var ht = hashtable.hashtable_init(8);
     defer _ = hashtable.hashtable_deinit(&ht);
 
     const Example = struct {
@@ -24,7 +24,7 @@ pub fn main() !void {
 }
 
 test "simple test" {
-    var ht = hashtable.hashtable_init();
+    var ht = hashtable.hashtable_init(8);
     defer _ = hashtable.hashtable_deinit(&ht);
     const data: i32 = 4;
     _ = hashtable.hashtable_put(ht, @constCast("key"), @constCast(&data));
@@ -33,7 +33,7 @@ test "simple test" {
 }
 
 test "removing element" {
-    var ht = hashtable.hashtable_init();
+    var ht = hashtable.hashtable_init(8);
     defer _ = hashtable.hashtable_deinit(&ht);
     const data: i32 = 4;
     _ = hashtable.hashtable_put(ht, @constCast("key"), @constCast(&data));