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-2021 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 List_H
44 #define 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 template<class T> class SortableList;
62 
63 template<class T> Istream& operator>>(Istream& is, List<T>& list);
64 
65 // Common list types
68 typedef List<label> labelList;
69 
70 
71 /*---------------------------------------------------------------------------*\
72  Class List Declaration
73 \*---------------------------------------------------------------------------*/
74 
75 template<class T>
76 class List
77 :
78  public UList<T>
79 {
80  // Private Member Functions
81 
82  //- Allocate list storage
83  inline void doAlloc();
84 
85  //- Reallocate list storage to the given size
86  // Discards old storage (if any). Does not copy old contents
87  inline void reAlloc(const label len);
88 
89  //- Copy all list contents
90  template<class List2>
91  inline void copyList(const List2& list);
92 
93  //- Change allocation size of List, retaining old contents.
94  // Backend for resize
95  void doResize(const label len);
96 
97  //- Construct given begin/end iterators and number of elements
98  // Since the size is provided, the end iterator is actually ignored.
99  template<class InputIterator>
100  inline List
101  (
102  InputIterator begIter,
103  InputIterator endIter,
104  const label len
105  );
106 
107 
108 public:
109 
110  // Related types
111 
112  //- Declare type of subList
113  typedef SubList<T> subList;
114 
115 
116  // Static Member Functions
117 
118  //- Return a null List
119  inline static const List<T>& null();
120 
121 
122  // Constructors
123 
124  //- Default construct
125  inline constexpr List() noexcept;
126 
127  //- Construct with given size
128  explicit List(const label len);
129 
130  //- Construct with given size and value for all elements
131  List(const label len, const T& val);
132 
133  //- Construct with given size initializing all elements to zero
134  List(const label len, const Foam::zero);
135 
136  //- Construct with length=1, copying the value as the only content
137  List(const Foam::one, const T& val);
138 
139  //- Construct with length=1, moving the value as the only content
140  List(const Foam::one, T&& val);
141 
142  //- Construct with length=1, initializing content to zero
143  List(const Foam::one, const Foam::zero);
144 
145  //- Copy construct from list
146  List(const List<T>& a);
147 
148  //- Copy construct contents from list
149  explicit List(const UList<T>& a);
150 
151  //- Construct as copy or re-use as specified
152  List(List<T>& a, bool reuse);
153 
154  //- Copy construct subset of list
155  List(const UList<T>& list, const labelUList& indices);
156 
157  //- Copy construct subset of list
158  template<unsigned N>
159  List(const UList<T>& list, const FixedList<label, N>& indices);
160 
161  //- Construct as copy of FixedList<T, N>
162  template<unsigned N>
163  explicit List(const FixedList<T, N>& list);
164 
165  //- Construct as copy of PtrList<T>
166  explicit List(const PtrList<T>& list);
167 
168  //- Construct as copy of SLList<T>
169  explicit List(const SLList<T>& list);
170 
171  //- Construct as copy of IndirectList contents
172  template<class Addr>
173  explicit List(const IndirectListBase<T, Addr>& list);
174 
175  //- Construct from an initializer list
176  List(std::initializer_list<T> list);
177 
178  //- Move construct from List
179  List(List<T>&& list);
180 
181  //- Move construct from DynamicList
182  template<int SizeMin>
183  List(DynamicList<T, SizeMin>&& list);
184 
185  //- Move construct from SortableList
186  List(SortableList<T>&& list);
187 
188  //- Move construct from SLList
189  List(SLList<T>&& list);
190 
191  //- Construct from Istream
192  List(Istream& is);
193 
194  //- Clone
195  inline autoPtr<List<T>> clone() const;
196 
197 
198  //- Destructor
199  ~List();
200 
201 
202  // Member Functions
203 
204  // Sizing
205 
206  //- Clear the list, i.e. set size to zero
207  inline void clear();
208 
209  //- Adjust allocated size of list.
210  // The boolList version fills new memory with false.
211  inline void resize(const label len);
212 
213  //- Adjust allocated size of list and set val for new elements
214  void resize(const label len, const T& val);
215 
216  //- Adjust allocated size of list \b without necessarily
217  // retaining old content.
218  // If no reallocation is required, the contents remain untouched.
219  // Otherwise the contents will be uninitialized.
220  inline void resize_nocopy(const label len);
221 
222  //- Alias for resize()
223  void setSize(const label n) { this->resize(n); }
224 
225  //- Alias for resize()
226  void setSize(const label n, const T& val) { this->resize(n, val); }
227 
228 
229  // Edit
230 
231  //- Append an element at the end of the list
232  // If this is frequently required, consider a DynamicList
233  inline void append(const T& val);
234 
235  //- Move append an element at the end of the list
236  // If this is frequently required, consider a DynamicList
237  inline void append(T&& val);
238 
239  //- Append a List to the end of this list
240  // If this is frequently required, consider a DynamicList
241  inline void append(const UList<T>& list);
242 
243  //- Append IndirectList contents at the end of this list
244  // If this is frequently required, consider a DynamicList
245  template<class Addr>
246  inline void append(const IndirectListBase<T, Addr>& list);
247 
248  //- Append an element if not already in the list.
249  // \return the change in list length
250  inline label appendUniq(const T& val);
251 
252  //- Transfer the contents of the argument List into this list
253  //- and annul the argument list
254  void transfer(List<T>& list);
255 
256  //- Transfer the contents of the argument List into this list
257  //- and annul the argument list
258  template<int SizeMin>
259  void transfer(DynamicList<T, SizeMin>& list);
260 
261  //- Transfer the contents of the argument List into this list
262  //- and annul the argument list
263  void transfer(SortableList<T>& list);
264 
265  //- Return subscript-checked element of UList and resizing the list
266  //- if required.
267  inline T& newElmt(const label i);
268 
269 
270  // Member Operators
271 
272  //- Assignment to UList operator. Takes linear time
273  void operator=(const UList<T>& a);
274 
275  //- Assignment operator. Takes linear time
276  void operator=(const List<T>& list);
277 
278  //- Assignment to SLList operator. Takes linear time
279  void operator=(const SLList<T>& list);
280 
281  //- Assignment from IndirectList. Takes linear time
282  template<class Addr>
283  void operator=(const IndirectListBase<T, Addr>& list);
284 
285  //- Copy assignment from FixedList
286  template<unsigned N>
287  void operator=(const FixedList<T, N>& list);
288 
289  //- Assignment to an initializer list
290  void operator=(std::initializer_list<T> list);
291 
292  //- Assignment of all entries to the given value
293  inline void operator=(const T& val);
294 
295  //- Assignment of all entries to zero
296  inline void operator=(const Foam::zero);
297 
298  //- Move assignment. Takes constant time
299  void operator=(List<T>&& list);
300 
301  //- Move assignment. Takes constant time.
302  template<int SizeMin>
303  void operator=(DynamicList<T, SizeMin>&& list);
304 
305  //- Move assignment. Takes constant time.
306  void operator=(SortableList<T>&& list);
307 
308  //- Move assignment. Takes constant time
309  void operator=(SLList<T>&& list);
310 
311 
312  // Reading/writing
313 
314  //- Read List from Istream, discarding contents of existing List
315  Istream& readList(Istream& is);
316 
317 
318  // IOstream Operators
319 
320  //- Use the readList() method to read contents from Istream.
321  friend Istream& operator>> <T>
322  (
323  Istream& is,
324  List<T>& list
325  );
326 
327 
328  // Housekeeping
329 
330  //- No shallowCopy permitted
331  void shallowCopy(const UList<T>&) = delete;
332 
333 
334  // Special Methods
335 
336  //- A bitSet::set() method for a list of bool
337  // Increases size when setting an out-of-bounds value.
338  //
339  // \return True if value changed.
340  template<class TypeT = T>
341  typename std::enable_if<std::is_same<bool, TypeT>::value, bool>::type
342  inline set(const label i, bool val = true)
343  {
344  if (i < 0)
345  {
346  return false; // Out-of-bounds: ignore
347  }
348  else if (i >= this->size())
349  {
350  if (!val) // Unset out-of-bounds: ignore
351  {
352  return false;
353  }
354  this->resize(i+1, false); // Adjust size for assign, fill 0
355  }
356 
357  (*this)[i] = val;
358  return true;
359  }
360 };
361 
362 
363 // * * * * * * * * * * * * Template Specializations * * * * * * * * * * * * //
364 
365 //- Specialized list reading for character lists which always uses
366 //- binary format.
367 template<>
368 Istream& List<char>::readList(Istream& is);
369 
370 //- Hashing for List data
371 template<class T>
372 struct Hash<List<T>> : List<T>::hasher {};
373 
374 
375 // * * * * * * * * * * * * * * * IOstream Operators * * * * * * * * * * * * //
376 
377 //- Read List contents from Istream
378 template<class T>
379 Istream& operator>>(Istream& is, List<T>& list)
380 {
381  return list.readList(is);
382 }
383 
384 
385 // * * * * * * * * * * * * * * Global Functions * * * * * * * * * * * * * * //
386 
387 //- Create identity map of the given length with (map[i] == i)
388 // Optionally with an alternative start index, so that (map[i] == i+start)
389 labelList identity(const label len, label start=0);
390 
391 
392 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
393 
394 } // End namespace Foam
395 
396 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
397 
398 #include "ListI.H"
399 
400 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
401 
402 #ifdef NoRepository
403  #include "List.C"
404  #include "ListIO.C"
405 #endif
406 
407 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
408 
409 #endif
410 
411 // ************************************************************************* //
Foam::labelList
List< label > labelList
A List of labels.
Definition: List.H:67
Foam::List::newElmt
T & newElmt(const label i)
Definition: ListI.H:153
Foam::List::resize
void resize(const label len)
Adjust allocated size of list.
Definition: ListI.H:139
Foam::charList
List< char > charList
A List of chars.
Definition: List.H:66
Foam::List::readList
Istream & readList(Istream &is)
Read List from Istream, discarding contents of existing List.
Definition: ListIO.C:49
Foam::DynamicList
A 1D vector of objects of type <T> that resizes itself as necessary to accept the new objects.
Definition: DynamicList.H:55
Foam::SubList
A List obtained as a section of another List.
Definition: SubList.H:54
Foam::one
A class representing the concept of 1 (one) that can be used to avoid manipulating objects known to b...
Definition: one.H:61
Foam::boolList
List< bool > boolList
A List of bools.
Definition: List.H:65
Foam::List::append
void append(const T &val)
Append an element at the end of the list.
Definition: ListI.H:175
Foam::operator>>
Istream & operator>>(Istream &, directionInfo &)
Definition: directionInfo.C:230
Foam::List::resize_nocopy
void resize_nocopy(const label len)
Adjust allocated size of list without necessarily.
Definition: ListI.H:146
Foam::LList
Template class for non-intrusive linked lists.
Definition: LList.H:54
n
label n
Definition: TABSMDCalcMethod2.H:31
Foam::Hash
Hash function class. The default definition is for primitives. Non-primitives used to hash entries on...
Definition: Hash.H:53
ListI.H
SLListFwd.H
Forward declarations for SLList.
Foam::Istream
An Istream is an abstract base class for all input systems (streams, files, token lists etc)....
Definition: Istream.H:61
Foam::List::setSize
void setSize(const label n)
Alias for resize()
Definition: List.H:222
Foam::List::transfer
void transfer(List< T > &list)
Definition: List.C:456
Foam::T
void T(FieldField< Field, Type > &f1, const FieldField< Field, Type > &f2)
Definition: FieldFieldFunctions.C:58
Foam::PtrList
A list of pointers to objects of type <T>, with allocation/deallocation management of the pointers....
Definition: List.H:59
Foam::List::operator=
void operator=(const UList< T > &a)
Assignment to UList operator. Takes linear time.
Definition: List.C:498
Foam::List::appendUniq
label appendUniq(const T &val)
Append an element if not already in the list.
Definition: ListI.H:232
ListIO.C
Foam::List::subList
SubList< T > subList
Declare type of subList.
Definition: List.H:112
Foam::SortableList
A list that is sorted upon construction or when explicitly requested with the sort() method.
Definition: List.H:60
Foam::List::setSize
void setSize(const label n, const T &val)
Alias for resize()
Definition: List.H:225
Foam
Namespace for OpenFOAM.
Definition: atmBoundaryLayer.C:33
Foam::List::List
constexpr List() noexcept
Default construct.
Definition: ListI.H:95
Foam::autoPtr
Pointer management similar to std::unique_ptr, with some additional methods and type checking.
Definition: HashPtrTable.H:53
List.C
UList.H
Foam::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:63
Foam::type
fileName::Type type(const fileName &name, const bool followLink=true)
Return the file type: DIRECTORY or FILE, normally following symbolic links.
Definition: MSwindows.C:590
Foam::FixedList
A 1D vector of objects of type <T> with a fixed length <N>.
Definition: HashTable.H:104
Foam::UList
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:103
Foam::identity
labelList identity(const label len, label start=0)
Create identity map of the given length with (map[i] == i)
Definition: labelList.C:38
Foam::List::clear
void clear()
Clear the list, i.e. set size to zero.
Definition: ListI.H:116
Foam::List::set
std::enable_if< std::is_same< bool, TypeT >::value, bool >::type set(const label i, bool val=true)
A bitSet::set() method for a list of bool.
Definition: List.H:341
Foam::List::shallowCopy
void shallowCopy(const UList< T > &)=delete
No shallowCopy permitted.
Foam::IndirectListBase
Base for lists with indirect addressing, templated on the list contents type and the addressing type....
Definition: IndirectListBase.H:56
N
const Vector< label > N(dict.get< Vector< label >>("N"))
Foam::List::clone
autoPtr< List< T > > clone() const
Clone.
Definition: ListI.H:100
Foam::zero
A class representing the concept of 0 (zero) that can be used to avoid manipulating objects known to ...
Definition: zero.H:62
autoPtr.H