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-2019 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 "UList.H"
47 #include "autoPtr.H"
48 #include "one.H"
49 #include "SLListFwd.H"
50 
51 #include <initializer_list>
52 
53 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
54 
55 namespace Foam
56 {
57 
58 // Forward declarations
59 class Istream;
60 class Ostream;
61 
62 template<class T> class List;
63 template<class T, unsigned N> class FixedList;
64 template<class T, int SizeMin> class DynamicList;
65 
66 template<class T> class PtrList;
67 template<class T> class SortableList;
68 template<class T, class Addr> class IndirectListBase;
69 
70 template<class T> Istream& operator>>(Istream& is, List<T>& list);
71 
72 // Common list types
75 typedef List<label> labelList;
76 
77 
78 /*---------------------------------------------------------------------------*\
79  Class List Declaration
80 \*---------------------------------------------------------------------------*/
81 
82 template<class T>
83 class List
84 :
85  public UList<T>
86 {
87  // Private Member Functions
88 
89  //- Allocate list storage
90  inline void doAlloc();
91 
92  //- Reallocate list storage to the given size
93  inline void reAlloc(const label len);
94 
95  //- Copy list of given type.
96  template<class List2>
97  inline void copyList(const List2& list);
98 
99  //- Change allocation size of List. Backend for resize/setSize.
100  void doResize(const label newSize);
101 
102  //- Construct given begin/end iterators and number of elements
103  // Since the size is provided, the end iterator is actually ignored.
104  template<class InputIterator>
105  inline List
106  (
107  InputIterator begIter,
108  InputIterator endIter,
109  const label len
110  );
111 
112 
113 public:
114 
115  // Related types
116 
117  //- Declare type of subList
118  typedef SubList<T> subList;
119 
120 
121  // Static Member Functions
122 
123  //- Return a null List
124  inline static const List<T>& null();
125 
126 
127  // Constructors
128 
129  //- Null constructor
130  inline constexpr List() noexcept;
131 
132  //- Construct with given size
133  explicit List(const label len);
134 
135  //- Construct with given size and value for all elements
136  List(const label len, const T& val);
137 
138  //- Construct with given size initializing all elements to zero
139  List(const label len, const zero);
140 
141  //- Construct with length=1, copying the value as the only content
142  List(const one, const T& val);
143 
144  //- Construct with length=1, moving the value as the only content
145  List(const one, T&& val);
146 
147  //- Construct with length=1, initializing content to zero
148  List(const one, const zero);
149 
150  //- Copy construct from list
151  List(const List<T>& a);
152 
153  //- Copy construct contents from list
154  explicit List(const UList<T>& a);
155 
156  //- Construct as copy or re-use as specified
157  List(List<T>& a, bool reuse);
158 
159  //- Construct as subset
160  List(const UList<T>& list, const labelUList& mapAddressing);
161 
162  //- Construct given begin/end iterators.
163  // Uses std::distance for the size.
164  template<class InputIterator>
165  List(InputIterator begIter, InputIterator endIter);
166 
167  //- Construct as copy of FixedList<T, N>
168  template<unsigned N>
169  explicit List(const FixedList<T, N>& list);
170 
171  //- Construct as copy of PtrList<T>
172  explicit List(const PtrList<T>& list);
173 
174  //- Construct as copy of SLList<T>
175  explicit List(const SLList<T>& list);
176 
177  //- Construct as copy of IndirectList contents
178  template<class Addr>
179  explicit List(const IndirectListBase<T, Addr>& list);
180 
181  //- Construct from an initializer list
182  List(std::initializer_list<T> list);
183 
184  //- Move construct from List
185  List(List<T>&& list);
186 
187  //- Move construct from DynamicList
188  template<int SizeMin>
189  List(DynamicList<T, SizeMin>&& list);
190 
191  //- Move construct from SortableList
192  List(SortableList<T>&& list);
193 
194  //- Move construct from SLList
195  List(SLList<T>&& list);
196 
197  //- Construct from Istream
198  List(Istream& is);
199 
200  //- Clone
201  inline autoPtr<List<T>> clone() const;
202 
203 
204  //- Destructor
205  ~List();
206 
207 
208  // Member Functions
209 
210  // Edit
211 
212  //- Adjust allocated size of list.
213  // The boolList version fills new memory with false.
214  inline void resize(const label newSize);
215 
216  //- Adjust allocated size of list and set val for new elements
217  void resize(const label newSize, const T& val);
218 
219  //- Alias for resize(const label)
220  inline void setSize(const label newSize);
221 
222  //- Alias for resize(const label, const T&)
223  inline void setSize(const label newSize, const T& val);
224 
225  //- Clear the list, i.e. set size to zero
226  inline void clear();
227 
228  //- Append an element at the end of the list
229  inline void append(const T& val);
230 
231  //- Move append an element at the end of the list
232  inline void append(T&& val);
233 
234  //- Append a List to the end of this list
235  inline void append(const UList<T>& list);
236 
237  //- Append IndirectList contents at the end of this list
238  template<class Addr>
239  inline void append(const IndirectListBase<T, Addr>& list);
240 
241  //- Transfer the contents of the argument List into this list
242  //- and annul the argument list
243  void transfer(List<T>& list);
244 
245  //- Transfer the contents of the argument List into this list
246  //- and annul the argument list
247  template<int SizeMin>
248  void transfer(DynamicList<T, SizeMin>& list);
249 
250  //- Transfer the contents of the argument List into this list
251  //- and annul the argument list
252  void transfer(SortableList<T>& list);
253 
254  //- Return subscript-checked element of UList and resizing the list
255  //- if required.
256  inline T& newElmt(const label i);
257 
258 
259  // Member Operators
260 
261  //- Assignment to UList operator. Takes linear time
262  void operator=(const UList<T>& a);
263 
264  //- Assignment operator. Takes linear time
265  void operator=(const List<T>& list);
266 
267  //- Assignment to SLList operator. Takes linear time
268  void operator=(const SLList<T>& list);
269 
270  //- Assignment from IndirectList. Takes linear time
271  template<class Addr>
272  void operator=(const IndirectListBase<T, Addr>& list);
273 
274  //- Assignment to an initializer list
275  void operator=(std::initializer_list<T> list);
276 
277  //- Assignment of all entries to the given value
278  inline void operator=(const T& val);
279 
280  //- Assignment of all entries to zero
281  inline void operator=(const zero);
282 
283  //- Move assignment. Takes constant time
284  void operator=(List<T>&& list);
285 
286  //- Move assignment. Takes constant time.
287  template<int SizeMin>
288  void operator=(DynamicList<T, SizeMin>&& list);
289 
290  //- Move assignment. Takes constant time.
291  void operator=(SortableList<T>&& list);
292 
293  //- Move assignment. Takes constant time
294  void operator=(SLList<T>&& list);
295 
296 
297  // Istream Operator
298 
299  //- Read List from Istream, discarding contents of existing List
300  friend Istream& operator>> <T>
301  (
302  Istream& is,
303  List<T>& list
304  );
305 
306 
307  // Housekeeping
308 
309  //- No shallowCopy permitted
310  void shallowCopy(const UList<T>&) = delete;
311 
312 
313  // Special Methods
314 
315  //- A bitSet::set() method for a list of bool
316  // Increases size when setting an out-of-bounds value.
317  //
318  // \return True if value changed.
319  template<class TypeT = T>
320  typename std::enable_if<std::is_same<bool, TypeT>::value, bool>::type
321  inline set(const label i, bool val = true)
322  {
323  if (i < 0)
324  {
325  return false; // Out-of-bounds: ignore
326  }
327  else if (i >= this->size())
328  {
329  if (!val) // Unset out-of-bounds: ignore
330  {
331  return false;
332  }
333  this->resize(i+1, false); // Adjust size for assign, fill 0
334  }
335 
336  (*this)[i] = val;
337  return true;
338  }
339 };
340 
341 
342 // * * * * * * * * * * * * * * Global Functions * * * * * * * * * * * * * * //
343 
344 //- Create identity map of the given length with (map[i] == i)
345 // Optionally with an alternative start index, so that (map[i] == i+start)
346 labelList identity(const label len, label start=0);
347 
348 
349 //- Hashing for List data, which uses Hasher for contiguous data and
350 //- element-wise incrementally hashing otherwise.
351 template<class T>
352 struct Hash<List<T>>
353 {
354  inline unsigned operator()(const UList<T>& obj, unsigned seed=0) const
355  {
357  {
358  return Hasher(obj.cdata(), obj.size()*sizeof(T), seed);
359  }
360 
361  for (const T& val : obj)
362  {
363  seed = Hash<T>()(val, seed);
364  }
365  return seed;
366  }
367 };
368 
369 
370 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
371 
372 } // End namespace Foam
373 
374 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
375 
376 #include "ListI.H"
377 
378 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
379 
380 #ifdef NoRepository
381  #include "List.C"
382 #endif
383 
384 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
385 
386 #endif
387 
388 // ************************************************************************* //
Foam::labelList
List< label > labelList
A List of labels.
Definition: List.H:74
Foam::List::newElmt
T & newElmt(const label i)
Definition: ListI.H:160
Foam::val
label ListType::const_reference val
Definition: ListOps.H:407
Foam::charList
List< char > charList
A List of chars.
Definition: List.H:73
Foam::DynamicList
A 1D vector of objects of type <T> that resizes itself as necessary to accept the new objects.
Definition: DynamicList.H:57
Foam::Hasher
unsigned Hasher(const void *data, size_t len, unsigned seed=0)
Bob Jenkins's 96-bit mixer hashing function (lookup3)
Definition: Hasher.C:473
Foam::SubList
A List obtained as a section of another List.
Definition: SubList.H:53
Foam::one
A class representing the concept of 1 (one), which can be used to avoid manipulating objects that are...
Definition: one.H:60
Foam::boolList
List< bool > boolList
A List of bools.
Definition: List.H:72
Foam::List::append
void append(const T &val)
Append an element at the end of the list.
Definition: ListI.H:182
Foam::Hash::operator()
unsigned operator()(const T &obj, unsigned seed=0) const
Definition: Hash.H:57
Foam::operator>>
Istream & operator>>(Istream &, directionInfo &)
Definition: directionInfo.C:228
Foam::LList
Template class for non-intrusive linked lists.
Definition: LList.H:54
one.H
Foam::Hash
Hash function class. The default definition is for primitives, non-primitives used to hash entries on...
Definition: Hash.H:55
ListI.H
SLListFwd.H
Forward declarations for SLList.
Foam::label
intWM_LABEL_SIZE_t label
A label is an int32_t or int64_t as specified by the pre-processor macro WM_LABEL_SIZE.
Definition: label.H:62
Foam::Istream
An Istream is an abstract base class for all input systems (streams, files, token lists etc)....
Definition: Istream.H:61
Foam::List::transfer
void transfer(List< T > &list)
Definition: List.C:436
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:65
Foam::List::resize
void resize(const label newSize)
Adjust allocated size of list.
Definition: ListI.H:139
Foam::List::subList
SubList< T > subList
Declare type of subList.
Definition: List.H:117
Foam::SortableList
A list that is sorted upon construction or when explicitly requested with the sort() method.
Definition: List.H:66
Foam
Namespace for OpenFOAM.
Definition: atmBoundaryLayer.C:33
Foam::List::List
constexpr List() noexcept
Null constructor.
Definition: ListI.H:94
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: HashTable.H:102
Foam::start
label ListType::const_reference const label start
Definition: ListOps.H:408
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:115
Foam::UList::cdata
const T * cdata() const
Return a const pointer to the first data element.
Definition: UListI.H:198
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:320
Foam::UList::size
void size(const label n) noexcept
Override size to be inconsistent with allocated storage.
Definition: UListI.H:360
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:57
N
const Vector< label > N(dict.get< Vector< label >>("N"))
Foam::List::clone
autoPtr< List< T > > clone() const
Clone.
Definition: ListI.H:99
Foam::List::setSize
void setSize(const label newSize)
Alias for resize(const label)
Definition: ListI.H:146
Foam::is_contiguous
A template class to specify that a data type can be considered as being contiguous in memory.
Definition: contiguous.H:75
Foam::Hash< List< T > >::operator()
unsigned operator()(const UList< T > &obj, unsigned seed=0) const
Definition: List.H:353
Foam::zero
A class representing the concept of 0 (zero), which can be used to avoid manipulating objects that ar...
Definition: zero.H:61
autoPtr.H