[Bf-blender-cvs] [0e60acc] master: BLI_listbase: Add BLI_listbase_count_ex (sets a limit)

Campbell Barton noreply at git.blender.org
Sun Nov 16 14:31:05 CET 2014


Commit: 0e60accf2afa4fc69da99743bb64d82cb3e0fbc4
Author: Campbell Barton
Date:   Sun Nov 16 14:02:18 2014 +0100
Branches: master
https://developer.blender.org/rB0e60accf2afa4fc69da99743bb64d82cb3e0fbc4

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.

===================================================================

M	source/blender/blenlib/BLI_listbase.h
M	source/blender/blenlib/intern/listbase.c

===================================================================

diff --git a/source/blender/blenlib/BLI_listbase.h b/source/blender/blenlib/BLI_listbase.h
index 543a662..2e01f74 100644
--- a/source/blender/blenlib/BLI_listbase.h
+++ b/source/blender/blenlib/BLI_listbase.h
@@ -70,6 +70,7 @@ void BLI_insertlinkafter(struct ListBase *listbase, void *vprevlink, void *vnewl
 void BLI_listbase_sort(struct ListBase *listbase, int (*cmp)(const void *, const void *)) ATTR_NONNULL(1, 2);
 void BLI_listbase_sort_r(ListBase *listbase, void *thunk, int (*cmp)(void *, const void *, const void *)) ATTR_NONNULL(1, 3);
 void BLI_freelist(struct ListBase *listbase) ATTR_NONNULL(1);
+int  BLI_listbase_count_ex(const struct ListBase *listbase, const int count_max) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1);
 int  BLI_listbase_count(const struct ListBase *listbase) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1);
 void BLI_freelinkN(struct ListBase *listbase, void *vlink) ATTR_NONNULL(1);
 
diff --git a/source/blender/blenlib/intern/listbase.c b/source/blender/blenlib/intern/listbase.c
index cd2e343..ea33b9b 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;
 }




More information about the Bf-blender-cvs mailing list