aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorDanny van Kooten <dannyvankooten@users.noreply.github.com>2020-03-16 14:06:02 +0100
committerDanny van Kooten <dannyvankooten@users.noreply.github.com>2020-03-16 14:06:02 +0100
commit31e98ced014b9326ddfc990b003864df69cd4dcc (patch)
tree6a7b55b591c7152adac7110804c174d6dd05564d /src
parent19597032cdbaba1271e8134f865f56410d886c11 (diff)
downloadunja-31e98ced014b9326ddfc990b003864df69cd4dcc.tar.gz
unja-31e98ced014b9326ddfc990b003864df69cd4dcc.zip
return old values when inserting or removing from hashmap
Diffstat (limited to 'src')
-rw-r--r--src/hashmap.c39
-rw-r--r--src/hashmap.h10
2 files changed, 37 insertions, 12 deletions
diff --git a/src/hashmap.c b/src/hashmap.c
index 6797475..f99ba4c 100644
--- a/src/hashmap.c
+++ b/src/hashmap.c
@@ -22,16 +22,18 @@ struct hashmap *hashmap_new() {
return hm;
}
-/* insert key-value into hashmap */
-void hashmap_insert(struct hashmap *hm, char *key, void *value) {
+/* Inserts a key-value pair into the map. Returns NULL if map did not have key, old value if it did. */
+void *hashmap_insert(struct hashmap *hm, char *key, void *value) {
int pos = HASH(key);
struct node *head = hm->buckets[pos];
struct node *node = head;
+ void *old_value;
while (node) {
if (strcmp(node->key, key) == 0) {
+ old_value = node->value;
node->value = value;
- return;
+ return old_value;
}
node = node->next;
}
@@ -41,9 +43,10 @@ void hashmap_insert(struct hashmap *hm, char *key, void *value) {
node->value = value;
node->next = head;
hm->buckets[pos] = node;
+ return NULL;
}
-/* retrieve value by key */
+/* Returns a pointer to the value corresponding to the key. */
void *hashmap_get(struct hashmap *hm, char *key) {
int pos = HASH(key);
struct node *node = hm->buckets[pos];
@@ -58,7 +61,7 @@ void *hashmap_get(struct hashmap *hm, char *key) {
return NULL;
}
-/* retrieve value by key, handles dot notation for nested hashmaps */
+/* Retrieve pointer to value by key, handles dot notation for nested hashmaps */
void *hashmap_resolve(struct hashmap *hm, char *key) {
char tmp_key[64];
int i = 0;
@@ -83,8 +86,30 @@ void *hashmap_resolve(struct hashmap *hm, char *key) {
return hm;
}
-void hashmap_remove(char *key) {
- // TODO: Implement this
+/* Removes a key from the map, returning the value at the key if the key was previously in the map. */
+void *hashmap_remove(struct hashmap *hm, char *key) {
+ int pos = HASH(key);
+ struct node *node = hm->buckets[pos];
+ struct node *prev = NULL;
+ void *old_value;
+
+ while (node) {
+ if (strcmp(node->key, key) == 0) {
+ if (prev) {
+ prev->next = node->next;
+ } else {
+ hm->buckets[pos] = node->next;
+ }
+ old_value = node->value;
+ free(node);
+ return old_value;
+ }
+
+ node = node->next;
+ prev = node;
+ }
+
+ return NULL;
}
/* free hashmap related memory */
diff --git a/src/hashmap.h b/src/hashmap.h
index 369f594..be2b80e 100644
--- a/src/hashmap.h
+++ b/src/hashmap.h
@@ -4,9 +4,9 @@ struct hashmap {
struct node *buckets[HASHMAP_CAP];
};
-void hashmap_insert(struct hashmap *hm, char *key, void *value);
-void *hashmap_get(struct hashmap *hm, char *key);
-void hashmap_remove(char *key);
struct hashmap *hashmap_new();
-void hashmap_free(struct hashmap *hm);
-void *hashmap_resolve(struct hashmap *hm, char *key); \ No newline at end of file
+void *hashmap_insert(struct hashmap *hm, char *key, void *value);
+void *hashmap_get(struct hashmap *hm, char *key);
+void *hashmap_resolve(struct hashmap *hm, char *key);
+void *hashmap_remove(struct hashmap *hm, char *key);
+void hashmap_free(struct hashmap *hm); \ No newline at end of file