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/intern/BLI_ghash.c | |
parent | 556b13f03e561b54d4f0186e207f080c786f8b66 (diff) |
GSet: utils to access data stored outside the set
Diffstat (limited to 'source/blender/blenlib/intern/BLI_ghash.c')
-rw-r--r-- | source/blender/blenlib/intern/BLI_ghash.c | 35 |
1 files changed, 35 insertions, 0 deletions
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 * \{ */ |