From 180a284814137ce0e52d702535ac87f6e24620af Mon Sep 17 00:00:00 2001 From: Mark Olesen <Mark.Olesen@esi-group.com> Date: Fri, 26 Apr 2019 13:01:39 +0200 Subject: [PATCH] ENH: add '==' and '!=' operators for PackedList/bitSet - also available as equal() member function --- applications/test/bitSet1/Test-bitSet1.C | 3 +++ .../containers/Bits/PackedList/PackedList.C | 23 ++++++++++++++++ .../containers/Bits/PackedList/PackedList.H | 14 +++++++++- .../containers/Bits/PackedList/PackedListI.H | 26 ++++++++++++++++++- src/OpenFOAM/containers/Bits/bitSet/bitSet.H | 3 ++- 5 files changed, 66 insertions(+), 3 deletions(-) diff --git a/applications/test/bitSet1/Test-bitSet1.C b/applications/test/bitSet1/Test-bitSet1.C index 776cb656992..7077bcb3711 100644 --- a/applications/test/bitSet1/Test-bitSet1.C +++ b/applications/test/bitSet1/Test-bitSet1.C @@ -76,6 +76,9 @@ int main(int argc, char *argv[]) bitSet set2b(set2, labelRange(15, 30)); Info<<"bitSet slice(15,30) :"; report(set2b, true); + Info<< "set1 == set2: " << (set2 == set2b) << nl; + Info<< "set1 != set2: " << (set2 != set2b) << nl; + { FixedList<label, 4> locs({ -1, 3, 4, 12}); diff --git a/src/OpenFOAM/containers/Bits/PackedList/PackedList.C b/src/OpenFOAM/containers/Bits/PackedList/PackedList.C index f1ecf217388..05d0e211b99 100644 --- a/src/OpenFOAM/containers/Bits/PackedList/PackedList.C +++ b/src/OpenFOAM/containers/Bits/PackedList/PackedList.C @@ -145,6 +145,29 @@ bool Foam::PackedList<Width>::uniform() const } +template<unsigned Width> +bool Foam::PackedList<Width>::equal(const PackedList<Width>& other) const +{ + if (size() != other.size()) + { + return false; + } + + const label nblocks = num_blocks(size()); + const auto& rhs = other.blocks_; + + for (label blocki = 0; blocki < nblocks; ++blocki) + { + if (blocks_[blocki] != rhs[blocki]) + { + return false; + } + } + + return true; +} + + template<unsigned Width> Foam::labelList Foam::PackedList<Width>::values() const { diff --git a/src/OpenFOAM/containers/Bits/PackedList/PackedList.H b/src/OpenFOAM/containers/Bits/PackedList/PackedList.H index 6ebadbf1c71..278426c89c8 100644 --- a/src/OpenFOAM/containers/Bits/PackedList/PackedList.H +++ b/src/OpenFOAM/containers/Bits/PackedList/PackedList.H @@ -286,6 +286,9 @@ public: //- True if all entries have identical values, and list is non-empty bool uniform() const; + //- Test for equality of sizes and the bits set + bool equal(const PackedList<Width>& other) const; + // Access @@ -495,7 +498,7 @@ public: }; -// * * * * * * * * * * * * * * * IOstream Operators * * * * * * * * * * * * // +// * * * * * * * * * * * * * * * Global Operators * * * * * * * * * * * * * // //- Write List to Ostream, as per UList::writeList() with default length. // The default short-length is given by Detail::ListPolicy::short_length @@ -506,6 +509,15 @@ Ostream& operator<<(Ostream& os, const PackedList<Width>& list) } +//- Test for equality of sizes and the bits set +template<unsigned Width> +inline bool operator==(const PackedList<Width>& a, const PackedList<Width>& b); + +//- Test for inequality of sizes or the bits set +template<unsigned Width> +inline bool operator!=(const PackedList<Width>& a, const PackedList<Width>& b); + + // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // } // End namespace Foam diff --git a/src/OpenFOAM/containers/Bits/PackedList/PackedListI.H b/src/OpenFOAM/containers/Bits/PackedList/PackedListI.H index 5470a2a7afa..56e481c3d47 100644 --- a/src/OpenFOAM/containers/Bits/PackedList/PackedListI.H +++ b/src/OpenFOAM/containers/Bits/PackedList/PackedListI.H @@ -2,7 +2,7 @@ ========= | \\ / F ield | OpenFOAM: The Open Source CFD Toolbox \\ / O peration | - \\ / A nd | Copyright (C) 2017-2018 OpenCFD Ltd. + \\ / A nd | Copyright (C) 2017-2019 OpenCFD Ltd. \\/ M anipulation | ------------------------------------------------------------------------------- | Copyright (C) 2011-2016 OpenFOAM Foundation @@ -744,4 +744,28 @@ inline void Foam::PackedList<Width>::operator=(PackedList<Width>&& rhs) } +// * * * * * * * * * * * * * * * Global Operators * * * * * * * * * * * * * // + +template<unsigned Width> +inline bool Foam::operator== +( + const PackedList<Width>& a, + const PackedList<Width>& b +) +{ + return a.equal(b); +} + + +template<unsigned Width> +inline bool Foam::operator!= +( + const PackedList<Width>& a, + const PackedList<Width>& b +) +{ + return !a.equal(b); +} + + // ************************************************************************* // diff --git a/src/OpenFOAM/containers/Bits/bitSet/bitSet.H b/src/OpenFOAM/containers/Bits/bitSet/bitSet.H index 00d94305968..2bd37c0ba25 100644 --- a/src/OpenFOAM/containers/Bits/bitSet/bitSet.H +++ b/src/OpenFOAM/containers/Bits/bitSet/bitSet.H @@ -120,6 +120,7 @@ protected: // A and B can have different sizes. Sizing behaviour as per orEq. bitSet& xorEq(const bitSet& other, const bool strict=true); + public: // Forward declaration of access classes @@ -554,7 +555,7 @@ public: }; -// Global Operators +// * * * * * * * * * * * * * * * Global Operators * * * * * * * * * * * * * // //- Write bitset to Ostream, as per bitSet::writeList() with default length //- of 40 items. -- GitLab