This commit is contained in:
2022-01-09 18:03:02 +08:00
parent 0a9e149e66
commit 78a3e890d2
6 changed files with 228 additions and 0 deletions

18
dict.h
View File

@@ -21,6 +21,24 @@ void dict_free(struct Dict<T, V>*& d, void(*free_func)(struct dict_entry<T, V>)
return linked_list_clear((struct LinkedList<struct dict_entry<T, V>>*&)d, free_func);
}
template <typename K, typename V>
bool dict_get_internal(struct dict_entry<K, V> o, K key) {
return o.key == key;
}
template <typename K, typename V>
struct dict_entry<K, V>* dict_get(struct Dict<K, V>* d, K key) {
struct Dict<K, V>* re = (struct Dict<K, V>*)linked_list_get((struct LinkedList<struct dict_entry<K, V>>*)d, key, &dict_get_internal);
if (!re) return nullptr;
return &re->d;
}
template <typename K, typename V>
bool dict_heve_key_internal(struct dict_entry<K, V> origin, K key) {
return key == origin.key;
}
template <typename K, typename V>
bool dict_have_key(struct Dict<K, V>* d, K key) {
return linked_list_have((struct LinkedList<struct dict_entry<K, V>>*)d, key, &dict_heve_key_internal);
}
template <typename K, typename V>
bool dict_set(struct Dict<K, V>*& d, K key, V value, void(*free_func)(V) = nullptr) {
if (!d) {
struct dict_entry<K, V> v = { key, value };