From 67d36f44364dda3d5ac60d4f8bc31df7aeaa23d2 Mon Sep 17 00:00:00 2001 From: Ben Pfaff Date: Thu, 15 Jan 2009 16:08:31 -0800 Subject: [PATCH] New function svec_sort_unique(), svec_is_unique(), svec_get_duplicate(). --- lib/svec.c | 30 +++++++++++++++++++++++++++++- lib/svec.h | 5 ++++- 2 files changed, 33 insertions(+), 2 deletions(-) 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); -- 2.30.2