first
This commit is contained in:
commit
a7124a9f87
2
.clang-format
Normal file
2
.clang-format
Normal file
|
@ -0,0 +1,2 @@
|
||||||
|
IndentWidth: 4
|
||||||
|
ColumnLimit: 200
|
12
Makefile
Normal file
12
Makefile
Normal file
|
@ -0,0 +1,12 @@
|
||||||
|
CC = gcc
|
||||||
|
CFLAGS = -g
|
||||||
|
LDFLAGS = -g
|
||||||
|
LDLIBS =
|
||||||
|
objects = map.o
|
||||||
|
|
||||||
|
map: $(objects)
|
||||||
|
|
||||||
|
$(objects): %.o: %.c
|
||||||
|
|
||||||
|
clean:
|
||||||
|
rm -f *.o map
|
123
map.c
Normal file
123
map.c
Normal file
|
@ -0,0 +1,123 @@
|
||||||
|
#include <stdbool.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <string.h>
|
||||||
|
|
||||||
|
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));
|
||||||
|
}
|
Loading…
Reference in a new issue