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
This commit is contained in:
Mark Olesen
2020-09-28 17:13:19 +02:00
parent 27e71c2d9e
commit 56c9134ccc
10 changed files with 487 additions and 142 deletions

View File

@ -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

View File

@ -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);

View File

@ -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;