BLI: Support negative steps in findlinkfrom

The function `BLI_findlinkfrom` returns the link that is n-steps
after the given start link. This did not work for negative steps.

This change makes it so that both positive and negative step
values work.
This commit is contained in:
Falk David
2023-10-09 13:39:26 +02:00
parent 2bafffd9f0
commit d0ee4f2283
2 changed files with 14 additions and 6 deletions

View File

@@ -45,9 +45,10 @@ void *BLI_findlink(const struct ListBase *listbase, int number) ATTR_WARN_UNUSED
ATTR_NONNULL(1);
/**
* Returns the nth element after \a link, numbering from 0.
* Returns the element before/after \a link that is \a step links away, numbering from 0. \a step
* is allowed to be negative. Returns NULL when the link is out-of-bounds.
*/
void *BLI_findlinkfrom(struct Link *start, int number) ATTR_WARN_UNUSED_RESULT;
void *BLI_findlinkfrom(struct Link *start, int step) ATTR_WARN_UNUSED_RESULT;
/**
* Finds the first element of \a listbase which contains the null-terminated

View File

@@ -560,17 +560,24 @@ void *BLI_rfindlink(const ListBase *listbase, int number)
return link;
}
void *BLI_findlinkfrom(Link *start, int number)
void *BLI_findlinkfrom(Link *start, int steps)
{
Link *link = nullptr;
if (number >= 0) {
if (steps >= 0) {
link = start;
while (link != nullptr && number != 0) {
number--;
while (link != nullptr && steps != 0) {
steps--;
link = link->next;
}
}
else {
link = start;
while (link != nullptr && steps != 0) {
steps++;
link = link->prev;
}
}
return link;
}