diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/hashtable.c | 119 | ||||
-rw-r--r-- | src/hashtable.h | 16 | ||||
-rw-r--r-- | src/main.c | 54 | ||||
-rw-r--r-- | src/main.zig | 31 |
4 files changed, 31 insertions, 189 deletions
diff --git a/src/hashtable.c b/src/hashtable.c deleted file mode 100644 index 00e05d7..0000000 --- a/src/hashtable.c +++ /dev/null @@ -1,119 +0,0 @@ -#include <stddef.h> -#include <stdlib.h> -#include <stdio.h> -#include <string.h> - -#include "hashtable.h" - -struct { - char* key; - void* data; - int deleted; -} typedef HashTableData; - -struct { - HashTableData* data; - size_t length; -} typedef HashTableBucket; - -struct { - HashTableBucket* buckets; - size_t capacity; -} typedef HashTableImpl; - -static int hash(char* key, size_t bucket_len) { - int sum = 0; - while(*key != '\0') { - sum += *key++; - } - - return sum % bucket_len; -} - -HashTable hashtable_init() { - HashTableImpl* ht = (HashTableImpl*) malloc(sizeof(HashTableImpl)); - - int capacity = 8; - ht->buckets = (HashTableBucket*) calloc(sizeof(HashTableBucket), capacity); - ht->capacity = capacity; - - return (HashTable) ht; -} - -int hashtable_deinit(HashTable* ht) { - HashTableImpl* ht_impl = (HashTableImpl*) *ht; - - for (int i = 0; i < ht_impl->capacity; ++i) { - HashTableBucket bucket = ht_impl->buckets[i]; - free(bucket.data); - } - - free(ht_impl->buckets); - free(ht_impl); - ht = NULL; - return 0; -} - -void* hashtable_get(HashTable ht, char* key) { - HashTableImpl* ht_impl = (HashTableImpl*) ht; - - int index = hash(key, ht_impl->capacity); - - HashTableBucket bucket = ht_impl->buckets[index]; - - for (int i = 0; i < bucket.length; ++i) { - HashTableData data = bucket.data[i]; - if (!data.deleted && strcmp(data.key, key) == 0) return data.data; - } - - return NULL; -} - -int hashtable_put(HashTable ht, char* key, void* val) { - HashTableImpl* ht_impl = (HashTableImpl*) ht; - - int index = hash(key, ht_impl->capacity); - HashTableBucket* bucket = &ht_impl->buckets[index]; - - - //TODO: reuse the deleted? - for (int i = 0; i < bucket->length; ++i) { - HashTableData* data = &bucket->data[i]; - if (strcmp(data->key, key) == 0) { - data->data = val; - data->deleted = 0; - return 0; - } - } - - - //otherwise, realloc - bucket->length++; - bucket->data = realloc(bucket->data, sizeof(HashTableData) * bucket->length); - - HashTableData newData = { - .key = key, - .data = val, - .deleted = 0 - }; - bucket->data[bucket->length - 1] = newData; - - return 0; -} - -int hashtable_remove(HashTable ht, char* key) { - HashTableImpl* ht_impl = (HashTableImpl*) ht; - - int index = hash(key, ht_impl->capacity); - HashTableBucket* bucket = &ht_impl->buckets[index]; - - for (int i = 0; i < bucket->length; ++i) { - HashTableData* data = &bucket->data[i]; - if (strcmp(data->key, key) == 0) { - data->deleted = 1; - return 0; - } - } - - return 0; -} diff --git a/src/hashtable.h b/src/hashtable.h deleted file mode 100644 index 5bdcb08..0000000 --- a/src/hashtable.h +++ /dev/null @@ -1,16 +0,0 @@ -#ifndef __HASHTABLE_H__ -#define __HASHTABLE_H__ - -void typedef *HashTable; - -HashTable hashtable_init(); - -int hashtable_deinit(HashTable*); - -int hashtable_put(HashTable, char*, void*); - -int hashtable_remove(HashTable, char*); - -void* hashtable_get(HashTable, char*); - -#endif diff --git a/src/main.c b/src/main.c deleted file mode 100644 index aa15631..0000000 --- a/src/main.c +++ /dev/null @@ -1,54 +0,0 @@ -#include <stdio.h> -#include "hashtable.h" - -int main(int argc, char** argv) { - printf("Testing hashing:\n"); - - HashTable ht = hashtable_init(); - - char* res = (char*) hashtable_get(ht, "a"); - - printf("Result: %s\n", res); - - hashtable_put(ht, "aa", (void*)"x"); - - res = hashtable_get(ht, "aa"); - - printf("Result: %s\n", res); - - hashtable_put(ht, "b", (void*)"1"); - - printf("This should still be x\n"); - - res = hashtable_get(ht, "aa"); - - printf("Result: %s\n", res); - - res = hashtable_get(ht, "b"); - - printf("Result: %s\n", res); - - hashtable_remove(ht, "b"); - - res = hashtable_get(ht, "b"); - - printf("Result: %s\n", res); - - struct test { - int value; - }; - - struct test example = { - .value = 7 - }; - - hashtable_put(ht, "b", (void*)&example); - - struct test* res2 = hashtable_get(ht, "b"); - - printf("Result: %d\n", res2->value); - - hashtable_deinit(&ht); - - return 0; -} diff --git a/src/main.zig b/src/main.zig new file mode 100644 index 0000000..926f26b --- /dev/null +++ b/src/main.zig @@ -0,0 +1,31 @@ +const std = @import("std"); + +const hashtable = @cImport({ + @cInclude("hashtable.c"); +}); + +pub fn main() !void { + std.debug.print("Testing hashmap!\n", .{}); + + var ht = hashtable.hashtable_init(); + defer _ = hashtable.hashtable_deinit(&ht); + + const Example = struct { + data: i32 align(1), + }; + + const data = Example{ + .data = 7, + }; + + _ = hashtable.hashtable_put(ht, @constCast("key"), @constCast(&data)); + const res: *Example = @ptrCast(hashtable.hashtable_get(ht, @constCast("key"))); + std.debug.print("Result: {d}\n", .{res.*.data}); +} + +test "simple test" { + var list = std.ArrayList(i32).init(std.testing.allocator); + defer list.deinit(); // try commenting this out and see if zig detects the memory leak! + try list.append(42); + try std.testing.expectEqual(@as(i32, 42), list.pop()); +} |