From: Ben Pfaff Date: Thu, 24 Mar 2011 16:40:07 +0000 (-0700) Subject: list: New functions list_is_singleton(), list_is_short(). X-Git-Url: https://pintos-os.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=dc1539ae02fcb9c22b98d587b790ce8bc3a68774;p=openvswitch list: New functions list_is_singleton(), list_is_short(). --- diff --git a/lib/list.c b/lib/list.c index 0f4f2f84..b5fa3893 100644 --- a/lib/list.c +++ b/lib/list.c @@ -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; +} diff --git a/lib/list.h b/lib/list.h index 915ee854..91c39663 100644 --- a/lib/list.h +++ b/lib/list.h @@ -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); \ diff --git a/tests/test-list.c b/tests/test-list.c index 5e62e0c0..b4ddd02e 100644 --- a/tests/test-list.c +++ b/tests/test-list.c @@ -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); }