From: Ben Pfaff Date: Fri, 16 Jan 2009 00:08:31 +0000 (-0800) Subject: New function svec_sort_unique(), svec_is_unique(), svec_get_duplicate(). X-Git-Url: https://pintos-os.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=67d36f44364dda3d5ac60d4f8bc31df7aeaa23d2;p=openvswitch New function svec_sort_unique(), svec_is_unique(), svec_get_duplicate(). --- diff --git a/lib/svec.c b/lib/svec.c index f0066392..728866b6 100644 --- a/lib/svec.c +++ b/lib/svec.c @@ -1,4 +1,4 @@ -/* Copyright (c) 2008 The Board of Trustees of The Leland Stanford +/* Copyright (c) 2008, 2009 The Board of Trustees of The Leland Stanford * Junior University * * We are making the OpenFlow specification and associated documentation @@ -121,6 +121,13 @@ svec_sort(struct svec *svec) qsort(svec->names, svec->n, sizeof *svec->names, compare_strings); } +void +svec_sort_unique(struct svec *svec) +{ + svec_sort(svec); + svec_unique(svec); +} + void svec_unique(struct svec *svec) { @@ -213,6 +220,27 @@ svec_is_sorted(const struct svec *svec) return true; } +bool +svec_is_unique(const struct svec *svec) +{ + return svec_get_duplicate(svec) == NULL; +} + +const char * +svec_get_duplicate(const struct svec *svec) +{ + assert(svec_is_sorted(svec)); + if (svec->n > 1) { + size_t i; + for (i = 1; i < svec->n; i++) { + if (!strcmp(svec->names[i - 1], svec->names[i])) { + return svec->names[i]; + } + } + } + return NULL; +} + void svec_swap(struct svec *a, struct svec *b) { diff --git a/lib/svec.h b/lib/svec.h index 2ee275de..084d2e85 100644 --- a/lib/svec.h +++ b/lib/svec.h @@ -1,4 +1,4 @@ -/* Copyright (c) 2008 The Board of Trustees of The Leland Stanford +/* Copyright (c) 2008, 2009 The Board of Trustees of The Leland Stanford * Junior University * * We are making the OpenFlow specification and associated documentation @@ -53,11 +53,14 @@ void svec_add_nocopy(struct svec *, char *); void svec_append(struct svec *, const struct svec *); void svec_terminate(struct svec *); void svec_sort(struct svec *); +void svec_sort_unique(struct svec *); void svec_unique(struct svec *); void svec_diff(const struct svec *a, const struct svec *b, struct svec *a_only, struct svec *both, struct svec *b_only); bool svec_contains(const struct svec *, const char *); bool svec_is_sorted(const struct svec *); +bool svec_is_unique(const struct svec *); +const char *svec_get_duplicate(const struct svec *); void svec_swap(struct svec *a, struct svec *b); void svec_print(const struct svec *svec, const char *title); void svec_parse_words(struct svec *svec, const char *words);