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