diff options
author | Campbell Barton <ideasman42@gmail.com> | 2012-08-18 20:16:13 +0400 |
---|---|---|
committer | Campbell Barton <ideasman42@gmail.com> | 2012-08-18 20:16:13 +0400 |
commit | 27b4b45543c0f7690a1978a60591a0b5c0f1adbb (patch) | |
tree | 14c54494059de30d8d4c8a24ec8400b8bcc83ff5 /source/blender/blenlib | |
parent | e982e9b04f13be046d194643ed28aaedd6181f3b (diff) |
utility functions: BLI_findptr, BLI_rfindptr --- use for finding an item in a linked list by a pointer.
Diffstat (limited to 'source/blender/blenlib')
-rw-r--r-- | source/blender/blenlib/BLI_listbase.h | 2 | ||||
-rw-r--r-- | source/blender/blenlib/intern/listbase.c | 38 |
2 files changed, 40 insertions, 0 deletions
diff --git a/source/blender/blenlib/BLI_listbase.h b/source/blender/blenlib/BLI_listbase.h index abe7eacb1ac..1330a74bea3 100644 --- a/source/blender/blenlib/BLI_listbase.h +++ b/source/blender/blenlib/BLI_listbase.h @@ -48,11 +48,13 @@ int BLI_findstringindex(const struct ListBase *listbase, const char *id, const i void *BLI_findlink(const struct ListBase *listbase, int number); void *BLI_findstring(const struct ListBase *listbase, const char *id, const int offset); void *BLI_findstring_ptr(const struct ListBase *listbase, const char *id, const int offset); +void *BLI_findptr(const struct ListBase *listbase, const void *ptr, const int offset); /* find backwards */ void *BLI_rfindlink(const struct ListBase *listbase, int number); void *BLI_rfindstring(const struct ListBase *listbase, const char *id, const int offset); void *BLI_rfindstring_ptr(const struct ListBase *listbase, const char *id, const int offset); +void *BLI_rfindptr(const struct ListBase *listbase, const void *ptr, const int offset); void BLI_freelistN(struct ListBase *listbase); void BLI_addtail(struct ListBase *listbase, void *vlink); diff --git a/source/blender/blenlib/intern/listbase.c b/source/blender/blenlib/intern/listbase.c index 8fe9a94b466..ad718ed8e11 100644 --- a/source/blender/blenlib/intern/listbase.c +++ b/source/blender/blenlib/intern/listbase.c @@ -439,6 +439,44 @@ void *BLI_rfindstring_ptr(const ListBase *listbase, const char *id, const int of return NULL; } +void *BLI_findptr(const ListBase *listbase, const void *ptr, const int offset) +{ + Link *link = NULL; + const void *ptr_iter; + + if (listbase == NULL) return NULL; + + for (link = listbase->first; link; link = link->next) { + /* exact copy of BLI_findstring(), except for this line */ + ptr_iter = *((const char **)(((const char *)link) + offset)); + + if (ptr == ptr_iter) { + return link; + } + } + + return NULL; +} +/* same as above but find reverse */ +void *BLI_rfindptr(const ListBase *listbase, const void *ptr, const int offset) +{ + Link *link = NULL; + const void *ptr_iter; + + if (listbase == NULL) return NULL; + + for (link = listbase->last; link; link = link->prev) { + /* exact copy of BLI_rfindstring(), except for this line */ + ptr_iter = *((const char **)(((const char *)link) + offset)); + + if (ptr == ptr_iter) { + return link; + } + } + + return NULL; +} + int BLI_findstringindex(const ListBase *listbase, const char *id, const int offset) { Link *link = NULL; |