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-2022 OpenCFD Ltd.
10-------------------------------------------------------------------------------
11License
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
27Class
28 Foam::IndirectListBase
29
30Description
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
35SourceFiles
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
49namespace Foam
50{
51
52/*---------------------------------------------------------------------------*\
53 Class IndirectListBase Declaration
54\*---------------------------------------------------------------------------*/
55
56template<class T, class Addr>
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
68protected:
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
77public:
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 // Only supports forward prefix increment, since the addressing
246 // may/may not support postfix or decrement.
247 class iterator
248 {
249 T* begin_;
250 typename addressing_type::const_iterator iter_;
251
252 public:
254 using difference_type = label;
255 using value_type = T;
256 using pointer = T*;
257 using reference = T&;
258 using iterator_category = std::forward_iterator_tag;
261 (
262 UList<T>& list,
263 typename addressing_type::const_iterator addrIter
264 )
265 :
266 begin_(list.data()),
267 iter_(addrIter)
268 {}
270 reference operator*() const { return *(begin_ + *iter_); }
273 {
274 ++iter_;
275 return *this;
276 }
278 bool operator==(const iterator& rhs) const
279 {
280 return (iter_ == rhs.iter_);
281 }
283 bool operator!=(const iterator& rhs) const
284 {
285 return (iter_ != rhs.iter_);
286 }
287 };
288
289
290 //- A const iterator for an indirect list
291 // Only supports forward prefix increment, since the addressing
292 // may/may not support postfix or decrement.
293 class const_iterator
294 {
295 const T* begin_;
296 typename addressing_type::const_iterator iter_;
297
298 public:
300 using difference_type = label;
301 using value_type = const T;
302 using pointer = const T*;
303 using reference = const T&;
304 using iterator_category = std::forward_iterator_tag;
307 (
308 const UList<T>& list,
309 typename addressing_type::const_iterator addrIter
310 )
311 :
312 begin_(list.cdata()),
313 iter_(addrIter)
314 {}
316 reference operator*() const { return *(begin_ + *iter_); }
319 {
320 ++iter_;
321 return *this;
322 }
324 bool operator==(const const_iterator& rhs) const
325 {
326 return (iter_ == rhs.iter_);
327 }
329 bool operator!=(const const_iterator& rhs) const
330 {
331 return (iter_ != rhs.iter_);
332 }
333 };
334
335
336 // Iterator (non-const)
337
338 //- Return an iterator at begin of list
339 inline iterator begin()
340 {
341 return iterator(values_, addr_.cbegin());
342 }
343
344 //- Return an iterator at end of list
345 inline iterator end()
346 {
347 return iterator(values_, addr_.cend());
348 }
349
350
351 // Iterator (const)
352
353 //- Return a const_iterator at begin of list
354 inline const_iterator cbegin() const
355 {
356 return const_iterator(values_, addr_.cbegin());
357 }
358
359 //- Return a const_iterator at end of list
360 inline const_iterator cend() const
361 {
362 return const_iterator(values_, addr_.cend());
363 }
364
365 //- Return a const_iterator at begin of list
366 const_iterator begin() const { return cbegin(); }
367
368 //- Return a const_iterator at end of list
369 const_iterator end() const { return cend(); }
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
384template<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// ************************************************************************* //
bool found
A const iterator for an indirect list.
std::forward_iterator_tag iterator_category
bool operator!=(const const_iterator &rhs) const
bool operator==(const const_iterator &rhs) const
const_iterator(const UList< T > &list, typename addressing_type::const_iterator addrIter)
A non-const iterator for an indirect list.
bool operator!=(const iterator &rhs) const
std::forward_iterator_tag iterator_category
bool operator==(const iterator &rhs) const
iterator(UList< T > &list, typename addressing_type::const_iterator addrIter)
Base for lists with indirect addressing, templated on the list contents type and the addressing type....
label size_type
The type to represent the size of a UList.
void copyList(const ListType &rhs)
Deep copy values from the list.
label difference_type
The difference between iterator objects.
T value_type
Type of values the list contains.
label rcIndex(const label i) const
const_iterator begin() const
Return a const_iterator at begin of list.
const_iterator cbegin() const
Return a const_iterator at begin of list.
void operator=(const T &val)
Assign all addressed elements to the given value.
bool empty() const noexcept
True if the list is empty (ie, size() is zero).
const T & rcValue(const label i) const
Return reverse circular value (ie, previous value in the list)
label fcIndex(const label i) const
const T * const_pointer
The pointer type for const access to value_type items.
T * pointer
The pointer type for non-const access to value_type items.
label rfind(const T &val, label pos=-1) const
Find index of the last occurrence of the value.
const T & last() const
The last element of the list.
const Addr & addressing() const noexcept
The addressing used for the list.
const T & fcValue(const label i) const
Return forward circular value (ie, next value in the list)
IndirectListBase()=delete
No default construct.
label find(const T &val, label pos=0) const
Find index of the first occurrence of the value.
const_iterator cend() const
Return a const_iterator at end of list.
T & reference
The type used for storing into value_type objects.
const T & first() const
The first element of the list.
bool uniform() const
True if all entries have identical values, and list is non-empty.
const UList< T > & values() const noexcept
The list of values (without addressing)
label size() const noexcept
The number of elements in the list.
UList< T > & values() noexcept
The list of values (without addressing)
Ostream & writeList(Ostream &os, const label shortLen=0) const
Write List, with line-breaks in ASCII when length exceeds shortLen.
iterator end()
Return an iterator at end of list.
const_iterator end() const
Return a const_iterator at end of list.
List< T > operator()() const
Return the addressed elements as a List.
iterator begin()
Return an iterator at begin of list.
Addr addressing_type
The addressing type (non-stl definition)
T & operator[](const label i)
Non-const access to an element in the list.
const T & const_reference
The type used for reading from constant value_type objects.
A 1D array of objects of type <T>, where the size of the vector is known and used for subscript bound...
Definition: List.H:77
An Ostream is an abstract base class for all output systems (streams, files, token lists,...
Definition: Ostream.H:62
A 1D vector of objects of type <T>, where the size of the vector is known and can be used for subscri...
Definition: UList.H:94
const_iterator cend() const noexcept
Return const_iterator to end traversing the constant UList.
Definition: UListI.H:364
const_iterator cbegin() const noexcept
Return const_iterator to begin traversing the constant UList.
Definition: UListI.H:343
Database for solution data, solver performance and other reduced data.
Definition: data.H:58
A class representing the concept of 0 (zero) that can be used to avoid manipulating objects known to ...
Definition: zero.H:63
const volScalarField & T
OBJstream os(runTime.globalPath()/outputName)
Namespace for OpenFOAM.
dimensionedScalar pos(const dimensionedScalar &ds)
Ostream & operator<<(Ostream &, const boundaryPatch &p)
Write boundaryPatch as dictionary entries (without surrounding braces)
Definition: boundaryPatch.C:83
const direction noexcept
Definition: Scalar.H:223
Number of items before requiring line-breaks in the list output.
Definition: ListPolicy.H:61