diff --git a/applications/test/sliceRange/Test-sliceRange.C b/applications/test/sliceRange/Test-sliceRange.C index d66af412f0..39bc97ce6d 100644 --- a/applications/test/sliceRange/Test-sliceRange.C +++ b/applications/test/sliceRange/Test-sliceRange.C @@ -5,7 +5,7 @@ \\ / A nd | www.openfoam.com \\/ M anipulation | ------------------------------------------------------------------------------- - Copyright (C) 2019 OpenCFD Ltd. + Copyright (C) 2019-2020 OpenCFD Ltd. ------------------------------------------------------------------------------- License This file is part of OpenFOAM. @@ -31,8 +31,9 @@ Description #include "labelList.H" #include "FixedList.H" #include "sliceRange.H" -#include "SliceList.H" #include "IndirectList.H" +#include "IndirectSubList.H" +#include "SliceList.H" #include "Random.H" using namespace Foam; @@ -146,6 +147,23 @@ int main(int argc, char *argv[]) Info<< nl << "Random list: " << flatOutput(list1) << nl; + { + IndirectSubList sublist1(list1, labelRange(0, 10)); + + Info<< nl << "SubList: " << sublist1.addressing() << " = " + << flatOutput(sublist1) << nl; + + // Adjust addressing + sublist1.addressing().reset(5, 8); + + // This should resolve as a no-op + sublist1 = sublist1; + + Info<< "SubList: " << sublist1.addressing() << " = " + << flatOutput(sublist1) << nl; + } + + SliceList slice1(list1, sliceRange(0, 15, 3)); Info<< nl << "slicing with: " << slice1.addressing() << nl; diff --git a/src/OpenFOAM/containers/IndirectLists/BiIndirectList/BiIndirectList.H b/src/OpenFOAM/containers/IndirectLists/BiIndirectList/BiIndirectList.H index 90105e31dd..3d3036bb76 100644 --- a/src/OpenFOAM/containers/IndirectLists/BiIndirectList/BiIndirectList.H +++ b/src/OpenFOAM/containers/IndirectLists/BiIndirectList/BiIndirectList.H @@ -52,7 +52,7 @@ namespace Foam template class BiIndirectList { - // Private data + // Private Data UList& posList_; UList& negList_; diff --git a/src/OpenFOAM/containers/IndirectLists/IndirectList/IndirectList.H b/src/OpenFOAM/containers/IndirectLists/IndirectList/IndirectList.H index 6cfd75fbcf..f79a9a6c27 100644 --- a/src/OpenFOAM/containers/IndirectLists/IndirectList/IndirectList.H +++ b/src/OpenFOAM/containers/IndirectLists/IndirectList/IndirectList.H @@ -72,6 +72,9 @@ public: //- Copy construct addressing, shallow copy values list reference inline IndirectList(const IndirectList& list); + //- Move construct addressing, shallow copy values list reference + inline IndirectList(IndirectList&& list); + //- Copy construct addressing, shallow copy values list reference inline explicit IndirectList(const UIndirectList& list); diff --git a/src/OpenFOAM/containers/IndirectLists/IndirectList/IndirectListI.H b/src/OpenFOAM/containers/IndirectLists/IndirectList/IndirectListI.H index 850bf8db53..13e8dcd7c5 100644 --- a/src/OpenFOAM/containers/IndirectLists/IndirectList/IndirectListI.H +++ b/src/OpenFOAM/containers/IndirectLists/IndirectList/IndirectListI.H @@ -6,7 +6,7 @@ \\/ M anipulation | ------------------------------------------------------------------------------- Copyright (C) 2011-2016 OpenFOAM Foundation - Copyright (C) 2019 OpenCFD Ltd. + Copyright (C) 2019-2020 OpenCFD Ltd. ------------------------------------------------------------------------------- License This file is part of OpenFOAM. @@ -73,6 +73,19 @@ inline Foam::IndirectList::IndirectList(const IndirectList& list) {} +template +inline Foam::IndirectList::IndirectList(IndirectList&& list) +: + // Move addressing + IndirectListAddressing(std::move(list.addressing())), + UIndirectList + ( + list.values(), + IndirectListAddressing::addressing() + ) +{} + + template inline Foam::IndirectList::IndirectList(const UIndirectList& list) : diff --git a/src/OpenFOAM/containers/IndirectLists/IndirectListBase/IndirectListAddressing.H b/src/OpenFOAM/containers/IndirectLists/IndirectListBase/IndirectListAddressing.H index 7b390944a2..913b3fdfad 100644 --- a/src/OpenFOAM/containers/IndirectLists/IndirectListBase/IndirectListAddressing.H +++ b/src/OpenFOAM/containers/IndirectLists/IndirectListBase/IndirectListAddressing.H @@ -5,8 +5,7 @@ \\ / A nd | www.openfoam.com \\/ 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. @@ -28,12 +27,11 @@ Class Foam::IndirectListAddressing Description - A class for storing list addressing (labels, slices etc) which are - normally to used by IndirectList. A private inheritance is often used - by any inheriting classes. + A class for storing list addressing (labels, slices etc), which are + normally to used by IndirectList. + Private inheritance is often used by any inheriting classes. SourceFiles - IndirectListAddressing.H \*---------------------------------------------------------------------------*/ diff --git a/src/OpenFOAM/containers/IndirectLists/IndirectListBase/IndirectListBase.C b/src/OpenFOAM/containers/IndirectLists/IndirectListBase/IndirectListBase.C index 6138b8ead6..94afdececc 100644 --- a/src/OpenFOAM/containers/IndirectLists/IndirectListBase/IndirectListBase.C +++ b/src/OpenFOAM/containers/IndirectLists/IndirectListBase/IndirectListBase.C @@ -5,7 +5,7 @@ \\ / A nd | www.openfoam.com \\/ M anipulation | ------------------------------------------------------------------------------- - Copyright (C) 2017-2019 OpenCFD Ltd. + Copyright (C) 2017-2020 OpenCFD Ltd. ------------------------------------------------------------------------------- License This file is part of OpenFOAM. @@ -31,21 +31,23 @@ template Foam::label Foam::IndirectListBase::find ( const T& val, - const label start + label pos ) const { const label len = addr_.size(); - if (start >= 0 && len) + if (pos >= 0 && len) { List_CONST_ACCESS(T, values_, vals); - for (label i = start; i < len; ++i) + while (pos < len) { - if (vals[addr_[i]] == val) + if (vals[addr_[pos]] == val) { - return i; + return pos; } + + ++pos; } } @@ -57,20 +59,26 @@ template Foam::label Foam::IndirectListBase::rfind ( const T& val, - const label pos + label pos ) const { + // pos == -1 has same meaning as std::string::npos - search from end + + if (pos < 0 || pos >= addr_.size()) + { + pos = addr_.size()-1; + } + List_CONST_ACCESS(T, values_, vals); - const label len1 = (addr_.size()-1); - - // pos == -1 has same meaning as std::string::npos - search from end - for (label i = ((pos >= 0 && pos < len1) ? pos : len1); i >= 0; --i) + while (pos >= 0) { - if (vals[addr_[i]] == val) + if (vals[addr_[pos]] == val) { - return i; + return pos; } + + --pos; } return -1; diff --git a/src/OpenFOAM/containers/IndirectLists/IndirectListBase/IndirectListBase.H b/src/OpenFOAM/containers/IndirectLists/IndirectListBase/IndirectListBase.H index e2b3b1de5b..1cf42b1f67 100644 --- a/src/OpenFOAM/containers/IndirectLists/IndirectListBase/IndirectListBase.H +++ b/src/OpenFOAM/containers/IndirectLists/IndirectListBase/IndirectListBase.H @@ -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. @@ -33,7 +33,6 @@ Description is held outside of the class. SourceFiles - IndirectListBase.H IndirectListBase.C IndirectListBaseI.H IndirectListBaseIO.C @@ -70,7 +69,7 @@ protected: // Protected Member Functions - //- Copy values The number of elements in the list + //- Deep copy values from the list template inline void copyList(const ListType& rhs); @@ -112,7 +111,7 @@ public: // Constructors - //- No null construct + //- No default construct IndirectListBase() = delete; //- Store references to the values list and the addressing array @@ -184,19 +183,21 @@ public: // Search //- Find index of the first occurrence of the value. - // When start is specified, any occurences before start are ignored. + // Any occurences before the start pos are ignored. // Linear search. // \return -1 if not found. - label find(const T& val, const label start=0) const; + label find(const T& val, label pos = 0) const; //- Find index of the last occurrence of the value. - // When pos is specified, any occurrences after pos are ignored. + // Any occurrences after the end pos are ignored. // Linear search. // \return -1 if not found. - label rfind(const T& val, const label pos=-1) const; + label rfind(const T& val, label pos = -1) const; - //- True if the value if found in the list. Linear search. - inline bool found(const T& val, const label start=0) const; + //- True if the value if found in the list. + // Any occurences before the start pos are ignored. + // Linear search. + inline bool found(const T& val, label pos=0) const; // Member Operators diff --git a/src/OpenFOAM/containers/IndirectLists/IndirectListBase/IndirectListBaseI.H b/src/OpenFOAM/containers/IndirectLists/IndirectListBase/IndirectListBaseI.H index b14de5fadc..db117469e7 100644 --- a/src/OpenFOAM/containers/IndirectLists/IndirectListBase/IndirectListBaseI.H +++ b/src/OpenFOAM/containers/IndirectLists/IndirectListBase/IndirectListBaseI.H @@ -5,8 +5,7 @@ \\ / A nd | www.openfoam.com \\/ M anipulation | ------------------------------------------------------------------------------- - Copyright (C) 2011-2015 OpenFOAM Foundation - Copyright (C) 2018-2019 OpenCFD Ltd. + Copyright (C) 2018-2020 OpenCFD Ltd. ------------------------------------------------------------------------------- License This file is part of OpenFOAM. @@ -32,6 +31,15 @@ template template inline void Foam::IndirectListBase::copyList(const ListType& rhs) { + if + ( + this + == reinterpret_cast*>(const_cast(&rhs)) + ) + { + return; // Self-assignment is a no-op + } + const label len = addr_.size(); if (len != rhs.size()) @@ -94,10 +102,10 @@ template inline bool Foam::IndirectListBase::found ( const T& val, - const label pos + label pos ) const { - return this->find(val, pos) != -1; + return (this->find(val, pos) >= 0); } diff --git a/src/OpenFOAM/containers/IndirectLists/IndirectListsFwd.H b/src/OpenFOAM/containers/IndirectLists/IndirectListsFwd.H new file mode 100644 index 0000000000..5d09dcc3b6 --- /dev/null +++ b/src/OpenFOAM/containers/IndirectLists/IndirectListsFwd.H @@ -0,0 +1,60 @@ +/*---------------------------------------------------------------------------*\ + ========= | + \\ / F ield | OpenFOAM: The Open Source CFD Toolbox + \\ / O peration | + \\ / A nd | www.openfoam.com + \\/ M anipulation | +------------------------------------------------------------------------------- + Copyright (C) 2020 OpenCFD Ltd. +------------------------------------------------------------------------------- +License + This file is part of OpenFOAM. + + OpenFOAM is free software: you can redistribute it and/or modify it + under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + OpenFOAM is distributed in the hope that it will be useful, but WITHOUT + ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + for more details. + + You should have received a copy of the GNU General Public License + along with OpenFOAM. If not, see . + +InClass + Foam + +Description + Forward declarations for common indirect list types. + +\*---------------------------------------------------------------------------*/ + +#ifndef IndirectListsFwd_H +#define IndirectListsFwd_H + +// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // + +namespace Foam +{ + +// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // + +template class IndirectListBase; +template class UIndirectList; +template class IndirectList; +template class IndirectSubList; +template class SliceList; + +//Not common enough: template class SortList; + +// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // + +} // End namespace Foam + +// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // + +#endif + +// ************************************************************************* // diff --git a/src/OpenFOAM/containers/IndirectLists/IndirectSubList/IndirectSubList.H b/src/OpenFOAM/containers/IndirectLists/IndirectSubList/IndirectSubList.H new file mode 100644 index 0000000000..0801a869c4 --- /dev/null +++ b/src/OpenFOAM/containers/IndirectLists/IndirectSubList/IndirectSubList.H @@ -0,0 +1,119 @@ +/*---------------------------------------------------------------------------*\ + ========= | + \\ / F ield | OpenFOAM: The Open Source CFD Toolbox + \\ / O peration | + \\ / A nd | www.openfoam.com + \\/ M anipulation | +------------------------------------------------------------------------------- + Copyright (C) 2020 OpenCFD Ltd. +------------------------------------------------------------------------------- +License + This file is part of OpenFOAM. + + OpenFOAM is free software: you can redistribute it and/or modify it + under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + OpenFOAM is distributed in the hope that it will be useful, but WITHOUT + ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + for more details. + + You should have received a copy of the GNU General Public License + along with OpenFOAM. If not, see . + +Class + Foam::IndirectSubList + +Description + Indirect access to a sub-section of a list. + + In many cases, using a Foam::SubList provides a simpler and more + efficient means of accessing a sub-list. + There are, however, some advantages of a IndirectSubList: + - allows adjustment of its addressing range after construct + - can recover the original, underlying list at any time + +SeeAlso + Foam::SubList + +SourceFiles + +\*---------------------------------------------------------------------------*/ + +#ifndef IndirectSubList_H +#define IndirectSubList_H + +#include "IndirectListAddressing.H" +#include "IndirectListBase.H" +#include "labelRange.H" + +// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // + +namespace Foam +{ + +/*---------------------------------------------------------------------------*\ + Class IndirectSubList Declaration +\*---------------------------------------------------------------------------*/ + +template +class IndirectSubList +: + private IndirectListAddressing, + public IndirectListBase +{ +public: + + // Constructors + + //- Construct from UList, the entire size + explicit IndirectSubList(const UList& values) + : + IndirectListAddressing(labelRange(0, values.size())), + IndirectListBase + ( + values, + IndirectListAddressing::addressing() + ) + {} + + //- Construct from values list and range + IndirectSubList + ( + const UList& values, + const labelRange& addr + ) + : + IndirectListAddressing(addr), + IndirectListBase + ( + values, + IndirectListAddressing::addressing() + ) + {} + + + // Member Functions + + //- The list addressing + using IndirectListAddressing::addressing; + + + // Member Operators + + //- Use standard assignment operations + using IndirectListBase::operator=; +}; + + +// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // + +} // End namespace Foam + +// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // + +#endif + +// ************************************************************************* // diff --git a/src/OpenFOAM/containers/IndirectLists/SliceList/SliceList.H b/src/OpenFOAM/containers/IndirectLists/SliceList/SliceList.H index 6dae8c2f48..55baf6f52f 100644 --- a/src/OpenFOAM/containers/IndirectLists/SliceList/SliceList.H +++ b/src/OpenFOAM/containers/IndirectLists/SliceList/SliceList.H @@ -30,7 +30,6 @@ Description A List with indirect slice addressing. SourceFiles - SliceList.H \*---------------------------------------------------------------------------*/ @@ -82,7 +81,6 @@ public: //- Use standard assignment operations using IndirectListBase::operator=; - }; diff --git a/src/OpenFOAM/containers/IndirectLists/UIndirectList/UIndirectList.H b/src/OpenFOAM/containers/IndirectLists/UIndirectList/UIndirectList.H index 7812fbceb9..f252a7921d 100644 --- a/src/OpenFOAM/containers/IndirectLists/UIndirectList/UIndirectList.H +++ b/src/OpenFOAM/containers/IndirectLists/UIndirectList/UIndirectList.H @@ -37,7 +37,6 @@ Description variant etc. SourceFiles - UIndirectListI.H \*---------------------------------------------------------------------------*/ @@ -52,7 +51,7 @@ SourceFiles namespace Foam { -// Forward declarations +// Forward Declarations template class UIndirectList; // Common list types @@ -78,8 +77,7 @@ public: IndirectListBase(values, addr) {} - //- Copy construct from UIndirectList with - //- shallow copy of values and addressing arrays + //- Copy construct (shallow copy of values and addressing arrays) UIndirectList(const UIndirectList& list) : UIndirectList(list.values(), list.addressing())