Commit 56c9134c authored by Mark Olesen's avatar Mark Olesen
Browse files

ENH: add identity(IntRange) and Istream operator for common types

- provides consistency with identity(label, label) and looks more
  familiar than using labelRange::labels()

- relocates labelRange IO operators to IntRange

ENH: make sliceRange interators random access

STYLE: scalarRanges::match() instead of predicate operator
parent 27e71c2d
......@@ -20,6 +20,7 @@ Description
#include "argList.H"
#include "labelPair.H"
#include "IntRange.H"
#include "StringStream.H"
using namespace Foam;
......@@ -66,12 +67,22 @@ int main(int argc, char *argv[])
typedef IntRange<int> intRange;
Info<< "Default construct int(32): " << IntRange<int32_t>() << nl
<< "Default construct int(64): " << IntRange<int64_t>() << nl;
Info<< "Default construct int32_t: " << IntRange<int32_t>() << nl
<< "Default construct int64_t: " << IntRange<int64_t>() << nl;
Info<< " one: " << intRange(10) << nl
<< " two: " << intRange(5, 10) << nl;
// Read from stream
{
IStringStream is("(10 100)");
intRange range;
is >> range;
Info<< "From stream int32_t: " << range << nl;
}
for
(
const labelPair& pr
......
......@@ -70,6 +70,11 @@ int main(int argc, char *argv[])
labelRange::debug = 1;
}
{
labelRange range(5, 10);
Info<< "identity: " << identity(range) << nl;
}
{
Info<<"test sorting" << endl;
DynamicList<labelRange> list1(10);
......
......@@ -94,6 +94,42 @@ void printInfo(const sliceCoeffs& coeffs)
}
void printForLoop(const sliceRange& range)
{
Info<< "for " << range << nl
<< " >";
for (const label val : range)
{
Info<< ' ' << val;
}
Info<< nl;
}
template<class IteratorType>
void printIteratorTest(IteratorType& iter)
{
const auto iter2 = (iter - 5);
const auto iter3 = (iter + 5);
// Info<< typeid(iter).name() << nl;
Info<< "begin: " << *iter++;
Info<< " next: " << *iter;
Info<< " next: " << *(++iter);
Info<< " [5]: " << iter[5];
Info<< " +10: " << *(iter + 10);
Info<< " -10: " << *(iter - 10);
Info<< nl;
Info<< "compare: " << *iter2 << " and " << *iter3 << nl;
Info<< " == " << (iter2 == iter3) << nl;
Info<< " < " << (iter2 < iter3) << nl;
}
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
// Main program:
......@@ -119,12 +155,25 @@ int main(int argc, char *argv[])
printInfo(coeffs);
}
// Some iterator tests
{
const sliceRange range(25, 8, 3);
auto iter1 = range.begin();
Info<< nl << "Forward iterator for " << range << nl;
printIteratorTest(iter1);
auto iter2 = range.rbegin();
Info<< nl << "Reverse iterator for " << range << nl;
printIteratorTest(iter2);
}
// Generator
{
sliceRange range(25, 8, 3);
Info<< "Generator for " << range << nl;
Info<< nl << "Generator for " << range << nl;
auto gen = range.generator();
......@@ -216,6 +265,16 @@ int main(int argc, char *argv[])
<< " = " << flatOutput(list1) << nl;
}
// For loops
{
Info<< nl << "Test for loops" << nl;
printForLoop(sliceRange(25, 8, -2));
printForLoop(sliceRange(10, 3, 0));
printForLoop(sliceRange(10, 3, 2));
}
Info<< "\nEnd\n" << endl;
return 0;
......
......@@ -6,7 +6,7 @@
\\/ M anipulation |
-------------------------------------------------------------------------------
Copyright (C) 2011-2017 OpenFOAM Foundation
Copyright (C) 2018 OpenCFD Ltd.
Copyright (C) 2018-2020 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
......@@ -49,7 +49,7 @@ Foam::timeSelector::timeSelector(const std::string& str)
bool Foam::timeSelector::selected(const instant& value) const
{
return scalarRanges::operator()(value.value());
return scalarRanges::match(value.value());
}
......
......@@ -42,7 +42,7 @@ SourceFiles
#ifndef IntRange_H
#define IntRange_H
#include <cstdint>
#include "labelFwd.H"
#include <iterator>
#include <type_traits>
......@@ -52,7 +52,9 @@ namespace Foam
{
// Forward Declarations
class Istream;
class Ostream;
template<class T> class List;
/*---------------------------------------------------------------------------*\
Class IntRange Declaration
......@@ -71,6 +73,7 @@ class IntRange
//- The length of the interval
IntType size_;
protected:
// Protected Member Functions
......@@ -200,6 +203,42 @@ public:
explicit operator bool() const noexcept { return bool(size_); }
// Bidirectional input iterators (const)
//- 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 IntType i) const;
//- A const_iterator set to the beginning of the range
inline const_iterator begin() const;
//- A const_iterator set to the beginning of the range
inline const_iterator cbegin() const;
//- A const_iterator set to 1 beyond the end of the range.
inline const_iterator cend() const;
//- A const_iterator set to 1 beyond the end of the range.
inline const_iterator end() const;
// Bidirectional reverse input iterators (const)
//- A const_reverse_iterator set to 1 before the end of range
inline const_reverse_iterator rbegin() const;
//- A const_reverse_iterator set to 1 before the end of range
inline const_reverse_iterator crbegin() const;
//- A const_reverse_iterator set to 1 before the begin of range
inline const_reverse_iterator rend() const;
//- A const_reverse_iterator set to 1 before the begin of range
inline const_reverse_iterator crend() const;
// Iterators
//- Random-access input iterator with const access
......@@ -422,50 +461,39 @@ public:
return !(*this < iter);
}
};
};
// Bidirectional input iterators (const)
//- 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 IntType i) const;
//- A const_iterator set to the beginning of the range
inline const_iterator begin() const;
// * * * * * * * * * * * * * * * Global Functions * * * * * * * * * * * * * //
//- A const_iterator set to the beginning of the range
inline const_iterator cbegin() const;
// Identity function for common integer types
//- A const_iterator set to 1 beyond the end of the range.
inline const_iterator cend() const;
//- Identity map from an int32_t IntRange
List<label> identity(const IntRange<int32_t>& range);
//- A const_iterator set to 1 beyond the end of the range.
inline const_iterator end() const;
#if defined(WM_LABEL_SIZE) && (WM_LABEL_SIZE >= 64)
//- Identity map from an int64_t IntRange
List<label> identity(const IntRange<int64_t>& range);
#endif
// Bidirectional reverse input iterators (const)
//- A const_reverse_iterator set to 1 before the end of range
inline const_reverse_iterator rbegin() const;
//- A const_reverse_iterator set to 1 before the end of range
inline const_reverse_iterator crbegin() const;
// * * * * * * * * * * * * * * * IOstream Operators * * * * * * * * * * * * //
//- A const_reverse_iterator set to 1 before the begin of range
inline const_reverse_iterator rend() const;
// Input operators for common integer types
//- A const_reverse_iterator set to 1 before the begin of range
inline const_reverse_iterator crend() const;
};
//- Read IntRange from Istream as bracketed (start size) tuple, no checks
Istream& operator>>(Istream& os, IntRange<int32_t>& range);
//- Read IntRange from Istream as bracketed (start size) tuple, no checks
Istream& operator>>(Istream& os, IntRange<int64_t>& range);
// * * * * * * * * * * * * * * * IOstream Operators * * * * * * * * * * * * //
// Output operators for common integer types
//- Write IntRange to Ostream as bracketed (start size) tuple
Ostream& operator<<(Ostream& os, const IntRange<int32_t>& range);
//- Write IntRange to Ostream as bracketed (start size) tuple
Ostream& operator<<(Ostream& os, const IntRange<int64_t>& range);
......
......@@ -25,15 +25,32 @@ License
\*---------------------------------------------------------------------------*/
#include "label.H"
#include "token.H"
#include "List.H"
#include "Istream.H"
#include "Ostream.H"
#include <numeric>
// * * * * * * * * * * * * * * * Local Functions * * * * * * * * * * * * * * //
namespace Foam
{
#if 0
template<class T>
inline static List<label> makeIdentity(const IntRange<T>& range)
{
if (range.size() < 0)
{
// Skip this check?
return List<label>();
}
List<label> result(range.size());
std::iota(result.begin(), result.end(), range.start());
return result;
}
template<class T>
inline static Istream& input(Istream& is, IntRange<T>& range)
{
......@@ -44,7 +61,6 @@ namespace Foam
is.check(FUNCTION_NAME);
return is;
}
#endif
template<class T>
inline static Ostream& output(Ostream& os, const IntRange<T>& range)
......@@ -56,12 +72,28 @@ namespace Foam
os.check(FUNCTION_NAME);
return os;
}
} // End namespace Foam
// * * * * * * * * * * * * * * * Global Functions * * * * * * * * * * * * * //
Foam::List<Foam::label> Foam::identity(const IntRange<int32_t>& range)
{
return makeIdentity(range);
}
#if defined(WM_LABEL_SIZE) && (WM_LABEL_SIZE >= 64)
Foam::List<Foam::label> Foam::identity(const IntRange<int64_t>& range)
{
return makeIdentity(range);
}
#endif
// * * * * * * * * * * * * * * * IOstream Operators * * * * * * * * * * * * //
#if 0
Foam::Istream& Foam::operator>>(Istream& is, IntRange<int32_t>& range)
{
return input(is, range);
......@@ -72,7 +104,6 @@ Foam::Istream& Foam::operator>>(Istream& is, IntRange<int64_t>& range)
{
return input(is, range);
}
#endif
Foam::Ostream& Foam::operator<<(Ostream& os, const IntRange<int32_t>& range)
......
......@@ -27,9 +27,8 @@ License
\*---------------------------------------------------------------------------*/
#include "labelRange.H"
#include "MinMax.H"
#include "List.H"
#include "token.H"
#include "MinMax.H"
#include <numeric>
// * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
......@@ -66,8 +65,9 @@ Foam::labelRange::labelRange(Istream& is)
Foam::List<Foam::label> Foam::labelRange::labels() const
{
if (size() <= 0)
if (size() < 0)
{
// Skip this check?
return List<label>();
}
......@@ -196,28 +196,4 @@ Foam::labelRange Foam::labelRange::subset0(const label size) const
}
// * * * * * * * * * * * * * * * IOstream Operators * * * * * * * * * * * * //
Foam::Istream& Foam::operator>>(Istream& is, labelRange& range)
{
is.readBegin("labelRange");
is >> range.start() >> range.size();
is.readEnd("labelRange");
is.check(FUNCTION_NAME);
return is;
}
Foam::Ostream& Foam::operator<<(Ostream& os, const labelRange& range)
{
os << token::BEGIN_LIST
<< range.start() << token::SPACE << range.size()
<< token::END_LIST;
os.check(FUNCTION_NAME);
return os;
}
// ************************************************************************* //
......@@ -35,10 +35,10 @@ SourceFiles
labelRangeI.H
\*---------------------------------------------------------------------------*/
#ifndef labelRange_H
#define labelRange_H
#include "label.H"
#include "IntRange.H"
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
......@@ -47,9 +47,6 @@ namespace Foam
{
// Forward Declarations
class Istream;
class Ostream;
template<class T> class List;
template<class T> class MinMax;
/*---------------------------------------------------------------------------*\
......@@ -151,7 +148,8 @@ public:
// Other
//- Return list of labels corresponding to the range
//- Return list of labels corresponding to the range.
// Same as Foam::identity()
List<label> labels() const;
//- Return true if the ranges overlap.
......@@ -187,7 +185,7 @@ public:
};
// Global Functions
// * * * * * * * * * * * * * * * Global Functions * * * * * * * * * * * * * //
//- Conversion/extraction to labelRange operation (functor).
// Specializations shall provide a corresponding \c operator().
......@@ -205,15 +203,6 @@ public:
template<class> struct labelRangeOp;
// IOstream Operators
//- Read labelRange from Istream as (start size) tuple, no checks
Istream& operator>>(Istream& is, labelRange& range);
//- Write labelRange to Ostream as (start size) tuple
Ostream& operator<<(Ostream& os, const labelRange& range);
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
} // End namespace Foam
......
......@@ -58,9 +58,7 @@ template<class T, unsigned N> class FixedList;
class sliceRange
{
protected:
// Protected Data
// Private Data
//- The start of the interval
label start_;
......@@ -177,15 +175,13 @@ public:
//- Return const_iterator to a position within the range,
//- with bounds checking.
// \return iterator at the requested position, or end() for
// out of bounds
// out-of-bounds
inline const_iterator at(const label i) const;
//- A const_iterator set to the beginning of the range
// The value returned is guaranteed to be the same as start()
inline const_iterator begin() const;
//- A const_iterator set to the beginning of the range
// The value returned is guaranteed to be the same as start()
inline const_iterator cbegin() const;
//- A const_iterator set to 1 beyond the end of the range.
......@@ -232,7 +228,7 @@ public:
//- Default construct with zero value and stride = 1
inline constexpr indexer() noexcept;
//- Construct with specified value and stride,
//- Construct with specified value and stride
inline constexpr indexer
(
const label val,
......@@ -242,6 +238,18 @@ public:
// Member Functions
//- The current value
constexpr label value() const noexcept { return value_; }
//- The stride
constexpr label stride() const noexcept { return stride_; }
//- Value with offset
constexpr label value(const label n) const noexcept
{
return value_ + (n * stride_);
}
//- Decrement value
void prev() noexcept { value_ -= stride_; }
......@@ -254,12 +262,6 @@ public:
//- Increase value
void next(const label n) noexcept { value_ += (n * stride_); }
//- Test for equality of values, ignore stride
constexpr bool equals(const indexer& other) const noexcept
{
return (value_ == other.value_);
}
// Member Operators
......@@ -269,7 +271,7 @@ public:
//- Apply a postfix increment and return the current value.
// This operator definition is required for a generator -
// see std::generate()
inline label operator()();
inline label operator()() noexcept;
};
......@@ -281,7 +283,7 @@ public:
public:
// STL definitions (as per std::iterator)
typedef std::bidirectional_iterator_tag iterator_category;
typedef std::random_access_iterator_tag iterator_category;
// Constructors
......@@ -292,28 +294,81 @@ public:
// Member Operators
//- Offset dereference operator
inline constexpr label operator[](const label n) const noexcept;
//- Prefix increment
inline const_iterator& operator++() noexcept;
//- Arbitrary increment
inline const_iterator& operator+=(const label n) noexcept;
//- Postfix increment
inline const_iterator operator++(int) noexcept;
//- Prefix decrement
inline const_iterator& operator--() noexcept;
//- Postfix decrement
inline const_iterator operator--(int) noexcept;
//- Arbitrary increment
inline const_iterator& operator+=(const label n) noexcept;
//- Arbitrary decrement
inline const_iterator& operator-=(const label n) noexcept;
//- Test for equality of values, ignore stride
constexpr bool operator==(const const_iterator& iter) const noexcept
//- Return iterator with offset
inline constexpr const_iterator operator+
(
const label n
) const noexcept;
//- Return iterator with offset
inline constexpr const_iterator operator-
(
const label n
) const noexcept;
//- Difference operator
inline constexpr label operator-
(
const const_iterator& iter
) const noexcept;
// Comparison
//- Test for equality of values (ignore stride)
inline constexpr bool operator==(const const_iterator& iter)
const noexcept;
//- Compare less-than values (ignore stride)
inline constexpr bool operator<(const const_iterator& iter)
const noexcept;
// Derived comparisons