chiark / gitweb /
hashmap: allow hashmap_move() to fail
[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 #include "strv.h"
27
28 #define MAKE_SET(h) ((Set*) (h))
29 #define MAKE_HASHMAP(s) ((Hashmap*) (s))
30
31 /* For now this is not much more than a wrapper around a hashmap */
32
33 Set *set_new(const struct hash_ops *hash_ops) {
34         return MAKE_SET(hashmap_new(hash_ops));
35 }
36
37 void set_free(Set* s) {
38         hashmap_free(MAKE_HASHMAP(s));
39 }
40
41 void set_free_free(Set *s) {
42         hashmap_free_free(MAKE_HASHMAP(s));
43 }
44
45 int set_ensure_allocated(Set **s, const struct hash_ops *hash_ops) {
46         return hashmap_ensure_allocated((Hashmap**) s, hash_ops);
47 }
48
49 int set_put(Set *s, void *value) {
50         return hashmap_put(MAKE_HASHMAP(s), value, value);
51 }
52
53 int set_consume(Set *s, void *value) {
54         int r;
55
56         r = set_put(s, value);
57         if (r < 0)
58                 free(value);
59
60         return r;
61 }
62
63 int set_put_strdup(Set *s, const char *p) {
64         char *c;
65         int r;
66
67         assert(s);
68         assert(p);
69
70         c = strdup(p);
71         if (!c)
72                 return -ENOMEM;
73
74         r = set_consume(s, c);
75         if (r == -EEXIST)
76                 return 0;
77
78         return r;
79 }
80
81 int set_put_strdupv(Set *s, char **l) {
82         int n = 0, r;
83         char **i;
84
85         STRV_FOREACH(i, l) {
86                 r = set_put_strdup(s, *i);
87                 if (r < 0)
88                         return r;
89
90                 n += r;
91         }
92
93         return n;
94 }
95
96 int set_replace(Set *s, void *value) {
97         return hashmap_replace(MAKE_HASHMAP(s), value, value);
98 }
99
100 void *set_get(Set *s, void *value) {
101         return hashmap_get(MAKE_HASHMAP(s), value);
102 }
103
104 bool set_contains(Set *s, void *value) {
105         return hashmap_contains(MAKE_HASHMAP(s), value);
106 }
107
108 void *set_remove(Set *s, void *value) {
109         return hashmap_remove(MAKE_HASHMAP(s), value);
110 }
111
112 int set_remove_and_put(Set *s, void *old_value, void *new_value) {
113         return hashmap_remove_and_put(MAKE_HASHMAP(s), old_value, new_value, new_value);
114 }
115
116 unsigned set_size(Set *s) {
117         return hashmap_size(MAKE_HASHMAP(s));
118 }
119
120 bool set_isempty(Set *s) {
121         return hashmap_isempty(MAKE_HASHMAP(s));
122 }
123
124 void *set_iterate(Set *s, Iterator *i) {
125         return hashmap_iterate(MAKE_HASHMAP(s), i, NULL);
126 }
127
128 void *set_steal_first(Set *s) {
129         return hashmap_steal_first(MAKE_HASHMAP(s));
130 }
131
132 void* set_first(Set *s) {
133         return hashmap_first(MAKE_HASHMAP(s));
134 }
135
136 int set_merge(Set *s, Set *other) {
137         return hashmap_merge(MAKE_HASHMAP(s), MAKE_HASHMAP(other));
138 }
139
140 int set_reserve(Set *s, unsigned entries_add) {
141         return hashmap_reserve(MAKE_HASHMAP(s), entries_add);
142 }
143
144 int set_move(Set *s, Set *other) {
145         return hashmap_move(MAKE_HASHMAP(s), MAKE_HASHMAP(other));
146 }
147
148 int set_move_one(Set *s, Set *other, void *value) {
149         return hashmap_move_one(MAKE_HASHMAP(s), MAKE_HASHMAP(other), value);
150 }
151
152 Set* set_copy(Set *s) {
153         return MAKE_SET(hashmap_copy(MAKE_HASHMAP(s)));
154 }
155
156 void set_clear(Set *s) {
157         hashmap_clear(MAKE_HASHMAP(s));
158 }
159
160 void set_clear_free(Set *s) {
161         hashmap_clear_free(MAKE_HASHMAP(s));
162 }
163
164 char **set_get_strv(Set *s) {
165         return hashmap_get_strv(MAKE_HASHMAP(s));
166 }