diff options
author | Campbell Barton <ideasman42@gmail.com> | 2017-11-15 14:45:37 +0300 |
---|---|---|
committer | Campbell Barton <ideasman42@gmail.com> | 2017-11-15 14:45:37 +0300 |
commit | 9cbf374814ce04a17d37cef7797fb5686646b62c (patch) | |
tree | c3dec117bdc505c168f7fd918c174ab844db8845 /source/blender/blenlib | |
parent | 556b13f03e561b54d4f0186e207f080c786f8b66 (diff) |
GSet: utils to access data stored outside the set
Diffstat (limited to 'source/blender/blenlib')
-rw-r--r-- | source/blender/blenlib/BLI_ghash.h | 4 | ||||
-rw-r--r-- | source/blender/blenlib/intern/BLI_ghash.c | 35 |
2 files changed, 39 insertions, 0 deletions
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 * \{ */ |