ENH: UList iterators at offset from begin

- simplifies addressing within sub-ranges.
  Clamps the access range directly
This commit is contained in:
Mark Olesen
2023-01-13 12:44:16 +01:00
parent 8a70c898ae
commit 3888bfa17f
5 changed files with 82 additions and 15 deletions

View File

@ -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;

View File

@ -261,24 +261,28 @@ Foam::label Foam::UList<T>::rfind(const T& val, label pos) const
template<class T>
bool Foam::UList<T>::operator==(const UList<T>& 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<T>::operator!=(const UList<T>& list) const
template<class T>
bool Foam::UList<T>::operator<(const UList<T>& 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)

View File

@ -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)

View File

@ -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<T>::cbegin() const noexcept
return v_;
}
template<class T>
inline typename Foam::UList<T>::iterator
Foam::UList<T>::begin(const label i) noexcept
{
return (v_ + (i < 0 ? 0 : size_ < i ? size_ : i));
}
template<class T>
inline typename Foam::UList<T>::const_iterator
Foam::UList<T>::begin(const label i) const noexcept
{
return (v_ + (i < 0 ? 0 : size_ < i ? size_ : i));
}
template<class T>
inline typename Foam::UList<T>::const_iterator
Foam::UList<T>::cbegin(const label i) const noexcept
{
return (v_ + (i < 0 ? 0 : size_ < i ? size_ : i));
}
template<class T>
inline typename Foam::UList<T>::iterator
Foam::UList<T>::end() noexcept

View File

@ -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<T>& list)
{
os << *iter;
for (++iter; iter != last; ++iter)
while (++iter != last)
{
os << token::SPACE << *iter;
}