From 990a9e7f573e064176c500ab1daf9191c1d6c019 Mon Sep 17 00:00:00 2001 From: Mark Olesen <Mark.Olesen@Germany> Date: Fri, 9 Jan 2009 09:35:53 +0100 Subject: [PATCH] added HashTable::erase(const HashTable&) method --- applications/test/HashTable/hashTableTest.C | 24 ++++++++++++-- .../StaticHashTable/staticHashTableTest.C | 33 ++++++++++++++----- .../containers/HashTables/HashSet/HashSet.C | 1 + .../HashTables/HashTable/HashTable.C | 28 ++++++++++++++-- .../HashTables/HashTable/HashTable.H | 4 +++ .../StaticHashTable/StaticHashTable.C | 22 +++++++++++++ .../StaticHashTable/StaticHashTable.H | 4 +++ 7 files changed, 102 insertions(+), 14 deletions(-) diff --git a/applications/test/HashTable/hashTableTest.C b/applications/test/HashTable/hashTableTest.C index 633c0e1ba41..8e3db2c9d7a 100644 --- a/applications/test/HashTable/hashTableTest.C +++ b/applications/test/HashTable/hashTableTest.C @@ -100,12 +100,12 @@ int main() << "\ntable2" << table1 << nl << "\ntable3" << table3 << nl; - Info<< "\ndelete table2" << nl; + Info<< "\nerase table2 by iterator" << nl; forAllIter(HASHTABLE_CLASS<double>, table2, iter) { - Info<< "deleting " << iter.key() << " => " << iter() << " ... "; + Info<< "erasing " << iter.key() << " => " << iter() << " ... "; table2.erase(iter); - Info<< "deleted" << endl; + Info<< "erased" << endl; } Info<< "\ntable1" << table1 << nl @@ -134,6 +134,24 @@ int main() table1.erase(table1.begin()); Info<< "removed an element - test table1 != table3 : " << (table1 != table3) << nl; + + // insert a few things into table2 + table2.set("ada", 14.0); + table2.set("aeq", 15.0); + table2.set("aaw", 16.0); + table2.set("abs", 17.0); + table2.set("adx", 20.0); + + Info<< "\ntable1" << table1 << nl + << "\ntable2" << table2 << nl; + + label nErased = table1.erase(table2); + + Info<< "\nerase table2 keys from table1 (removed " + << nErased << " elements)" << nl + << "\ntable1" << table1 << nl + << "\ntable2" << table2 << nl; + Info<< "\nclearStorage table3 ... "; table3.clearStorage(); diff --git a/applications/test/StaticHashTable/staticHashTableTest.C b/applications/test/StaticHashTable/staticHashTableTest.C index da879d4d5e2..2060ec582c2 100644 --- a/applications/test/StaticHashTable/staticHashTableTest.C +++ b/applications/test/StaticHashTable/staticHashTableTest.C @@ -100,13 +100,13 @@ int main() << "\ntable2" << table1 << nl << "\ntable3" << table3 << nl; - Info<< "\ndelete table2" << nl; - forAllIter(HASHTABLE_CLASS<double>, table2, iter) - { - Info<< "deleting " << iter.key() << " => " << iter() << " ... "; - table2.erase(iter); - Info<< "deleted" << endl; - } + Info<< "\nerase table2 by iterator" << nl; + forAllIter(HASHTABLE_CLASS<double>, table2, iter) + { + Info<< "erasing " << iter.key() << " => " << iter() << " ... "; + table2.erase(iter); + Info<< "erased" << endl; + } Info<< "\ntable1" << table1 << nl << "\ntable2" << table2 << nl @@ -134,6 +134,24 @@ int main() table1.erase(table1.begin()); Info<< "removed an element - test table1 != table3 : " << (table1 != table3) << nl; + + // insert a few things into table2 + table2.set("ada", 14.0); + table2.set("aeq", 15.0); + table2.set("aaw", 16.0); + table2.set("abs", 17.0); + table2.set("adx", 20.0); + + Info<< "\ntable1" << table1 << nl + << "\ntable2" << table2 << nl; + + label nErased = table1.erase(table2); + + Info<< "\nerase table2 keys from table1 (removed " + << nErased << " elements)" << nl + << "\ntable1" << table1 << nl + << "\ntable2" << table2 << nl; + Info<< "\nclearStorage table3 ... "; table3.clearStorage(); @@ -144,5 +162,4 @@ int main() return 0; } - // ************************************************************************* // diff --git a/src/OpenFOAM/containers/HashTables/HashSet/HashSet.C b/src/OpenFOAM/containers/HashTables/HashSet/HashSet.C index acb603bebd6..3e4123ae4f9 100644 --- a/src/OpenFOAM/containers/HashTables/HashSet/HashSet.C +++ b/src/OpenFOAM/containers/HashTables/HashSet/HashSet.C @@ -126,6 +126,7 @@ void Foam::HashSet<Key, Hash>::operator^=(const HashSet<Key, Hash>& rhs) } +// same as HashTable::erase() template<class Key, class Hash> void Foam::HashSet<Key, Hash>::operator-=(const HashSet<Key, Hash>& rhs) { diff --git a/src/OpenFOAM/containers/HashTables/HashTable/HashTable.C b/src/OpenFOAM/containers/HashTables/HashTable/HashTable.C index 5daf0eb6fee..d6f6a57502d 100644 --- a/src/OpenFOAM/containers/HashTables/HashTable/HashTable.C +++ b/src/OpenFOAM/containers/HashTables/HashTable/HashTable.C @@ -388,11 +388,11 @@ bool Foam::HashTable<T, Key, Hash>::erase(const iterator& cit) template<class T, class Key, class Hash> bool Foam::HashTable<T, Key, Hash>::erase(const Key& key) { - iterator it = find(key); + iterator fnd = find(key); - if (it != end()) + if (fnd != end()) { - return erase(it); + return erase(fnd); } else { @@ -401,6 +401,28 @@ bool Foam::HashTable<T, Key, Hash>::erase(const Key& key) } +template<class T, class Key, class Hash> +Foam::label Foam::HashTable<T, Key, Hash>::erase +( + const HashTable<T, Key, Hash>& rhs +) +{ + label count = 0; + + // Remove rhs elements from this table + // NOTE: could optimize depending on which hash is smaller + for (iterator iter = this->begin(); iter != this->end(); ++iter) + { + if (rhs.found(iter.key()) && erase(iter)) + { + count++; + } + } + + return count; +} + + template<class T, class Key, class Hash> void Foam::HashTable<T, Key, Hash>::resize(const label newSize) { diff --git a/src/OpenFOAM/containers/HashTables/HashTable/HashTable.H b/src/OpenFOAM/containers/HashTables/HashTable/HashTable.H index a7319c833fe..0ecc044222e 100644 --- a/src/OpenFOAM/containers/HashTables/HashTable/HashTable.H +++ b/src/OpenFOAM/containers/HashTables/HashTable/HashTable.H @@ -200,6 +200,10 @@ public: //- Erase an hashedEntry specified by given key if in table bool erase(const Key&); + //- Remove entries in the given HashTable from this HashTable + // Return the number of elements removed + label erase(const HashTable<T, Key, Hash>&); + //- Resize the hash table for efficiency void resize(const label newSize); diff --git a/src/OpenFOAM/containers/HashTables/StaticHashTable/StaticHashTable.C b/src/OpenFOAM/containers/HashTables/StaticHashTable/StaticHashTable.C index 50d8ff57639..992205afaf9 100644 --- a/src/OpenFOAM/containers/HashTables/StaticHashTable/StaticHashTable.C +++ b/src/OpenFOAM/containers/HashTables/StaticHashTable/StaticHashTable.C @@ -346,6 +346,28 @@ bool Foam::StaticHashTable<T, Key, Hash>::erase(const Key& key) } +template<class T, class Key, class Hash> +Foam::label Foam::StaticHashTable<T, Key, Hash>::erase +( + const StaticHashTable<T, Key, Hash>& rhs +) +{ + label count = 0; + + // Remove rhs elements from this table + // NOTE: could optimize depending on which hash is smaller + for (iterator iter = this->begin(); iter != this->end(); ++iter) + { + if (rhs.found(iter.key()) && erase(iter)) + { + count++; + } + } + + return count; +} + + template<class T, class Key, class Hash> void Foam::StaticHashTable<T, Key, Hash>::resize(const label newSize) { diff --git a/src/OpenFOAM/containers/HashTables/StaticHashTable/StaticHashTable.H b/src/OpenFOAM/containers/HashTables/StaticHashTable/StaticHashTable.H index 95df9befce4..15044c33e21 100644 --- a/src/OpenFOAM/containers/HashTables/StaticHashTable/StaticHashTable.H +++ b/src/OpenFOAM/containers/HashTables/StaticHashTable/StaticHashTable.H @@ -196,6 +196,10 @@ public: //- Resize the hash table for efficiency void resize(const label newSize); + //- Remove entries in the given hash table from this hash table + // Return the number of elements removed + label erase(const StaticHashTable<T, Key, Hash>&); + //- Clear all entries from table void clear(); -- GitLab