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
This commit is contained in:
Mark Olesen
2009-01-11 20:01:53 +01:00
parent a53b47355a
commit c2ac216eaf
7 changed files with 88 additions and 52 deletions

View File

@ -51,17 +51,11 @@ class Ostream;
template<class T, class Key, class Hash> class HashPtrTable; template<class T, class Key, class Hash> class HashPtrTable;
template<class T, class Key, class Hash> Istream& operator>> template<class T, class Key, class Hash>
( Istream& operator>>(Istream&, HashPtrTable<T, Key, Hash>&);
Istream&,
HashPtrTable<T, Key, Hash>&
);
template<class T, class Key, class Hash> Ostream& operator<< template<class T, class Key, class Hash>
( Ostream& operator<<(Ostream&, const HashPtrTable<T, Key, Hash>&);
Ostream&,
const HashPtrTable<T, Key, Hash>&
);
/*---------------------------------------------------------------------------*\ /*---------------------------------------------------------------------------*\

View File

@ -32,19 +32,19 @@ License
// * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * // // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
template<class Key, class Hash> template<class Key, class Hash>
template<class T> template<class AnyType>
Foam::HashSet<Key, Hash>::HashSet(const HashTable<T, Key, Hash>& ht) Foam::HashSet<Key, Hash>::HashSet(const HashTable<AnyType, Key, Hash>& ht)
: :
HashTable<nil, Key, Hash>(ht.size()) HashTable<nil, Key, Hash>(ht.size())
{ {
for for
( (
typename HashTable<T, Key, Hash>::const_iterator iter = ht.begin(); typename HashTable<AnyType, Key, Hash>::const_iterator cit = ht.begin();
iter != ht.end(); cit != ht.end();
++iter ++cit
) )
{ {
insert(iter.key()); insert(cit.key());
} }
} }

View File

@ -112,9 +112,10 @@ public:
HashTable<nil, Key, Hash>(hs) HashTable<nil, Key, Hash>(hs)
{} {}
//- Construct from table of contents of the HashTable //- Construct from the keys of another HashTable,
template<class T> // the type of values held is arbitrary.
HashSet(const HashTable<T, Key, Hash>& ht); template<class AnyType>
HashSet(const HashTable<AnyType, Key, Hash>&);
// Member Functions // Member Functions

View File

@ -402,22 +402,47 @@ bool Foam::HashTable<T, Key, Hash>::erase(const Key& key)
template<class T, class Key, class Hash> 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 Foam::label Foam::HashTable<T, Key, Hash>::erase
( (
const HashTable<T, Key, Hash>& rhs const HashTable<AnyType, Key, Hash>& rhs
) )
{ {
label count = 0; label count = 0;
// Remove rhs elements from this table // Remove rhs elements from this table
// NOTE: could optimize depending on which hash is smaller if (this->size())
for (iterator iter = this->begin(); iter != this->end(); ++iter) {
// NOTE: could further optimize depending on which hash is smaller
for (iterator iter = begin(); iter != end(); ++iter)
{ {
if (rhs.found(iter.key()) && erase(iter)) if (rhs.found(iter.key()) && erase(iter))
{ {
count++; count++;
} }
} }
}
return count; return count;
} }

View File

@ -51,20 +51,15 @@ namespace Foam
// Forward declaration of friend functions and operators // Forward declaration of friend functions and operators
template<class T> class List; 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 HashTable;
template<class T, class Key, class Hash> class HashPtrTable; template<class T, class Key, class Hash> class HashPtrTable;
template<class T, class Key, class Hash> Istream& operator>> template<class T, class Key, class Hash>
( Istream& operator>>(Istream&, HashTable<T, Key, Hash>&);
Istream&,
HashTable<T, Key, Hash>&
);
template<class T, class Key, class Hash> Ostream& operator<< template<class T, class Key, class Hash>
( Ostream& operator<<(Ostream&, const HashTable<T, Key, Hash>&);
Ostream&,
const HashTable<T, Key, Hash>&
);
/*---------------------------------------------------------------------------*\ /*---------------------------------------------------------------------------*\
@ -203,9 +198,16 @@ public:
//- Erase an hashedEntry specified by given key if in table //- Erase an hashedEntry specified by given key if in table
bool erase(const Key&); 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 // 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 //- Resize the hash table for efficiency
void resize(const label newSize); void resize(const label newSize);

View File

@ -514,22 +514,32 @@ void Foam::cellTable::combine(const dictionary& mapDict, labelList& tableIds)
return; return;
} }
bool remap = false; Map<word> origNames(names());
labelList mapping(identity(max(this->toc()) + 1)); labelList mapping(identity(max(origNames.toc()) + 1));
bool remap = false;
forAllConstIter(dictionary, mapDict, iter) 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()) if (matches.size())
{ {
remap = true;
label targetId = this->findIndex(iter().keyword()); label targetId = this->findIndex(iter().keyword());
Info<< "combine cellTable: " << iter().keyword(); Info<< "combine cellTable: " << iter().keyword();
if (targetId < 0) if (targetId < 0)
{ {
// re-use the first element if possible // not found - reuse 1st element but with different name
targetId = min(matches.toc()); targetId = min(matches.toc());
operator[](targetId).set("Label", iter().keyword()); 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<word>, matches, matchIter) forAllConstIter(Map<word>, matches, matchIter)
{ {
label idx = matchIter.key(); mapping[matchIter.key()] = targetId;
if (idx != targetId && idx >= 0)
{
mapping[idx] = targetId;
this->erase(idx);
}
Info<< " " << matchIter(); Info<< " " << matchIter();
} }
Info<< " )" << endl; Info<< " )" << endl;
remap = true;
} }
} }

View File

@ -21,10 +21,11 @@ FoamFile
// newName ( listOldNames ); // newName ( listOldNames );
cellTable 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)" ); cat1 ( CAT1 "cat1_(Back|Front|Gamma)" );
} }
// rename boundary regions // rename boundary regions
// newName oldName; // newName oldName;
boundaryRegion boundaryRegion