From c2ac216eafbf3f4ca3679b25be65dcfe252612a3 Mon Sep 17 00:00:00 2001 From: Mark Olesen Date: Sun, 11 Jan 2009 20:01:53 +0100 Subject: [PATCH] HashTable::erase from list of keys or the keys from another HashTable - the parameter HashTable can hold arbitrary data .. just the type of keys needs to match --- .../HashTables/HashPtrTable/HashPtrTable.H | 16 +++----- .../containers/HashTables/HashSet/HashSet.C | 12 +++--- .../containers/HashTables/HashSet/HashSet.H | 7 ++-- .../HashTables/HashTable/HashTable.C | 37 +++++++++++++++--- .../HashTables/HashTable/HashTable.H | 26 +++++++------ src/conversion/meshTables/cellTable.C | 39 ++++++++++++------- src/conversion/meshTables/remappingDict | 3 +- 7 files changed, 88 insertions(+), 52 deletions(-) diff --git a/src/OpenFOAM/containers/HashTables/HashPtrTable/HashPtrTable.H b/src/OpenFOAM/containers/HashTables/HashPtrTable/HashPtrTable.H index e452017d23..af3ea9c423 100644 --- a/src/OpenFOAM/containers/HashTables/HashPtrTable/HashPtrTable.H +++ b/src/OpenFOAM/containers/HashTables/HashPtrTable/HashPtrTable.H @@ -51,21 +51,15 @@ class Ostream; template class HashPtrTable; -template Istream& operator>> -( - Istream&, - HashPtrTable& -); +template +Istream& operator>>(Istream&, HashPtrTable&); -template Ostream& operator<< -( - Ostream&, - const HashPtrTable& -); +template +Ostream& operator<<(Ostream&, const HashPtrTable&); /*---------------------------------------------------------------------------*\ - Class HashPtrTable Declaration + Class HashPtrTable Declaration \*---------------------------------------------------------------------------*/ template diff --git a/src/OpenFOAM/containers/HashTables/HashSet/HashSet.C b/src/OpenFOAM/containers/HashTables/HashSet/HashSet.C index b741bd2b0b..2136916780 100644 --- a/src/OpenFOAM/containers/HashTables/HashSet/HashSet.C +++ b/src/OpenFOAM/containers/HashTables/HashSet/HashSet.C @@ -32,19 +32,19 @@ License // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * // template -template -Foam::HashSet::HashSet(const HashTable& ht) +template +Foam::HashSet::HashSet(const HashTable& ht) : HashTable(ht.size()) { for ( - typename HashTable::const_iterator iter = ht.begin(); - iter != ht.end(); - ++iter + typename HashTable::const_iterator cit = ht.begin(); + cit != ht.end(); + ++cit ) { - insert(iter.key()); + insert(cit.key()); } } diff --git a/src/OpenFOAM/containers/HashTables/HashSet/HashSet.H b/src/OpenFOAM/containers/HashTables/HashSet/HashSet.H index 0ea8f8c646..ad45dc9540 100644 --- a/src/OpenFOAM/containers/HashTables/HashSet/HashSet.H +++ b/src/OpenFOAM/containers/HashTables/HashSet/HashSet.H @@ -112,9 +112,10 @@ public: HashTable(hs) {} - //- Construct from table of contents of the HashTable - template - HashSet(const HashTable& ht); + //- Construct from the keys of another HashTable, + // the type of values held is arbitrary. + template + HashSet(const HashTable&); // Member Functions diff --git a/src/OpenFOAM/containers/HashTables/HashTable/HashTable.C b/src/OpenFOAM/containers/HashTables/HashTable/HashTable.C index d6f6a57502..ae9bcc2718 100644 --- a/src/OpenFOAM/containers/HashTables/HashTable/HashTable.C +++ b/src/OpenFOAM/containers/HashTables/HashTable/HashTable.C @@ -402,24 +402,49 @@ bool Foam::HashTable::erase(const Key& key) template +Foam::label Foam::HashTable::erase(const UList& keys) +{ + label count = 0; + + // Remove listed keys from this table + if (this->size()) + { + forAll(keys, keyI) + { + if (erase(keys[keyI])) + { + count++; + } + } + } + + return count; +} + + +template +template Foam::label Foam::HashTable::erase ( - const HashTable& rhs + const HashTable& 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 (this->size()) { - if (rhs.found(iter.key()) && erase(iter)) + // NOTE: could further optimize depending on which hash is smaller + for (iterator iter = begin(); iter != end(); ++iter) { - count++; + if (rhs.found(iter.key()) && erase(iter)) + { + count++; + } } } - return count; + return count; } diff --git a/src/OpenFOAM/containers/HashTables/HashTable/HashTable.H b/src/OpenFOAM/containers/HashTables/HashTable/HashTable.H index 47cbc6b4c9..2fbb548d0e 100644 --- a/src/OpenFOAM/containers/HashTables/HashTable/HashTable.H +++ b/src/OpenFOAM/containers/HashTables/HashTable/HashTable.H @@ -51,20 +51,15 @@ namespace Foam // Forward declaration of friend functions and operators template class List; +template class UList; template class HashTable; template class HashPtrTable; -template Istream& operator>> -( - Istream&, - HashTable& -); +template +Istream& operator>>(Istream&, HashTable&); -template Ostream& operator<< -( - Ostream&, - const HashTable& -); +template +Ostream& operator<<(Ostream&, const HashTable&); /*---------------------------------------------------------------------------*\ @@ -203,9 +198,16 @@ public: //- Erase an hashedEntry specified by given key if in table bool erase(const Key&); - //- Remove entries in the given HashTable from this HashTable + //- Remove entries given by the listed keys from this HashTable // Return the number of elements removed - label erase(const HashTable&); + label erase(const UList&); + + //- Remove entries given by the given keys from this HashTable + // Return the number of elements removed. + // The parameter HashTable needs the same type of keys, but + // but the type of values held is arbitrary. + template + label erase(const HashTable&); //- Resize the hash table for efficiency void resize(const label newSize); diff --git a/src/conversion/meshTables/cellTable.C b/src/conversion/meshTables/cellTable.C index 5239e6bc12..2d640deb7e 100644 --- a/src/conversion/meshTables/cellTable.C +++ b/src/conversion/meshTables/cellTable.C @@ -514,22 +514,32 @@ void Foam::cellTable::combine(const dictionary& mapDict, labelList& tableIds) return; } - bool remap = false; - labelList mapping(identity(max(this->toc()) + 1)); + Map origNames(names()); + labelList mapping(identity(max(origNames.toc()) + 1)); + bool remap = false; forAllConstIter(dictionary, mapDict, iter) { - Map matches = names(wordReList(iter().stream())); + wordReList patterns(iter().stream()); + + // find all matches + Map matches; + forAllConstIter(Map, origNames, namesIter) + { + if (findStrings(patterns, namesIter())) + { + matches.insert(namesIter.key(), namesIter()); + } + } if (matches.size()) { - remap = true; label targetId = this->findIndex(iter().keyword()); Info<< "combine cellTable: " << iter().keyword(); if (targetId < 0) { - // re-use the first element if possible + // not found - reuse 1st element but with different name targetId = min(matches.toc()); operator[](targetId).set("Label", iter().keyword()); @@ -541,19 +551,22 @@ void Foam::cellTable::combine(const dictionary& mapDict, labelList& tableIds) } + // the mapping and name for targetId is already okay + matches.erase(targetId); + origNames.erase(targetId); + + // remove matched names, leaving targetId on 'this' + this->erase(matches); + origNames.erase(matches); + forAllConstIter(Map, matches, matchIter) { - label idx = matchIter.key(); - - if (idx != targetId && idx >= 0) - { - mapping[idx] = targetId; - this->erase(idx); - } - + mapping[matchIter.key()] = targetId; Info<< " " << matchIter(); } Info<< " )" << endl; + + remap = true; } } diff --git a/src/conversion/meshTables/remappingDict b/src/conversion/meshTables/remappingDict index d7059d051b..1a53982f79 100644 --- a/src/conversion/meshTables/remappingDict +++ b/src/conversion/meshTables/remappingDict @@ -21,10 +21,11 @@ FoamFile // newName ( listOldNames ); cellTable { - fluid ( fluid "[Ff]Luid[0-9]+" "(inlet|outlet)Region" ); + fluid ( fluid "[Ff]Luid[0-9]+" "(inlet|outlet)Region" "cellTable_[0-9]+" ); cat1 ( CAT1 "cat1_(Back|Front|Gamma)" ); } + // rename boundary regions // newName oldName; boundaryRegion