shash: New function shash_sort().
[sliver-openvswitch.git] / lib / shash.c
1 /*
2  * Copyright (c) 2009 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_clear(struct shash *sh)
45 {
46     struct shash_node *node, *next;
47
48     SHASH_FOR_EACH_SAFE (node, next, sh) {
49         hmap_remove(&sh->map, &node->node);
50         free(node->name);
51         free(node);
52     }
53 }
54
55 bool
56 shash_is_empty(const struct shash *shash)
57 {
58     return hmap_is_empty(&shash->map);
59 }
60
61 size_t
62 shash_count(const struct shash *shash)
63 {
64     return hmap_count(&shash->map);
65 }
66
67 /* It is the caller's responsibility to avoid duplicate names, if that is
68  * desirable. */
69 struct shash_node *
70 shash_add(struct shash *sh, const char *name, void *data)
71 {
72     struct shash_node *node = xmalloc(sizeof *node);
73     node->name = xstrdup(name);
74     node->data = data;
75     hmap_insert(&sh->map, &node->node, hash_name(name));
76     return node;
77 }
78
79 void
80 shash_delete(struct shash *sh, struct shash_node *node)
81 {
82     hmap_remove(&sh->map, &node->node);
83     free(node->name);
84     free(node);
85 }
86
87 /* If there are duplicates, returns a random element. */
88 struct shash_node *
89 shash_find(const struct shash *sh, const char *name)
90 {
91     struct shash_node *node;
92
93     HMAP_FOR_EACH_WITH_HASH (node, struct shash_node, node,
94                              hash_name(name), &sh->map) {
95         if (!strcmp(node->name, name)) {
96             return node;
97         }
98     }
99     return NULL;
100 }
101
102 void *
103 shash_find_data(const struct shash *sh, const char *name)
104 {
105     struct shash_node *node = shash_find(sh, name);
106     return node ? node->data : NULL;
107 }
108
109 void *
110 shash_find_and_delete(struct shash *sh, const char *name)
111 {
112     struct shash_node *node = shash_find(sh, name);
113     if (node) {
114         void *data = node->data;
115         shash_delete(sh, node);
116         return data;
117     } else {
118         return NULL;
119     }
120 }
121
122 struct shash_node *
123 shash_first(const struct shash *shash)
124 {
125     struct hmap_node *node = hmap_first(&shash->map);
126     return node ? CONTAINER_OF(node, struct shash_node, node) : NULL;
127 }
128
129 static int
130 compare_nodes_by_name(const void *a_, const void *b_)
131 {
132     const struct shash_node *const *a = a_;
133     const struct shash_node *const *b = b_;
134     return strcmp((*a)->name, (*b)->name);
135 }
136
137 const struct shash_node **
138 shash_sort(const struct shash *sh)
139 {
140     if (shash_is_empty(sh)) {
141         return NULL;
142     } else {
143         const struct shash_node **nodes;
144         struct shash_node *node;
145         size_t i, n;
146
147         n = shash_count(sh);
148         nodes = xmalloc(n * sizeof *nodes);
149         i = 0;
150         SHASH_FOR_EACH (node, sh) {
151             nodes[i++] = node;
152         }
153         assert(i == n);
154
155         qsort(nodes, n, sizeof *nodes, compare_nodes_by_name);
156
157         return nodes;
158     }
159 }