2 * Copyright (c) 2011 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.
25 static struct hmapx_node *
26 hmapx_find__(const struct hmapx *map, const void *data, size_t hash)
28 struct hmapx_node *node;
30 HMAP_FOR_EACH_IN_BUCKET (node, hmap_node, hash, &map->map) {
31 if (node->data == data) {
38 static struct hmapx_node *
39 hmapx_add__(struct hmapx *map, void *data, size_t hash)
41 struct hmapx_node *node = xmalloc(sizeof *node);
43 hmap_insert(&map->map, &node->hmap_node, hash);
47 /* Initializes 'map' as an empty set of pointers. */
49 hmapx_init(struct hmapx *map)
56 hmapx_destroy(struct hmapx *map)
60 hmap_destroy(&map->map);
64 /* Initializes 'map' to contain the same pointers as 'orig'. */
66 hmapx_clone(struct hmapx *map, const struct hmapx *orig)
68 struct hmapx_node *node;
71 HMAP_FOR_EACH (node, hmap_node, &orig->map) {
72 hmapx_add__(map, node->data, node->hmap_node.hash);
76 /* Exchanges the contents of 'a' and 'b'. */
78 hmapx_swap(struct hmapx *a, struct hmapx *b)
80 hmap_swap(&a->map, &b->map);
83 /* Adjusts 'map' so that it is still valid after it has been moved around in
84 * memory (e.g. due to realloc()). */
86 hmapx_moved(struct hmapx *map)
88 hmap_moved(&map->map);
91 /* Returns true if 'map' contains no nodes, false if it contains at least one
94 hmapx_is_empty(const struct hmapx *map)
96 return hmap_is_empty(&map->map);
99 /* Returns the number of nodes in 'map'. */
101 hmapx_count(const struct hmapx *map)
103 return hmap_count(&map->map);
106 /* Adds 'data' to 'map'. If 'data' is new, returns the new hmapx_node;
107 * otherwise (if a 'data' already existed in 'map'), returns NULL. */
109 hmapx_add(struct hmapx *map, void *data)
111 uint32_t hash = hash_pointer(data, 0);
112 return (hmapx_find__(map, data, hash)
114 : hmapx_add__(map, data, hash));
117 /* Adds 'data' to 'map'. Assert-fails if 'data' was already in 'map'. */
119 hmapx_add_assert(struct hmapx *map, void *data)
121 bool added OVS_UNUSED = hmapx_add(map, data);
125 /* Removes all of the nodes from 'map'. */
127 hmapx_clear(struct hmapx *map)
129 struct hmapx_node *node, *next;
131 HMAPX_FOR_EACH_SAFE (node, next, map) {
132 hmapx_delete(map, node);
136 /* Deletes 'node' from 'map' and frees 'node'. */
138 hmapx_delete(struct hmapx *map, struct hmapx_node *node)
140 hmap_remove(&map->map, &node->hmap_node);
144 /* Searches for 'data' in 'map'. If found, deletes it and returns true. If
145 * not found, returns false without modifying 'map'. */
147 hmapx_find_and_delete(struct hmapx *map, const void *data)
149 struct hmapx_node *node = hmapx_find(map, data);
151 hmapx_delete(map, node);
156 /* Searches for 'data' in 'map' and deletes it. Assert-fails if 'data' is not
159 hmapx_find_and_delete_assert(struct hmapx *map, const void *data)
161 bool deleted OVS_UNUSED = hmapx_find_and_delete(map, data);
165 /* Searches for 'data' in 'map'. Returns its node, if found, otherwise a null
168 hmapx_find(const struct hmapx *map, const void *data)
170 return hmapx_find__(map, data, hash_pointer(data, 0));
173 /* Returns true if 'map' contains 'data', false otherwise. */
175 hmapx_contains(const struct hmapx *map, const void *data)
177 return hmapx_find(map, data) != NULL;
180 /* Returns true if 'a' and 'b' contain the same pointers, false otherwise. */
182 hmapx_equals(const struct hmapx *a, const struct hmapx *b)
184 struct hmapx_node *node;
186 if (hmapx_count(a) != hmapx_count(b)) {
190 HMAP_FOR_EACH (node, hmap_node, &a->map) {
191 if (!hmapx_find__(b, node->data, node->hmap_node.hash)) {