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-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::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 PtrList;
61
62template<class T> Istream& operator>>(Istream& is, List<T>& list);
63
64// Common list types
67typedef List<label> labelList;
68
69
70/*---------------------------------------------------------------------------*\
71 Class List Declaration
72\*---------------------------------------------------------------------------*/
73
74template<class T>
75class List
76:
77 public UList<T>
78{
79 // Private Member Functions
80
81 //- Allocate list storage
82 inline void doAlloc();
83
84 //- Reallocate list storage to the given size
85 // Discards old storage (if any). Does not copy old contents
86 inline void reAlloc(const label len);
87
88 //- Copy all list contents
89 template<class List2>
90 inline void copyList(const List2& list);
91
92 //- Change allocation size of List, retaining old contents.
93 // Backend for resize
94 void doResize(const label len);
95
96 //- Construct given begin/end iterators and number of elements
97 // Since the size is provided, the end iterator is actually ignored.
98 template<class InputIterator>
99 inline List
100 (
101 InputIterator begIter,
102 InputIterator endIter,
103 const label len
104 );
105
106
107public:
108
109 // Related types
110
111 //- Declare type of subList
112 typedef SubList<T> subList;
113
114
115 // Static Member Functions
116
117 //- Return a null List
118 inline static const List<T>& null();
119
120
121 // Constructors
122
123 //- Default construct
124 inline constexpr List() noexcept;
125
126 //- Construct with given size
127 explicit List(const label len);
128
129 //- Construct with given size and value for all elements
130 List(const label len, const T& val);
131
132 //- Construct with given size initializing all elements to zero
133 List(const label len, const Foam::zero);
134
135 //- Construct with length=1, copying the value as the only content
136 List(const Foam::one, const T& val);
137
138 //- Construct with length=1, moving the value as the only content
139 List(const Foam::one, T&& val);
140
141 //- Construct with length=1, initializing content to zero
142 List(const Foam::one, const Foam::zero);
143
144 //- Copy construct from list
145 List(const List<T>& a);
146
147 //- Copy construct contents from list
148 explicit List(const UList<T>& a);
149
150 //- Construct as copy or re-use as specified
151 List(List<T>& a, bool reuse);
152
153 //- Copy construct subset of list
154 List(const UList<T>& list, const labelUList& indices);
155
156 //- Copy construct subset of list
157 template<unsigned N>
158 List(const UList<T>& list, const FixedList<label, N>& indices);
159
160 //- Construct as copy of FixedList<T, N>
161 template<unsigned N>
162 explicit List(const FixedList<T, N>& list);
163
164 //- Construct as copy of PtrList<T>
165 explicit List(const PtrList<T>& list);
166
167 //- Construct as copy of SLList<T>
168 explicit List(const SLList<T>& list);
169
170 //- Construct as copy of IndirectList contents
171 template<class Addr>
172 explicit List(const IndirectListBase<T, Addr>& list);
173
174 //- Construct from an initializer list
175 List(std::initializer_list<T> list);
176
177 //- Move construct from List
178 List(List<T>&& list);
179
180 //- Move construct from DynamicList
181 template<int SizeMin>
182 List(DynamicList<T, SizeMin>&& list);
183
184 //- Move construct from SLList
185 List(SLList<T>&& list);
186
187 //- Construct from Istream
188 List(Istream& is);
189
190 //- Clone
191 inline autoPtr<List<T>> clone() const;
192
193
194 //- Destructor
195 ~List();
196
197
198 // Member Functions
199
200 // Sizing
201
202 //- Clear the list, i.e. set size to zero
203 inline void clear();
204
205 //- Adjust allocated size of list.
206 // The boolList version fills new memory with false.
207 inline void resize(const label len);
208
209 //- Adjust allocated size of list and set val for new elements
210 void resize(const label len, const T& val);
211
212 //- Adjust allocated size of list \b without necessarily
213 // retaining old content.
214 // If no reallocation is required, the contents remain untouched.
215 // Otherwise the contents will be uninitialized.
216 inline void resize_nocopy(const label len);
217
218 //- Alias for resize()
219 void setSize(const label n) { this->resize(n); }
220
221 //- Alias for resize()
222 void setSize(const label n, const T& val) { this->resize(n, val); }
223
224
225 // Edit
226
227 //- Append an element at the end of the list
228 // If this is frequently required, consider a DynamicList
229 inline void append(const T& val);
230
231 //- Move append an element at the end of the list
232 // If this is frequently required, consider a DynamicList
233 inline void append(T&& val);
234
235 //- Append a List to the end of this list
236 // If this is frequently required, consider a DynamicList
237 inline void append(const UList<T>& list);
238
239 //- Append IndirectList contents at the end of this list
240 // If this is frequently required, consider a DynamicList
241 template<class Addr>
242 inline void append(const IndirectListBase<T, Addr>& list);
243
244 //- Append an element if not already in the list.
245 // \return the change in list length
246 inline label appendUniq(const T& val);
247
248 //- Transfer the contents of the argument List into this list
249 //- and annul the argument list
250 void transfer(List<T>& list);
251
252 //- Transfer the contents of the argument List into this list
253 //- and annul the argument list
254 template<int SizeMin>
256
257 //- Return subscript-checked element of UList and resizing the list
258 //- if required.
259 inline T& newElmt(const label i);
260
261
262 // Member Operators
263
264 //- Assignment to UList operator. Takes linear time
265 void operator=(const UList<T>& a);
266
267 //- Assignment operator. Takes linear time
268 void operator=(const List<T>& list);
269
270 //- Assignment to SLList operator. Takes linear time
271 void operator=(const SLList<T>& list);
272
273 //- Assignment from IndirectList. Takes linear time
274 template<class Addr>
275 void operator=(const IndirectListBase<T, Addr>& list);
276
277 //- Copy assignment from FixedList
278 template<unsigned N>
279 void operator=(const FixedList<T, N>& list);
280
281 //- Assignment to an initializer list
282 void operator=(std::initializer_list<T> list);
283
284 //- Assignment of all entries to the given value
285 inline void operator=(const T& val);
286
287 //- Assignment of all entries to zero
288 inline void operator=(const Foam::zero);
289
290 //- Move assignment. Takes constant time
291 void operator=(List<T>&& list);
292
293 //- Move assignment. Takes constant time.
294 template<int SizeMin>
296
297 //- Move assignment. Takes constant time
298 void operator=(SLList<T>&& list);
299
300
301 // Reading/writing
302
303 //- Read List from Istream, discarding contents of existing List
305
306
307 // IOstream Operators
308
309 //- Use the readList() method to read contents from Istream.
310 friend Istream& operator>> <T>
311 (
312 Istream& is,
313 List<T>& list
314 );
315
316
317 // Housekeeping
318
319 //- No shallowCopy permitted
320 void shallowCopy(const UList<T>&) = delete;
321
322
323 // Special Methods
324
325 //- A bitSet::set() method for a list of bool
326 // Increases size when setting an out-of-bounds value.
327 //
328 // \return True if value changed.
329 template<class TypeT = T>
330 typename std::enable_if<std::is_same<bool, TypeT>::value, bool>::type
331 inline set(const label i, bool val = true)
332 {
333 if (i < 0)
334 {
335 return false; // Out-of-bounds: ignore
336 }
337 else if (i >= this->size())
338 {
339 if (!val) // Unset out-of-bounds: ignore
340 {
341 return false;
342 }
343 this->resize(i+1, false); // Adjust size for assign, fill 0
344 }
345
346 (*this)[i] = val;
347 return true;
348 }
349};
350
351
352// * * * * * * * * * * * * Template Specializations * * * * * * * * * * * * //
353
354//- Specialized list reading for character lists which always uses
355//- binary format.
356template<>
358
359//- Hashing for List data
360template<class T>
361struct Hash<List<T>> : List<T>::hasher {};
362
363
364// * * * * * * * * * * * * * * * IOstream Operators * * * * * * * * * * * * //
365
366//- Read List contents from Istream
367template<class T>
369{
370 return list.readList(is);
371}
372
373
374// * * * * * * * * * * * * * * Global Functions * * * * * * * * * * * * * * //
375
376//- Return an identity map of the given length with (map[i] == i)
377// Optionally with an alternative start index, so that (map[i] == i+start)
378labelList identity(const label len, label start=0);
379
380//- Return the (stable) sort order for the list
381template<class T>
383
384//- Generate the (stable) sort order for the list
385template<class T>
386void sortedOrder(const UList<T>& input, labelList& order);
387
388//- Sort using specified list compare predicate
389template<class T, class ListComparePredicate>
390void sortedOrder
391(
392 const UList<T>& input,
393 labelList& order,
394 const ListComparePredicate& comp
395);
396
397
398// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
399
400} // End namespace Foam
401
402// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
403
404#include "ListI.H"
405
406// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
407
408#ifdef NoRepository
409 #include "List.C"
410 #include "ListIO.C"
411#endif
412
413// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
414
415#endif
416
417// ************************************************************************* //
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:72
A 1D vector of objects of type <T> with a fixed length <N>.
Definition: FixedList.H:81
Base for lists with indirect addressing, templated on the list contents type and the addressing type....
An Istream is an abstract base class for all input systems (streams, files, token lists etc)....
Definition: Istream.H:64
Template class for non-intrusive linked lists.
Definition: LList.H:79
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
std::enable_if< std::is_same< bool, TypeT >::value, bool >::type set(const label i, bool val=true)
A bitSet::set() method for a list of bool.
Definition: List.H:330
void transfer(List< T > &list)
Definition: List.C:447
T & newElmt(const label i)
Definition: ListI.H:153
SubList< T > subList
Declare type of subList.
Definition: List.H:111
void setSize(const label n)
Alias for resize()
Definition: List.H:218
autoPtr< List< T > > clone() const
Clone.
Definition: ListI.H:100
void resize_nocopy(const label len)
Adjust allocated size of list without necessarily.
Definition: ListI.H:146
void operator=(const UList< T > &a)
Assignment to UList operator. Takes linear time.
Definition: List.C:480
constexpr List() noexcept
Default construct.
Definition: ListI.H:95
void append(const T &val)
Append an element at the end of the list.
Definition: ListI.H:175
label appendUniq(const T &val)
Append an element if not already in the list.
Definition: ListI.H:232
void shallowCopy(const UList< T > &)=delete
No shallowCopy permitted.
void setSize(const label n, const T &val)
Alias for resize()
Definition: List.H:221
void resize(const label len)
Adjust allocated size of list.
Definition: ListI.H:139
void clear()
Clear the list, i.e. set size to zero.
Definition: ListI.H:116
Istream & readList(Istream &is)
Read List from Istream, discarding contents of existing List.
Definition: ListIO.C:49
static const List< T > & null()
Return a null List.
Definition: ListI.H:109
A list of pointers to objects of type <T>, with allocation/deallocation management of the pointers....
Definition: PtrList.H:73
A List obtained as a section of another List.
Definition: SubList.H:70
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
label size() const noexcept
The number of elements in the UList.
Definition: UListI.H:420
Pointer management similar to std::unique_ptr, with some additional methods and type checking.
Definition: autoPtr.H:66
A class representing the concept of 1 (one) that can be used to avoid manipulating objects known to b...
Definition: one.H:62
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
Namespace for OpenFOAM.
labelList identity(const label len, label start=0)
Return an identity map of the given length with (map[i] == i)
Definition: labelList.C:38
List< label > labelList
A List of labels.
Definition: List.H:66
static Istream & input(Istream &is, IntRange< T > &range)
Definition: IntRanges.C:55
fileName::Type type(const fileName &name, const bool followLink=true)
Return the file type: DIRECTORY or FILE, normally following symbolic links.
Definition: MSwindows.C:598
Istream & operator>>(Istream &, directionInfo &)
labelList sortedOrder(const UList< T > &input)
Return the (stable) sort order for the list.
List< bool > boolList
A List of bools.
Definition: List.H:64
const direction noexcept
Definition: Scalar.H:223
List< char > charList
A List of chars.
Definition: List.H:65
Hash function class. The default definition is for primitives. Non-primitives used to hash entries on...
Definition: Hash.H:54
const Vector< label > N(dict.get< Vector< label > >("N"))