Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create an implementation of a generic map (#13) #31

Open
wants to merge 21 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 9 additions & 9 deletions include/lib/list.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,27 +11,27 @@ struct list {
};

/** Initializes a new list */
void list_init(struct list *l);
void list_init(struct list* l);

/** Frees the list from memory */
void list_free(struct list *l);
void list_free(struct list* l);

/** Adds the given element to the list */
void list_add(struct list *l, void *elm);
void list_add(struct list* l, void* elm);

/** Removes the given element from the list */
void list_remove(struct list *l, void *elm);
void list_remove(struct list* l, void* elm);

/** Removes all elements from the list */
void list_clear(struct list *l);
void list_clear(struct list* l);

/** Returns true if the element is in the list, false otherwise */
bool list_contains(struct list *l, void *elm);
bool list_contains(struct list* l, void* elm);

/** Returns the number of elements in the list */
size_t list_size(struct list *l);
size_t list_size(struct list* l);

/** Places the first size number of elements of the list into the given array */
void list_to_array(struct list *l, void **buffer, size_t size);
void list_to_array(struct list* l, void** buffer, size_t size);

#endif //_LIST_H_
#endif //_LIST_H_
31 changes: 17 additions & 14 deletions include/lib/map.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,27 @@

#include "stdlib/stdtypes.h"

/** Data structure for a generic map */
struct map {
//TODO
/** Table which holds all entries. */
struct entry *table;
/** Amount of space used. */
size_t used;
/** Maximum space to allocate for entries */
size_t size;
};

/** Initializes a new hashtable */
/** Initializes a new hashmap. Returns `-1` if not allocated. */
void map_init(struct map *m);

/** Frees the hashtable from memory */
/** Resizes the hashmap. */
void map_resize(struct map *m, double resize_factor);
/** Frees the hashtable from memory. */
void map_free(struct map *m);

/** Adds the key/value pair to the hashtable */
void map_put(struct map *m, void *key, void *value);

/** Gets the value associated with the specified key */
void *map_get(struct map *m, void *key);

/** Returns true if the hashtable contains the key, false otherwise */
bool map_contains(struct map *m, void *key);
Altanis marked this conversation as resolved.
Show resolved Hide resolved
/** Adds the key/value pair to the hashtable. */
void map_put(struct map *m, void *key, void *value, size_t size);
/** Gets the value associated with the specified key. */
void *map_get(struct map *m, void *key, size_t size);
/** Checks if key exists in hashmap. */
bool map_contains(struct map *m, void *key, size_t size);

#endif //_MAP_H_
#endif //_MAP_H_
4 changes: 4 additions & 0 deletions include/stdlib/stdhash.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#include <stdlib/stdtypes.h>

/** FNV-1A hash for generic map. */
size_t fnv_1a(char *str, size_t size);
3 changes: 3 additions & 0 deletions include/stdlib/stdtypes.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@
#ifndef USER
/** Size type */
typedef unsigned int size_t;
/** Unsigned 64 bit integer */
typedef unsigned long long uint64_t;
Altanis marked this conversation as resolved.
Show resolved Hide resolved
/** nullptr */
#define NULL (void *) 0
#else
#include <stddef.h>
Expand Down
2 changes: 1 addition & 1 deletion src/lib/list.c
Original file line number Diff line number Diff line change
Expand Up @@ -86,4 +86,4 @@ void list_to_array(struct list *l, void **buffer, size_t size) {
buffer[i++] = n->data;
n = n->next;
}
}
}
61 changes: 61 additions & 0 deletions src/lib/map.c
Original file line number Diff line number Diff line change
@@ -1,2 +1,63 @@
#include "lib/map.h"
#include "stdlib/stdhash.h"
#include "stdlib/stdalloc.h"

/** Structures for a generic map. */
struct entry {
/** The key associated with the entry. */
void *key;
/** The value associated with the entry. */
void *value;
};

void map_init(struct map *m) {
/** Default 1MB of data. */
size_t size = 1024 * 1024;

m->table = calloc(size, sizeof(struct entry));
Altanis marked this conversation as resolved.
Show resolved Hide resolved
Altanis marked this conversation as resolved.
Show resolved Hide resolved
m->used = 0;
m->size = size;
}

void map_resize(struct map *m, double resize_factor) {
m->size *= resize_factor;
m->table = realloc(m->table, m->size * sizeof(struct entry));
}

void map_free(struct map *m) {
free(m->table);
}

void map_put(struct map *m, void *key, void *value, size_t size) {
if (m->used * 2 >= m->size) {
map_resize(m, 2);
}

size_t idx = fnv_1a(key, size) % m->size;
size_t start = idx;

while (m->table[idx].key != NULL) {
idx = (++idx) % m->size;
if (memcmp(key, m->table[idx].key, size) == 0) break;
}

m->table[idx].key = key;
m->table[idx].value = value;
m->used++;
}

void *map_get(struct map *m, void *key, size_t size) {
size_t idx = fnv_1a(key, size) % m->size;

if (m->table[idx].key == NULL) return NULL;
while (m->table[idx].key != NULL) {
if (memcmp(key, m->table[idx].key, size) == 0) return m->table[idx].value;
idx = (++idx) % m->size;
}

return NULL;
}

bool map_contains(struct map *m, void *k, size_t size) {
return map_get(m, k, size) != NULL;
}
10 changes: 10 additions & 0 deletions src/stdlib/stdalloc.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include "stdlib/stdalloc.h"
#include "stdlib/stdstr.h"

#include "alloc/heapalloc.h"
#include "alloc/pagealloc.h"
Expand All @@ -13,6 +14,15 @@ void *malloc(size_t size) {
return heap_malloc(current_heap, size);
}

void *calloc(size_t _NumOfElements, size_t _SizeOfElements) {
void* buffer = malloc(_NumOfElements * _SizeOfElements);

if (buffer == NULL) return NULL;

memset(buffer, 0, _NumOfElements * _SizeOfElements);
return buffer;
}

void *realloc(void *p, size_t size) {
return heap_realloc(current_heap, p, size);
}
Expand Down
12 changes: 12 additions & 0 deletions src/stdlib/stdhash.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#include <stdlib/stdtypes.h>

size_t fnv_1a(char *ptr, size_t size) {
size_t hash = 0x811C9DC5;

for (;size--;) {
hash ^= ptr[size];
hash *= 0x01000193;
}

return hash;
}