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-2021 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 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
29 
30 inline constexpr Foam::sliceRange::sliceRange() noexcept
31 :
32  start_(0),
33  size_(0),
34  stride_(0)
35 {}
36 
37 
38 inline constexpr Foam::sliceRange::sliceRange
39 (
40  const label beg,
41  const label len,
42  const label stride
43 ) noexcept
44 :
45  start_(beg),
46  size_(len),
47  stride_(stride)
48 {}
49 
50 
51 // * * * * * * * * * * * * * * * * Iterators * * * * * * * * * * * * * * * * //
52 
54 :
55  stride_(1),
56  value_(0)
57 {}
58 
59 
61 (
62  const label val,
63  const label stride
64 ) noexcept
65 :
66  stride_(stride),
67  value_(val)
68 {}
69 
70 
71 inline Foam::label
74 {
75  const label old(value_);
76  next();
77  return old;
78 }
79 
80 
81 // * * * * * * * * * * * * * * Forward Iterators * * * * * * * * * * * * * * //
82 
83 inline constexpr Foam::label
85 operator[](const label n) const noexcept
86 {
87  return value(n);
88 }
89 
90 
94 {
95  next();
96  return *this;
97 }
98 
99 
102 operator++(int) noexcept
103 {
104  const_iterator old(*this);
105  next();
106  return old;
107 }
108 
109 
113 {
114  prev();
115  return *this;
116 }
117 
118 
121 operator--(int) noexcept
122 {
123  const_iterator old(*this);
124  prev();
125  return old;
126 }
127 
128 
131 operator+=(const label n) noexcept
132 {
133  next(n);
134  return *this;
135 }
136 
137 
140 operator-=(const label n) noexcept
141 {
142  prev(n);
143  return *this;
144 }
145 
146 
147 inline constexpr Foam::sliceRange::const_iterator
149 operator+(const label n) const noexcept
150 {
151  return const_iterator(value(n), stride());
152 }
153 
154 
155 inline constexpr Foam::sliceRange::const_iterator
157 operator-(const label n) const noexcept
158 {
159  return const_iterator(value(-n), stride());
160 }
161 
162 
163 inline constexpr Foam::label
165 operator-(const const_iterator& iter) const noexcept
166 {
167  return (stride() ? (value() - iter.value()) / stride() : label{0});
168 }
169 
170 
171 inline constexpr bool
173 operator==(const const_iterator& iter) const noexcept
174 {
175  return (value() == iter.value());
176 }
177 
178 
179 inline constexpr bool
181 operator<(const const_iterator& iter) const noexcept
182 {
183  return (value() < iter.value());
184 }
185 
186 
187 // * * * * * * * * * * * * * * Reverse Iterators * * * * * * * * * * * * * * //
188 
189 inline constexpr Foam::label
191 operator[](const label n) const noexcept
192 {
193  return value(-n);
194 }
195 
196 
200 {
201  prev();
202  return *this;
203 }
204 
205 
208 operator++(int) noexcept
209 {
210  const_reverse_iterator old(*this);
211  prev();
212  return old;
213 }
214 
215 
219 {
220  next();
221  return *this;
222 }
223 
224 
227 operator--(int) noexcept
228 {
229  const_reverse_iterator old(*this);
230  next();
231  return old;
232 }
233 
234 
237 operator+=(const label n) noexcept
238 {
239  prev(n);
240  return *this;
241 }
242 
243 
246 operator-=(const label n) noexcept
247 {
248  next(n);
249  return *this;
250 }
251 
252 
255 operator+(const label n) const noexcept
256 {
257  return const_reverse_iterator(value(-n), stride());
258 }
259 
260 
263 operator-(const label n) const noexcept
264 {
265  return const_reverse_iterator(value(n), stride());
266 }
267 
268 
269 inline constexpr Foam::label
271 operator-(const const_reverse_iterator& iter) const noexcept
272 {
273  return (stride() ? (iter.value() - value()) / stride() : label{0});
274 }
275 
276 
277 inline constexpr bool
279 operator==(const const_reverse_iterator& iter) const noexcept
280 {
281  return (value() == iter.value());
282 }
283 
284 
285 inline constexpr bool
287 operator<(const const_reverse_iterator& iter) const noexcept
288 {
289  return (iter.value() < value());
290 }
291 
292 
293 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
294 
297 {
298  return indexer(start_, stride_);
299 }
300 
301 
303 Foam::sliceRange::at(const label i) const
304 {
305  return
306  const_iterator
307  (
308  start_ + ((i < 0 || i > size_) ? size_ : i) * stride_,
309  stride_
310  );
311 }
312 
313 
316 {
317  return const_iterator(start_, stride_);
318 }
319 
320 
323 {
324  return const_iterator(start_, stride_);
325 }
326 
327 
330 {
331  return const_iterator(start_ + size_*stride_, stride_);
332 }
333 
334 
337 {
338  return const_iterator(start_ + size_*stride_, stride_);
339 }
340 
341 
344 {
345  return const_reverse_iterator(start_ + (size_-1)*stride_, stride_);
346 }
347 
348 
351 {
352  return const_reverse_iterator(start_ + (size_-1)*stride_, stride_);
353 }
354 
355 
358 {
359  return const_reverse_iterator(start_ - stride_, stride_);
360 }
361 
362 
365 {
366  return const_reverse_iterator(start_ - stride_, stride_);
367 }
368 
369 
370 // ************************************************************************* //
const_iterator end() const noexcept
A const_iterator set to 1 beyond the end of the range.
Definition: sliceRangeI.H:322
constexpr label operator[](const label n) const noexcept
Offset dereference operator.
Definition: sliceRangeI.H:78
const_iterator at(const label i) const
Return const_iterator to a position within the range, with bounds checking.
Definition: sliceRangeI.H:296
constexpr indexer() noexcept
Default construct with zero value and stride = 1.
Definition: sliceRangeI.H:46
label stride() const noexcept
The stride for the range.
Definition: sliceRange.H:163
constexpr const_reverse_iterator operator+(const label n) const noexcept
Return iterator with offset.
Definition: sliceRangeI.H:248
constexpr const_iterator operator-(const label n) const noexcept
Return iterator with offset.
Definition: sliceRangeI.H:150
const_iterator cbegin() const noexcept
A const_iterator set to the beginning of the range.
Definition: sliceRangeI.H:315
constexpr bool operator==(const const_reverse_iterator &iter) const noexcept
Test for equality of values (ignore stride)
Definition: sliceRangeI.H:272
constexpr const_reverse_iterator operator-(const label n) const noexcept
Return iterator with offset.
Definition: sliceRangeI.H:256
A value indexer, for iteration or generation.
Definition: sliceRange.H:271
constexpr bool operator==(const const_iterator &iter) const noexcept
Test for equality of values (ignore stride)
Definition: sliceRangeI.H:166
const_reverse_iterator crend() const noexcept
A const_reverse_iterator set to 1 before the begin of range.
Definition: sliceRangeI.H:357
Bidirectional input iterator with const access.
Definition: sliceRange.H:370
constexpr label operator[](const label n) const noexcept
Offset dereference operator.
Definition: sliceRangeI.H:184
void next() noexcept
Increment value.
Definition: sliceRange.H:342
Bidirectional reverse input iterator with const access.
Definition: sliceRange.H:496
const_iterator & operator--() noexcept
Prefix decrement.
Definition: sliceRangeI.H:105
constexpr bool operator<(const const_reverse_iterator &iter) const noexcept
Reverse compare less-than values (ignore stride)
Definition: sliceRangeI.H:280
const_iterator begin() const noexcept
A const_iterator set to the beginning of the range.
Definition: sliceRangeI.H:308
const_iterator & operator++() noexcept
Prefix increment.
Definition: sliceRangeI.H:86
constexpr sliceRange() noexcept
Default construct an empty slice (0,0,0)
Definition: sliceRangeI.H:23
const_reverse_iterator & operator++() noexcept
Prefix increment.
Definition: sliceRangeI.H:192
const direction noexcept
Definition: Scalar.H:258
const_reverse_iterator & operator+=(const label n) noexcept
Arbitrary increment.
Definition: sliceRangeI.H:230
const_reverse_iterator & operator-=(const label n) noexcept
Arbitrary decrement.
Definition: sliceRangeI.H:239
constexpr bool operator<(const const_iterator &iter) const noexcept
Compare less-than values (ignore stride)
Definition: sliceRangeI.H:174
constexpr const_iterator operator+(const label n) const noexcept
Return iterator with offset.
Definition: sliceRangeI.H:142
const_reverse_iterator rbegin() const noexcept
A const_reverse_iterator set to 1 before the end of range.
Definition: sliceRangeI.H:336
const_reverse_iterator & operator--() noexcept
Prefix decrement.
Definition: sliceRangeI.H:211
const_iterator & operator+=(const label n) noexcept
Arbitrary increment.
Definition: sliceRangeI.H:124
const_iterator & operator-=(const label n) noexcept
Arbitrary decrement.
Definition: sliceRangeI.H:133
label n
const_reverse_iterator rend() const noexcept
A const_reverse_iterator set to 1 before the begin of range.
Definition: sliceRangeI.H:350
label operator()() noexcept
Apply a postfix increment and return the current value.
Definition: sliceRangeI.H:66
const_reverse_iterator crbegin() const noexcept
A const_reverse_iterator set to 1 before the end of range.
Definition: sliceRangeI.H:343
const_iterator cend() const noexcept
A const_iterator set to 1 beyond the end of the range.
Definition: sliceRangeI.H:329
indexer generator() const
Return a forward values generator.
Definition: sliceRangeI.H:289