diff --git a/src/OpenFOAM/containers/HashTables/HashPtrTable/HashPtrTable.C b/src/OpenFOAM/containers/HashTables/HashPtrTable/HashPtrTable.C index c8a8fffed5e02b825c5b6fe90f68472e62a943b2..89351b375adcbfc2fcddd4620ecc8462cfb56c52 100644 --- a/src/OpenFOAM/containers/HashTables/HashPtrTable/HashPtrTable.C +++ b/src/OpenFOAM/containers/HashTables/HashPtrTable/HashPtrTable.C @@ -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); } } } diff --git a/src/OpenFOAM/containers/HashTables/HashPtrTable/HashPtrTable.H b/src/OpenFOAM/containers/HashTables/HashPtrTable/HashPtrTable.H index f79898c5f7dddd33899f8f68699a1a7fa218ee52..49ed14999112f89aee241ce8f4b08adf6f1e616c 100644 --- a/src/OpenFOAM/containers/HashTables/HashPtrTable/HashPtrTable.H +++ b/src/OpenFOAM/containers/HashTables/HashPtrTable/HashPtrTable.H @@ -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); }; diff --git a/src/OpenFOAM/containers/HashTables/HashPtrTable/HashPtrTableI.H b/src/OpenFOAM/containers/HashTables/HashPtrTable/HashPtrTableI.H index 5c76321ab6354e74bb2eea845176cb4dbd60162b..485f618b8257c061024d1242ecb0a136db640b70 100644 --- a/src/OpenFOAM/containers/HashTables/HashPtrTable/HashPtrTableI.H +++ b/src/OpenFOAM/containers/HashTables/HashPtrTable/HashPtrTableI.H @@ -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()); } diff --git a/src/OpenFOAM/containers/HashTables/HashSet/HashSet.C b/src/OpenFOAM/containers/HashTables/HashSet/HashSet.C index 227c109a07e488372ebc9644db01a8ddd5ac7114..52cc1f6c589f263fbbcf703e4dfcf42381f9edcf 100644 --- a/src/OpenFOAM/containers/HashTables/HashSet/HashSet.C +++ b/src/OpenFOAM/containers/HashTables/HashSet/HashSet.C @@ -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; } diff --git a/src/OpenFOAM/containers/HashTables/HashSet/HashSet.H b/src/OpenFOAM/containers/HashTables/HashSet/HashSet.H index 12af96c818b67a0c104486d75a3de2e84a6eb6f8..2b0f31e341be4332b49e54c67346dea641b9e03c 100644 --- a/src/OpenFOAM/containers/HashTables/HashSet/HashSet.H +++ b/src/OpenFOAM/containers/HashTables/HashSet/HashSet.H @@ -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 ); diff --git a/src/OpenFOAM/containers/HashTables/HashTable/HashTable.C b/src/OpenFOAM/containers/HashTables/HashTable/HashTable.C index 694a8e94429424546979f70e14cb4fc8c9a51fd5..126353040b3cce9d6c3367c14831008f01b7a809 100644 --- a/src/OpenFOAM/containers/HashTables/HashTable/HashTable.C +++ b/src/OpenFOAM/containers/HashTables/HashTable/HashTable.C @@ -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); }