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.
This commit is contained in:
Campbell Barton
2014-11-16 14:02:18 +01:00
parent 7d040d2a08
commit 0e60accf2a
2 changed files with 16 additions and 7 deletions

View File

@@ -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);

View File

@@ -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;
}