configure: Silence check for broken strtok_r().
[sliver-openvswitch.git] / lib / hmap.h
1 /*
2  * Copyright (c) 2008, 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 #ifndef HMAP_H
18 #define HMAP_H 1
19
20 #include <stdbool.h>
21 #include <stdlib.h>
22 #include "util.h"
23
24 #ifdef  __cplusplus
25 extern "C" {
26 #endif
27
28 /* A hash map node, to be embedded inside the data structure being mapped. */
29 struct hmap_node {
30     size_t hash;                /* Hash value. */
31     struct hmap_node *next;     /* Next in linked list. */
32 };
33
34 /* Returns the hash value embedded in 'node'. */
35 static inline size_t hmap_node_hash(const struct hmap_node *node)
36 {
37     return node->hash;
38 }
39
40 /* A hash map. */
41 struct hmap {
42     struct hmap_node **buckets;
43     struct hmap_node *one;
44     size_t mask;
45     size_t n;
46 };
47
48 /* Initializer for an empty hash map. */
49 #define HMAP_INITIALIZER(HMAP) { &(HMAP)->one, NULL, 0, 0 }
50
51 /* Initialization. */
52 void hmap_init(struct hmap *);
53 void hmap_destroy(struct hmap *);
54 void hmap_swap(struct hmap *a, struct hmap *b);
55 static inline size_t hmap_count(const struct hmap *);
56 static inline bool hmap_is_empty(const struct hmap *);
57
58 /* Adjusting capacity. */
59 void hmap_expand(struct hmap *);
60 void hmap_shrink(struct hmap *);
61 void hmap_reserve(struct hmap *, size_t capacity);
62
63 /* Insertion and deletion. */
64 static inline void hmap_insert_fast(struct hmap *,
65                                     struct hmap_node *, size_t hash);
66 static inline void hmap_insert(struct hmap *, struct hmap_node *, size_t hash);
67 static inline void hmap_remove(struct hmap *, struct hmap_node *);
68 static inline void hmap_moved(struct hmap *,
69                               struct hmap_node *, struct hmap_node *);
70 static inline void hmap_replace(struct hmap *,
71                                 const struct hmap_node *old_node,
72                                 struct hmap_node *new_node);
73
74 /* Search. */
75 #define HMAP_FOR_EACH_WITH_HASH(NODE, STRUCT, MEMBER, HASH, HMAP)       \
76     for ((NODE) = CONTAINER_OF(hmap_first_with_hash(HMAP, HASH),        \
77                                STRUCT, MEMBER);                         \
78          &(NODE)->MEMBER != NULL;                                       \
79          (NODE) = CONTAINER_OF(hmap_next_with_hash(&(NODE)->MEMBER),    \
80                                STRUCT, MEMBER))
81
82 static inline struct hmap_node *hmap_first_with_hash(const struct hmap *,
83                                                      size_t hash);
84 static inline struct hmap_node *hmap_next_with_hash(const struct hmap_node *);
85
86 /* Iteration.
87  *
88  * The _SAFE version is needed when NODE may be freed.  It is not needed when
89  * NODE may be removed from the hash map but its members remain accessible and
90  * intact. */
91 #define HMAP_FOR_EACH(NODE, STRUCT, MEMBER, HMAP)                   \
92     for ((NODE) = CONTAINER_OF(hmap_first(HMAP), STRUCT, MEMBER);   \
93          &(NODE)->MEMBER != NULL;                                   \
94          (NODE) = CONTAINER_OF(hmap_next(HMAP, &(NODE)->MEMBER),    \
95                                STRUCT, MEMBER))
96
97 #define HMAP_FOR_EACH_SAFE(NODE, NEXT, STRUCT, MEMBER, HMAP)        \
98     for ((NODE) = CONTAINER_OF(hmap_first(HMAP), STRUCT, MEMBER);   \
99          (&(NODE)->MEMBER != NULL                                   \
100           ? (NEXT) = CONTAINER_OF(hmap_next(HMAP, &(NODE)->MEMBER), \
101                                   STRUCT, MEMBER), 1                \
102           : 0);                                                     \
103          (NODE) = (NEXT))
104
105 static inline struct hmap_node *hmap_first(const struct hmap *);
106 static inline struct hmap_node *hmap_next(const struct hmap *,
107                                           const struct hmap_node *);
108
109 /* Returns the number of nodes currently in 'hmap'. */
110 static inline size_t
111 hmap_count(const struct hmap *hmap)
112 {
113     return hmap->n;
114 }
115
116 /* Returns the maximum number of nodes that 'hmap' may hold before it should be
117  * rehashed. */
118 static inline size_t
119 hmap_capacity(const struct hmap *hmap)
120 {
121     return hmap->mask * 2 + 1;
122 }
123
124 /* Returns true if 'hmap' currently contains no nodes,
125  * false otherwise. */
126 static inline bool
127 hmap_is_empty(const struct hmap *hmap)
128 {
129     return hmap->n == 0;
130 }
131
132 /* Inserts 'node', with the given 'hash', into 'hmap'.  'hmap' is never
133  * expanded automatically. */
134 static inline void
135 hmap_insert_fast(struct hmap *hmap, struct hmap_node *node, size_t hash)
136 {
137     struct hmap_node **bucket = &hmap->buckets[hash & hmap->mask];
138     node->hash = hash;
139     node->next = *bucket;
140     *bucket = node;
141     hmap->n++;
142 }
143
144 /* Inserts 'node', with the given 'hash', into 'hmap', and expands 'hmap' if
145  * necessary to optimize search performance. */
146 static inline void
147 hmap_insert(struct hmap *hmap, struct hmap_node *node, size_t hash)
148 {
149     hmap_insert_fast(hmap, node, hash);
150     if (hmap->n / 2 > hmap->mask) {
151         hmap_expand(hmap);
152     }
153 }
154
155 /* Removes 'node' from 'hmap'.  Does not shrink the hash table; call
156  * hmap_shrink() directly if desired. */
157 static inline void
158 hmap_remove(struct hmap *hmap, struct hmap_node *node)
159 {
160     struct hmap_node **bucket = &hmap->buckets[node->hash & hmap->mask];
161     while (*bucket != node) {
162         bucket = &(*bucket)->next;
163     }
164     *bucket = node->next;
165     hmap->n--;
166 }
167
168 /* Adjusts 'hmap' to compensate for 'old_node' having moved position in memory
169  * to 'node' (e.g. due to realloc()). */
170 static inline void
171 hmap_moved(struct hmap *hmap,
172            struct hmap_node *old_node, struct hmap_node *node)
173 {
174     struct hmap_node **bucket = &hmap->buckets[node->hash & hmap->mask];
175     while (*bucket != old_node) {
176         bucket = &(*bucket)->next;
177     }
178     *bucket = node;
179 }
180
181 /* Puts 'new_node' in the position in 'hmap' currently occupied by 'old_node'.
182  * The 'new_node' must hash to the same value as 'old_node'.  The client is
183  * responsible for ensuring that the replacement does not violate any
184  * client-imposed invariants (e.g. uniqueness of keys within a map).
185  *
186  * Afterward, 'old_node' is not part of 'hmap', and the client is responsible
187  * for freeing it (if this is desirable). */
188 static inline void
189 hmap_replace(struct hmap *hmap,
190              const struct hmap_node *old_node, struct hmap_node *new_node)
191 {
192     struct hmap_node **bucket = &hmap->buckets[old_node->hash & hmap->mask];
193     while (*bucket != old_node) {
194         bucket = &(*bucket)->next;
195     }
196     *bucket = new_node;
197     new_node->hash = old_node->hash;
198 }
199
200 static inline struct hmap_node *
201 hmap_next_with_hash__(const struct hmap_node *node, size_t hash)
202 {
203     while (node != NULL && node->hash != hash) {
204         node = node->next;
205     }
206     return (struct hmap_node *) node;
207 }
208
209 /* Returns the first node in 'hmap' with the given 'hash', or a null pointer if
210  * no nodes have that hash value. */
211 static inline struct hmap_node *
212 hmap_first_with_hash(const struct hmap *hmap, size_t hash)
213 {
214     return hmap_next_with_hash__(hmap->buckets[hash & hmap->mask], hash);
215 }
216
217 /* Returns the next node in the same hash map as 'node' with the same hash
218  * value, or a null pointer if no more nodes have that hash value.
219  *
220  * If the hash map has been reallocated since 'node' was visited, some nodes
221  * may be skipped; if new nodes with the same hash value have been added, they
222  * will be skipped.  (Removing 'node' from the hash map does not prevent
223  * calling this function, since node->next is preserved, although freeing
224  * 'node' of course does.) */
225 static inline struct hmap_node *
226 hmap_next_with_hash(const struct hmap_node *node)
227 {
228     return hmap_next_with_hash__(node->next, node->hash);
229 }
230
231 static inline struct hmap_node *
232 hmap_next__(const struct hmap *hmap, size_t start)
233 {
234     size_t i;
235     for (i = start; i <= hmap->mask; i++) {
236         struct hmap_node *node = hmap->buckets[i];
237         if (node) {
238             return node;
239         }
240     }
241     return NULL;
242 }
243
244 /* Returns the first node in 'hmap', in arbitrary order, or a null pointer if
245  * 'hmap' is empty. */
246 static inline struct hmap_node *
247 hmap_first(const struct hmap *hmap)
248 {
249     return hmap_next__(hmap, 0);
250 }
251
252 /* Returns the next node in 'hmap' following 'node', in arbitrary order, or a
253  * null pointer if 'node' is the last node in 'hmap'.
254  *
255  * If the hash map has been reallocated since 'node' was visited, some nodes
256  * may be skipped or visited twice.  (Removing 'node' from the hash map does
257  * not prevent calling this function, since node->next is preserved, although
258  * freeing 'node' of course does.) */
259 static inline struct hmap_node *
260 hmap_next(const struct hmap *hmap, const struct hmap_node *node)
261 {
262     return (node->next
263             ? node->next
264             : hmap_next__(hmap, (node->hash & hmap->mask) + 1));
265 }
266
267 #ifdef  __cplusplus
268 }
269 #endif
270
271 #endif /* hmap.h */