From 1c061dea4be2186b39020cb8cf7352b7ed1efdff Mon Sep 17 00:00:00 2001 From: Max Horn Date: Fri, 24 Mar 2006 17:13:24 +0000 Subject: Changed the DEBUG_HASH_COLLISIONS feature: Now measures the ratio between lookup collisions and total number of lookups svn-id: r21432 --- common/assocarray.h | 20 +++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) (limited to 'common') diff --git a/common/assocarray.h b/common/assocarray.h index 081eac91c0..0e10ea7f4f 100644 --- a/common/assocarray.h +++ b/common/assocarray.h @@ -113,7 +113,7 @@ private: uint _arrsize, _nele; #ifdef DEBUG_HASH_COLLISIONS - mutable int _collisions; + mutable int _collisions, _lookups; #endif int lookup(const Key &key) const; @@ -154,13 +154,19 @@ int AssocArray::lookup(const Key &key) const { while (_arr[ctr] != NULL && !data_eq(_arr[ctr]->key, key)) { ctr++; +#ifdef DEBUG_HASH_COLLISIONS + _collisions++; +#endif if (ctr == _arrsize) ctr = 0; } #ifdef DEBUG_HASH_COLLISIONS - fprintf(stderr, "collisions = %d in AssocArray %p\n", _collisions, (const void *)this); + _lookups++; + fprintf(stderr, "collisions %d, lookups %d, ratio %f in AssocArray %p; size %d num elements %d\n", + _collisions, _lookups, ((double) _collisions / (double)_lookups), + (const void *)this, _arrsize, _nele); #endif return ctr; @@ -239,6 +245,7 @@ AssocArray::AssocArray() { #ifdef DEBUG_HASH_COLLISIONS _collisions = 0; + _lookups = 0; #endif } @@ -303,9 +310,10 @@ void AssocArray::expand_array(void) { // (*this)[old_arr[ctr]->key] = old_arr[ctr]->dat; dex = hashit(old_arr[ctr]->key, _arrsize); - while (_arr[dex] != NULL) + while (_arr[dex] != NULL) { if (++dex == _arrsize) dex = 0; + } _arr[dex] = old_arr[ctr]; _nele++; @@ -326,12 +334,6 @@ Val &AssocArray::operator [](const Key &key) { _arr[ctr] = new aa_ref_t(key); _nele++; -#ifdef DEBUG_HASH_COLLISIONS - if (ctr != hashit(key, _arrsize)) { - _collisions++; -// fprintf(stderr, "collisions = %d\n", _collisions); - } -#endif // Only fill array to fifty percent if (_nele > _arrsize / 2) { expand_array(); -- cgit v1.2.3