diff --git a/applications/test/List/Test-List.C b/applications/test/List/Test-List.C index 9ab2732c2e68ff4e9bb496aa64d27a11720b421f..d68bc4d7baa108673feab7951d386b3904a09317 100644 --- a/applications/test/List/Test-List.C +++ b/applications/test/List/Test-List.C @@ -42,6 +42,7 @@ See also #include "vector.H" #include "labelRange.H" +#include "scalarList.H" #include "ListOps.H" #include "SubList.H" @@ -144,6 +145,18 @@ int main(int argc, char *argv[]) labelList longLabelList = identity(15); + // This does not work: + // scalarList slist = identity(15); + // + // More writing, but does work: + scalarList slist + ( + labelRange::null.begin(), + labelRange::identity(15).end() + ); + + Info<<"scalar identity:" << flatOutput(slist) << endl; + Info<< "labels (contiguous=" << contiguous<label>() << ")" << nl; Info<< "normal: " << longLabelList << nl; @@ -220,7 +233,32 @@ int main(int argc, char *argv[]) } Info<< "sub-sorted: " << flatOutput(longLabelList) << nl; - // Info<<"Slice=" << longLabelList[labelRange(23,5)] << nl; + // construct from a label-range + labelRange range(25,15); + + labelList ident(range.begin(), range.end()); + Info<<"range-list (label)=" << ident << nl; + + List<scalar> sident(range.begin(), range.end()); + Info<<"range-list (scalar)=" << sident << nl; + + // Sub-ranges also work + List<scalar> sident2(range(3), range(10)); + Info<<"range-list (scalar)=" << sident2 << nl; + + // VERY BAD IDEA: List<scalar> sident3(range(10), range(3)); + + // This doesn't work, and don't know what it should do anyhow + // List<vector> vident(range.begin(), range.end()); + // Info<<"range-list (vector)=" << vident << nl; + + // Even weird things like this + List<scalar> sident4 + ( + labelRange().begin(), + labelRange::identity(8).end() + ); + Info<<"range-list (scalar)=" << sident4 << nl; } wordReList reLst; diff --git a/applications/test/labelRanges/Test-labelRanges.C b/applications/test/labelRanges/Test-labelRanges.C index ec2d15233900b2cf6abd25678dfcda3cc474f3d0..5acb0d296c134ed5aa632fe4dceaf98d260715f7 100644 --- a/applications/test/labelRanges/Test-labelRanges.C +++ b/applications/test/labelRanges/Test-labelRanges.C @@ -58,7 +58,7 @@ int main(int argc, char *argv[]) Info<<"test sorting" << endl; DynamicList<labelRange> list1(10); list1.append(labelRange(25, 8)); - list1.append(labelRange(0, 10)); + list1.append(labelRange::identity(8)); list1.append(labelRange(15, 5)); list1.append(labelRange(50, -10)); diff --git a/src/OpenFOAM/primitives/ranges/labelRange/labelRange.C b/src/OpenFOAM/primitives/ranges/labelRange/labelRange.C index b1033ac17279b4bf00810ac80b6ede0fe51e63aa..38349e92b2f0892a9ea65e85d7ecf097666b9a29 100644 --- a/src/OpenFOAM/primitives/ranges/labelRange/labelRange.C +++ b/src/OpenFOAM/primitives/ranges/labelRange/labelRange.C @@ -34,6 +34,8 @@ namespace Foam int labelRange::debug(debug::debugSwitch("labelRange", 0)); } +const Foam::labelRange Foam::labelRange::null; + // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * // diff --git a/src/OpenFOAM/primitives/ranges/labelRange/labelRange.H b/src/OpenFOAM/primitives/ranges/labelRange/labelRange.H index c4eb8488a62b143cea7ba469ec637804892f35be..dc3483c109618bcf06dde89c95b78912b65eaeff 100644 --- a/src/OpenFOAM/primitives/ranges/labelRange/labelRange.H +++ b/src/OpenFOAM/primitives/ranges/labelRange/labelRange.H @@ -35,6 +35,7 @@ SourceFiles #define labelRange_H #include "label.H" +#include <iterator> // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // @@ -63,26 +64,32 @@ class labelRange public: - static int debug; + // Static Data Members + + static int debug; + + //- An empty range with start=0, size=0. + static const labelRange null; + // STL type definitions similar to what UList has //- Type of values the range contains typedef label value_type; - //- The type that can represent the difference between two iterators - typedef label difference_type; - //- The type that can represent the size of the range typedef label size_type; + // Forward declaration + class const_iterator; + // Constructors //- An empty range with zero for start/size. inline labelRange(); - //- Construct a range from start and size, enforcing non-negative size. + //- Construct a range from start/size, enforcing non-negative size. // Optionally adjust the start to avoid any negative indices. inline labelRange ( @@ -95,6 +102,12 @@ public: labelRange(Istream& is); + // Static Member Functions + + //- An identity range with range[i] == i. + inline static labelRange identity(const label len); + + // Member Functions //- Adjust start position @@ -184,6 +197,9 @@ public: //- Return element in the range, no bounds checking inline label operator[](const label localIndex) const; + //- Return const_iterator to element in the range + inline const_iterator operator()(const label localIndex) const; + //- Increase the size by 1. inline label operator++(); inline label operator++(int); @@ -197,21 +213,30 @@ public: //- Forward iterator with const access class const_iterator + : + public std::iterator + < + std::input_iterator_tag, + label, + label, + const label*, + const label& + > { - //- The current label (not the local index) - label index_; + //- The current (global) value + label value_; public: // Constructors //- Construct from range at given local index. - // A negative index signals the 'end' position - inline const_iterator(const labelRange* range, const label i = 0); + // A negative index is invalid and corresponds to the 'end' + inline const_iterator(const labelRange* range, const label i=0); // Member operators - //- Return the current label + //- Return the current (global) value inline label operator*() const; inline const_iterator& operator++(); diff --git a/src/OpenFOAM/primitives/ranges/labelRange/labelRangeI.H b/src/OpenFOAM/primitives/ranges/labelRange/labelRangeI.H index 0563c1cd22f100df318029dbadff834d11e3d406..dc0e90ae85577ebe492a465e45e9622a4c20d3a8 100644 --- a/src/OpenFOAM/primitives/ranges/labelRange/labelRangeI.H +++ b/src/OpenFOAM/primitives/ranges/labelRange/labelRangeI.H @@ -64,7 +64,7 @@ inline Foam::labelRange::const_iterator::const_iterator const label i ) : - index_ + value_ ( range->start() + ((i < 0 || i > range->size()) ? range->size() : i) @@ -74,14 +74,14 @@ inline Foam::labelRange::const_iterator::const_iterator inline Foam::label Foam::labelRange::const_iterator::operator*() const { - return index_; + return value_; } inline Foam::labelRange::const_iterator& Foam::labelRange::const_iterator::operator++() { - ++index_; + ++value_; return *this; } @@ -90,7 +90,7 @@ inline Foam::labelRange::const_iterator Foam::labelRange::const_iterator::operator++(int) { const_iterator old = *this; - ++index_; + ++value_; return old; } @@ -100,7 +100,7 @@ inline bool Foam::labelRange::const_iterator::operator== const const_iterator& iter ) const { - return (this->index_ == iter.index_); + return (this->value_ == iter.value_); } @@ -109,7 +109,7 @@ inline bool Foam::labelRange::const_iterator::operator!= const const_iterator& iter ) const { - return (this->index_ != iter.index_); + return (this->value_ != iter.value_); } @@ -139,6 +139,12 @@ inline const Foam::labelRange::const_iterator Foam::labelRange::cend() const // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * // +inline Foam::labelRange Foam::labelRange::identity(const label len) +{ + return labelRange(0, len); +} + + inline void Foam::labelRange::setStart(const label i) { start_ = i; @@ -264,6 +270,13 @@ inline Foam::label Foam::labelRange::operator[](const label localIndex) const } +inline Foam::labelRange::const_iterator +Foam::labelRange::operator()(const label localIndex) const +{ + return const_iterator(this, localIndex); +} + + inline Foam::label Foam::labelRange::operator++() { return ++size_;