ENH: add unary functor for Foam::zero

- acts somewhat like an identity op that 'swallows' its parameter
  and maps back to zero.
This commit is contained in:
Mark Olesen 2023-10-20 10:31:57 +02:00
parent 93f48d88ea
commit 1476de89ee
3 changed files with 29 additions and 46 deletions

View File

@ -5,7 +5,7 @@
\\ / A nd | www.openfoam.com
\\/ M anipulation |
-------------------------------------------------------------------------------
Copyright (C) 2017-2021 OpenCFD Ltd.
Copyright (C) 2017-2023 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
@ -81,32 +81,35 @@ int main(int argc, char *argv[])
labelList values(identity(40, -10));
Info<<"words: " << flatOutput(words) << endl;
Info<<"values: " << flatOutput(values) << endl;
Info<< "words: " << flatOutput(words) << endl;
Info<< "values: " << flatOutput(values) << endl;
regExp matcher(".*_.*");
Info<<"With '_': ";
Info<< "With '_': ";
printMatching(words, matcher);
Info<<"All: ";
Info<< "All: ";
printMatching(words, predicates::always());
Info<<"None: ";
Info<< "None: ";
printMatching(words, predicates::never());
Info<<"Neg values: ";
Info<< "Neg values: ";
printMatching(values, [](const label v) { return v < 0; });
Info<<"Even values: ";
Info<< "Even values: ";
printMatching(values, [](const label v) { return !(v % 2); });
Info<<"All: ";
Info<< "All: ";
printMatching(values, predicates::always());
Info<<"None: ";
Info<< "None: ";
printMatching(values, predicates::never());
Info<< "zero:";
Info<< static_cast<label>(Foam::zero{}(values)) << nl;
return 0;
}

View File

@ -73,28 +73,16 @@ public:
//- Return 1 for label
constexpr operator label() const noexcept
{
return 1;
}
constexpr operator label() const noexcept { return 1; }
//- Return 1 for float
constexpr operator float() const noexcept
{
return 1;
}
constexpr operator float() const noexcept { return 1; }
//- Return 1 for double
constexpr operator double() const noexcept
{
return 1;
}
constexpr operator double() const noexcept { return 1; }
//- Component-wise or element-wise access returns one
one operator[](const label) const noexcept
{
return one{};
}
one operator[](const label) const noexcept { return one{}; }
};

View File

@ -77,38 +77,30 @@ public:
//- Return false (0) for bool
constexpr operator bool() const noexcept
{
return false;
}
constexpr operator bool() const noexcept { return false; }
//- Return 0 for label
constexpr operator label() const noexcept
{
return 0;
}
constexpr operator label() const noexcept { return 0; }
//- Return 0 for float
constexpr operator float() const noexcept
{
return 0;
}
constexpr operator float() const noexcept { return 0; }
//- Return 0 for double
constexpr operator double() const noexcept
{
return 0;
}
constexpr operator double() const noexcept { return 0; }
//- Component-wise or element-wise access returns zero
zero operator[](const label) const noexcept
{
return zero{};
}
zero operator[](const label) const noexcept { return zero{}; }
// FUTURE?: Swallow assignment (as per std::ignore)
// template<class T>
// constexpr const zero& operator=(const T&) const { return *this; }
//- Unary functor returns zero
template<class T>
constexpr zero operator()(const T&) const noexcept
{
return zero{};
}
};