diff --git a/applications/test/List/Test-List.C b/applications/test/List/Test-List.C index 63ef615003..e676a859a4 100644 --- a/applications/test/List/Test-List.C +++ b/applications/test/List/Test-List.C @@ -6,7 +6,7 @@ \\/ M anipulation | ------------------------------------------------------------------------------- Copyright (C) 2011-2016 OpenFOAM Foundation - Copyright (C) 2017-2021 OpenCFD Ltd. + Copyright (C) 2017-2023 OpenCFD Ltd. ------------------------------------------------------------------------------- License This file is part of OpenFOAM. @@ -365,6 +365,12 @@ int main(int argc, char *argv[]) Info<<"assigned identity in range:" << subset << "=> " << flatOutput(longLabelList) << nl; + // Assign values in iterator range + std::iota(longLabelList.begin(15), longLabelList.begin(50), 115); + + Info<<"assigned values in iterator range " + << "=> " << flatOutput(longLabelList) << nl; + labelList someList(identity(24)); longLabelList.slice(subset) = @@ -410,8 +416,20 @@ int main(int argc, char *argv[]) longLabelList.slice({5,-5}) = 42; longLabelList.slice({21,100}) = 42; - //Good: does not compile - longLabelList.slice(labelRange(20,50)) = constLabelList; + #if 0 + // Compiles, but is runtime abort! + const bool oldThrowingError = FatalError.throwing(true); + try + { + longLabelList.slice(labelRange(20,50)) = constLabelList; + } + catch (const Foam::error& err) + { + Info<< "Caught FatalError " + << err << nl << endl; + } + FatalError.throwing(oldThrowingError); + #endif //Good: does not compile // longLabelList[labelRange(20,50)] = fixedLabelList; diff --git a/src/OpenFOAM/containers/Lists/List/UList.C b/src/OpenFOAM/containers/Lists/List/UList.C index b4b83b902c..4a16d82fd3 100644 --- a/src/OpenFOAM/containers/Lists/List/UList.C +++ b/src/OpenFOAM/containers/Lists/List/UList.C @@ -261,24 +261,28 @@ Foam::label Foam::UList::rfind(const T& val, label pos) const template bool Foam::UList::operator==(const UList& list) const { + /// OR: + /// return + /// ( + /// (this->size_ == list.size_) + // && std::equal(cbegin(), cend(), list.cbegin()) + /// ); + const label len = this->size_; if (len != list.size_) { return false; } - bool equal = true; - List_CONST_ACCESS(T, (*this), lhs); List_CONST_ACCESS(T, (list), rhs); for (label i = 0; i < len; ++i) { - equal = (lhs[i] == rhs[i]); - if (!equal) break; + if (!(lhs[i] == rhs[i])) return false; } - return equal; + return true; } @@ -292,11 +296,21 @@ bool Foam::UList::operator!=(const UList& list) const template bool Foam::UList::operator<(const UList& list) const { + /// OR: + /// return std::lexicographical_compare + /// ( + /// cbegin(), cend(), + /// list.cbegin(), list.cend() + /// ); + + const const_iterator last1 = cend(); + const const_iterator last2 = list.cend(); + for ( - const_iterator lhs = begin(), rhs = list.begin(); - lhs < end() && rhs < list.end(); - ++lhs, ++rhs + const_iterator lhs = cbegin(), rhs = list.cbegin(); + (lhs != last1) && (rhs != last2); + ++lhs, (void) ++rhs ) { if (*lhs < *rhs) diff --git a/src/OpenFOAM/containers/Lists/List/UList.H b/src/OpenFOAM/containers/Lists/List/UList.H index eecbb82904..ac241aef9a 100644 --- a/src/OpenFOAM/containers/Lists/List/UList.H +++ b/src/OpenFOAM/containers/Lists/List/UList.H @@ -6,7 +6,7 @@ \\/ M anipulation | ------------------------------------------------------------------------------- Copyright (C) 2011-2016 OpenFOAM Foundation - Copyright (C) 2017-2021 OpenCFD Ltd. + Copyright (C) 2017-2023 OpenCFD Ltd. ------------------------------------------------------------------------------- License This file is part of OpenFOAM. @@ -407,6 +407,10 @@ public: //- Return an iterator to end traversing the UList inline iterator end() noexcept; + //- Return iterator at offset i from begin, + //- clamped to [0,size] range + inline iterator begin(const label i) noexcept; + // Random access iterator (const) @@ -422,6 +426,14 @@ public: //- Return const_iterator to end traversing the constant UList inline const_iterator end() const noexcept; + //- Return const_iterator at offset i from begin, + //- clamped to [0,size] range + inline const_iterator cbegin(const label i) const noexcept; + + //- Return const_iterator at offset i from begin, + //- clamped to [0,size] range + inline const_iterator begin(const label i) const noexcept; + // Reverse iterators (non-const) diff --git a/src/OpenFOAM/containers/Lists/List/UListI.H b/src/OpenFOAM/containers/Lists/List/UListI.H index 053109c50e..6d9519fb8b 100644 --- a/src/OpenFOAM/containers/Lists/List/UListI.H +++ b/src/OpenFOAM/containers/Lists/List/UListI.H @@ -6,7 +6,7 @@ \\/ M anipulation | ------------------------------------------------------------------------------- Copyright (C) 2011-2016 OpenFOAM Foundation - Copyright (C) 2015-2021 OpenCFD Ltd. + Copyright (C) 2015-2023 OpenCFD Ltd. ------------------------------------------------------------------------------- License This file is part of OpenFOAM. @@ -345,6 +345,29 @@ Foam::UList::cbegin() const noexcept return v_; } + +template +inline typename Foam::UList::iterator +Foam::UList::begin(const label i) noexcept +{ + return (v_ + (i < 0 ? 0 : size_ < i ? size_ : i)); +} + +template +inline typename Foam::UList::const_iterator +Foam::UList::begin(const label i) const noexcept +{ + return (v_ + (i < 0 ? 0 : size_ < i ? size_ : i)); +} + +template +inline typename Foam::UList::const_iterator +Foam::UList::cbegin(const label i) const noexcept +{ + return (v_ + (i < 0 ? 0 : size_ < i ? size_ : i)); +} + + template inline typename Foam::UList::iterator Foam::UList::end() noexcept diff --git a/src/OpenFOAM/containers/Lists/List/stdVectorIO.C b/src/OpenFOAM/containers/Lists/List/stdVectorIO.C index ba8db32641..a11964a66a 100644 --- a/src/OpenFOAM/containers/Lists/List/stdVectorIO.C +++ b/src/OpenFOAM/containers/Lists/List/stdVectorIO.C @@ -5,7 +5,7 @@ \\ / A nd | www.openfoam.com \\/ M anipulation | ------------------------------------------------------------------------------- - Copyright (C) 2019 OpenCFD Ltd. + Copyright (C) 2019-2023 OpenCFD Ltd. ------------------------------------------------------------------------------- License This file is part of OpenFOAM. @@ -46,7 +46,7 @@ Foam::Ostream& Foam::operator<<(Ostream& os, const std::vector& list) { os << *iter; - for (++iter; iter != last; ++iter) + while (++iter != last) { os << token::SPACE << *iter; }