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  //- Find index of the first occurrence of the value.
183  // Any occurrences before the start pos are ignored.
184  // Linear search.
185  // \return -1 if not found.
186  label find(const T& val, label pos = 0) const;
187 
188  //- Find index of the last occurrence of the value.
189  // Any occurrences after the end pos are ignored.
190  // Linear search.
191  // \return -1 if not found.
192  label rfind(const T& val, label pos = -1) const;
193 
194  //- Is the value contained in the list?
195  // Linear search from start pos until the end of the list.
196  // Any occurrences before the start pos are ignored.
197  // \return true if found.
198  inline bool contains(const T& val, label pos = 0) const;
199 
200 
201  // Member Operators
202 
203  //- Return the addressed elements as a List
204  List<T> operator()() const { return this->list(); }
205 
206  //- Non-const access to an element in the list
207  inline T& operator[](const label i);
208 
209  //- Const access to an element in the list
210  inline const T& operator[](const label i) const;
211 
212  //- Assign all addressed elements to the given value
213  inline void operator=(const T& val);
214 
215  //- Assignment of all entries to zero
216  inline void operator=(const Foam::zero);
217 
218  //- Deep copy values from a list of the addressed elements
219  // Fatal if list sizes are not identical
220  inline void operator=(const UList<T>& rhs);
221 
222  //- Deep copy values from a list of the addressed elements
223  // Fatal if list sizes are not identical
224  inline void operator=(const IndirectListBase<T, Addr>& rhs);
225 
226  //- Deep copy values from a list of the addressed elements
227  // Fatal if list sizes are not identical
228  template<class AnyAddr>
229  inline void operator=(const IndirectListBase<T, AnyAddr>& rhs);
230 
231 
232  // Iterators
233 
234  //- A non-const iterator for an indirect list
235  // Only supports forward prefix increment, since the addressing
236  // may/may not support postfix or decrement.
237  class iterator
238  {
239  T* begin_;
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 
250  iterator
251  (
252  UList<T>& list,
253  typename addressing_type::const_iterator addrIter
254  )
255  :
256  begin_(list.data()),
257  iter_(addrIter)
258  {}
259 
260  reference operator*() const { return *(begin_ + *iter_); }
261 
263  {
264  ++iter_;
265  return *this;
266  }
267 
268  bool operator==(const iterator& rhs) const
269  {
270  return (iter_ == rhs.iter_);
271  }
272 
273  bool operator!=(const iterator& rhs) const
274  {
275  return (iter_ != rhs.iter_);
276  }
277  };
278 
279 
280  //- A const iterator for an indirect list
281  // Only supports forward prefix increment, since the addressing
282  // may/may not support postfix or decrement.
283  class const_iterator
284  {
285  const T* begin_;
286  typename addressing_type::const_iterator iter_;
287 
288  public:
289 
290  using difference_type = label;
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 addrIter
300  )
301  :
302  begin_(list.cdata()),
303  iter_(addrIter)
304  {}
305 
306  reference operator*() const { return *(begin_ + *iter_); }
307 
309  {
310  ++iter_;
311  return *this;
312  }
313 
314  bool operator==(const const_iterator& rhs) const
315  {
316  return (iter_ == rhs.iter_);
317  }
318 
319  bool operator!=(const const_iterator& rhs) const
320  {
321  return (iter_ != rhs.iter_);
322  }
323  };
324 
325 
326  // Iterator (non-const)
328  //- Return an iterator at begin of list
329  inline iterator begin()
330  {
331  return iterator(values_, addr_.cbegin());
332  }
333 
334  //- Return an iterator at end of list
335  inline iterator end()
336  {
337  return iterator(values_, addr_.cend());
338  }
339 
340 
341  // Iterator (const)
342 
343  //- Return a const_iterator at begin of list
344  inline const_iterator cbegin() const
345  {
346  return const_iterator(values_, addr_.cbegin());
347  }
348 
349  //- Return a const_iterator at end of list
350  inline const_iterator cend() const
351  {
352  return const_iterator(values_, addr_.cend());
353  }
354 
355  //- Return a const_iterator at begin of list
356  const_iterator begin() const { return cbegin(); }
357 
358  //- Return a const_iterator at end of list
359  const_iterator end() const { return cend(); }
360 
361 
362  // Writing
364  //- Write List, with line-breaks in ASCII when length exceeds shortLen.
365  // Using '0' suppresses line-breaks entirely.
366  Ostream& writeList(Ostream& os, const label shortLen=0) const;
367 
368 
369  // Housekeeping
370 
371  //- Access first element of the list, position [0]
372  //FOAM_DEPRECATED_FOR(2022-10, "front()")
373  T& first() { return front(); }
374 
375  //- Access first element of the list
376  //FOAM_DEPRECATED_FOR(2022-10, "front()")
377  const T& first() const { return front(); };
378 
379  //- Access last element of the list, position [size()-1]
380  //FOAM_DEPRECATED_FOR(2022-10, "back()")
381  T& last() { return back(); }
382 
383  //- Access last element of the list, position [size()-1]
384  //FOAM_DEPRECATED_FOR(2022-10, "back()")
385  const T& last() const { return back(); };
387  //- Same as contains()
388  bool found(const T& val, label pos = 0) const
389  {
390  return this->contains(val, pos);
391  }
392 };
393 
394 
395 // * * * * * * * * * * * * * * * IOstream Operators * * * * * * * * * * * * //
396 
397 //- Write List to Ostream, as per UList::writeList() with default length.
398 // The default short-length is given by Detail::ListPolicy::short_length
399 template<class T, class Addr>
400 Ostream& operator<<(Ostream& os, const IndirectListBase<T, Addr>& list)
401 {
403 }
404 
405 
406 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
408 } // End namespace Foam
409 
410 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
411 
413 
414 #ifdef NoRepository
415  #include "IndirectListBase.C"
416  #include "IndirectListBaseIO.C"
417 #endif
418 
419 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
420 
421 #endif
422 
423 // ************************************************************************* //
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.
bool contains(const T &val, label pos=0) const
Is the value contained in the list?
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.
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:72
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.
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
label find(const T &val, label pos=0) const
Find index of the first occurrence of the value.
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:99
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:55
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)
Database for solution data, solver performance and other reduced data.
Definition: data.H:52
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:335
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)