chiark / gitweb /
core: serialize/deserialize bus subscribers
[elogind.git] / src / shared / set.c
1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
2
3 /***
4   This file is part of systemd.
5
6   Copyright 2010 Lennart Poettering
7
8   systemd is free software; you can redistribute it and/or modify it
9   under the terms of the GNU Lesser General Public License as published by
10   the Free Software Foundation; either version 2.1 of the License, or
11   (at your option) any later version.
12
13   systemd is distributed in the hope that it will be useful, but
14   WITHOUT ANY WARRANTY; without even the implied warranty of
15   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16   Lesser General Public License for more details.
17
18   You should have received a copy of the GNU Lesser General Public License
19   along with systemd; If not, see <http://www.gnu.org/licenses/>.
20 ***/
21
22 #include <stdlib.h>
23
24 #include "set.h"
25 #include "hashmap.h"
26
27 #define MAKE_SET(h) ((Set*) (h))
28 #define MAKE_HASHMAP(s) ((Hashmap*) (s))
29
30 /* For now this is not much more than a wrapper around a hashmap */
31
32 Set *set_new(hash_func_t hash_func, compare_func_t compare_func) {
33         return MAKE_SET(hashmap_new(hash_func, compare_func));
34 }
35
36 void set_free(Set* s) {
37         hashmap_free(MAKE_HASHMAP(s));
38 }
39
40 void set_free_free(Set *s) {
41         hashmap_free_free(MAKE_HASHMAP(s));
42 }
43
44 int set_ensure_allocated(Set **s, hash_func_t hash_func, compare_func_t compare_func) {
45         return hashmap_ensure_allocated((Hashmap**) s, hash_func, compare_func);
46 }
47
48 int set_put(Set *s, void *value) {
49         return hashmap_put(MAKE_HASHMAP(s), value, value);
50 }
51
52 int set_consume(Set *s, void *value) {
53         int r;
54
55         r = set_put(s, value);
56         if (r < 0)
57                 free(value);
58
59         return r;
60 }
61
62 int set_replace(Set *s, void *value) {
63         return hashmap_replace(MAKE_HASHMAP(s), value, value);
64 }
65
66 void *set_get(Set *s, void *value) {
67         return hashmap_get(MAKE_HASHMAP(s), value);
68 }
69
70 bool set_contains(Set *s, void *value) {
71         return hashmap_contains(MAKE_HASHMAP(s), value);
72 }
73
74 void *set_remove(Set *s, void *value) {
75         return hashmap_remove(MAKE_HASHMAP(s), value);
76 }
77
78 int set_remove_and_put(Set *s, void *old_value, void *new_value) {
79         return hashmap_remove_and_put(MAKE_HASHMAP(s), old_value, new_value, new_value);
80 }
81
82 unsigned set_size(Set *s) {
83         return hashmap_size(MAKE_HASHMAP(s));
84 }
85
86 bool set_isempty(Set *s) {
87         return hashmap_isempty(MAKE_HASHMAP(s));
88 }
89
90 void *set_iterate(Set *s, Iterator *i) {
91         return hashmap_iterate(MAKE_HASHMAP(s), i, NULL);
92 }
93
94 void *set_iterate_backwards(Set *s, Iterator *i) {
95         return hashmap_iterate_backwards(MAKE_HASHMAP(s), i, NULL);
96 }
97
98 void *set_iterate_skip(Set *s, void *value, Iterator *i) {
99         return hashmap_iterate_skip(MAKE_HASHMAP(s), value, i);
100 }
101
102 void *set_steal_first(Set *s) {
103         return hashmap_steal_first(MAKE_HASHMAP(s));
104 }
105
106 void* set_first(Set *s) {
107         return hashmap_first(MAKE_HASHMAP(s));
108 }
109
110 void* set_last(Set *s) {
111         return hashmap_last(MAKE_HASHMAP(s));
112 }
113
114 int set_merge(Set *s, Set *other) {
115         return hashmap_merge(MAKE_HASHMAP(s), MAKE_HASHMAP(other));
116 }
117
118 void set_move(Set *s, Set *other) {
119         return hashmap_move(MAKE_HASHMAP(s), MAKE_HASHMAP(other));
120 }
121
122 int set_move_one(Set *s, Set *other, void *value) {
123         return hashmap_move_one(MAKE_HASHMAP(s), MAKE_HASHMAP(other), value);
124 }
125
126 Set* set_copy(Set *s) {
127         return MAKE_SET(hashmap_copy(MAKE_HASHMAP(s)));
128 }
129
130 void set_clear(Set *s) {
131         hashmap_clear(MAKE_HASHMAP(s));
132 }
133
134 void set_clear_free(Set *s) {
135         hashmap_clear_free(MAKE_HASHMAP(s));
136 }
137
138 char **set_get_strv(Set *s) {
139         return hashmap_get_strv(MAKE_HASHMAP(s));
140 }