[Bf-blender-cvs] SVN commit: /data/svn/bf-blender [47134] trunk/blender/source/blender: 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.

Campbell Barton ideasman42 at gmail.com
Mon May 28 21:33:15 CEST 2012


Revision: 47134
          http://projects.blender.org/scm/viewvc.php?view=rev&root=bf-blender&revision=47134
Author:   campbellbarton
Date:     2012-05-28 19:33:14 +0000 (Mon, 28 May 2012)
Log Message:
-----------
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.

Modified Paths:
--------------
    trunk/blender/source/blender/blenlib/BLI_ghash.h
    trunk/blender/source/blender/blenlib/intern/BLI_ghash.c
    trunk/blender/source/blender/editors/curve/editcurve.c
    trunk/blender/source/blender/editors/sculpt_paint/paint_vertex.c

Modified: trunk/blender/source/blender/blenlib/BLI_ghash.h
===================================================================
--- trunk/blender/source/blender/blenlib/BLI_ghash.h	2012-05-28 19:21:13 UTC (rev 47133)
+++ trunk/blender/source/blender/blenlib/BLI_ghash.h	2012-05-28 19:33:14 UTC (rev 47134)
@@ -70,6 +70,7 @@
 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);
 

Modified: trunk/blender/source/blender/blenlib/intern/BLI_ghash.c
===================================================================
--- trunk/blender/source/blender/blenlib/intern/BLI_ghash.c	2012-05-28 19:21:13 UTC (rev 47133)
+++ trunk/blender/source/blender/blenlib/intern/BLI_ghash.c	2012-05-28 19:33:14 UTC (rev 47134)
@@ -132,18 +132,14 @@
 		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 @@
 	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;

Modified: trunk/blender/source/blender/editors/curve/editcurve.c
===================================================================
--- trunk/blender/source/blender/editors/curve/editcurve.c	2012-05-28 19:21:13 UTC (rev 47133)
+++ trunk/blender/source/blender/editors/curve/editcurve.c	2012-05-28 19:33:14 UTC (rev 47134)
@@ -356,6 +356,11 @@
 	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,10 +464,8 @@
 	}
 
 	for (i = 0; i < count; i++) {
-		index = getCVKeyIndex(editnurb, cv);
+		index = popCVKeyIndex(editnurb, cv);
 
-		BLI_ghash_remove(editnurb->keyindex, cv, NULL, NULL);
-
 		if (index) {
 			BLI_ghash_insert(editnurb->keyindex, newcv, index);
 		}
@@ -496,12 +499,9 @@
 
 static void keyIndex_swap(EditNurb *editnurb, void *a, void *b)
 {
-	CVKeyIndex *index1 = getCVKeyIndex(editnurb, a);
-	CVKeyIndex *index2 = getCVKeyIndex(editnurb, b);
+	CVKeyIndex *index1 = popCVKeyIndex(editnurb, a);
+	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 (index1) BLI_ghash_insert(editnurb->keyindex, b, index1);
 }

Modified: trunk/blender/source/blender/editors/sculpt_paint/paint_vertex.c
===================================================================
--- trunk/blender/source/blender/editors/sculpt_paint/paint_vertex.c	2012-05-28 19:21:13 UTC (rev 47133)
+++ trunk/blender/source/blender/editors/sculpt_paint/paint_vertex.c	2012-05-28 19:33:14 UTC (rev 47134)
@@ -2100,8 +2100,7 @@
 					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));
 					}
 				}




More information about the Bf-blender-cvs mailing list