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>
constexpr bool operator()(const Int val) const noexcept
{
return val > 0;
return (val > 0);
}
};
@ -109,7 +109,7 @@ public:
template<class Int>
constexpr bool operator()(const Int val) const noexcept
{
return val < 0;
return (val < 0);
}
};
@ -119,7 +119,7 @@ public:
template<class Int>
constexpr bool operator()(const Int val) const noexcept
{
return val >= 0;
return (val >= 0);
}
};
@ -129,7 +129,7 @@ public:
template<class Int>
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
\\/ M anipulation |
-------------------------------------------------------------------------------
Copyright (C) 2020 OpenCFD Ltd.
Copyright (C) 2020-2023 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
@ -36,20 +36,6 @@ License
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>
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)
{
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)
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

View File

@ -29,6 +29,7 @@ License
#include "labelRange.H"
#include "List.H"
#include "MinMax.H"
#include "Pair.H"
#include <numeric>
// * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
@ -45,10 +46,24 @@ Foam::labelRange::labelRange(const MinMax<label>& range) noexcept
:
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();
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
{
if (size() < 0)
{
// Skip this check?
return List<label>();
}
List<label> result;
List<label> result(this->size());
std::iota(result.begin(), result.end(), this->start());
if (this->size() > 0)
{
result.resize(this->size());
std::iota(result.begin(), result.end(), this->start());
}
return result;
}

View File

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