]>
Commit | Line | Data |
---|---|---|
064af421 BP |
1 | /* |
2 | * Copyright (c) 2009 Nicira Networks. | |
3 | * | |
a14bc59f BP |
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: | |
064af421 | 7 | * |
a14bc59f BP |
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. | |
064af421 BP |
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); | |
d855aead | 39 | hmap_destroy(&sh->map); |
064af421 BP |
40 | } |
41 | } | |
42 | ||
43 | void | |
44 | shash_clear(struct shash *sh) | |
45 | { | |
46 | struct shash_node *node, *next; | |
47 | ||
fd30311c | 48 | SHASH_FOR_EACH_SAFE (node, next, sh) { |
064af421 BP |
49 | hmap_remove(&sh->map, &node->node); |
50 | free(node->name); | |
51 | free(node); | |
52 | } | |
53 | } | |
54 | ||
732dcb37 BP |
55 | bool |
56 | shash_is_empty(const struct shash *shash) | |
57 | { | |
58 | return hmap_is_empty(&shash->map); | |
59 | } | |
60 | ||
78299b0a | 61 | /* It is the caller's responsibility to avoid duplicate names, if that is |
064af421 | 62 | * desirable. */ |
78299b0a | 63 | struct shash_node * |
064af421 BP |
64 | shash_add(struct shash *sh, const char *name, void *data) |
65 | { | |
66 | struct shash_node *node = xmalloc(sizeof *node); | |
67 | node->name = xstrdup(name); | |
68 | node->data = data; | |
69 | hmap_insert(&sh->map, &node->node, hash_name(name)); | |
78299b0a | 70 | return node; |
064af421 BP |
71 | } |
72 | ||
73 | void | |
74 | shash_delete(struct shash *sh, struct shash_node *node) | |
75 | { | |
76 | hmap_remove(&sh->map, &node->node); | |
77 | free(node->name); | |
78 | free(node); | |
79 | } | |
80 | ||
81 | /* If there are duplicates, returns a random element. */ | |
82 | struct shash_node * | |
83 | shash_find(const struct shash *sh, const char *name) | |
84 | { | |
85 | struct shash_node *node; | |
86 | ||
87 | HMAP_FOR_EACH_WITH_HASH (node, struct shash_node, node, | |
88 | hash_name(name), &sh->map) { | |
89 | if (!strcmp(node->name, name)) { | |
90 | return node; | |
91 | } | |
92 | } | |
93 | return NULL; | |
94 | } | |
95 | ||
96 | void * | |
97 | shash_find_data(const struct shash *sh, const char *name) | |
98 | { | |
99 | struct shash_node *node = shash_find(sh, name); | |
100 | return node ? node->data : NULL; | |
101 | } | |
7efc68eb BP |
102 | |
103 | struct shash_node * | |
104 | shash_first(const struct shash *shash) | |
105 | { | |
106 | struct hmap_node *node = hmap_first(&shash->map); | |
107 | return node ? CONTAINER_OF(node, struct shash_node, node) : NULL; | |
108 | } | |
109 |