2 * Copyright (c) 2009, 2010 Nicira Networks.
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:
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
23 hash_name(const char *name)
25 return hash_string(name, 0);
29 shash_init(struct shash *sh)
35 shash_destroy(struct shash *sh)
39 hmap_destroy(&sh->map);
44 shash_swap(struct shash *a, struct shash *b)
46 hmap_swap(&a->map, &b->map);
50 shash_clear(struct shash *sh)
52 struct shash_node *node, *next;
54 SHASH_FOR_EACH_SAFE (node, next, sh) {
55 hmap_remove(&sh->map, &node->node);
62 shash_is_empty(const struct shash *shash)
64 return hmap_is_empty(&shash->map);
68 shash_count(const struct shash *shash)
70 return hmap_count(&shash->map);
73 /* It is the caller's responsibility to avoid duplicate names, if that is
76 shash_add(struct shash *sh, const char *name, const void *data)
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));
86 shash_add_once(struct shash *sh, const char *name, const void *data)
88 if (!shash_find(sh, name)) {
89 shash_add(sh, name, data);
97 shash_delete(struct shash *sh, struct shash_node *node)
99 hmap_remove(&sh->map, &node->node);
104 /* If there are duplicates, returns a random element. */
106 shash_find(const struct shash *sh, const char *name)
108 struct shash_node *node;
110 HMAP_FOR_EACH_WITH_HASH (node, struct shash_node, node,
111 hash_name(name), &sh->map) {
112 if (!strcmp(node->name, name)) {
120 shash_find_data(const struct shash *sh, const char *name)
122 struct shash_node *node = shash_find(sh, name);
123 return node ? node->data : NULL;
127 shash_find_and_delete(struct shash *sh, const char *name)
129 struct shash_node *node = shash_find(sh, name);
131 void *data = node->data;
132 shash_delete(sh, node);
140 shash_first(const struct shash *shash)
142 struct hmap_node *node = hmap_first(&shash->map);
143 return node ? CONTAINER_OF(node, struct shash_node, node) : NULL;
147 compare_nodes_by_name(const void *a_, const void *b_)
149 const struct shash_node *const *a = a_;
150 const struct shash_node *const *b = b_;
151 return strcmp((*a)->name, (*b)->name);
154 const struct shash_node **
155 shash_sort(const struct shash *sh)
157 if (shash_is_empty(sh)) {
160 const struct shash_node **nodes;
161 struct shash_node *node;
165 nodes = xmalloc(n * sizeof *nodes);
167 SHASH_FOR_EACH (node, sh) {
172 qsort(nodes, n, sizeof *nodes, compare_nodes_by_name);
178 /* Returns true if 'a' and 'b' contain the same keys (regardless of their
179 * values), false otherwise. */
181 shash_equal_keys(const struct shash *a, const struct shash *b)
183 struct shash_node *node;
185 if (hmap_count(&a->map) != hmap_count(&b->map)) {
188 SHASH_FOR_EACH (node, a) {
189 if (!shash_find(b, node->name)) {