Commit c2ac216e authored by Mark Olesen's avatar Mark Olesen
Browse files

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
parent a53b4735
......@@ -51,21 +51,15 @@ class Ostream;
template<class T, class Key, class Hash> class HashPtrTable;
template<class T, class Key, class Hash> Istream& operator>>
(
Istream&,
HashPtrTable<T, Key, Hash>&
);
template<class T, class Key, class Hash>
Istream& operator>>(Istream&, HashPtrTable<T, Key, Hash>&);
template<class T, class Key, class Hash> Ostream& operator<<
(
Ostream&,
const HashPtrTable<T, Key, Hash>&
);
template<class T, class Key, class Hash>
Ostream& operator<<(Ostream&, const HashPtrTable<T, Key, Hash>&);
/*---------------------------------------------------------------------------*\
Class HashPtrTable Declaration
Class HashPtrTable Declaration
\*---------------------------------------------------------------------------*/
template<class T, class Key=word, class Hash=string::hash>
......
......@@ -32,19 +32,19 @@ License
// * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
template<class Key, class Hash>
template<class T>
Foam::HashSet<Key, Hash>::HashSet(const HashTable<T, Key, Hash>& ht)
template<class AnyType>
Foam::HashSet<Key, Hash>::HashSet(const HashTable<AnyType, Key, Hash>& ht)
:
HashTable<nil, Key, Hash>(ht.size())
{
for
(
typename HashTable<T, Key, Hash>::const_iterator iter = ht.begin();
iter != ht.end();
++iter
typename HashTable<AnyType, Key, Hash>::const_iterator cit = ht.begin();
cit != ht.end();
++cit
)
{
insert(iter.key());
insert(cit.key());
}
}
......
......@@ -112,9 +112,10 @@ public:
HashTable<nil, Key, Hash>(hs)
{}
//- Construct from table of contents of the HashTable
template<class T>
HashSet(const HashTable<T, Key, Hash>& ht);
//- Construct from the keys of another HashTable,
// the type of values held is arbitrary.
template<class AnyType>
HashSet(const HashTable<AnyType, Key, Hash>&);
// Member Functions
......
......@@ -402,24 +402,49 @@ 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 UList<Key>& keys)
{
label count = 0;
// Remove listed keys from this table
if (this->size())
{
forAll(keys, keyI)
{
if (erase(keys[keyI]))
{
count++;
}
}
}
return count;
}
template<class T, class Key, class Hash>
template<class AnyType>
Foam::label Foam::HashTable<T, Key, Hash>::erase
(
const HashTable<T, Key, Hash>& rhs
const HashTable<AnyType, 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 (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;
}
......
......@@ -51,20 +51,15 @@ namespace Foam
// Forward declaration of friend functions and operators
template<class T> class List;
template<class T> class UList;
template<class T, class Key, class Hash> class HashTable;
template<class T, class Key, class Hash> class HashPtrTable;
template<class T, class Key, class Hash> Istream& operator>>
(
Istream&,
HashTable<T, Key, Hash>&
);
template<class T, class Key, class Hash>
Istream& operator>>(Istream&, HashTable<T, Key, Hash>&);
template<class T, class Key, class Hash> Ostream& operator<<
(
Ostream&,
const HashTable<T, Key, Hash>&
);
template<class T, class Key, class Hash>
Ostream& operator<<(Ostream&, const HashTable<T, Key, Hash>&);
/*---------------------------------------------------------------------------*\
......@@ -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<T, Key, Hash>&);
label erase(const UList<Key>&);
//- 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<class AnyType>
label erase(const HashTable<AnyType, Key, Hash>&);
//- Resize the hash table for efficiency
void resize(const label newSize);
......
......@@ -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<word> origNames(names());
labelList mapping(identity(max(origNames.toc()) + 1));
bool remap = false;
forAllConstIter(dictionary, mapDict, iter)
{
Map<word> matches = names(wordReList(iter().stream()));
wordReList patterns(iter().stream());
// find all matches
Map<word> matches;
forAllConstIter(Map<word>, 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)
}
forAllConstIter(Map<word>, matches, matchIter)
{
label idx = matchIter.key();
// the mapping and name for targetId is already okay
matches.erase(targetId);
origNames.erase(targetId);
if (idx != targetId && idx >= 0)
{
mapping[idx] = targetId;
this->erase(idx);
}
// remove matched names, leaving targetId on 'this'
this->erase(matches);
origNames.erase(matches);
forAllConstIter(Map<word>, matches, matchIter)
{
mapping[matchIter.key()] = targetId;
Info<< " " << matchIter();
}
Info<< " )" << endl;
remap = true;
}
}
......
......@@ -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
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment