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 | |
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')
-rw-r--r-- | source/blender/blenkernel/intern/anim.c | 6 | ||||
-rw-r--r-- | source/blender/blenkernel/intern/group.c | 22 | ||||
-rw-r--r-- | source/blender/blenkernel/intern/scene.c | 17 | ||||
-rw-r--r-- | source/blender/blenlib/BLI_listbase.h | 2 | ||||
-rw-r--r-- | source/blender/blenlib/intern/listbase.c | 38 | ||||
-rw-r--r-- | source/blender/bmesh/intern/bmesh_marking.c | 27 | ||||
-rw-r--r-- | source/blender/editors/gpencil/gpencil_paint.c | 9 | ||||
-rw-r--r-- | source/blender/editors/interface/resources.c | 4 | ||||
-rw-r--r-- | source/blender/editors/object/object_edit.c | 12 | ||||
-rw-r--r-- | source/blender/editors/object/object_modifier.c | 9 | ||||
-rw-r--r-- | source/blender/editors/object/object_select.c | 8 | ||||
-rw-r--r-- | source/blender/editors/sculpt_paint/sculpt_undo.c | 11 |
12 files changed, 86 insertions, 79 deletions
diff --git a/source/blender/blenkernel/intern/anim.c b/source/blender/blenkernel/intern/anim.c index 111ac68b345..33cdede6fce 100644 --- a/source/blender/blenkernel/intern/anim.c +++ b/source/blender/blenkernel/intern/anim.c @@ -1279,9 +1279,9 @@ static void new_particle_duplilist(ListBase *lb, ID *id, Scene *scene, Object *p if (part->dup_group == NULL || part->dup_group->gobject.first == NULL) return; - for (go = part->dup_group->gobject.first; go; go = go->next) - if (go->ob == par) - return; + if (BLI_findptr(&part->dup_group->gobject, par, offsetof(GroupObject, ob))) { + return; + } } /* if we have a hair particle system, use the path cache */ diff --git a/source/blender/blenkernel/intern/group.c b/source/blender/blenkernel/intern/group.c index 89341845615..500df1b7b75 100644 --- a/source/blender/blenkernel/intern/group.c +++ b/source/blender/blenkernel/intern/group.c @@ -161,11 +161,13 @@ static int add_to_group_internal(Group *group, Object *ob) { GroupObject *go; - if (group == NULL || ob == NULL) return 0; + if (group == NULL || ob == NULL) { + return FALSE; + } /* check if the object has been added already */ - for (go = group->gobject.first; go; go = go->next) { - if (go->ob == ob) return 0; + if (BLI_findptr(&group->gobject, ob, offsetof(GroupObject, ob))) { + return FALSE; } go = MEM_callocN(sizeof(GroupObject), "groupobject"); @@ -173,7 +175,7 @@ static int add_to_group_internal(Group *group, Object *ob) go->ob = ob; - return 1; + return TRUE; } int add_to_group(Group *group, Object *object, Scene *scene, Base *base) @@ -239,15 +241,11 @@ int rem_from_group(Group *group, Object *object, Scene *scene, Base *base) int object_in_group(Object *ob, Group *group) { - GroupObject *go; - - if (group == NULL || ob == NULL) return 0; - - for (go = group->gobject.first; go; go = go->next) { - if (go->ob == ob) - return 1; + if (group == NULL || ob == NULL) { + return FALSE; } - return 0; + + return (BLI_findptr(&group->gobject, ob, offsetof(GroupObject, ob)) != NULL); } Group *find_group(Object *ob, Group *group) diff --git a/source/blender/blenkernel/intern/scene.c b/source/blender/blenkernel/intern/scene.c index 889792ad3f8..d476e90498c 100644 --- a/source/blender/blenkernel/intern/scene.c +++ b/source/blender/blenkernel/intern/scene.c @@ -550,14 +550,7 @@ Scene *BKE_scene_add(const char *name) Base *BKE_scene_base_find(Scene *scene, Object *ob) { - Base *base; - - base = scene->base.first; - while (base) { - if (base->object == ob) return base; - base = base->next; - } - return NULL; + return BLI_findptr(&scene->base, ob, offsetof(Base, object)); } void BKE_scene_set_background(Main *bmain, Scene *scene) @@ -582,10 +575,10 @@ void BKE_scene_set_background(Main *bmain, Scene *scene) /* group flags again */ for (group = bmain->group.first; group; group = group->id.next) { - go = group->gobject.first; - while (go) { - if (go->ob) go->ob->flag |= OB_FROMGROUP; - go = go->next; + for (go = group->gobject.first; go; go = go->next) { + if (go->ob) { + go->ob->flag |= OB_FROMGROUP; + } } } 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; diff --git a/source/blender/bmesh/intern/bmesh_marking.c b/source/blender/bmesh/intern/bmesh_marking.c index 8e9a19b54fb..58ccfa79a02 100644 --- a/source/blender/bmesh/intern/bmesh_marking.c +++ b/source/blender/bmesh/intern/bmesh_marking.c @@ -31,6 +31,8 @@ * that should be addressed eventually. */ +#include <stddef.h> + #include "MEM_guardedalloc.h" #include "DNA_scene_types.h" @@ -708,28 +710,19 @@ void BM_editselection_plane(BMEditSelection *ese, float r_plane[3]) /* --- macro wrapped funcs --- */ int _bm_select_history_check(BMesh *bm, const BMHeader *ele) { - BMEditSelection *ese; - - for (ese = bm->selected.first; ese; ese = ese->next) { - if (ese->ele == (BMElem *)ele) { - return TRUE; - } - } - - return FALSE; + return (BLI_findptr(&bm->selected, ele, offsetof(BMEditSelection, ele)) != NULL); } int _bm_select_history_remove(BMesh *bm, BMHeader *ele) { - BMEditSelection *ese; - for (ese = bm->selected.first; ese; ese = ese->next) { - if (ese->ele == (BMElem *)ele) { - BLI_freelinkN(&(bm->selected), ese); - return TRUE; - } + BMEditSelection *ese = BLI_findptr(&bm->selected, ele, offsetof(BMEditSelection, ele)); + if (ese) { + BLI_freelinkN(&bm->selected, ese); + return TRUE; + } + else { + return FALSE; } - - return FALSE; } void _bm_select_history_store_notest(BMesh *bm, BMHeader *ele) diff --git a/source/blender/editors/gpencil/gpencil_paint.c b/source/blender/editors/gpencil/gpencil_paint.c index fd98dd83a7d..d98f4891dc5 100644 --- a/source/blender/editors/gpencil/gpencil_paint.c +++ b/source/blender/editors/gpencil/gpencil_paint.c @@ -1667,14 +1667,7 @@ static int gpencil_draw_invoke(bContext *C, wmOperator *op, wmEvent *event) static int gpencil_area_exists(bContext *C, ScrArea *sa_test) { bScreen *sc = CTX_wm_screen(C); - ScrArea *sa; - - for (sa = sc->areabase.first; sa; sa = sa->next) { - if (sa == sa_test) - return 1; - } - - return 0; + return (BLI_findindex(&sc->areabase, sa_test) != -1); } static tGPsdata *gpencil_stroke_begin(bContext *C, wmOperator *op) diff --git a/source/blender/editors/interface/resources.c b/source/blender/editors/interface/resources.c index 0ff81f27f6b..20e4360b791 100644 --- a/source/blender/editors/interface/resources.c +++ b/source/blender/editors/interface/resources.c @@ -643,9 +643,7 @@ void ui_theme_init_default(void) bTheme *btheme; /* we search for the theme with name Default */ - for (btheme = U.themes.first; btheme; btheme = btheme->next) { - if (strcmp("Default", btheme->name) == 0) break; - } + btheme = BLI_findstring(&U.themes, "Default", offsetof(bTheme, name)); if (btheme == NULL) { btheme = MEM_callocN(sizeof(bTheme), "theme"); diff --git a/source/blender/editors/object/object_edit.c b/source/blender/editors/object/object_edit.c index b5e85c3712b..aa885320b37 100644 --- a/source/blender/editors/object/object_edit.c +++ b/source/blender/editors/object/object_edit.c @@ -1720,13 +1720,15 @@ static int game_property_copy_exec(bContext *C, wmOperator *op) CTX_DATA_BEGIN(C, Object *, ob_iter, selected_editable_objects) { if (ob != ob_iter) { - if (type == COPY_PROPERTIES_REPLACE) + if (type == COPY_PROPERTIES_REPLACE) { copy_properties(&ob_iter->prop, &ob->prop); - - /* merge - the default when calling with no argument */ - else - for (prop = ob->prop.first; prop; prop = prop->next) + } + else { + /* merge - the default when calling with no argument */ + for (prop = ob->prop.first; prop; prop = prop->next) { set_ob_property(ob_iter, prop); + } + } } } CTX_DATA_END; diff --git a/source/blender/editors/object/object_modifier.c b/source/blender/editors/object/object_modifier.c index 47f5a285374..d3b099887cc 100644 --- a/source/blender/editors/object/object_modifier.c +++ b/source/blender/editors/object/object_modifier.c @@ -268,17 +268,12 @@ static int object_modifier_safe_to_delete(Main *bmain, Object *ob, static int object_modifier_remove(Main *bmain, Object *ob, ModifierData *md, int *sort_depsgraph) { - ModifierData *obmd; - /* It seems on rapid delete it is possible to * get called twice on same modifier, so make * sure it is in list. */ - for (obmd = ob->modifiers.first; obmd; obmd = obmd->next) - if (obmd == md) - break; - - if (!obmd) + if (BLI_findindex(&ob->modifiers, md) != -1) { return 0; + } /* special cases */ if (md->type == eModifierType_ParticleSystem) { diff --git a/source/blender/editors/object/object_select.c b/source/blender/editors/object/object_select.c index ff7728d4f68..7eb8cc01db9 100644 --- a/source/blender/editors/object/object_select.c +++ b/source/blender/editors/object/object_select.c @@ -988,13 +988,11 @@ static int object_select_same_group_exec(bContext *C, wmOperator *op) RNA_string_get(op->ptr, "group", group_name); - for (group = CTX_data_main(C)->group.first; group; group = group->id.next) { - if (!strcmp(group->id.name, group_name)) - break; - } + group = (Group *)BKE_libblock_find_name(ID_GR, group_name); - if (!group) + if (!group) { return OPERATOR_PASS_THROUGH; + } CTX_DATA_BEGIN (C, Base *, base, visible_bases) { diff --git a/source/blender/editors/sculpt_paint/sculpt_undo.c b/source/blender/editors/sculpt_paint/sculpt_undo.c index f327f67be33..c62dc687c73 100644 --- a/source/blender/editors/sculpt_paint/sculpt_undo.c +++ b/source/blender/editors/sculpt_paint/sculpt_undo.c @@ -32,6 +32,7 @@ * \ingroup edsculpt */ +#include <stddef.h> #include "MEM_guardedalloc.h" @@ -379,16 +380,12 @@ static void sculpt_undo_free(ListBase *lb) SculptUndoNode *sculpt_undo_get_node(PBVHNode *node) { ListBase *lb = undo_paint_push_get_list(UNDO_PAINT_MESH); - SculptUndoNode *unode; - if (!lb) + if (!lb) { return NULL; + } - for (unode = lb->first; unode; unode = unode->next) - if (unode->node == node) - return unode; - - return NULL; + return BLI_findptr(lb, node, offsetof(SculptUndoNode, node)); } static void sculpt_undo_alloc_and_store_hidden(PBVH *pbvh, |