From 4468799a8611bdc567221ced58f158bfef82b1bb Mon Sep 17 00:00:00 2001 From: lifegpc Date: Fri, 14 Jan 2022 17:58:41 +0800 Subject: [PATCH] Update --- dict.h | 12 ++++++++++++ linked_list.h | 25 +++++++++++++++++++++++++ 2 files changed, 37 insertions(+) diff --git a/dict.h b/dict.h index e7148fd..efb7353 100644 --- a/dict.h +++ b/dict.h @@ -54,6 +54,18 @@ void dict_iter(struct Dict* d, void(*callback)(K key, V value, Args... arg callback(t->d.key, t->d.value, args...); } } +template +void dict_iter(struct Dict* d, void(*callback)(size_t index, K key, V value, Args... args), Args... args) { + if (!d || !callback) return; + struct Dict* t = d; + size_t i = 0; + callback(i, t->d.key, t->d.value, args...); + while (t->next) { + t = t->next; + i++; + callback(i, t->d.key, t->d.value, args...); + } +} template bool dict_set(struct Dict*& d, K key, V value, void(*free_func)(V) = nullptr) { if (!d) { diff --git a/linked_list.h b/linked_list.h index b9231d6..ba09f37 100644 --- a/linked_list.h +++ b/linked_list.h @@ -259,6 +259,31 @@ void linked_list_iter(struct LinkedList* list, void(*callback)(size_t index, } } +template +void linked_list_iter(struct LinkedList* list, void(*callback)(T data, Args... args), Args... args) { + if (!list || !callback) return; + struct LinkedList* t = list; + callback(t->d, args...); + while (t->next) { + t = t->next; + callback(t->d, args...); + } +} + +template +D linked_list_iter(struct LinkedList* list, D(*callback)(T data, Args... args), D failed, Args... args) { + if (!list || !callback) return failed; + struct LinkedList* t = list; + D re = callback(t->d, args...); + if (re == failed) return re; + while (t->next) { + t = t->next; + re = callback(t->d, args...); + if (re == failed) return re; + } + return re; +} + template void linked_list_remove(struct LinkedList*& node, void(*free_func)(T) = nullptr) { if (!node) return;