ovs-vsctl: Support references among records at creation time.
[sliver-openvswitch.git] / 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 void
44 shash_swap(struct shash *a, struct shash *b)
45 {
46     hmap_swap(&a->map, &b->map);
47 }
48
49 void
50 shash_moved(struct shash *sh)
51 {
52     hmap_moved(&sh->map);
53 }
54
55 void
56 shash_clear(struct shash *sh)
57 {
58     struct shash_node *node, *next;
59
60     SHASH_FOR_EACH_SAFE (node, next, sh) {
61         hmap_remove(&sh->map, &node->node);
62         free(node->name);
63         free(node);
64     }
65 }
66
67 bool
68 shash_is_empty(const struct shash *shash)
69 {
70     return hmap_is_empty(&shash->map);
71 }
72
73 size_t
74 shash_count(const struct shash *shash)
75 {
76     return hmap_count(&shash->map);
77 }
78
79 /* It is the caller's responsibility to avoid duplicate names, if that is
80  * desirable. */
81 struct shash_node *
82 shash_add_nocopy(struct shash *sh, char *name, const void *data)
83 {
84     struct shash_node *node = xmalloc(sizeof *node);
85     node->name = name;
86     node->data = (void *) data;
87     hmap_insert(&sh->map, &node->node, hash_name(name));
88     return node;
89 }
90
91 /* It is the caller's responsibility to avoid duplicate names, if that is
92  * desirable. */
93 struct shash_node *
94 shash_add(struct shash *sh, const char *name, const void *data)
95 {
96     return shash_add_nocopy(sh, xstrdup(name), data);
97 }
98
99 bool
100 shash_add_once(struct shash *sh, const char *name, const void *data)
101 {
102     if (!shash_find(sh, name)) {
103         shash_add(sh, name, data);
104         return true;
105     } else {
106         return false;
107     }
108 }
109
110 void
111 shash_add_assert(struct shash *sh, const char *name, const void *data)
112 {
113     bool added OVS_UNUSED = shash_add_once(sh, name, data);
114     assert(added);
115 }
116
117 void
118 shash_delete(struct shash *sh, struct shash_node *node)
119 {
120     hmap_remove(&sh->map, &node->node);
121     free(node->name);
122     free(node);
123 }
124
125 /* If there are duplicates, returns a random element. */
126 struct shash_node *
127 shash_find(const struct shash *sh, const char *name)
128 {
129     struct shash_node *node;
130
131     HMAP_FOR_EACH_WITH_HASH (node, struct shash_node, node,
132                              hash_name(name), &sh->map) {
133         if (!strcmp(node->name, name)) {
134             return node;
135         }
136     }
137     return NULL;
138 }
139
140 void *
141 shash_find_data(const struct shash *sh, const char *name)
142 {
143     struct shash_node *node = shash_find(sh, name);
144     return node ? node->data : NULL;
145 }
146
147 void *
148 shash_find_and_delete(struct shash *sh, const char *name)
149 {
150     struct shash_node *node = shash_find(sh, name);
151     if (node) {
152         void *data = node->data;
153         shash_delete(sh, node);
154         return data;
155     } else {
156         return NULL;
157     }
158 }
159
160 void *
161 shash_find_and_delete_assert(struct shash *sh, const char *name)
162 {
163     void *data = shash_find_and_delete(sh, name);
164     assert(data != NULL);
165     return data;
166 }
167
168 struct shash_node *
169 shash_first(const struct shash *shash)
170 {
171     struct hmap_node *node = hmap_first(&shash->map);
172     return node ? CONTAINER_OF(node, struct shash_node, node) : NULL;
173 }
174
175 static int
176 compare_nodes_by_name(const void *a_, const void *b_)
177 {
178     const struct shash_node *const *a = a_;
179     const struct shash_node *const *b = b_;
180     return strcmp((*a)->name, (*b)->name);
181 }
182
183 const struct shash_node **
184 shash_sort(const struct shash *sh)
185 {
186     if (shash_is_empty(sh)) {
187         return NULL;
188     } else {
189         const struct shash_node **nodes;
190         struct shash_node *node;
191         size_t i, n;
192
193         n = shash_count(sh);
194         nodes = xmalloc(n * sizeof *nodes);
195         i = 0;
196         SHASH_FOR_EACH (node, sh) {
197             nodes[i++] = node;
198         }
199         assert(i == n);
200
201         qsort(nodes, n, sizeof *nodes, compare_nodes_by_name);
202
203         return nodes;
204     }
205 }
206
207 /* Returns true if 'a' and 'b' contain the same keys (regardless of their
208  * values), false otherwise. */
209 bool
210 shash_equal_keys(const struct shash *a, const struct shash *b)
211 {
212     struct shash_node *node;
213
214     if (hmap_count(&a->map) != hmap_count(&b->map)) {
215         return false;
216     }
217     SHASH_FOR_EACH (node, a) {
218         if (!shash_find(b, node->name)) {
219             return false;
220         }
221     }
222     return true;
223 }