ENH: add labelRange comparison operators and subset methods

- improve interface consistency.
This commit is contained in:
Mark Olesen
2017-04-30 21:29:24 +02:00
parent 7b427c382e
commit 75ef6f4e50
11 changed files with 252 additions and 192 deletions

View File

@ -25,7 +25,7 @@ Class
Foam::labelRange
Description
A range of labels.
A range of labels defined by a start and a size.
SourceFiles
labelRange.C
@ -66,28 +66,13 @@ public:
static int debug;
// Public classes
//- Less function class for sorting labelRange
class less
{
public:
bool operator()(const labelRange& a, const labelRange& b)
{
return a.operator<(b);
}
};
// Constructors
//- Construct an empty range with zero as start and size.
//- An empty range with zero for start/size.
inline labelRange();
//- Construct a range from start and size.
//- Construct a range from start and size, enforcing non-negative size.
// Optionally adjust the start to avoid any negative indices.
// Always reduce a negative size to zero.
inline labelRange
(
const label start,
@ -116,21 +101,23 @@ public:
//- Adjust the start to avoid any negative indices
void adjust();
//- Is the range valid (non-empty)?
//- Is the range non-empty?
inline bool valid() const;
//- Return the effective size of the range
//- The (inclusive) lower value of the range
inline label start() const;
//- The effective size of the range
inline label size() const;
//- The (inclusive) lower value of the range
//- The (inclusive) lower value of the range - same as start
inline label first() const;
//- The (inclusive) upper value of the range
inline label last() const;
//- Reset start and size.
//- Reset start and size, enforcing non-negative size.
// 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
(
@ -139,8 +126,8 @@ public:
const bool adjustStart = false
);
//- Return true if the value is within the range
inline bool contains(const label value) const;
//- Return true if the value is located the range
inline bool found(const label value) const;
//- Return true if the ranges overlap.
// Optional test for ranges that also just touch each other
@ -150,22 +137,21 @@ public:
// A prior overlaps() check can be used to avoid squashing gaps.
labelRange join(const labelRange& range) const;
//- Calculate the intersection of the range with another.
// If there is no intersection, it returns an empty range with zero
// for start/size.
labelRange subset(const labelRange& range) const;
//- Calculate the intersection of the range with another.
// If there is no intersection, it returns an empty range with zero
// for start/size.
labelRange subset(const label start, const label size) const;
// Member Operators
//- Return element in range, no bounds checking
inline label operator[](const label i) const;
//- Comparison function for sorting, compares the start.
// If the start values are equal, also compares the size.
inline bool operator<(const labelRange& rhs) const;
//- Join ranges, squashing any gaps in between
// A prior overlaps() check can be used to avoid squashing gaps.
void operator+=(const labelRange& rhs);
inline bool operator==(const labelRange& rhs) const;
inline bool operator!=(const labelRange& rhs) const;
//- Return element in the range, no bounds checking
inline label operator[](const label localIndex) const;
// STL iterator
@ -175,36 +161,32 @@ public:
{
friend class labelRange;
// Private data
// Private data
//- Reference to the range for which this is an iterator
const labelRange& range_;
//- Reference to the range for which this is an iterator
const labelRange* range_;
//- Current index
label index_;
//- Current index, relative to the start
label index_;
// Constructors
// Constructors
//- Construct from range at 'begin' or 'end' position
inline const_iterator
(
const labelRange& range,
const bool endIter = false
);
//- Construct from range at given index.
// A negative index signals the 'end' position
inline const_iterator(const labelRange* range, const label i);
public:
// Member operators
// Member operators
inline bool operator==(const const_iterator& iter) const;
inline bool operator!=(const const_iterator& iter) const;
inline bool operator==(const const_iterator& iter) const;
inline bool operator!=(const const_iterator& iter) const;
inline label operator*() const;
inline label operator()() const;
//- Return the current label
inline label operator*() const;
inline const_iterator& operator++();
inline const_iterator operator++(int);
inline const_iterator& operator++();
inline const_iterator operator++(int);
};
@ -229,6 +211,51 @@ public:
};
// Global Operators
inline bool operator==(const labelRange& a, const labelRange& b)
{
return (a.first() == b.first() && a.size() == b.size());
}
inline bool operator!=(const labelRange& a, const labelRange& b)
{
return !(a == b);
}
//- Comparison function for sorting, compares the start.
// If the start values are equal, also compares the size.
inline bool operator<(const labelRange& a, const labelRange& b)
{
return
(
a.first() < b.first()
||
(
!(b.first() < a.first())
&& a.size() < b.size()
)
);
}
inline bool operator<=(const labelRange& a, const labelRange& b)
{
return !(b < a);
}
inline bool operator>(const labelRange& a, const labelRange& b)
{
return (b < a);
}
inline bool operator>=(const labelRange& a, const labelRange& b)
{
return !(a < b);
}
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
} // End namespace Foam