From ed5e7684b766a3914b30c5b449608542695fd3b8 Mon Sep 17 00:00:00 2001 From: Mike Buland Date: Tue, 4 Feb 2020 09:01:45 -0800 Subject: Minor Bu::Hash updates and additions. Bu::Hash::KeyList has been added, I thought that was always there. Bu::Hash::rehash has been added. Rehashes can be triggered manually now. --- src/stable/hash.h | 40 ++++++++++++++++++++++++++++++++++------ 1 file changed, 34 insertions(+), 6 deletions(-) diff --git a/src/stable/hash.h b/src/stable/hash.h index 7f4066e..0c5bd9e 100644 --- a/src/stable/hash.h +++ b/src/stable/hash.h @@ -211,7 +211,7 @@ namespace Bu return 0; } - uint32_t probe( uint32_t hash, const key &k, bool &bFill, bool rehash=true ) + uint32_t probe( uint32_t hash, const key &k, bool &bFill, bool bRehash=true ) { init(); @@ -244,9 +244,9 @@ namespace Bu // This is our insurance, if the table is full, then go ahead and // rehash, then try again. - if( (isFilled( nCur ) || j == 32) && rehash == true ) + if( (isFilled( nCur ) || j == 32) && bRehash == true ) { - reHash( szCalc( nCapacity, nFilled, nDeleted ) ); + rehash( szCalc( nCapacity, nFilled, nDeleted ) ); // This is potentially dangerous, and could cause an infinite loop. // Be careful writing probe, eh? @@ -309,7 +309,7 @@ namespace Bu } } - void reHash( uint32_t nNewSize ) + void rehash( uint32_t nNewSize ) { //printf("--rehash: %d --> %d (%d, %d)\n", nCapacity, nNewSize, nFilled, nDeleted ); //printf("---REHASH---"); @@ -386,11 +386,13 @@ namespace Bu for( uint32_t j = 0; j < nCapacity; j++ ) { if( isFilled( j ) ) + { if( !isDeleted( j ) ) { va.destroy( &aValues[j] ); ka.destroy( &aKeys[j] ); } + } } va.deallocate( aValues, nCapacity ); ka.deallocate( aKeys, nCapacity ); @@ -501,6 +503,8 @@ namespace Bu { } + typedef Bu::List KeyList; + /** * Get the current hash table capacity. (Changes at re-hash) *@returns (uint32_t) The current capacity. @@ -1135,9 +1139,9 @@ namespace Bu * Get a list of all the keys in the hash table. *@returns (std::list) The list of keys in the hash table. */ - Bu::List getKeys() const + KeyList getKeys() const { - Bu::List lKeys; + KeyList lKeys; for( uint32_t j = 0; j < core->nCapacity; j++ ) { @@ -1171,6 +1175,30 @@ namespace Bu return lValues; } + /** + * This can be a very expensive operation, but when there are a decent + * number of deleted entries it can be good to be able to clean them + * up on your own terms. + * + * This will always allocate a new table and move all non-deleted items + * over to it. The size of the new table depends on which resizing + * calculator is selected. The default resize calculator will shrink + * the table if it's mostly deleted/empty space. + * + * This will be done by the system whenever it deems necesarry, but + * only during probing operations. That means that an insert could + * trigger a rehash, which could be very expensive. If you know, for + * example, that you'll be deleting most of the entries once a night + * during a low-usage time, that would probably be a good time to + * manually trigger a rehash and save the extra time on the next insert + * after the cleanup. This is partucularly true for systems like + * caches that need to be periodically cleaned up. + */ + void rehash() + { + core->rehash( core->szCalc( core->nCapacity, core->nFilled, core->nDeleted ) ); + } + bool operator==( const MyType &rhs ) const { if( this == &rhs ) -- cgit v1.2.3