diff --git a/applications/test/HashTable1/Test-HashTable1.C b/applications/test/HashTable1/Test-HashTable1.C index f0017d4e5a..b713db2c49 100644 --- a/applications/test/HashTable1/Test-HashTable1.C +++ b/applications/test/HashTable1/Test-HashTable1.C @@ -352,6 +352,16 @@ int main() HashTable table1b(std::move(table2)); Info<<"table1 = " << table1b << nl <<"table2 = " << table2 << nl; + table1b.set("more", 14.0); + table1b.set("less", 15.0); + table1b.set("other", 16.0); + + Info<<"Test a += b " << nl; + Info<<"a = " << flatOutput(table1b.sortedToc()) << nl + <<"b = " << flatOutput(table3.sortedToc()) << nl; + + Info<<"=> " << (table1b += table3) << nl; + Info<< "\nDone\n"; return 0; diff --git a/src/OpenFOAM/containers/HashTables/HashSet/HashSet.C b/src/OpenFOAM/containers/HashTables/HashSet/HashSet.C index 3070831f26..4148a52a6f 100644 --- a/src/OpenFOAM/containers/HashTables/HashSet/HashSet.C +++ b/src/OpenFOAM/containers/HashTables/HashSet/HashSet.C @@ -224,25 +224,31 @@ bool Foam::HashSet::operator!=(const HashSet& rhs) const template -void Foam::HashSet::operator|=(const HashSet& rhs) +Foam::HashSet& +Foam::HashSet::operator|=(const HashSet& rhs) { // Add rhs elements into lhs for (const_iterator iter = rhs.cbegin(); iter != rhs.cend(); ++iter) { this->insert(iter.key()); } + + return *this; } template -inline void Foam::HashSet::operator&=(const HashSet& rhs) +inline Foam::HashSet& +Foam::HashSet::operator&=(const HashSet& rhs) { this->parent_type::retain(rhs); + return *this; } template -void Foam::HashSet::operator^=(const HashSet& rhs) +Foam::HashSet& +Foam::HashSet::operator^=(const HashSet& rhs) { // Add missed rhs elements, remove duplicate elements for (const_iterator iter = rhs.cbegin(); iter != rhs.cend(); ++iter) @@ -256,13 +262,18 @@ void Foam::HashSet::operator^=(const HashSet& rhs) this->insert(iter.key()); } } + + return *this; } template -inline void Foam::HashSet::operator-=(const HashSet& rhs) +inline Foam::HashSet& +Foam::HashSet::operator-=(const HashSet& rhs) { this->parent_type::erase(rhs); + + return *this; } diff --git a/src/OpenFOAM/containers/HashTables/HashSet/HashSet.H b/src/OpenFOAM/containers/HashTables/HashSet/HashSet.H index 5c467a2562..5c0f542346 100644 --- a/src/OpenFOAM/containers/HashTables/HashSet/HashSet.H +++ b/src/OpenFOAM/containers/HashTables/HashSet/HashSet.H @@ -3,7 +3,7 @@ \\ / F ield | OpenFOAM: The Open Source CFD Toolbox \\ / O peration | \\ / A nd | Copyright (C) 2011-2016 OpenFOAM Foundation - \\/ M anipulation | Copyright (C) 2016-2017 OpenCFD Ltd. + \\/ M anipulation | Copyright (C) 2016-2018 OpenCFD Ltd. ------------------------------------------------------------------------------- License This file is part of OpenFOAM. @@ -69,7 +69,7 @@ Description namespace Foam { -// Forward declaration of friend functions and operators +// Forward declarations template class HashSet; template @@ -175,14 +175,14 @@ public: {} //- Construct from the keys of another HashTable, - // the type of values held is arbitrary. + //- the type of values held is arbitrary. template explicit HashSet(const HashTable& tbl); // Member Functions - // Edit + // Edit //- Insert a new entry, not overwriting existing entries. // \return True if the entry inserted, which means that it did @@ -329,7 +329,7 @@ public: } - // Comparison + // Comparison //- Sets are equal if all keys are equal, //- independent of order or underlying storage size. @@ -339,7 +339,7 @@ public: bool operator!=(const this_type& rhs) const; - // Assignment + // Assignment //- Assignment from a UList of keys void operator=(const UList& lst); @@ -352,25 +352,25 @@ public: void operator=(std::initializer_list lst); - // Logical operations + // Logical and set operations - //- Combine entries from HashSets - void operator|=(const this_type& rhs); + //- Add entries to this HashSet + this_type& operator|=(const this_type& rhs); //- Only retain entries found in both HashSets - inline void operator&=(const this_type& rhs); + inline this_type& operator&=(const this_type& rhs); //- Only retain unique entries (xor) - void operator^=(const this_type& rhs); + this_type& operator^=(const this_type& rhs); - //- Add entries listed in the given HashSet to this HashSet - inline void operator+=(const this_type& rhs) + //- Add entries to this HashSet + inline this_type& operator+=(const this_type& rhs) { - this->operator|=(rhs); + return this->operator|=(rhs); } - //- Remove entries listed in the given HashSet from this HashSet - inline void operator-=(const this_type& rhs); + //- Remove entries from this HashSet + inline this_type& operator-=(const this_type& rhs); // IOstream Operator diff --git a/src/OpenFOAM/containers/HashTables/HashTable/HashTable.C b/src/OpenFOAM/containers/HashTables/HashTable/HashTable.C index 68faaafb49..62471ecff7 100644 --- a/src/OpenFOAM/containers/HashTables/HashTable/HashTable.C +++ b/src/OpenFOAM/containers/HashTables/HashTable/HashTable.C @@ -3,7 +3,7 @@ \\ / F ield | OpenFOAM: The Open Source CFD Toolbox \\ / O peration | \\ / A nd | Copyright (C) 2011-2016 OpenFOAM Foundation - \\/ M anipulation | Copyright (C) 2017 OpenCFD Ltd. + \\/ M anipulation | Copyright (C) 2017-2018 OpenCFD Ltd. ------------------------------------------------------------------------------- License This file is part of OpenFOAM. @@ -880,6 +880,32 @@ bool Foam::HashTable::operator!= } +template +Foam::HashTable& Foam::HashTable::operator+= +( + const HashTable& rhs +) +{ + // Avoid no-ops: + if (rhs.size() || this != &rhs) + { + if (this->size()) + { + for (const_iterator iter = rhs.cbegin(); iter != rhs.cend(); ++iter) + { + insert(iter.key(), iter.object()); + } + } + else + { + (*this) = rhs; + } + } + + return *this; +} + + // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // // Iterators, Friend Operators diff --git a/src/OpenFOAM/containers/HashTables/HashTable/HashTable.H b/src/OpenFOAM/containers/HashTables/HashTable/HashTable.H index 85e19aed49..40e7b1d309 100644 --- a/src/OpenFOAM/containers/HashTables/HashTable/HashTable.H +++ b/src/OpenFOAM/containers/HashTables/HashTable/HashTable.H @@ -3,7 +3,7 @@ \\ / F ield | OpenFOAM: The Open Source CFD Toolbox \\ / O peration | \\ / A nd | Copyright (C) 2011-2016 OpenFOAM Foundation - \\/ M anipulation | Copyright (C) 2017 OpenCFD Ltd. + \\/ M anipulation | Copyright (C) 2017-2018 OpenCFD Ltd. ------------------------------------------------------------------------------- License This file is part of OpenFOAM. @@ -641,6 +641,9 @@ public: //- The opposite of the equality operation. bool operator!=(const HashTable& rhs) const; + //- Add entries into this HashTable + this_type& operator+=(const this_type& rhs); + protected: @@ -785,7 +788,7 @@ public: // Constructors //- Construct null (end iterator) - inline iterator() {} + inline iterator() = default; //- Copy construct from similar access type inline explicit iterator(const Iterator& iter) @@ -829,7 +832,7 @@ public: // Constructors //- Construct null (end iterator) - inline const_iterator() {} + inline const_iterator() = default; //- Copy construct from similar access type inline explicit const_iterator(const Iterator& iter) diff --git a/src/OpenFOAM/containers/HashTables/HashTableOps/HashTableOps.H b/src/OpenFOAM/containers/HashTables/HashTableOps/HashTableOps.H index 034742e59c..20631f776d 100644 --- a/src/OpenFOAM/containers/HashTables/HashTableOps/HashTableOps.H +++ b/src/OpenFOAM/containers/HashTables/HashTableOps/HashTableOps.H @@ -63,20 +63,7 @@ struct HashTablePlusEqOp void operator()(value_type& a, const value_type& b) const { - if (b.size()) - { - if (a.size()) - { - forAllConstIters(b, citer) - { - a.insert(citer.key(), citer.object()); - } - } - else - { - a = b; - } - } + a += b; } };