diff options
author | Campbell Barton <ideasman42@gmail.com> | 2012-05-28 23:33:14 +0400 |
---|---|---|
committer | Campbell Barton <ideasman42@gmail.com> | 2012-05-28 23:33:14 +0400 |
commit | 4f90d757c79bc665c9e4d1cd32573d47e76d2ddd (patch) | |
tree | 6c4d74e8f9c985254e36304764e9b31eb3d15d53 /source/blender | |
parent | 131de4352b9e8f3e4ce77d2d9dc145f5db461aed (diff) |
add BLI_ghash_pop() which returns the value for a key and removes it at the same time, saves a lookup if you need to check if the item exists before removing.
Diffstat (limited to 'source/blender')
-rw-r--r-- | source/blender/blenlib/BLI_ghash.h | 1 | ||||
-rw-r--r-- | source/blender/blenlib/intern/BLI_ghash.c | 42 | ||||
-rw-r--r-- | source/blender/editors/curve/editcurve.c | 16 | ||||
-rw-r--r-- | source/blender/editors/sculpt_paint/paint_vertex.c | 3 |
4 files changed, 44 insertions, 18 deletions
diff --git a/source/blender/blenlib/BLI_ghash.h b/source/blender/blenlib/BLI_ghash.h index 9034e8e51d9..c26e6cd15b3 100644 --- a/source/blender/blenlib/BLI_ghash.h +++ b/source/blender/blenlib/BLI_ghash.h @@ -70,6 +70,7 @@ void BLI_ghash_free(GHash *gh, GHashKeyFreeFP keyfreefp, GHashValFreeFP valfre void BLI_ghash_insert(GHash *gh, void *key, void *val); void *BLI_ghash_lookup(GHash *gh, const void *key); int BLI_ghash_remove(GHash *gh, void *key, GHashKeyFreeFP keyfreefp, GHashValFreeFP valfreefp); +void *BLI_ghash_pop(GHash *gh, void *key, GHashKeyFreeFP keyfreefp); int BLI_ghash_haskey(GHash *gh, void *key); int BLI_ghash_size(GHash *gh); diff --git a/source/blender/blenlib/intern/BLI_ghash.c b/source/blender/blenlib/intern/BLI_ghash.c index 5cfde3dfb77..4bcedb06fb0 100644 --- a/source/blender/blenlib/intern/BLI_ghash.c +++ b/source/blender/blenlib/intern/BLI_ghash.c @@ -132,18 +132,14 @@ int BLI_ghash_remove(GHash *gh, void *key, GHashKeyFreeFP keyfreefp, GHashValFre if (gh->cmpfp(key, e->key) == 0) { Entry *n = e->next; - if (keyfreefp) - keyfreefp(e->key); - if (valfreefp) - valfreefp(e->val); + if (keyfreefp) keyfreefp(e->key); + if (valfreefp) valfreefp(e->val); BLI_mempool_free(gh->entrypool, e); /* correct but 'e' isn't used before return */ /* e= n; *//*UNUSED*/ - if (p) - p->next = n; - else - gh->buckets[hash] = n; + if (p) p->next = n; + else gh->buckets[hash] = n; gh->nentries--; return 1; @@ -154,6 +150,36 @@ int BLI_ghash_remove(GHash *gh, void *key, GHashKeyFreeFP keyfreefp, GHashValFre return 0; } +/* same as above but return the value, + * no free value argument since it will be returned */ +void *BLI_ghash_pop(GHash *gh, void *key, GHashKeyFreeFP keyfreefp) +{ + unsigned int hash = gh->hashfp(key) % gh->nbuckets; + Entry *e; + Entry *p = NULL; + + for (e = gh->buckets[hash]; e; e = e->next) { + if (gh->cmpfp(key, e->key) == 0) { + Entry *n = e->next; + void *value = e->val; + + if (keyfreefp) keyfreefp(e->key); + BLI_mempool_free(gh->entrypool, e); + + /* correct but 'e' isn't used before return */ + /* e= n; *//*UNUSED*/ + if (p) p->next = n; + else gh->buckets[hash] = n; + + gh->nentries--; + return value; + } + p = e; + } + + return NULL; +} + int BLI_ghash_haskey(GHash *gh, void *key) { unsigned int hash = gh->hashfp(key) % gh->nbuckets; diff --git a/source/blender/editors/curve/editcurve.c b/source/blender/editors/curve/editcurve.c index 492111ff8e7..b94d3653f60 100644 --- a/source/blender/editors/curve/editcurve.c +++ b/source/blender/editors/curve/editcurve.c @@ -356,6 +356,11 @@ static CVKeyIndex *getCVKeyIndex(EditNurb *editnurb, void *cv) return BLI_ghash_lookup(editnurb->keyindex, cv); } +static CVKeyIndex *popCVKeyIndex(EditNurb *editnurb, void *cv) +{ + return BLI_ghash_pop(editnurb->keyindex, cv, NULL); +} + static BezTriple *getKeyIndexOrig_bezt(EditNurb *editnurb, BezTriple *bezt) { CVKeyIndex *index = getCVKeyIndex(editnurb, bezt); @@ -459,9 +464,7 @@ static void keyIndex_updateCV(EditNurb *editnurb, char *cv, } for (i = 0; i < count; i++) { - index = getCVKeyIndex(editnurb, cv); - - BLI_ghash_remove(editnurb->keyindex, cv, NULL, NULL); + index = popCVKeyIndex(editnurb, cv); if (index) { BLI_ghash_insert(editnurb->keyindex, newcv, index); @@ -496,11 +499,8 @@ static void keyIndex_updateNurb(EditNurb *editnurb, Nurb *nu, Nurb *newnu) static void keyIndex_swap(EditNurb *editnurb, void *a, void *b) { - CVKeyIndex *index1 = getCVKeyIndex(editnurb, a); - CVKeyIndex *index2 = getCVKeyIndex(editnurb, b); - - BLI_ghash_remove(editnurb->keyindex, a, NULL, NULL); - BLI_ghash_remove(editnurb->keyindex, b, NULL, NULL); + CVKeyIndex *index1 = popCVKeyIndex(editnurb, a); + CVKeyIndex *index2 = popCVKeyIndex(editnurb, b); if (index2) BLI_ghash_insert(editnurb->keyindex, a, index2); if (index1) BLI_ghash_insert(editnurb->keyindex, b, index1); diff --git a/source/blender/editors/sculpt_paint/paint_vertex.c b/source/blender/editors/sculpt_paint/paint_vertex.c index c5c08a94ae5..e05191dad3b 100644 --- a/source/blender/editors/sculpt_paint/paint_vertex.c +++ b/source/blender/editors/sculpt_paint/paint_vertex.c @@ -2100,8 +2100,7 @@ static char *wpaint_make_validmap(Object *ob) if (chan->bone->flag & BONE_NO_DEFORM) continue; - if (BLI_ghash_haskey(gh, chan->name)) { - BLI_ghash_remove(gh, chan->name, NULL, NULL); + if (BLI_ghash_remove(gh, chan->name, NULL, NULL)) { BLI_ghash_insert(gh, chan->name, SET_INT_IN_POINTER(1)); } } |