STYLE: add reset() method for labelRange for symmetry with MinMax

This commit is contained in:
Mark Olesen
2023-07-24 12:14:08 +02:00
parent 779c3fe11e
commit 3430ab3aa8
10 changed files with 101 additions and 133 deletions

View File

@ -184,11 +184,14 @@ public:
// Edit
//- Reset to zero start and zero size
inline void clear() noexcept;
inline void reset() noexcept;
//- Reset start and length, no checks
inline void reset(const IntType beg, const IntType len) noexcept;
//- Same as reset() - reset to zero start and zero size
void clear() noexcept { reset(); }
//- Set the start position, no checks
inline void setStart(const IntType i) noexcept;

View File

@ -401,7 +401,7 @@ Foam::IntRange<IntType>::crend() const noexcept
// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
template<class IntType>
inline void Foam::IntRange<IntType>::clear() noexcept
inline void Foam::IntRange<IntType>::reset() noexcept
{
start_ = size_ = 0;
}

View File

@ -41,7 +41,7 @@ Description
// on construction
MinMax<scalar> range(values);
range.clear();
range.reset();
range += val;

View File

@ -164,7 +164,7 @@ bool Foam::labelRanges::add(const labelRange& range)
if (currRange.overlaps(nextRange, true))
{
currRange.join(nextRange);
nextRange.clear();
nextRange.reset();
}
else
{

View File

@ -5,7 +5,7 @@
\\ / A nd | www.openfoam.com
\\/ M anipulation |
-------------------------------------------------------------------------------
Copyright (C) 2018-2020 OpenCFD Ltd.
Copyright (C) 2018-2023 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
@ -35,7 +35,7 @@ License
const Foam::scalarRange Foam::scalarRange::always
(
scalarRange::ALWAYS,
scalarRange::rangeTypes::ALWAYS,
-GREAT,
GREAT
);
@ -45,7 +45,7 @@ const Foam::scalarRange Foam::scalarRange::always
bool Foam::scalarRange::parse(const std::string& str, scalarRange& range)
{
range.clear();
range.reset();
const auto colon = str.find(':');
@ -139,7 +139,7 @@ Foam::scalarRange::scalarRange(const MinMax<label>& range) noexcept
:
min_(range.min()),
max_(range.max()),
type_(max_ < min_ ? scalarRange::NONE : scalarRange::GE_LE)
type_(max_ < min_ ? rangeTypes::NONE : rangeTypes::GE_LE)
{}
@ -147,35 +147,35 @@ Foam::scalarRange::scalarRange(const MinMax<scalar>& range) noexcept
:
min_(range.min()),
max_(range.max()),
type_(max_ < min_ ? scalarRange::NONE : scalarRange::GE_LE)
type_(max_ < min_ ? rangeTypes::NONE : rangeTypes::GE_LE)
{}
// * * * * * * * * * * * * * * * Friend Operators * * * * * * * * * * * * * //
// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
Foam::Ostream& Foam::operator<<(Ostream& os, const scalarRange& range)
void Foam::scalarRange::print(Ostream& os) const
{
switch (range.type_)
switch (type_)
{
case scalarRange::EQ:
os << range.min();
case rangeTypes::EQ:
os << min_;
break;
case scalarRange::GE:
case scalarRange::GT:
os << range.min() << ":Inf";
case rangeTypes::GE:
case rangeTypes::GT:
os << min_ << ":Inf";
break;
case scalarRange::LE:
case scalarRange::LT:
os << "-Inf:" << range.max();
case rangeTypes::LE:
case rangeTypes::LT:
os << "-Inf:" << max_;
break;
case scalarRange::GE_LE:
os << range.min() << ':' << range.max();
case rangeTypes::GE_LE:
os << min_ << ':' << max_;
break;
case scalarRange::ALWAYS:
case rangeTypes::ALWAYS:
os << "true";
break;
@ -183,7 +183,12 @@ Foam::Ostream& Foam::operator<<(Ostream& os, const scalarRange& range)
os << "none";
break;
}
}
Foam::Ostream& Foam::operator<<(Ostream& os, const scalarRange& range)
{
range.print(os);
return os;
}

View File

@ -5,7 +5,7 @@
\\ / A nd | www.openfoam.com
\\/ M anipulation |
-------------------------------------------------------------------------------
Copyright (C) 2018-2022 OpenCFD Ltd.
Copyright (C) 2018-2023 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
@ -67,11 +67,11 @@ Ostream& operator<<(Ostream& os, const scalarRange& range);
class scalarRange
{
//- The type of range test to use
enum testType
//- The type of range
enum rangeTypes : unsigned char
{
NONE = 0, //!< Empty or invalid (inverse) range. Never matches.
EQ, //!< A single value test.
EQ, //!< Single value equality
GE, //!< Greater-than-equal.
GT, //!< Greater-than.
LE, //!< Less-than-equal.
@ -89,13 +89,13 @@ class scalarRange
//- The max value of the range
scalar max_;
//- The type of range test
enum testType type_;
//- The range type
enum rangeTypes type_;
//- Construct from components, no checks
inline constexpr scalarRange
(
const testType type,
const rangeTypes type,
const scalar minVal,
const scalar maxVal
) noexcept;
@ -179,9 +179,6 @@ public:
// Member Functions
//- Reset to an empty (inverse, NONE) range.
inline void clear() noexcept;
//- True if range is empty (eg, inverted, NONE)
inline bool empty() const noexcept;
@ -197,6 +194,12 @@ public:
//- The max value of the range.
scalar max() const noexcept { return max_; }
//- Reset to an empty (inverse, NONE) range.
inline void reset() noexcept;
//- Same as reset() - reset to an empty (inverse, NONE) range.
void clear() noexcept { reset(); }
//- A representative (average) value for the range.
// For GE, LE bounds it is the min/max value, respectively.
@ -220,7 +223,10 @@ public:
// IOstream Operators
//- Print information about the range.
//- Print information about the range
void print(Ostream& os) const;
//- Print information about the range
friend Ostream& operator<<(Ostream& os, const scalarRange& range);

View File

@ -29,7 +29,7 @@ License
inline constexpr Foam::scalarRange::scalarRange
(
const scalarRange::testType type,
const scalarRange::rangeTypes type,
const scalar minVal,
const scalar maxVal
) noexcept
@ -42,13 +42,13 @@ inline constexpr Foam::scalarRange::scalarRange
inline constexpr Foam::scalarRange::scalarRange() noexcept
:
scalarRange(scalarRange::NONE, GREAT, -GREAT)
scalarRange(rangeTypes::NONE, GREAT, -GREAT)
{}
inline constexpr Foam::scalarRange::scalarRange(const scalar val) noexcept
:
scalarRange(scalarRange::EQ, val, val)
scalarRange(rangeTypes::EQ, val, val)
{}
@ -58,15 +58,15 @@ inline Foam::scalarRange::scalarRange
const scalar maxVal
) noexcept
:
scalarRange(scalarRange::GE_LE, minVal, maxVal)
scalarRange(rangeTypes::GE_LE, minVal, maxVal)
{
if (maxVal < minVal)
{
clear(); // Inverted - explicitly mark as such
reset(); // Inverted - explicitly mark as such
}
else if (equal(minVal, maxVal))
{
type_ = scalarRange::EQ;
type_ = rangeTypes::EQ;
}
}
@ -74,76 +74,76 @@ inline Foam::scalarRange::scalarRange
inline constexpr Foam::scalarRange
Foam::scalarRange::ge(const scalar minVal) noexcept
{
return scalarRange(scalarRange::GE, minVal, GREAT);
return scalarRange(rangeTypes::GE, minVal, GREAT);
}
inline constexpr Foam::scalarRange
Foam::scalarRange::gt(const scalar minVal) noexcept
{
return scalarRange(scalarRange::GT, minVal, GREAT);
return scalarRange(rangeTypes::GT, minVal, GREAT);
}
inline constexpr Foam::scalarRange
Foam::scalarRange::ge0() noexcept
{
return scalarRange(scalarRange::GE, 0, GREAT);
return scalarRange(rangeTypes::GE, 0, GREAT);
}
inline constexpr Foam::scalarRange
Foam::scalarRange::gt0() noexcept
{
return scalarRange(scalarRange::GT, 0, GREAT);
return scalarRange(rangeTypes::GT, 0, GREAT);
}
inline constexpr Foam::scalarRange
Foam::scalarRange::le(const scalar maxVal) noexcept
{
return scalarRange(scalarRange::LE, -GREAT, maxVal);
return scalarRange(rangeTypes::LE, -GREAT, maxVal);
}
inline constexpr Foam::scalarRange
Foam::scalarRange::lt(const scalar maxVal) noexcept
{
return scalarRange(scalarRange::LT, -GREAT, maxVal);
return scalarRange(rangeTypes::LT, -GREAT, maxVal);
}
inline constexpr Foam::scalarRange
Foam::scalarRange::zero_one() noexcept
{
return scalarRange(scalarRange::GE_LE, 0, 1);
return scalarRange(rangeTypes::GE_LE, 0, 1);
}
// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
inline void Foam::scalarRange::clear() noexcept
inline void Foam::scalarRange::reset() noexcept
{
min_ = GREAT;
max_ = -GREAT;
type_ = scalarRange::NONE;
type_ = rangeTypes::NONE;
}
inline bool Foam::scalarRange::empty() const noexcept
{
return type_ == scalarRange::NONE;
return (type_ == rangeTypes::NONE);
}
inline bool Foam::scalarRange::good() const noexcept
{
return type_ != scalarRange::NONE;
return (type_ != rangeTypes::NONE);
}
inline bool Foam::scalarRange::single() const noexcept
{
return type_ == scalarRange::EQ;
return (type_ == rangeTypes::EQ);
}
@ -151,16 +151,16 @@ inline Foam::scalar Foam::scalarRange::value() const
{
switch (type_)
{
case scalarRange::EQ: // For equals, min and max are identical
case scalarRange::GE:
case scalarRange::GT:
case rangeTypes::EQ: // For equals, min and max are identical
case rangeTypes::GE:
case rangeTypes::GT:
return min_;
case scalarRange::LE:
case scalarRange::LT:
case rangeTypes::LE:
case rangeTypes::LT:
return max_;
case scalarRange::GE_LE:
case rangeTypes::GE_LE:
// Multiply before adding to avoid possible overflow
return (0.5 * min_) + (0.5 * max_);
@ -174,13 +174,13 @@ inline bool Foam::scalarRange::contains(const scalar val) const
{
switch (type_)
{
case scalarRange::EQ: return equal(val, min_);
case scalarRange::GE: return (val >= min_);
case scalarRange::GT: return (val > min_);
case scalarRange::LE: return (val <= max_);
case scalarRange::LT: return (val < max_);
case scalarRange::GE_LE: return (val >= min_ && val <= max_);
case scalarRange::ALWAYS: return true;
case rangeTypes::EQ: return equal(val, min_);
case rangeTypes::GE: return (val >= min_);
case rangeTypes::GT: return (val > min_);
case rangeTypes::LE: return (val <= max_);
case rangeTypes::LT: return (val < max_);
case rangeTypes::GE_LE: return (val >= min_ && val <= max_);
case rangeTypes::ALWAYS: return true;
default: return false;
}
}

View File

@ -5,7 +5,7 @@
\\ / A nd | www.openfoam.com
\\/ M anipulation |
-------------------------------------------------------------------------------
Copyright (C) 2018-2020 OpenCFD Ltd.
Copyright (C) 2018-2023 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
@ -54,7 +54,7 @@ Foam::scalarRanges Foam::scalarRanges::parse
}
else if (report)
{
Info<< "Bad scalar-range parsing: " << s << endl;
InfoErr<< "Bad scalar-range parsing: " << s << endl;
}
}

View File

@ -5,7 +5,7 @@
\\ / A nd | www.openfoam.com
\\/ M anipulation |
-------------------------------------------------------------------------------
Copyright (C) 2018-2020 OpenCFD Ltd.
Copyright (C) 2018-2023 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
@ -68,28 +68,35 @@ public:
constexpr scalarRanges() noexcept = default;
//- Construct by parsing string using the parse() static method
inline explicit scalarRanges
(
const std::string& str,
bool report = true //!< Report when any range fails to parse
);
//- with optional reporting if any range fails to parse
explicit scalarRanges(const std::string& str, bool report = true)
:
List<scalarRange>(scalarRanges::parse(str, report))
{}
// Static Constructors
//- Construct by parsing string for scalar ranges
//- with optional reporting if any range fails to parse.
// The individual items are space, comma or semicolon delimited.
static scalarRanges parse
(
const std::string& str,
bool report = true //!< Report when any range fails to parse
);
static scalarRanges parse(const std::string& str, bool report = true);
// Member Functions
//- True if the value is contained by any of the sub-ranges
inline bool contains(const scalar value) const;
bool contains(const scalar value) const
{
for (const scalarRange& range : *this)
{
if (range.contains(value))
{
return true;
}
}
return false;
}
//- True if the value is matched by any of the sub-ranges
bool match(const scalar value) const { return contains(value); }
@ -108,8 +115,6 @@ public:
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
#include "scalarRangesI.H"
#endif
// ************************************************************************* //

View File

@ -1,52 +1 @@
/*---------------------------------------------------------------------------*\
========= |
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration |
\\ / A nd | www.openfoam.com
\\/ M anipulation |
-------------------------------------------------------------------------------
Copyright (C) 2018-2023 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/>.
\*---------------------------------------------------------------------------*/
// * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
inline Foam::scalarRanges::scalarRanges(const std::string& str, bool report)
:
List<scalarRange>(scalarRanges::parse(str, report))
{}
// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
inline bool Foam::scalarRanges::contains(const scalar val) const
{
for (const scalarRange& range : *this)
{
if (range.contains(val))
{
return true;
}
}
return false;
}
// ************************************************************************* //
#warning File removed - left for old dependency check only