diff --git a/applications/test/HashTable3/hashTableTest3.C b/applications/test/HashTable3/hashTableTest3.C index f8d26caf0643dfc856f1a828a73a04e8850ddf32..910e4f229052bc8c9305ea7b30d30a3907cbbfd0 100644 --- a/applications/test/HashTable3/hashTableTest3.C +++ b/applications/test/HashTable3/hashTableTest3.C @@ -74,6 +74,8 @@ int main(int argc, char *argv[]) { map.erase(elemI++); } + + map.shrink(); Info<< "loop " << iLoop << " - Erased " << nBase << " elements" << " (size " << map.size() << " capacity " << map.capacity() << ") " << timer.cpuTimeIncrement() << " s\n"; diff --git a/src/OpenFOAM/containers/HashTables/HashTbl/HashTbl.C b/src/OpenFOAM/containers/HashTables/HashTbl/HashTbl.C index 861f93df1121fbc41af9914993e6627848c9ad25..e6ae7b4ec91b8e6d8b80393e7e4cf716c72ee28d 100644 --- a/src/OpenFOAM/containers/HashTables/HashTbl/HashTbl.C +++ b/src/OpenFOAM/containers/HashTables/HashTbl/HashTbl.C @@ -542,6 +542,19 @@ void Foam::HashTbl<T, Key, Hash>::clearStorage() } +template<class T, class Key, class Hash> +void Foam::HashTbl<T, Key, Hash>::shrink() +{ + const label newSize = canonicalSize(nElmts_); + + if (newSize < tableSize_) + { + // avoid having the table disappear on us + resize(newSize ? newSize : 2); + } +} + + template<class T, class Key, class Hash> void Foam::HashTbl<T, Key, Hash>::transfer(HashTbl<T, Key, Hash>& ht) { diff --git a/src/OpenFOAM/containers/HashTables/HashTbl/HashTbl.H b/src/OpenFOAM/containers/HashTables/HashTbl/HashTbl.H index 877f8a1370205beae80dcaa02c7b55d149f8c179..ba7b010507c01c84d4b5bba4d63d27ca9d8de694 100644 --- a/src/OpenFOAM/containers/HashTables/HashTbl/HashTbl.H +++ b/src/OpenFOAM/containers/HashTables/HashTbl/HashTbl.H @@ -242,6 +242,9 @@ public: // Equivalent to clear() followed by resize(0) void clearStorage(); + //- Shrink the allocated table to approx. twice number of elements + void shrink(); + //- Transfer the contents of the argument table into this table // and annull the argument table. void transfer(HashTbl<T, Key, Hash>&);