]>
git.proxmox.com Git - mirror_ovs.git/blob - lib/hmapx.c
2 * Copyright (c) 2011, 2012 Nicira, Inc.
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 static struct hmapx_node
*
24 hmapx_find__(const struct hmapx
*map
, const void *data
, size_t hash
)
26 struct hmapx_node
*node
;
28 HMAP_FOR_EACH_IN_BUCKET (node
, hmap_node
, hash
, &map
->map
) {
29 if (node
->data
== data
) {
36 static struct hmapx_node
*
37 hmapx_add__(struct hmapx
*map
, void *data
, size_t hash
)
39 struct hmapx_node
*node
= xmalloc(sizeof *node
);
41 hmap_insert(&map
->map
, &node
->hmap_node
, hash
);
45 /* Initializes 'map' as an empty set of pointers. */
47 hmapx_init(struct hmapx
*map
)
54 hmapx_destroy(struct hmapx
*map
)
58 hmap_destroy(&map
->map
);
62 /* Initializes 'map' to contain the same pointers as 'orig'. */
64 hmapx_clone(struct hmapx
*map
, const struct hmapx
*orig
)
66 struct hmapx_node
*node
;
69 HMAP_FOR_EACH (node
, hmap_node
, &orig
->map
) {
70 hmapx_add__(map
, node
->data
, node
->hmap_node
.hash
);
74 /* Exchanges the contents of 'a' and 'b'. */
76 hmapx_swap(struct hmapx
*a
, struct hmapx
*b
)
78 hmap_swap(&a
->map
, &b
->map
);
81 /* Adjusts 'map' so that it is still valid after it has been moved around in
82 * memory (e.g. due to realloc()). */
84 hmapx_moved(struct hmapx
*map
)
86 hmap_moved(&map
->map
);
89 /* Returns true if 'map' contains no nodes, false if it contains at least one
92 hmapx_is_empty(const struct hmapx
*map
)
94 return hmap_is_empty(&map
->map
);
97 /* Returns the number of nodes in 'map'. */
99 hmapx_count(const struct hmapx
*map
)
101 return hmap_count(&map
->map
);
104 /* Adds 'data' to 'map'. If 'data' is new, returns the new hmapx_node;
105 * otherwise (if a 'data' already existed in 'map'), returns NULL. */
107 hmapx_add(struct hmapx
*map
, void *data
)
109 uint32_t hash
= hash_pointer(data
, 0);
110 return (hmapx_find__(map
, data
, hash
)
112 : hmapx_add__(map
, data
, hash
));
115 /* Adds 'data' to 'map'. Assert-fails if 'data' was already in 'map'. */
117 hmapx_add_assert(struct hmapx
*map
, void *data
)
119 ovs_assert(hmapx_add(map
, data
));
122 /* Removes all of the nodes from 'map'. */
124 hmapx_clear(struct hmapx
*map
)
126 struct hmapx_node
*node
, *next
;
128 HMAPX_FOR_EACH_SAFE (node
, next
, map
) {
129 hmapx_delete(map
, node
);
133 /* Deletes 'node' from 'map' and frees 'node'. */
135 hmapx_delete(struct hmapx
*map
, struct hmapx_node
*node
)
137 hmap_remove(&map
->map
, &node
->hmap_node
);
141 /* Searches for 'data' in 'map'. If found, deletes it and returns true. If
142 * not found, returns false without modifying 'map'. */
144 hmapx_find_and_delete(struct hmapx
*map
, const void *data
)
146 struct hmapx_node
*node
= hmapx_find(map
, data
);
148 hmapx_delete(map
, node
);
153 /* Searches for 'data' in 'map' and deletes it. Assert-fails if 'data' is not
156 hmapx_find_and_delete_assert(struct hmapx
*map
, const void *data
)
158 ovs_assert(hmapx_find_and_delete(map
, data
));
161 /* Searches for 'data' in 'map'. Returns its node, if found, otherwise a null
164 hmapx_find(const struct hmapx
*map
, const void *data
)
166 return hmapx_find__(map
, data
, hash_pointer(data
, 0));
169 /* Returns true if 'map' contains 'data', false otherwise. */
171 hmapx_contains(const struct hmapx
*map
, const void *data
)
173 return hmapx_find(map
, data
) != NULL
;
176 /* Returns true if 'a' and 'b' contain the same pointers, false otherwise. */
178 hmapx_equals(const struct hmapx
*a
, const struct hmapx
*b
)
180 struct hmapx_node
*node
;
182 if (hmapx_count(a
) != hmapx_count(b
)) {
186 HMAP_FOR_EACH (node
, hmap_node
, &a
->map
) {
187 if (!hmapx_find__(b
, node
->data
, node
->hmap_node
.hash
)) {