From 41a496fe998f5d5d18df89c60c7272b33e2c4908 Mon Sep 17 00:00:00 2001 From: Andrea Mazzoleni Date: Mon, 19 Jan 2015 22:22:36 +0100 Subject: [PATCH] Add a new list count operation to TommyDS --- cmdline/device.c | 6 +----- tommyds/tommylist.h | 17 +++++++++++++++++ 2 files changed, 18 insertions(+), 5 deletions(-) diff --git a/cmdline/device.c b/cmdline/device.c index ae8d72a0..8e439578 100644 --- a/cmdline/device.c +++ b/cmdline/device.c @@ -517,11 +517,7 @@ void state_device(struct snapraid_state* state, int operation) #endif if (operation == DEVICE_SMART) { - /* count the logical disks forming the array */ - unsigned count = state->level; - for (i = state->disklist; i != 0; i = i->next) - ++count; - state_smart(count, &low); + state_smart(state->level + tommy_list_count(&state->disklist), &low); } tommy_list_foreach(&high, free); diff --git a/tommyds/tommylist.h b/tommyds/tommylist.h index ccee71af..8004a44a 100644 --- a/tommyds/tommylist.h +++ b/tommyds/tommylist.h @@ -298,6 +298,23 @@ tommy_inline tommy_bool_t tommy_list_empty(tommy_list* list) return tommy_list_head(list) == 0; } +/** + * Gets the number of elements. + * \note This operation is O(n). + */ +tommy_inline tommy_count_t tommy_list_count(tommy_list* list) +{ + tommy_count_t count = 0; + tommy_node* i = tommy_list_head(list); + + while (i) { + ++count; + i = i->next; + } + + return count; +} + /** * Calls the specified function for each element in the list. *