Loading...
Searching...
No Matches
List.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-2025 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::List
29
30Description
31 A 1D array of objects of type <T>, where the size of the vector
32 is known and used for subscript bounds checking, etc.
33
34 Storage is allocated on free-store during construction.
35
36SourceFiles
37 List.C
38 ListI.H
39 ListIO.C
40
41\*---------------------------------------------------------------------------*/
42
43#ifndef Foam_List_H
44#define Foam_List_H
45
46#include "autoPtr.H"
47#include "UList.H"
48#include "SLListFwd.H"
49
50// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
51
52namespace Foam
53{
54
55// Forward Declarations
56template<class T> class List;
57template<class T, unsigned N> class FixedList;
58template<class T, int SizeMin> class DynamicList;
59
60template<class T> class UPtrList;
62template<class T> Istream& operator>>(Istream& is, List<T>& list);
63
64// Common list types
65typedef List<bool> boolList;
66typedef List<char> charList;
67typedef List<label> labelList;
68
69/*---------------------------------------------------------------------------*\
70 Class List Declaration
71\*---------------------------------------------------------------------------*/
72
73template<class T>
74class List
75:
76 public UList<T>
77{
78 // Private Member Functions
79
80 //- Allocate list storage. Assumes there is no existing content
81 inline void doAlloc(const label len);
82
83 //- Copy all list contents. Uses operator[] on the input list
84 template<class ListType>
85 inline void copyList(const ListType& list);
86
87 //- Copy list contents via indirect indices.
88 // Uses operator[] on the values and indices lists
89 template<class ListType, class ListIndices>
90 inline void copyList(const ListType& list, const ListIndices& indices);
91
92 //- Construct given begin/end iterators and number of elements
93 // Since the size is provided, the end iterator is actually ignored.
94 template<class InputIterator>
95 inline List
96 (
97 InputIterator input,
98 InputIterator inputEnd, // (unused)
99 const label len
100 );
101
102 //- Read List from Istream between '(' and ')' delimiters.
103 //- The size is not known a priori.
104 bool readBracketList(Istream& is);
105
106
107 // Methods as per DynamicList to simplify code maintenance
108
109 //- Stub method for internal naming as per DynamicList
110 void setCapacity_nocopy(label len) { resize_nocopy(len); }
111
112
113public:
114
115 // Related types
116
117 //- Declare type of subList
118 typedef SubList<T> subList;
119
120
121 // Static Member Functions
122
123 //- Return a null List (reference to a nullObject).
124 //- Behaves like an empty List.
125 static const List<T>& null() noexcept
126 {
127 return NullObjectRef<List<T>>();
128 }
130
131 // Constructors
132
133 //- Default construct
134 inline constexpr List() noexcept;
135
136 //- Construct with given size
137 explicit List(const label len);
139 //- Construct with given size and value for all elements
140 List(const label len, const T& val);
141
142 //- Construct with given size initializing all elements to zero
143 List(const label len, Foam::zero);
144
145 //- Construct with length=1, copying the value as the only content
146 List(Foam::one, const T& val);
147
148 //- Construct with length=1, moving the value as the only content
149 List(Foam::one, T&& val);
150
151 //- Construct with length=1, initializing content to zero
152 List(Foam::one, Foam::zero);
153
154 //- Copy construct from list
155 List(const List<T>& list);
156
157 //- Copy construct contents from list
158 explicit List(const UList<T>& list);
160 //- Construct as copy or re-use as specified
161 List(List<T>& list, bool reuse);
162
163 //- Copy construct subset of list
164 List(const UList<T>& list, const labelUList& indices);
165
166 //- Copy construct subset of list
167 template<unsigned N>
168 List(const UList<T>& list, const FixedList<label, N>& indices);
170 //- Construct as copy of FixedList<T, N>
171 template<unsigned N>
172 explicit List(const FixedList<T, N>& list);
173
174 //- Construct as copy of UPtrList<T> content
175 explicit List(const UPtrList<T>& list);
176
177 //- Construct as copy of IndirectList contents
178 template<class Addr>
179 explicit List(const IndirectListBase<T, Addr>& list);
180
181 //- Construct from an initializer list
182 List(std::initializer_list<T> list);
183
184 //- Move construct from List
185 inline List(List<T>&& list) noexcept;
186
187 //- Move construct from DynamicList
188 template<int SizeMin>
189 List(DynamicList<T, SizeMin>&& list);
190
191 //- Construct from Istream
192 List(Istream& is);
193
194 //- Clone
195 inline autoPtr<List<T>> clone() const;
196
197
198 //- Destructor
199 ~List();
200
201
202 // Member Functions
203
204 // Sizing
206 //- Clear the list, i.e. set size to zero
207 inline void clear();
208
209 //- Adjust allocated size of list.
210 // The boolList version fills new memory with false.
211 inline void resize(const label len);
212
213 //- Adjust allocated size of list and set val for \em new elements
214 void resize(const label len, const T& val);
215
216 //- Change allocated size of list, retaining the first count elements.
217 // \note Only uses a limited number of internal checks.
218 void resize_copy(label count, const label len);
219
220 //- Adjust allocated size of list and set val for \em all elements
221 inline void resize_fill(const label len, const T& val);
223 //- Adjust allocated size of list \b without necessarily
224 // retaining old content.
225 // If no reallocation is required, the contents remain untouched.
226 // Otherwise the contents will be uninitialized.
227 // Shorthand for \c resize(0, len)
228 inline void resize_nocopy(const label len);
229
230
231 // Edit
233 //- Transfer the contents of the argument List into this list
234 //- and annul the argument list
235 void transfer(List<T>& list);
236
237 //- Transfer the contents of the argument List into this list
238 //- and annul the argument list
239 template<int SizeMin>
240 void transfer(DynamicList<T, SizeMin>& list);
241
242 //- Return subscript-checked element of UList and resizing the list
243 //- if required.
244 inline T& newElmt(const label i);
245
246
247 // Edit
249 //- Construct an element at the end of the list,
250 //- return reference to the new list element.
251 // If this is frequently required, consider a DynamicList instead.
252 template<class... Args>
253 inline T& emplace_back(Args&&... args);
255 //- Append an element at the end of the list
256 // If this is frequently required, consider a DynamicList
257 inline void push_back(const T& val);
258
259 //- Move append an element at the end of the list
260 // If this is frequently required, consider a DynamicList
261 inline void push_back(T&& val);
262
263 //- Append a List to the end of this list
264 // If this is frequently required, consider a DynamicList
265 inline void push_back(const UList<T>& list);
266
267 //- Append IndirectList contents at the end of this list
268 // If this is frequently required, consider a DynamicList
269 template<class Addr>
270 inline void push_back(const IndirectListBase<T, Addr>& list);
272 //- Append an element if not already in the list.
273 // \return the change in list length
274 inline label push_uniq(const T& val);
275
276 //- Reduce size by 1 or more elements. Can be called on an empty list.
277 inline void pop_back(label n = 1);
278
279
280 // Member Operators
281
282 //- Assignment to UList operator. Takes linear time
283 void operator=(const UList<T>& list);
284
285 //- Assignment operator. Takes linear time
286 void operator=(const List<T>& list);
287
288 //- Assignment from IndirectList. Takes linear time
289 template<class Addr>
290 void operator=(const IndirectListBase<T, Addr>& list);
291
292 //- Copy assignment from FixedList
293 template<unsigned N>
294 void operator=(const FixedList<T, N>& list);
295
296 //- Assignment to an initializer list
297 void operator=(std::initializer_list<T> list);
299 //- Assignment of all entries to the given value
300 inline void operator=(const T& val);
301
302 //- Assignment of all entries to zero
303 inline void operator=(Foam::zero);
304
305 //- Move assignment. Takes constant time
306 void operator=(List<T>&& list);
308 //- Move assignment. Takes constant time.
309 template<int SizeMin>
310 void operator=(DynamicList<T, SizeMin>&& list);
311
312
313 // Reading/writing
315 //- Read List from Istream, discarding contents of existing List
317
318
319 // IOstream Operators
321 //- Use the readList() method to read contents from Istream.
322 friend Istream& operator>> <T>
323 (
324 Istream& is,
325 List<T>& list
326 );
327
328
329 // Housekeeping
330
331 //- No shallowCopy permitted
332 void shallowCopy(const UList<T>&) = delete;
333
334
335 // Special Methods
336
337 //- A bitSet::set() method for a list of bool
338 // Increases size when setting an out-of-bounds value.
339 //
340 // \return True if value changed.
341 template
342 <
343 class TypeT = T,
344 class = std::enable_if_t<stdFoam::is_bool_v<TypeT>>
345 >
346 inline bool set(const label i, bool val = true)
347 {
348 if (i < 0)
349 {
350 return false; // Out-of-bounds: ignore
351 }
352 else if (i >= this->size())
354 if (!val) // Unset out-of-bounds: ignore
355 {
356 return false;
357 }
358 this->resize(i+1, false); // Adjust size for assign, fill 0
359 }
360
361 (*this)[i] = val;
362 return true;
363 }
364
365
366 // Housekeeping
367
368 //- Append an element at the end of the list
369 // If this is frequently required, consider a DynamicList
370 //FOAM_DEPRECATED_FOR(2022-10, "push_back()")
371 void append(const T& val) { this->push_back(val); }
372
373 //- Move append an element at the end of the list
374 // If this is frequently required, consider a DynamicList
375 //FOAM_DEPRECATED_FOR(2022-10, "push_back()")
376 void append(T&& val) { this->push_back(std::move(val)); }
377
378 //- Append a List to the end of this list
379 // If this is frequently required, consider a DynamicList
380 //FOAM_DEPRECATED_FOR(2022-10, "push_back()")
381 void append(const UList<T>& list) { this->push_back(list); }
382
383 //- Append IndirectList contents at the end of this list
384 // If this is frequently required, consider a DynamicList
385 //FOAM_DEPRECATED_FOR(2022-10, "push_back()")
386 template<class Addr>
387 void append(const IndirectListBase<T, Addr>& list)
388 {
389 this->push_back(list);
390 }
391
392 //- Same as push_uniq()
393 FOAM_DEPRECATED_FOR(2022-10, "push_uniq()")
394 label appendUniq(const T& val) { return this->push_uniq(val); }
395
396 //- Alias for resize()
397 void setSize(label n) { this->resize(n); }
399 //- Alias for resize()
400 void setSize(label n, const T& val) { this->resize(n, val); }
401
402
403 // Expression templates
404
405 //- Construct from value expression
406 template<typename E>
409 expr.evaluate(*this);
410 }
411
412 //- Assign values from expression
413 template<typename E>
415 {
416 expr.evaluate(*this);
417 }
419
420
421// * * * * * * * * * * * * Template Specializations * * * * * * * * * * * * //
422
423//- Hashing for List data
424template<class T>
425struct Hash<List<T>> : List<T>::hasher {};
426
427
428// * * * * * * * * * * * * * * * IOstream Operators * * * * * * * * * * * * //
429
430//- Read List contents from Istream
431template<class T>
432Istream& operator>>(Istream& is, List<T>& list)
433{
434 return list.readList(is);
435}
436
437
438// * * * * * * * * * * * * * * Global Functions * * * * * * * * * * * * * * //
439
440//- Return an identity map of the given length with (map[i] == i),
441//- works like std::iota() but returning a list of label values.
442// Optionally with an alternative start index, so that (map[i] == i+start)
443labelList identity(const label len, label start=0);
444
445//- Return an identity map corresponding to the index range (start, size)
446template<class IntType>
448{
449 return Foam::identity(label(range.size()), label(range.start()));
450}
451
453//- Return the (stable) sort order for the list
454template<class T>
455labelList sortedOrder(const UList<T>& input);
456
457//- Generate the (stable) sort order for the list
458template<class T>
459void sortedOrder(const UList<T>& input, labelList& order);
460
461//- Sort using specified list compare predicate
462template<class T, class ListComparePredicate>
463void sortedOrder
464(
465 const UList<T>& input,
466 labelList& order,
467 const ListComparePredicate& comp
468);
470
471// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
472
473} // End namespace Foam
474
475// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
476
477#include "ListI.H"
478
479// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
480
481#ifdef NoRepository
482 #include "List.C"
483 #include "ListIO.C"
484#endif
485
486// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
487
488#endif
489
490// ************************************************************************* //
scalar range
Forward declarations for SLList.
label n
A 1D vector of objects of type <T> that resizes itself as necessary to accept the new objects.
Definition DynamicList.H:68
Expression templates for List.
A 1D vector of objects of type <T> with a fixed length <N>.
Definition FixedList.H:73
Base for lists with indirect addressing, templated on the list contents type and the addressing type....
An interval of (signed) integers defined by a start and a size.
Definition IntRange.H:69
An Istream is an abstract base class for all input systems (streams, files, token lists etc)....
Definition Istream.H:60
A 1D array of objects of type <T>, where the size of the vector is known and used for subscript bound...
Definition List.H:72
void transfer(List< block_type > &list)
block_type & newElmt(const label i)
SubList< block_type > subList
Definition List.H:129
autoPtr< List< block_type > > clone() const
void pop_back(label n=1)
label appendUniq(const block_type &val)
Definition List.H:531
void resize_nocopy(const label len)
Adjust allocated size of list without necessarily.
Definition ListI.H:171
void operator=(const UList< T > &list)
Assignment to UList operator. Takes linear time.
Definition List.C:381
block_type & emplace_back(Args &&... args)
bool set(const label i, bool val=true)
Definition List.H:469
void append(const IndirectListBase< T, Addr > &list)
Append IndirectList contents at the end of this list.
Definition List.H:522
List(const Expression::ListExpression< E > &expr)
Construct from value expression.
Definition List.H:550
constexpr List() noexcept
Default construct.
Definition ListI.H:108
void resize_copy(label count, const label len)
void append(const T &val)
Append an element at the end of the list.
Definition List.H:497
void shallowCopy(const UList< block_type > &)=delete
void append(T &&val)
Move append an element at the end of the list.
Definition List.H:505
label push_uniq(const block_type &val)
void operator=(const Expression::ListExpression< E > &expr)
Assign values from expression.
Definition List.H:559
void push_back(const block_type &val)
void setSize(label n)
Alias for resize().
Definition List.H:536
void resize(const label len)
void resize_fill(const label len, const block_type &val)
Istream & readList(Istream &is)
void setSize(label n, const T &val)
Alias for resize().
Definition List.H:541
void append(const UList< T > &list)
Append a List to the end of this list.
Definition List.H:513
static const List< T > & null() noexcept
Return a null List (reference to a nullObject). Behaves like an empty List.
Definition List.H:138
A non-owning sub-view of a List (allocated or unallocated storage).
Definition SubList.H:61
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:89
auto expr() const
Wrap value as expression.
Definition UList.H:999
UList(const UList< T > &) noexcept=default
Copy construct, shallow copy.
label size() const noexcept
The number of elements in the container.
Definition UList.H:706
A list of pointers to objects of type <T>, without allocation/deallocation management of the pointers...
Definition UPtrList.H:101
Pointer management similar to std::unique_ptr, with some additional methods and type checking.
Definition autoPtr.H:65
A class representing the concept of 1 (one) that can be used to avoid manipulating objects known to b...
Definition one.H:57
A class representing the concept of 0 (zero) that can be used to avoid manipulating objects known to ...
Definition zero.H:58
Namespace for OpenFOAM.
const T & NullObjectRef() noexcept
Const reference (of type T) to the nullObject.
Definition nullObject.H:228
List< label > labelList
A List of labels.
Definition List.H:62
Istream & operator>>(Istream &, directionInfo &)
labelList sortedOrder(const UList< T > &input)
Return the (stable) sort order for the list.
labelList identity(const label len, label start=0)
Return an identity map of the given length with (map[i] == i), works like std::iota() but returning a...
List< bool > boolList
A List of bools.
Definition List.H:60
const direction noexcept
Definition scalarImpl.H:265
UList< label > labelUList
A UList of labels.
Definition UList.H:75
void T(FieldField< Field, Type > &f1, const FieldField< Field, Type > &f2)
List< char > charList
A List of chars.
Definition List.H:61
Namespace for std templates that are are part of future C++ standards or that are in a state of chang...
Definition stdFoam.H:175
Foam::argList args(argc, argv)
#define FOAM_DEPRECATED_FOR(since, replacement)
Definition stdFoam.H:43
Hash function class. The default definition is for primitives. Non-primitives used to hash entries on...
Definition Hash.H:48
Deprecated(2021-04) hashing functor. Use hasher().
Definition UList.H:945
Hashing functor for UList.
Definition UList.H:915
const Vector< label > N(dict.get< Vector< label > >("N"))