commit a7124a9f8714eced815e0ab1d2e2936981f51e27 Author: InventorXtreme Date: Fri Mar 29 01:01:45 2024 -0400 first diff --git a/.clang-format b/.clang-format new file mode 100644 index 0000000..63540cd --- /dev/null +++ b/.clang-format @@ -0,0 +1,2 @@ +IndentWidth: 4 +ColumnLimit: 200 diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..c8b45f4 --- /dev/null +++ b/Makefile @@ -0,0 +1,12 @@ +CC = gcc +CFLAGS = -g +LDFLAGS = -g +LDLIBS = +objects = map.o + +map: $(objects) + +$(objects): %.o: %.c + +clean: + rm -f *.o map diff --git a/map.c b/map.c new file mode 100644 index 0000000..0d0377c --- /dev/null +++ b/map.c @@ -0,0 +1,123 @@ +#include +#include +#include +#include + +struct HashMapNode { + void *key; + void *value; + void *NextHashNodeInBucket; +}; +typedef struct HashMapNode HashMapNode; + +HashMapNode *NewHashMapNode(void *key, void *value) { + HashMapNode *newnode = malloc(sizeof(HashMapNode)); + newnode->key = key; + newnode->value = value; + return newnode; +} + +struct Bucket { + HashMapNode *firstone; +}; +typedef struct Bucket Bucket; + +struct HashMap { + int (*HashFunction)(void *); + bool (*KeyCompare)(void *, void *); + Bucket *buckets; + int size; +}; +typedef struct HashMap HashMap; + +HashMap MakeHashMap(int (*HashFunction)(void *), bool (*KeyCompare)(void *, void *), int size) { + HashMap hm; + hm.size = size; + hm.KeyCompare = KeyCompare; + hm.HashFunction = HashFunction; + hm.buckets = calloc(sizeof(Bucket), size); + return hm; +} + +int HashMapNodeStack(Bucket *bucket, HashMapNode *nodetoadd) { + if (bucket->firstone == NULL) { + bucket->firstone = nodetoadd; + return 0; + } + HashMapNode *currentnode = bucket->firstone; + while (currentnode->NextHashNodeInBucket != NULL) { + currentnode = currentnode->NextHashNodeInBucket; + } + currentnode->NextHashNodeInBucket = nodetoadd; + return 0; +} + +Bucket *HashMapGetBucketWithKey(HashMap *hm, void *key) { + int keyhash = hm->HashFunction(key); + int keyindex = keyhash % hm->size; + Bucket *bucketwithindex = &hm->buckets[keyindex]; + return bucketwithindex; +} + +HashMapNode *HashMapGetNodeWithKey(HashMap *hm, void *key) { + Bucket *bucketwithindex = HashMapGetBucketWithKey(hm, key); + HashMapNode *currentnode = bucketwithindex->firstone; + + if (currentnode == NULL) { + return NULL; + } + + while (!hm->KeyCompare(currentnode->key, key)) { // while the keys are not equal; + currentnode = currentnode->NextHashNodeInBucket; + + if (currentnode == NULL) { + return NULL; + } + } + return currentnode; +} + +void *HashMapGet(HashMap *hm, void *key) { + HashMapNode *node = HashMapGetNodeWithKey(hm, key); + if (node == NULL) { + return NULL; + } + return node->value; +} + +int HashMapSet(HashMap *hm, void *key, void *value) { + int keyhash = hm->HashFunction(key); + int keyindex = keyhash % hm->size; + + HashMapNode *existingnode = HashMapGetNodeWithKey(hm, key); + if (existingnode != NULL) { + existingnode->value = value; + } + + HashMapNode *newnode = NewHashMapNode(key, value); + HashMapNodeStack(HashMapGetBucketWithKey(hm, key), newnode); + + return 0; +} + +bool IntToIntCmp(void *x, void *y) { + if (*(int *)x == *(int *)y) { + return true; + } + return false; +} + +int IntHash(void *x) { return *(int *)x; } + +int *AllocInt(int x) { + int *spot = malloc(sizeof(int)); + *spot = x; + return spot; +} + +int main() { + HashMap inttointhashmap = MakeHashMap(IntHash, IntToIntCmp, 2); + + HashMapSet(&inttointhashmap, AllocInt(0), AllocInt(2)); + HashMapSet(&inttointhashmap, AllocInt(1), AllocInt(100)); +}