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