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.

This commit is contained in:
Campbell Barton
2012-05-28 19:33:14 +00:00
parent 131de4352b
commit 4f90d757c7
4 changed files with 44 additions and 18 deletions

View File

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

View File

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

View File

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

View File

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