From 3893cb63e034180eab0764ee18567a56e8307a80 Mon Sep 17 00:00:00 2001 From: Mike Buland Date: Wed, 31 Dec 2008 02:33:43 +0000 Subject: Wow, that was a freaky bug. Turned out to not have anything to do with the size of the table, it had to do with using non pointer types for the key (some more complex types worked as well, probably because of lazy memory collection) and then using the [] indexing operators. You wound up with pointers to local variables that didn't exist by the end of the assignemnt operator. Strange, but I didn't actually use references inside of all of the Bu::Hash accessor functions, that means in cases where more complex variables are used as keys (like Bu::FString) it was making several copies of them per operation and destroying them all immediately. Now it will be even faster and use much less memory. Good catch, David. --- src/hash.h | 26 +++++++++++++------------- src/unit/hash.cpp | 4 ++-- 2 files changed, 15 insertions(+), 15 deletions(-) diff --git a/src/hash.h b/src/hash.h index f06c40f..e717c60 100644 --- a/src/hash.h +++ b/src/hash.h @@ -55,7 +55,7 @@ namespace Bu { friend class Hash; private: - HashProxy( Hash &h, key *k, uint32_t nPos, uint32_t hash ) : + HashProxy( Hash &h, const key *k, uint32_t nPos, uint32_t hash ) : hsh( h ), pKey( k ), nPos( nPos ), @@ -73,7 +73,7 @@ namespace Bu } Hash &hsh; - key *pKey; + const key *pKey; uint32_t nPos; _value *pValue; uint32_t hash; @@ -151,8 +151,8 @@ namespace Bu { if( bFilled ) { - hsh.va.destroy( pValue ); - hsh.va.construct( pValue, nval ); + hsh.va.destroy( &hsh.aValues[nPos] ); + hsh.va.construct( &hsh.aValues[nPos], nval ); hsh.onUpdate(); } else @@ -380,7 +380,7 @@ namespace Bu *@param k (key_type) Key of data to be retrieved. *@returns (HashProxy) Proxy pointing to the data. */ - virtual HashProxy operator[]( key k ) + virtual HashProxy operator[]( const key &k ) { uint32_t hash = __calcHashCode( k ); bool bFill; @@ -401,7 +401,7 @@ namespace Bu *@param k (key_type) Key to list the value under. *@param v (value_type) Value to store in the hash table. */ - virtual void insert( key k, value v ) + virtual void insert( const key &k, const value &v ) { uint32_t hash = __calcHashCode( k ); bool bFill; @@ -424,7 +424,7 @@ namespace Bu * Remove a value from the hash table. *@param k (key_type) The data under this key will be erased. */ - virtual void erase( key k ) + virtual void erase( const key &k ) { uint32_t hash = __calcHashCode( k ); bool bFill; @@ -477,7 +477,7 @@ namespace Bu *@param k (key_type) Key pointing to the data to be retrieved. *@returns (value_type &) The data pointed to by (k). */ - virtual value &get( key k ) + virtual value &get( const key &k ) { uint32_t hash = __calcHashCode( k ); bool bFill; @@ -502,7 +502,7 @@ namespace Bu *@returns (const value_type &) A const version of the data pointed * to by (k). */ - virtual const value &get( key k ) const + virtual const value &get( const key &k ) const { uint32_t hash = __calcHashCode( k ); bool bFill; @@ -526,7 +526,7 @@ namespace Bu *@param k (key_type) The key to check. *@returns (bool) Whether there was an item in the hash under key (k). */ - virtual bool has( key k ) + virtual bool has( const key &k ) { bool bFill; probe( __calcHashCode( k ), k, bFill, false ); @@ -534,7 +534,7 @@ namespace Bu return bFill; } - virtual bool has( key k ) const + virtual bool has( const key &k ) const { bool bFill; probe( __calcHashCode( k ), k, bFill ); @@ -873,7 +873,7 @@ namespace Bu } } - virtual void fill( uint32_t loc, key &k, value &v, uint32_t hash ) + virtual void fill( uint32_t loc, const key &k, const value &v, uint32_t hash ) { bFilled[loc/32] |= (1<<(loc%32)); va.construct( &aValues[loc], v ); @@ -945,7 +945,7 @@ namespace Bu return 0; } - uint32_t probe( uint32_t hash, key k, bool &bFill, bool rehash=true ) + uint32_t probe( uint32_t hash, const key &k, bool &bFill, bool rehash=true ) { uint32_t nCur = hash%nCapacity; diff --git a/src/unit/hash.cpp b/src/unit/hash.cpp index 1c873fd..e04a656 100644 --- a/src/unit/hash.cpp +++ b/src/unit/hash.cpp @@ -94,11 +94,11 @@ public: unitFailed("h.get(\"Number 2\") should have thrown an exception."); } catch( Bu::HashException &e ) { } - printf("\n"); + /* printf("\n"); for( StrIntHash::iterator i = h.begin(); i != h.end(); i++ ) { printf(" - \"%s\" = %d\n", i.getKey().getStr(), i.getValue() ); - } + } */ } }; -- cgit v1.2.3