ENH: added subtraction operator for HashSet

- offers similarity with bitSet

STYLE: remove remnant parent::operator= from HashSet

STYLE: code formatting in HashTables
This commit is contained in:
Mark Olesen
2020-07-24 18:12:31 +02:00
parent 2de4501e47
commit 85e74567ff
6 changed files with 115 additions and 78 deletions

View File

@ -6,7 +6,7 @@
\\/ M anipulation | \\/ M anipulation |
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
Copyright (C) 2011-2016 OpenFOAM Foundation Copyright (C) 2011-2016 OpenFOAM Foundation
Copyright (C) 2017-2019 OpenCFD Ltd. Copyright (C) 2017-2020 OpenCFD Ltd.
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
License License
This file is part of OpenFOAM. This file is part of OpenFOAM.
@ -34,21 +34,23 @@ License
template<class T, class Key, class Hash> template<class T, class Key, class Hash>
Foam::HashPtrTable<T, Key, Hash>::HashPtrTable Foam::HashPtrTable<T, Key, Hash>::HashPtrTable
( (
const HashPtrTable<T, Key, Hash>& ht const HashPtrTable<T, Key, Hash>& rhs
) )
: :
parent_type(ht.capacity()) parent_type(rhs.capacity())
{ {
for (const_iterator iter = ht.begin(); iter != ht.end(); ++iter) for (const_iterator iter = rhs.begin(); iter != rhs.end(); ++iter)
{ {
const Key& k = iter.key();
const T* ptr = iter.val(); const T* ptr = iter.val();
if (ptr) if (ptr)
{ {
this->set(iter.key(), new T(*ptr)); this->set(k, new T(*ptr));
} }
else else
{ {
this->set(iter.key(), nullptr); this->set(k, nullptr);
} }
} }
} }
@ -57,10 +59,10 @@ Foam::HashPtrTable<T, Key, Hash>::HashPtrTable
template<class T, class Key, class Hash> template<class T, class Key, class Hash>
Foam::HashPtrTable<T, Key, Hash>::HashPtrTable Foam::HashPtrTable<T, Key, Hash>::HashPtrTable
( (
HashPtrTable<T, Key, Hash>&& ht HashPtrTable<T, Key, Hash>&& rhs
) )
: :
parent_type(std::move(ht)) parent_type(std::move(rhs))
{} {}
@ -80,9 +82,9 @@ Foam::autoPtr<T> Foam::HashPtrTable<T, Key, Hash>::remove(iterator& iter)
{ {
if (iter.good()) if (iter.good())
{ {
autoPtr<T> aptr(iter.val()); autoPtr<T> old(iter.val());
this->parent_type::erase(iter); this->parent_type::erase(iter);
return aptr; return old;
} }
return nullptr; return nullptr;
@ -92,7 +94,7 @@ Foam::autoPtr<T> Foam::HashPtrTable<T, Key, Hash>::remove(iterator& iter)
template<class T, class Key, class Hash> template<class T, class Key, class Hash>
Foam::autoPtr<T> Foam::HashPtrTable<T, Key, Hash>::remove(const Key& key) Foam::autoPtr<T> Foam::HashPtrTable<T, Key, Hash>::remove(const Key& key)
{ {
auto iter = this->find(key); iterator iter(this->find(key));
return this->remove(iter); return this->remove(iter);
} }
@ -105,12 +107,8 @@ bool Foam::HashPtrTable<T, Key, Hash>::erase(iterator& iter)
T* ptr = iter.val(); T* ptr = iter.val();
if (this->parent_type::erase(iter)) if (this->parent_type::erase(iter))
{
if (ptr)
{ {
delete ptr; delete ptr;
}
return true; return true;
} }
} }
@ -122,7 +120,7 @@ bool Foam::HashPtrTable<T, Key, Hash>::erase(iterator& iter)
template<class T, class Key, class Hash> template<class T, class Key, class Hash>
bool Foam::HashPtrTable<T, Key, Hash>::erase(const Key& key) bool Foam::HashPtrTable<T, Key, Hash>::erase(const Key& key)
{ {
auto iter = this->find(key); iterator iter(this->find(key));
return this->erase(iter); return this->erase(iter);
} }
@ -156,14 +154,16 @@ void Foam::HashPtrTable<T, Key, Hash>::operator=
for (const_iterator iter = rhs.begin(); iter != rhs.end(); ++iter) for (const_iterator iter = rhs.begin(); iter != rhs.end(); ++iter)
{ {
const Key& k = iter.key();
const T* ptr = iter.val(); const T* ptr = iter.val();
if (ptr) if (ptr)
{ {
this->set(iter.key(), new T(*ptr)); this->set(k, new T(*ptr));
} }
else else
{ {
this->set(iter.key(), nullptr); this->set(k, nullptr);
} }
} }
} }

