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-2019 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.H
37  IndirectListBase.C
38  IndirectListBaseI.H
39  IndirectListBaseIO.C
40 
41 \*---------------------------------------------------------------------------*/
42 
43 #ifndef IndirectListBase_H
44 #define IndirectListBase_H
45 
46 #include "List.H"
47 
48 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
49 
50 namespace Foam
51 {
52 
53 /*---------------------------------------------------------------------------*\
54  Class IndirectListBase Declaration
55 \*---------------------------------------------------------------------------*/
56 
57 template<class T, class Addr>
58 class IndirectListBase
59 {
60  // Private Data
61 
62  //- The list values
63  UList<T>& values_;
64 
65  //- Reference to the addressing for the list values
66  const Addr& addr_;
67 
68 
69 protected:
70 
71  // Protected Member Functions
72 
73  //- Copy values The number of elements in the list
74  template<class ListType>
75  inline void copyList(const ListType& rhs);
76 
77 
78 public:
79 
80  // Type definitions (STL)
81 
82  //- Type of values the list contains.
83  typedef T value_type;
84 
85  //- The pointer type for non-const access to value_type items
86  typedef T* pointer;
87 
88  //- The pointer type for const access to value_type items
89  typedef const T* const_pointer;
90 
91  //- The type used for storing into value_type objects
92  typedef T& reference;
93 
94  //- The type used for reading from constant value_type objects.
95  typedef const T& const_reference;
96 
97  //- The type to represent the size of a UList
98  typedef label size_type;
99 
100  //- The difference between iterator objects
101  typedef label difference_type;
102 
103  //- Forward iterator with non-const access
104  class iterator;
105 
106  //- Forward iterator with const access
107  class const_iterator;
108 
109  //- The addressing type (non-stl definition)
110  typedef Addr addressing_type;
111 
112 
113  // Constructors
114 
115  //- No null construct
116  IndirectListBase() = delete;
117 
118  //- Store references to the values list and the addressing array
119  inline IndirectListBase(const UList<T>& values, const Addr& addr);
120 
121 
122  // Member Functions
123 
124  // Access
125 
126  //- The number of elements in the list
127  inline label size() const
128  {
129  return addr_.size();
130  }
131 
132  //- True if the list is empty (ie, size() is zero).
133  inline bool empty() const
134  {
135  return addr_.empty();
136  }
137 
138  //- True if all entries have identical values, and list is non-empty
139  inline bool uniform() const;
140 
141  //- The first element of the list.
142  inline T& first()
143  {
144  return values_[addr_.first()];
145  }
146 
147  //- The first element of the list.
148  inline const T& first() const
149  {
150  return values_[addr_.first()];
151  }
152 
153  //- The last element of the list.
154  inline T& last()
155  {
156  return values_[addr_.last()];
157  }
158 
159  //- The last element of the list.
160  inline const T& last() const
161  {
162  return values_[addr_.last()];
163  }
164 
165  //- The list of values (without addressing)
166  inline const UList<T>& values() const
167  {
168  return values_;
169  }
170 
171  //- The list of values (without addressing)
172  inline UList<T>& values()
173  {
174  return values_;
175  }
176 
177  //- The addressing used for the list
178  inline const Addr& addressing() const
179  {
180  return addr_;
181  }
182 
183 
184  // Search
185 
186  //- Find index of the first occurrence of the value.
187  // When start is specified, any occurences before start are ignored.
188  // Linear search.
189  // \return -1 if not found.
190  label find(const T& val, const label start=0) const;
191 
192  //- Find index of the last occurrence of the value.
193  // When pos is specified, any occurrences after pos are ignored.
194  // Linear search.
195  // \return -1 if not found.
196  label rfind(const T& val, const label pos=-1) const;
197 
198  //- True if the value if found in the list. Linear search.
199  inline bool found(const T& val, const label start=0) const;
200 
201 
202  // Member Operators
203 
204  //- Return the addressed elements as a List
205  inline List<T> operator()() const;
206 
207  //- Non-const access to an element in the list
208  inline T& operator[](const label i);
209 
210  //- Const access to an element in the list
211  inline const T& operator[](const label i) const;
212 
213  //- Assign all addressed elements to the given value
214  inline void operator=(const T& val);
215 
216  //- Assignment of all entries to zero
217  inline void operator=(const zero);
218 
219  //- Deep copy values from a list of the addressed elements
220  // Fatal if list sizes are not identical
221  inline void operator=(const UList<T>& rhs);
222 
223  //- Deep copy values from a list of the addressed elements
224  // Fatal if list sizes are not identical
225  inline void operator=(const IndirectListBase<T, Addr>& rhs);
226 
227  //- Deep copy values from a list of the addressed elements
228  // Fatal if list sizes are not identical
229  template<class AnyAddr>
230  inline void operator=(const IndirectListBase<T, AnyAddr>& rhs);
231 
232 
233  // Iterators
234 
235  //- A non-const iterator for an indirect list
236  class iterator
237  {
238  typename UList<T>::pointer data_;
239  typename addressing_type::const_iterator iter_;
240 
241  public:
242 
244  using value_type = T;
245  using pointer = T*;
246  using reference = T&;
247  using iterator_category = std::forward_iterator_tag;
248 
250  (
251  UList<T>& list,
252  typename addressing_type::const_iterator baseIter
253  )
254  :
255  data_(list.begin()),
256  iter_(baseIter)
257  {}
258 
259  reference operator*() const
260  {
261  return data_[*iter_];
262  }
263 
265  {
266  ++iter_;
267  return *this;
268  }
269 
270  bool operator==(iterator& rhs) const
271  {
272  return iter_ == rhs.iter_;
273  }
274 
275  bool operator!=(iterator& rhs) const
276  {
277  return (iter_ != rhs.iter_);
278  }
279  };
280 
281 
282  //- A const iterator for an indirect list
283  class const_iterator
284  {
285  typename UList<T>::const_pointer data_;
286  typename addressing_type::const_iterator iter_;
287 
288  public:
289 
291  using value_type = const T;
292  using pointer = const T*;
293  using reference = const T&;
294  using iterator_category = std::forward_iterator_tag;
295 
297  (
298  const UList<T>& list,
299  typename addressing_type::const_iterator baseIter
300  )
301  :
302  data_(list.begin()),
303  iter_(baseIter)
304  {}
305 
306  reference operator*() const
307  {
308  return data_[*iter_];
309  }
310 
312  {
313  ++iter_;
314  return *this;
315  }
316 
317  bool operator==(const_iterator& rhs) const
318  {
319  return iter_ == rhs.iter_;
320  }
321 
322  bool operator!=(const_iterator& rhs) const
323  {
324  return iter_ != rhs.iter_;
325  }
326  };
327 
328 
329  // Iterator (non-const)
330 
331  //- Return an iterator at begin of list
332  inline iterator begin()
333  {
334  return iterator(values_, addr_.cbegin());
335  }
336 
337  //- Return an iterator at end of list
338  inline iterator end()
339  {
340  return iterator(values_, addr_.cend());
341  }
342 
343 
344  // Iterator (const)
345 
346  //- Return a const_iterator at begin of list
347  inline const_iterator cbegin() const
348  {
349  return const_iterator(values_, addr_.cbegin());
350  }
351 
352  //- Return a const_iterator at end of list
353  inline const_iterator cend() const
354  {
355  return const_iterator(values_, addr_.cend());
356  }
357 
358  //- Return a const_iterator at end of list
359  inline const_iterator begin() const
360  {
361  return cbegin();
362  }
363 
364  //- Return a const_iterator at end of list
365  inline const_iterator end() const
366  {
367  return cend();
368  }
369 
370 
371  // Writing
372 
373  //- Write List, with line-breaks in ASCII when length exceeds shortLen.
374  // Using '0' suppresses line-breaks entirely.
375  Ostream& writeList(Ostream& os, const label shortLen=0) const;
376 };
377 
378 
379 // * * * * * * * * * * * * * * * IOstream Operators * * * * * * * * * * * * //
380 
381 //- Write List to Ostream, as per UList::writeList() with default length.
382 // The default short-length is given by Detail::ListPolicy::short_length
383 template<class T, class Addr>
385 {
387 }
388 
389 
390 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
391 
392 } // End namespace Foam
393 
394 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
395 
396 #include "IndirectListBaseI.H"
397 
398 #ifdef NoRepository
399  #include "IndirectListBase.C"
400  #include "IndirectListBaseIO.C"
401 #endif
402 
403 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
404 
405 #endif
406 
407 // ************************************************************************* //
Foam::UList::cbegin
const_iterator cbegin() const
Return const_iterator to begin traversing the constant UList.
Definition: UListI.H:290
Foam::IndirectListBase::const_pointer
const typedef T * const_pointer
The pointer type for const access to value_type items.
Definition: IndirectListBase.H:88
Foam::IndirectListBase::end
iterator end()
Return an iterator at end of list.
Definition: IndirectListBase.H:337
Foam::IndirectListBase::value_type
T value_type
Type of values the list contains.
Definition: IndirectListBase.H:82
Foam::val
label ListType::const_reference val
Definition: ListOps.H:407
Foam::IndirectListBase::const_iterator::operator++
const_iterator & operator++()
Definition: IndirectListBase.H:310
List.H
Foam::IndirectListBase::operator()
List< T > operator()() const
Return the addressed elements as a List.
Definition: IndirectListBaseI.H:108
Foam::UList::cend
const_iterator cend() const
Return const_iterator to end traversing the constant UList.
Definition: UListI.H:311
Foam::IndirectListBase::const_iterator::reference
const T & reference
Definition: IndirectListBase.H:292
Foam::IndirectListBase::const_iterator::operator*
reference operator*() const
Definition: IndirectListBase.H:305
Foam::IndirectListBase::found
bool found(const T &val, const label start=0) const
True if the value if found in the list. Linear search.
Definition: IndirectListBaseI.H:95
Foam::IndirectListBase::iterator::iterator
iterator(UList< T > &list, typename addressing_type::const_iterator baseIter)
Definition: IndirectListBase.H:249
Foam::IndirectListBase::copyList
void copyList(const ListType &rhs)
Copy values The number of elements in the list.
Definition: IndirectListBaseI.H:33
Foam::IndirectListBase::const_reference
const typedef T & const_reference
The type used for reading from constant value_type objects.
Definition: IndirectListBase.H:94
Foam::IndirectListBase::const_iterator::const_iterator
const_iterator(const UList< T > &list, typename addressing_type::const_iterator baseIter)
Definition: IndirectListBase.H:296
Foam::IndirectListBase::addressing
const Addr & addressing() const
The addressing used for the list.
Definition: IndirectListBase.H:177
Foam::IndirectListBase::empty
bool empty() const
True if the list is empty (ie, size() is zero).
Definition: IndirectListBase.H:132
Foam::IndirectListBase::const_iterator::pointer
const T * pointer
Definition: IndirectListBase.H:291
IndirectListBaseIO.C
Foam::IndirectListBase::const_iterator::operator!=
bool operator!=(const_iterator &rhs) const
Definition: IndirectListBase.H:321
Foam::IndirectListBase::iterator::operator*
reference operator*() const
Definition: IndirectListBase.H:258
Foam::IndirectListBase::values
const UList< T > & values() const
The list of values (without addressing)
Definition: IndirectListBase.H:165
Foam::IndirectListBase::const_iterator::operator==
bool operator==(const_iterator &rhs) const
Definition: IndirectListBase.H:316
Foam::IndirectListBase::pointer
T * pointer
The pointer type for non-const access to value_type items.
Definition: IndirectListBase.H:85
Foam::IndirectListBase::iterator
A non-const iterator for an indirect list.
Definition: IndirectListBase.H:235
Foam::IndirectListBase::const_iterator::difference_type
label difference_type
Definition: IndirectListBase.H:289
Foam::IndirectListBase::iterator::pointer
T * pointer
Definition: IndirectListBase.H:244
Foam::UList::begin
iterator begin()
Return an iterator to begin traversing the UList.
Definition: UListI.H:276
Foam::IndirectListBase::operator[]
T & operator[](const label i)
Non-const access to an element in the list.
Definition: IndirectListBaseI.H:125
Foam::IndirectListBase::first
T & first()
The first element of the list.
Definition: IndirectListBase.H:141
Foam::IndirectListBase::iterator::reference
T & reference
Definition: IndirectListBase.H:245
Foam::IndirectListBase::reference
T & reference
The type used for storing into value_type objects.
Definition: IndirectListBase.H:91
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::IndirectListBase::const_iterator::value_type
const T value_type
Definition: IndirectListBase.H:290
Foam::IndirectListBase::IndirectListBase
IndirectListBase()=delete
No null 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 ouput.
Definition: ListPolicy.H:61
Foam::IndirectListBase::iterator::iterator_category
std::forward_iterator_tag iterator_category
Definition: IndirectListBase.H:246
Foam::IndirectListBase::values
UList< T > & values()
The list of values (without addressing)
Definition: IndirectListBase.H:171
Foam::IndirectListBase::iterator::operator==
bool operator==(iterator &rhs) const
Definition: IndirectListBase.H:269
Foam::IndirectListBase::addressing_type
Addr addressing_type
The addressing type (non-stl definition)
Definition: IndirectListBase.H:106
IndirectListBase.C
IndirectListBaseI.H
Foam::IndirectListBase::const_iterator::iterator_category
std::forward_iterator_tag iterator_category
Definition: IndirectListBase.H:293
Foam::IndirectListBase::end
const_iterator end() const
Return a const_iterator at end of list.
Definition: IndirectListBase.H:364
Foam::IndirectListBase::iterator::operator!=
bool operator!=(iterator &rhs) const
Definition: IndirectListBase.H:274
Foam
Namespace for OpenFOAM.
Definition: atmBoundaryLayer.C:33
Foam::IndirectListBase::begin
iterator begin()
Return an iterator at begin of list.
Definition: IndirectListBase.H:331
Foam::IndirectListBase::size_type
label size_type
The type to represent the size of a UList.
Definition: IndirectListBase.H:97
Foam::IndirectListBase::last
const T & last() const
The last element of the list.
Definition: IndirectListBase.H:159
Foam::IndirectListBase::last
T & last()
The last element of the list.
Definition: IndirectListBase.H:153
Foam::IndirectListBase::cend
const_iterator cend() const
Return a const_iterator at end of list.
Definition: IndirectListBase.H:352
Foam::IndirectListBase::iterator::value_type
T value_type
Definition: IndirectListBase.H:243
Foam::IndirectListBase::uniform
bool uniform() const
True if all entries have identical values, and list is non-empty.
Definition: IndirectListBaseI.H:70
Foam::List
A 1D array of objects of type <T>, where the size of the vector is known and used for subscript bound...
Definition: HashTable.H:102
Foam::start
label ListType::const_reference const label start
Definition: ListOps.H:408
Foam::IndirectListBase::first
const T & first() const
The first element of the list.
Definition: IndirectListBase.H:147
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::find
label find(const T &val, const label start=0) const
Find index of the first occurrence of the value.
Definition: IndirectListBase.C:32
Foam::IndirectListBase::begin
const_iterator begin() const
Return a const_iterator at end of list.
Definition: IndirectListBase.H:358
Foam::IndirectListBase::operator=
void operator=(const T &val)
Assign all addressed elements to the given value.
Definition: IndirectListBaseI.H:140
Foam::IndirectListBase
Base for lists with indirect addressing, templated on the list contents type and the addressing type....
Definition: IndirectListBase.H:57
Foam::UList::pointer
T * pointer
The pointer type for non-const access to value_type items.
Definition: UList.H:130
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:282
Foam::IndirectListBase::iterator::difference_type
label difference_type
Definition: IndirectListBase.H:242
Foam::operator<<
Ostream & operator<<(Ostream &, const boundaryPatch &)
Definition: boundaryPatch.C:102
Foam::IndirectListBase::cbegin
const_iterator cbegin() const
Return a const_iterator at begin of list.
Definition: IndirectListBase.H:346
Foam::IndirectListBase::size
label size() const
The number of elements in the list.
Definition: IndirectListBase.H:126
Foam::IndirectListBase::rfind
label rfind(const T &val, const label pos=-1) const
Find index of the last occurrence of the value.
Definition: IndirectListBase.C:58
Foam::IndirectListBase::iterator::operator++
iterator & operator++()
Definition: IndirectListBase.H:263
Foam::zero
A class representing the concept of 0 (zero), which can be used to avoid manipulating objects that ar...
Definition: zero.H:61
Foam::IndirectListBase::difference_type
label difference_type
The difference between iterator objects.
Definition: IndirectListBase.H:100
Foam::pos
dimensionedScalar pos(const dimensionedScalar &ds)
Definition: dimensionedScalar.C:177