1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
6 This file is part of systemd.
8 Copyright 2010 Lennart Poettering
10 systemd is free software; you can redistribute it and/or modify it
11 under the terms of the GNU Lesser General Public License as published by
12 the Free Software Foundation; either version 2.1 of the License, or
13 (at your option) any later version.
15 systemd is distributed in the hope that it will be useful, but
16 WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 Lesser General Public License for more details.
20 You should have received a copy of the GNU Lesser General Public License
21 along with systemd; If not, see <http://www.gnu.org/licenses/>.
26 /* Pretty straightforward hash table implementation. As a minor
27 * optimization a NULL hashmap object will be treated as empty hashmap
28 * for all read operations. That way it is not necessary to
29 * instantiate an object for each Hashmap use. */
31 typedef struct Hashmap Hashmap;
32 typedef struct _IteratorStruct _IteratorStruct;
33 typedef _IteratorStruct* Iterator;
35 #define ITERATOR_FIRST ((Iterator) 0)
36 #define ITERATOR_LAST ((Iterator) -1)
38 typedef unsigned (*hash_func_t)(const void *p);
39 typedef int (*compare_func_t)(const void *a, const void *b);
41 unsigned string_hash_func(const void *p);
42 int string_compare_func(const void *a, const void *b);
44 unsigned trivial_hash_func(const void *p);
45 int trivial_compare_func(const void *a, const void *b);
47 unsigned uint64_hash_func(const void *p);
48 int uint64_compare_func(const void *a, const void *b);
50 Hashmap *hashmap_new(hash_func_t hash_func, compare_func_t compare_func);
51 void hashmap_free(Hashmap *h);
52 void hashmap_free_free(Hashmap *h);
53 void hashmap_free_free_free(Hashmap *h);
54 Hashmap *hashmap_copy(Hashmap *h);
55 int hashmap_ensure_allocated(Hashmap **h, hash_func_t hash_func, compare_func_t compare_func);
57 int hashmap_put(Hashmap *h, const void *key, void *value);
58 int hashmap_update(Hashmap *h, const void *key, void *value);
59 int hashmap_replace(Hashmap *h, const void *key, void *value);
60 void* hashmap_get(Hashmap *h, const void *key);
61 void* hashmap_get2(Hashmap *h, const void *key, void **rkey);
62 bool hashmap_contains(Hashmap *h, const void *key);
63 void* hashmap_remove(Hashmap *h, const void *key);
64 void* hashmap_remove_value(Hashmap *h, const void *key, void *value);
65 int hashmap_remove_and_put(Hashmap *h, const void *old_key, const void *new_key, void *value);
66 int hashmap_remove_and_replace(Hashmap *h, const void *old_key, const void *new_key, void *value);
68 int hashmap_merge(Hashmap *h, Hashmap *other);
69 void hashmap_move(Hashmap *h, Hashmap *other);
70 int hashmap_move_one(Hashmap *h, Hashmap *other, const void *key);
72 unsigned hashmap_size(Hashmap *h);
73 bool hashmap_isempty(Hashmap *h);
75 void *hashmap_iterate(Hashmap *h, Iterator *i, const void **key);
76 void *hashmap_iterate_backwards(Hashmap *h, Iterator *i, const void **key);
77 void *hashmap_iterate_skip(Hashmap *h, const void *key, Iterator *i);
79 void hashmap_clear(Hashmap *h);
80 void hashmap_clear_free(Hashmap *h);
81 void hashmap_clear_free_free(Hashmap *h);
83 void *hashmap_steal_first(Hashmap *h);
84 void *hashmap_steal_first_key(Hashmap *h);
85 void* hashmap_first(Hashmap *h);
86 void* hashmap_first_key(Hashmap *h);
87 void* hashmap_last(Hashmap *h);
89 void *hashmap_next(Hashmap *h, const void *key);
91 char **hashmap_get_strv(Hashmap *h);
93 #define HASHMAP_FOREACH(e, h, i) \
94 for ((i) = ITERATOR_FIRST, (e) = hashmap_iterate((h), &(i), NULL); (e); (e) = hashmap_iterate((h), &(i), NULL))
96 #define HASHMAP_FOREACH_KEY(e, k, h, i) \
97 for ((i) = ITERATOR_FIRST, (e) = hashmap_iterate((h), &(i), (const void**) &(k)); (e); (e) = hashmap_iterate((h), &(i), (const void**) &(k)))
99 #define HASHMAP_FOREACH_BACKWARDS(e, h, i) \
100 for ((i) = ITERATOR_LAST, (e) = hashmap_iterate_backwards((h), &(i), NULL); (e); (e) = hashmap_iterate_backwards((h), &(i), NULL))