Commit 8917b944 authored by Mark Olesen's avatar Mark Olesen
Browse files

ENH: add labelRange labels() method (for a labelList source)

- add range support in globalIndex and SubField, SubDimensionedField
parent dd67b338
......@@ -66,7 +66,7 @@ public:
// Constructors
//- Construct from UList and sub-list size
//- Construct from UList and sub-list size, start at 0
inline SubList
(
const UList<T>& list,
......@@ -91,7 +91,7 @@ public:
);
//- Construct from UList and a (start,size) range, but bypassing
// run-time range checking.
//- run-time range checking.
inline SubList
(
const labelRange& range,
......@@ -99,7 +99,7 @@ public:
);
// Member operators
// Member Operators
//- Allow cast to a const List<T>&
inline operator const Foam::List<T>&() const;
......
......@@ -70,10 +70,7 @@ public:
// Constructors
//- Construct from a SubField
inline SubDimensionedField
(
const SubField<Type>& slist
);
inline SubDimensionedField(const SubField<Type>& sfield);
//- Construct from a UList and size
inline SubDimensionedField
......@@ -90,6 +87,24 @@ public:
const label startIndex
);
//- Construct from UList and a (start,size) range.
// The range is subsetted with the list size itself to ensure that the
// result always addresses a valid section of the list.
inline SubDimensionedField
(
const UList<Type>& list,
const labelRange& range
);
//- Construct from UList and a (start,size) range, but bypassing
//- run-time range checking.
inline SubDimensionedField
(
const labelRange& range,
const UList<Type>& list
);
//- Construct as copy
inline SubDimensionedField
(
......
......@@ -28,12 +28,13 @@ License
template<class Type, class GeoMesh>
inline Foam::SubDimensionedField<Type, GeoMesh>::SubDimensionedField
(
const SubField<Type>& slist
const SubField<Type>& sfield
)
:
SubField<Type>(slist)
SubField<Type>(sfield)
{}
template<class Type, class GeoMesh>
inline Foam::SubDimensionedField<Type, GeoMesh>::SubDimensionedField
(
......@@ -57,6 +58,28 @@ inline Foam::SubDimensionedField<Type, GeoMesh>::SubDimensionedField
{}
template<class Type, class GeoMesh>
inline Foam::SubDimensionedField<Type, GeoMesh>::SubDimensionedField
(
const UList<Type>& list,
const labelRange& range
)
:
SubField<Type>(list, range)
{}
template<class Type, class GeoMesh>
inline Foam::SubDimensionedField<Type, GeoMesh>::SubDimensionedField
(
const labelRange& range,
const UList<Type>& list
)
:
SubField<Type>(range, list)
{}
template<class Type, class GeoMesh>
inline Foam::SubDimensionedField<Type, GeoMesh>::SubDimensionedField
(
......
......@@ -3,7 +3,7 @@
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration |
\\ / A nd | Copyright (C) 2011-2016 OpenFOAM Foundation
\\/ M anipulation |
\\/ M anipulation | Copyright (C) 2018 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
......@@ -48,7 +48,7 @@ SourceFiles
namespace Foam
{
//- Pre-declare SubField and related Field type
// Forward declarations
template<class Type> class Field;
template<class Type> class SubField;
......@@ -71,20 +71,20 @@ public:
// Constructors
//- Construct from a SubList
inline SubField(const SubList<Type>&);
//- Copy construct from a SubList
inline SubField(const SubList<Type>& list);
//- Construct from a UList\<Type\>, using the entire size
inline explicit SubField(const UList<Type>&);
//- Construct from a UList, the entire size
inline explicit SubField(const UList<Type>& list);
//- Construct from a UList\<Type\> with a given size
//- Construct from a UList with a given sub-list size, start at 0
inline SubField
(
const UList<Type>& list,
const label subSize
);
//- Construct from a UList\<Type\> with a given size and start index
//- Construct from a UList with a given size and start index
inline SubField
(
const UList<Type>& list,
......@@ -92,11 +92,28 @@ public:
const label startIndex
);
//- Construct as copy
inline SubField(const SubField<Type>&);
//- Construct from UList and a (start,size) range.
// The range is subsetted with the list size itself to ensure that the
// result always addresses a valid section of the list.
inline SubField
(
const UList<Type>& list,
const labelRange& range
);
//- Construct from UList and a (start,size) range, but bypassing
//- run-time range checking.
inline SubField
(
const labelRange& range,
const UList<Type>& list
);
//- Copy construct
inline SubField(const SubField<Type>& sfield);
// Member functions
// Member Functions
//- Return a null SubField
static inline const SubField<Type>& null();
......@@ -110,13 +127,13 @@ public:
// Member operators
//- Assignment via UList operator. Takes linear time.
//- Copy assign via UList operator. Takes linear time.
inline void operator=(const SubField<Type>&);
//- Assignment via UList operator. Takes linear time.
//- Copy assign via UList operator. Takes linear time.
inline void operator=(const Field<Type>&);
//- Assignment via UList operator. Takes linear time.
//- Copy assign via UList operator. Takes linear time.
template<class Form, direction Ncmpts>
inline void operator=(const VectorSpace<Form, Type, Ncmpts>&);
......
......@@ -68,6 +68,28 @@ inline Foam::SubField<Type>::SubField
{}
template<class Type>
inline Foam::SubField<Type>::SubField
(
const UList<Type>& list,
const labelRange& range
)
:
SubList<Type>(list, range)
{}
template<class Type>
inline Foam::SubField<Type>::SubField
(
const labelRange& range,
const UList<Type>& list
)
:
SubList<Type>(range, list)
{}
template<class Type>
inline Foam::SubField<Type>::SubField
(
......
......@@ -31,7 +31,6 @@ Description
globalIndex globalFaces(mesh.nFaces());
label globalFacei = globalFaces.toGlobal(facei);
SourceFiles
globalIndexI.H
globalIndex.C
......@@ -49,9 +48,10 @@ SourceFiles
namespace Foam
{
// Forward declaration of friend functions and operators
// Forward declarations
class globalIndex;
class labelRange;
Istream& operator>>(Istream& is, globalIndex& gi);
Ostream& operator<<(Ostream& os, const globalIndex& gi);
......@@ -76,12 +76,12 @@ public:
//- Construct null
globalIndex() = default;
//- Construct from local max size. Does communication with default
// communicator and message tag.
//- Construct from local max size.
// Does communication with default communicator and message tag.
globalIndex(const label localSize);
//- Construct from local max size. Does communication with given
// communicator and message tag
//- Construct from local max size.
// Does communication with given communicator and message tag
globalIndex
(
const label localSize,
......@@ -113,6 +113,9 @@ public:
//- My local size
inline label localSize() const;
//- Return start/size range of local processor data
inline labelRange range() const;
//- From local to global
inline label toGlobal(const label i) const;
......@@ -132,6 +135,9 @@ public:
//- Size of proci data
inline label localSize(const label proci) const;
//- Return start/size range of proci data
inline labelRange range(const label proci) const;
//- From local to global on proci
inline label toGlobal(const label proci, const label i) const;
......
......@@ -24,6 +24,7 @@ License
\*---------------------------------------------------------------------------*/
#include "ListOps.H"
#include "labelRange.H"
// * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
......@@ -65,6 +66,18 @@ inline Foam::label Foam::globalIndex::localSize() const
}
inline Foam::labelRange Foam::globalIndex::range(const label proci) const
{
return labelRange(offsets_[proci], offsets_[proci+1] - offsets_[proci]);
}
inline Foam::labelRange Foam::globalIndex::range() const
{
return range(Pstream::myProcNo());
}
inline Foam::label Foam::globalIndex::size() const
{
return offsets_.last();
......@@ -87,7 +100,6 @@ inline Foam::label Foam::globalIndex::toGlobal(const label i) const
}
//- Is on local processor
inline bool Foam::globalIndex::isLocal(const label proci, const label i) const
{
return i >= offsets_[proci] && i < offsets_[proci+1];
......@@ -100,19 +112,19 @@ inline bool Foam::globalIndex::isLocal(const label i) const
}
inline Foam::label Foam::globalIndex::toLocal(const label proci, const label i)
const
inline Foam::label
Foam::globalIndex::toLocal(const label proci, const label i) const
{
label localI = i - offsets_[proci];
const label locali = i - offsets_[proci];
if (localI < 0 || i >= offsets_[proci+1])
if (locali < 0 || i >= offsets_[proci+1])
{
FatalErrorInFunction
<< "Global " << i << " does not belong on processor "
<< proci << endl << "Offsets:" << offsets_
<< proci << nl << "Offsets:" << offsets_
<< abort(FatalError);
}
return localI;
return locali;
}
......
......@@ -3,7 +3,7 @@
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration |
\\ / A nd | Copyright (C) 2011 OpenFOAM Foundation
\\/ M anipulation | Copyright (C) 2017 OpenCFD Ltd.
\\/ M anipulation | Copyright (C) 2017-2018 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
......@@ -24,13 +24,15 @@ License
\*---------------------------------------------------------------------------*/
#include "labelRange.H"
#include "List.H"
#include "token.H"
#include <numeric>
// * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
namespace Foam
{
int labelRange::debug(debug::debugSwitch("labelRange", 0));
int labelRange::debug(debug::debugSwitch("labelRange", 0));
}
const Foam::labelRange Foam::labelRange::null;
......@@ -49,6 +51,20 @@ Foam::labelRange::labelRange(Istream& is)
// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
Foam::List<Foam::label> Foam::labelRange::labels() const
{
if (size_ <= 0)
{
return List<label>();
}
List<label> result(size_);
std::iota(result.begin(), result.end(), start_);
return result;
}
void Foam::labelRange::adjust() noexcept
{
if (start_ < 0)
......
......@@ -25,7 +25,7 @@ Class
Foam::labelRange
Description
An range or interval of labels defined by a start and a size.
A range or interval of labels defined by a start and a size.
SourceFiles
labelRange.C
......@@ -46,6 +46,7 @@ namespace Foam
class labelRange;
class Istream;
class Ostream;
template<class T> class List;
Istream& operator>>(Istream& is, labelRange& range);
Ostream& operator<<(Ostream& os, const labelRange& range);
......@@ -136,6 +137,9 @@ public:
//- Is the range empty?
inline bool empty() const noexcept;
//- Return the range as a list of labels
List<label> labels() const;
//- Adjust the start to avoid negative indices.
// The size is decreased accordingly, but will never become negative.
// Eg, adjusting (-10, 15) becomes (0,5).
......@@ -204,8 +208,8 @@ public:
labelRange subset0(const label size) const;
//- Return const_iterator to element at in the range, with bounds
//- checking.
//- Return const_iterator to element in the range,
//- with bounds checking.
// \return iterator at the requested position, or end() if it is
// out of bounds
inline const_iterator at(const label localIndex) const;
......@@ -217,7 +221,7 @@ public:
inline label operator[](const label localIndex) const noexcept;
//- Return true if the (global) value is located within the range.
// Behaviour identical to found().
// Behaviour identical to found() - usable as a predicate
inline bool operator()(const label value) const noexcept;
//- Increase the size by 1.
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment