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-2020 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
127  {
128  return addr_.size();
129  }
130 
131  //- True if the list is empty (ie, size() is zero).
132  inline bool empty() const
133  {
134  return addr_.empty();
135  }
136 
137  //- True if all entries have identical values, and list is non-empty
138  inline bool uniform() const;
139 
140  //- The first element of the list.
141  inline T& first()
142  {
143  return values_[addr_.first()];
144  }
145 
146  //- The first element of the list.
147  inline const T& first() const
148  {
149  return values_[addr_.first()];
150  }
151 
152  //- The last element of the list.
153  inline T& last()
154  {
155  return values_[addr_.last()];
156  }
157 
158  //- The last element of the list.
159  inline const T& last() const
160  {
161  return values_[addr_.last()];
162  }
163 
164  //- The list of values (without addressing)
165  inline const UList<T>& values() const
166  {
167  return values_;
168  }
169 
170  //- The list of values (without addressing)
171  inline UList<T>& values()
172  {
173  return values_;
174  }
175 
176  //- The addressing used for the list
177  inline const Addr& addressing() const
178  {
179  return addr_;
180  }
181 
182 
183  // Search
184 
185  //- Find index of the first occurrence of the value.
186  // Any occurrences before the start pos are ignored.
187  // Linear search.
188  // \return -1 if not found.
189  label find(const T& val, label pos = 0) const;
190 
191  //- Find index of the last occurrence of the value.
192  // Any occurrences after the end pos are ignored.
193  // Linear search.
194  // \return -1 if not found.
195  label rfind(const T& val, label pos = -1) const;
196 
197  //- True if the value if found in the list.
198  // Any occurrences before the start pos are ignored.
199  // Linear search.
200  inline bool found(const T& val, label pos=0) const;
201 
202 
203  // Member Operators
204 
205  //- Return the addressed elements as a List
206  inline List<T> operator()() const;
207 
208  //- Non-const access to an element in the list
209  inline T& operator[](const label i);
210 
211  //- Const access to an element in the list
212  inline const T& operator[](const label i) const;
213 
214  //- Assign all addressed elements to the given value
215  inline void operator=(const T& val);
216 
217  //- Assignment of all entries to zero
218  inline void operator=(const Foam::zero);
219 
220  //- Deep copy values from a list of the addressed elements
221  // Fatal if list sizes are not identical
222  inline void operator=(const UList<T>& rhs);
223 
224  //- Deep copy values from a list of the addressed elements
225  // Fatal if list sizes are not identical
226  inline void operator=(const IndirectListBase<T, Addr>& rhs);
227 
228  //- Deep copy values from a list of the addressed elements
229  // Fatal if list sizes are not identical
230  template<class AnyAddr>
231  inline void operator=(const IndirectListBase<T, AnyAddr>& rhs);
232 
233 
234  // Iterators
235 
236  //- A non-const iterator for an indirect list
237  class iterator
238  {
239  typename UList<T>::pointer data_;
240  typename addressing_type::const_iterator iter_;
241 
242  public:
243 
244  using difference_type = label;
245  using value_type = T;
246  using pointer = T*;
247  using reference = T&;
248  using iterator_category = std::forward_iterator_tag;
249 
251  (
252  UList<T>& list,
253  typename addressing_type::const_iterator baseIter
254  )
255  :
256  data_(list.begin()),
257  iter_(baseIter)
258  {}
259 
260  reference operator*() const
261  {
262  return data_[*iter_];
263  }
264 
266  {
267  ++iter_;
268  return *this;
269  }
270 
271  bool operator==(iterator& rhs) const
272  {
273  return iter_ == rhs.iter_;
274  }
275 
276  bool operator!=(iterator& rhs) const
277  {
278  return (iter_ != rhs.iter_);
279  }
280  };
281 
282 
283  //- A const iterator for an indirect list
284  class const_iterator
285  {
286  typename UList<T>::const_pointer data_;
287  typename addressing_type::const_iterator iter_;
288 
289  public:
290 
291  using difference_type = label;
292  using value_type = const T;
293  using pointer = const T*;
294  using reference = const T&;
295  using iterator_category = std::forward_iterator_tag;
296 
298  (
299  const UList<T>& list,
300  typename addressing_type::const_iterator baseIter
301  )
302  :
303  data_(list.begin()),
304  iter_(baseIter)
305  {}
306 
307  reference operator*() const
308  {
309  return data_[*iter_];
310  }
311 
313  {
314  ++iter_;
315  return *this;
316  }
317 
318  bool operator==(const_iterator& rhs) const
319  {
320  return iter_ == rhs.iter_;
321  }
322 
323  bool operator!=(const_iterator& rhs) const
324  {
325  return iter_ != rhs.iter_;
326  }
327  };
328 
329 
330  // Iterator (non-const)
331 
332  //- Return an iterator at begin of list
333  inline iterator begin()
334  {
335  return iterator(values_, addr_.cbegin());
336  }
337 
338  //- Return an iterator at end of list
339  inline iterator end()
340  {
341  return iterator(values_, addr_.cend());
342  }
343 
344 
345  // Iterator (const)
346 
347  //- Return a const_iterator at begin of list
348  inline const_iterator cbegin() const
349  {
350  return const_iterator(values_, addr_.cbegin());
351  }
352 
353  //- Return a const_iterator at end of list
354  inline const_iterator cend() const
355  {
356  return const_iterator(values_, addr_.cend());
357  }
358 
359  //- Return a const_iterator at end of list
360  inline const_iterator begin() const
361  {
362  return cbegin();
363  }
364 
365  //- Return a const_iterator at end of list
366  inline const_iterator end() const
367  {
368  return cend();
369  }
370 
371 
372  // Writing
373 
374  //- Write List, with line-breaks in ASCII when length exceeds shortLen.
375  // Using '0' suppresses line-breaks entirely.
376  Ostream& writeList(Ostream& os, const label shortLen=0) const;
377 };
378 
379 
380 // * * * * * * * * * * * * * * * IOstream Operators * * * * * * * * * * * * //
381 
382 //- Write List to Ostream, as per UList::writeList() with default length.
383 // The default short-length is given by Detail::ListPolicy::short_length
384 template<class T, class Addr>
386 {
388 }
389 
390 
391 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
392 
393 } // End namespace Foam
394 
395 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
396 
397 #include "IndirectListBaseI.H"
398 
399 #ifdef NoRepository
400  #include "IndirectListBase.C"
401  #include "IndirectListBaseIO.C"
402 #endif
403 
404 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
405 
406 #endif
407 
408 // ************************************************************************* //
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:87
Foam::IndirectListBase::end
iterator end()
Return an iterator at end of list.
Definition: IndirectListBase.H:338
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:311
List.H
Foam::IndirectListBase::operator()
List< T > operator()() const
Return the addressed elements as a List.
Definition: IndirectListBaseI.H:116
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:293
Foam::IndirectListBase::const_iterator::operator*
reference operator*() const
Definition: IndirectListBase.H:306
Foam::IndirectListBase::iterator::iterator
iterator(UList< T > &list, typename addressing_type::const_iterator baseIter)
Definition: IndirectListBase.H:250
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::const_iterator::const_iterator
const_iterator(const UList< T > &list, typename addressing_type::const_iterator baseIter)
Definition: IndirectListBase.H:297
Foam::IndirectListBase::addressing
const Addr & addressing() const
The addressing used for the list.
Definition: IndirectListBase.H:176
Foam::IndirectListBase::empty
bool empty() const
True if the list is empty (ie, size() is zero).
Definition: IndirectListBase.H:131
Foam::IndirectListBase::const_iterator::pointer
const T * pointer
Definition: IndirectListBase.H:292
IndirectListBaseIO.C
Foam::IndirectListBase::const_iterator::operator!=
bool operator!=(const_iterator &rhs) const
Definition: IndirectListBase.H:322
Foam::IndirectListBase::iterator::operator*
reference operator*() const
Definition: IndirectListBase.H:259
Foam::IndirectListBase::values
const UList< T > & values() const
The list of values (without addressing)
Definition: IndirectListBase.H:164
Foam::IndirectListBase::const_iterator::operator==
bool operator==(const_iterator &rhs) const
Definition: IndirectListBase.H:317
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:236
Foam::IndirectListBase::const_iterator::difference_type
label difference_type
Definition: IndirectListBase.H:290
Foam::IndirectListBase::iterator::pointer
T * pointer
Definition: IndirectListBase.H:245
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:133
Foam::IndirectListBase::first
T & first()
The first element of the list.
Definition: IndirectListBase.H:140
Foam::IndirectListBase::iterator::reference
T & reference
Definition: IndirectListBase.H:246
Foam::operator<<
Ostream & operator<<(Ostream &, const boundaryPatch &p)
Write boundaryPatch as dictionary entries (without surrounding braces)
Definition: boundaryPatch.C:83
Foam::IndirectListBase::reference
T & reference
The type used for storing into value_type objects.
Definition: IndirectListBase.H:90
Foam::IndirectListBase::const_iterator::value_type
const T value_type
Definition: IndirectListBase.H:291
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:247
Foam::IndirectListBase::values
UList< T > & values()
The list of values (without addressing)
Definition: IndirectListBase.H:170
Foam::IndirectListBase::iterator::operator==
bool operator==(iterator &rhs) const
Definition: IndirectListBase.H:270
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:294
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
Foam::IndirectListBase::end
const_iterator end() const
Return a const_iterator at end of list.
Definition: IndirectListBase.H:365
Foam::IndirectListBase::iterator::operator!=
bool operator!=(iterator &rhs) const
Definition: IndirectListBase.H:275
Foam
Namespace for OpenFOAM.
Definition: atmBoundaryLayer.C:33
Foam::IndirectListBase::begin
iterator begin()
Return an iterator at begin of list.
Definition: IndirectListBase.H:332
Foam::IndirectListBase::size_type
label size_type
The type to represent the size of a UList.
Definition: IndirectListBase.H:96
Foam::IndirectListBase::last
const T & last() const
The last element of the list.
Definition: IndirectListBase.H:158
Foam::IndirectListBase::last
T & last()
The last element of the list.
Definition: IndirectListBase.H:152
Foam::IndirectListBase::cend
const_iterator cend() const
Return a const_iterator at end of list.
Definition: IndirectListBase.H:353
Foam::IndirectListBase::iterator::value_type
T value_type
Definition: IndirectListBase.H:244
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::IndirectListBase::first
const T & first() const
The first element of the list.
Definition: IndirectListBase.H:146
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:359
Foam::IndirectListBase::operator=
void operator=(const T &val)
Assign all addressed elements to the given value.
Definition: IndirectListBaseI.H:148
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:131
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:283
Foam::IndirectListBase::iterator::difference_type
label difference_type
Definition: IndirectListBase.H:243
Foam::IndirectListBase::cbegin
const_iterator cbegin() const
Return a const_iterator at begin of list.
Definition: IndirectListBase.H:347
Foam::IndirectListBase::size
label size() const
The number of elements in the list.
Definition: IndirectListBase.H:125
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:264
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