[Bf-blender-cvs] [9cbf374814c] master: GSet: utils to access data stored outside the set

Campbell Barton noreply at git.blender.org
Wed Nov 15 12:55:49 CET 2017


Commit: 9cbf374814ce04a17d37cef7797fb5686646b62c
Author: Campbell Barton
Date:   Wed Nov 15 22:45:37 2017 +1100
Branches: master
https://developer.blender.org/rB9cbf374814ce04a17d37cef7797fb5686646b62c

GSet: utils to access data stored outside the set

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

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

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

diff --git a/source/blender/blenlib/BLI_ghash.h b/source/blender/blenlib/BLI_ghash.h
index b42a36a3567..2720c0058b7 100644
--- a/source/blender/blenlib/BLI_ghash.h
+++ b/source/blender/blenlib/BLI_ghash.h
@@ -257,6 +257,10 @@ void   BLI_gset_clear_ex(GSet *gs, GSetKeyFreeFP keyfreefp,
                          const unsigned int nentries_reserve);
 void   BLI_gset_clear(GSet *gs, GSetKeyFreeFP keyfreefp);
 
+/* When set's are used for key & value. */
+void  *BLI_gset_lookup(GSet *gh, const void *key) ATTR_WARN_UNUSED_RESULT;
+void  *BLI_gset_pop_key(GSet *gs, const void *key) ATTR_WARN_UNUSED_RESULT;
+
 GSet *BLI_gset_ptr_new_ex(const char *info, const unsigned int nentries_reserve) ATTR_MALLOC ATTR_WARN_UNUSED_RESULT;
 GSet *BLI_gset_ptr_new(const char *info);
 GSet *BLI_gset_str_new_ex(const char *info, const unsigned int nentries_reserve) ATTR_MALLOC ATTR_WARN_UNUSED_RESULT;
diff --git a/source/blender/blenlib/intern/BLI_ghash.c b/source/blender/blenlib/intern/BLI_ghash.c
index 1b2a27e33d8..5067a13cfa3 100644
--- a/source/blender/blenlib/intern/BLI_ghash.c
+++ b/source/blender/blenlib/intern/BLI_ghash.c
@@ -1533,6 +1533,41 @@ void BLI_gset_flag_clear(GSet *gs, unsigned int flag)
 
 /** \} */
 
+/** \name GSet Combined Key/Value Usage
+ *
+ * \note Not typical ``set`` use, only use when the pointer identity matters.
+ * This can be useful when the key references data stored outside the GSet.
+ * \{ */
+
+/**
+ * Returns the pointer to the key if it's found.
+ */
+void *BLI_gset_lookup(GSet *gs, const void *key)
+{
+	Entry *e = ghash_lookup_entry((GHash *)gs, key);
+	return e ? e->key : NULL;
+}
+
+/**
+ * Returns the pointer to the key if it's found, removing it from the GSet.
+ * \node Caller must handle freeing.
+ */
+void *BLI_gset_pop_key(GSet *gs, const void *key)
+{
+	const unsigned int hash = ghash_keyhash((GHash *)gs, key);
+	const unsigned int bucket_index = ghash_bucket_index((GHash *)gs, hash);
+	Entry *e = ghash_remove_ex((GHash *)gs, key, NULL, NULL, bucket_index);
+	if (e) {
+		void *key_ret = e->key;
+		BLI_mempool_free(((GHash *)gs)->entrypool, e);
+		return key_ret;
+	}
+	else {
+		return NULL;
+	}
+}
+
+/** \} */
 
 /** \name Convenience GSet Creation Functions
  * \{ */



More information about the Bf-blender-cvs mailing list