add BLI_ghash_pop() which returns the value for a key and removes it at the same...
authorCampbell Barton <ideasman42@gmail.com>
Mon, 28 May 2012 19:33:14 +0000 (19:33 +0000)
committerCampbell Barton <ideasman42@gmail.com>
Mon, 28 May 2012 19:33:14 +0000 (19:33 +0000)
source/blender/blenlib/BLI_ghash.h
source/blender/blenlib/intern/BLI_ghash.c
source/blender/editors/curve/editcurve.c
source/blender/editors/sculpt_paint/paint_vertex.c

index 9034e8e..c26e6cd 100644 (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);
 
index 5cfde3d..4bcedb0 100644 (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;
index 492111f..b94d365 100644 (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);
index c5c08a9..e05191d 100644 (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));
                                        }
                                }