IndirectListBase.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) 2011-2016 OpenFOAM Foundation
9  Copyright (C) 2017-2021 OpenCFD Ltd.
10 -------------------------------------------------------------------------------
11 License
12  This file is part of OpenFOAM.
13 
14  OpenFOAM is free software: you can redistribute it and/or modify it
15  under the terms of the GNU General Public License as published by
16  the Free Software Foundation, either version 3 of the License, or
17  (at your option) any later version.
18 
19  OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
20  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
21  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
22  for more details.
23 
24  You should have received a copy of the GNU General Public License
25  along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
26 
27 Class
28  Foam::IndirectListBase
29 
30 Description
31  Base for lists with indirect addressing, templated on the list contents
32  type and the addressing type. Storage for both values and addressing
33  is held outside of the class.
34 
35 SourceFiles
36  IndirectListBase.C
37  IndirectListBaseI.H
38  IndirectListBaseIO.C
39 
40 \*---------------------------------------------------------------------------*/
41 
42 #ifndef IndirectListBase_H
43 #define IndirectListBase_H
44 
45 #include "List.H"
46 
47 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
48 
49 namespace Foam
50 {
51 
52 /*---------------------------------------------------------------------------*\
53  Class IndirectListBase Declaration
54 \*---------------------------------------------------------------------------*/
55 
56 template<class T, class Addr>
57 class IndirectListBase
58 {
59  // Private Data
60 
61  //- The list values
62  UList<T>& values_;
63 
64  //- Reference to the addressing for the list values
65  const Addr& addr_;
66 
67 
68 protected:
69 
70  // Protected Member Functions
71 
72  //- Deep copy values from the list
73  template<class ListType>
74  inline void copyList(const ListType& rhs);
75 
76 
77 public:
78 
79  // Type definitions (STL)
80 
81  //- Type of values the list contains.
82  typedef T value_type;
83 
84  //- The pointer type for non-const access to value_type items
85  typedef T* pointer;
86 
87  //- The pointer type for const access to value_type items
88  typedef const T* const_pointer;
89 
90  //- The type used for storing into value_type objects
91  typedef T& reference;
92 
93  //- The type used for reading from constant value_type objects.
94  typedef const T& const_reference;
95 
96  //- The type to represent the size of a UList
97  typedef label size_type;
98 
99  //- The difference between iterator objects
100  typedef label difference_type;
101 
102  //- Forward iterator with non-const access
103  class iterator;
104 
105  //- Forward iterator with const access
106  class const_iterator;
107 
108  //- The addressing type (non-stl definition)
109  typedef Addr addressing_type;
110 
111 
112  // Constructors
113 
114  //- No default construct
115  IndirectListBase() = delete;
116 
117  //- Store references to the values list and the addressing array
118  inline IndirectListBase(const UList<T>& values, const Addr& addr);
119 
120 
121  // Member Functions
122 
123  // Access
124 
125  //- The number of elements in the list
126  inline label size() const noexcept
127  {
128  return addr_.size();
129  }
130 
131  //- True if the list is empty (ie, size() is zero).
132  inline bool empty() const noexcept
133  {
134  return addr_.empty();
135  }
136 
137  //- The list of values (without addressing)
138  inline const UList<T>& values() const noexcept
139  {
140  return values_;
141  }
142 
143  //- The list of values (without addressing)
144  inline UList<T>& values() noexcept
145  {
146  return values_;
147  }
148 
149  //- The addressing used for the list
150  inline const Addr& addressing() const noexcept
151  {
152  return addr_;
153  }
154 
155  //- True if all entries have identical values, and list is non-empty
156  inline bool uniform() const;
157 
158  //- The first element of the list.
159  inline const T& first() const;
160 
161  //- The first element of the list.
162  inline T& first();
163 
164  //- The last element of the list.
165  inline const T& last() const;
166 
167  //- The last element of the list.
168  inline T& last();
169 
170  //- The forward circular index. The next index in the list
171  //- which returns to the first at the end of the list
172  inline label fcIndex(const label i) const;
173 
174  //- The reverse circular index. The previous index in the list
175  //- which returns to the last at the beginning of the list
176  inline label rcIndex(const label i) const;
177 
178  //- Return forward circular value (ie, next value in the list)
179  inline const T& fcValue(const label i) const;
180 
181  //- Return forward circular value (ie, next value in the list)
182  inline T& fcValue(const label i);
183 
184  //- Return reverse circular value (ie, previous value in the list)
185  inline const T& rcValue(const label i) const;
186 
187  //- Return reverse circular value (ie, previous value in the list)
188  inline T& rcValue(const label i);
189 
190 
191  // Search
192 
193  //- Find index of the first occurrence of the value.
194  // Any occurrences before the start pos are ignored.
195  // Linear search.
196  // \return -1 if not found.
197  label find(const T& val, label pos = 0) const;
198 
199  //- Find index of the last occurrence of the value.
200  // Any occurrences after the end pos are ignored.
201  // Linear search.
202  // \return -1 if not found.
203  label rfind(const T& val, label pos = -1) const;
204 
205  //- True if the value if found in the list.
206  // Any occurrences before the start pos are ignored.
207  // Linear search.
208  inline bool found(const T& val, label pos=0) const;
209 
210 
211  // Member Operators
212 
213  //- Return the addressed elements as a List
214  inline List<T> operator()() const;
215 
216  //- Non-const access to an element in the list
217  inline T& operator[](const label i);
218 
219  //- Const access to an element in the list
220  inline const T& operator[](const label i) const;
221 
222  //- Assign all addressed elements to the given value
223  inline void operator=(const T& val);
224 
225  //- Assignment of all entries to zero
226  inline void operator=(const Foam::zero);
227 
228  //- Deep copy values from a list of the addressed elements
229  // Fatal if list sizes are not identical
230  inline void operator=(const UList<T>& rhs);
231 
232  //- Deep copy values from a list of the addressed elements
233  // Fatal if list sizes are not identical
234  inline void operator=(const IndirectListBase<T, Addr>& rhs);
235 
236  //- Deep copy values from a list of the addressed elements
237  // Fatal if list sizes are not identical
238  template<class AnyAddr>
239  inline void operator=(const IndirectListBase<T, AnyAddr>& rhs);
240 
241 
242  // Iterators
243 
244  //- A non-const iterator for an indirect list
245  class iterator
246  {
247  typename UList<T>::pointer data_;
248  typename addressing_type::const_iterator iter_;
249 
250  public:
251 
252  using difference_type = label;
253  using value_type = T;
254  using pointer = T*;
255  using reference = T&;
256  using iterator_category = std::forward_iterator_tag;
257 
259  (
260  UList<T>& list,
261  typename addressing_type::const_iterator baseIter
262  )
263  :
264  data_(list.begin()),
265  iter_(baseIter)
266  {}
267 
268  reference operator*() const
269  {
270  return data_[*iter_];
271  }
272 
274  {
275  ++iter_;
276  return *this;
277  }
278 
279  bool operator==(iterator& rhs) const
280  {
281  return iter_ == rhs.iter_;
282  }
283 
284  bool operator!=(iterator& rhs) const
285  {
286  return (iter_ != rhs.iter_);
287  }
288  };
289 
290 
291  //- A const iterator for an indirect list
292  class const_iterator
293  {
294  typename UList<T>::const_pointer data_;
295  typename addressing_type::const_iterator iter_;
296 
297  public:
298 
299  using difference_type = label;
300  using value_type = const T;
301  using pointer = const T*;
302  using reference = const T&;
303  using iterator_category = std::forward_iterator_tag;
304 
306  (
307  const UList<T>& list,
308  typename addressing_type::const_iterator baseIter
309  )
310  :
311  data_(list.begin()),
312  iter_(baseIter)
313  {}
314 
315  reference operator*() const
316  {
317  return data_[*iter_];
318  }
319 
321  {
322  ++iter_;
323  return *this;
324  }
325 
326  bool operator==(const_iterator& rhs) const
327  {
328  return iter_ == rhs.iter_;
329  }
330 
331  bool operator!=(const_iterator& rhs) const
332  {
333  return iter_ != rhs.iter_;
334  }
335  };
336 
337 
338  // Iterator (non-const)
339 
340  //- Return an iterator at begin of list
341  inline iterator begin()
342  {
343  return iterator(values_, addr_.cbegin());
344  }
345 
346  //- Return an iterator at end of list
347  inline iterator end()
348  {
349  return iterator(values_, addr_.cend());
350  }
351 
352 
353  // Iterator (const)
354 
355  //- Return a const_iterator at begin of list
356  inline const_iterator cbegin() const
357  {
358  return const_iterator(values_, addr_.cbegin());
359  }
360 
361  //- Return a const_iterator at end of list
362  inline const_iterator cend() const
363  {
364  return const_iterator(values_, addr_.cend());
365  }
366 
367  //- Return a const_iterator at end of list
368  inline const_iterator begin() const
369  {
370  return cbegin();
371  }
372 
373  //- Return a const_iterator at end of list
374  inline const_iterator end() const
375  {
376  return cend();
377  }
378 
379 
380  // Writing
381 
382  //- Write List, with line-breaks in ASCII when length exceeds shortLen.
383  // Using '0' suppresses line-breaks entirely.
384  Ostream& writeList(Ostream& os, const label shortLen=0) const;
385 };
386 
387 
388 // * * * * * * * * * * * * * * * IOstream Operators * * * * * * * * * * * * //
389 
390 //- Write List to Ostream, as per UList::writeList() with default length.
391 // The default short-length is given by Detail::ListPolicy::short_length
392 template<class T, class Addr>
394 {
396 }
397 
398 
399 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
400 
401 } // End namespace Foam
402 
403 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
404 
405 #include "IndirectListBaseI.H"
406 
407 #ifdef NoRepository
408  #include "IndirectListBase.C"
409  #include "IndirectListBaseIO.C"
410 #endif
411 
412 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
413 
414 #endif
415 
416 // ************************************************************************* //
Foam::IndirectListBase::const_pointer
const typedef T * const_pointer
The pointer type for const access to value_type items.
Definition: IndirectListBase.H:87
Foam::IndirectListBase::end
iterator end()
Return an iterator at end of list.
Definition: IndirectListBase.H:346
Foam::IndirectListBase::value_type
T value_type
Type of values the list contains.
Definition: IndirectListBase.H:81
Foam::IndirectListBase::const_iterator::operator++
const_iterator & operator++()
Definition: IndirectListBase.H:319
List.H
Foam::IndirectListBase::operator()
List< T > operator()() const
Return the addressed elements as a List.
Definition: IndirectListBaseI.H:184
Foam::IndirectListBase::const_iterator::reference
const T & reference
Definition: IndirectListBase.H:301
Foam::IndirectListBase::const_iterator::operator*
reference operator*() const
Definition: IndirectListBase.H:314
Foam::IndirectListBase::iterator::iterator
iterator(UList< T > &list, typename addressing_type::const_iterator baseIter)
Definition: IndirectListBase.H:258
Foam::IndirectListBase::values
const UList< T > & values() const noexcept
The list of values (without addressing)
Definition: IndirectListBase.H:137
Foam::IndirectListBase::copyList
void copyList(const ListType &rhs)
Deep copy values from the list.
Definition: IndirectListBaseI.H:32
Foam::IndirectListBase::rfind
label rfind(const T &val, label pos=-1) const
Find index of the last occurrence of the value.
Definition: IndirectListBase.C:60
Foam::IndirectListBase::const_reference
const typedef T & const_reference
The type used for reading from constant value_type objects.
Definition: IndirectListBase.H:93
Foam::IndirectListBase::rcIndex
label rcIndex(const label i) const
Definition: IndirectListBaseI.H:120
Foam::IndirectListBase::const_iterator::const_iterator
const_iterator(const UList< T > &list, typename addressing_type::const_iterator baseIter)
Definition: IndirectListBase.H:305
Foam::IndirectListBase::addressing
const Addr & addressing() const noexcept
The addressing used for the list.
Definition: IndirectListBase.H:149
Foam::IndirectListBase::const_iterator::pointer
const T * pointer
Definition: IndirectListBase.H:300
IndirectListBaseIO.C
Foam::IndirectListBase::const_iterator::operator!=
bool operator!=(const_iterator &rhs) const
Definition: IndirectListBase.H:330
Foam::IndirectListBase::iterator::operator*
reference operator*() const
Definition: IndirectListBase.H:267
Foam::IndirectListBase::const_iterator::operator==
bool operator==(const_iterator &rhs) const
Definition: IndirectListBase.H:325
Foam::IndirectListBase::pointer
T * pointer
The pointer type for non-const access to value_type items.
Definition: IndirectListBase.H:84
Foam::IndirectListBase::iterator
A non-const iterator for an indirect list.
Definition: IndirectListBase.H:244
Foam::IndirectListBase::const_iterator::difference_type
label difference_type
Definition: IndirectListBase.H:298
Foam::IndirectListBase::iterator::pointer
T * pointer
Definition: IndirectListBase.H:253
Foam::IndirectListBase::operator[]
T & operator[](const label i)
Non-const access to an element in the list.
Definition: IndirectListBaseI.H:201
Foam::IndirectListBase::fcIndex
label fcIndex(const label i) const
Definition: IndirectListBaseI.H:113
Foam::IndirectListBase::iterator::reference
T & reference
Definition: IndirectListBase.H:254
Foam::IndirectListBase::rcValue
const T & rcValue(const label i) const
Return reverse circular value (ie, previous value in the list)
Definition: IndirectListBaseI.H:167
Foam::operator<<
Ostream & operator<<(Ostream &, const boundaryPatch &p)
Write boundaryPatch as dictionary entries (without surrounding braces)
Definition: boundaryPatch.C:83
Foam::IndirectListBase::last
const T & last() const
The last element of the list.
Definition: IndirectListBaseI.H:140
Foam::IndirectListBase::reference
T & reference
The type used for storing into value_type objects.
Definition: IndirectListBase.H:90
Foam::UList::begin
iterator begin() noexcept
Return an iterator to begin traversing the UList.
Definition: UListI.H:329
Foam::UList::cend
const_iterator cend() const noexcept
Return const_iterator to end traversing the constant UList.
Definition: UListI.H:364
Foam::IndirectListBase::const_iterator::value_type
const T value_type
Definition: IndirectListBase.H:299
Foam::IndirectListBase::IndirectListBase
IndirectListBase()=delete
No default construct.
Foam::T
void T(FieldField< Field, Type > &f1, const FieldField< Field, Type > &f2)
Definition: FieldFieldFunctions.C:58
Foam::Detail::ListPolicy::short_length
Number of items before requiring line-breaks in the list output.
Definition: ListPolicy.H:61
Foam::IndirectListBase::iterator::iterator_category
std::forward_iterator_tag iterator_category
Definition: IndirectListBase.H:255
Foam::IndirectListBase::empty
bool empty() const noexcept
True if the list is empty (ie, size() is zero).
Definition: IndirectListBase.H:131
Foam::IndirectListBase::iterator::operator==
bool operator==(iterator &rhs) const
Definition: IndirectListBase.H:278
Foam::IndirectListBase::addressing_type
Addr addressing_type
The addressing type (non-stl definition)
Definition: IndirectListBase.H:105
IndirectListBase.C
IndirectListBaseI.H
Foam::IndirectListBase::const_iterator::iterator_category
std::forward_iterator_tag iterator_category
Definition: IndirectListBase.H:302
Foam::IndirectListBase::find
label find(const T &val, label pos=0) const
Find index of the first occurrence of the value.
Definition: IndirectListBase.C:32
os
OBJstream os(runTime.globalPath()/outputName)
Foam::IndirectListBase::size
label size() const noexcept
The number of elements in the list.
Definition: IndirectListBase.H:125
Foam::IndirectListBase::end
const_iterator end() const
Return a const_iterator at end of list.
Definition: IndirectListBase.H:373
Foam::IndirectListBase::iterator::operator!=
bool operator!=(iterator &rhs) const
Definition: IndirectListBase.H:283
Foam
Namespace for OpenFOAM.
Definition: atmBoundaryLayer.C:33
Foam::IndirectListBase::begin
iterator begin()
Return an iterator at begin of list.
Definition: IndirectListBase.H:340
Foam::IndirectListBase::fcValue
const T & fcValue(const label i) const
Return forward circular value (ie, next value in the list)
Definition: IndirectListBaseI.H:153
Foam::IndirectListBase::size_type
label size_type
The type to represent the size of a UList.
Definition: IndirectListBase.H:96
Foam::IndirectListBase::values
UList< T > & values() noexcept
The list of values (without addressing)
Definition: IndirectListBase.H:143
Foam::UList::cbegin
const_iterator cbegin() const noexcept
Return const_iterator to begin traversing the constant UList.
Definition: UListI.H:343
Foam::IndirectListBase::cend
const_iterator cend() const
Return a const_iterator at end of list.
Definition: IndirectListBase.H:361
Foam::IndirectListBase::iterator::value_type
T value_type
Definition: IndirectListBase.H:252
Foam::IndirectListBase::uniform
bool uniform() const
True if all entries have identical values, and list is non-empty.
Definition: IndirectListBaseI.H:78
Foam::List
A 1D array of objects of type <T>, where the size of the vector is known and used for subscript bound...
Definition: BitOps.H:63
Foam::UList
A 1D vector of objects of type <T>, where the size of the vector is known and can be used for subscri...
Definition: HashTable.H:103
Foam::IndirectListBase::begin
const_iterator begin() const
Return a const_iterator at end of list.
Definition: IndirectListBase.H:367
Foam::IndirectListBase::operator=
void operator=(const T &val)
Assign all addressed elements to the given value.
Definition: IndirectListBaseI.H:216
Foam::IndirectListBase
Base for lists with indirect addressing, templated on the list contents type and the addressing type....
Definition: IndirectListBase.H:56
Foam::UList::pointer
T * pointer
The pointer type for non-const access to value_type items.
Definition: UList.H:139
Foam::Ostream
An Ostream is an abstract base class for all output systems (streams, files, token lists,...
Definition: Ostream.H:56
Foam::IndirectListBase::writeList
Ostream & writeList(Ostream &os, const label shortLen=0) const
Write List, with line-breaks in ASCII when length exceeds shortLen.
Definition: IndirectListBaseIO.C:38
Foam::IndirectListBase::const_iterator
A const iterator for an indirect list.
Definition: IndirectListBase.H:291
Foam::IndirectListBase::iterator::difference_type
label difference_type
Definition: IndirectListBase.H:251
Foam::IndirectListBase::first
const T & first() const
The first element of the list.
Definition: IndirectListBaseI.H:127
Foam::IndirectListBase::cbegin
const_iterator cbegin() const
Return a const_iterator at begin of list.
Definition: IndirectListBase.H:355
Foam::IndirectListBase::found
bool found(const T &val, label pos=0) const
True if the value if found in the list.
Definition: IndirectListBaseI.H:103
Foam::IndirectListBase::iterator::operator++
iterator & operator++()
Definition: IndirectListBase.H:272
Foam::zero
A class representing the concept of 0 (zero) that can be used to avoid manipulating objects known to ...
Definition: zero.H:62
Foam::IndirectListBase::difference_type
label difference_type
The difference between iterator objects.
Definition: IndirectListBase.H:99
Foam::pos
dimensionedScalar pos(const dimensionedScalar &ds)
Definition: dimensionedScalar.C:177