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 -------------------------------------------------------------------------------
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  //- Append an element at the end of the list
244  // If this is frequently required, consider a DynamicList
245  inline void push_back(const T& val);
246 
247  //- Move append an element at the end of the list
248  // If this is frequently required, consider a DynamicList
249  inline void push_back(T&& val);
250 
251  //- Append a List to the end of this list
252  // If this is frequently required, consider a DynamicList
253  inline void push_back(const UList<T>& list);
254 
255  //- Append IndirectList contents at the end of this list
256  // If this is frequently required, consider a DynamicList
257  template<class Addr>
258  inline void push_back(const IndirectListBase<T, Addr>& list);
259 
260  //- Append an element if not already in the list.
261  // \return the change in list length
262  inline label push_uniq(const T& val);
263 
264  //- Reduce size by 1 or more elements. Can be called on an empty list.
265  inline void pop_back(label n = 1);
266 
267 
268  // Member Operators
269 
270  //- Assignment to UList operator. Takes linear time
271  void operator=(const UList<T>& a);
272 
273  //- Assignment operator. Takes linear time
274  void operator=(const List<T>& list);
275 
276  //- Assignment to SLList operator. Takes linear time
277  void operator=(const SLList<T>& list);
278 
279  //- Assignment from IndirectList. Takes linear time
280  template<class Addr>
281  void operator=(const IndirectListBase<T, Addr>& list);
282 
283  //- Copy assignment from FixedList
284  template<unsigned N>
285  void operator=(const FixedList<T, N>& list);
286 
287  //- Assignment to an initializer list
288  void operator=(std::initializer_list<T> list);
290  //- Assignment of all entries to the given value
291  inline void operator=(const T& val);
292 
293  //- Assignment of all entries to zero
294  inline void operator=(const Foam::zero);
295 
296  //- Move assignment. Takes constant time
297  void operator=(List<T>&& list);
298 
299  //- Move assignment. Takes constant time.
300  template<int SizeMin>
301  void operator=(DynamicList<T, SizeMin>&& list);
302 
303  //- Move assignment. Takes constant time
304  void operator=(SLList<T>&& list);
305 
306 
307  // Reading/writing
308 
309  //- Read List from Istream, discarding contents of existing List
310  Istream& readList(Istream& is);
311 
312 
313  // IOstream Operators
314 
315  //- Use the readList() method to read contents from Istream.
316  friend Istream& operator>> <T>
317  (
318  Istream& is,
319  List<T>& list
320  );
321 
322 
323  // Housekeeping
324 
325  //- No shallowCopy permitted
326  void shallowCopy(const UList<T>&) = delete;
327 
328 
329  // Special Methods
330 
331  //- A bitSet::set() method for a list of bool
332  // Increases size when setting an out-of-bounds value.
333  //
334  // \return True if value changed.
335  template<class TypeT = T>
336  typename std::enable_if<std::is_same<bool, TypeT>::value, bool>::type
337  inline set(const label i, bool val = true)
338  {
339  if (i < 0)
340  {
341  return false; // Out-of-bounds: ignore
342  }
343  else if (i >= this->size())
344  {
345  if (!val) // Unset out-of-bounds: ignore
346  {
347  return false;
348  }
349  this->resize(i+1, false); // Adjust size for assign, fill 0
350  }
351 
352  (*this)[i] = val;
353  return true;
354  }
355 
356 
357  // Housekeeping
358 
359  //- Append an element at the end of the list
360  // If this is frequently required, consider a DynamicList
361  //FOAM_DEPRECATED_FOR(2022-10, "push_back()")
362  void append(const T& val) { this->push_back(val); }
363 
364  //- Move append an element at the end of the list
365  // If this is frequently required, consider a DynamicList
366  //FOAM_DEPRECATED_FOR(2022-10, "push_back()")
367  void append(T&& val) { this->push_back(std::move(val)); }
368 
369  //- Append a List to the end of this list
370  // If this is frequently required, consider a DynamicList
371  //FOAM_DEPRECATED_FOR(2022-10, "push_back()")
372  void append(const UList<T>& list) { this->push_back(list); }
373 
374  //- Append IndirectList contents at the end of this list
375  // If this is frequently required, consider a DynamicList
376  //FOAM_DEPRECATED_FOR(2022-10, "push_back()")
377  template<class Addr>
378  void append(const IndirectListBase<T, Addr>& list)
379  {
380  this->push_back(list);
381  }
382 
383  //- Append an element if not already in the list.
384  //FOAM_DEPRECATED_FOR(2022-10, "push_uniq()")
385  label appendUniq(const T& val) { return this->push_uniq(val); }
386 };
387 
388 
389 // * * * * * * * * * * * * Template Specializations * * * * * * * * * * * * //
390 
391 //- Specialized list reading for character lists which always uses
392 //- binary format.
393 template<>
394 Istream& List<char>::readList(Istream& is);
395 
396 //- Hashing for List data
397 template<class T>
398 struct Hash<List<T>> : List<T>::hasher {};
399 
400 
401 // * * * * * * * * * * * * * * * IOstream Operators * * * * * * * * * * * * //
402 
403 //- Read List contents from Istream
404 template<class T>
405 Istream& operator>>(Istream& is, List<T>& list)
406 {
407  return list.readList(is);
408 }
409 
410 
411 // * * * * * * * * * * * * * * Global Functions * * * * * * * * * * * * * * //
412 
413 //- Return an identity map of the given length with (map[i] == i)
414 // Optionally with an alternative start index, so that (map[i] == i+start)
415 labelList identity(const label len, label start=0);
416 
417 //- Return the (stable) sort order for the list
418 template<class T>
419 labelList sortedOrder(const UList<T>& input);
420 
421 //- Generate the (stable) sort order for the list
422 template<class T>
423 void sortedOrder(const UList<T>& input, labelList& order);
424 
425 //- Sort using specified list compare predicate
426 template<class T, class ListComparePredicate>
427 void sortedOrder
428 (
429  const UList<T>& input,
430  labelList& order,
431  const ListComparePredicate& comp
432 );
433 
434 
435 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
436 
437 } // End namespace Foam
438 
439 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
440 
441 #include "ListI.H"
442 
443 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
444 
445 #ifdef NoRepository
446  #include "List.C"
447  #include "ListIO.C"
448 #endif
449 
450 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
451 
452 #endif
453 
454 // ************************************************************************* //
void pop_back(label n=1)
Reduce size by 1 or more elements. Can be called on an empty list.
Definition: ListI.H:240
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:439
void append(const T &val)
Append an element at the end of the list.
Definition: List.H:491
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:50
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:526
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:472
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:168
label push_uniq(const T &val)
Append an element if not already in the list.
Definition: ListI.H:225
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
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:752
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:48
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
void T(FieldField< Field, Type > &f1, const FieldField< Field, Type > &f2)
const Vector< label > N(dict.get< Vector< label >>("N"))
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
label size() const noexcept
The number of elements in the UList.
Definition: UListI.H:413
A class representing the concept of 0 (zero) that can be used to avoid manipulating objects known to ...
Definition: zero.H:58
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
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:57