]>
Commit | Line | Data |
---|---|---|
f391294f | 1 | /* |
05e43c5d | 2 | * Copyright (c) 2011, 2012, 2013, 2015, 2016 Nicira, Inc. |
f391294f BP |
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 | ||
ee89ea7b | 20 | #include "openvswitch/hmap.h" |
ebc56baa | 21 | #include "util.h" |
f391294f | 22 | |
43d1478b CB |
23 | #ifdef __cplusplus |
24 | extern "C" { | |
25 | #endif | |
26 | ||
f391294f BP |
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 | ||
3ae9a07a | 46 | /* String parsing and formatting. */ |
63a10e1e BP |
47 | void sset_from_delimited_string(struct sset *, const char *s, |
48 | const char *delimiters); | |
3ae9a07a BP |
49 | char *sset_join(const struct sset *, |
50 | const char *delimiter, const char *terminator); | |
63a10e1e | 51 | |
f391294f BP |
52 | /* Count. */ |
53 | bool sset_is_empty(const struct sset *); | |
54 | size_t sset_count(const struct sset *); | |
55 | ||
56 | /* Insertion. */ | |
57 | struct sset_node *sset_add(struct sset *, const char *); | |
58 | struct sset_node *sset_add_and_free(struct sset *, char *); | |
59 | void sset_add_assert(struct sset *, const char *); | |
60 | void sset_add_array(struct sset *, char **, size_t n); | |
61 | ||
62 | /* Deletion. */ | |
63 | void sset_clear(struct sset *); | |
64 | void sset_delete(struct sset *, struct sset_node *); | |
65 | bool sset_find_and_delete(struct sset *, const char *); | |
66 | void sset_find_and_delete_assert(struct sset *, const char *); | |
67 | char *sset_pop(struct sset *); | |
68 | ||
69 | /* Search. */ | |
70 | struct sset_node *sset_find(const struct sset *, const char *); | |
71 | bool sset_contains(const struct sset *, const char *); | |
72 | bool sset_equals(const struct sset *, const struct sset *); | |
bfbcebc2 DDP |
73 | |
74 | struct sset_position { | |
75 | struct hmap_position pos; | |
76 | }; | |
77 | ||
6822daf2 | 78 | struct sset_node *sset_at_position(const struct sset *, |
bfbcebc2 | 79 | struct sset_position *); |
f391294f | 80 | |
a4686ba3 BP |
81 | /* Set operations. */ |
82 | void sset_intersect(struct sset *, const struct sset *); | |
83 | ||
f391294f BP |
84 | /* Iteration macros. */ |
85 | #define SSET_FOR_EACH(NAME, SSET) \ | |
86 | for ((NAME) = SSET_FIRST(SSET); \ | |
55d26906 | 87 | NAME != NULL; \ |
f391294f BP |
88 | (NAME) = SSET_NEXT(SSET, NAME)) |
89 | ||
90 | #define SSET_FOR_EACH_SAFE(NAME, NEXT, SSET) \ | |
91 | for ((NAME) = SSET_FIRST(SSET); \ | |
55d26906 | 92 | (NAME != NULL \ |
f391294f BP |
93 | ? (NEXT) = SSET_NEXT(SSET, NAME), true \ |
94 | : false); \ | |
95 | (NAME) = (NEXT)) | |
808311f6 | 96 | |
842733c3 | 97 | const char **sset_array(const struct sset *); |
808311f6 | 98 | const char **sset_sort(const struct sset *); |
f391294f BP |
99 | \f |
100 | /* Implementation helper macros. */ | |
101 | ||
102 | #define SSET_NODE_FROM_HMAP_NODE(HMAP_NODE) \ | |
103 | CONTAINER_OF(HMAP_NODE, struct sset_node, hmap_node) | |
104 | #define SSET_NAME_FROM_HMAP_NODE(HMAP_NODE) \ | |
55d26906 AW |
105 | HMAP_NODE == NULL \ |
106 | ? NULL \ | |
107 | : (CONST_CAST(const char *, (SSET_NODE_FROM_HMAP_NODE(HMAP_NODE)->name))) | |
f391294f | 108 | #define SSET_NODE_FROM_NAME(NAME) CONTAINER_OF(NAME, struct sset_node, name) |
05e43c5d BP |
109 | #define SSET_FIRST(SSET) \ |
110 | (BUILD_ASSERT_TYPE(SSET, struct sset *), \ | |
111 | SSET_NAME_FROM_HMAP_NODE(hmap_first(&(SSET)->map))) | |
f391294f | 112 | #define SSET_NEXT(SSET, NAME) \ |
05e43c5d BP |
113 | (BUILD_ASSERT_TYPE(SSET, struct sset *), \ |
114 | SSET_NAME_FROM_HMAP_NODE( \ | |
115 | hmap_next(&(SSET)->map, &SSET_NODE_FROM_NAME(NAME)->hmap_node))) | |
f391294f | 116 | |
43d1478b CB |
117 | #ifdef __cplusplus |
118 | } | |
119 | #endif | |
120 | ||
f391294f | 121 | #endif /* sset.h */ |