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-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::List
29 
30 Description
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 
36 SourceFiles
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 
52 namespace Foam
53 {
54 
55 // Forward Declarations
56 template<class T> class List;
57 template<class T, unsigned N> class FixedList;
58 template<class T, int SizeMin> class DynamicList;
59 
60 template<class T> class PtrList;
61 
62 template<class T> Istream& operator>>(Istream& is, List<T>& list);
63 
64 // Common list types
65 typedef List<bool> boolList;
66 typedef List<char> charList;
67 typedef List<label> labelList;
68 
69 
70 /*---------------------------------------------------------------------------*\
71  Class List Declaration
72 \*---------------------------------------------------------------------------*/
73 
74 template<class T>
75 class 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 
107 public:
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
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  //- Transfer the contents of the argument List into this list
228  //- and annul the argument list
229  void transfer(List<T>& list);
230 
231  //- Transfer the contents of the argument List into this list
232  //- and annul the argument list
233  template<int SizeMin>
234  void transfer(DynamicList<T, SizeMin>& list);
235 
236  //- Return subscript-checked element of UList and resizing the list
237  //- if required.
238  inline T& newElmt(const label i);
239 
240 
241  // Edit
242 
243  //- Construct an element at the end of the list,
244  //- return reference to the new list element.
245  // If this is frequently required, consider a DynamicList instead.
246  template<class... Args>
247  inline T& emplace_back(Args&&... args);
248 
249  //- Append an element at the end of the list
250  // If this is frequently required, consider a DynamicList
251  inline void push_back(const T& val);
252 
253  //- Move append an element at the end of the list
254  // If this is frequently required, consider a DynamicList
255  inline void push_back(T&& val);
256 
257  //- Append a List to the end of this list
258  // If this is frequently required, consider a DynamicList
259  inline void push_back(const UList<T>& list);
260 
261  //- Append IndirectList contents at the end of this list
262  // If this is frequently required, consider a DynamicList
263  template<class Addr>
264  inline void push_back(const IndirectListBase<T, Addr>& list);
265 
266  //- Append an element if not already in the list.
267  // \return the change in list length
268  inline label push_uniq(const T& val);
269 
270  //- Reduce size by 1 or more elements. Can be called on an empty list.
271  inline void pop_back(label n = 1);
272 
273 
274  // Member Operators
275 
276  //- Assignment to UList operator. Takes linear time
277  void operator=(const UList<T>& a);
278 
279  //- Assignment operator. Takes linear time
280  void operator=(const List<T>& list);
281 
282  //- Assignment to SLList operator. Takes linear time
283  void operator=(const SLList<T>& list);
284 
285  //- Assignment from IndirectList. Takes linear time
286  template<class Addr>
287  void operator=(const IndirectListBase<T, Addr>& list);
288 
289  //- Copy assignment from FixedList
290  template<unsigned N>
291  void operator=(const FixedList<T, N>& list);
292 
293  //- Assignment to an initializer list
294  void operator=(std::initializer_list<T> list);
295 
296  //- Assignment of all entries to the given value
297  inline void operator=(const T& val);
298 
299  //- Assignment of all entries to zero
300  inline void operator=(const Foam::zero);
301 
302  //- Move assignment. Takes constant time
303  void operator=(List<T>&& list);
304 
305  //- Move assignment. Takes constant time.
306  template<int SizeMin>
307  void operator=(DynamicList<T, SizeMin>&& list);
308 
309  //- Move assignment. Takes constant time
310  void operator=(SLList<T>&& list);
311 
312 
313  // Reading/writing
314 
315  //- Read List from Istream, discarding contents of existing List
316  Istream& readList(Istream& is);
317 
318 
319  // IOstream Operators
320 
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<class TypeT = T>
342  typename std::enable_if<std::is_same<bool, TypeT>::value, bool>::type
343  inline set(const label i, bool val = true)
344  {
345  if (i < 0)
346  {
347  return false; // Out-of-bounds: ignore
348  }
349  else if (i >= this->size())
350  {
351  if (!val) // Unset out-of-bounds: ignore
352  {
353  return false;
354  }
355  this->resize(i+1, false); // Adjust size for assign, fill 0
356  }
357 
358  (*this)[i] = val;
359  return true;
360  }
361 
362 
363  // Housekeeping
364 
365  //- Append an element at the end of the list
366  // If this is frequently required, consider a DynamicList
367  //FOAM_DEPRECATED_FOR(2022-10, "push_back()")
368  void append(const T& val) { this->push_back(val); }
369 
370  //- Move append an element at the end of the list
371  // If this is frequently required, consider a DynamicList
372  //FOAM_DEPRECATED_FOR(2022-10, "push_back()")
373  void append(T&& val) { this->push_back(std::move(val)); }
374 
375  //- Append a List to the end of this list
376  // If this is frequently required, consider a DynamicList
377  //FOAM_DEPRECATED_FOR(2022-10, "push_back()")
378  void append(const UList<T>& list) { this->push_back(list); }
379 
380  //- Append IndirectList contents at the end of this list
381  // If this is frequently required, consider a DynamicList
382  //FOAM_DEPRECATED_FOR(2022-10, "push_back()")
383  template<class Addr>
384  void append(const IndirectListBase<T, Addr>& list)
385  {
386  this->push_back(list);
387  }
388 
389  //- Append an element if not already in the list.
390  //FOAM_DEPRECATED_FOR(2022-10, "push_uniq()")
391  label appendUniq(const T& val) { return this->push_uniq(val); }
392 };
393 
394 
395 // * * * * * * * * * * * * Template Specializations * * * * * * * * * * * * //
396 
397 //- Specialized list reading for character lists which always uses
398 //- binary format.
399 template<>
400 Istream& List<char>::readList(Istream& is);
401 
402 //- Hashing for List data
403 template<class T>
404 struct Hash<List<T>> : List<T>::hasher {};
405 
406 
407 // * * * * * * * * * * * * * * * IOstream Operators * * * * * * * * * * * * //
408 
409 //- Read List contents from Istream
410 template<class T>
411 Istream& operator>>(Istream& is, List<T>& list)
412 {
413  return list.readList(is);
414 }
415 
416 
417 // * * * * * * * * * * * * * * Global Functions * * * * * * * * * * * * * * //
418 
419 //- Return an identity map of the given length with (map[i] == i)
420 // Optionally with an alternative start index, so that (map[i] == i+start)
421 labelList identity(const label len, label start=0);
422 
423 //- Return the (stable) sort order for the list
424 template<class T>
425 labelList sortedOrder(const UList<T>& input);
426 
427 //- Generate the (stable) sort order for the list
428 template<class T>
429 void sortedOrder(const UList<T>& input, labelList& order);
430 
431 //- Sort using specified list compare predicate
432 template<class T, class ListComparePredicate>
433 void sortedOrder
434 (
435  const UList<T>& input,
436  labelList& order,
437  const ListComparePredicate& comp
438 );
439 
440 
441 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
442 
443 } // End namespace Foam
444 
445 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
446 
447 #include "ListI.H"
448 
449 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
450 
451 #ifdef NoRepository
452  #include "List.C"
453  #include "ListIO.C"
454 #endif
455 
456 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
457 
458 #endif
459 
460 // ************************************************************************* //
void pop_back(label n=1)
Reduce size by 1 or more elements. Can be called on an empty list.
Definition: ListI.H:257
A 1D vector of objects of type <T> with a fixed length <N>.
Definition: HashTable.H:101
void resize(const label len)
Adjust allocated size of list.
Definition: ListI.H:132
void transfer(List< T > &list)
Transfer the contents of the argument List into this list and annul the argument list.
Definition: List.C:452
void append(const T &val)
Append an element at the end of the list.
Definition: List.H:500
labelList sortedOrder(const UList< T > &input)
Return the (stable) sort order for the list.
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
Template class for non-intrusive linked lists.
Definition: LList.H:46
An Istream is an abstract base class for all input systems (streams, files, token lists etc)...
Definition: Istream.H:57
label appendUniq(const T &val)
Append an element if not already in the list.
Definition: List.H:535
void resize_nocopy(const label len)
Adjust allocated size of list without necessarily.
Definition: ListI.H:139
SubList< T > subList
Declare type of subList.
Definition: List.H:122
void operator=(const UList< T > &a)
Assignment to UList operator. Takes linear time.
Definition: List.C:485
static const List< T > & null()
Return a null List.
Definition: ListI.H:102
void push_back(const T &val)
Append an element at the end of the list.
Definition: ListI.H:185
label push_uniq(const T &val)
Append an element if not already in the list.
Definition: ListI.H:242
Base for lists with indirect addressing, templated on the list contents type and the addressing type...
friend class List< T >
Declare friendship with the List class.
Definition: UList.H:205
Forward declarations for SLList.
fileName::Type type(const fileName &name, const bool followLink=true)
Return the file type: DIRECTORY or FILE, normally following symbolic links.
Definition: POSIX.C:799
A List obtained as a section of another List.
Definition: SubList.H:50
void setSize(const label n)
Alias for resize()
Definition: List.H:289
void shallowCopy(const UList< T > &)=delete
No shallowCopy permitted.
A 1D vector of objects of type <T> that resizes itself as necessary to accept the new objects...
Definition: DynamicList.H:51
labelList identity(const label len, label start=0)
Return an identity map of the given length with (map[i] == i)
Definition: labelList.C:31
void clear()
Clear the list, i.e. set size to zero.
Definition: ListI.H:109
Istream & operator>>(Istream &, directionInfo &)
static Istream & input(Istream &is, IntRange< T > &range)
Definition: IntRanges.C:47
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
Istream & readList(Istream &is)
Read List from Istream, discarding contents of existing List.
Definition: ListIO.C:42
const direction noexcept
Definition: Scalar.H:258
label size() const noexcept
The number of elements in the List.
Definition: UList.H:637
void T(FieldField< Field, Type > &f1, const FieldField< Field, Type > &f2)
const Vector< label > N(dict.get< Vector< label >>("N"))
T & emplace_back(Args &&... args)
Construct an element at the end of the list, return reference to the new list element.
Definition: ListI.H:169
T & newElmt(const label i)
Return subscript-checked element of UList and resizing the list if required.
Definition: ListI.H:146
List< char > charList
A List of chars.
Definition: List.H:61
A list of pointers to objects of type <T>, with allocation/deallocation management of the pointers...
Definition: List.H:55
autoPtr< List< T > > clone() const
Clone.
Definition: ListI.H:93
A class representing the concept of 0 (zero) that can be used to avoid manipulating objects known to ...
Definition: zero.H:57
label n
Pointer management similar to std::unique_ptr, with some additional methods and type checking...
Definition: HashPtrTable.H:48
List< label > labelList
A List of labels.
Definition: List.H:62
Foam::argList args(argc, argv)
constexpr List() noexcept
Default construct.
Definition: ListI.H:88
List< bool > boolList
A List of bools.
Definition: List.H:60
Namespace for OpenFOAM.
A class representing the concept of 1 (one) that can be used to avoid manipulating objects known to b...
Definition: one.H:56