diff --git a/applications/test/IntRange/Test-IntRange.C b/applications/test/IntRange/Test-IntRange.C index 76936098fc..533d349bb1 100644 --- a/applications/test/IntRange/Test-IntRange.C +++ b/applications/test/IntRange/Test-IntRange.C @@ -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 intRange; - Info<< "Default construct int(32): " << IntRange() << nl - << "Default construct int(64): " << IntRange() << nl; + Info<< "Default construct int32_t: " << IntRange() << nl + << "Default construct int64_t: " << IntRange() << 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 diff --git a/applications/test/labelRanges/Test-labelRanges.C b/applications/test/labelRanges/Test-labelRanges.C index 447230d78a..48acf4b532 100644 --- a/applications/test/labelRanges/Test-labelRanges.C +++ b/applications/test/labelRanges/Test-labelRanges.C @@ -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 list1(10); diff --git a/applications/test/sliceRange/Test-sliceRange.C b/applications/test/sliceRange/Test-sliceRange.C index 9467c0bd2f..7a0c0abc61 100644 --- a/applications/test/sliceRange/Test-sliceRange.C +++ b/applications/test/sliceRange/Test-sliceRange.C @@ -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 +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; diff --git a/src/OpenFOAM/db/Time/timeSelector.C b/src/OpenFOAM/db/Time/timeSelector.C index afd60eb7e2..0f009f52d8 100644 --- a/src/OpenFOAM/db/Time/timeSelector.C +++ b/src/OpenFOAM/db/Time/timeSelector.C @@ -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()); } diff --git a/src/OpenFOAM/primitives/ranges/IntRange/IntRange.H b/src/OpenFOAM/primitives/ranges/IntRange/IntRange.H index 6af9fb06f4..8e0c30772b 100644 --- a/src/OpenFOAM/primitives/ranges/IntRange/IntRange.H +++ b/src/OpenFOAM/primitives/ranges/IntRange/IntRange.H @@ -42,7 +42,7 @@ SourceFiles #ifndef IntRange_H #define IntRange_H -#include +#include "labelFwd.H" #include #include @@ -52,7 +52,9 @@ namespace Foam { // Forward Declarations +class Istream; class Ostream; +template 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; - - //- 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; }; +// * * * * * * * * * * * * * * * Global Functions * * * * * * * * * * * * * // + +// Identity function for common integer types + +//- Identity map from an int32_t IntRange +List