]>
git.proxmox.com Git - mirror_ovs.git/blob - lib/svec.c
2 * Copyright (c) 2008, 2009, 2010, 2011, 2012 Nicira, Inc.
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:
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
22 #include "openvswitch/dynamic-string.h"
25 #include "openvswitch/vlog.h"
27 VLOG_DEFINE_THIS_MODULE(svec
);
30 svec_init(struct svec
*svec
)
38 svec_clone(struct svec
*svec
, const struct svec
*other
)
41 svec_append(svec
, other
);
45 svec_destroy(struct svec
*svec
)
52 svec_clear(struct svec
*svec
)
56 for (i
= 0; i
< svec
->n
; i
++) {
63 svec_is_empty(const struct svec
*svec
)
69 svec_add(struct svec
*svec
, const char *name
)
71 svec_add_nocopy(svec
, xstrdup(name
));
75 svec_del(struct svec
*svec
, const char *name
)
79 offset
= svec_find(svec
, name
);
80 if (offset
!= SIZE_MAX
) {
81 free(svec
->names
[offset
]);
82 memmove(&svec
->names
[offset
], &svec
->names
[offset
+ 1],
83 sizeof *svec
->names
* (svec
->n
- offset
- 1));
89 svec_expand(struct svec
*svec
)
91 if (svec
->n
>= svec
->allocated
) {
92 svec
->names
= x2nrealloc(svec
->names
, &svec
->allocated
,
98 svec_add_nocopy(struct svec
*svec
, char *name
)
101 svec
->names
[svec
->n
++] = name
;
105 svec_append(struct svec
*svec
, const struct svec
*other
)
108 for (i
= 0; i
< other
->n
; i
++) {
109 svec_add(svec
, other
->names
[i
]);
114 svec_terminate(struct svec
*svec
)
117 svec
->names
[svec
->n
] = NULL
;
121 compare_strings(const void *a_
, const void *b_
)
125 return strcmp(*a
, *b
);
129 svec_sort(struct svec
*svec
)
132 qsort(svec
->names
, svec
->n
, sizeof *svec
->names
, compare_strings
);
137 svec_sort_unique(struct svec
*svec
)
144 svec_unique(struct svec
*svec
)
146 ovs_assert(svec_is_sorted(svec
));
148 /* This algorithm is lazy and sub-optimal, but it's "obviously correct"
149 * and asymptotically optimal . */
154 svec_add(&tmp
, svec
->names
[0]);
155 for (i
= 1; i
< svec
->n
; i
++) {
156 if (strcmp(svec
->names
[i
- 1], svec
->names
[i
])) {
157 svec_add(&tmp
, svec
->names
[i
]);
160 svec_swap(&tmp
, svec
);
166 svec_compact(struct svec
*svec
)
170 for (i
= j
= 0; i
< svec
->n
; i
++) {
171 if (svec
->names
[i
] != NULL
) {
172 svec
->names
[j
++] = svec
->names
[i
];
179 swap_strings(char **a
, char **b
)
187 svec_shuffle(struct svec
*svec
)
189 for (size_t i
= 0; i
< svec
->n
; i
++) {
190 size_t j
= i
+ random_range(svec
->n
- i
);
191 swap_strings(&svec
->names
[i
], &svec
->names
[j
]);
196 svec_diff(const struct svec
*a
, const struct svec
*b
,
197 struct svec
*a_only
, struct svec
*both
, struct svec
*b_only
)
201 ovs_assert(svec_is_sorted(a
));
202 ovs_assert(svec_is_sorted(b
));
212 for (i
= j
= 0; i
< a
->n
&& j
< b
->n
; ) {
213 int cmp
= strcmp(a
->names
[i
], b
->names
[j
]);
216 svec_add(a_only
, a
->names
[i
]);
219 } else if (cmp
> 0) {
221 svec_add(b_only
, b
->names
[j
]);
226 svec_add(both
, a
->names
[i
]);
233 for (; i
< a
->n
; i
++) {
234 svec_add(a_only
, a
->names
[i
]);
238 for (; j
< b
->n
; j
++) {
239 svec_add(b_only
, b
->names
[j
]);
245 svec_contains(const struct svec
*svec
, const char *name
)
247 return svec_find(svec
, name
) != SIZE_MAX
;
251 svec_find(const struct svec
*svec
, const char *name
)
255 ovs_assert(svec_is_sorted(svec
));
256 p
= bsearch(&name
, svec
->names
, svec
->n
, sizeof *svec
->names
,
258 return p
? p
- svec
->names
: SIZE_MAX
;
262 svec_is_sorted(const struct svec
*svec
)
266 for (i
= 1; i
< svec
->n
; i
++) {
267 if (strcmp(svec
->names
[i
- 1], svec
->names
[i
]) > 0) {
275 svec_is_unique(const struct svec
*svec
)
277 return svec_get_duplicate(svec
) == NULL
;
281 svec_get_duplicate(const struct svec
*svec
)
283 ovs_assert(svec_is_sorted(svec
));
286 for (i
= 1; i
< svec
->n
; i
++) {
287 if (!strcmp(svec
->names
[i
- 1], svec
->names
[i
])) {
288 return svec
->names
[i
];
296 svec_swap(struct svec
*a
, struct svec
*b
)
298 struct svec tmp
= *a
;
304 svec_print(const struct svec
*svec
, const char *title
)
308 printf("%s:\n", title
);
309 for (i
= 0; i
< svec
->n
; i
++) {
310 printf("\"%s\"\n", svec
->names
[i
]);
314 /* Breaks 'words' into words at white space, respecting shell-like quoting
315 * conventions, and appends the words to 'svec'. */
317 svec_parse_words(struct svec
*svec
, const char *words
)
319 struct ds word
= DS_EMPTY_INITIALIZER
;
322 for (p
= words
; *p
!= '\0'; p
= q
) {
325 while (isspace((unsigned char) *p
)) {
333 for (q
= p
; *q
!= '\0'; q
++) {
336 } else if (*q
== '\'' || *q
== '"') {
338 } else if (*q
== '\\' && (!quote
|| quote
== '"')) {
341 VLOG_WARN("%s: ends in trailing backslash", words
);
344 ds_put_char(&word
, *q
);
345 } else if (isspace((unsigned char) *q
) && !quote
) {
349 ds_put_char(&word
, *q
);
352 svec_add(svec
, ds_cstr(&word
));
354 VLOG_WARN("%s: word ends inside quoted string", words
);
361 svec_equal(const struct svec
*a
, const struct svec
*b
)
368 for (i
= 0; i
< a
->n
; i
++) {
369 if (strcmp(a
->names
[i
], b
->names
[i
])) {
377 svec_join(const struct svec
*svec
,
378 const char *delimiter
, const char *terminator
)
384 for (i
= 0; i
< svec
->n
; i
++) {
386 ds_put_cstr(&ds
, delimiter
);
388 ds_put_cstr(&ds
, svec
->names
[i
]);
390 ds_put_cstr(&ds
, terminator
);
395 svec_back(const struct svec
*svec
)
398 return svec
->names
[svec
->n
- 1];
402 svec_pop_back(struct svec
*svec
)
405 free(svec
->names
[--svec
->n
]);