Reimplement port mirroring configuration for database.
[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_clear(struct shash *sh)
51 {
52     struct shash_node *node, *next;
53
54     SHASH_FOR_EACH_SAFE (node, next, sh) {
55         hmap_remove(&sh->map, &node->node);
56         free(node->name);
57         free(node);
58     }
59 }
60
61 bool
62 shash_is_empty(const struct shash *shash)
63 {
64     return hmap_is_empty(&shash->map);
65 }
66
67 size_t
68 shash_count(const struct shash *shash)
69 {
70     return hmap_count(&shash->map);
71 }
72
73 /* It is the caller's responsibility to avoid duplicate names, if that is
74  * desirable. */
75 struct shash_node *
76 shash_add(struct shash *sh, const char *name, const void *data)
77 {
78     struct shash_node *node = xmalloc(sizeof *node);
79     node->name = xstrdup(name);
80     node->data = (void *) data;
81     hmap_insert(&sh->map, &node->node, hash_name(name));
82     return node;
83 }
84
85 bool
86 shash_add_once(struct shash *sh, const char *name, const void *data)
87 {
88     if (!shash_find(sh, name)) {
89         shash_add(sh, name, data);
90         return true;
91     } else {
92         return false;
93     }
94 }
95
96 void
97 shash_delete(struct shash *sh, struct shash_node *node)
98 {
99     hmap_remove(&sh->map, &node->node);
100     free(node->name);
101     free(node);
102 }
103
104 /* If there are duplicates, returns a random element. */
105 struct shash_node *
106 shash_find(const struct shash *sh, const char *name)
107 {
108     struct shash_node *node;
109
110     HMAP_FOR_EACH_WITH_HASH (node, struct shash_node, node,
111                              hash_name(name), &sh->map) {
112         if (!strcmp(node->name, name)) {
113             return node;
114         }
115     }
116     return NULL;
117 }
118
119 void *
120 shash_find_data(const struct shash *sh, const char *name)
121 {
122     struct shash_node *node = shash_find(sh, name);
123     return node ? node->data : NULL;
124 }
125
126 void *
127 shash_find_and_delete(struct shash *sh, const char *name)
128 {
129     struct shash_node *node = shash_find(sh, name);
130     if (node) {
131         void *data = node->data;
132         shash_delete(sh, node);
133         return data;
134     } else {
135         return NULL;
136     }
137 }
138
139 struct shash_node *
140 shash_first(const struct shash *shash)
141 {
142     struct hmap_node *node = hmap_first(&shash->map);
143     return node ? CONTAINER_OF(node, struct shash_node, node) : NULL;
144 }
145
146 static int
147 compare_nodes_by_name(const void *a_, const void *b_)
148 {
149     const struct shash_node *const *a = a_;
150     const struct shash_node *const *b = b_;
151     return strcmp((*a)->name, (*b)->name);
152 }
153
154 const struct shash_node **
155 shash_sort(const struct shash *sh)
156 {
157     if (shash_is_empty(sh)) {
158         return NULL;
159     } else {
160         const struct shash_node **nodes;
161         struct shash_node *node;
162         size_t i, n;
163
164         n = shash_count(sh);
165         nodes = xmalloc(n * sizeof *nodes);
166         i = 0;
167         SHASH_FOR_EACH (node, sh) {
168             nodes[i++] = node;
169         }
170         assert(i == n);
171
172         qsort(nodes, n, sizeof *nodes, compare_nodes_by_name);
173
174         return nodes;
175     }
176 }
177
178 /* Returns true if 'a' and 'b' contain the same keys (regardless of their
179  * values), false otherwise. */
180 bool
181 shash_equal_keys(const struct shash *a, const struct shash *b)
182 {
183     struct shash_node *node;
184
185     if (hmap_count(&a->map) != hmap_count(&b->map)) {
186         return false;
187     }
188     SHASH_FOR_EACH (node, a) {
189         if (!shash_find(b, node->name)) {
190             return false;
191         }
192     }
193     return true;
194 }