View File

@ -32,6 +32,7 @@ Description
SourceFiles SourceFiles
HashPtrTable.C HashPtrTable.C
HashPtrTableI.H
HashPtrTableIO.C HashPtrTableIO.C
\*---------------------------------------------------------------------------*/ \*---------------------------------------------------------------------------*/
@ -46,10 +47,7 @@ SourceFiles
namespace Foam namespace Foam
{ {
// Forward declarations // Forward Declarations
class Istream;
class Ostream;
template<class T> class autoPtr; template<class T> class autoPtr;
template<class T, class Key, class Hash> class HashPtrTable; template<class T, class Key, class Hash> class HashPtrTable;
@ -92,7 +90,7 @@ public:
// Constructors // Constructors
//- Construct null with default table capacity //- Default construct with default table capacity
inline HashPtrTable(); inline HashPtrTable();
//- Construct given initial table capacity //- Construct given initial table capacity
@ -108,11 +106,11 @@ public:
//- Construct from dictionary with default dictionary constructor class //- Construct from dictionary with default dictionary constructor class
explicit HashPtrTable(const dictionary& dict); explicit HashPtrTable(const dictionary& dict);
//- Copy construct //- Copy construct, making a copy of each element
HashPtrTable(const this_type& ht); HashPtrTable(const this_type& rhs);
//- Move construct //- Move construct
HashPtrTable(this_type&& ht); HashPtrTable(this_type&& rhs);
//- Destructor //- Destructor
@ -172,7 +170,7 @@ public:
); );
// Housekeeping // Override HashTable methods
//- No insert() with raw pointers (potential memory leaks). //- No insert() with raw pointers (potential memory leaks).
//- Use insert() with autoPtr or set() //- Use insert() with autoPtr or set()
@ -181,21 +179,21 @@ public:
//- Insert a new entry, not overwriting existing entries. //- Insert a new entry, not overwriting existing entries.
// //
// \return True if the entry inserted (not previously in table) // \return True if the entry inserted (not previously in table)
inline bool insert(const Key& key, autoPtr<T>& aptr); inline bool insert(const Key& key, autoPtr<T>& ptr);
//- Insert a new entry, not overwriting existing entries. //- Insert a new entry, not overwriting existing entries.
// //
// \return True if the entry inserted (not previously in table) // \return True if the entry inserted (not previously in table)
inline bool insert(const Key& key, autoPtr<T>&& aptr); inline bool insert(const Key& key, autoPtr<T>&& ptr);
//- Assign a new entry, overwriting existing entries. //- Assign a new entry, overwriting existing entries.
inline bool set(const Key& key, T* ptr); inline bool set(const Key& key, T* ptr);
//- Assign a new entry, overwriting existing entries. //- Assign a new entry, overwriting existing entries.
inline bool set(const Key& key, autoPtr<T>& aptr); inline bool set(const Key& key, autoPtr<T>& ptr);
//- Assign a new entry, overwriting existing entries. //- Assign a new entry, overwriting existing entries.
inline bool set(const Key& key, autoPtr<T>&& aptr); inline bool set(const Key& key, autoPtr<T>&& ptr);
}; };

View File

