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_insert(GHash *gh, void *key, void *val);
void *BLI_ghash_lookup(GHash *gh, const void *key); void *BLI_ghash_lookup(GHash *gh, const void *key);
int BLI_ghash_remove(GHash *gh, void *key, GHashKeyFreeFP keyfreefp, GHashValFreeFP valfreefp); 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_haskey(GHash *gh, void *key);
int BLI_ghash_size(GHash *gh); 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) { if (gh->cmpfp(key, e->key) == 0) {
Entry *n = e->next; Entry *n = e->next;
if (keyfreefp) if (keyfreefp) keyfreefp(e->key);
keyfreefp(e->key); if (valfreefp) valfreefp(e->val);
if (valfreefp)
valfreefp(e->val);
BLI_mempool_free(gh->entrypool, e); BLI_mempool_free(gh->entrypool, e);
/* correct but 'e' isn't used before return */ /* correct but 'e' isn't used before return */
/* e= n; *//*UNUSED*/ /* e= n; *//*UNUSED*/
if (p) if (p) p->next = n;
p->next = n; else gh->buckets[hash] = n;
else
gh->buckets[hash] = n;
gh->nentries--; gh->nentries--;
return 1; return 1;
@@ -154,6 +150,36 @@ int BLI_ghash_remove(GHash *gh, void *key, GHashKeyFreeFP keyfreefp, GHashValFre
return 0; 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) int BLI_ghash_haskey(GHash *gh, void *key)
{ {
unsigned int hash = gh->hashfp(key) % gh->nbuckets; 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); 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) static BezTriple *getKeyIndexOrig_bezt(EditNurb *editnurb, BezTriple *bezt)
{ {
CVKeyIndex *index = getCVKeyIndex(editnurb, bezt); CVKeyIndex *index = getCVKeyIndex(editnurb, bezt);
@@ -459,9 +464,7 @@ static void keyIndex_updateCV(EditNurb *editnurb, char *cv,
} }
for (i = 0; i < count; i++) { for (i = 0; i < count; i++) {
index = getCVKeyIndex(editnurb, cv); index = popCVKeyIndex(editnurb, cv);
BLI_ghash_remove(editnurb->keyindex, cv, NULL, NULL);
if (index) { if (index) {
BLI_ghash_insert(editnurb->keyindex, newcv, 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) static void keyIndex_swap(EditNurb *editnurb, void *a, void *b)
{ {
CVKeyIndex *index1 = getCVKeyIndex(editnurb, a); CVKeyIndex *index1 = popCVKeyIndex(editnurb, a);
CVKeyIndex *index2 = getCVKeyIndex(editnurb, b); CVKeyIndex *index2 = popCVKeyIndex(editnurb, b);
BLI_ghash_remove(editnurb->keyindex, a, NULL, NULL);
BLI_ghash_remove(editnurb->keyindex, b, NULL, NULL);
if (index2) BLI_ghash_insert(editnurb->keyindex, a, index2); if (index2) BLI_ghash_insert(editnurb->keyindex, a, index2);
if (index1) BLI_ghash_insert(editnurb->keyindex, b, index1); 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) if (chan->bone->flag & BONE_NO_DEFORM)
continue; continue;
if (BLI_ghash_haskey(gh, chan->name)) { if (BLI_ghash_remove(gh, chan->name, NULL, NULL)) {
BLI_ghash_remove(gh, chan->name, NULL, NULL);
BLI_ghash_insert(gh, chan->name, SET_INT_IN_POINTER(1)); BLI_ghash_insert(gh, chan->name, SET_INT_IN_POINTER(1));
} }
} }