diff options
Diffstat (limited to 'source/blender/blenlib/intern/BLI_ghash.c')
-rw-r--r-- | source/blender/blenlib/intern/BLI_ghash.c | 60 |
1 files changed, 57 insertions, 3 deletions
diff --git a/source/blender/blenlib/intern/BLI_ghash.c b/source/blender/blenlib/intern/BLI_ghash.c index b30553d0b5e..74d7fdc88cb 100644 --- a/source/blender/blenlib/intern/BLI_ghash.c +++ b/source/blender/blenlib/intern/BLI_ghash.c @@ -399,7 +399,7 @@ void *BLI_ghash_lookup_default(GHash *gh, const void *key, void *val_default) * \param key The key to lookup. * \returns the pointer to value for \a key or NULL. * - * \note This has 2 main benifits over #BLI_ghash_lookup. + * \note This has 2 main benefits over #BLI_ghash_lookup. * - A NULL return always means that \a key isn't in \a gh. * - The value can be modified in-place without further function calls (faster). */ @@ -570,12 +570,12 @@ void BLI_ghashIterator_init(GHashIterator *ghi, GHash *gh) ghi->curEntry = NULL; ghi->curBucket = UINT_MAX; /* wraps to zero */ if (gh->nentries) { - while (!ghi->curEntry) { + do { ghi->curBucket++; if (UNLIKELY(ghi->curBucket == ghi->gh->nbuckets)) break; ghi->curEntry = ghi->gh->buckets[ghi->curBucket]; - } + } while (!ghi->curEntry); } } @@ -704,6 +704,11 @@ unsigned int BLI_ghashutil_uinthash_v4(const unsigned int key[4]) return hash; } +int BLI_ghashutil_uinthash_v4_cmp(const void *a, const void *b) +{ + return memcmp(a, b, sizeof(unsigned int[4])); +} + unsigned int BLI_ghashutil_uinthash(unsigned int key) { key += ~(key << 16); @@ -967,6 +972,17 @@ void BLI_gset_free(GSet *gs, GSetKeyFreeFP keyfreefp) { BLI_ghash_free((GHash *)gs, keyfreefp, NULL); } + +void BLI_gset_flag_set(GSet *gs, unsigned int flag) +{ + ((GHash *)gs)->flag |= flag; +} + +void BLI_gset_flag_clear(GSet *gs, unsigned int flag) +{ + ((GHash *)gs)->flag &= ~flag; +} + /** \} */ @@ -996,3 +1012,41 @@ GSet *BLI_gset_pair_new(const char *info) } /** \} */ + + +/** \name Debugging & Introspection + * \{ */ +#ifdef DEBUG + +/** + * Measure how well the hash function performs + * (1.0 is approx as good as random distribution). + * + * Smaller is better! + */ +double BLI_ghash_calc_quality(GHash *gh) +{ + uint64_t sum = 0; + unsigned int i; + + if (gh->nentries == 0) + return -1.0; + + for (i = 0; i < gh->nbuckets; i++) { + uint64_t count = 0; + Entry *e; + for (e = gh->buckets[i]; e; e = e->next) { + count += 1; + } + sum += count * (count + 1); + } + return ((double)sum * (double)gh->nbuckets / + ((double)gh->nentries * (gh->nentries + 2 * gh->nbuckets - 1))); +} +double BLI_gset_calc_quality(GSet *gs) +{ + return BLI_ghash_calc_quality((GHash *)gs); +} + +#endif +/** \} */ |