ENH: freshen code in labelRange classes

- misc improvements in functionality.
This commit is contained in:
Mark Olesen
2017-01-23 17:09:26 +01:00
parent 2fa6ae6f62
commit 7af6fa7b67
7 changed files with 245 additions and 180 deletions

View File

@ -3,7 +3,7 @@
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration | \\ / O peration |
\\ / A nd | Copyright (C) 2011 OpenFOAM Foundation \\ / A nd | Copyright (C) 2011 OpenFOAM Foundation
\\/ M anipulation | \\/ M anipulation | Copyright (C) 2017 OpenCFD Ltd.
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
License License
This file is part of OpenFOAM. This file is part of OpenFOAM.
@ -58,6 +58,7 @@ int main(int argc, char *argv[])
} }
labelRange range;
labelRanges ranges; labelRanges ranges;
bool removeMode = false; bool removeMode = false;
@ -74,14 +75,16 @@ int main(int argc, char *argv[])
continue; continue;
} }
label start = 0; {
label size = 0; label start = 0;
label size = 0;
IStringStream(args[argI])() >> start; IStringStream(args[argI])() >> start;
++argI; ++argI;
IStringStream(args[argI])() >> size; IStringStream(args[argI])() >> size;
labelRange range(start, size); range.reset(start, size);
}
Info<< "---------------" << nl; Info<< "---------------" << nl;
if (removeMode) if (removeMode)
@ -107,10 +110,11 @@ int main(int argc, char *argv[])
ranges.add(range); ranges.add(range);
} }
Info<< "<list>" << ranges << "</list>" << nl; Info<< "<list>" << ranges << "</list>" << nl
forAllConstIter(labelRanges, ranges, iter) << "content:";
for (auto i : ranges)
{ {
Info<< " " << iter(); Info<< " " << i;
} }
Info<< nl; Info<< nl;
} }

View File

