ENH: construct labelRange from begin/end value pair

This commit is contained in:
Mark Olesen
2023-10-26 09:44:15 +02:00
parent 1faa143a7c
commit ef92d31493
4 changed files with 56 additions and 34 deletions

View File

@ -99,7 +99,7 @@ public:
template<class Int> template<class Int>
constexpr bool operator()(const Int val) const noexcept constexpr bool operator()(const Int val) const noexcept
{ {
return val > 0; return (val > 0);
} }
}; };
@ -109,7 +109,7 @@ public:
template<class Int> template<class Int>
constexpr bool operator()(const Int val) const noexcept constexpr bool operator()(const Int val) const noexcept
{ {
return val < 0; return (val < 0);
} }
}; };
@ -119,7 +119,7 @@ public:
template<class Int> template<class Int>
constexpr bool operator()(const Int val) const noexcept constexpr bool operator()(const Int val) const noexcept
{ {
return val >= 0; return (val >= 0);
} }
}; };
@ -129,7 +129,7 @@ public:
template<class Int> template<class Int>
constexpr bool operator()(const Int val) const noexcept constexpr bool operator()(const Int val) const noexcept
{ {
return val <= 0; return (val <= 0);
} }
}; };

View File

@ -5,7 +5,7 @@
\\ / A nd | www.openfoam.com \\ / A nd | www.openfoam.com
\\/ M anipulation | \\/ M anipulation |
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
Copyright (C) 2020 OpenCFD Ltd. Copyright (C) 2020-2023 OpenCFD Ltd.
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
License License
This file is part of OpenFOAM. This file is part of OpenFOAM.
@ -36,20 +36,6 @@ License
namespace Foam namespace Foam
{ {
template<class T>
inline static List<label> makeIdentity(const IntRange<T>& range)
{
if (range.size() < 1)
{
return List<label>();
}
List<label> result(range.size());
std::iota(result.begin(), result.end(), range.start());
return result;
}
template<class T> template<class T>
inline static Istream& input(Istream& is, IntRange<T>& range) inline static Istream& input(Istream& is, IntRange<T>& range)
{ {
@ -79,14 +65,30 @@ inline static Ostream& output(Ostream& os, const IntRange<T>& range)
Foam::List<Foam::label> Foam::identity(const IntRange<int32_t>& range) Foam::List<Foam::label> Foam::identity(const IntRange<int32_t>& range)
{ {
return makeIdentity(range); List<label> result;
if (range.size() > 0)
{
result.resize(range.size());
std::iota(result.begin(), result.end(), range.start());
}
return result;
} }
#if defined(WM_LABEL_SIZE) && (WM_LABEL_SIZE >= 64) #if defined(WM_LABEL_SIZE) && (WM_LABEL_SIZE >= 64)
Foam::List<Foam::label> Foam::identity(const IntRange<int64_t>& range) Foam::List<Foam::label> Foam::identity(const IntRange<int64_t>& range)
{ {
return makeIdentity(range); List<label> result;
if (range.size() > 0)
{
result.resize(range.size());
std::iota(result.begin(), result.end(), range.start());
}
return result;
} }
#endif #endif

View File

@ -29,6 +29,7 @@ License
#include "labelRange.H" #include "labelRange.H"
#include "List.H" #include "List.H"
#include "MinMax.H" #include "MinMax.H"
#include "Pair.H"
#include <numeric> #include <numeric>
// * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * // // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
@ -45,10 +46,24 @@ Foam::labelRange::labelRange(const MinMax<label>& range) noexcept
: :
labelRange() labelRange()
{ {
if (range.min() < range.max()) if (range.min() <= range.max())
{ {
// max is inclusive, so size with +1. Hope for no overflow
start() = range.min(); start() = range.min();
size() = (range.max() - range.min()); // Hope for no overflow? size() = 1 + (range.max() - range.min());
}
}
Foam::labelRange::labelRange(const Pair<label>& start_end) noexcept
:
labelRange()
{
if (start_end.first() <= start_end.second())
{
// second is exclusive, so size directly. Hope for no overflow
start() = start_end.first();
size() = (start_end.second() - start_end.first());
} }
} }
@ -65,14 +80,13 @@ Foam::labelRange::labelRange(Istream& is)
Foam::List<Foam::label> Foam::labelRange::labels() const Foam::List<Foam::label> Foam::labelRange::labels() const
{ {
if (size() < 0) List<label> result;
{
// Skip this check?
return List<label>();
}
List<label> result(this->size()); if (this->size() > 0)
std::iota(result.begin(), result.end(), this->start()); {
result.resize(this->size());
std::iota(result.begin(), result.end(), this->start());
}
return result; return result;
} }

View File

@ -6,7 +6,7 @@
\\/ M anipulation | \\/ M anipulation |
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
Copyright (C) 2011 OpenFOAM Foundation Copyright (C) 2011 OpenFOAM Foundation
Copyright (C) 2017-2022 OpenCFD Ltd. Copyright (C) 2017-2023 OpenCFD Ltd.
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
License License
This file is part of OpenFOAM. This file is part of OpenFOAM.
@ -48,6 +48,7 @@ namespace Foam
// Forward Declarations // Forward Declarations
template<class T> class MinMax; template<class T> class MinMax;
template<class T> class Pair;
/*---------------------------------------------------------------------------*\ /*---------------------------------------------------------------------------*\
Class labelRange Declaration Class labelRange Declaration
@ -98,11 +99,16 @@ public:
const bool adjustStart const bool adjustStart
) noexcept; ) noexcept;
//- Construct from a min/max range, enforces non-negative size. //- Construct from a min/max range (both inclusive),
//- Does not adjust the start. //- enforces non-negative size.
// Passing an invalid min/max range results in an empty labelRange // Passing an invalid range results in an empty labelRange
explicit labelRange(const MinMax<label>& range) noexcept; explicit labelRange(const MinMax<label>& range) noexcept;
//- Construct from start (inclusive) and end (exclusive) values,
//- enforces non-negative size.
// Passing an invalid range results in an empty labelRange
explicit labelRange(const Pair<label>& start_end) noexcept;
//- Construct from Istream. //- Construct from Istream.
explicit labelRange(Istream& is); explicit labelRange(Istream& is);