Commit a2ddf7dd authored by Mark OLESEN's avatar Mark OLESEN
Browse files

ENH: support HashTable erasure via a FixedList

- propagate common erasure methods as HashSet::unset() method,
  for symmetry with HashSet::set()
parent ded105c5
......@@ -64,8 +64,8 @@ class HashSet
public:
typedef typename HashTable<nil, Key, Hash>::iterator iterator;
typedef typename HashTable<nil, Key, Hash>::const_iterator const_iterator;
using iterator = typename HashTable<nil, Key, Hash>::iterator;
using const_iterator = typename HashTable<nil, Key, Hash>::const_iterator;
// Constructors
......@@ -154,6 +154,25 @@ public:
return HashTable<nil, Key, Hash>::erase(key);
}
//- Unset the listed keys - same as erase
label unset(const UList<Key>& lst)
{
return HashTable<nil, Key, Hash>::erase(lst);
}
//- Unset the listed keys - same as erase
template<unsigned Size>
label unset(const FixedList<Key, Size>& lst)
{
return HashTable<nil, Key, Hash>::erase(lst);
}
//- Unset the listed keys - same as erase
label unset(std::initializer_list<Key> lst)
{
return HashTable<nil, Key, Hash>::erase(lst);
}
// Member Operators
......
......@@ -28,8 +28,34 @@ License
#include "HashTable.H"
#include "List.H"
#include "FixedList.H"
#include "Tuple2.H"
// * * * * * * * * * * * * Protected Member Functions * * * * * * * * * * * //
template<class T, class Key, class Hash>
template<class InputIter>
Foam::label Foam::HashTable<T, Key, Hash>::eraseMultiple
(
const InputIter begIter,
const InputIter endIter
)
{
const label nTotal = this->size();
label changed = 0;
// Terminates early if possible
for (InputIter iter = begIter; changed < nTotal && iter != endIter; ++iter)
{
if (this->erase(*iter))
{
++changed;
}
}
return changed;
}
// * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
template<class T, class Key, class Hash>
......@@ -389,19 +415,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 UList<Key>& keys)
{
const label nTotal = nElmts_;
label count = 0;
return eraseMultiple(keys.begin(), keys.end());
}
// Remove listed keys from this table - terminates early if possible
for (label keyI = 0; count < nTotal && keyI < keys.size(); ++keyI)
{
if (erase(keys[keyI]))
{
++count;
}
}
return count;
template<class T, class Key, class Hash>
template<unsigned Size>
Foam::label Foam::HashTable<T, Key, Hash>::erase
(
const FixedList<Key, Size>& keys
)
{
return eraseMultiple(keys.begin(), keys.end());
}
template<class T, class Key, class Hash>
Foam::label Foam::HashTable<T, Key, Hash>::erase
(
std::initializer_list<Key> keys
)
{
return eraseMultiple(keys.begin(), keys.end());
}
......@@ -412,19 +447,21 @@ Foam::label Foam::HashTable<T, Key, Hash>::erase
const HashTable<AnyType, Key, AnyHash>& rhs
)
{
label count = 0;
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 ...
for (iterator iter = begin(); iter != end(); ++iter)
// Terminates early if possible
for (iterator iter = begin(); changed < nTotal && iter != end(); ++iter)
{
if (rhs.found(iter.key()) && erase(iter))
{
++count;
++changed;
}
}
return count;
return changed;
}
......
......@@ -60,6 +60,7 @@ namespace Foam
template<class T> class List;
template<class T> class UList;
template<class T, unsigned Size> class FixedList;
template<class T, class Key, class Hash> class HashTable;
template<class T, class Key, class Hash> class HashPtrTable;
......@@ -174,6 +175,18 @@ class HashTable
bool set(const Key& key, const T& newEntry, const bool protect);
protected:
// Protected Member Functions
//- Remove using begin/end iterators of listed keys
template<class InputIter>
inline label eraseMultiple
(
const InputIter begIter,
const InputIter endIter
);
public:
// Forward declaration of iterators
......@@ -274,6 +287,15 @@ public:
// Return the number of elements removed
label erase(const UList<Key>& keys);
//- Remove entries given by the listed keys from this HashTable
// Return the number of elements removed
template<unsigned Size>
label erase(const FixedList<Key, Size>& keys);
//- Remove entries given by the listed keys from this HashTable
// Return the number of elements removed
label erase(std::initializer_list<Key> keys);
//- Remove entries given by the given keys from this HashTable
// Return the number of elements removed.
// The parameter HashTable needs the same type of key, but the
......
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