2 * ***** BEGIN GPL LICENSE BLOCK *****
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version 2
7 * of the License, or (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software Foundation,
16 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
19 * All rights reserved.
21 * The Original Code is: all of this file.
23 * Contributor(s): none yet.
25 * ***** END GPL LICENSE BLOCK *****
28 /** \file blender/blenlib/intern/BLI_ghash.c
31 * A general (pointer -> pointer) chaining hash table
32 * for 'Abstract Data Types' (known as an ADT Hash Table).
34 * \note edgehash.c is based on this, make sure they stay in sync.
42 #include "MEM_guardedalloc.h"
44 #include "BLI_sys_types.h" /* for intptr_t support */
45 #include "BLI_utildefines.h"
46 #include "BLI_hash_mm2a.h"
47 #include "BLI_mempool.h"
49 #define GHASH_INTERNAL_API
50 #include "BLI_ghash.h"
51 #include "BLI_strict_flags.h"
53 #define GHASH_USE_MODULO_BUCKETS
55 /* Also used by smallhash! */
56 const unsigned int hashsizes[] = {
57 5, 11, 17, 37, 67, 131, 257, 521, 1031, 2053, 4099, 8209,
58 16411, 32771, 65537, 131101, 262147, 524309, 1048583, 2097169,
59 4194319, 8388617, 16777259, 33554467, 67108879, 134217757,
63 #ifdef GHASH_USE_MODULO_BUCKETS
64 # define GHASH_MAX_SIZE 27
66 # define GHASH_BUCKET_BIT_MIN 2
67 # define GHASH_BUCKET_BIT_MAX 28 /* About 268M of buckets... */
71 * \note Max load #GHASH_LIMIT_GROW used to be 3. (pre 2.74).
72 * Python uses 0.6666, tommyhaslib even goes down to 0.5.
73 * Reducing our from 3 to 0.75 gives huge speedup (about twice quicker pure GHash insertions/lookup,
74 * about 25% - 30% quicker 'dynamic-topology' stroke drawing e.g.).
75 * Min load #GHASH_LIMIT_SHRINK is a quarter of max load, to avoid resizing to quickly.
77 #define GHASH_LIMIT_GROW(_nbkt) (((_nbkt) * 3) / 4)
78 #define GHASH_LIMIT_SHRINK(_nbkt) (((_nbkt) * 3) / 16)
82 /* WARNING! Keep in sync with ugly _gh_Entry in header!!! */
83 typedef struct Entry {
89 typedef struct GHashEntry {
95 typedef Entry GSetEntry;
97 #define GHASH_ENTRY_SIZE(_is_gset) \
98 ((_is_gset) ? sizeof(GSetEntry) : sizeof(GHashEntry))
105 struct BLI_mempool *entrypool;
106 unsigned int nbuckets;
107 unsigned int limit_grow, limit_shrink;
108 #ifdef GHASH_USE_MODULO_BUCKETS
109 unsigned int cursize, size_min;
111 unsigned int bucket_mask, bucket_bit, bucket_bit_min;
114 unsigned int nentries;
119 BLI_INLINE void ghash_entry_copy(
120 GHash *gh_dst, Entry *dst, GHash *gh_src, Entry *src,
121 GHashKeyCopyFP keycopyfp, GHashValCopyFP valcopyfp)
123 dst->key = (keycopyfp) ? keycopyfp(src->key) : src->key;
125 if ((gh_dst->flag & GHASH_FLAG_IS_GSET) == 0) {
126 if ((gh_src->flag & GHASH_FLAG_IS_GSET) == 0) {
127 ((GHashEntry *)dst)->val = (valcopyfp) ? valcopyfp(((GHashEntry *)src)->val) : ((GHashEntry *)src)->val;
130 ((GHashEntry *)dst)->val = NULL;
135 /* -------------------------------------------------------------------- */
138 /** \name Internal Utility API
142 * Get the full hash for a key.
144 BLI_INLINE unsigned int ghash_keyhash(GHash *gh, const void *key)
146 return gh->hashfp(key);
150 * Get the full hash for an entry.
152 BLI_INLINE unsigned int ghash_entryhash(GHash *gh, const Entry *e)
154 return gh->hashfp(e->key);
158 * Get the bucket-index for an already-computed full hash.
160 BLI_INLINE unsigned int ghash_bucket_index(GHash *gh, const unsigned int hash)
162 #ifdef GHASH_USE_MODULO_BUCKETS
163 return hash % gh->nbuckets;
165 return hash & gh->bucket_mask;
170 * Find the index of next used bucket, starting from \a curr_bucket (\a gh is assumed non-empty).
172 BLI_INLINE unsigned int ghash_find_next_bucket_index(GHash *gh, unsigned int curr_bucket)
174 if (curr_bucket >= gh->nbuckets) {
177 if (gh->buckets[curr_bucket]) {
180 for (; curr_bucket < gh->nbuckets; curr_bucket++) {
181 if (gh->buckets[curr_bucket]) {
185 for (curr_bucket = 0; curr_bucket < gh->nbuckets; curr_bucket++) {
186 if (gh->buckets[curr_bucket]) {
195 * Expand buckets to the next size up or down.
197 static void ghash_buckets_resize(GHash *gh, const unsigned int nbuckets)
199 Entry **buckets_old = gh->buckets;
201 const unsigned int nbuckets_old = gh->nbuckets;
204 BLI_assert((gh->nbuckets != nbuckets) || !gh->buckets);
205 // printf("%s: %d -> %d\n", __func__, nbuckets_old, nbuckets);
207 gh->nbuckets = nbuckets;
208 #ifdef GHASH_USE_MODULO_BUCKETS
210 gh->bucket_mask = nbuckets - 1;
213 buckets_new = (Entry **)MEM_callocN(sizeof(*gh->buckets) * gh->nbuckets, __func__);
216 if (nbuckets > nbuckets_old) {
217 for (i = 0; i < nbuckets_old; i++) {
218 for (Entry *e = buckets_old[i], *e_next; e; e = e_next) {
219 const unsigned hash = ghash_entryhash(gh, e);
220 const unsigned bucket_index = ghash_bucket_index(gh, hash);
222 e->next = buckets_new[bucket_index];
223 buckets_new[bucket_index] = e;
228 for (i = 0; i < nbuckets_old; i++) {
229 #ifdef GHASH_USE_MODULO_BUCKETS
230 for (Entry *e = buckets_old[i], *e_next; e; e = e_next) {
231 const unsigned hash = ghash_entryhash(gh, e);
232 const unsigned bucket_index = ghash_bucket_index(gh, hash);
234 e->next = buckets_new[bucket_index];
235 buckets_new[bucket_index] = e;
238 /* No need to recompute hashes in this case, since our mask is just smaller, all items in old bucket i
239 * will go in same new bucket (i & new_mask)! */
240 const unsigned bucket_index = ghash_bucket_index(gh, i);
241 BLI_assert(!buckets_old[i] || (bucket_index == ghash_bucket_index(gh, ghash_entryhash(gh, buckets_old[i]))));
243 for (e = buckets_old[i]; e && e->next; e = e->next);
245 e->next = buckets_new[bucket_index];
246 buckets_new[bucket_index] = buckets_old[i];
253 gh->buckets = buckets_new;
255 MEM_freeN(buckets_old);
260 * Check if the number of items in the GHash is large enough to require more buckets,
261 * or small enough to require less buckets, and resize \a gh accordingly.
263 static void ghash_buckets_expand(
264 GHash *gh, const unsigned int nentries, const bool user_defined)
266 unsigned int new_nbuckets;
268 if (LIKELY(gh->buckets && (nentries < gh->limit_grow))) {
272 new_nbuckets = gh->nbuckets;
274 #ifdef GHASH_USE_MODULO_BUCKETS
275 while ((nentries > gh->limit_grow) &&
276 (gh->cursize < GHASH_MAX_SIZE - 1))
278 new_nbuckets = hashsizes[++gh->cursize];
279 gh->limit_grow = GHASH_LIMIT_GROW(new_nbuckets);
282 while ((nentries > gh->limit_grow) &&
283 (gh->bucket_bit < GHASH_BUCKET_BIT_MAX))
285 new_nbuckets = 1u << ++gh->bucket_bit;
286 gh->limit_grow = GHASH_LIMIT_GROW(new_nbuckets);
291 #ifdef GHASH_USE_MODULO_BUCKETS
292 gh->size_min = gh->cursize;
294 gh->bucket_bit_min = gh->bucket_bit;
298 if ((new_nbuckets == gh->nbuckets) && gh->buckets) {
302 gh->limit_grow = GHASH_LIMIT_GROW(new_nbuckets);
303 gh->limit_shrink = GHASH_LIMIT_SHRINK(new_nbuckets);
304 ghash_buckets_resize(gh, new_nbuckets);
307 static void ghash_buckets_contract(
308 GHash *gh, const unsigned int nentries, const bool user_defined, const bool force_shrink)
310 unsigned int new_nbuckets;
312 if (!(force_shrink || (gh->flag & GHASH_FLAG_ALLOW_SHRINK))) {
316 if (LIKELY(gh->buckets && (nentries > gh->limit_shrink))) {
320 new_nbuckets = gh->nbuckets;
322 #ifdef GHASH_USE_MODULO_BUCKETS
323 while ((nentries < gh->limit_shrink) &&
324 (gh->cursize > gh->size_min))
326 new_nbuckets = hashsizes[--gh->cursize];
327 gh->limit_shrink = GHASH_LIMIT_SHRINK(new_nbuckets);
330 while ((nentries < gh->limit_shrink) &&
331 (gh->bucket_bit > gh->bucket_bit_min))
333 new_nbuckets = 1u << --gh->bucket_bit;
334 gh->limit_shrink = GHASH_LIMIT_SHRINK(new_nbuckets);
339 #ifdef GHASH_USE_MODULO_BUCKETS
340 gh->size_min = gh->cursize;
342 gh->bucket_bit_min = gh->bucket_bit;
346 if ((new_nbuckets == gh->nbuckets) && gh->buckets) {
350 gh->limit_grow = GHASH_LIMIT_GROW(new_nbuckets);
351 gh->limit_shrink = GHASH_LIMIT_SHRINK(new_nbuckets);
352 ghash_buckets_resize(gh, new_nbuckets);
356 * Clear and reset \a gh buckets, reserve again buckets for given number of entries.
358 BLI_INLINE void ghash_buckets_reset(GHash *gh, const unsigned int nentries)
360 MEM_SAFE_FREE(gh->buckets);
362 #ifdef GHASH_USE_MODULO_BUCKETS
365 gh->nbuckets = hashsizes[gh->cursize];
367 gh->bucket_bit = GHASH_BUCKET_BIT_MIN;
368 gh->bucket_bit_min = GHASH_BUCKET_BIT_MIN;
369 gh->nbuckets = 1u << gh->bucket_bit;
370 gh->bucket_mask = gh->nbuckets - 1;
373 gh->limit_grow = GHASH_LIMIT_GROW(gh->nbuckets);
374 gh->limit_shrink = GHASH_LIMIT_SHRINK(gh->nbuckets);
378 ghash_buckets_expand(gh, nentries, (nentries != 0));
382 * Internal lookup function.
383 * Takes hash and bucket_index arguments to avoid calling #ghash_keyhash and #ghash_bucket_index multiple times.
385 BLI_INLINE Entry *ghash_lookup_entry_ex(
386 GHash *gh, const void *key, const unsigned int bucket_index)
389 /* If we do not store GHash, not worth computing it for each entry here!
390 * Typically, comparison function will be quicker, and since it's needed in the end anyway... */
391 for (e = gh->buckets[bucket_index]; e; e = e->next) {
392 if (UNLIKELY(gh->cmpfp(key, e->key) == false)) {
401 * Internal lookup function, returns previous entry of target one too.
402 * Takes bucket_index argument to avoid calling #ghash_keyhash and #ghash_bucket_index multiple times.
403 * Useful when modifying buckets somehow (like removing an entry...).
405 BLI_INLINE Entry *ghash_lookup_entry_prev_ex(
406 GHash *gh, const void *key,
407 Entry **r_e_prev, const unsigned int bucket_index)
409 /* If we do not store GHash, not worth computing it for each entry here!
410 * Typically, comparison function will be quicker, and since it's needed in the end anyway... */
411 for (Entry *e_prev = NULL, *e = gh->buckets[bucket_index]; e; e_prev = e, e = e->next) {
412 if (UNLIKELY(gh->cmpfp(key, e->key) == false)) {
423 * Internal lookup function. Only wraps #ghash_lookup_entry_ex
425 BLI_INLINE Entry *ghash_lookup_entry(GHash *gh, const void *key)
427 const unsigned int hash = ghash_keyhash(gh, key);
428 const unsigned int bucket_index = ghash_bucket_index(gh, hash);
429 return ghash_lookup_entry_ex(gh, key, bucket_index);
432 static GHash *ghash_new(GHashHashFP hashfp, GHashCmpFP cmpfp, const char *info,
433 const unsigned int nentries_reserve, const unsigned int flag)
435 GHash *gh = MEM_mallocN(sizeof(*gh), info);
443 ghash_buckets_reset(gh, nentries_reserve);
444 gh->entrypool = BLI_mempool_create(GHASH_ENTRY_SIZE(flag & GHASH_FLAG_IS_GSET), 64, 64, BLI_MEMPOOL_NOP);
450 * Internal insert function.
451 * Takes hash and bucket_index arguments to avoid calling #ghash_keyhash and #ghash_bucket_index multiple times.
453 BLI_INLINE void ghash_insert_ex(
454 GHash *gh, void *key, void *val, const unsigned int bucket_index)
456 GHashEntry *e = BLI_mempool_alloc(gh->entrypool);
458 BLI_assert((gh->flag & GHASH_FLAG_ALLOW_DUPES) || (BLI_ghash_haskey(gh, key) == 0));
459 BLI_assert(!(gh->flag & GHASH_FLAG_IS_GSET));
461 e->e.next = gh->buckets[bucket_index];
464 gh->buckets[bucket_index] = (Entry *)e;
466 ghash_buckets_expand(gh, ++gh->nentries, false);
470 * Insert function that takes a pre-allocated entry.
472 BLI_INLINE void ghash_insert_ex_keyonly_entry(
473 GHash *gh, void *key, const unsigned int bucket_index,
476 BLI_assert((gh->flag & GHASH_FLAG_ALLOW_DUPES) || (BLI_ghash_haskey(gh, key) == 0));
478 e->next = gh->buckets[bucket_index];
480 gh->buckets[bucket_index] = e;
482 ghash_buckets_expand(gh, ++gh->nentries, false);
486 * Insert function that doesn't set the value (use for GSet)
488 BLI_INLINE void ghash_insert_ex_keyonly(
489 GHash *gh, void *key, const unsigned int bucket_index)
491 Entry *e = BLI_mempool_alloc(gh->entrypool);
493 BLI_assert((gh->flag & GHASH_FLAG_ALLOW_DUPES) || (BLI_ghash_haskey(gh, key) == 0));
494 BLI_assert((gh->flag & GHASH_FLAG_IS_GSET) != 0);
496 e->next = gh->buckets[bucket_index];
498 gh->buckets[bucket_index] = e;
500 ghash_buckets_expand(gh, ++gh->nentries, false);
503 BLI_INLINE void ghash_insert(GHash *gh, void *key, void *val)
505 const unsigned int hash = ghash_keyhash(gh, key);
506 const unsigned int bucket_index = ghash_bucket_index(gh, hash);
508 ghash_insert_ex(gh, key, val, bucket_index);
511 BLI_INLINE bool ghash_insert_safe(
512 GHash *gh, void *key, void *val, const bool override,
513 GHashKeyFreeFP keyfreefp, GHashValFreeFP valfreefp)
515 const unsigned int hash = ghash_keyhash(gh, key);
516 const unsigned int bucket_index = ghash_bucket_index(gh, hash);
517 GHashEntry *e = (GHashEntry *)ghash_lookup_entry_ex(gh, key, bucket_index);
519 BLI_assert(!(gh->flag & GHASH_FLAG_IS_GSET));
535 ghash_insert_ex(gh, key, val, bucket_index);
540 BLI_INLINE bool ghash_insert_safe_keyonly(
541 GHash *gh, void *key, const bool override,
542 GHashKeyFreeFP keyfreefp)
544 const unsigned int hash = ghash_keyhash(gh, key);
545 const unsigned int bucket_index = ghash_bucket_index(gh, hash);
546 Entry *e = ghash_lookup_entry_ex(gh, key, bucket_index);
548 BLI_assert((gh->flag & GHASH_FLAG_IS_GSET) != 0);
560 ghash_insert_ex_keyonly(gh, key, bucket_index);
566 * Remove the entry and return it, caller must free from gh->entrypool.
568 static Entry *ghash_remove_ex(
569 GHash *gh, const void *key,
570 GHashKeyFreeFP keyfreefp, GHashValFreeFP valfreefp,
571 const unsigned int bucket_index)
574 Entry *e = ghash_lookup_entry_prev_ex(gh, key, &e_prev, bucket_index);
576 BLI_assert(!valfreefp || !(gh->flag & GHASH_FLAG_IS_GSET));
583 valfreefp(((GHashEntry *)e)->val);
587 e_prev->next = e->next;
590 gh->buckets[bucket_index] = e->next;
593 ghash_buckets_contract(gh, --gh->nentries, false, false);
600 * Remove a random entry and return it (or NULL if empty), caller must free from gh->entrypool.
602 static Entry *ghash_pop(GHash *gh, GHashIterState *state)
604 unsigned int curr_bucket = state->curr_bucket;
605 if (gh->nentries == 0) {
609 /* Note: using first_bucket_index here allows us to avoid potential huge number of loops over buckets,
610 * in case we are popping from a large ghash with few items in it... */
611 curr_bucket = ghash_find_next_bucket_index(gh, curr_bucket);
613 Entry *e = gh->buckets[curr_bucket];
616 ghash_remove_ex(gh, e->key, NULL, NULL, curr_bucket);
618 state->curr_bucket = curr_bucket;
623 * Run free callbacks for freeing entries.
625 static void ghash_free_cb(
627 GHashKeyFreeFP keyfreefp, GHashValFreeFP valfreefp)
631 BLI_assert(keyfreefp || valfreefp);
632 BLI_assert(!valfreefp || !(gh->flag & GHASH_FLAG_IS_GSET));
634 for (i = 0; i < gh->nbuckets; i++) {
637 for (e = gh->buckets[i]; e; e = e->next) {
642 valfreefp(((GHashEntry *)e)->val);
651 static GHash *ghash_copy(GHash *gh, GHashKeyCopyFP keycopyfp, GHashValCopyFP valcopyfp)
655 /* This allows us to be sure to get the same number of buckets in gh_new as in ghash. */
656 const unsigned int reserve_nentries_new = MAX2(GHASH_LIMIT_GROW(gh->nbuckets) - 1, gh->nentries);
658 BLI_assert(!valcopyfp || !(gh->flag & GHASH_FLAG_IS_GSET));
660 gh_new = ghash_new(gh->hashfp, gh->cmpfp, __func__, 0, gh->flag);
661 ghash_buckets_expand(gh_new, reserve_nentries_new, false);
663 BLI_assert(gh_new->nbuckets == gh->nbuckets);
665 for (i = 0; i < gh->nbuckets; i++) {
668 for (e = gh->buckets[i]; e; e = e->next) {
669 Entry *e_new = BLI_mempool_alloc(gh_new->entrypool);
670 ghash_entry_copy(gh_new, e_new, gh, e, keycopyfp, valcopyfp);
673 * This means entries in buckets in new copy will be in reversed order!
674 * This shall not be an issue though, since order should never be assumed in ghash. */
676 /* Note: We can use 'i' here, since we are sure that 'gh' and 'gh_new' have the same number of buckets! */
677 e_new->next = gh_new->buckets[i];
678 gh_new->buckets[i] = e_new;
681 gh_new->nentries = gh->nentries;
693 * Creates a new, empty GHash.
695 * \param hashfp Hash callback.
696 * \param cmpfp Comparison callback.
697 * \param info Identifier string for the GHash.
698 * \param nentries_reserve Optionally reserve the number of members that the hash will hold.
699 * Use this to avoid resizing buckets if the size is known or can be closely approximated.
700 * \return An empty GHash.
702 GHash *BLI_ghash_new_ex(GHashHashFP hashfp, GHashCmpFP cmpfp, const char *info,
703 const unsigned int nentries_reserve)
705 return ghash_new(hashfp, cmpfp, info, nentries_reserve, 0);
709 * Wraps #BLI_ghash_new_ex with zero entries reserved.
711 GHash *BLI_ghash_new(GHashHashFP hashfp, GHashCmpFP cmpfp, const char *info)
713 return BLI_ghash_new_ex(hashfp, cmpfp, info, 0);
717 * Copy given GHash. Keys and values are also copied if relevant callback is provided, else pointers remain the same.
719 GHash *BLI_ghash_copy(GHash *gh, GHashKeyCopyFP keycopyfp, GHashValCopyFP valcopyfp)
721 return ghash_copy(gh, keycopyfp, valcopyfp);
725 * Reserve given amount of entries (resize \a gh accordingly if needed).
727 void BLI_ghash_reserve(GHash *gh, const unsigned int nentries_reserve)
729 ghash_buckets_expand(gh, nentries_reserve, true);
730 ghash_buckets_contract(gh, nentries_reserve, true, false);
734 * \return size of the GHash.
736 unsigned int BLI_ghash_size(GHash *gh)
742 * Insert a key/value pair into the \a gh.
744 * \note Duplicates are not checked,
745 * the caller is expected to ensure elements are unique unless
746 * GHASH_FLAG_ALLOW_DUPES flag is set.
748 void BLI_ghash_insert(GHash *gh, void *key, void *val)
750 ghash_insert(gh, key, val);
754 * Inserts a new value to a key that may already be in ghash.
756 * Avoids #BLI_ghash_remove, #BLI_ghash_insert calls (double lookups)
758 * \returns true if a new key has been added.
760 bool BLI_ghash_reinsert(GHash *gh, void *key, void *val, GHashKeyFreeFP keyfreefp, GHashValFreeFP valfreefp)
762 return ghash_insert_safe(gh, key, val, true, keyfreefp, valfreefp);
766 * Lookup the value of \a key in \a gh.
768 * \param key The key to lookup.
769 * \returns the value for \a key or NULL.
771 * \note When NULL is a valid value, use #BLI_ghash_lookup_p to differentiate a missing key
772 * from a key with a NULL value. (Avoids calling #BLI_ghash_haskey before #BLI_ghash_lookup)
774 void *BLI_ghash_lookup(GHash *gh, const void *key)
776 GHashEntry *e = (GHashEntry *)ghash_lookup_entry(gh, key);
777 BLI_assert(!(gh->flag & GHASH_FLAG_IS_GSET));
778 return e ? e->val : NULL;
782 * A version of #BLI_ghash_lookup which accepts a fallback argument.
784 void *BLI_ghash_lookup_default(GHash *gh, const void *key, void *val_default)
786 GHashEntry *e = (GHashEntry *)ghash_lookup_entry(gh, key);
787 BLI_assert(!(gh->flag & GHASH_FLAG_IS_GSET));
788 return e ? e->val : val_default;
792 * Lookup a pointer to the value of \a key in \a gh.
794 * \param key The key to lookup.
795 * \returns the pointer to value for \a key or NULL.
797 * \note This has 2 main benefits over #BLI_ghash_lookup.
798 * - A NULL return always means that \a key isn't in \a gh.
799 * - The value can be modified in-place without further function calls (faster).
801 void **BLI_ghash_lookup_p(GHash *gh, const void *key)
803 GHashEntry *e = (GHashEntry *)ghash_lookup_entry(gh, key);
804 BLI_assert(!(gh->flag & GHASH_FLAG_IS_GSET));
805 return e ? &e->val : NULL;
809 * Ensure \a key is exists in \a gh.
811 * This handles the common situation where the caller needs ensure a key is added to \a gh,
812 * constructing a new value in the case the key isn't found.
813 * Otherwise use the existing value.
815 * Such situations typically incur multiple lookups, however this function
816 * avoids them by ensuring the key is added,
817 * returning a pointer to the value so it can be used or initialized by the caller.
819 * \returns true when the value didn't need to be added.
820 * (when false, the caller _must_ initialize the value).
822 bool BLI_ghash_ensure_p(GHash *gh, void *key, void ***r_val)
824 const unsigned int hash = ghash_keyhash(gh, key);
825 const unsigned int bucket_index = ghash_bucket_index(gh, hash);
826 GHashEntry *e = (GHashEntry *)ghash_lookup_entry_ex(gh, key, bucket_index);
827 const bool haskey = (e != NULL);
830 e = BLI_mempool_alloc(gh->entrypool);
831 ghash_insert_ex_keyonly_entry(gh, key, bucket_index, (Entry *)e);
839 * A version of #BLI_ghash_ensure_p that allows caller to re-assign the key.
840 * Typically used when the key is to be duplicated.
842 * \warning Caller _must_ write to \a r_key when returning false.
844 bool BLI_ghash_ensure_p_ex(
845 GHash *gh, const void *key, void ***r_key, void ***r_val)
847 const unsigned int hash = ghash_keyhash(gh, key);
848 const unsigned int bucket_index = ghash_bucket_index(gh, hash);
849 GHashEntry *e = (GHashEntry *)ghash_lookup_entry_ex(gh, key, bucket_index);
850 const bool haskey = (e != NULL);
853 /* pass 'key' incase we resize */
854 e = BLI_mempool_alloc(gh->entrypool);
855 ghash_insert_ex_keyonly_entry(gh, (void *)key, bucket_index, (Entry *)e);
856 e->e.key = NULL; /* caller must re-assign */
865 * Remove \a key from \a gh, or return false if the key wasn't found.
867 * \param key The key to remove.
868 * \param keyfreefp Optional callback to free the key.
869 * \param valfreefp Optional callback to free the value.
870 * \return true if \a key was removed from \a gh.
872 bool BLI_ghash_remove(GHash *gh, const void *key, GHashKeyFreeFP keyfreefp, GHashValFreeFP valfreefp)
874 const unsigned int hash = ghash_keyhash(gh, key);
875 const unsigned int bucket_index = ghash_bucket_index(gh, hash);
876 Entry *e = ghash_remove_ex(gh, key, keyfreefp, valfreefp, bucket_index);
878 BLI_mempool_free(gh->entrypool, e);
886 /* same as above but return the value,
887 * no free value argument since it will be returned */
889 * Remove \a key from \a gh, returning the value or NULL if the key wasn't found.
891 * \param key The key to remove.
892 * \param keyfreefp Optional callback to free the key.
893 * \return the value of \a key int \a gh or NULL.
895 void *BLI_ghash_popkey(GHash *gh, const void *key, GHashKeyFreeFP keyfreefp)
897 const unsigned int hash = ghash_keyhash(gh, key);
898 const unsigned int bucket_index = ghash_bucket_index(gh, hash);
899 GHashEntry *e = (GHashEntry *)ghash_remove_ex(gh, key, keyfreefp, NULL, bucket_index);
900 BLI_assert(!(gh->flag & GHASH_FLAG_IS_GSET));
903 BLI_mempool_free(gh->entrypool, e);
912 * \return true if the \a key is in \a gh.
914 bool BLI_ghash_haskey(GHash *gh, const void *key)
916 return (ghash_lookup_entry(gh, key) != NULL);
920 * Remove a random entry from \a gh, returning true if a key/value pair could be removed, false otherwise.
922 * \param r_key: The removed key.
923 * \param r_val: The removed value.
924 * \param state: Used for efficient removal.
925 * \return true if there was something to pop, false if ghash was already empty.
928 GHash *gh, GHashIterState *state,
929 void **r_key, void **r_val)
931 GHashEntry *e = (GHashEntry *)ghash_pop(gh, state);
933 BLI_assert(!(gh->flag & GHASH_FLAG_IS_GSET));
939 BLI_mempool_free(gh->entrypool, e);
943 *r_key = *r_val = NULL;
949 * Reset \a gh clearing all entries.
951 * \param keyfreefp Optional callback to free the key.
952 * \param valfreefp Optional callback to free the value.
953 * \param nentries_reserve Optionally reserve the number of members that the hash will hold.
955 void BLI_ghash_clear_ex(GHash *gh, GHashKeyFreeFP keyfreefp, GHashValFreeFP valfreefp,
956 const unsigned int nentries_reserve)
958 if (keyfreefp || valfreefp)
959 ghash_free_cb(gh, keyfreefp, valfreefp);
961 ghash_buckets_reset(gh, nentries_reserve);
962 BLI_mempool_clear_ex(gh->entrypool, nentries_reserve ? (int)nentries_reserve : -1);
966 * Wraps #BLI_ghash_clear_ex with zero entries reserved.
968 void BLI_ghash_clear(GHash *gh, GHashKeyFreeFP keyfreefp, GHashValFreeFP valfreefp)
970 BLI_ghash_clear_ex(gh, keyfreefp, valfreefp, 0);
974 * Frees the GHash and its members.
976 * \param gh The GHash to free.
977 * \param keyfreefp Optional callback to free the key.
978 * \param valfreefp Optional callback to free the value.
980 void BLI_ghash_free(GHash *gh, GHashKeyFreeFP keyfreefp, GHashValFreeFP valfreefp)
982 BLI_assert((int)gh->nentries == BLI_mempool_count(gh->entrypool));
983 if (keyfreefp || valfreefp)
984 ghash_free_cb(gh, keyfreefp, valfreefp);
986 MEM_freeN(gh->buckets);
987 BLI_mempool_destroy(gh->entrypool);
994 void BLI_ghash_flag_set(GHash *gh, unsigned int flag)
1000 * Clear a GHash flag.
1002 void BLI_ghash_flag_clear(GHash *gh, unsigned int flag)
1010 /* -------------------------------------------------------------------- */
1011 /* GHash Iterator API */
1013 /** \name Iterator API
1017 * Create a new GHashIterator. The hash table must not be mutated
1018 * while the iterator is in use, and the iterator will step exactly
1019 * BLI_ghash_size(gh) times before becoming done.
1021 * \param gh The GHash to iterate over.
1022 * \return Pointer to a new DynStr.
1024 GHashIterator *BLI_ghashIterator_new(GHash *gh)
1026 GHashIterator *ghi = MEM_mallocN(sizeof(*ghi), "ghash iterator");
1027 BLI_ghashIterator_init(ghi, gh);
1032 * Init an already allocated GHashIterator. The hash table must not
1033 * be mutated while the iterator is in use, and the iterator will
1034 * step exactly BLI_ghash_size(gh) times before becoming done.
1036 * \param ghi The GHashIterator to initialize.
1037 * \param gh The GHash to iterate over.
1039 void BLI_ghashIterator_init(GHashIterator *ghi, GHash *gh)
1042 ghi->curEntry = NULL;
1043 ghi->curBucket = UINT_MAX; /* wraps to zero */
1047 if (UNLIKELY(ghi->curBucket == ghi->gh->nbuckets))
1049 ghi->curEntry = ghi->gh->buckets[ghi->curBucket];
1050 } while (!ghi->curEntry);
1055 * Steps the iterator to the next index.
1057 * \param ghi The iterator.
1059 void BLI_ghashIterator_step(GHashIterator *ghi)
1061 if (ghi->curEntry) {
1062 ghi->curEntry = ghi->curEntry->next;
1063 while (!ghi->curEntry) {
1065 if (ghi->curBucket == ghi->gh->nbuckets)
1067 ghi->curEntry = ghi->gh->buckets[ghi->curBucket];
1073 * Free a GHashIterator.
1075 * \param ghi The iterator to free.
1077 void BLI_ghashIterator_free(GHashIterator *ghi)
1082 /* inline functions now */
1085 * Retrieve the key from an iterator.
1087 * \param ghi The iterator.
1088 * \return The key at the current index, or NULL if the
1091 void *BLI_ghashIterator_getKey(GHashIterator *ghi)
1093 return ghi->curEntry->key;
1097 * Retrieve the value from an iterator.
1099 * \param ghi The iterator.
1100 * \return The value at the current index, or NULL if the
1103 void *BLI_ghashIterator_getValue(GHashIterator *ghi)
1105 return ghi->curEntry->val;
1109 * Retrieve the value from an iterator.
1111 * \param ghi The iterator.
1112 * \return The value at the current index, or NULL if the
1115 void **BLI_ghashIterator_getValue_p(GHashIterator *ghi)
1117 return &ghi->curEntry->val;
1121 * Determine if an iterator is done (has reached the end of
1124 * \param ghi The iterator.
1125 * \return True if done, False otherwise.
1127 bool BLI_ghashIterator_done(GHashIterator *ghi)
1129 return ghi->curEntry == NULL;
1136 /** \name Generic Key Hash & Comparison Functions
1142 /* works but slower */
1143 unsigned int BLI_ghashutil_ptrhash(const void *key)
1145 return (unsigned int)(intptr_t)key;
1148 /* based python3.3's pointer hashing function */
1149 unsigned int BLI_ghashutil_ptrhash(const void *key)
1151 size_t y = (size_t)key;
1152 /* bottom 3 or 4 bits are likely to be 0; rotate y by 4 to avoid
1153 * excessive hash collisions for dicts and sets */
1154 y = (y >> 4) | (y << (8 * sizeof(void *) - 4));
1155 return (unsigned int)y;
1158 bool BLI_ghashutil_ptrcmp(const void *a, const void *b)
1163 unsigned int BLI_ghashutil_uinthash_v4(const unsigned int key[4])
1175 unsigned int BLI_ghashutil_uinthash_v4_murmur(const unsigned int key[4])
1177 return BLI_hash_mm2((const unsigned char *)key, sizeof(int) * 4 /* sizeof(key) */, 0);
1180 bool BLI_ghashutil_uinthash_v4_cmp(const void *a, const void *b)
1182 return (memcmp(a, b, sizeof(unsigned int[4])) != 0);
1185 unsigned int BLI_ghashutil_uinthash(unsigned int key)
1187 key += ~(key << 16);
1197 unsigned int BLI_ghashutil_inthash_p(const void *ptr)
1199 uintptr_t key = (uintptr_t)ptr;
1201 key += ~(key << 16);
1208 return (unsigned int)(key & 0xffffffff);
1211 unsigned int BLI_ghashutil_inthash_p_murmur(const void *ptr)
1213 uintptr_t key = (uintptr_t)ptr;
1215 return BLI_hash_mm2((const unsigned char *)&key, sizeof(key), 0);
1218 unsigned int BLI_ghashutil_inthash_p_simple(const void *ptr)
1220 return GET_UINT_FROM_POINTER(ptr);
1223 bool BLI_ghashutil_intcmp(const void *a, const void *b)
1229 * This function implements the widely used "djb" hash apparently posted
1230 * by Daniel Bernstein to comp.lang.c some time ago. The 32 bit
1231 * unsigned hash value starts at 5381 and for each byte 'c' in the
1232 * string, is updated: ``hash = hash * 33 + c``. This
1233 * function uses the signed value of each byte.
1235 * note: this is the same hash method that glib 2.34.0 uses.
1237 unsigned int BLI_ghashutil_strhash_n(const char *key, size_t n)
1239 const signed char *p;
1240 unsigned int h = 5381;
1242 for (p = (const signed char *)key; n-- && *p != '\0'; p++) {
1243 h = (h << 5) + h + (unsigned int)*p;
1248 unsigned int BLI_ghashutil_strhash_p(const void *ptr)
1250 const signed char *p;
1251 unsigned int h = 5381;
1253 for (p = ptr; *p != '\0'; p++) {
1254 h = (h << 5) + h + (unsigned int)*p;
1259 unsigned int BLI_ghashutil_strhash_p_murmur(const void *ptr)
1261 const unsigned char *key = ptr;
1263 return BLI_hash_mm2(key, strlen((const char *)key) + 1, 0);
1265 bool BLI_ghashutil_strcmp(const void *a, const void *b)
1267 return (a == b) ? false : !STREQ(a, b);
1270 GHashPair *BLI_ghashutil_pairalloc(const void *first, const void *second)
1272 GHashPair *pair = MEM_mallocN(sizeof(GHashPair), "GHashPair");
1273 pair->first = first;
1274 pair->second = second;
1278 unsigned int BLI_ghashutil_pairhash(const void *ptr)
1280 const GHashPair *pair = ptr;
1281 unsigned int hash = BLI_ghashutil_ptrhash(pair->first);
1282 return hash ^ BLI_ghashutil_ptrhash(pair->second);
1285 bool BLI_ghashutil_paircmp(const void *a, const void *b)
1287 const GHashPair *A = a;
1288 const GHashPair *B = b;
1290 return (BLI_ghashutil_ptrcmp(A->first, B->first) ||
1291 BLI_ghashutil_ptrcmp(A->second, B->second));
1294 void BLI_ghashutil_pairfree(void *ptr)
1302 /** \name Convenience GHash Creation Functions
1305 GHash *BLI_ghash_ptr_new_ex(const char *info, const unsigned int nentries_reserve)
1307 return BLI_ghash_new_ex(BLI_ghashutil_ptrhash, BLI_ghashutil_ptrcmp, info, nentries_reserve);
1309 GHash *BLI_ghash_ptr_new(const char *info)
1311 return BLI_ghash_ptr_new_ex(info, 0);
1314 GHash *BLI_ghash_str_new_ex(const char *info, const unsigned int nentries_reserve)
1316 return BLI_ghash_new_ex(BLI_ghashutil_strhash_p, BLI_ghashutil_strcmp, info, nentries_reserve);
1318 GHash *BLI_ghash_str_new(const char *info)
1320 return BLI_ghash_str_new_ex(info, 0);
1323 GHash *BLI_ghash_int_new_ex(const char *info, const unsigned int nentries_reserve)
1325 return BLI_ghash_new_ex(BLI_ghashutil_inthash_p, BLI_ghashutil_intcmp, info, nentries_reserve);
1327 GHash *BLI_ghash_int_new(const char *info)
1329 return BLI_ghash_int_new_ex(info, 0);
1332 GHash *BLI_ghash_pair_new_ex(const char *info, const unsigned int nentries_reserve)
1334 return BLI_ghash_new_ex(BLI_ghashutil_pairhash, BLI_ghashutil_paircmp, info, nentries_reserve);
1336 GHash *BLI_ghash_pair_new(const char *info)
1338 return BLI_ghash_pair_new_ex(info, 0);
1344 /* -------------------------------------------------------------------- */
1347 /* Use ghash API to give 'set' functionality */
1349 /** \name GSet Functions
1351 GSet *BLI_gset_new_ex(GSetHashFP hashfp, GSetCmpFP cmpfp, const char *info,
1352 const unsigned int nentries_reserve)
1354 return (GSet *)ghash_new(hashfp, cmpfp, info, nentries_reserve, GHASH_FLAG_IS_GSET);
1357 GSet *BLI_gset_new(GSetHashFP hashfp, GSetCmpFP cmpfp, const char *info)
1359 return BLI_gset_new_ex(hashfp, cmpfp, info, 0);
1363 * Copy given GSet. Keys are also copied if callback is provided, else pointers remain the same.
1365 GSet *BLI_gset_copy(GSet *gs, GHashKeyCopyFP keycopyfp)
1367 return (GSet *)ghash_copy((GHash *)gs, keycopyfp, NULL);
1370 unsigned int BLI_gset_size(GSet *gs)
1372 return ((GHash *)gs)->nentries;
1376 * Adds the key to the set (no checks for unique keys!).
1377 * Matching #BLI_ghash_insert
1379 void BLI_gset_insert(GSet *gs, void *key)
1381 const unsigned int hash = ghash_keyhash((GHash *)gs, key);
1382 const unsigned int bucket_index = ghash_bucket_index((GHash *)gs, hash);
1383 ghash_insert_ex_keyonly((GHash *)gs, key, bucket_index);
1387 * A version of BLI_gset_insert which checks first if the key is in the set.
1388 * \returns true if a new key has been added.
1390 * \note GHash has no equivalent to this because typically the value would be different.
1392 bool BLI_gset_add(GSet *gs, void *key)
1394 return ghash_insert_safe_keyonly((GHash *)gs, key, false, NULL);
1398 * Set counterpart to #BLI_ghash_ensure_p_ex.
1399 * similar to BLI_gset_add, except it returns the key pointer.
1401 * \warning Caller _must_ write to \a r_key when returning false.
1403 bool BLI_gset_ensure_p_ex(GSet *gs, const void *key, void ***r_key)
1405 const unsigned int hash = ghash_keyhash((GHash *)gs, key);
1406 const unsigned int bucket_index = ghash_bucket_index((GHash *)gs, hash);
1407 GSetEntry *e = (GSetEntry *)ghash_lookup_entry_ex((GHash *)gs, key, bucket_index);
1408 const bool haskey = (e != NULL);
1411 /* pass 'key' incase we resize */
1412 e = BLI_mempool_alloc(((GHash *)gs)->entrypool);
1413 ghash_insert_ex_keyonly_entry((GHash *)gs, (void *)key, bucket_index, (Entry *)e);
1414 e->key = NULL; /* caller must re-assign */
1422 * Adds the key to the set (duplicates are managed).
1423 * Matching #BLI_ghash_reinsert
1425 * \returns true if a new key has been added.
1427 bool BLI_gset_reinsert(GSet *gs, void *key, GSetKeyFreeFP keyfreefp)
1429 return ghash_insert_safe_keyonly((GHash *)gs, key, true, keyfreefp);
1432 bool BLI_gset_remove(GSet *gs, const void *key, GSetKeyFreeFP keyfreefp)
1434 return BLI_ghash_remove((GHash *)gs, key, keyfreefp, NULL);
1438 bool BLI_gset_haskey(GSet *gs, const void *key)
1440 return (ghash_lookup_entry((GHash *)gs, key) != NULL);
1444 * Remove a random entry from \a gs, returning true if a key could be removed, false otherwise.
1446 * \param r_key: The removed key.
1447 * \param state: Used for efficient removal.
1448 * \return true if there was something to pop, false if gset was already empty.
1451 GSet *gs, GSetIterState *state,
1454 GSetEntry *e = (GSetEntry *)ghash_pop((GHash *)gs, (GHashIterState *)state);
1459 BLI_mempool_free(((GHash *)gs)->entrypool, e);
1468 void BLI_gset_clear_ex(GSet *gs, GSetKeyFreeFP keyfreefp,
1469 const unsigned int nentries_reserve)
1471 BLI_ghash_clear_ex((GHash *)gs, keyfreefp, NULL,
1475 void BLI_gset_clear(GSet *gs, GSetKeyFreeFP keyfreefp)
1477 BLI_ghash_clear((GHash *)gs, keyfreefp, NULL);
1480 void BLI_gset_free(GSet *gs, GSetKeyFreeFP keyfreefp)
1482 BLI_ghash_free((GHash *)gs, keyfreefp, NULL);
1485 void BLI_gset_flag_set(GSet *gs, unsigned int flag)
1487 ((GHash *)gs)->flag |= flag;
1490 void BLI_gset_flag_clear(GSet *gs, unsigned int flag)
1492 ((GHash *)gs)->flag &= ~flag;
1498 /** \name Convenience GSet Creation Functions
1501 GSet *BLI_gset_ptr_new_ex(const char *info, const unsigned int nentries_reserve)
1503 return BLI_gset_new_ex(BLI_ghashutil_ptrhash, BLI_ghashutil_ptrcmp, info, nentries_reserve);
1505 GSet *BLI_gset_ptr_new(const char *info)
1507 return BLI_gset_ptr_new_ex(info, 0);
1510 GSet *BLI_gset_str_new_ex(const char *info, const unsigned int nentries_reserve)
1512 return BLI_gset_new_ex(BLI_ghashutil_strhash_p, BLI_ghashutil_strcmp, info, nentries_reserve);
1514 GSet *BLI_gset_str_new(const char *info)
1516 return BLI_gset_str_new_ex(info, 0);
1519 GSet *BLI_gset_pair_new_ex(const char *info, const unsigned int nentries_reserve)
1521 return BLI_gset_new_ex(BLI_ghashutil_pairhash, BLI_ghashutil_paircmp, info, nentries_reserve);
1523 GSet *BLI_gset_pair_new(const char *info)
1525 return BLI_gset_pair_new_ex(info, 0);
1531 /** \name Debugging & Introspection
1534 #include "BLI_math.h"
1537 * \return number of buckets in the GHash.
1539 int BLI_ghash_buckets_size(GHash *gh)
1541 return (int)gh->nbuckets;
1543 int BLI_gset_buckets_size(GSet *gs)
1545 return BLI_ghash_buckets_size((GHash *)gs);
1549 * Measure how well the hash function performs (1.0 is approx as good as random distribution),
1550 * and return a few other stats like load, variance of the distribution of the entries in the buckets, etc.
1552 * Smaller is better!
1554 double BLI_ghash_calc_quality_ex(
1555 GHash *gh, double *r_load, double *r_variance,
1556 double *r_prop_empty_buckets, double *r_prop_overloaded_buckets, int *r_biggest_bucket)
1561 if (gh->nentries == 0) {
1568 if (r_prop_empty_buckets) {
1569 *r_prop_empty_buckets = 1.0;
1571 if (r_prop_overloaded_buckets) {
1572 *r_prop_overloaded_buckets = 0.0;
1574 if (r_biggest_bucket) {
1575 *r_biggest_bucket = 0;
1581 mean = (double)gh->nentries / (double)gh->nbuckets;
1585 if (r_biggest_bucket) {
1586 *r_biggest_bucket = 0;
1590 /* We already know our mean (i.e. load factor), easy to compute variance.
1591 * See http://en.wikipedia.org/wiki/Algorithms_for_calculating_variance#Two-pass_algorithm
1594 for (i = 0; i < gh->nbuckets; i++) {
1597 for (e = gh->buckets[i]; e; e = e->next) {
1600 sum += ((double)count - mean) * ((double)count - mean);
1602 *r_variance = sum / (double)(gh->nbuckets - 1);
1607 uint64_t overloaded_buckets_threshold = (uint64_t)max_ii(GHASH_LIMIT_GROW(1), 1);
1608 uint64_t sum_overloaded = 0;
1609 uint64_t sum_empty = 0;
1611 for (i = 0; i < gh->nbuckets; i++) {
1614 for (e = gh->buckets[i]; e; e = e->next) {
1617 if (r_biggest_bucket) {
1618 *r_biggest_bucket = max_ii(*r_biggest_bucket, (int)count);
1620 if (r_prop_overloaded_buckets && (count > overloaded_buckets_threshold)) {
1623 if (r_prop_empty_buckets && !count) {
1626 sum += count * (count + 1);
1628 if (r_prop_overloaded_buckets) {
1629 *r_prop_overloaded_buckets = (double)sum_overloaded / (double)gh->nbuckets;
1631 if (r_prop_empty_buckets) {
1632 *r_prop_empty_buckets = (double)sum_empty / (double)gh->nbuckets;
1634 return ((double)sum * (double)gh->nbuckets /
1635 ((double)gh->nentries * (gh->nentries + 2 * gh->nbuckets - 1)));
1638 double BLI_gset_calc_quality_ex(
1639 GSet *gs, double *r_load, double *r_variance,
1640 double *r_prop_empty_buckets, double *r_prop_overloaded_buckets, int *r_biggest_bucket)
1642 return BLI_ghash_calc_quality_ex((GHash *)gs, r_load, r_variance,
1643 r_prop_empty_buckets, r_prop_overloaded_buckets, r_biggest_bucket);
1646 double BLI_ghash_calc_quality(GHash *gh)
1648 return BLI_ghash_calc_quality_ex(gh, NULL, NULL, NULL, NULL, NULL);
1650 double BLI_gset_calc_quality(GSet *gs)
1652 return BLI_ghash_calc_quality_ex((GHash *)gs, NULL, NULL, NULL, NULL, NULL);