mirror of
https://github.com/lifegpc/c-utils.git
synced 2026-07-08 01:30:39 +08:00
Update
This commit is contained in:
18
dict.h
18
dict.h
@@ -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 };
|
||||
|
||||
Reference in New Issue
Block a user