]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/shared/hashmap.h
test: extend test-send to send some weirder data
[thirdparty/systemd.git] / src / shared / hashmap.h
CommitLineData
03467c88 1/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
60918275 2
c2f1db8f 3#pragma once
60918275 4
a7334b09
LP
5/***
6 This file is part of systemd.
7
8 Copyright 2010 Lennart Poettering
9
10 systemd is free software; you can redistribute it and/or modify it
5430f7f2
LP
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
a7334b09
LP
13 (at your option) any later version.
14
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
5430f7f2 18 Lesser General Public License for more details.
a7334b09 19
5430f7f2 20 You should have received a copy of the GNU Lesser General Public License
a7334b09
LP
21 along with systemd; If not, see <http://www.gnu.org/licenses/>.
22***/
23
60918275
LP
24#include <stdbool.h>
25
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. */
30
31typedef struct Hashmap Hashmap;
034c6ed7
LP
32typedef struct _IteratorStruct _IteratorStruct;
33typedef _IteratorStruct* Iterator;
34
35#define ITERATOR_FIRST ((Iterator) 0)
36#define ITERATOR_LAST ((Iterator) -1)
60918275
LP
37
38typedef unsigned (*hash_func_t)(const void *p);
39typedef int (*compare_func_t)(const void *a, const void *b);
40
41unsigned string_hash_func(const void *p);
42int string_compare_func(const void *a, const void *b);
43
44unsigned trivial_hash_func(const void *p);
45int trivial_compare_func(const void *a, const void *b);
46
47Hashmap *hashmap_new(hash_func_t hash_func, compare_func_t compare_func);
91cdde8a 48void hashmap_free(Hashmap *h);
449ddb2d 49void hashmap_free_free(Hashmap *h);
91cdde8a 50Hashmap *hashmap_copy(Hashmap *h);
034c6ed7 51int hashmap_ensure_allocated(Hashmap **h, hash_func_t hash_func, compare_func_t compare_func);
60918275
LP
52
53int hashmap_put(Hashmap *h, const void *key, void *value);
d99ae53a 54int hashmap_update(Hashmap *h, const void *key, void *value);
3158713e 55int hashmap_replace(Hashmap *h, const void *key, void *value);
60918275 56void* hashmap_get(Hashmap *h, const void *key);
d99ae53a 57void* hashmap_get2(Hashmap *h, const void *key, void **rkey);
96342de6 58bool hashmap_contains(Hashmap *h, const void *key);
60918275 59void* hashmap_remove(Hashmap *h, const void *key);
3158713e 60void* hashmap_remove_value(Hashmap *h, const void *key, void *value);
101d8e63 61int hashmap_remove_and_put(Hashmap *h, const void *old_key, const void *new_key, void *value);
8fe914ec 62int hashmap_remove_and_replace(Hashmap *h, const void *old_key, const void *new_key, void *value);
60918275 63
91cdde8a 64int hashmap_merge(Hashmap *h, Hashmap *other);
101d8e63
LP
65void hashmap_move(Hashmap *h, Hashmap *other);
66int hashmap_move_one(Hashmap *h, Hashmap *other, const void *key);
91cdde8a 67
60918275
LP
68unsigned hashmap_size(Hashmap *h);
69bool hashmap_isempty(Hashmap *h);
70
034c6ed7
LP
71void *hashmap_iterate(Hashmap *h, Iterator *i, const void **key);
72void *hashmap_iterate_backwards(Hashmap *h, Iterator *i, const void **key);
73void *hashmap_iterate_skip(Hashmap *h, const void *key, Iterator *i);
60918275 74
11dd41ce 75void hashmap_clear(Hashmap *h);
9946996c
LP
76void hashmap_clear_free(Hashmap *h);
77
60918275 78void *hashmap_steal_first(Hashmap *h);
22be093f 79void *hashmap_steal_first_key(Hashmap *h);
60918275 80void* hashmap_first(Hashmap *h);
2e4a6ff4 81void* hashmap_first_key(Hashmap *h);
60918275
LP
82void* hashmap_last(Hashmap *h);
83
3c1668da
LP
84void *hashmap_next(Hashmap *h, const void *key);
85
db1413d7
KS
86char **hashmap_get_strv(Hashmap *h);
87
034c6ed7 88#define HASHMAP_FOREACH(e, h, i) \
aed5e44d 89 for ((i) = ITERATOR_FIRST, (e) = hashmap_iterate((h), &(i), NULL); (e); (e) = hashmap_iterate((h), &(i), NULL))
60918275 90
034c6ed7 91#define HASHMAP_FOREACH_KEY(e, k, h, i) \
aed5e44d 92 for ((i) = ITERATOR_FIRST, (e) = hashmap_iterate((h), &(i), (const void**) &(k)); (e); (e) = hashmap_iterate((h), &(i), (const void**) &(k)))
11dd41ce 93
034c6ed7 94#define HASHMAP_FOREACH_BACKWARDS(e, h, i) \
aed5e44d 95 for ((i) = ITERATOR_LAST, (e) = hashmap_iterate_backwards((h), &(i), NULL); (e); (e) = hashmap_iterate_backwards((h), &(i), NULL))