From 0e60accf2afa4fc69da99743bb64d82cb3e0fbc4 Mon Sep 17 00:00:00 2001 From: Campbell Barton Date: Sun, 16 Nov 2014 14:02:18 +0100 Subject: BLI_listbase: Add BLI_listbase_count_ex (sets a limit) This can be used to avoid redundant looping when we only want to know if a list is smaller then some size. also remove paranoid NULL check in list counting. --- source/blender/blenlib/intern/listbase.c | 22 +++++++++++++++------- 1 file changed, 15 insertions(+), 7 deletions(-) (limited to 'source/blender/blenlib/intern/listbase.c') diff --git a/source/blender/blenlib/intern/listbase.c b/source/blender/blenlib/intern/listbase.c index cd2e343560f..ea33b9b1e3b 100644 --- a/source/blender/blenlib/intern/listbase.c +++ b/source/blender/blenlib/intern/listbase.c @@ -372,6 +372,17 @@ void BLI_freelistN(ListBase *listbase) BLI_listbase_clear(listbase); } +int BLI_listbase_count_ex(const ListBase *listbase, const int count_max) +{ + Link *link; + int count = 0; + + for (link = listbase->first; link && count != count_max; link = link->next) { + count++; + } + + return count; +} /** * Returns the number of elements in \a listbase. @@ -380,14 +391,11 @@ int BLI_listbase_count(const ListBase *listbase) { Link *link; int count = 0; - - if (listbase) { - link = listbase->first; - while (link) { - count++; - link = link->next; - } + + for (link = listbase->first; link; link = link->next) { + count++; } + return count; } -- cgit v1.2.3