openfoam/applications/test/labelRanges/Test-labelRanges.C
Mark Olesen c65e2e580d ENH: add some standard templates and macros into stdFoam.H
- some functionality similar to what the standary library <iterator>
  provides.

  * stdFoam::begin() and stdFoam::end() do type deduction,
    which means that many cases it is possible to manage these types
    of changes.

    For example, when managing a number of indices:
       Map<labelHashSet> lookup;

    1) Longhand:

        for
        (
            Map<labelHashSet>::const_iterator iter = lookup.begin();
            iter != lookup.end();
            ++iter
        )
        { .... }

    1b) The same, but wrapped via a macro:

        forAllConstIter(Map<labelHashSet>, lookup, iter)
        { .... }

    2) Using stdFoam begin/end templates directly

        for
        (
            auto iter = stdFoam::begin(lookup);
            iter != stdFoam::end(lookup);
            ++iter
        )
        { .... }

    2b) The same, but wrapped via a macro:

        forAllConstIters(lookup, iter)
        { .... }

Note that in many cases it is possible to simply use a range-based for.
Eg,
     labelList myList;

     for (auto val : myList)
     { ... }

     for (const auto& val : myList)
     { ... }

These however will not work with any of the OpenFOAM hash-tables,
since the standard C++ concept of an iterator would return a key,value
pair when deferencing the *iter.

The deduction methods also exhibits some slightly odd behaviour with
some PtrLists (needs some more investigation).
2017-04-29 22:28:16 +02:00

120 lines
3.1 KiB
C

/*---------------------------------------------------------------------------*\
========= |
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration |
\\ / A nd | Copyright (C) 2011 OpenFOAM Foundation
\\/ M anipulation | Copyright (C) 2017 OpenCFD Ltd.
-------------------------------------------------------------------------------
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 <http://www.gnu.org/licenses/>.
Application
Description
Test label ranges
\*---------------------------------------------------------------------------*/
#include "argList.H"
#include "labelRanges.H"
using namespace Foam;
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
// Main program:
int main(int argc, char *argv[])
{
argList::noParallel();
argList::noFunctionObjects();
argList::validArgs.insert("start size .. startN sizeN");
argList::addOption("verbose");
argList::addNote
(
"The default is to add ranges, use 'add' and 'del' to toggle\n\n"
"Eg, 0 10 30 10 del 20 15"
);
argList args(argc, argv, false, true);
if (args.optionFound("verbose"))
{
labelRange::debug = 1;
}
labelRange range;
labelRanges ranges;
bool removeMode = false;
for (label argI=1; argI < args.size()-1; ++argI)
{
if (args[argI] == "add")
{
removeMode = false;
continue;
}
else if (args[argI] == "del")
{
removeMode = true;
continue;
}
{
label start = args.argRead<label>(argI);
label size = args.argRead<label>(argI+1);
++argI;
range.reset(start, size);
}
Info<< "---------------" << nl;
if (removeMode)
{
Info<< "del " << range << " :";
for (auto i : range)
{
Info<< " " << i;
}
Info<< nl;
ranges.remove(range);
}
else
{
Info<< "add " << range << " :";
for (auto i : range)
{
Info<< " " << i;
}
Info<< nl;
ranges.add(range);
}
Info<< "<list>" << ranges << "</list>" << nl
<< "content:";
for (auto i : ranges)
{
Info<< " " << i;
}
Info<< nl;
}
return 0;
}
// ************************************************************************* //