@ -3,7 +3,7 @@
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration | \\ / O peration |
\\ / A nd | Copyright (C) 2011 OpenFOAM Foundation \\ / A nd | Copyright (C) 2011 OpenFOAM Foundation
\\/ M anipulation | \\/ M anipulation | Copyright (C) 2017 OpenCFD Ltd.
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
License License
This file is part of OpenFOAM. This file is part of OpenFOAM.
@ -29,8 +29,6 @@ License
// * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * // // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
const Foam::labelRange::const_iterator Foam::labelRange::endIter_;
int Foam::labelRange::debug(::Foam::debug::debugSwitch("labelRange", 0)); int Foam::labelRange::debug(::Foam::debug::debugSwitch("labelRange", 0));
@ -47,13 +45,24 @@ Foam::labelRange::labelRange(Istream& is)
// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * // // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
bool Foam::labelRange::intersects void Foam::labelRange::adjust()
(
const labelRange& range,
const bool touches
) const
{ {
label final = touches ? 1 : 0; if (start_ < 0)
{
size_ += start_;
start_ = 0;
}
if (size_ < 0)
{
size_ = 0;
}
}
bool Foam::labelRange::overlaps(const labelRange& range, bool touches) const
{
const label final = touches ? 1 : 0;
return return
( (
@ -97,7 +106,7 @@ Foam::labelRange Foam::labelRange::join(const labelRange& range) const
// * * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * // // * * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * //
Foam::labelRange& Foam::labelRange::operator+=(const labelRange& rhs) void Foam::labelRange::operator+=(const labelRange& rhs)
{ {
if (!size_) if (!size_)
{ {
@ -112,8 +121,6 @@ Foam::labelRange& Foam::labelRange::operator+=(const labelRange& rhs)
start_ = lower; start_ = lower;
size_ = upper - lower + 1; size_ = upper - lower + 1;
} }
return *this;
} }
@ -127,10 +134,10 @@ Foam::Istream& Foam::operator>>(Istream& is, labelRange& range)
is.check("operator>>(Istream&, labelRange&)"); is.check("operator>>(Istream&, labelRange&)");
// disallow invalid sizes // Disallow invalid sizes
if (range.size_ <= 0) if (range.size_ < 0)
{ {
range.clear(); range.size_ = 0;
} }
return is; return is;
@ -139,15 +146,11 @@ Foam::Istream& Foam::operator>>(Istream& is, labelRange& range)
Foam::Ostream& Foam::operator<<(Ostream& os, const labelRange& range) Foam::Ostream& Foam::operator<<(Ostream& os, const labelRange& range)
{ {
// write ASCII only for now // Write ASCII only for now
os << token::BEGIN_LIST os << token::BEGIN_LIST
<< range.start_ << token::SPACE << range.size_ << range.start_ << token::SPACE << range.size_
<< token::END_LIST; << token::END_LIST;
// os << token::BEGIN_BLOCK
// << range.start_ << "-" << range.last()
// << token::END_BLOCK;
os.check("operator<<(Ostream&, const labelRange&)"); os.check("operator<<(Ostream&, const labelRange&)");
return os; return os;
} }

View File

@ -3,7 +3,7 @@
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration | \\ / O peration |
\\ / A nd | Copyright (C) 2011 OpenFOAM Foundation \\ / A nd | Copyright (C) 2011 OpenFOAM Foundation
\\/ M anipulation | \\/ M anipulation | Copyright (C) 2017 OpenCFD Ltd.
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
License License
This file is part of OpenFOAM. This file is part of OpenFOAM.
@ -25,7 +25,7 @@ Class
Foam::labelRange Foam::labelRange
Description Description
A label range specifier. A range of labels.
SourceFiles SourceFiles
labelRange.C labelRange.C
@ -47,8 +47,8 @@ class Ostream;
// Forward declaration of friend functions and operators // Forward declaration of friend functions and operators
class labelRange; class labelRange;
Istream& operator>>(Istream&, labelRange&); Istream& operator>>(Istream& is, labelRange& range);
Ostream& operator<<(Ostream&, const labelRange&); Ostream& operator<<(Ostream& os, const labelRange& range);
/*---------------------------------------------------------------------------*\ /*---------------------------------------------------------------------------*\
Class labelRange Declaration Class labelRange Declaration
@ -63,7 +63,7 @@ class labelRange
public: public:
static int debug; static int debug;
// Public classes // Public classes
@ -75,31 +75,50 @@ public:
bool operator()(const labelRange& a, const labelRange& b) bool operator()(const labelRange& a, const labelRange& b)
{ {
return a.first() < b.first(); return a.operator<(b);
} }
}; };
// Constructors // Constructors
//- Construct an empty range //- Construct an empty range with zero as start and size.
inline labelRange(); inline labelRange();
//- Construct a range //- Construct a range from start and size.
// A negative size is autmatically changed to zero. // Optionally adjust the start to avoid any negative indices.
inline labelRange(const label start, const label size); // Always reduce a negative size to zero.
inline labelRange
(
const label start,
const label size,
const bool adjustStart = false
);
//- Construct from Istream. //- Construct from Istream.
labelRange(Istream&); labelRange(Istream& is);
// Member Functions // Member Functions
//- Reset to zero size //- Alias for setSize(const label)
inline void resize(const label n);
//- Adjust size
inline void setSize(const label n);
//- Reset to zero start and zero size
inline void clear(); inline void clear();
//- Is the range empty? //- Is the range empty?
inline bool empty() const; inline bool empty() const;
//- Adjust the start to avoid any negative indices
void adjust();
//- Is the range valid (non-empty)?
inline bool valid() const;
//- Return the effective size of the range //- Return the effective size of the range
inline label size() const; inline label size() const;
@ -109,32 +128,44 @@ public:
//- The (inclusive) upper value of the range //- The (inclusive) upper value of the range
inline label last() const; inline label last() const;
//- Return true if the value is within the range //- Reset start and size.
inline bool contains(const label) const; // Optionally adjust the start to avoid any negative indices.
// Always reduce a negative size to zero.
// Return true if the updated range valid (non-empty).
inline bool reset
(
const label start,
const label size,
const bool adjustStart = false
);
//- Return true if the ranges intersect //- Return true if the value is within the range
inline bool contains(const label value) const;
//- Return true if the ranges overlap.
// Optional test for ranges that also just touch each other // Optional test for ranges that also just touch each other
bool intersects(const labelRange&, const bool touches = false) const; bool overlaps(const labelRange& range, bool touches=false) const;
//- Return a joined range, squashing any gaps in between //- Return a joined range, squashing any gaps in between
// A prior intersects() check can be used to avoid squashing gaps. // A prior overlaps() check can be used to avoid squashing gaps.
labelRange join(const labelRange&) const; labelRange join(const labelRange& range) const;
// Member Operators // Member Operators
//- Return element in range, no bounds checking //- Return element in range, no bounds checking
inline label operator[](const label) const; inline label operator[](const label i) const;
//- Comparison function for sorting, compares the start //- Comparison function for sorting, compares the start.
// If the start values are equal, also compares the size.
inline bool operator<(const labelRange& rhs) const; inline bool operator<(const labelRange& rhs) const;
//- Join ranges, squashing any gaps in between //- Join ranges, squashing any gaps in between
// A prior intersects() check can be used to avoid squashing gaps. // A prior overlaps() check can be used to avoid squashing gaps.
labelRange& operator+=(const labelRange&); void operator+=(const labelRange& rhs);
inline bool operator==(const labelRange&) const; inline bool operator==(const labelRange& rhs) const;
inline bool operator!=(const labelRange&) const; inline bool operator!=(const labelRange& rhs) const;
// STL iterator // STL iterator
@ -142,6 +173,8 @@ public:
//- An STL const_iterator //- An STL const_iterator
class const_iterator class const_iterator
{ {
friend class labelRange;
// Private data // Private data
//- Reference to the range for which this is an iterator //- Reference to the range for which this is an iterator
@ -150,54 +183,48 @@ public:
//- Current index //- Current index
label index_; label index_;
public:
// Constructors // Constructors
//- Construct null - equivalent to an 'end' position //- Construct from range at 'begin' or 'end' position
inline const_iterator(); inline const_iterator
(
//- Construct from range, moving to its 'begin' position const labelRange& range,
inline explicit const_iterator(const labelRange&); const bool endIter = false
);
public:
// Member operators // Member operators
inline bool operator==(const const_iterator&) const; inline bool operator==(const const_iterator& iter) const;
inline bool operator!=(const const_iterator& iter) const;
inline bool operator!=(const const_iterator&) const; inline label operator*() const;
inline label operator()() const;
inline label operator*();
inline label operator()();
inline const_iterator& operator++(); inline const_iterator& operator++();
inline const_iterator operator++(int); inline const_iterator operator++(int);
}; };
//- const_iterator set to the beginning of the range //- A const_iterator set to the beginning of the range
inline const_iterator cbegin() const; inline const_iterator cbegin() const;
//- const_iterator set to beyond the end of the range //- A const_iterator set to beyond the end of the range
inline const const_iterator& cend() const; inline const const_iterator cend() const;
//- const_iterator set to the beginning of the range //- A const_iterator set to the beginning of the range
inline const_iterator begin() const; inline const_iterator begin() const;
//- const_iterator set to beyond the end of the range //- A const_iterator set to beyond the end of the range
inline const const_iterator& end() const; inline const const_iterator end() const;
// IOstream Operators // IOstream Operators
friend Istream& operator>>(Istream&, labelRange&); friend Istream& operator>>(Istream& is, labelRange& range);
friend Ostream& operator<<(Ostream&, const labelRange&); friend Ostream& operator<<(Ostream& os, const labelRange& range);
private:
//- const_iterator returned by end(), cend()
static const const_iterator endIter_;
}; };

View File

@ -3,7 +3,7 @@
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration | \\ / O peration |
\\ / A nd | Copyright (C) 2011-2016 OpenFOAM Foundation \\ / A nd | Copyright (C) 2011-2016 OpenFOAM Foundation
\\/ M anipulation | \\/ M anipulation | Copyright (C) 2017 OpenCFD Ltd.
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
License License
This file is part of OpenFOAM. This file is part of OpenFOAM.
@ -33,32 +33,39 @@ inline Foam::labelRange::labelRange()
{} {}
inline Foam::labelRange::labelRange(const label start, const label size) inline Foam::labelRange::labelRange
(
const label start,
const label size,
const bool adjustStart
)
: :
start_(start), start_(start),
size_(size) size_(size)
{ {
// disallow invalid sizes if (adjustStart)
if (size_ <= 0)
{ {
this->clear(); // Disallow invalid indices and sizes
adjust();
}
else if (size_ < 0)
{
// Disallow invalid sizes
size_ = 0;
} }
} }
// * * * * * * * * * * * * * * * * Iterators * * * * * * * * * * * * * * * * // // * * * * * * * * * * * * * * * * Iterators * * * * * * * * * * * * * * * * //
inline Foam::labelRange::const_iterator::const_iterator() inline Foam::labelRange::const_iterator::const_iterator
: (
range_(*reinterpret_cast<Foam::labelRange* >(0)), const labelRange& range,
index_(-1) const bool endIter
{} )
inline Foam::labelRange::const_iterator::const_iterator(const labelRange& range)
: :
range_(range), range_(range),
index_(range_.empty() ? -1 : 0) index_(endIter ? range_.size() : 0)
{} {}
@ -76,17 +83,17 @@ inline bool Foam::labelRange::const_iterator::operator!=
const const_iterator& iter const const_iterator& iter
) const ) const
{ {
return !(this->operator==(iter)); return (this->index_ != iter.index_);
} }
inline Foam::label Foam::labelRange::const_iterator::operator*() inline Foam::label Foam::labelRange::const_iterator::operator*() const
{ {
return range_[index_]; return range_[index_];
} }
inline Foam::label Foam::labelRange::const_iterator::operator()() inline Foam::label Foam::labelRange::const_iterator::operator()() const
{ {
return range_[index_]; return range_[index_];
} }
@ -95,12 +102,7 @@ inline Foam::label Foam::labelRange::const_iterator::operator()()
inline Foam::labelRange::const_iterator& inline Foam::labelRange::const_iterator&
Foam::labelRange::const_iterator::operator++() Foam::labelRange::const_iterator::operator++()
{ {
if (++index_ >= range_.size()) ++index_;
{
// equivalent to end iterator
index_ = -1;
}
return *this; return *this;
} }
@ -109,7 +111,7 @@ inline Foam::labelRange::const_iterator
Foam::labelRange::const_iterator::operator++(int) Foam::labelRange::const_iterator::operator++(int)
{ {
const_iterator old = *this; const_iterator old = *this;
this->operator++(); ++index_;
return old; return old;
} }
@ -120,9 +122,9 @@ inline Foam::labelRange::const_iterator Foam::labelRange::cbegin() const
} }
inline const Foam::labelRange::const_iterator& Foam::labelRange::cend() const inline const Foam::labelRange::const_iterator Foam::labelRange::cend() const
{ {
return endIter_; return const_iterator(*this, true);
} }
@ -132,14 +134,31 @@ inline Foam::labelRange::const_iterator Foam::labelRange::begin() const
} }
inline const Foam::labelRange::const_iterator& Foam::labelRange::end() const inline const Foam::labelRange::const_iterator Foam::labelRange::end() const
{ {
return endIter_; return const_iterator(*this, true);
} }
// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * // // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
inline void Foam::labelRange::resize(const label n)
{
setSize(n);
}
inline void Foam::labelRange::setSize(const label n)
{
size_ = n;
if (size_ < 0)
{
size_ = 0;
}
}
inline void Foam::labelRange::clear() inline void Foam::labelRange::clear()
{ {
start_ = size_ = 0; start_ = size_ = 0;
@ -152,6 +171,12 @@ inline bool Foam::labelRange::empty() const
} }
inline bool Foam::labelRange::valid() const
{
return size_;
}
inline Foam::label Foam::labelRange::size() const inline Foam::label Foam::labelRange::size() const
{ {
return size_; return size_;
@ -170,6 +195,31 @@ inline Foam::label Foam::labelRange::last() const
} }
inline bool Foam::labelRange::reset
(
const label start,
const label size,
const bool adjustStart
)
{
start_ = start;
size_ = size;
if (adjustStart)
{
// Disallow invalid indices and sizes
adjust();
}
else if (size_ < 0)
{
// Disallow invalid sizes
size_ = 0;
}
return size_;
}
inline bool Foam::labelRange::contains(const label value) const inline bool Foam::labelRange::contains(const label value) const
{ {
return value >= this->first() && value <= this->last(); return value >= this->first() && value <= this->last();
@ -186,7 +236,11 @@ inline Foam::label Foam::labelRange::operator[](const label i) const
inline bool Foam::labelRange::operator<(const labelRange& rhs) const inline bool Foam::labelRange::operator<(const labelRange& rhs) const
{ {
return this->first() < rhs.first(); return
(
this->first() < rhs.first()
|| (this->first() == rhs.first() && this->size() < rhs.size())
);
} }

View File

@ -3,7 +3,7 @@
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration | \\ / O peration |
\\ / A nd | Copyright (C) 2011 OpenFOAM Foundation \\ / A nd | Copyright (C) 2011 OpenFOAM Foundation
\\/ M anipulation | \\/ M anipulation | Copyright (C) 2017 OpenCFD Ltd.
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
License License
This file is part of OpenFOAM. This file is part of OpenFOAM.
@ -26,11 +26,6 @@ License
#include "labelRanges.H" #include "labelRanges.H"
#include "ListOps.H" #include "ListOps.H"
// * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
const Foam::labelRanges::const_iterator Foam::labelRanges::endIter_;
// * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * // // * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * //
void Foam::labelRanges::insertBefore void Foam::labelRanges::insertBefore
@ -141,7 +136,7 @@ bool Foam::labelRanges::add(const labelRange& range)
{ {
labelRange& currRange = ParentType::operator[](elemI); labelRange& currRange = ParentType::operator[](elemI);
if (currRange.intersects(range, true)) if (currRange.overlaps(range, true))
{ {
// absorb into the existing (adjacent/overlapping) range // absorb into the existing (adjacent/overlapping) range
currRange += range; currRange += range;
@ -150,7 +145,7 @@ bool Foam::labelRanges::add(const labelRange& range)
for (; elemI < this->size()-1; ++elemI) for (; elemI < this->size()-1; ++elemI)
{ {
labelRange& nextRange = ParentType::operator[](elemI+1); labelRange& nextRange = ParentType::operator[](elemI+1);
if (currRange.intersects(nextRange, true)) if (currRange.overlaps(nextRange, true))
{ {
currRange += nextRange; currRange += nextRange;
nextRange.clear(); nextRange.clear();

View File

@ -3,7 +3,7 @@
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration | \\ / O peration |
\\ / A nd | Copyright (C) 2011 OpenFOAM Foundation \\ / A nd | Copyright (C) 2011 OpenFOAM Foundation
\\/ M anipulation | \\/ M anipulation | Copyright (C) 2017 OpenCFD Ltd.
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
License License
This file is part of OpenFOAM. This file is part of OpenFOAM.
@ -49,8 +49,8 @@ class Ostream;
// Forward declaration of friend functions and operators // Forward declaration of friend functions and operators
class labelRanges; class labelRanges;
Istream& operator>>(Istream&, labelRanges&); Istream& operator>>(Istream& is, labelRanges& ranges);
Ostream& operator<<(Ostream&, const labelRanges&); Ostream& operator<<(Ostream& is, const labelRanges& ranges);
/*---------------------------------------------------------------------------*\ /*---------------------------------------------------------------------------*\
Class labelRanges Declaration Class labelRanges Declaration
@ -68,13 +68,13 @@ class labelRanges
// Private Member Functions // Private Member Functions
//- Insert range before specified insertion index, by copying up //- Insert range before specified insertion index, by copying up
void insertBefore(const label, const labelRange&); void insertBefore(const label insert, const labelRange& range);
//- Purge empty ranges, by copying down //- Purge empty ranges, by copying down
void purgeEmpty(); void purgeEmpty();
//- Print the range for debugging purposes //- Print the range for debugging purposes
Ostream& printRange(Ostream&, const labelRange&) const; Ostream& printRange(Ostream& os, const labelRange& range) const;
public: public:
@ -85,10 +85,10 @@ public:
inline labelRanges(); inline labelRanges();
//- Construct given size //- Construct given size
inline explicit labelRanges(const label); inline explicit labelRanges(const label nElem);
//- Construct from Istream. //- Construct from Istream.
labelRanges(Istream&); labelRanges(Istream& is);
// Member Functions // Member Functions
@ -100,19 +100,22 @@ public:
using DynamicList<labelRange>::empty; using DynamicList<labelRange>::empty;
//- Return true if the value is within any of the ranges //- Return true if the value is within any of the ranges
inline bool contains(const label) const; inline bool contains(const label value) const;
//- Add the range to the list //- Add the range to the list
bool add(const labelRange&); bool add(const labelRange& range);
//- Remove the range from the list //- Remove the range from the list
bool remove(const labelRange&); bool remove(const labelRange& range);
// STL iterator // STL iterator
//- An STL const_iterator //- An STL const_iterator
class const_iterator class const_iterator
{ {
friend class labelRanges;
// Private data // Private data
//- Reference to the list for which this is an iterator //- Reference to the list for which this is an iterator
@ -124,22 +127,21 @@ public:
//- Index of current element at listIndex //- Index of current element at listIndex
label subIndex_; label subIndex_;
public:
// Constructors // Constructors
//- Construct null - equivalent to an 'end' position //- Construct from ranges at 'begin' or 'end' position
inline const_iterator(); inline const_iterator
(
//- Construct from list, moving to its 'begin' position const labelRanges& lst,
inline explicit const_iterator(const labelRanges&); const bool endIter = false
);
public:
// Member operators // Member operators
inline bool operator==(const const_iterator&) const; inline bool operator==(const const_iterator& iter) const;
inline bool operator!=(const const_iterator& iter) const;
inline bool operator!=(const const_iterator&) const;
inline label operator*(); inline label operator*();
inline label operator()(); inline label operator()();
@ -149,29 +151,23 @@ public:
}; };
//- const_iterator set to the beginning of the list //- A const_iterator set to the beginning of the list
inline const_iterator cbegin() const; inline const_iterator cbegin() const;
//- const_iterator set to beyond the end of the list //- A const_iterator set to beyond the end of the list
inline const const_iterator& cend() const; inline const const_iterator cend() const;
//- const_iterator set to the beginning of the list //- A const_iterator set to the beginning of the list
inline const_iterator begin() const; inline const_iterator begin() const;
//- const_iterator set to beyond the end of the list //- A const_iterator set to beyond the end of the list
inline const const_iterator& end() const; inline const const_iterator end() const;
// IOstream Operators // IOstream Operators
friend Istream& operator>>(Istream&, labelRanges&); friend Istream& operator>>(Istream& is, labelRanges& ranges);
friend Ostream& operator<<(Ostream&, const labelRanges&); friend Ostream& operator<<(Ostream& os, const labelRanges& ranges);
private:
//- const_iterator returned by end(), cend()
static const const_iterator endIter_;
}; };

View File

@ -3,7 +3,7 @@
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration | \\ / O peration |
\\ / A nd | Copyright (C) 2011-2016 OpenFOAM Foundation \\ / A nd | Copyright (C) 2011-2016 OpenFOAM Foundation
\\/ M anipulation | \\/ M anipulation | Copyright (C) 2017 OpenCFD Ltd.
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
License License
This file is part of OpenFOAM. This file is part of OpenFOAM.
@ -40,28 +40,18 @@ inline Foam::labelRanges::labelRanges(const label nElem)
// * * * * * * * * * * * * * * * * Iterators * * * * * * * * * * * * * * * * // // * * * * * * * * * * * * * * * * Iterators * * * * * * * * * * * * * * * * //
inline Foam::labelRanges::const_iterator::const_iterator() inline Foam::labelRanges::const_iterator::const_iterator
(
const labelRanges& lst,
const bool endIter
)
: :
list_(*reinterpret_cast<Foam::labelRanges* >(0)), list_(lst),
index_(-1), index_(endIter ? lst.size() : 0),
subIndex_(-1) subIndex_(0)
{} {}
inline Foam::labelRanges::const_iterator::const_iterator(const labelRanges& lst)
:
list_(lst),
index_(0),
subIndex_(0)
{
if (list_.empty())
{
// equivalent to end iterator
index_ = subIndex_ = -1;
}
}
inline bool Foam::labelRanges::const_iterator::operator== inline bool Foam::labelRanges::const_iterator::operator==
( (
const const_iterator& iter const const_iterator& iter
@ -69,7 +59,7 @@ inline bool Foam::labelRanges::const_iterator::operator==
{ {
return return
( (
this->index_ == iter.index_ this->index_ == iter.index_
&& this->subIndex_ == iter.subIndex_ && this->subIndex_ == iter.subIndex_
); );
} }
@ -101,13 +91,9 @@ Foam::labelRanges::const_iterator::operator++()
{ {
if (++subIndex_ >= list_[index_].size()) if (++subIndex_ >= list_[index_].size())
{ {
// go to next list entry // Next sub-list
++index_;
subIndex_ = 0; subIndex_ = 0;
if (++index_ >= list_.size())
{
// equivalent to end iterator
index_ = subIndex_ = -1;
}
} }
return *this; return *this;
@ -129,9 +115,9 @@ inline Foam::labelRanges::const_iterator Foam::labelRanges::cbegin() const
} }
inline const Foam::labelRanges::const_iterator& Foam::labelRanges::cend() const inline const Foam::labelRanges::const_iterator Foam::labelRanges::cend() const
{ {
return endIter_; return const_iterator(*this, true);
} }
@ -141,9 +127,9 @@ inline Foam::labelRanges::const_iterator Foam::labelRanges::begin() const
} }
inline const Foam::labelRanges::const_iterator& Foam::labelRanges::end() const inline const Foam::labelRanges::const_iterator Foam::labelRanges::end() const
{ {
return endIter_; return const_iterator(*this, true);
} }