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 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 #ifndef sliceRange_H
38 #define sliceRange_H
39 
40 #include "label.H"
41 #include <iterator>
42 
43 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
44 
45 namespace Foam
46 {
47 
48 // Forward Declarations
49 class Ostream;
50 template<class T> class List;
51 template<class T, unsigned N> class FixedList;
52 
53 /*---------------------------------------------------------------------------*\
54  Class sliceRange Declaration
55 \*---------------------------------------------------------------------------*/
56 
57 class sliceRange
58 {
59 protected:
60 
61  // Protected Data
62 
63  //- The start point for the interval
64  label start_;
65 
66  //- The length of the interval
67  label size_;
68 
69  //- The stride within 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  //- Forward iterator with const access
84  class const_iterator;
85 
86 
87  // Constructors
88 
89  //- Default copy construct
90  sliceRange(const sliceRange&) = default;
91 
92  //- Default move construct
93  sliceRange(sliceRange&&) = default;
94 
95  //- An empty slice (0,0,0)
96  inline constexpr sliceRange() noexcept;
97 
98  //- Construct slice from start/size/stride,
99  //- enforcing non-negative size and stride.
100  inline sliceRange(label start, label size, label stride) noexcept;
101 
102  //- Construct slice from start/size/stride coefficients,
103  //- enforcing non-negative size and stride.
104  explicit sliceRange(const FixedList<label,3>& coeffs);
105 
106 
107  // Member Functions
108 
109  //- Is the range empty?
110  bool empty() const noexcept
111  {
112  return !size_;
113  }
114 
115  //- Is the range non-empty?
116  bool valid() const noexcept
117  {
118  return size_;
119  }
120 
121  //- The (inclusive) lower value of the range
122  constexpr label start() const noexcept
123  {
124  return start_;
125  }
126 
127  //- The size of the range
128  constexpr label size() const noexcept
129  {
130  return size_;
131  }
132 
133  //- The stride for the range
134  constexpr label stride() const noexcept
135  {
136  return stride_;
137  }
138 
139  //- The (inclusive) lower value of the range - same as start()
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 the slice as a list of labels
152  List<label> labels() const;
153 
154 
155  // Member Operators
156 
157  //- Default copy assignment
158  sliceRange& operator=(const sliceRange&) = default;
159 
160  //- Default move assignment
161  sliceRange& operator=(sliceRange&&) = default;
162 
163  //- Return element in the range, without bounds checking
164  inline label operator[](const label i) const noexcept;
165 
166 
167  // Iterators
168 
169  //- A value indexer, for iteration or generation
170  class indexer
171  {
172  //- The stride when indexing
173  const label stride_;
174 
175  //- The global value
176  label value_;
177 
178  public:
179 
180  // Constructors
181 
182  //- Construct from range at given local index.
183  // An out-of-range index (eg, negative) creates an 'end' iterator
184  inline indexer(const sliceRange* range, const label i=0);
185 
186 
187  // Member Functions
188 
189  //- Forward increment, no checking
190  inline void next() noexcept;
191 
192  //- Forward increment, no checking
193  inline void next(const label n) noexcept;
194 
195  //- Test for equality of values, not stride
196  inline bool equals(const indexer& other) const noexcept;
197 
198  public:
199 
200  // Member Operators
201 
202  //- Return the value
203  inline label operator*() const noexcept;
204 
205  //- Apply a postfix increment and return the current value.
206  // This operator definition is required for a generator -
207  // see std::generate()
208  inline label operator()();
209  };
210 
211 
212  //- Forward iterator with const access
213  class const_iterator
214  :
215  protected indexer,
216  public std::iterator
217  <
218  std::input_iterator_tag,
219  label,
220  label,
221  const label*,
222  const label&
223  >
224  {
225  public:
226 
227  // Constructors
228 
229  //- Construct from range at given local index.
230  // An out-of-range index (eg, negative) creates an 'end' iterator
231  inline const_iterator(const sliceRange* range, const label i=0);
232 
233 
234  // Member Operators
235 
236  //- Return the (global) value
237  using indexer::operator*;
238 
239  //- Prefix increment, no checking
240  inline const_iterator& operator++() noexcept;
241 
242  //- Arbitrary increment, no checking
243  inline const_iterator& operator+=(const label n) noexcept;
244 
245  //- Prefix decrement, no checking
246  inline const_iterator& operator--() noexcept;
247 
248  //- Arbitrary decrement, no checking
249  inline const_iterator& operator-=(const label n) noexcept;
250 
251  //- Test for equality of values, not stride
252  inline bool operator==(const const_iterator& iter) const noexcept;
253 
254  //- Test for inequality of values, not stride
255  inline bool operator!=(const const_iterator& iter) const noexcept;
256  };
257 
258 
259  //- A const_iterator set to the beginning of the range
260  // The value returned is guaranteed to be the same as start()
261  inline const_iterator begin() const;
262 
263  //- A const_iterator set to the beginning of the range
264  // The value returned is guaranteed to be the same as start()
265  inline const_iterator cbegin() const;
266 
267  //- A const_iterator set to 1 beyond the end of the range.
268  // The value returned is the same as after()
269  inline const const_iterator cend() const;
270 
271  //- A const_iterator set to 1 beyond the end of the range.
272  // The value returned is the same as after()
273  inline const const_iterator end() const;
274 
275  //- Return a forward values generator
276  inline indexer generator() const;
277 
278  //- Return const_iterator to a position within the range,
279  //- with bounds checking.
280  // \return iterator at the requested position, or end() for
281  // out of bounds
282  inline const_iterator at(const label i) const;
283 };
284 
285 
286 // IOstream Operators
287 
288 //- Write sliceRange to Ostream as (start size stride) tuple
290 
291 
292 // Global Operators
293 
294 inline bool operator==(const sliceRange& a, const sliceRange& b) noexcept
295 {
296  return
297  (
298  a.first() == b.first()
299  && a.size() == b.size()
300  && a.stride() == b.stride()
301  );
302 }
303 
304 
305 inline bool operator!=(const sliceRange& a, const sliceRange& b) noexcept
306 {
307  return !(a == b);
308 }
309 
310 
311 //- Comparison function for sorting, compares the start.
312 // If the start values are equal, also compares the size.
313 // If the sizes are equal, also compares the stride.
314 inline bool operator<(const sliceRange& a, const sliceRange& b) noexcept
315 {
316  return
317  (
318  a.first() < b.first()
319  ||
320  (
321  !(b.first() < a.first())
322  &&
323  (
324  a.size() < b.size()
325  ||
326  (
327  !(b.size() < a.size())
328  && a.stride() < b.stride()
329  )
330  )
331  )
332  );
333 
334 }
335 
336 
337 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
338 
339 } // End namespace Foam
340 
341 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
342 
343 #include "sliceRangeI.H"
344 
345 #endif
346 
347 // ************************************************************************* //
Foam::sliceRange::cbegin
const_iterator cbegin() const
A const_iterator set to the beginning of the range.
Definition: sliceRangeI.H:171
Foam::sliceRange::size_type
label size_type
The type that can represent the size of the range.
Definition: sliceRange.H:80
Foam::sliceRange::size_
label size_
The length of the interval.
Definition: sliceRange.H:66
Foam::sliceRange::labels
List< label > labels() const
Return the slice as a list of labels.
Definition: sliceRange.C:46
Foam::sliceRange::start
constexpr label start() const noexcept
The (inclusive) lower value of the range.
Definition: sliceRange.H:121
Foam::sliceRange::size
constexpr label size() const noexcept
The size of the range.
Definition: sliceRange.H:127
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::first
constexpr label first() const noexcept
The (inclusive) lower value of the range - same as start()
Definition: sliceRange.H:139
Foam::sliceRange::at
const_iterator at(const label i) const
Definition: sliceRangeI.H:196
Foam::sliceRange::operator=
sliceRange & operator=(const sliceRange &)=default
Default copy assignment.
Foam::sliceRange::indexer
A value indexer, for iteration or generation.
Definition: sliceRange.H:169
Foam::sliceRange::stride
constexpr label stride() const noexcept
The stride for the range.
Definition: sliceRange.H:133
Foam::sliceRange::const_iterator::operator!=
bool operator!=(const const_iterator &iter) const noexcept
Test for inequality of values, not stride.
Definition: sliceRangeI.H:157
Foam::sliceRange::generator
indexer generator() const
Return a forward values generator.
Definition: sliceRangeI.H:189
Foam::operator!=
bool operator!=(const eddy &a, const eddy &b)
Definition: eddy.H:235
n
label n
Definition: TABSMDCalcMethod2.H:31
Foam::label
intWM_LABEL_SIZE_t label
A label is an int32_t or int64_t as specified by the pre-processor macro WM_LABEL_SIZE.
Definition: label.H:62
Foam::constant::physicoChemical::b
const dimensionedScalar b
Wien displacement law constant: default SI units: [m.K].
Definition: createFields.H:27
Foam::operator==
tmp< faMatrix< Type > > operator==(const faMatrix< Type > &, const faMatrix< Type > &)
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::const_iterator::operator==
bool operator==(const const_iterator &iter) const noexcept
Test for equality of values, not stride.
Definition: sliceRangeI.H:148
Foam::sliceRange::valid
bool valid() const noexcept
Is the range non-empty?
Definition: sliceRange.H:115
Foam::sliceRange::stride_
label stride_
The stride within the interval.
Definition: sliceRange.H:69
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::sliceRange::begin
const_iterator begin() const
A const_iterator set to the beginning of the range.
Definition: sliceRangeI.H:165
Foam
Namespace for OpenFOAM.
Definition: atmBoundaryLayer.C:33
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::last
label last() const noexcept
The (inclusive) upper value of the range.
Definition: sliceRange.H:145
Foam::sliceRange::sliceRange
constexpr sliceRange() noexcept
An empty slice (0,0,0)
Definition: sliceRangeI.H:32
range
scalar range
Definition: LISASMDCalcMethod1.H:12
Foam::operator<
bool operator<(const Instant< T1 > &a, const Instant< T2 > &b)
Compare instant values for less-than.
Definition: Instant.C:90
sliceRangeI.H
label.H
Foam::List< label >
Foam::FixedList
A 1D vector of objects of type <T> with a fixed length <N>.
Definition: HashTable.H:104
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::empty
bool empty() const noexcept
Is the range empty?
Definition: sliceRange.H:109
Foam::sliceRange::value_type
label value_type
Type of values the range contains.
Definition: sliceRange.H:77
Foam::Ostream
An Ostream is an abstract base class for all output systems (streams, files, token lists,...
Definition: Ostream.H:56
Foam::sliceRange::start_
label start_
The start point for the interval.
Definition: sliceRange.H:63
Foam::operator<<
Ostream & operator<<(Ostream &, const boundaryPatch &)
Definition: boundaryPatch.C:102
Foam::sliceRange::const_iterator::operator+=
const_iterator & operator+=(const label n) noexcept
Arbitrary increment, no checking.
Definition: sliceRangeI.H:124