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 -------------------------------------------------------------------------------
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 
49 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
50 
51 namespace Foam
52 {
53 
54 // Forward Declarations
55 template<class T> class List;
56 template<class T, unsigned N> class FixedList;
57 template<class T, int SizeMin> class DynamicList;
58 
59 template<class T> class PtrList;
60 
61 template<class T> Istream& operator>>(Istream& is, List<T>& list);
62 
63 // Common list types
64 typedef List<bool> boolList;
65 typedef List<char> charList;
66 typedef List<label> labelList;
67 
68 
69 /*---------------------------------------------------------------------------*\
70  Class List Declaration
71 \*---------------------------------------------------------------------------*/
72 
73 template<class T>
74 class List
75 :
76  public UList<T>
77 {
78  // Private Member Functions
79 
80  //- Allocate list storage
81  inline void doAlloc();
82 
83  //- Reallocate list storage to the given size
84  // Discards old storage (if any). Does not copy old contents
85  inline void reAlloc(const label len);
86 
87  //- Copy all list contents. Uses operator[] on the input list
88  template<class ListType>
89  inline void copyList(const ListType& list);
90 
91  //- Copy list contents via indirect indices.
92  // Uses operator[] on the values and indices lists
93  template<class ListType, class ListIndices>
94  inline void copyList(const ListType& list, const ListIndices& indices);
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 firstIter,
102  InputIterator lastIter, // (unused)
103  const label len
104  );
105 
106  //- Read List from Istream between '(' and ')' delimiters.
107  //- The size is not known a priori.
108  bool readBracketList(Istream& is);
109 
110 
111  // Methods as per DynamicList to simplify code maintenance
112 
113  //- Stub method for internal naming as per DynamicList
114  void setCapacity_nocopy(const label len) { resize_nocopy(len); }
115 
116 
117 protected:
118 
119  // Protected Member Functions
120 
121  //- Low-level resizing (backend for resize).
122  //- Change allocation size of list, retaining the first count contents.
123  // \note Only uses a limited number of internal checks.
124  void resize_copy(const label count, const label len);
125 
126 
127 public:
128 
129  // Related types
130 
131  //- Declare type of subList
132  typedef SubList<T> subList;
133 
134 
135  // Static Member Functions
136 
137  //- Return a null List (reference to a nullObject).
138  //- Behaves like an empty List.
139  static const List<T>& null() noexcept
140  {
141  return NullObjectRef<List<T>>();
142  }
143 
144 
145  // Constructors
146 
147  //- Default construct
148  inline constexpr List() noexcept;
150  //- Construct with given size
151  explicit List(const label len);
152 
153  //- Construct with given size and value for all elements
154  List(const label len, const T& val);
155 
156  //- Construct with given size initializing all elements to zero
157  List(const label len, Foam::zero);
159  //- Construct with length=1, copying the value as the only content
160  List(Foam::one, const T& val);
161 
162  //- Construct with length=1, moving the value as the only content
163  List(Foam::one, T&& val);
164 
165  //- Construct with length=1, initializing content to zero
166  List(Foam::one, Foam::zero);
167 
168  //- Copy construct from list
169  List(const List<T>& list);
170 
171  //- Copy construct contents from list
172  explicit List(const UList<T>& list);
173 
174  //- Construct as copy or re-use as specified
175  List(List<T>& list, bool reuse);
176 
177  //- Copy construct subset of list
178  List(const UList<T>& list, const labelUList& indices);
179 
180  //- Copy construct subset of list
181  template<unsigned N>
182  List(const UList<T>& list, const FixedList<label, N>& indices);
183 
184  //- Construct as copy of FixedList<T, N>
185  template<unsigned N>
186  explicit List(const FixedList<T, N>& list);
187 
188  //- Construct as copy of PtrList<T>
189  explicit List(const PtrList<T>& list);
190 
191  //- Construct as copy of IndirectList contents
192  template<class Addr>
193  explicit List(const IndirectListBase<T, Addr>& list);
194 
195  //- Construct from an initializer list
196  List(std::initializer_list<T> list);
197 
198  //- Move construct from List
199  List(List<T>&& list) noexcept;
200 
201  //- Move construct from DynamicList
202  template<int SizeMin>
203  List(DynamicList<T, SizeMin>&& list);
204 
205  //- Construct from Istream
206  List(Istream& is);
207 
208  //- Clone
209  inline autoPtr<List<T>> clone() const;
210 
211 
212  //- Destructor
213  ~List();
214 
215 
216  // Member Functions
217 
218  // Sizing
219 
220  //- Clear the list, i.e. set size to zero
221  inline void clear();
222 
223  //- Adjust allocated size of list.
224  // The boolList version fills new memory with false.
225  inline void resize(const label len);
226 
227  //- Adjust allocated size of list and set val for \em new elements
228  void resize(const label len, const T& val);
229 
230  //- Adjust allocated size of list and set val for \em all elements
231  inline void resize_fill(const label len, const T& val);
232 
233  //- Adjust allocated size of list \b without necessarily
234  // retaining old content.
235  // If no reallocation is required, the contents remain untouched.
236  // Otherwise the contents will be uninitialized.
237  inline void resize_nocopy(const label len);
238 
239  //- Change the addressed list size directly without affecting
240  //- any memory management (advanced usage).
241  //
242  // It is left to the caller to avoid \em unsafe lengthening beyond
243  // the allocated memory region.
244  inline void resize_unsafe(const label len) noexcept;
245 
246  //- Alias for resize()
247  void setSize(const label n) { this->resize(n); }
248 
249  //- Alias for resize()
250  void setSize(const label n, const T& val) { this->resize(n, val); }
251 
252 
253  // Edit
254 
255  //- Transfer the contents of the argument List into this list
256  //- and annul the argument list
257  void transfer(List<T>& list);
258 
259  //- Transfer the contents of the argument List into this list
260  //- and annul the argument list
261  template<int SizeMin>
262  void transfer(DynamicList<T, SizeMin>& list);
263 
264  //- Return subscript-checked element of UList and resizing the list
265  //- if required.
266  inline T& newElmt(const label i);
267 
268 
269  // Edit
270 
271  //- Construct an element at the end of the list,
272  //- return reference to the new list element.
273  // If this is frequently required, consider a DynamicList instead.
274  template<class... Args>
275  inline T& emplace_back(Args&&... args);
276 
277  //- Append an element at the end of the list
278  // If this is frequently required, consider a DynamicList
279  inline void push_back(const T& val);
280 
281  //- Move append an element at the end of the list
282  // If this is frequently required, consider a DynamicList
283  inline void push_back(T&& val);
284 
285  //- Append a List to the end of this list
286  // If this is frequently required, consider a DynamicList
287  inline void push_back(const UList<T>& list);
288 
289  //- Append IndirectList contents at the end of this list
290  // If this is frequently required, consider a DynamicList
291  template<class Addr>
292  inline void push_back(const IndirectListBase<T, Addr>& list);
293 
294  //- Append an element if not already in the list.
295  // \return the change in list length
296  inline label push_uniq(const T& val);
297 
298  //- Reduce size by 1 or more elements. Can be called on an empty list.
299  inline void pop_back(label n = 1);
300 
301 
302  // Member Operators
303 
304  //- Assignment to UList operator. Takes linear time
305  void operator=(const UList<T>& list);
306 
307  //- Assignment operator. Takes linear time
308  void operator=(const List<T>& list);
309 
310  //- Assignment from IndirectList. Takes linear time
311  template<class Addr>
312  void operator=(const IndirectListBase<T, Addr>& list);
313 
314  //- Copy assignment from FixedList
315  template<unsigned N>
316  void operator=(const FixedList<T, N>& list);
317 
318  //- Assignment to an initializer list
319  void operator=(std::initializer_list<T> list);
320 
321  //- Assignment of all entries to the given value
322  inline void operator=(const T& val);
323 
324  //- Assignment of all entries to zero
325  inline void operator=(Foam::zero);
326 
327  //- Move assignment. Takes constant time
328  void operator=(List<T>&& list);
329 
330  //- Move assignment. Takes constant time.
331  template<int SizeMin>
332  void operator=(DynamicList<T, SizeMin>&& list);
333 
334 
335  // Reading/writing
336 
337  //- Read List from Istream, discarding contents of existing List
338  Istream& readList(Istream& is);
339 
340 
341  // IOstream Operators
342 
343  //- Use the readList() method to read contents from Istream.
344  friend Istream& operator>> <T>
345  (
346  Istream& is,
347  List<T>& list
348  );
349 
350 
351  // Housekeeping
352 
353  //- No shallowCopy permitted
354  void shallowCopy(const UList<T>&) = delete;
355 
356 
357  // Special Methods
358 
359  //- A bitSet::set() method for a list of bool
360  // Increases size when setting an out-of-bounds value.
361  //
362  // \return True if value changed.
363  template<class TypeT = T>
364  std::enable_if_t<std::is_same_v<bool, TypeT>, bool>
365  inline set(const label i, bool val = true)
366  {
367  if (i < 0)
368  {
369  return false; // Out-of-bounds: ignore
370  }
371  else if (i >= this->size())
372  {
373  if (!val) // Unset out-of-bounds: ignore
374  {
375  return false;
376  }
377  this->resize(i+1, false); // Adjust size for assign, fill 0
378  }
379 
380  (*this)[i] = val;
381  return true;
382  }
383 
384 
385  // Housekeeping
386 
387  //- Append an element at the end of the list
388  // If this is frequently required, consider a DynamicList
389  //FOAM_DEPRECATED_FOR(2022-10, "push_back()")
390  void append(const T& val) { this->push_back(val); }
391 
392  //- Move append an element at the end of the list
393  // If this is frequently required, consider a DynamicList
394  //FOAM_DEPRECATED_FOR(2022-10, "push_back()")
395  void append(T&& val) { this->push_back(std::move(val)); }
396 
397  //- Append a List to the end of this list
398  // If this is frequently required, consider a DynamicList
399  //FOAM_DEPRECATED_FOR(2022-10, "push_back()")
400  void append(const UList<T>& list) { this->push_back(list); }
401 
402  //- Append IndirectList contents at the end of this list
403  // If this is frequently required, consider a DynamicList
404  //FOAM_DEPRECATED_FOR(2022-10, "push_back()")
405  template<class Addr>
406  void append(const IndirectListBase<T, Addr>& list)
407  {
408  this->push_back(list);
409  }
410 
411  //- Same as push_uniq()
412  FOAM_DEPRECATED_FOR(2022-10, "push_uniq()")
413  label appendUniq(const T& val) { return this->push_uniq(val); }
414 };
415 
416 
417 // * * * * * * * * * * * * Template Specializations * * * * * * * * * * * * //
418 
419 //- Hashing for List data
420 template<class T>
421 struct Hash<List<T>> : List<T>::hasher {};
422 
423 
424 // * * * * * * * * * * * * * * * IOstream Operators * * * * * * * * * * * * //
425 
426 //- Read List contents from Istream
427 template<class T>
428 Istream& operator>>(Istream& is, List<T>& list)
429 {
430  return list.readList(is);
431 }
432 
433 
434 // * * * * * * * * * * * * * * Global Functions * * * * * * * * * * * * * * //
435 
436 //- Return an identity map of the given length with (map[i] == i),
437 //- works like std::iota() but returning a list of label values.
438 // Optionally with an alternative start index, so that (map[i] == i+start)
439 labelList identity(const label len, label start=0);
440 
441 //- Return the (stable) sort order for the list
442 template<class T>
443 labelList sortedOrder(const UList<T>& input);
444 
445 //- Generate the (stable) sort order for the list
446 template<class T>
447 void sortedOrder(const UList<T>& input, labelList& order);
448 
449 //- Sort using specified list compare predicate
450 template<class T, class ListComparePredicate>
451 void sortedOrder
452 (
453  const UList<T>& input,
454  labelList& order,
455  const ListComparePredicate& comp
456 );
457 
458 
459 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
460 
461 } // End namespace Foam
462 
463 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
464 
465 #include "ListI.H"
466 
467 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
468 
469 #ifdef NoRepository
470  #include "List.C"
471  #include "ListIO.C"
472 #endif
473 
474 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
475 
476 #endif
477 
478 // ************************************************************************* //
void pop_back(label n=1)
Reduce size by 1 or more elements. Can be called on an empty list.
Definition: ListI.H:292
A 1D vector of objects of type <T> with a fixed length <N>.
Definition: HashTable.H:107
void resize(const label len)
Adjust allocated size of list.
Definition: ListI.H:150
void transfer(List< T > &list)
Transfer the contents of the argument List into this list and annul the argument list.
Definition: List.C:337
void append(const T &val)
Append an element at the end of the list.
Definition: List.H:526
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
friend class SubList< T >
Declare friendship with the SubList class.
Definition: UList.H:212
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)
Same as push_uniq()
Definition: List.H:560
void resize_nocopy(const label len)
Adjust allocated size of list without necessarily.
Definition: ListI.H:168
SubList< T > subList
Declare type of subList.
Definition: List.H:149
void resize_fill(const label len, const T &val)
Adjust allocated size of list and set val for all elements.
Definition: ListI.H:160
void push_back(const T &val)
Append an element at the end of the list.
Definition: ListI.H:220
label push_uniq(const T &val)
Append an element if not already in the list.
Definition: ListI.H:277
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:207
class FOAM_DEPRECATED_FOR(2017-05, "Foam::Enum") NamedEnum
Definition: NamedEnum.H:65
unsigned int count(const UList< bool > &bools, const bool val=true)
Count number of &#39;true&#39; entries.
Definition: BitOps.H:73
void setSize(const label n)
Alias for resize()
Definition: List.H:325
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
void operator=(const UList< T > &list)
Assignment to UList operator. Takes linear time.
Definition: List.C:371
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...
Definition: labelLists.C:44
void clear()
Clear the list, i.e. set size to zero.
Definition: ListI.H:130
Istream & operator>>(Istream &, directionInfo &)
static Istream & input(Istream &is, IntRange< T > &range)
Definition: IntRanges.C:33
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:105
Istream & readList(Istream &is)
Read List from Istream, discarding contents of existing List.
Definition: ListIO.C:167
const direction noexcept
Definition: scalarImpl.H:255
label size() const noexcept
The number of elements in the container.
Definition: UList.H:702
void T(FieldField< Field, Type > &f1, const FieldField< Field, Type > &f2)
void resize_unsafe(const label len) noexcept
Change the addressed list size directly without affecting any memory management (advanced usage)...
Definition: ListI.H:175
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:205
T & newElmt(const label i)
Return subscript-checked element of UList and resizing the list if required.
Definition: ListI.H:182
List< char > charList
A List of chars.
Definition: List.H:60
A list of pointers to objects of type <T>, with allocation/deallocation management of the pointers...
Definition: List.H:54
autoPtr< List< T > > clone() const
Clone.
Definition: ListI.H:121
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:61
Foam::argList args(argc, argv)
void resize_copy(const label count, const label len)
Low-level resizing (backend for resize). Change allocation size of list, retaining the first count co...
Definition: List.C:29
constexpr List() noexcept
Default construct.
Definition: ListI.H:116
List< bool > boolList
A List of bools.
Definition: List.H:59
static const List< T > & null() noexcept
Return a null List (reference to a nullObject). Behaves like an empty List.
Definition: List.H:158
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