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