Merge "master" branch into "db".
[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 /* A hash map node, to be embedded inside the data structure being mapped. */
25 struct hmap_node {
26     size_t hash;                /* Hash value. */
27     struct hmap_node *next;     /* Next in linked list. */
28 };
29
30 /* Returns the hash value embedded in 'node'. */
31 static inline size_t hmap_node_hash(const struct hmap_node *node)
32 {
33     return node->hash;
34 }
35
36 /* A hash map. */
37 struct hmap {
38     struct hmap_node **buckets;
39     struct hmap_node *one;
40     size_t mask;
41     size_t n;
42 };
43
44 /* Initializer for an empty hash map. */
45 #define HMAP_INITIALIZER(HMAP) { &(HMAP)->one, NULL, 0, 0 }
46
47 /* Initialization. */
48 void hmap_init(struct hmap *);
49 void hmap_destroy(struct hmap *);
50 void hmap_swap(struct hmap *a, struct hmap *b);
51 static inline size_t hmap_count(const struct hmap *);
52 static inline bool hmap_is_empty(const struct hmap *);
53
54 /* Adjusting capacity. */
55 void hmap_expand(struct hmap *);
56 void hmap_shrink(struct hmap *);
57 void hmap_reserve(struct hmap *, size_t capacity);
58
59 /* Insertion and deletion. */
60 static inline void hmap_insert_fast(struct hmap *,
61                                     struct hmap_node *, size_t hash);
62 static inline void hmap_insert(struct hmap *, struct hmap_node *, size_t hash);
63 static inline void hmap_remove(struct hmap *, struct hmap_node *);
64 static inline void hmap_moved(struct hmap *,
65                               struct hmap_node *, struct hmap_node *);
66 static inline void hmap_replace(struct hmap *, const struct hmap_node *old,
67                                 struct hmap_node *new);
68
69 /* Search.
70  *
71  * HMAP_FOR_EACH_WITH_HASH iterates NODE over all of the nodes in HMAP that
72  * have hash value equal to HASH.  HMAP_FOR_EACH_IN_BUCKET iterates NODE over
73  * all of the nodes in HMAP that would fall in the same bucket as HASH.  STRUCT
74  * and MEMBER must be the name of the struct that contains the 'struct
75  * hmap_node' and the name of the 'struct hmap_node' member, respectively.
76  *
77  * These macros may be used interchangeably to search for a particular value in
78  * an hmap, see, e.g. shash_find() for an example.  Usually, using
79  * HMAP_FOR_EACH_WITH_HASH provides an optimization, because comparing a hash
80  * value is usually cheaper than comparing an entire hash map key.  But for
81  * simple hash map keys, it makes sense to use HMAP_FOR_EACH_IN_BUCKET because
82  * it avoids doing two comparisons when a single simple comparison suffices.
83  *
84  * The loop should not change NODE to point to a different node or insert or
85  * delete nodes in HMAP (unless it "break"s out of the loop to terminate
86  * iteration).
87  *
88  * HASH is only evaluated once.
89  */
90 #define HMAP_FOR_EACH_WITH_HASH(NODE, STRUCT, MEMBER, HASH, HMAP)       \
91     for ((NODE) = CONTAINER_OF(hmap_first_with_hash(HMAP, HASH),        \
92                                STRUCT, MEMBER);                         \
93          &(NODE)->MEMBER != NULL;                                       \
94          (NODE) = CONTAINER_OF(hmap_next_with_hash(&(NODE)->MEMBER),    \
95                                STRUCT, MEMBER))
96 #define HMAP_FOR_EACH_IN_BUCKET(NODE, STRUCT, MEMBER, HASH, HMAP)       \
97     for ((NODE) = CONTAINER_OF(hmap_first_in_bucket(HMAP, HASH),        \
98                                STRUCT, MEMBER);                         \
99          &(NODE)->MEMBER != NULL;                                       \
100          (NODE) = CONTAINER_OF(hmap_next_in_bucket(&(NODE)->MEMBER),    \
101                                STRUCT, MEMBER))
102
103 static inline struct hmap_node *hmap_first_with_hash(const struct hmap *,
104                                                      size_t hash);
105 static inline struct hmap_node *hmap_next_with_hash(const struct hmap_node *);
106 static inline struct hmap_node *hmap_first_in_bucket(const struct hmap *,
107                                                      size_t hash);
108 static inline struct hmap_node *hmap_next_in_bucket(const struct hmap_node *);
109
110 /* Iteration.
111  *
112  * The _SAFE version is needed when NODE may be freed.  It is not needed when
113  * NODE may be removed from the hash map but its members remain accessible and
114  * intact. */
115 #define HMAP_FOR_EACH(NODE, STRUCT, MEMBER, HMAP)                   \
116     for ((NODE) = CONTAINER_OF(hmap_first(HMAP), STRUCT, MEMBER);   \
117          &(NODE)->MEMBER != NULL;                                   \
118          (NODE) = CONTAINER_OF(hmap_next(HMAP, &(NODE)->MEMBER),    \
119                                STRUCT, MEMBER))
120
121 #define HMAP_FOR_EACH_SAFE(NODE, NEXT, STRUCT, MEMBER, HMAP)        \
122     for ((NODE) = CONTAINER_OF(hmap_first(HMAP), STRUCT, MEMBER);   \
123          (&(NODE)->MEMBER != NULL                                   \
124           ? (NEXT) = CONTAINER_OF(hmap_next(HMAP, &(NODE)->MEMBER), \
125                                   STRUCT, MEMBER), 1                \
126           : 0);                                                     \
127          (NODE) = (NEXT))
128
129 static inline struct hmap_node *hmap_first(const struct hmap *);
130 static inline struct hmap_node *hmap_next(const struct hmap *,
131                                           const struct hmap_node *);
132
133 /* Returns the number of nodes currently in 'hmap'. */
134 static inline size_t
135 hmap_count(const struct hmap *hmap)
136 {
137     return hmap->n;
138 }
139
140 /* Returns the maximum number of nodes that 'hmap' may hold before it should be
141  * rehashed. */
142 static inline size_t
143 hmap_capacity(const struct hmap *hmap)
144 {
145     return hmap->mask * 2 + 1;
146 }
147
148 /* Returns true if 'hmap' currently contains no nodes,
149  * false otherwise. */
150 static inline bool
151 hmap_is_empty(const struct hmap *hmap)
152 {
153     return hmap->n == 0;
154 }
155
156 /* Inserts 'node', with the given 'hash', into 'hmap'.  'hmap' is never
157  * expanded automatically. */
158 static inline void
159 hmap_insert_fast(struct hmap *hmap, struct hmap_node *node, size_t hash)
160 {
161     struct hmap_node **bucket = &hmap->buckets[hash & hmap->mask];
162     node->hash = hash;
163     node->next = *bucket;
164     *bucket = node;
165     hmap->n++;
166 }
167
168 /* Inserts 'node', with the given 'hash', into 'hmap', and expands 'hmap' if
169  * necessary to optimize search performance. */
170 static inline void
171 hmap_insert(struct hmap *hmap, struct hmap_node *node, size_t hash)
172 {
173     hmap_insert_fast(hmap, node, hash);
174     if (hmap->n / 2 > hmap->mask) {
175         hmap_expand(hmap);
176     }
177 }
178
179 /* Removes 'node' from 'hmap'.  Does not shrink the hash table; call
180  * hmap_shrink() directly if desired. */
181 static inline void
182 hmap_remove(struct hmap *hmap, struct hmap_node *node)
183 {
184     struct hmap_node **bucket = &hmap->buckets[node->hash & hmap->mask];
185     while (*bucket != node) {
186         bucket = &(*bucket)->next;
187     }
188     *bucket = node->next;
189     hmap->n--;
190 }
191
192 /* Adjusts 'hmap' to compensate for 'old_node' having moved position in memory
193  * to 'node' (e.g. due to realloc()). */
194 static inline void
195 hmap_moved(struct hmap *hmap,
196            struct hmap_node *old_node, struct hmap_node *node)
197 {
198     struct hmap_node **bucket = &hmap->buckets[node->hash & hmap->mask];
199     while (*bucket != old_node) {
200         bucket = &(*bucket)->next;
201     }
202     *bucket = node;
203 }
204
205 /* Puts 'new' in the position in 'hmap' currently occupied by 'old'.  The 'new'
206  * node must hash to the same value as 'old'.  The client is responsible for
207  * ensuring that the replacement does not violate any client-imposed
208  * invariants (e.g. uniqueness of keys within a map).
209  *
210  * Afterward, 'old' is not part of 'hmap', and the client is responsible for
211  * freeing it (if this is desirable). */
212 static inline void
213 hmap_replace(struct hmap *hmap,
214              const struct hmap_node *old, struct hmap_node *new)
215 {
216     struct hmap_node **bucket = &hmap->buckets[old->hash & hmap->mask];
217     while (*bucket != old) {
218         bucket = &(*bucket)->next;
219     }
220     *bucket = new;
221     new->hash = old->hash;
222     new->next = old->next;
223 }
224
225 static inline struct hmap_node *
226 hmap_next_with_hash__(const struct hmap_node *node, size_t hash)
227 {
228     while (node != NULL && node->hash != hash) {
229         node = node->next;
230     }
231     return (struct hmap_node *) node;
232 }
233
234 /* Returns the first node in 'hmap' with the given 'hash', or a null pointer if
235  * no nodes have that hash value. */
236 static inline struct hmap_node *
237 hmap_first_with_hash(const struct hmap *hmap, size_t hash)
238 {
239     return hmap_next_with_hash__(hmap->buckets[hash & hmap->mask], hash);
240 }
241
242 /* Returns the first node in 'hmap' in the bucket in which the given 'hash'
243  * would land, or a null pointer if that bucket is empty. */
244 static inline struct hmap_node *
245 hmap_first_in_bucket(const struct hmap *hmap, size_t hash)
246 {
247     return hmap->buckets[hash & hmap->mask];
248 }
249
250 /* Returns the next node in the same bucket as 'node', or a null pointer if
251  * there are no more nodes in that bucket.
252  *
253  * If the hash map has been reallocated since 'node' was visited, some nodes
254  * may be skipped; if new nodes with the same hash value have been added, they
255  * will be skipped.  (Removing 'node' from the hash map does not prevent
256  * calling this function, since node->next is preserved, although freeing
257  * 'node' of course does.) */
258 static inline struct hmap_node *
259 hmap_next_in_bucket(const struct hmap_node *node)
260 {
261     return node->next;
262 }
263
264 /* Returns the next node in the same hash map as 'node' with the same hash
265  * value, or a null pointer if no more nodes have that hash value.
266  *
267  * If the hash map has been reallocated since 'node' was visited, some nodes
268  * may be skipped; if new nodes with the same hash value have been added, they
269  * will be skipped.  (Removing 'node' from the hash map does not prevent
270  * calling this function, since node->next is preserved, although freeing
271  * 'node' of course does.) */
272 static inline struct hmap_node *
273 hmap_next_with_hash(const struct hmap_node *node)
274 {
275     return hmap_next_with_hash__(node->next, node->hash);
276 }
277
278 static inline struct hmap_node *
279 hmap_next__(const struct hmap *hmap, size_t start)
280 {
281     size_t i;
282     for (i = start; i <= hmap->mask; i++) {
283         struct hmap_node *node = hmap->buckets[i];
284         if (node) {
285             return node;
286         }
287     }
288     return NULL;
289 }
290
291 /* Returns the first node in 'hmap', in arbitrary order, or a null pointer if
292  * 'hmap' is empty. */
293 static inline struct hmap_node *
294 hmap_first(const struct hmap *hmap)
295 {
296     return hmap_next__(hmap, 0);
297 }
298
299 /* Returns the next node in 'hmap' following 'node', in arbitrary order, or a
300  * null pointer if 'node' is the last node in 'hmap'.
301  *
302  * If the hash map has been reallocated since 'node' was visited, some nodes
303  * may be skipped or visited twice.  (Removing 'node' from the hash map does
304  * not prevent calling this function, since node->next is preserved, although
305  * freeing 'node' of course does.) */
306 static inline struct hmap_node *
307 hmap_next(const struct hmap *hmap, const struct hmap_node *node)
308 {
309     return (node->next
310             ? node->next
311             : hmap_next__(hmap, (node->hash & hmap->mask) + 1));
312 }
313
314 #endif /* hmap.h */