mirror of
https://develop.openfoam.com/Development/openfoam.git
synced 2025-11-28 03:28:01 +00:00
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:
@ -6,7 +6,7 @@
|
||||
\\/ M anipulation |
|
||||
-------------------------------------------------------------------------------
|
||||
Copyright (C) 2011-2016 OpenFOAM Foundation
|
||||
Copyright (C) 2017-2019 OpenCFD Ltd.
|
||||
Copyright (C) 2017-2020 OpenCFD Ltd.
|
||||
-------------------------------------------------------------------------------
|
||||
License
|
||||
This file is part of OpenFOAM.
|
||||
@ -34,21 +34,23 @@ License
|
||||
template<class T, class Key, class Hash>
|
||||
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();
|
||||
|
||||
if (ptr)
|
||||
{
|
||||
this->set(iter.key(), new T(*ptr));
|
||||
this->set(k, new T(*ptr));
|
||||
}
|
||||
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>
|
||||
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())
|
||||
{
|
||||
autoPtr<T> aptr(iter.val());
|
||||
autoPtr<T> old(iter.val());
|
||||
this->parent_type::erase(iter);
|
||||
return aptr;
|
||||
return old;
|
||||
}
|
||||
|
||||
return nullptr;
|
||||
@ -92,7 +94,7 @@ Foam::autoPtr<T> Foam::HashPtrTable<T, Key, Hash>::remove(iterator& iter)
|
||||
template<class T, class Key, class Hash>
|
||||
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);
|
||||
}
|
||||
|
||||
@ -106,11 +108,7 @@ bool Foam::HashPtrTable<T, Key, Hash>::erase(iterator& iter)
|
||||
|
||||
if (this->parent_type::erase(iter))
|
||||
{
|
||||
if (ptr)
|
||||
{
|
||||
delete ptr;
|
||||
}
|
||||
|
||||
delete ptr;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
@ -122,7 +120,7 @@ bool Foam::HashPtrTable<T, Key, Hash>::erase(iterator& iter)
|
||||
template<class T, class Key, class Hash>
|
||||
bool Foam::HashPtrTable<T, Key, Hash>::erase(const Key& key)
|
||||
{
|
||||
auto iter = this->find(key);
|
||||
iterator iter(this->find(key));
|
||||
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)
|
||||
{
|
||||
const Key& k = iter.key();
|
||||
const T* ptr = iter.val();
|
||||
|
||||
if (ptr)
|
||||
{
|
||||
this->set(iter.key(), new T(*ptr));
|
||||
this->set(k, new T(*ptr));
|
||||
}
|
||||
else
|
||||
{
|
||||
this->set(iter.key(), nullptr);
|
||||
this->set(k, nullptr);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -32,6 +32,7 @@ Description
|
||||
|
||||
SourceFiles
|
||||
HashPtrTable.C
|
||||
HashPtrTableI.H
|
||||
HashPtrTableIO.C
|
||||
|
||||
\*---------------------------------------------------------------------------*/
|
||||
@ -46,10 +47,7 @@ SourceFiles
|
||||
namespace Foam
|
||||
{
|
||||
|
||||
// Forward declarations
|
||||
|
||||
class Istream;
|
||||
class Ostream;
|
||||
// Forward Declarations
|
||||
|
||||
template<class T> class autoPtr;
|
||||
template<class T, class Key, class Hash> class HashPtrTable;
|
||||
@ -92,7 +90,7 @@ public:
|
||||
|
||||
// Constructors
|
||||
|
||||
//- Construct null with default table capacity
|
||||
//- Default construct with default table capacity
|
||||
inline HashPtrTable();
|
||||
|
||||
//- Construct given initial table capacity
|
||||
@ -108,11 +106,11 @@ public:
|
||||
//- Construct from dictionary with default dictionary constructor class
|
||||
explicit HashPtrTable(const dictionary& dict);
|
||||
|
||||
//- Copy construct
|
||||
HashPtrTable(const this_type& ht);
|
||||
//- Copy construct, making a copy of each element
|
||||
HashPtrTable(const this_type& rhs);
|
||||
|
||||
//- Move construct
|
||||
HashPtrTable(this_type&& ht);
|
||||
HashPtrTable(this_type&& rhs);
|
||||
|
||||
|
||||
//- Destructor
|
||||
@ -172,7 +170,7 @@ public:
|
||||
);
|
||||
|
||||
|
||||
// Housekeeping
|
||||
// Override HashTable methods
|
||||
|
||||
//- No insert() with raw pointers (potential memory leaks).
|
||||
//- Use insert() with autoPtr or set()
|
||||
@ -181,21 +179,21 @@ public:
|
||||
//- Insert a new entry, not overwriting existing entries.
|
||||
//
|
||||
// \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.
|
||||
//
|
||||
// \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.
|
||||
inline bool set(const Key& key, T* ptr);
|
||||
|
||||
//- 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.
|
||||
inline bool set(const Key& key, autoPtr<T>&& aptr);
|
||||
inline bool set(const Key& key, autoPtr<T>&& ptr);
|
||||
};
|
||||
|
||||
|
||||
|
||||
@ -5,7 +5,7 @@
|
||||
\\ / A nd | www.openfoam.com
|
||||
\\/ M anipulation |
|
||||
-------------------------------------------------------------------------------
|
||||
Copyright (C) 2018 OpenCFD Ltd.
|
||||
Copyright (C) 2018-2020 OpenCFD Ltd.
|
||||
-------------------------------------------------------------------------------
|
||||
License
|
||||
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
|
||||
(
|
||||
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;
|
||||
}
|
||||
|
||||
@ -66,12 +66,12 @@ template<class T, class Key, class Hash>
|
||||
inline bool Foam::HashPtrTable<T, Key, Hash>::insert
|
||||
(
|
||||
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;
|
||||
}
|
||||
|
||||
@ -94,10 +94,10 @@ template<class T, class Key, class Hash>
|
||||
inline bool Foam::HashPtrTable<T, Key, Hash>::set
|
||||
(
|
||||
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
|
||||
(
|
||||
const Key& key,
|
||||
autoPtr<T>&& aptr
|
||||
autoPtr<T>&& ptr
|
||||
)
|
||||
{
|
||||
return this->set(key, aptr.release());
|
||||
return this->set(key, ptr.release());
|
||||
}
|
||||
|
||||
|
||||
|
||||
@ -136,12 +136,13 @@ inline Foam::label Foam::HashSet<Key, Hash>::insert
|
||||
)
|
||||
{
|
||||
label changed = 0;
|
||||
for (; first != last; ++first)
|
||||
while (first != last)
|
||||
{
|
||||
if (insert(*first))
|
||||
{
|
||||
++changed;
|
||||
}
|
||||
++first;
|
||||
}
|
||||
return changed;
|
||||
}
|
||||
@ -374,9 +375,9 @@ Foam::HashSet<Key, Hash>::operator-=(const HashSet<Key, Hash>& rhs)
|
||||
// * * * * * * * * * * * * * * * IOstream Operators * * * * * * * * * * * * //
|
||||
|
||||
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>
|
||||
Foam::HashSet<Key, Hash> Foam::operator|
|
||||
(
|
||||
const HashSet<Key, Hash>& hash1,
|
||||
const HashSet<Key, Hash>& hash2
|
||||
const HashSet<Key, Hash>& a,
|
||||
const HashSet<Key, Hash>& b
|
||||
)
|
||||
{
|
||||
HashSet<Key, Hash> out(hash1);
|
||||
out |= hash2;
|
||||
return out;
|
||||
HashSet<Key, Hash> result(a);
|
||||
result |= b;
|
||||
return result;
|
||||
}
|
||||
|
||||
|
||||
template<class Key, class Hash>
|
||||
Foam::HashSet<Key, Hash> Foam::operator&
|
||||
(
|
||||
const HashSet<Key, Hash>& hash1,
|
||||
const HashSet<Key, Hash>& hash2
|
||||
const HashSet<Key, Hash>& a,
|
||||
const HashSet<Key, Hash>& b
|
||||
)
|
||||
{
|
||||
HashSet<Key, Hash> out(hash1);
|
||||
out &= hash2;
|
||||
return out;
|
||||
HashSet<Key, Hash> result(a.capacity());
|
||||
|
||||
for (const Key& k : a)
|
||||
{
|
||||
if (b.found(k))
|
||||
{
|
||||
result.insert(k);
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
|
||||
template<class Key, class Hash>
|
||||
Foam::HashSet<Key, Hash> Foam::operator^
|
||||
(
|
||||
const HashSet<Key, Hash>& hash1,
|
||||
const HashSet<Key, Hash>& hash2
|
||||
const HashSet<Key, Hash>& a,
|
||||
const HashSet<Key, Hash>& b
|
||||
)
|
||||
{
|
||||
HashSet<Key, Hash> out(hash1);
|
||||
out ^= hash2;
|
||||
return out;
|
||||
HashSet<Key, Hash> result(a);
|
||||
result ^= b;
|
||||
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;
|
||||
}
|
||||
|
||||
|
||||
|
||||
@ -313,20 +313,19 @@ public:
|
||||
// Member Operators
|
||||
|
||||
//- 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;
|
||||
|
||||
//- Return true if the entry exists, same as found().
|
||||
inline bool operator[](const Key& key) const noexcept;
|
||||
|
||||
using parent_type::operator=;
|
||||
|
||||
//- Copy assignment
|
||||
//- Copy assign
|
||||
void operator=(const this_type& rhs)
|
||||
{
|
||||
parent_type::operator=(rhs);
|
||||
}
|
||||
|
||||
//- Move assignment
|
||||
//- Move assign
|
||||
void operator=(this_type&& rhs)
|
||||
{
|
||||
parent_type::operator=(std::move(rhs));
|
||||
@ -429,33 +428,43 @@ MinMax<label> minMax(const labelHashSet& set);
|
||||
|
||||
//- Write the list of HashSet keys
|
||||
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>
|
||||
HashSet<Key, Hash> operator|
|
||||
(
|
||||
const HashSet<Key, Hash>& hash1,
|
||||
const HashSet<Key, Hash>& hash2
|
||||
const HashSet<Key, Hash>& a,
|
||||
const HashSet<Key, Hash>& b
|
||||
);
|
||||
|
||||
|
||||
//- Create a HashSet that only contains entries found in both HashSets
|
||||
//- Subset (AND) intersection of two HashSet
|
||||
// See HashSet::operator&= for more details.
|
||||
template<class Key, class Hash>
|
||||
HashSet<Key, Hash> operator&
|
||||
(
|
||||
const HashSet<Key, Hash>& hash1,
|
||||
const HashSet<Key, Hash>& hash2
|
||||
const HashSet<Key, Hash>& a,
|
||||
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>
|
||||
HashSet<Key, Hash> operator^
|
||||
(
|
||||
const HashSet<Key, Hash>& hash1,
|
||||
const HashSet<Key, Hash>& hash2
|
||||
const HashSet<Key, Hash>& a,
|
||||
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
|
||||
);
|
||||
|
||||
|
||||
|
||||
@ -405,7 +405,7 @@ bool Foam::HashTable<T, Key, Hash>::erase(const iterator& iter)
|
||||
template<class T, class Key, class Hash>
|
||||
bool Foam::HashTable<T, Key, Hash>::erase(const Key& key)
|
||||
{
|
||||
auto iter = find(key);
|
||||
iterator iter(find(key));
|
||||
return erase(iter);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user