Prepare Open vSwitch 1.1.2 release.
[sliver-openvswitch.git] / lib / sset.h
1 /*
2  * Copyright (c) 2011 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 SSET_H
18 #define SSET_H
19
20 #include "hmap.h"
21
22 struct sset_node {
23     struct hmap_node hmap_node;
24     char name[1];
25 };
26
27 /* A set of strings. */
28 struct sset {
29     struct hmap map;
30 };
31
32 #define SSET_INITIALIZER(SSET) { HMAP_INITIALIZER(&(SSET)->map) }
33
34 /* Basics. */
35 void sset_init(struct sset *);
36 void sset_destroy(struct sset *);
37 void sset_clone(struct sset *, const struct sset *);
38 void sset_swap(struct sset *, struct sset *);
39 void sset_moved(struct sset *);
40
41 /* Count. */
42 bool sset_is_empty(const struct sset *);
43 size_t sset_count(const struct sset *);
44
45 /* Insertion. */
46 struct sset_node *sset_add(struct sset *, const char *);
47 struct sset_node *sset_add_and_free(struct sset *, char *);
48 void sset_add_assert(struct sset *, const char *);
49 void sset_add_array(struct sset *, char **, size_t n);
50
51 /* Deletion. */
52 void sset_clear(struct sset *);
53 void sset_delete(struct sset *, struct sset_node *);
54 bool sset_find_and_delete(struct sset *, const char *);
55 void sset_find_and_delete_assert(struct sset *, const char *);
56 char *sset_pop(struct sset *);
57
58 /* Search. */
59 struct sset_node *sset_find(const struct sset *, const char *);
60 bool sset_contains(const struct sset *, const char *);
61 bool sset_equals(const struct sset *, const struct sset *);
62
63 /* Iteration macros. */
64 #define SSET_FOR_EACH(NAME, SSET)               \
65     for ((NAME) = SSET_FIRST(SSET);             \
66          SSET_NODE_FROM_NAME(NAME) != NULL;     \
67          (NAME) = SSET_NEXT(SSET, NAME))
68
69 #define SSET_FOR_EACH_SAFE(NAME, NEXT, SSET)        \
70     for ((NAME) = SSET_FIRST(SSET);                 \
71          (SSET_NODE_FROM_NAME(NAME) != NULL         \
72           ? (NEXT) = SSET_NEXT(SSET, NAME), true    \
73           : false);                                 \
74          (NAME) = (NEXT))
75 \f
76 /* Implementation helper macros. */
77
78 #define SSET_NODE_FROM_HMAP_NODE(HMAP_NODE) \
79     CONTAINER_OF(HMAP_NODE, struct sset_node, hmap_node)
80 #define SSET_NAME_FROM_HMAP_NODE(HMAP_NODE) \
81     ((const char *) (SSET_NODE_FROM_HMAP_NODE(HMAP_NODE)->name))
82 #define SSET_NODE_FROM_NAME(NAME) CONTAINER_OF(NAME, struct sset_node, name)
83 #define SSET_FIRST(SSET) SSET_NAME_FROM_HMAP_NODE(hmap_first(&(SSET)->map))
84 #define SSET_NEXT(SSET, NAME)                                           \
85     SSET_NAME_FROM_HMAP_NODE(                                           \
86         hmap_next(&(SSET)->map, &SSET_NODE_FROM_NAME(NAME)->hmap_node))
87
88 #endif /* sset.h */