Commit 5d6bf3e4 authored by Mark OLESEN's avatar Mark OLESEN
Browse files

ENH: HashTable improvements

- optimize erasure using different HashTable based on its size.
  Eg, hashtable.erase(other);

  If 'other' is smaller than the hashtable, it is more efficient to
  use the keys from other to remove from the hashtable.

  Otherwise simply iterate over the hashtable and remove it if
  that key was found in other.
parent c65e2e58
......@@ -44,8 +44,12 @@ Foam::label Foam::HashTable<T, Key, Hash>::eraseMultiple
const label nTotal = this->size();
label changed = 0;
// Terminates early if possible
for (InputIter iter = begIter; changed < nTotal && iter != endIter; ++iter)
for
(
InputIter iter = begIter;
changed < nTotal && iter != endIter; // terminate early
++iter
)
{
if (this->erase(*iter))
{
......@@ -444,23 +448,49 @@ template<class T, class Key, class Hash>
template<class AnyType, class AnyHash>
Foam::label Foam::HashTable<T, Key, Hash>::erase
(
const HashTable<AnyType, Key, AnyHash>& rhs
const HashTable<AnyType, Key, AnyHash>& other
)
{
// Remove other keys from this table
const label nTotal = this->size();
label changed = 0;
// Remove rhs keys from this table - terminates early if possible
// Could optimize depending on which hash is smaller ...
if (other.size() < nTotal)
{
// other is smaller, use its keys for removal
using other_iter =
typename HashTable<AnyType, Key, AnyHash>::const_iterator;
// Terminates early if possible
for (iterator iter = begin(); changed < nTotal && iter != end(); ++iter)
for
(
other_iter iter = other.begin();
changed < nTotal && iter != other.end(); // terminate early
++iter
)
{
if (erase(iter.key()))
{
++changed;
}
}
}
else
{
if (rhs.found(iter.key()) && erase(iter))
// other is same/larger: iterate ourselves and check for key in other
for
(
iterator iter = begin();
changed < nTotal && iter != end(); // terminate early
++iter
)
{
++changed;
if (other.found(iter.key()) && erase(iter))
{
++changed;
}
}
}
return changed;
}
......
......@@ -301,7 +301,7 @@ public:
// The parameter HashTable needs the same type of key, but the
// type of values held and the hashing function are arbitrary.
template<class AnyType, class AnyHash>
label erase(const HashTable<AnyType, Key, AnyHash>& rhs);
label erase(const HashTable<AnyType, Key, AnyHash>& other);
//- Resize the hash table for efficiency
void resize(const label sz);
......
Supports Markdown
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