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-2023 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 Foam_IndirectListBase_H
43 #define Foam_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;
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  //- True if the list is empty (ie, size() is zero).
126  bool empty() const noexcept { return addr_.empty(); }
127 
128  //- The number of elements in the list
129  label size() const noexcept { return addr_.size(); }
130 
131  //- The list of values (without addressing)
132  const UList<T>& values() const noexcept { return values_; }
133 
134  //- The list of values (without addressing)
135  UList<T>& values() noexcept { return values_; }
136 
137  //- The addressing used for the list
138  const Addr& addressing() const noexcept { return addr_; }
139 
140 
141  //- True if all entries have identical values, and list is non-empty
142  inline bool uniform() const;
143 
144  //- The first element of the list.
145  inline const T& front() const;
146 
147  //- The first element of the list.
148  inline T& front();
149 
150  //- The last element of the list.
151  inline const T& back() const;
152 
153  //- The last element of the list.
154  inline T& back();
155 
156  //- The forward circular index. The next index in the list
157  //- which returns to the first at the end of the list
158  inline label fcIndex(const label i) const;
159 
160  //- The reverse circular index. The previous index in the list
161  //- which returns to the last at the beginning of the list
162  inline label rcIndex(const label i) const;
164  //- Return forward circular value (ie, next value in the list)
165  inline const T& fcValue(const label i) const;
166 
167  //- Return forward circular value (ie, next value in the list)
168  inline T& fcValue(const label i);
169 
170  //- Return reverse circular value (ie, previous value in the list)
171  inline const T& rcValue(const label i) const;
172 
173  //- Return reverse circular value (ie, previous value in the list)
174  inline T& rcValue(const label i);
175 
176  //- Return the addressed elements as a List
177  inline List<T> list() const;
178 
179 
180  // Search
181 
182  //- Is the value contained in the list?
183  // \param val The value to search for
184  // \param pos The first position to examine (default: 0, no-op if -ve)
185  // \param len The length of the search region (-ve until the end)
186  // \return true if found.
187  inline bool contains(const T& val, label pos = 0, label len = -1) const;
188 
189  //- Find index of the first occurrence of the value.
190  // \param val The value to search for
191  // \param pos The first position to examine (default: 0, no-op if -ve)
192  // \param len The length of the search region (-ve until the end)
193  // \return position in list or -1 if not found.
194  label find(const T& val, label pos = 0, label len = -1) const;
195 
196  //- Find index of the last occurrence of the value.
197  // Any occurrences after the end pos are ignored.
198  // Linear search.
199  // \return -1 if not found.
200  label rfind(const T& val, label pos = -1) const;
201 
202 
203  // Member Operators
204 
205  //- Return the addressed elements as a List
206  List<T> operator()() const { return this->list(); }
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  // Only supports forward prefix increment, since the addressing
238  // may/may not support postfix or decrement.
239  class iterator
240  {
241  T* begin_;
242  typename addressing_type::const_iterator iter_;
243 
244  public:
245 
246  using difference_type = label;
247  using value_type = T;
248  using pointer = T*;
249  using reference = T&;
250  using iterator_category = std::forward_iterator_tag;
251 
252  iterator
253  (
254  UList<T>& list,
255  typename addressing_type::const_iterator addrIter
256  )
257  :
258  begin_(list.data()),
259  iter_(addrIter)
260  {}
261 
262  reference operator*() const { return *(begin_ + *iter_); }
263 
265  {
266  ++iter_;
267  return *this;
268  }
269 
270  bool operator==(const iterator& rhs) const
271  {
272  return (iter_ == rhs.iter_);
273  }
274 
275  bool operator!=(const iterator& rhs) const
276  {
277  return (iter_ != rhs.iter_);
278  }
279  };
280 
281 
282  //- A const iterator for an indirect list
283  // Only supports forward prefix increment, since the addressing
284  // may/may not support postfix or decrement.
285  class const_iterator
286  {
287  const T* begin_;
288  typename addressing_type::const_iterator iter_;
289 
290  public:
291 
292  using difference_type = label;
293  using value_type = const T;
294  using pointer = const T*;
295  using reference = const T&;
296  using iterator_category = std::forward_iterator_tag;
297 
299  (
300  const UList<T>& list,
301  typename addressing_type::const_iterator addrIter
302  )
303  :
304  begin_(list.cdata()),
305  iter_(addrIter)
306  {}
307 
308  reference operator*() const { return *(begin_ + *iter_); }
309 
311  {
312  ++iter_;
313  return *this;
314  }
315 
316  bool operator==(const const_iterator& rhs) const
317  {
318  return (iter_ == rhs.iter_);
319  }
320 
321  bool operator!=(const const_iterator& rhs) const
322  {
323  return (iter_ != rhs.iter_);
324  }
325  };
326 
327 
328  // Iterator (non-const)
330  //- Return an iterator at begin of list
331  inline iterator begin()
332  {
333  return iterator(values_, addr_.cbegin());
334  }
335 
336  //- Return an iterator at end of list
337  inline iterator end()
338  {
339  return iterator(values_, addr_.cend());
340  }
341 
342 
343  // Iterator (const)
344 
345  //- Return a const_iterator at begin of list
346  inline const_iterator cbegin() const
347  {
348  return const_iterator(values_, addr_.cbegin());
349  }
350 
351  //- Return a const_iterator at end of list
352  inline const_iterator cend() const
353  {
354  return const_iterator(values_, addr_.cend());
355  }
356 
357  //- Return a const_iterator at begin of list
358  const_iterator begin() const { return cbegin(); }
359 
360  //- Return a const_iterator at end of list
361  const_iterator end() const { return cend(); }
362 
363 
364  // Writing
366  //- Write List, with line-breaks in ASCII when length exceeds shortLen.
367  // Using '0' suppresses line-breaks entirely.
368  Ostream& writeList(Ostream& os, const label shortLen=0) const;
369 
370 
371  // Housekeeping
372 
373  //- Access first element of the list, position [0]
374  //FOAM_DEPRECATED_FOR(2022-10, "front()")
375  T& first() { return front(); }
376 
377  //- Access first element of the list
378  //FOAM_DEPRECATED_FOR(2022-10, "front()")
379  const T& first() const { return front(); };
380 
381  //- Access last element of the list, position [size()-1]
382  //FOAM_DEPRECATED_FOR(2022-10, "back()")
383  T& last() { return back(); }
384 
385  //- Access last element of the list, position [size()-1]
386  //FOAM_DEPRECATED_FOR(2022-10, "back()")
387  const T& last() const { return back(); };
389  //- Same as contains()
390  bool found(const T& val, label pos = 0) const
391  {
392  return this->contains(val, pos);
393  }
394 };
395 
396 
397 // * * * * * * * * * * * * * * * IOstream Operators * * * * * * * * * * * * //
398 
399 //- Write List to Ostream, as per UList::writeList() with default length.
400 // The default short-length is given by Detail::ListPolicy::short_length
401 template<class T, class Addr>
402 Ostream& operator<<(Ostream& os, const IndirectListBase<T, Addr>& list)
403 {
405 }
406 
407 
408 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
410 } // End namespace Foam
411 
412 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
413 
415 
416 #ifdef NoRepository
417  #include "IndirectListBase.C"
418  #include "IndirectListBaseIO.C"
419 #endif
420 
421 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
422 
423 #endif
424 
425 // ************************************************************************* //
label difference_type
The difference between iterator objects.
bool found(const T &val, label pos=0) const
Same as contains()
T & reference
The type used for storing into value_type objects.
const UList< T > & values() const noexcept
The list of values (without addressing)
T & last()
Access last element of the list, position [size()-1].
Addr addressing_type
The addressing type (non-stl definition)
Number of items before requiring line-breaks in the list output.
Definition: ListPolicy.H:57
label rfind(const T &val, label pos=-1) const
Find index of the last occurrence of the value.
label find(const T &val, label pos=0, label len=-1) const
Find index of the first occurrence of the value.
bool operator==(const const_iterator &rhs) const
A 1D array of objects of type <T>, where the size of the vector is known and used for subscript bound...
Definition: BitOps.H:56
const T & back() const
The last element of the list.
std::forward_iterator_tag iterator_category
void operator=(const T &val)
Assign all addressed elements to the given value.
const_iterator cbegin() const
Return a const_iterator at begin of list.
iterator(UList< T > &list, typename addressing_type::const_iterator addrIter)
T & first()
Access first element of the list, position [0].
Base for lists with indirect addressing, templated on the list contents type and the addressing type...
IndirectListBase()=delete
No default construct.
bool uniform() const
True if all entries have identical values, and list is non-empty.
T & operator[](const label i)
Non-const access to an element in the list.
bool operator!=(const const_iterator &rhs) const
dimensionedScalar pos(const dimensionedScalar &ds)
Ostream & writeList(Ostream &os, const label shortLen=0) const
Write List, with line-breaks in ASCII when length exceeds shortLen.
Definition: UListIO.C:76
const_iterator cend() const
Return a const_iterator at end of list.
const_iterator(const UList< T > &list, typename addressing_type::const_iterator addrIter)
A const iterator for an indirect list.
bool contains(const T &val, label pos=0, label len=-1) const
Is the value contained in the list?
const T * const_pointer
The pointer type for const access to value_type items.
label size() const noexcept
The number of elements in the list.
bool operator!=(const iterator &rhs) const
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:105
const T & front() const
The first element of the list.
An Ostream is an abstract base class for all output systems (streams, files, token lists...
Definition: Ostream.H:56
const direction noexcept
Definition: Scalar.H:258
const Addr & addressing() const noexcept
The addressing used for the list.
List< T > operator()() const
Return the addressed elements as a List.
OBJstream os(runTime.globalPath()/outputName)
iterator begin()
Return an iterator at begin of list.
void T(FieldField< Field, Type > &f1, const FieldField< Field, Type > &f2)
bool empty() const noexcept
True if the list is empty (ie, size() is zero).
iterator end()
Return an iterator at end of list.
label size_type
The type to represent the size of a UList.
label rcIndex(const label i) const
The reverse circular index. The previous index in the list which returns to the last at the beginning...
const T & const_reference
The type used for reading from constant value_type objects.
T value_type
Type of values the list contains.
A non-const iterator for an indirect list.
label fcIndex(const label i) const
The forward circular index. The next index in the list which returns to the first at the end of the l...
A class representing the concept of 0 (zero) that can be used to avoid manipulating objects known to ...
Definition: zero.H:57
const_iterator cbegin() const noexcept
Return const_iterator to begin traversing the constant UList.
Definition: UListI.H:405
T * pointer
The pointer type for non-const access to value_type items.
void copyList(const ListType &rhs)
Deep copy values from the list.
bool operator==(const iterator &rhs) const
const T & fcValue(const label i) const
Return forward circular value (ie, next value in the list)
Ostream & writeList(Ostream &os, const label shortLen=0) const
Write List, with line-breaks in ASCII when length exceeds shortLen.
std::forward_iterator_tag iterator_category
List< T > list() const
Return the addressed elements as a List.
Namespace for OpenFOAM.
const T & rcValue(const label i) const
Return reverse circular value (ie, previous value in the list)