@ -5,7 +5,7 @@
\\ / A nd | www.openfoam.com \\ / A nd | www.openfoam.com
\\/ M anipulation | \\/ M anipulation |
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
Copyright (C) 2018 OpenCFD Ltd. Copyright (C) 2018-2020 OpenCFD Ltd.
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
License License
This file is part of OpenFOAM. This file is part of OpenFOAM.
@ -49,12 +49,12 @@ template<class T, class Key, class Hash>
inline bool Foam::HashPtrTable<T, Key, Hash>::insert inline bool Foam::HashPtrTable<T, Key, Hash>::insert
( (
const Key& key, const Key& key,
autoPtr<T>& aptr autoPtr<T>&& ptr
) )
{ {
if (parent_type::insert(key, aptr.get())) if (parent_type::insert(key, ptr.get()))
{ {
aptr.release(); // Now owned by HashPtrTable ptr.release(); // Now owned by HashPtrTable
return true; return true;
} }
@ -66,12 +66,12 @@ template<class T, class Key, class Hash>
inline bool Foam::HashPtrTable<T, Key, Hash>::insert inline bool Foam::HashPtrTable<T, Key, Hash>::insert
( (
const Key& key, const Key& key,
autoPtr<T>&& aptr autoPtr<T>&& ptr
) )
{ {
if (parent_type::insert(key, aptr.get())) if (parent_type::insert(key, ptr.get()))
{ {
aptr.release(); // Now owned by HashPtrTable ptr.release(); // Now owned by HashPtrTable
return true; return true;
} }
@ -94,10 +94,10 @@ template<class T, class Key, class Hash>
inline bool Foam::HashPtrTable<T, Key, Hash>::set inline bool Foam::HashPtrTable<T, Key, Hash>::set
( (
const Key& key, const Key& key,
autoPtr<T>& aptr autoPtr<T>& ptr
) )
{ {
return this->set(key, aptr.release()); return this->set(key, ptr.release());
} }
@ -105,10 +105,10 @@ template<class T, class Key, class Hash>
inline bool Foam::HashPtrTable<T, Key, Hash>::set inline bool Foam::HashPtrTable<T, Key, Hash>::set
( (
const Key& key, const Key& key,
autoPtr<T>&& aptr autoPtr<T>&& ptr
) )
{ {
return this->set(key, aptr.release()); return this->set(key, ptr.release());
} }

View File

@ -136,12 +136,13 @@ inline Foam::label Foam::HashSet<Key, Hash>::insert
) )
{ {
label changed = 0; label changed = 0;
for (; first != last; ++first) while (first != last)
{ {
if (insert(*first)) if (insert(*first))
{ {
++changed; ++changed;
} }
++first;
} }
return changed; return changed;
} }
@ -374,9 +375,9 @@ Foam::HashSet<Key, Hash>::operator-=(const HashSet<Key, Hash>& rhs)
// * * * * * * * * * * * * * * * IOstream Operators * * * * * * * * * * * * // // * * * * * * * * * * * * * * * IOstream Operators * * * * * * * * * * * * //
template<class Key, class Hash> template<class Key, class Hash>
Foam::Ostream& Foam::operator<<(Ostream& os, const HashSet<Key, Hash>& tbl) Foam::Ostream& Foam::operator<<(Ostream& os, const HashSet<Key, Hash>& rhs)
{ {
return tbl.writeKeys(os, Detail::ListPolicy::short_length<Key>::value); return rhs.writeKeys(os, Detail::ListPolicy::short_length<Key>::value);
} }
@ -385,39 +386,68 @@ Foam::Ostream& Foam::operator<<(Ostream& os, const HashSet<Key, Hash>& tbl)
template<class Key, class Hash> template<class Key, class Hash>
Foam::HashSet<Key, Hash> Foam::operator| Foam::HashSet<Key, Hash> Foam::operator|
( (
const HashSet<Key, Hash>& hash1, const HashSet<Key, Hash>& a,
const HashSet<Key, Hash>& hash2 const HashSet<Key, Hash>& b
) )
{ {
HashSet<Key, Hash> out(hash1); HashSet<Key, Hash> result(a);
out |= hash2; result |= b;
return out; return result;
} }
template<class Key, class Hash> template<class Key, class Hash>
Foam::HashSet<Key, Hash> Foam::operator& Foam::HashSet<Key, Hash> Foam::operator&
( (
const HashSet<Key, Hash>& hash1, const HashSet<Key, Hash>& a,
const HashSet<Key, Hash>& hash2 const HashSet<Key, Hash>& b
) )
{ {
HashSet<Key, Hash> out(hash1); HashSet<Key, Hash> result(a.capacity());
out &= hash2;
return out; for (const Key& k : a)
{
if (b.found(k))
{
result.insert(k);
}
}
return result;
} }
template<class Key, class Hash> template<class Key, class Hash>
Foam::HashSet<Key, Hash> Foam::operator^ Foam::HashSet<Key, Hash> Foam::operator^
( (
const HashSet<Key, Hash>& hash1, const HashSet<Key, Hash>& a,
const HashSet<Key, Hash>& hash2 const HashSet<Key, Hash>& b
) )
{ {
HashSet<Key, Hash> out(hash1); HashSet<Key, Hash> result(a);
out ^= hash2; result ^= b;
return out; return result;
}
template<class Key, class Hash>
Foam::HashSet<Key, Hash> Foam::operator-
(
const HashSet<Key, Hash>& a,
const HashSet<Key, Hash>& b
)
{
HashSet<Key, Hash> result(a.capacity());
for (const Key& k : a)
{
if (!b.found(k))
{
result.insert(k);
}
}
return result;
} }

View File

@ -313,20 +313,19 @@ public:
// Member Operators // Member Operators
//- Return true if the entry exists, same as found() //- Return true if the entry exists, same as found()
// \note this allows use of HashSet as a predicate test
inline bool operator()(const Key& key) const noexcept; inline bool operator()(const Key& key) const noexcept;
//- Return true if the entry exists, same as found(). //- Return true if the entry exists, same as found().
inline bool operator[](const Key& key) const noexcept; inline bool operator[](const Key& key) const noexcept;
using parent_type::operator=; //- Copy assign
//- Copy assignment
void operator=(const this_type& rhs) void operator=(const this_type& rhs)
{ {
parent_type::operator=(rhs); parent_type::operator=(rhs);
} }
//- Move assignment //- Move assign
void operator=(this_type&& rhs) void operator=(this_type&& rhs)
{ {
parent_type::operator=(std::move(rhs)); parent_type::operator=(std::move(rhs));
@ -429,33 +428,43 @@ MinMax<label> minMax(const labelHashSet& set);
//- Write the list of HashSet keys //- Write the list of HashSet keys
template<class Key, class Hash> template<class Key, class Hash>
Ostream& operator<<(Ostream& os, const HashSet<Key, Hash>& tbl); Ostream& operator<<(Ostream& os, const HashSet<Key, Hash>& rhs);
//- Combine entries from HashSets //- Combine entries (OR) for two HashSets
// See HashSet::operator|= for more details.
template<class Key, class Hash> template<class Key, class Hash>
HashSet<Key, Hash> operator| HashSet<Key, Hash> operator|
( (
const HashSet<Key, Hash>& hash1, const HashSet<Key, Hash>& a,
const HashSet<Key, Hash>& hash2 const HashSet<Key, Hash>& b
); );
//- Subset (AND) intersection of two HashSet
//- Create a HashSet that only contains entries found in both HashSets // See HashSet::operator&= for more details.
template<class Key, class Hash> template<class Key, class Hash>
HashSet<Key, Hash> operator& HashSet<Key, Hash> operator&
( (
const HashSet<Key, Hash>& hash1, const HashSet<Key, Hash>& a,
const HashSet<Key, Hash>& hash2 const HashSet<Key, Hash>& b
); );
//- Create a HashSet that only contains unique entries (XOR)
//- Create a HashSet that only contains unique entries (xor) // See HashSet::operator^= for more details.
template<class Key, class Hash> template<class Key, class Hash>
HashSet<Key, Hash> operator^ HashSet<Key, Hash> operator^
( (
const HashSet<Key, Hash>& hash1, const HashSet<Key, Hash>& a,
const HashSet<Key, Hash>& hash2 const HashSet<Key, Hash>& b
);
//- Subset difference of two HashSets
// See HashSet::operator-= for more details.
template<class Key, class Hash>
HashSet<Key, Hash> operator-
(
const HashSet<Key, Hash>& a,
const HashSet<Key, Hash>& b
); );

View File

@ -405,7 +405,7 @@ bool Foam::HashTable<T, Key, Hash>::erase(const iterator& iter)
template<class T, class Key, class Hash> template<class T, class Key, class Hash>
bool Foam::HashTable<T, Key, Hash>::erase(const Key& key) bool Foam::HashTable<T, Key, Hash>::erase(const Key& key)
{ {
auto iter = find(key); iterator iter(find(key));
return erase(iter); return erase(iter);
} }