sliceRange.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 Class
27  Foam::sliceRange
28 
29 Description
30  A set of labels defined by a start, a length and a stride.
31 
32 SourceFiles
33  sliceRange.C
34  sliceRangeI.H
35 
36 \*---------------------------------------------------------------------------*/
37 
38 #ifndef sliceRange_H
39 #define sliceRange_H
40 
41 #include "label.H"
42 #include <iterator>
43 
44 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
45 
46 namespace Foam
47 {
48 
49 // Forward Declarations
50 class Istream;
51 class Ostream;
52 template<class T> class List;
53 template<class T, unsigned N> class FixedList;
54 
55 /*---------------------------------------------------------------------------*\
56  Class sliceRange Declaration
57 \*---------------------------------------------------------------------------*/
58 
59 class sliceRange
60 {
61  // Private Data
62 
63  //- The start of the interval
64  label start_;
65 
66  //- The length of the interval
67  label size_;
68 
69  //- The stride for the interval
70  label stride_;
71 
72 
73 public:
74 
75  // STL type definitions
76 
77  //- Type of values the range contains
78  typedef label value_type;
79 
80  //- The type that can represent the size of the range
81  typedef label size_type;
82 
83  //- Input iterator with const access
84  class const_iterator;
85 
86  //- Reverse input iterator with const access
88 
89 
90  // Generated Methods: copy/move construct, copy/move assignment
91 
92 
93  // Constructors
94 
95  //- Default construct an empty slice (0,0,0)
96  inline constexpr sliceRange() noexcept;
97 
98  //- Construct slice from start/size/stride, no checks
99  inline constexpr sliceRange
100  (
101  const label beg,
102  const label len,
103  const label stride
104  ) noexcept;
105 
106  //- Construct slice from start/size/stride coefficients,
107  //- enforce non-negative size and stride.
108  explicit sliceRange(const FixedList<label,3>& coeffs);
109 
110 
111  // Member Functions
112 
113  // Access
114 
115  //- The first/start value in the range (inclusive)
116  constexpr label start() const noexcept
117  {
118  return start_;
119  }
120 
121  //- The size of the range
122  constexpr label size() const noexcept
123  {
124  return size_;
125  }
126 
127  //- The stride for the range
128  constexpr label stride() const noexcept
129  {
130  return stride_;
131  }
132 
133  //- True if range is empty (zero-sized)
134  bool empty() const noexcept
135  {
136  return !size_;
137  }
138 
139  //- The first/start value in the range (inclusive)
140  constexpr label first() const noexcept
141  {
142  return start_;
143  }
144 
145  //- The (inclusive) upper value of the range
146  label last() const noexcept
147  {
148  return start_ + (size_-1) * stride_;
149  }
150 
151  //- Return list of labels corresponding to the slice
152  List<label> labels() const;
153 
154 
155  // Member Operators
156 
157  //- Return element in the range, without bounds checking
158  label operator[](const label i) const noexcept
159  {
160  return start_ + i * stride_;
161  }
162 
163  //- True if range is non-empty
164  explicit operator bool() const noexcept { return bool(size_); }
166 
167  // Iteration / Generation
168 
169  //- A value indexer, for iteration or generation
170  class indexer;
171 
172  //- Return a forward values generator
173  inline indexer generator() const;
174 
175  //- Return const_iterator to a position within the range,
176  //- with bounds checking.
177  // \return iterator at the requested position, or end() for
178  // out-of-bounds
179  inline const_iterator at(const label i) const;
180 
181  //- A const_iterator set to the beginning of the range
182  inline const_iterator begin() const noexcept;
183 
184  //- A const_iterator set to the beginning of the range
185  inline const_iterator cbegin() const noexcept;
186 
187  //- A const_iterator set to 1 beyond the end of the range.
188  inline const_iterator cend() const noexcept;
190  //- A const_iterator set to 1 beyond the end of the range.
191  inline const_iterator end() const noexcept;
192 
193  //- A const_reverse_iterator set to 1 before the end of range
194  inline const_reverse_iterator rbegin() const noexcept;
195 
196  //- A const_reverse_iterator set to 1 before the end of range
198 
199  //- A const_reverse_iterator set to 1 before the begin of range
200  inline const_reverse_iterator rend() const noexcept;
201 
202  //- A const_reverse_iterator set to 1 before the begin of range
203  inline const_reverse_iterator crend() const noexcept;
204 
205 
206  // Iterators
207 
208  //- A value indexer, for iteration or generation
209  class indexer
210  {
211  //- The stride when indexing
212  const label stride_;
213 
214  //- The global value
215  label value_;
216 
217  public:
218 
219  // STL definitions (as per std::iterator)
220  typedef label value_type;
221  typedef label difference_type;
222  typedef const label* pointer;
223  typedef label reference;
224 
225 
226  // Constructors
227 
228  //- Default construct with zero value and stride = 1
229  inline constexpr indexer() noexcept;
230 
231  //- Construct with specified value and stride
232  inline constexpr indexer
233  (
234  const label val,
235  const label stride
236  ) noexcept;
237 
238 
239  // Member Functions
240 
241  //- The current value
242  constexpr label value() const noexcept { return value_; }
243 
244  //- The stride
245  constexpr label stride() const noexcept { return stride_; }
246 
247  //- Value with offset
248  constexpr label value(const label n) const noexcept
249  {
250  return value_ + (n * stride_);
251  }
252 
253  //- Decrement value
254  void prev() noexcept { value_ -= stride_; }
255 
256  //- Decrease value
257  void prev(const label n) noexcept { value_ -= (n * stride_); }
258 
259  //- Increment value
260  void next() noexcept { value_ += stride_; }
261 
262  //- Increase value
263  void next(const label n) noexcept { value_ += (n * stride_); }
264 
265 
266  // Member Operators
268  //- Return the value
269  constexpr label operator*() const noexcept { return value_; }
270 
271  //- Apply a postfix increment and return the current value.
272  // This operator definition is required for a generator -
273  // see std::generate()
274  inline label operator()() noexcept;
275  };
276 
277 
278  //- Bidirectional input iterator with const access
279  class const_iterator
280  :
281  public indexer
282  {
283  public:
285  // STL definitions (as per std::iterator)
286  typedef std::random_access_iterator_tag iterator_category;
287 
288 
289  // Constructors
290 
291  //- Inherit constructors from indexer
292  using indexer::indexer;
293 
294 
295  // Member Operators
296 
297  //- Offset dereference operator
298  inline constexpr label operator[](const label n) const noexcept;
299 
300  //- Prefix increment
301  inline const_iterator& operator++() noexcept;
302 
303  //- Postfix increment
304  inline const_iterator operator++(int) noexcept;
305 
306  //- Prefix decrement
307  inline const_iterator& operator--() noexcept;
308 
309  //- Postfix decrement
310  inline const_iterator operator--(int) noexcept;
311 
312  //- Arbitrary increment
313  inline const_iterator& operator+=(const label n) noexcept;
314 
315  //- Arbitrary decrement
316  inline const_iterator& operator-=(const label n) noexcept;
317 
318  //- Return iterator with offset
319  inline constexpr const_iterator operator+
320  (
321  const label n
322  ) const noexcept;
323 
324  //- Return iterator with offset
325  inline constexpr const_iterator operator-
326  (
327  const label n
328  ) const noexcept;
329 
330  //- Difference operator
331  inline constexpr label operator-
332  (
333  const const_iterator& iter
334  ) const noexcept;
335 
336 
337  // Comparison
339  //- Test for equality of values (ignore stride)
340  inline constexpr bool operator==(const const_iterator& iter)
341  const noexcept;
342 
343  //- Compare less-than values (ignore stride)
344  inline constexpr bool operator<(const const_iterator& iter)
345  const noexcept;
346 
347 
348  // Derived comparisons
349 
350  constexpr bool operator!=(const const_iterator& iter)
351  const noexcept
352  {
353  return !(*this == iter);
354  }
355 
356  constexpr bool operator<=(const const_iterator& iter)
357  const noexcept
358  {
359  return !(iter < *this);
360  }
361 
362  constexpr bool operator>(const const_iterator& iter)
363  const noexcept
364  {
365  return (iter < *this);
366  }
367 
368  constexpr bool operator>=(const const_iterator& iter)
369  const noexcept
370  {
371  return !(*this < iter);
372  }
373  };
374 
375 
376  //- Bidirectional reverse input iterator with const access
378  :
379  public indexer
380  {
381  public:
382 
383  // STL definitions (as per std::iterator)
384  typedef std::random_access_iterator_tag iterator_category;
385 
386 
387  // Constructors
388 
389  //- Inherit constructors from indexer
390  using indexer::indexer;
391 
392 
393  // Member Operators
394 
395  //- Offset dereference operator
396  inline constexpr label operator[](const label n) const noexcept;
397 
398  //- Prefix increment
400 
401  //- Postfix increment
403 
404  //- Prefix decrement
406 
407  //- Postfix decrement
409 
410  //- Arbitrary increment
411  inline const_reverse_iterator& operator+=(const label n) noexcept;
412 
413  //- Arbitrary decrement
414  inline const_reverse_iterator& operator-=(const label n) noexcept;
415 
416  //- Return iterator with offset
417  inline constexpr const_reverse_iterator operator+
418  (
419  const label n
420  ) const noexcept;
421 
422  //- Return iterator with offset
423  inline constexpr const_reverse_iterator operator-
424  (
425  const label n
426  ) const noexcept;
427 
428  //- Difference operator
429  inline constexpr label operator-
430  (
431  const const_reverse_iterator& iter
432  ) const noexcept;
433 
434 
435  // Comparison
436 
437  //- Test for equality of values (ignore stride)
438  inline constexpr bool operator==(const const_reverse_iterator& iter)
439  const noexcept;
440 
441  //- Reverse compare less-than values (ignore stride)
442  inline constexpr bool operator<(const const_reverse_iterator& iter)
443  const noexcept;
444 
445 
446  // Derived comparisons
447 
448  constexpr bool operator!=(const const_reverse_iterator& iter)
449  const noexcept
450  {
451  return !(*this == iter);
452  }
453 
454  constexpr bool operator<=(const const_reverse_iterator& iter)
455  const noexcept
456  {
457  return !(iter < *this);
458  }
459 
460  constexpr bool operator>(const const_reverse_iterator& iter)
461  const noexcept
462  {
463  return (iter < *this);
464  }
465 
466  constexpr bool operator>=(const const_reverse_iterator& iter)
467  const noexcept
468  {
469  return !(*this < iter);
470  }
471  };
472 };
473 
474 
475 // * * * * * * * * * * * * * * * IOstream Operators * * * * * * * * * * * * //
476 
477 //- Read sliceRange from Istream as (start size stride) tuple, no checks
479 
480 //- Write sliceRange to Ostream as (start size stride) tuple
482 
483 
484 // * * * * * * * * * * * * * * * Global Operators * * * * * * * * * * * * * //
485 
486 //- Test for equality of begin/size/slice values
487 inline constexpr bool operator==
488 (
489  const sliceRange& a,
490  const sliceRange& b
491 ) noexcept
492 {
493  return
494  (
495  a.first() == b.first()
496  && a.size() == b.size()
497  && a.stride() == b.stride()
498  );
499 }
500 
501 
502 //- Comparison function for sorting, compares the start.
503 // If the start values are equal, compare the sizes.
504 // If the sizes are also equal, compare the strides.
505 inline constexpr bool operator<
506 (
507  const sliceRange& a,
508  const sliceRange& b
509 ) noexcept
510 {
511  return
512  (
513  a.first() < b.first()
514  ||
515  (
516  !(b.first() < a.first())
517  &&
518  (
519  a.size() < b.size()
520  ||
521  (
522  !(b.size() < a.size())
523  && a.stride() < b.stride()
524  )
525  )
526  )
527  );
528 }
529 
530 
531 // Derived comparisons
532 
533 inline constexpr bool operator!=
534 (
535  const sliceRange& a,
536  const sliceRange& b
537 ) noexcept
538 {
539  return !(a == b);
540 }
541 
542 
543 inline constexpr bool operator<=
544 (
545  const sliceRange& a,
546  const sliceRange& b
547 ) noexcept
548 {
549  return !(b < a);
550 }
551 
552 inline constexpr bool operator>
553 (
554  const sliceRange& a,
555  const sliceRange& b
556 ) noexcept
557 {
558  return (b < a);
559 }
560 
561 inline constexpr bool operator>=
562 (
563  const sliceRange& a,
564  const sliceRange& b
565 ) noexcept
566 {
567  return !(a < b);
568 }
569 
570 
571 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
572 
573 } // End namespace Foam
574 
575 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
576 
577 #include "sliceRangeI.H"
578 
579 #endif
580 
581 // ************************************************************************* //
const_iterator end() const noexcept
A const_iterator set to 1 beyond the end of the range.
Definition: sliceRangeI.H:322
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
A 1D vector of objects of type <T> with a fixed length <N>.
Definition: HashTable.H:101
std::random_access_iterator_tag iterator_category
Definition: sliceRange.H:499
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 bool operator>=(const const_reverse_iterator &iter) const noexcept
Definition: sliceRange.H:607
An Istream is an abstract base class for all input systems (streams, files, token lists etc)...
Definition: Istream.H:57
A value indexer, for iteration or generation.
Definition: sliceRange.H:267
bool empty() const noexcept
True if range is empty (zero-sized)
Definition: sliceRange.H:157
bool operator>(const IOstreamOption::versionNumber &a, const IOstreamOption::versionNumber &b) noexcept
Version A newer than B.
const_reverse_iterator crend() const noexcept
A const_reverse_iterator set to 1 before the begin of range.
Definition: sliceRangeI.H:357
List< label > labels() const
Return list of labels corresponding to the slice.
Definition: sliceRange.C:38
label last() const noexcept
The (inclusive) upper value of the range.
Definition: sliceRange.H:173
label size_type
The type that can represent the size of the range.
Definition: sliceRange.H:86
Bidirectional input iterator with const access.
Definition: sliceRange.H:366
constexpr label operator[](const label n) const noexcept
Offset dereference operator.
Definition: sliceRangeI.H:184
bool operator>=(const IOstreamOption::versionNumber &a, const IOstreamOption::versionNumber &b) noexcept
Version A same or newer than B.
scalar range
label value_type
Type of values the range contains.
Definition: sliceRange.H:81
bool operator<=(const IOstreamOption::versionNumber &a, const IOstreamOption::versionNumber &b) noexcept
Version A same or older than B.
Bidirectional reverse input iterator with const access.
Definition: sliceRange.H:492
constexpr label size() const noexcept
The size of the range.
Definition: sliceRange.H:141
tmp< faMatrix< Type > > operator*(const areaScalarField::Internal &, const faMatrix< Type > &)
const dimensionedScalar b
Wien displacement law constant: default SI units: [m.K].
Definition: createFields.H:27
Istream & operator>>(Istream &, directionInfo &)
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
constexpr bool operator!=(const const_reverse_iterator &iter) const noexcept
Definition: sliceRange.H:589
label operator[](const label i) const noexcept
Return element in the range, without bounds checking.
Definition: sliceRange.H:189
constexpr sliceRange() noexcept
Default construct an empty slice (0,0,0)
Definition: sliceRangeI.H:23
A set of labels defined by a start, a length and a stride.
Definition: sliceRange.H:54
const_reverse_iterator & operator++() noexcept
Prefix increment.
Definition: sliceRangeI.H:192
An Ostream is an abstract base class for all output systems (streams, files, token lists...
Definition: Ostream.H:55
const direction noexcept
Definition: Scalar.H:258
OBJstream os(runTime.globalPath()/outputName)
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
Ostream & operator<<(Ostream &, const boundaryPatch &p)
Write boundaryPatch as dictionary entries (without surrounding braces)
Definition: boundaryPatch.C:76
const_reverse_iterator rbegin() const noexcept
A const_reverse_iterator set to 1 before the end of range.
Definition: sliceRangeI.H:336
constexpr label first() const noexcept
The first/start value in the range (inclusive)
Definition: sliceRange.H:165
const_reverse_iterator & operator--() noexcept
Prefix decrement.
Definition: sliceRangeI.H:211
constexpr label start() const noexcept
The first/start value in the range (inclusive)
Definition: sliceRange.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
constexpr bool operator>(const const_reverse_iterator &iter) const noexcept
Definition: sliceRange.H:601
const_reverse_iterator crbegin() const noexcept
A const_reverse_iterator set to 1 before the end of range.
Definition: sliceRangeI.H:343
constexpr bool operator<=(const const_reverse_iterator &iter) const noexcept
Definition: sliceRange.H:595
Namespace for OpenFOAM.
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
constexpr label stride() const noexcept
The stride for the range.
Definition: sliceRange.H:149