[Bf-blender-cvs] [02bc062] temp-ghash-basis: Split expand/shrink into own functions

Campbell Barton noreply at git.blender.org
Thu Mar 19 16:37:27 CET 2015


Commit: 02bc06203643f601e87de5cd80216b1f81a7deba
Author: Campbell Barton
Date:   Fri Mar 20 02:35:54 2015 +1100
Branches: temp-ghash-basis
https://developer.blender.org/rB02bc06203643f601e87de5cd80216b1f81a7deba

Split expand/shrink into own functions

===================================================================

M	source/blender/blenlib/intern/BLI_ghash.c

===================================================================

diff --git a/source/blender/blenlib/intern/BLI_ghash.c b/source/blender/blenlib/intern/BLI_ghash.c
index 9597024..18a0e49 100644
--- a/source/blender/blenlib/intern/BLI_ghash.c
+++ b/source/blender/blenlib/intern/BLI_ghash.c
@@ -116,7 +116,7 @@ struct GHash {
 };
 
 
-BLI_INLINE void entry_copy(
+BLI_INLINE void ghash_entry_copy(
         GHash *gh_dst, Entry *dst, GHash *gh_src, Entry *src,
         const unsigned int UNUSED(hash), GHashKeyCopyFP keycopyfp, GHashValCopyFP valcopyfp)
 {
@@ -169,7 +169,7 @@ BLI_INLINE unsigned int ghash_bucket_hash(GHash *gh, const unsigned int full_has
 /**
  * Expand buckets to the next size up or down.
  */
-BLI_INLINE void ghash_resize_buckets(GHash *gh, const unsigned int nbuckets)
+static void ghash_buckets_resize(GHash *gh, const unsigned int nbuckets)
 {
 	Entry **buckets_old = gh->buckets;
 	Entry **buckets_new;
@@ -237,12 +237,12 @@ BLI_INLINE void ghash_resize_buckets(GHash *gh, const unsigned int nbuckets)
  * Check if the number of items in the GHash is large enough to require more buckets,
  * or small enough to require less buckets, and resize \a gh accordingly.
  */
-BLI_INLINE void ghash_expand_buckets(
-        GHash *gh, const unsigned int nentries, const bool user_defined, const bool force_shrink)
+BLI_INLINE void ghash_buckets_expand(
+        GHash *gh, const unsigned int nentries, const bool user_defined)
 {
 	unsigned int new_nbuckets;
 
-	if (LIKELY(gh->buckets && (nentries < gh->limit_grow) && (nentries > gh->limit_shrink))) {
+	if (LIKELY(gh->buckets && (nentries < gh->limit_grow))) {
 		return;
 	}
 
@@ -264,6 +264,34 @@ BLI_INLINE void ghash_expand_buckets(
 	}
 #endif
 
+	if (user_defined) {
+#ifdef GHASH_USE_MODULO_BUCKETS
+		gh->size_min = gh->cursize;
+#else
+		gh->bucket_bit_min = gh->bucket_bit;
+#endif
+	}
+
+	if ((new_nbuckets == gh->nbuckets) && gh->buckets) {
+		return;
+	}
+
+	gh->limit_grow = GHASH_LIMIT_GROW(new_nbuckets);
+	gh->limit_shrink = GHASH_LIMIT_SHRINK(new_nbuckets);
+	ghash_buckets_resize(gh, new_nbuckets);
+}
+
+BLI_INLINE void ghash_buckets_shrink(
+        GHash *gh, const unsigned int nentries, const bool user_defined, const bool force_shrink)
+{
+	unsigned int new_nbuckets;
+
+	if (LIKELY(gh->buckets && (nentries > gh->limit_shrink))) {
+		return;
+	}
+
+	new_nbuckets = gh->nbuckets;
+
 	if (force_shrink || (gh->flag & GHASH_FLAG_ALLOW_SHRINK)) {
 #ifdef GHASH_USE_MODULO_BUCKETS
 		while ((nentries    < gh->limit_shrink) &&
@@ -296,7 +324,7 @@ BLI_INLINE void ghash_expand_buckets(
 
 	gh->limit_grow = GHASH_LIMIT_GROW(new_nbuckets);
 	gh->limit_shrink = GHASH_LIMIT_SHRINK(new_nbuckets);
-	ghash_resize_buckets(gh, new_nbuckets);
+	ghash_buckets_resize(gh, new_nbuckets);
 }
 
 /**
@@ -323,7 +351,8 @@ BLI_INLINE void ghash_buckets_reset(GHash *gh, const unsigned int nentries)
 	gh->nentries = 0;
 	gh->flag = 0;
 
-	ghash_expand_buckets(gh, nentries, (nentries != 0), false);
+	ghash_buckets_expand(gh, nentries, (nentries != 0));
+	ghash_buckets_shrink(gh, nentries, (nentries != 0), false);
 }
 
 /**
@@ -413,7 +442,7 @@ BLI_INLINE void ghash_insert_ex(
 	e->val = val;
 	gh->buckets[bucket_hash] = (Entry *)e;
 
-	ghash_expand_buckets(gh, ++gh->nentries, false, false);
+	ghash_buckets_expand(gh, ++gh->nentries, false);
 }
 
 /**
@@ -431,7 +460,7 @@ BLI_INLINE void ghash_insert_ex_keyonly(
 	e->key = key;
 	gh->buckets[bucket_hash] = (Entry *)e;
 
-	ghash_expand_buckets(gh, ++gh->nentries, false, false);
+	ghash_buckets_expand(gh, ++gh->nentries, false);
 }
 
 BLI_INLINE void ghash_insert(GHash *gh, void *key, void *val)
@@ -506,7 +535,7 @@ static Entry *ghash_remove_ex(
 		if (e_prev) e_prev->next = e->next;
 		else gh->buckets[bucket_hash] = e->next;
 
-		ghash_expand_buckets(gh, --gh->nentries, false, false);
+		ghash_buckets_shrink(gh, --gh->nentries, false, false);
 	}
 
 	return e;
@@ -544,7 +573,8 @@ static GHash *ghash_copy(GHash *gh, GHashKeyCopyFP keycopyfp, GHashValCopyFP val
 	BLI_assert(!valcopyfp || !is_gset);
 
 	gh_new = ghash_new(gh->hashfp, gh->cmpfp, __func__, 0, is_gset);
-	ghash_expand_buckets(gh_new, gh->nentries, false, false);
+	ghash_buckets_expand(gh_new, gh->nentries, false);
+	ghash_buckets_shrink(gh_new, gh->nentries, false, false);
 
 	for (i = 0; i < gh->nbuckets; i++) {
 		Entry *e;
@@ -554,7 +584,7 @@ static GHash *ghash_copy(GHash *gh, GHashKeyCopyFP keycopyfp, GHashValCopyFP val
 			const unsigned int hash = ghash_entryhash(gh, e);
 			const unsigned int gh_new_bucket_hash = ghash_bucket_hash(gh_new, hash);
 
-			entry_copy(gh_new, e_new, gh, e, hash, keycopyfp, valcopyfp);
+			ghash_entry_copy(gh_new, e_new, gh, e, hash, keycopyfp, valcopyfp);
 
 			/* Warning! This means entries in buckets in new copy will be in reversed order!
 			 *          This shall not be an issue though, since order should never be assumed in ghash. */
@@ -612,7 +642,8 @@ GHash *BLI_ghash_copy(GHash *gh, GHashKeyCopyFP keycopyfp, GHashValCopyFP valcop
  */
 void BLI_ghash_reserve(GHash *gh, const unsigned int nentries_reserve)
 {
-	ghash_expand_buckets(gh, nentries_reserve, true, false);
+	ghash_buckets_expand(gh, nentries_reserve, true);
+	ghash_buckets_shrink(gh, nentries_reserve, true, false);
 }
 
 /**




More information about the Bf-blender-cvs mailing list