From c312e74f95fcfe9bd1e91c092a2d987cb067a075 Mon Sep 17 00:00:00 2001 From: Katharina Fey Date: Sun, 14 Jul 2019 02:00:32 +0100 Subject: Implementing HASH node `remove_key` function --- bowl.c | 15 ++++++++++++++- bowl.h | 5 ++++- hash.c | 12 ++++++++++++ 3 files changed, 30 insertions(+), 2 deletions(-) diff --git a/bowl.c b/bowl.c index c1271a2..e515f6b 100644 --- a/bowl.c +++ b/bowl.c @@ -106,7 +106,20 @@ err_t bowl_remove(struct bowl *self, struct bowl *child) } } -err_t bowl_remove_key(struct bowl *self, size_t idx, struct bowl **prev) +err_t bowl_remove_key(struct bowl *self, char *key, struct bowl **prev) +{ + CHECK(self, INVALID_PARAMS) + CHECK(key, INVALID_PARAMS) + + switch(self->type) { + case LEAF: return INVALID_PARAMS; + case HASH: return hash_remove_key(self, key, prev); + + default: return INVALID_STATE; + } +} + +err_t bowl_remove_idx(struct bowl *self, size_t idx, struct bowl **prev) { CHECK(self, INVALID_PARAMS) CHECK((idx >= 0), INVALID_PARAMS) diff --git a/bowl.h b/bowl.h index ae7efe3..ae1321e 100644 --- a/bowl.h +++ b/bowl.h @@ -82,8 +82,11 @@ err_t bowl_swap_idx(struct bowl *, size_t idx, struct bowl *, struct bowl **); /// Remove a bowl node by it's pointer reference err_t bowl_remove(struct bowl *, struct bowl *); +/// Remove a specific key (relevant for HASH nodes) +err_t bowl_remove_key(struct bowl *, char *key, struct bowl **); + /// Removing a bowl node with a key -err_t bowl_remove_key(struct bowl *, size_t idx, struct bowl **); +err_t bowl_remove_idx(struct bowl *, size_t idx, struct bowl **); /// Cascade-free memory from a bowl node err_t bowl_free(struct bowl *); diff --git a/hash.c b/hash.c index 689bf18..5ee4f90 100644 --- a/hash.c +++ b/hash.c @@ -65,9 +65,21 @@ err_t hash_insert_key(struct bowl *self, char *key, struct bowl *child) err_t hash_remove_key(struct bowl *self, char *key, struct bowl **child) { + CHECK(self, INVALID_STATE) + CHECK(child, INVALID_STATE) CHECK(key, INVALID_STATE) + // Even though we're a HASH node, we use an array under the hood + struct bowl_arr *arr = self->_pl.array; + CHECK(arr, INVALID_STATE) + + size_t idx; + err_t e = _hash(key, arr->size, &idx); + if(e) return e; + + (*child) = arr->ptr[idx]; + arr->ptr[idx] = NULL; return OK; } -- cgit v1.2.3