diff --git a/applications/test/sliceRange/Make/files b/applications/test/sliceRange/Make/files new file mode 100644 index 0000000000..6d24718f23 --- /dev/null +++ b/applications/test/sliceRange/Make/files @@ -0,0 +1,3 @@ +Test-sliceRange.C + +EXE = $(FOAM_USER_APPBIN)/Test-sliceRange diff --git a/applications/test/sliceRange/Make/options b/applications/test/sliceRange/Make/options new file mode 100644 index 0000000000..18e6fe47af --- /dev/null +++ b/applications/test/sliceRange/Make/options @@ -0,0 +1,2 @@ +/* EXE_INC = */ +/* EXE_LIBS = */ diff --git a/applications/test/sliceRange/Test-sliceRange.C b/applications/test/sliceRange/Test-sliceRange.C new file mode 100644 index 0000000000..477283ebb7 --- /dev/null +++ b/applications/test/sliceRange/Test-sliceRange.C @@ -0,0 +1,136 @@ +/*---------------------------------------------------------------------------*\ + ========= | + \\ / F ield | OpenFOAM: The Open Source CFD Toolbox + \\ / O peration | + \\ / A nd | Copyright (C) 2019 OpenCFD Ltd. + \\/ M anipulation | +------------------------------------------------------------------------------- +License + This file is part of OpenFOAM. + + OpenFOAM is free software: you can redistribute it and/or modify it + under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + OpenFOAM is distributed in the hope that it will be useful, but WITHOUT + ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + for more details. + + You should have received a copy of the GNU General Public License + along with OpenFOAM. If not, see . + +Description + Test slice range +\*---------------------------------------------------------------------------*/ + +#include "argList.H" +#include "labelList.H" +#include "FixedList.H" +#include "sliceRange.H" + +using namespace Foam; + + +typedef FixedList sliceCoeffs; + +void printInfo(const sliceCoeffs& coeffs) +{ + sliceRange range(coeffs); + + Info<< nl + << "coeffs: " << coeffs << nl + << "range: " << range << nl + << "first: " << range.first() << nl + << "*begin " << *range.begin() << nl + << "last: " << range.last() << nl + << "*end " << *range.end() << nl + << "values: " << flatOutput(range.labels()) << nl; + + Info<< "for :"; + for (const label val : range) + { + Info<< ' ' << val; + } + Info<< nl; + + if (range.empty()) + { + Info<< "empty"<< nl; + return; + } + + Info<< "mid-point [" << (range.size()/2) << "] = " + << range[range.size()/2] << nl; + + const auto endIter = range.cend(); + + for (const label i : {-1, (range.size()/2), range.size()}) + { + const auto iter = range.at(i); + + Info<< "at(" << i << ") = " << *iter; + + if (iter == endIter) + { + Info<< " (out-of-range)"; + } + + Info<< nl; + } + + // Copy construct + sliceRange range2(range); + + // Copy assign + range2 = range; +} + + +// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // +// Main program: + +int main(int argc, char *argv[]) +{ + argList::noBanner(); + argList::noParallel(); + argList::noFunctionObjects(); + + argList args(argc, argv, false, true); + + + for + ( + sliceCoeffs coeffs : + { + sliceCoeffs{25, 8, 2}, + sliceCoeffs{15, 5, 3}, + sliceCoeffs{15, -5, 2}, + } + ) + { + printInfo(coeffs); + } + + + // Generator + { + sliceRange range(25, 8, 3); + + Info<< "Generator for " << range << nl; + + auto gen = range.generator(); + + for (label i=0; i < 10; ++i) + { + Info<< " " << gen() << nl; + } + } + + Info<< "\nEnd\n" << endl; + + return 0; +} + +// ************************************************************************* // diff --git a/src/OpenFOAM/Make/files b/src/OpenFOAM/Make/files index b3c9ecfdcc..0e7ae03ba7 100644 --- a/src/OpenFOAM/Make/files +++ b/src/OpenFOAM/Make/files @@ -147,6 +147,7 @@ $(ranges)/labelRange/labelRange.C $(ranges)/labelRange/labelRanges.C $(ranges)/scalarRange/scalarRange.C $(ranges)/scalarRange/scalarRanges.C +$(ranges)/sliceRange/sliceRange.C $(ranges)/tableBounds/tableBounds.C spatialVectorAlgebra = primitives/spatialVectorAlgebra diff --git a/src/OpenFOAM/primitives/ranges/sliceRange/sliceRange.C b/src/OpenFOAM/primitives/ranges/sliceRange/sliceRange.C new file mode 100644 index 0000000000..0a39749b26 --- /dev/null +++ b/src/OpenFOAM/primitives/ranges/sliceRange/sliceRange.C @@ -0,0 +1,78 @@ +/*---------------------------------------------------------------------------*\ + ========= | + \\ / F ield | OpenFOAM: The Open Source CFD Toolbox + \\ / O peration | + \\ / A nd | Copyright (C) 2019 OpenCFD Ltd. + \\/ M anipulation | +------------------------------------------------------------------------------- +License + This file is part of OpenFOAM. + + OpenFOAM is free software: you can redistribute it and/or modify it + under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + OpenFOAM is distributed in the hope that it will be useful, but WITHOUT + ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + for more details. + + You should have received a copy of the GNU General Public License + along with OpenFOAM. If not, see . + +\*---------------------------------------------------------------------------*/ + +#include "sliceRange.H" +#include "FixedList.H" +#include "List.H" +#include "token.H" +#include + +// * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * // + +Foam::sliceRange::sliceRange(const FixedList& coeffs) +: + start_(coeffs[0]), + size_(std::max(0,coeffs[1])), // No negative size + stride_(std::max(0,coeffs[2])) // No negative stride +{} + + +// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * // + +Foam::List Foam::sliceRange::labels() const +{ + List