dpif-netlink: add GENEVE creation support
[cascardo/ovs.git] / lib / sset.h
1 /*
2  * Copyright (c) 2011, 2012, 2013, 2015, 2016 Nicira, Inc.
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 #include "util.h"
22
23 #ifdef __cplusplus
24 extern "C" {
25 #endif
26
27 struct sset_node {
28     struct hmap_node hmap_node;
29     char name[1];
30 };
31
32 /* A set of strings. */
33 struct sset {
34     struct hmap map;
35 };
36
37 #define SSET_INITIALIZER(SSET) { HMAP_INITIALIZER(&(SSET)->map) }
38
39 /* Basics. */
40 void sset_init(struct sset *);
41 void sset_destroy(struct sset *);
42 void sset_clone(struct sset *, const struct sset *);
43 void sset_swap(struct sset *, struct sset *);
44 void sset_moved(struct sset *);
45
46 /* Count. */
47 bool sset_is_empty(const struct sset *);
48 size_t sset_count(const struct sset *);
49
50 /* Insertion. */
51 struct sset_node *sset_add(struct sset *, const char *);
52 struct sset_node *sset_add_and_free(struct sset *, char *);
53 void sset_add_assert(struct sset *, const char *);
54 void sset_add_array(struct sset *, char **, size_t n);
55
56 /* Deletion. */
57 void sset_clear(struct sset *);
58 void sset_delete(struct sset *, struct sset_node *);
59 bool sset_find_and_delete(struct sset *, const char *);
60 void sset_find_and_delete_assert(struct sset *, const char *);
61 char *sset_pop(struct sset *);
62
63 /* Search. */
64 struct sset_node *sset_find(const struct sset *, const char *);
65 bool sset_contains(const struct sset *, const char *);
66 bool sset_equals(const struct sset *, const struct sset *);
67
68 struct sset_position {
69     struct hmap_position pos;
70 };
71
72 struct sset_node *sset_at_position(const struct sset *,
73                                    struct sset_position *);
74
75 /* Set operations. */
76 void sset_intersect(struct sset *, const struct sset *);
77
78 /* Iteration macros. */
79 #define SSET_FOR_EACH(NAME, SSET)               \
80     for ((NAME) = SSET_FIRST(SSET);             \
81          NAME != NULL;                          \
82          (NAME) = SSET_NEXT(SSET, NAME))
83
84 #define SSET_FOR_EACH_SAFE(NAME, NEXT, SSET)        \
85     for ((NAME) = SSET_FIRST(SSET);                 \
86          (NAME != NULL                              \
87           ? (NEXT) = SSET_NEXT(SSET, NAME), true    \
88           : false);                                 \
89          (NAME) = (NEXT))
90
91 const char **sset_array(const struct sset *);
92 const char **sset_sort(const struct sset *);
93 \f
94 /* Implementation helper macros. */
95
96 #define SSET_NODE_FROM_HMAP_NODE(HMAP_NODE) \
97     CONTAINER_OF(HMAP_NODE, struct sset_node, hmap_node)
98 #define SSET_NAME_FROM_HMAP_NODE(HMAP_NODE) \
99     HMAP_NODE == NULL                       \
100     ? NULL                                  \
101     : (CONST_CAST(const char *, (SSET_NODE_FROM_HMAP_NODE(HMAP_NODE)->name)))
102 #define SSET_NODE_FROM_NAME(NAME) CONTAINER_OF(NAME, struct sset_node, name)
103 #define SSET_FIRST(SSET)                                    \
104     (BUILD_ASSERT_TYPE(SSET, struct sset *),                \
105      SSET_NAME_FROM_HMAP_NODE(hmap_first(&(SSET)->map)))
106 #define SSET_NEXT(SSET, NAME)                                           \
107     (BUILD_ASSERT_TYPE(SSET, struct sset *),                            \
108      SSET_NAME_FROM_HMAP_NODE(                                          \
109          hmap_next(&(SSET)->map, &SSET_NODE_FROM_NAME(NAME)->hmap_node)))
110
111 #ifdef __cplusplus
112 }
113 #endif
114
115 #endif /* sset.h */