shash: New functions shash_destroy_free_data() and shash_clear_free_data().
[openvswitch] / lib / shash.c
1 /*
2  * Copyright (c) 2009, 2010 Nicira Networks.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at:
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 #include <config.h>
18 #include "shash.h"
19 #include <assert.h>
20 #include "hash.h"
21
22 static size_t
23 hash_name(const char *name)
24 {
25     return hash_string(name, 0);
26 }
27
28 void
29 shash_init(struct shash *sh)
30 {
31     hmap_init(&sh->map);
32 }
33
34 void
35 shash_destroy(struct shash *sh)
36 {
37     if (sh) {
38         shash_clear(sh);
39         hmap_destroy(&sh->map);
40     }
41 }
42
43 /* Like shash_destroy(), but also free() each node's 'data'. */
44 void
45 shash_destroy_free_data(struct shash *sh)
46 {
47     if (sh) {
48         shash_clear_free_data(sh);
49         hmap_destroy(&sh->map);
50     }
51 }
52
53 void
54 shash_swap(struct shash *a, struct shash *b)
55 {
56     hmap_swap(&a->map, &b->map);
57 }
58
59 void
60 shash_moved(struct shash *sh)
61 {
62     hmap_moved(&sh->map);
63 }
64
65 void
66 shash_clear(struct shash *sh)
67 {
68     struct shash_node *node, *next;
69
70     SHASH_FOR_EACH_SAFE (node, next, sh) {
71         hmap_remove(&sh->map, &node->node);
72         free(node->name);
73         free(node);
74     }
75 }
76
77 /* Like shash_clear(), but also free() each node's 'data'. */
78 void
79 shash_clear_free_data(struct shash *sh)
80 {
81     struct shash_node *node, *next;
82
83     SHASH_FOR_EACH_SAFE (node, next, sh) {
84         hmap_remove(&sh->map, &node->node);
85         free(node->data);
86         free(node->name);
87         free(node);
88     }
89 }
90
91 bool
92 shash_is_empty(const struct shash *shash)
93 {
94     return hmap_is_empty(&shash->map);
95 }
96
97 size_t
98 shash_count(const struct shash *shash)
99 {
100     return hmap_count(&shash->map);
101 }
102
103 /* It is the caller's responsibility to avoid duplicate names, if that is
104  * desirable. */
105 struct shash_node *
106 shash_add_nocopy(struct shash *sh, char *name, const void *data)
107 {
108     struct shash_node *node = xmalloc(sizeof *node);
109     node->name = name;
110     node->data = (void *) data;
111     hmap_insert(&sh->map, &node->node, hash_name(name));
112     return node;
113 }
114
115 /* It is the caller's responsibility to avoid duplicate names, if that is
116  * desirable. */
117 struct shash_node *
118 shash_add(struct shash *sh, const char *name, const void *data)
119 {
120     return shash_add_nocopy(sh, xstrdup(name), data);
121 }
122
123 bool
124 shash_add_once(struct shash *sh, const char *name, const void *data)
125 {
126     if (!shash_find(sh, name)) {
127         shash_add(sh, name, data);
128         return true;
129     } else {
130         return false;
131     }
132 }
133
134 void
135 shash_add_assert(struct shash *sh, const char *name, const void *data)
136 {
137     bool added OVS_UNUSED = shash_add_once(sh, name, data);
138     assert(added);
139 }
140
141 void
142 shash_delete(struct shash *sh, struct shash_node *node)
143 {
144     hmap_remove(&sh->map, &node->node);
145     free(node->name);
146     free(node);
147 }
148
149 /* If there are duplicates, returns a random element. */
150 struct shash_node *
151 shash_find(const struct shash *sh, const char *name)
152 {
153     struct shash_node *node;
154
155     HMAP_FOR_EACH_WITH_HASH (node, struct shash_node, node,
156                              hash_name(name), &sh->map) {
157         if (!strcmp(node->name, name)) {
158             return node;
159         }
160     }
161     return NULL;
162 }
163
164 void *
165 shash_find_data(const struct shash *sh, const char *name)
166 {
167     struct shash_node *node = shash_find(sh, name);
168     return node ? node->data : NULL;
169 }
170
171 void *
172 shash_find_and_delete(struct shash *sh, const char *name)
173 {
174     struct shash_node *node = shash_find(sh, name);
175     if (node) {
176         void *data = node->data;
177         shash_delete(sh, node);
178         return data;
179     } else {
180         return NULL;
181     }
182 }
183
184 void *
185 shash_find_and_delete_assert(struct shash *sh, const char *name)
186 {
187     void *data = shash_find_and_delete(sh, name);
188     assert(data != NULL);
189     return data;
190 }
191
192 struct shash_node *
193 shash_first(const struct shash *shash)
194 {
195     struct hmap_node *node = hmap_first(&shash->map);
196     return node ? CONTAINER_OF(node, struct shash_node, node) : NULL;
197 }
198
199 static int
200 compare_nodes_by_name(const void *a_, const void *b_)
201 {
202     const struct shash_node *const *a = a_;
203     const struct shash_node *const *b = b_;
204     return strcmp((*a)->name, (*b)->name);
205 }
206
207 const struct shash_node **
208 shash_sort(const struct shash *sh)
209 {
210     if (shash_is_empty(sh)) {
211         return NULL;
212     } else {
213         const struct shash_node **nodes;
214         struct shash_node *node;
215         size_t i, n;
216
217         n = shash_count(sh);
218         nodes = xmalloc(n * sizeof *nodes);
219         i = 0;
220         SHASH_FOR_EACH (node, sh) {
221             nodes[i++] = node;
222         }
223         assert(i == n);
224
225         qsort(nodes, n, sizeof *nodes, compare_nodes_by_name);
226
227         return nodes;
228     }
229 }
230
231 /* Returns true if 'a' and 'b' contain the same keys (regardless of their
232  * values), false otherwise. */
233 bool
234 shash_equal_keys(const struct shash *a, const struct shash *b)
235 {
236     struct shash_node *node;
237
238     if (hmap_count(&a->map) != hmap_count(&b->map)) {
239         return false;
240     }
241     SHASH_FOR_EACH (node, a) {
242         if (!shash_find(b, node->name)) {
243             return false;
244         }
245     }
246     return true;
247 }