mirror of
https://develop.openfoam.com/Development/openfoam.git
synced 2025-11-28 03:28:01 +00:00
ENH: added labelRange += and -= operators
- removed unused decrement() and increment() methods, which provided identical functionality as the ++, +=, --, -= operators.
This commit is contained in:
committed by
Andrew Heather
parent
a9747b90b1
commit
06e709c26f
@ -2,7 +2,7 @@
|
||||
========= |
|
||||
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
|
||||
\\ / O peration |
|
||||
\\ / A nd | Copyright (C) 2017 OpenCFD Ltd.
|
||||
\\ / A nd | Copyright (C) 2017-2019 OpenCFD Ltd.
|
||||
\\/ M anipulation |
|
||||
-------------------------------------------------------------------------------
|
||||
| Copyright (C) 2011 OpenFOAM Foundation
|
||||
@ -143,6 +143,39 @@ int main(int argc, char *argv[])
|
||||
Info<< nl;
|
||||
}
|
||||
|
||||
|
||||
{
|
||||
range.reset(5, 5);
|
||||
Info<< nl << "Tests on " << range << nl;
|
||||
|
||||
Info<< "first " << range.first() << nl
|
||||
<< "last " << range.last() << nl
|
||||
<< "min " << range.min() << nl
|
||||
<< "max " << range.max() << nl
|
||||
<< "after " << range.after() << nl
|
||||
<< "*begin " << *range.begin() << nl
|
||||
<< "*end " << *range.end() << nl;
|
||||
|
||||
range += 3;
|
||||
Info<< "increase size " << range << nl;
|
||||
|
||||
range -= 3; // Probably not a great idea
|
||||
Info<< "decrese size " << range << nl;
|
||||
|
||||
auto iter = range.begin();
|
||||
|
||||
Info<< "iter: " << *iter << nl;
|
||||
++iter;
|
||||
|
||||
Info<< "iter: " << *iter << nl;
|
||||
|
||||
iter += 3;
|
||||
Info<< "iter: " << *iter << nl;
|
||||
}
|
||||
|
||||
|
||||
Info<< "\nEnd\n" << endl;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
@ -198,18 +198,20 @@ Foam::labelRange Foam::labelRange::subset0(const label size) const
|
||||
}
|
||||
|
||||
|
||||
// * * * * * * * * * * * * * * * Friend Operators * * * * * * * * * * * * * //
|
||||
// * * * * * * * * * * * * * * * IOstream Operators * * * * * * * * * * * * //
|
||||
|
||||
Foam::Istream& Foam::operator>>(Istream& is, labelRange& range)
|
||||
{
|
||||
label start, size;
|
||||
|
||||
is.readBegin("labelRange");
|
||||
is >> range.start_ >> range.size_;
|
||||
is >> start >> size;
|
||||
is.readEnd("labelRange");
|
||||
|
||||
if (range.size_ < 0)
|
||||
{
|
||||
range.size_ = 0; // No negative sizes
|
||||
}
|
||||
if (size < 0) size = 0; // No negative sizes
|
||||
|
||||
range.setStart(start);
|
||||
range.setSize(size);
|
||||
|
||||
is.check(FUNCTION_NAME);
|
||||
return is;
|
||||
@ -218,7 +220,6 @@ Foam::Istream& Foam::operator>>(Istream& is, labelRange& range)
|
||||
|
||||
Foam::Ostream& Foam::operator<<(Ostream& os, const labelRange& range)
|
||||
{
|
||||
// Only write as ASCII for now
|
||||
os << token::BEGIN_LIST
|
||||
<< range.start() << token::SPACE << range.size()
|
||||
<< token::END_LIST;
|
||||
|
||||
@ -31,6 +31,7 @@ Description
|
||||
|
||||
SourceFiles
|
||||
labelRange.C
|
||||
labelRangeI.H
|
||||
|
||||
\*---------------------------------------------------------------------------*/
|
||||
#ifndef labelRange_H
|
||||
@ -45,15 +46,11 @@ namespace Foam
|
||||
{
|
||||
|
||||
// Forward Declarations
|
||||
class labelRange;
|
||||
class Istream;
|
||||
class Ostream;
|
||||
template<class T> class List;
|
||||
template<class T> class MinMax;
|
||||
|
||||
Istream& operator>>(Istream& is, labelRange& range);
|
||||
Ostream& operator<<(Ostream& os, const labelRange& range);
|
||||
|
||||
/*---------------------------------------------------------------------------*\
|
||||
Class labelRange Declaration
|
||||
\*---------------------------------------------------------------------------*/
|
||||
@ -122,7 +119,7 @@ public:
|
||||
|
||||
//- An identity range corresponding to (map[i] == i), or with
|
||||
//- an optional start index, so that (map[i] == i+start)
|
||||
// The order of parameters is as per the Foam::identity() function.
|
||||
// The parameter order as per the Foam::identity() function.
|
||||
inline static labelRange identity
|
||||
(
|
||||
const label len,
|
||||
@ -135,33 +132,18 @@ public:
|
||||
//- Change the start position
|
||||
inline void setStart(const label i) noexcept;
|
||||
|
||||
//- Change the size
|
||||
//- Change the size, enforcing non-negative size.
|
||||
inline void resize(const label n) noexcept;
|
||||
|
||||
//- Change the size - alias for resize()
|
||||
inline void setSize(const label n) noexcept;
|
||||
|
||||
//- Decrease the size by 1, but never below 0.
|
||||
inline void decrement() noexcept;
|
||||
|
||||
//- Increase the size by 1.
|
||||
inline void increment() noexcept;
|
||||
|
||||
//- Reset to zero start and zero size
|
||||
inline void clear() noexcept;
|
||||
|
||||
//- Is the range empty?
|
||||
inline bool empty() const noexcept;
|
||||
|
||||
//- Return the range as a list of labels
|
||||
List<label> labels() const;
|
||||
|
||||
//- Adjust the start to avoid negative indices.
|
||||
// The size is decreased accordingly, but will never become negative.
|
||||
// Eg, adjusting (-10, 15) becomes (0,5).
|
||||
// adjusting (-20, 15) becomes (0,0)
|
||||
void adjust() noexcept;
|
||||
|
||||
//- Is the range non-empty?
|
||||
inline bool valid() const noexcept;
|
||||
|
||||
@ -171,24 +153,31 @@ public:
|
||||
//- The effective size of the range
|
||||
inline label size() const noexcept;
|
||||
|
||||
//- The (inclusive) lower value of the range - same as start(), first()
|
||||
inline label min() const noexcept;
|
||||
|
||||
//- The (inclusive) upper value of the range - same as last()
|
||||
inline label max() const noexcept;
|
||||
//- The value before the start of the range
|
||||
inline label before() const noexcept;
|
||||
|
||||
//- The (inclusive) lower value of the range - same as start()
|
||||
inline label first() const noexcept;
|
||||
|
||||
//- The (inclusive) lower value of the range - same as start(), first()
|
||||
inline label min() const noexcept;
|
||||
|
||||
//- The (inclusive) upper value of the range - same as max()
|
||||
inline label last() const noexcept;
|
||||
|
||||
//- The value before the start of the range
|
||||
inline label before() const noexcept;
|
||||
//- The (inclusive) upper value of the range - same as last()
|
||||
inline label max() const noexcept;
|
||||
|
||||
//- The value after the last element in the range
|
||||
// This is identical to the value of cend()
|
||||
inline label after() const noexcept;
|
||||
|
||||
//- Adjust the start to avoid negative indices.
|
||||
// The size is decreased accordingly, but will never become negative.
|
||||
// Eg, adjusting (-10, 15) becomes (0,5).
|
||||
// adjusting (-20, 15) becomes (0,0)
|
||||
void adjust() noexcept;
|
||||
|
||||
//- Reset start and size, enforcing non-negative size.
|
||||
// \return true if the updated range is valid (non-empty).
|
||||
inline bool reset(const label start, const label size) noexcept;
|
||||
@ -206,6 +195,9 @@ public:
|
||||
//- Return true if the (global) value is located within the range
|
||||
inline bool found(const label value) const noexcept;
|
||||
|
||||
//- Return the range as a list of labels
|
||||
List<label> labels() const;
|
||||
|
||||
//- Return true if the ranges overlap.
|
||||
// Optional test for ranges that also just touch each other
|
||||
bool overlaps(const labelRange& range, bool touches=false) const;
|
||||
@ -230,19 +222,12 @@ public:
|
||||
labelRange subset0(const label size) const;
|
||||
|
||||
|
||||
//- Return const_iterator to element in the range,
|
||||
//- with bounds checking.
|
||||
// \return iterator at the requested position, or end() if it is
|
||||
// out of bounds
|
||||
inline const_iterator at(const label localIndex) const;
|
||||
|
||||
|
||||
// Member Operators
|
||||
|
||||
//- Return element in the range, without bounds checking
|
||||
inline label operator[](const label localIndex) const noexcept;
|
||||
inline label operator[](const label i) const noexcept;
|
||||
|
||||
//- Return true if the (global) value is located within the range.
|
||||
//- Return true if the global value is located within the range.
|
||||
// Behaviour identical to found() - usable as a predicate
|
||||
inline bool operator()(const label value) const noexcept;
|
||||
|
||||
@ -250,10 +235,16 @@ public:
|
||||
inline label operator++() noexcept;
|
||||
inline label operator++(int) noexcept;
|
||||
|
||||
//- Increase the size by n.
|
||||
inline label operator+=(const label n) noexcept;
|
||||
|
||||
//- Decrease the size by 1, but never below 0.
|
||||
inline label operator--() noexcept;
|
||||
inline label operator--(int) noexcept;
|
||||
|
||||
//- Decrease the size by n, but never below 0.
|
||||
inline label operator-=(const label n) noexcept;
|
||||
|
||||
|
||||
// STL iterator
|
||||
|
||||
@ -269,7 +260,7 @@ public:
|
||||
const label&
|
||||
>
|
||||
{
|
||||
//- The current (global) index value
|
||||
//- The global value
|
||||
label value_;
|
||||
|
||||
public:
|
||||
@ -277,20 +268,31 @@ public:
|
||||
// Constructors
|
||||
|
||||
//- Construct from range at given local index.
|
||||
// If the local index is out of range (eg, negative),
|
||||
// this creates the 'end' iterator
|
||||
// An out-of-range index (eg, negative) creates an 'end' iterator
|
||||
inline const_iterator(const labelRange* range, const label i=0);
|
||||
|
||||
// Member Operators
|
||||
|
||||
//- Return the current (global) value
|
||||
inline label operator*() const;
|
||||
//- Return the (global) value
|
||||
inline label operator*() const noexcept;
|
||||
|
||||
inline const_iterator& operator++();
|
||||
//- Prefix increment, no range checking
|
||||
inline const_iterator& operator++() noexcept;
|
||||
|
||||
//- Postfix increment, no range checking
|
||||
inline const_iterator operator++(int);
|
||||
|
||||
inline bool operator==(const const_iterator& iter) const;
|
||||
inline bool operator!=(const const_iterator& iter) const;
|
||||
//- Arbitrary increment, no range checking
|
||||
inline const_iterator& operator+=(const label n) noexcept;
|
||||
|
||||
//- Arbitrary decrement, no range checking
|
||||
inline const_iterator& operator-=(const label n) noexcept;
|
||||
|
||||
//- Test for equality of values
|
||||
inline bool operator==(const const_iterator& iter) const noexcept;
|
||||
|
||||
//- Test for inequality of values
|
||||
inline bool operator!=(const const_iterator& iter) const noexcept;
|
||||
};
|
||||
|
||||
|
||||
@ -302,18 +304,19 @@ public:
|
||||
// The value returned is guaranteed to be the same as start()
|
||||
inline const_iterator cbegin() const;
|
||||
|
||||
//- A const_iterator set to beyond the end of the range
|
||||
//- A const_iterator set to 1 beyond the end of the range.
|
||||
// The value returned is the same as after()
|
||||
inline const const_iterator cend() const;
|
||||
|
||||
//- A const_iterator set to beyond the end of the range
|
||||
//- A const_iterator set to 1 beyond the end of the range.
|
||||
// The value returned is the same as after()
|
||||
inline const const_iterator end() const;
|
||||
|
||||
|
||||
// IOstream Operators
|
||||
|
||||
friend Istream& operator>>(Istream& is, labelRange& range);
|
||||
friend Ostream& operator<<(Ostream& os, const labelRange& range);
|
||||
|
||||
//- Return const_iterator to a position within the range,
|
||||
//- with bounds checking.
|
||||
// \return iterator at the requested position, or end() for
|
||||
// out of bounds
|
||||
inline const_iterator at(const label i) const;
|
||||
};
|
||||
|
||||
|
||||
@ -335,6 +338,15 @@ public:
|
||||
template<class> struct labelRangeOp;
|
||||
|
||||
|
||||
// IOstream Operators
|
||||
|
||||
//- Read labelRange from Istream as (start size) pair, enforce no negative size
|
||||
Istream& operator>>(Istream& is, labelRange& range);
|
||||
|
||||
//- Write labelRange to Ostream as (start size) pair
|
||||
Ostream& operator<<(Ostream& os, const labelRange& range);
|
||||
|
||||
|
||||
// Global Operators
|
||||
|
||||
inline bool operator==(const labelRange& a, const labelRange& b) noexcept
|
||||
@ -344,7 +356,7 @@ inline bool operator==(const labelRange& a, const labelRange& b) noexcept
|
||||
|
||||
inline bool operator!=(const labelRange& a, const labelRange& b) noexcept
|
||||
{
|
||||
return (a.first() != b.first() || a.size() != b.size());
|
||||
return !(a == b);
|
||||
}
|
||||
|
||||
|
||||
|
||||
@ -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
|
||||
@ -43,12 +43,10 @@ inline Foam::labelRange::labelRange
|
||||
start_(start),
|
||||
size_(size)
|
||||
{
|
||||
if (size_ < 0)
|
||||
{
|
||||
size_ = 0; // No negative sizes
|
||||
}
|
||||
if (size_ < 0) size_ = 0; // No negative sizes
|
||||
}
|
||||
|
||||
|
||||
inline Foam::labelRange::labelRange
|
||||
(
|
||||
const label start,
|
||||
@ -86,14 +84,14 @@ inline Foam::labelRange::const_iterator::const_iterator
|
||||
{}
|
||||
|
||||
|
||||
inline Foam::label Foam::labelRange::const_iterator::operator*() const
|
||||
inline Foam::label Foam::labelRange::const_iterator::operator*() const noexcept
|
||||
{
|
||||
return value_;
|
||||
}
|
||||
|
||||
|
||||
inline Foam::labelRange::const_iterator&
|
||||
Foam::labelRange::const_iterator::operator++()
|
||||
Foam::labelRange::const_iterator::operator++() noexcept
|
||||
{
|
||||
++value_;
|
||||
return *this;
|
||||
@ -109,21 +107,37 @@ Foam::labelRange::const_iterator::operator++(int)
|
||||
}
|
||||
|
||||
|
||||
inline Foam::labelRange::const_iterator&
|
||||
Foam::labelRange::const_iterator::operator+=(const label n) noexcept
|
||||
{
|
||||
value_ += n;
|
||||
return *this;
|
||||
}
|
||||
|
||||
|
||||
inline Foam::labelRange::const_iterator&
|
||||
Foam::labelRange::const_iterator::operator-=(const label n) noexcept
|
||||
{
|
||||
value_ -= n;
|
||||
return *this;
|
||||
}
|
||||
|
||||
|
||||
inline bool Foam::labelRange::const_iterator::operator==
|
||||
(
|
||||
const const_iterator& iter
|
||||
) const
|
||||
) const noexcept
|
||||
{
|
||||
return (this->value_ == iter.value_);
|
||||
return (value_ == iter.value_);
|
||||
}
|
||||
|
||||
|
||||
inline bool Foam::labelRange::const_iterator::operator!=
|
||||
(
|
||||
const const_iterator& iter
|
||||
) const
|
||||
) const noexcept
|
||||
{
|
||||
return (this->value_ != iter.value_);
|
||||
return (value_ != iter.value_);
|
||||
}
|
||||
|
||||
|
||||
@ -139,14 +153,6 @@ inline Foam::labelRange::const_iterator Foam::labelRange::cbegin() const
|
||||
}
|
||||
|
||||
|
||||
inline Foam::labelRange::const_iterator
|
||||
Foam::labelRange::at(const label localIndex) const
|
||||
{
|
||||
// The constructor handles out-of-range properly
|
||||
return const_iterator(this, localIndex);
|
||||
}
|
||||
|
||||
|
||||
inline const Foam::labelRange::const_iterator Foam::labelRange::end() const
|
||||
{
|
||||
return const_iterator(this, -1);
|
||||
@ -159,6 +165,13 @@ inline const Foam::labelRange::const_iterator Foam::labelRange::cend() const
|
||||
}
|
||||
|
||||
|
||||
inline Foam::labelRange::const_iterator
|
||||
Foam::labelRange::at(const label i) const
|
||||
{
|
||||
return const_iterator(this, i);
|
||||
}
|
||||
|
||||
|
||||
// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
|
||||
|
||||
inline Foam::labelRange Foam::labelRange::identity
|
||||
@ -191,19 +204,6 @@ inline void Foam::labelRange::setSize(const label n) noexcept
|
||||
}
|
||||
|
||||
|
||||
inline void Foam::labelRange::decrement() noexcept
|
||||
{
|
||||
--size_;
|
||||
if (size_ < 0) size_ = 0; // No negative sizes
|
||||
}
|
||||
|
||||
|
||||
inline void Foam::labelRange::increment() noexcept
|
||||
{
|
||||
++size_;
|
||||
}
|
||||
|
||||
|
||||
inline void Foam::labelRange::clear() noexcept
|
||||
{
|
||||
start_ = size_ = 0;
|
||||
@ -234,19 +234,19 @@ inline Foam::label Foam::labelRange::start() const noexcept
|
||||
}
|
||||
|
||||
|
||||
inline Foam::label Foam::labelRange::min() const noexcept
|
||||
inline Foam::label Foam::labelRange::before() const noexcept
|
||||
{
|
||||
return start_ - 1;
|
||||
}
|
||||
|
||||
|
||||
inline Foam::label Foam::labelRange::first() const noexcept
|
||||
{
|
||||
return start_;
|
||||
}
|
||||
|
||||
|
||||
inline Foam::label Foam::labelRange::max() const noexcept
|
||||
{
|
||||
return start_ + size_ - 1;
|
||||
}
|
||||
|
||||
|
||||
inline Foam::label Foam::labelRange::first() const noexcept
|
||||
inline Foam::label Foam::labelRange::min() const noexcept
|
||||
{
|
||||
return start_;
|
||||
}
|
||||
@ -258,9 +258,9 @@ inline Foam::label Foam::labelRange::last() const noexcept
|
||||
}
|
||||
|
||||
|
||||
inline Foam::label Foam::labelRange::before() const noexcept
|
||||
inline Foam::label Foam::labelRange::max() const noexcept
|
||||
{
|
||||
return start_ - 1;
|
||||
return start_ + size_ - 1;
|
||||
}
|
||||
|
||||
|
||||
@ -279,10 +279,7 @@ inline bool Foam::labelRange::reset
|
||||
start_ = start;
|
||||
size_ = size;
|
||||
|
||||
if (size_ < 0)
|
||||
{
|
||||
size_ = 0; // No negative sizes
|
||||
}
|
||||
if (size_ < 0) size_ = 0; // No negative sizes
|
||||
|
||||
return size_;
|
||||
}
|
||||
@ -313,18 +310,15 @@ inline bool Foam::labelRange::reset
|
||||
|
||||
inline bool Foam::labelRange::found(const label value) const noexcept
|
||||
{
|
||||
return (value >= this->first() && value <= this->last());
|
||||
return (size_ && start_ <= value && (value - start_) < size_);
|
||||
}
|
||||
|
||||
|
||||
// * * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * //
|
||||
|
||||
inline Foam::label Foam::labelRange::operator[]
|
||||
(
|
||||
const label localIndex
|
||||
) const noexcept
|
||||
inline Foam::label Foam::labelRange::operator[](const label i) const noexcept
|
||||
{
|
||||
return start_ + localIndex;
|
||||
return start_ + i;
|
||||
}
|
||||
|
||||
|
||||
@ -348,7 +342,8 @@ inline Foam::label Foam::labelRange::operator++(int) noexcept
|
||||
|
||||
inline Foam::label Foam::labelRange::operator--() noexcept
|
||||
{
|
||||
decrement();
|
||||
--size_;
|
||||
if (size_ < 0) size_ = 0; // No negative sizes
|
||||
return size_;
|
||||
}
|
||||
|
||||
@ -356,9 +351,26 @@ inline Foam::label Foam::labelRange::operator--() noexcept
|
||||
inline Foam::label Foam::labelRange::operator--(int) noexcept
|
||||
{
|
||||
const label old(size_);
|
||||
decrement();
|
||||
--size_;
|
||||
if (size_ < 0) size_ = 0; // No negative sizes
|
||||
return old;
|
||||
}
|
||||
|
||||
|
||||
inline Foam::label Foam::labelRange::operator+=(const label n) noexcept
|
||||
{
|
||||
size_ += n;
|
||||
return size_;
|
||||
}
|
||||
|
||||
|
||||
inline Foam::label Foam::labelRange::operator-=(const label n) noexcept
|
||||
{
|
||||
size_ -= n;
|
||||
if (size_ < 0) size_ = 0; // No negative sizes
|
||||
return size_;
|
||||
}
|
||||
|
||||
|
||||
|
||||
// ************************************************************************* //
|
||||
|
||||
Reference in New Issue
Block a user