list: New functions list_is_singleton(), list_is_short().
authorBen Pfaff <blp@nicira.com>
Thu, 24 Mar 2011 16:40:07 +0000 (09:40 -0700)
committerBen Pfaff <blp@nicira.com>
Fri, 1 Apr 2011 22:52:19 +0000 (15:52 -0700)
lib/list.c
lib/list.h
tests/test-list.c

index 0f4f2f8..b5fa389 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2008, 2009, 2010 Nicira Networks.
+ * Copyright (c) 2008, 2009, 2010, 2011 Nicira Networks.
  *
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
@@ -168,3 +168,17 @@ list_is_empty(const struct list *list)
 {
     return list->next == list;
 }
+
+/* Returns true if 'list' has exactly 1 element, false otherwise. */
+bool
+list_is_singleton(const struct list *list)
+{
+    return list_is_short(list) && !list_is_empty(list);
+}
+
+/* Returns true if 'list' has 0 or 1 elements, false otherwise. */
+bool
+list_is_short(const struct list *list)
+{
+    return list->next == list->prev;
+}
index 915ee85..91c3966 100644 (file)
@@ -53,6 +53,8 @@ struct list *list_back(const struct list *);
 /* List properties. */
 size_t list_size(const struct list *);
 bool list_is_empty(const struct list *);
+bool list_is_singleton(const struct list *);
+bool list_is_short(const struct list *);
 
 #define LIST_FOR_EACH(ITER, MEMBER, LIST)                               \
     for (ASSIGN_CONTAINER(ITER, (LIST)->next, MEMBER);                  \
index 5e62e0c..b4ddd02 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2008, 2009, 2010 Nicira Networks.
+ * Copyright (c) 2008, 2009, 2010, 2011 Nicira Networks.
  *
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
@@ -73,6 +73,8 @@ check_list(struct list *list, const int values[], size_t n)
     assert(i == n);
 
     assert(list_is_empty(list) == !n);
+    assert(list_is_singleton(list) == (n == 1));
+    assert(list_is_short(list) == (n < 2));
     assert(list_size(list) == n);
 }