From 4bdef58fe5965a511b2682f393499e9a34f4bd13 Mon Sep 17 00:00:00 2001 From: lifegpc Date: Thu, 17 Feb 2022 18:21:04 +0800 Subject: [PATCH] Update --- dict.h | 9 +++++++++ linked_list.h | 43 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 52 insertions(+) diff --git a/dict.h b/dict.h index e33863f..1d8e7f7 100644 --- a/dict.h +++ b/dict.h @@ -16,6 +16,15 @@ template size_t dict_count(struct Dict* d) { return linked_list_count((struct LinkedList>*)d); } +template +bool dict_delete_internal(struct dict_entry o, K key) { + return o.key == key; +} +template +bool dict_delete(struct Dict*& d, K key) { + if (!d) return false; + return linked_list_delete((struct LinkedList>*&)d, key, dict_delete_internal); +} template void dict_free(struct Dict*& d, void(*free_func)(struct dict_entry) = nullptr) { return linked_list_clear((struct LinkedList>*&)d, free_func); diff --git a/linked_list.h b/linked_list.h index 25604db..1e44129 100644 --- a/linked_list.h +++ b/linked_list.h @@ -153,6 +153,49 @@ bool linked_list_delete(struct LinkedList*& list, T data) { return false; } +template +bool linked_list_delete(struct LinkedList*& list, D data, bool(*is_same)(T, D)) { + if (!list || !is_same) return false; + struct LinkedList* t = list; + while (t->prev) { + t = t->prev; + } + if (is_same(t->d, data)) { + if (t->prev) t->prev->next = t->next; + if (t->next) t->next->prev = t->prev; + if (t == list) { + if (t->prev) { + list = t->prev; + } else if (t->next) { + list = t->next; + } else { + list = nullptr; + } + } + free(t); + return true; + } + while (t->next) { + t = t->next; + if (is_same(t->d, data)) { + if (t->prev) t->prev->next = t->next; + if (t->next) t->next->prev = t->prev; + if (t == list) { + if (t->prev) { + list = t->prev; + } else if (t->next) { + list = t->next; + } else { + list = nullptr; + } + } + free(t); + return true; + } + } + return false; +} + template void linked_list_free_tail(struct LinkedList*& list, void(*free_func)(T) = nullptr) { if (!list) return;