sliceRangeI.H
Go to the documentation of this file.
1 /*---------------------------------------------------------------------------*\
2  ========= |
3  \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
4  \\ / O peration |
5  \\ / A nd | www.openfoam.com
6  \\/ M anipulation |
7 -------------------------------------------------------------------------------
8  Copyright (C) 2019 OpenCFD Ltd.
9 -------------------------------------------------------------------------------
10 License
11  This file is part of OpenFOAM.
12 
13  OpenFOAM is free software: you can redistribute it and/or modify it
14  under the terms of the GNU General Public License as published by
15  the Free Software Foundation, either version 3 of the License, or
16  (at your option) any later version.
17 
18  OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
19  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
20  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
21  for more details.
22 
23  You should have received a copy of the GNU General Public License
24  along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
25 
26 \*---------------------------------------------------------------------------*/
27 
28 #include <algorithm>
29 
30 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
31 
32 inline constexpr Foam::sliceRange::sliceRange() noexcept
33 :
34  start_(0),
35  size_(0),
36  stride_(0)
37 {}
38 
39 
41 (
42  const label start,
43  const label size,
44  const label stride
45 ) noexcept
46 :
47  start_(start),
48  size_(std::max(label(0),size)), // No negative size
49  stride_(std::max(label(0),stride)) // No negative stride
50 {}
51 
52 
53 // * * * * * * * * * * * * * * * * Iterators * * * * * * * * * * * * * * * * //
54 
56 (
57  const sliceRange* range,
58  const label i
59 )
60 :
61  stride_(range->stride()),
62  value_
63  (
64  range->start()
65  + ((i < 0 || i > range->size()) ? range->size() : i) * stride_
66  )
67 {}
68 
69 
70 inline void Foam::sliceRange::indexer::next() noexcept
71 {
72  value_ += stride_;
73 }
74 
75 
76 inline void Foam::sliceRange::indexer::next(const label n) noexcept
77 {
78  value_ += (n * stride_);
79 }
80 
81 
83 (
84  const indexer& other
85 ) const noexcept
86 {
87  return (value_ == other.value_);
88 }
89 
90 
91 inline Foam::label Foam::sliceRange::indexer::operator*() const noexcept
92 {
93  return value_;
94 }
95 
96 
98 {
99  const label old(value_);
100  next();
101  return old;
102 }
103 
104 
106 (
107  const sliceRange* range,
108  const label i
109 )
110 :
111  indexer(range, i)
112 {}
113 
114 
117 {
118  next();
119  return *this;
120 }
121 
122 
125 {
126  next(n);
127  return *this;
128 }
129 
130 
133 {
134  next(-1);
135  return *this;
136 }
137 
138 
141 {
142  next(-n);
143  return *this;
144 }
145 
146 
147 inline bool Foam::sliceRange::const_iterator::operator==
148 (
149  const const_iterator& iter
150 ) const noexcept
151 {
152  return equals(iter);
153 }
154 
155 
156 inline bool Foam::sliceRange::const_iterator::operator!=
157 (
158  const const_iterator& iter
159 ) const noexcept
160 {
161  return !equals(iter);
162 }
163 
164 
166 {
167  return const_iterator(this, 0);
168 }
169 
170 
172 {
173  return const_iterator(this, 0);
174 }
175 
176 
178 {
179  return const_iterator(this, -1);
180 }
181 
182 
184 {
185  return const_iterator(this, -1);
186 }
187 
188 
190 {
191  return indexer(this, 0);
192 }
193 
194 
196 Foam::sliceRange::at(const label i) const
197 {
198  return const_iterator(this, i);
199 }
200 
201 
202 // * * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * //
203 
204 inline Foam::label Foam::sliceRange::operator[](const label i) const noexcept
205 {
206  return start_ + stride_ * i;
207 }
208 
209 
210 // ************************************************************************* //
Foam::sliceRange::cbegin
const_iterator cbegin() const
A const_iterator set to the beginning of the range.
Definition: sliceRangeI.H:171
Foam::sliceRange::indexer::indexer
indexer(const sliceRange *range, const label i=0)
Construct from range at given local index.
Definition: sliceRangeI.H:56
Foam::sliceRange::indexer::operator*
label operator*() const noexcept
Return the value.
Definition: sliceRangeI.H:91
Foam::sliceRange::const_iterator
Forward iterator with const access.
Definition: sliceRange.H:212
Foam::sliceRange::end
const const_iterator end() const
A const_iterator set to 1 beyond the end of the range.
Definition: sliceRangeI.H:177
Foam::sliceRange::const_iterator::const_iterator
const_iterator(const sliceRange *range, const label i=0)
Construct from range at given local index.
Definition: sliceRangeI.H:106
Foam::sliceRange::at
const_iterator at(const label i) const
Definition: sliceRangeI.H:196
Foam::sliceRange::indexer
A value indexer, for iteration or generation.
Definition: sliceRange.H:169
Foam::sliceRange::generator
indexer generator() const
Return a forward values generator.
Definition: sliceRangeI.H:189
n
label n
Definition: TABSMDCalcMethod2.H:31
Foam::sliceRange::cend
const const_iterator cend() const
A const_iterator set to 1 beyond the end of the range.
Definition: sliceRangeI.H:183
Foam::sliceRange::operator[]
label operator[](const label i) const noexcept
Return element in the range, without bounds checking.
Definition: sliceRangeI.H:204
Foam::sliceRange::const_iterator::operator-=
const_iterator & operator-=(const label n) noexcept
Arbitrary decrement, no checking.
Definition: sliceRangeI.H:140
Foam::max
label max(const labelHashSet &set, label maxValue=labelMin)
Find the max value in labelHashSet, optionally limited by second argument.
Definition: hashSets.C:47
Foam::sliceRange::begin
const_iterator begin() const
A const_iterator set to the beginning of the range.
Definition: sliceRangeI.H:165
Foam::sliceRange::indexer::next
void next() noexcept
Forward increment, no checking.
Definition: sliceRangeI.H:70
Foam::sliceRange::indexer::operator()
label operator()()
Apply a postfix increment and return the current value.
Definition: sliceRangeI.H:97
Foam::sliceRange
A set of labels defined by a start, a length and a stride.
Definition: sliceRange.H:56
Foam::sliceRange::sliceRange
constexpr sliceRange() noexcept
An empty slice (0,0,0)
Definition: sliceRangeI.H:32
range
scalar range
Definition: LISASMDCalcMethod1.H:12
Foam::sliceRange::indexer::equals
bool equals(const indexer &other) const noexcept
Test for equality of values, not stride.
Definition: sliceRangeI.H:83
Foam::sliceRange::const_iterator::operator++
const_iterator & operator++() noexcept
Prefix increment, no checking.
Definition: sliceRangeI.H:116
Foam::sliceRange::const_iterator::operator--
const_iterator & operator--() noexcept
Prefix decrement, no checking.
Definition: sliceRangeI.H:132
Foam::sliceRange::const_iterator::operator+=
const_iterator & operator+=(const label n) noexcept
Arbitrary increment, no checking.
Definition: sliceRangeI.H:124