UList.H
Go to the documentation of this file.
1 /*---------------------------------------------------------------------------*\
2  ========= |
3  \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
4  \\ / O peration | Website: https://openfoam.org
5  \\ / A nd | Copyright (C) 2011-2024 OpenFOAM Foundation
6  \\/ M anipulation |
7 -------------------------------------------------------------------------------
8 License
9  This file is part of OpenFOAM.
10 
11  OpenFOAM is free software: you can redistribute it and/or modify it
12  under the terms of the GNU General Public License as published by
13  the Free Software Foundation, either version 3 of the License, or
14  (at your option) any later version.
15 
16  OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
17  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
18  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
19  for more details.
20 
21  You should have received a copy of the GNU General Public License
22  along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
23 
24 Class
25  Foam::UList
26 
27 Description
28  A 1D vector of objects of type <T>, where the size of the vector is
29  known and can be used for subscript bounds checking, etc.
30 
31  Storage is not allocated during construction or use but is supplied to
32  the constructor as an argument. This type of list is particularly useful
33  for lists that refer to parts of existing lists such as SubList.
34 
35 SourceFiles
36  UList.C
37  UListI.H
38  UListIO.C
39 
40 \*---------------------------------------------------------------------------*/
41 
42 #ifndef UList_H
43 #define UList_H
44 
45 #include "bool.H"
46 #include "label.H"
47 #include "uLabel.H"
48 #include "nullObject.H"
49 #include "zero.H"
50 
51 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
52 
53 namespace Foam
54 {
55 
56 // Forward declaration of friend classes
57 template<class T> class List;
58 template<class T> class SubList;
59 
60 // Forward declaration of friend functions and operators
61 template<class T> class UList;
62 template<class T> void writeEntry(Ostream& os, const UList<T>&);
63 template<class T> Ostream& operator<<(Ostream&, const UList<T>&);
64 template<class T> Istream& operator>>(Istream&, UList<T>&);
65 
66 typedef UList<label> labelUList;
67 
68 
69 /*---------------------------------------------------------------------------*\
70  Class UList Declaration
71 \*---------------------------------------------------------------------------*/
72 
73 template<class T>
74 class UList
75 {
76  // Private Data
77 
78  //- Number of elements in UList
79  label size_;
80 
81  //- Vector of values of type T
82  T* __restrict__ v_;
83 
84 
85  // Private Member Functions
86 
87  //- Disallow default shallow-copy assignment
88  //
89  // Assignment of UList<T> may need to be either shallow (copy pointer)
90  // or deep (copy elements) depending on context or the particular type
91  // of list derived from UList and it is confusing and prone to error
92  // for the default assignment to be either. The solution is to
93  // disallow default assignment and provide separate 'shallowCopy' and
94  // 'deepCopy' member functions
95  void operator=(const UList<T>&) = delete;
96 
97 
98 public:
99 
100  // Related types
101 
102  //- Declare friendship with the List class
103  friend class List<T>;
104 
105  //- Declare friendship with the SubList class
106  friend class SubList<T>;
107 
108 
109  // Static Member Functions
110 
111  //- Return a null UList
112  inline static const UList<T>& null();
113 
114 
115  // Public classes
116 
117  //- Less function class that can be used for sorting
118  class less
119  {
120  const UList<T>& values_;
121 
122  public:
123 
124  less(const UList<T>& values)
125  :
126  values_(values)
127  {}
128 
129  bool operator()(const label a, const label b)
130  {
131  return values_[a] < values_[b];
132  }
133  };
134 
135  //- Greater function class that can be used for sorting
136  class greater
137  {
138  const UList<T>& values_;
139 
140  public:
141 
142  greater(const UList<T>& values)
143  :
144  values_(values)
145  {}
146 
147  bool operator()(const label a, const label b)
148  {
149  return values_[a] > values_[b];
150  }
151  };
152 
153 
154  // Constructors
155 
156  //- Null constructor
157  inline UList();
158 
159  //- Construct from components
160  inline UList(T* __restrict__ v, label size);
161 
162  //- Copy construct
163  inline UList(const UList<T>&) = default;
164 
165 
166  // Member Functions
167 
168  // Access
169 
170  //- Return the forward circular index, i.e. the next index
171  // which returns to the first at the end of the list
172  inline label fcIndex(const label i) const;
173 
174  //- Return the reverse circular index, i.e. the previous index
175  // which returns to the last at the beginning of the list
176  inline label rcIndex(const label i) const;
177 
178  //- Return the binary size in number of characters of the UList
179  // if the element is a primitive type
180  // i.e. contiguous<T>() == true.
181  // Note that is of type streamsize since used in stream ops
182  std::streamsize byteSize() const;
183 
184  //- Return a const pointer to the first data element,
185  // similar to the STL front() method and the string::data() method
186  // This can be used (with caution) when interfacing with C code
187  inline const T* cdata() const;
188 
189  //- Return a pointer to the first data element,
190  // similar to the STL front() method and the string::data() method
191  // This can be used (with caution) when interfacing with C code
192  inline T* data();
193 
194  //- Return the first element of the list
195  inline T& first();
196 
197  //- Return first element of the list
198  inline const T& first() const;
199 
200  //- Return the last element of the list
201  inline T& last();
202 
203  //- Return the last element of the list
204  inline const T& last() const;
205 
206 
207  // Check
208 
209  //- Check start is within valid range (0 ... size-1)
210  inline void checkStart(const label start) const;
211 
212  //- Check size is within valid range (0 ... size)
213  inline void checkSize(const label size) const;
214 
215  //- Check index i is within valid range (0 ... size-1)
216  inline void checkIndex(const label i) const;
217 
218 
219  // Edit
220 
221  //- Copy the pointer held by the given UList
222  inline void shallowCopy(const UList<T>&);
223 
224  //- Copy elements of the given UList
225  void deepCopy(const UList<T>&);
226 
227 
228  // Member Operators
229 
230  //- Return element of UList
231  inline T& operator[](const label);
232 
233  //- Return element of constant UList
234  // Note that the bool specialisation adds lazy evaluation so reading
235  // an out-of-range element returns false without any ill-effects
236  inline const T& operator[](const label) const;
237 
238  //- Allow cast to a const List<T>&
239  inline operator const Foam::List<T>&() const;
240 
241  //- Assignment of all entries to the given value
242  void operator=(const T&);
243 
244  //- Assignment of all entries to zero
245  void operator=(const zero);
246 
247 
248  // STL type definitions
249 
250  //- Type of values the UList contains
251  typedef T value_type;
252 
253  //- Type that can be used for storing into
254  // UList::value_type objects
255  typedef T& reference;
256 
257  //- Type that can be used for storing into
258  // constant UList::value_type objects
259  typedef const T& const_reference;
260 
261  //- The type that can represent the difference between any two
262  // UList iterator objects
263  typedef label difference_type;
264 
265  //- The type that can represent the size of a UList
266  typedef label size_type;
267 
268 
269  // STL iterator
270 
271  //- Random access iterator for traversing UList
272  typedef T* iterator;
273 
274  //- Return an iterator to begin traversing the UList
275  inline iterator begin();
276 
277  //- Return an iterator to end traversing the UList
278  inline iterator end();
279 
280 
281  // STL const_iterator
282 
283  //- Random access iterator for traversing UList
284  typedef const T* const_iterator;
285 
286  //- Return const_iterator to begin traversing the constant UList
287  inline const_iterator cbegin() const;
288 
289  //- Return const_iterator to end traversing the constant UList
290  inline const_iterator cend() const;
291 
292  //- Return const_iterator to begin traversing the constant UList
293  inline const_iterator begin() const;
294 
295  //- Return const_iterator to end traversing the constant UList
296  inline const_iterator end() const;
297 
298 
299  // STL reverse_iterator
300 
301  //- Reverse iterator for reverse traversal of UList
302  typedef std::reverse_iterator<iterator> reverse_iterator;
303 
304  //- Return reverse_iterator to begin reverse traversing the UList
305  inline reverse_iterator rbegin();
306 
307  //- Return reverse_iterator to end reverse traversing the UList
308  inline reverse_iterator rend();
309 
310 
311  // STL const_reverse_iterator
312 
313  //- Reverse iterator for reverse traversal of constant UList
314  typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
315 
316  //- Return const_reverse_iterator to begin reverse traversing the UList
317  inline const_reverse_iterator crbegin() const;
318 
319  //- Return const_reverse_iterator to end reverse traversing the UList
320  inline const_reverse_iterator crend() const;
321 
322  //- Return const_reverse_iterator to begin reverse traversing the UList
323  inline const_reverse_iterator rbegin() const;
324 
325  //- Return const_reverse_iterator to end reverse traversing the UList
326  inline const_reverse_iterator rend() const;
327 
328 
329  // STL member functions
330 
331  //- Return the number of elements in the UList
332  inline label size() const;
333 
334  //- Return size of the largest possible UList
335  inline label max_size() const;
336 
337  //- Return true if the UList is empty (ie, size() is zero)
338  inline bool empty() const;
339 
340  //- Swap two ULists of the same type in constant time
341  void swap(UList<T>&);
342 
343 
344  // STL member operators
345 
346  //- Equality operation on ULists of the same type.
347  // Returns true when the ULists are element-wise equal
348  // (using UList::value_type::operator==). Takes linear time
349  bool operator==(const UList<T>&) const;
350 
351  //- The opposite of the equality operation. Takes linear time
352  bool operator!=(const UList<T>&) const;
353 
354  //- Compare two ULists lexicographically. Takes linear time
355  bool operator<(const UList<T>&) const;
356 
357  //- Compare two ULists lexicographically. Takes linear time
358  bool operator>(const UList<T>&) const;
359 
360  //- Return true if !(a > b). Takes linear time
361  bool operator<=(const UList<T>&) const;
362 
363  //- Return true if !(a < b). Takes linear time
364  bool operator>=(const UList<T>&) const;
365 
366 
367  // Ostream operator
368 
369  // Write UList to Ostream
370  friend Ostream& operator<< <T>
371  (
372  Ostream&,
373  const UList<T>&
374  );
375 
376  //- Read UList contents from Istream. Requires size to have been set
377  // before
378  friend Istream& operator>> <T>
379  (
380  Istream&,
381  UList<T>&
382  );
383 };
384 
385 template<class T>
386 void sort(UList<T>&);
387 
388 template<class T, class Cmp>
389 void sort(UList<T>&, const Cmp&);
390 
391 template<class T>
392 void stableSort(UList<T>&);
393 
394 template<class T, class Cmp>
395 void stableSort(UList<T>&, const Cmp&);
396 
397 // Reverse the first n elements of the list
398 template<class T>
399 inline void reverse(UList<T>&, const label n);
400 
401 // Reverse all the elements of the list
402 template<class T>
403 inline void reverse(UList<T>&);
404 
405 template<class ListType>
406 void writeListEntry(Ostream& os, const ListType& l);
407 
408 template<class ListType>
409 void writeListEntries(Ostream& os, const ListType& l);
410 
411 template<class ListType>
412 void writeListEntries(Ostream& os, const word& keyword, const ListType& l);
413 
414 
415 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
416 
417 } // End namespace Foam
418 
419 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
420 
421 #include "UListI.H"
422 
423 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
424 
425 //- Loop across all elements in \a list
426 // \par Usage
427 // \code
428 // forAll(anyList, i)
429 // {
430 // statements;
431 // }
432 // \endcode
433 // \sa forAllReverse
434 #define forAll(list, i) \
435  for (Foam::label i=0; i<(list).size(); i++)
436 
437 //- Reverse loop across all elements in \a list
438 // \par Usage
439 // \code
440 // forAllReverse(anyList, i)
441 // {
442 // statements;
443 // }
444 // \endcode
445 // \sa forAll
446 #define forAllReverse(list, i) \
447  for (Foam::label i=(list).size()-1; i>=0; i--)
448 
449 //- Iterate across all elements in the \a container object of type
450 // \a Container.
451 // \par Usage
452 // \code
453 // forAll(ContainerType, container, iter)
454 // {
455 // statements;
456 // }
457 // \endcode
458 // \sa forAllConstIter
459 #define forAllIter(Container,container,iter) \
460  for \
461  ( \
462  Container::iterator iter = (container).begin(); \
463  iter != (container).end(); \
464  ++iter \
465  )
466 
467 //- Iterate across all elements in the \a container object of type
468 // \a Container with const access.
469 // \par Usage
470 // \code
471 // forAllConstIter(ContainerType, container, iter)
472 // {
473 // statements;
474 // }
475 // \endcode
476 // \sa forAllIter
477 #define forAllConstIter(Container,container,iter) \
478  for \
479  ( \
480  Container::const_iterator iter = (container).begin(); \
481  iter != (container).end(); \
482  ++iter \
483  )
484 
485 
486 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
487 
488 #ifdef NoRepository
489  #include "UList.C"
490 #endif
491 
492 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
493 
494 #endif
495 
496 // ************************************************************************* //
label n
System bool.
An Istream is an abstract base class for all input systems (streams, files, token lists etc)....
Definition: Istream.H:60
A 1D array of objects of type <T>, where the size of the vector is known and used for subscript bound...
Definition: List.H:91
An Ostream is an abstract base class for all output systems (streams, files, token lists,...
Definition: Ostream.H:57
A List obtained as a section of another List.
Definition: SubList.H:56
Greater function class that can be used for sorting.
Definition: UList.H:136
bool operator()(const label a, const label b)
Definition: UList.H:146
greater(const UList< T > &values)
Definition: UList.H:141
Less function class that can be used for sorting.
Definition: UList.H:118
less(const UList< T > &values)
Definition: UList.H:123
bool operator()(const label a, const label b)
Definition: UList.H:128
A 1D vector of objects of type <T>, where the size of the vector is known and can be used for subscri...
Definition: UList.H:74
label size_type
The type that can represent the size of a UList.
Definition: UList.H:265
UList()
Null constructor.
Definition: UListI.H:33
const_reverse_iterator crbegin() const
Return const_reverse_iterator to begin reverse traversing the UList.
Definition: UListI.H:280
void deepCopy(const UList< T > &)
Copy elements of the given UList.
Definition: UList.C:35
label difference_type
The type that can represent the difference between any two.
Definition: UList.H:262
T & first()
Return the first element of the list.
Definition: UListI.H:114
label max_size() const
Return size of the largest possible UList.
Definition: UListI.H:318
T value_type
Type of values the UList contains.
Definition: UList.H:250
bool operator<(const UList< T > &) const
Compare two ULists lexicographically. Takes linear time.
Definition: UList.C:173
label rcIndex(const label i) const
Return the reverse circular index, i.e. the previous index.
Definition: UListI.H:65
const_reverse_iterator crend() const
Return const_reverse_iterator to end reverse traversing the UList.
Definition: UListI.H:288
const T * const_iterator
Random access iterator for traversing UList.
Definition: UList.H:283
const_iterator cbegin() const
Return const_iterator to begin traversing the constant UList.
Definition: UListI.H:232
void checkIndex(const label i) const
Check index i is within valid range (0 ... size-1)
Definition: UListI.H:96
T * iterator
Random access iterator for traversing UList.
Definition: UList.H:271
bool operator>=(const UList< T > &) const
Return true if !(a < b). Takes linear time.
Definition: UList.C:218
std::reverse_iterator< const_iterator > const_reverse_iterator
Reverse iterator for reverse traversal of constant UList.
Definition: UList.H:313
label size() const
Return the number of elements in the UList.
Definition: UListI.H:311
label fcIndex(const label i) const
Return the forward circular index, i.e. the next index.
Definition: UListI.H:58
bool empty() const
Return true if the UList is empty (ie, size() is zero)
Definition: UListI.H:325
reverse_iterator rbegin()
Return reverse_iterator to begin reverse traversing the UList.
Definition: UListI.H:264
T & operator[](const label)
Return element of UList.
Definition: UListI.H:167
iterator begin()
Return an iterator to begin traversing the UList.
Definition: UListI.H:216
const_iterator cend() const
Return const_iterator to end traversing the constant UList.
Definition: UListI.H:240
std::reverse_iterator< iterator > reverse_iterator
Reverse iterator for reverse traversal of UList.
Definition: UList.H:301
bool operator>(const UList< T > &) const
Compare two ULists lexicographically. Takes linear time.
Definition: UList.C:204
T & reference
Type that can be used for storing into.
Definition: UList.H:254
const T * cdata() const
Return a const pointer to the first data element,.
Definition: UListI.H:142
std::streamsize byteSize() const
Return the binary size in number of characters of the UList.
Definition: UList.C:100
void swap(UList< T > &)
Swap two ULists of the same type in constant time.
Definition: UList.C:90
bool operator==(const UList< T > &) const
Equality operation on ULists of the same type.
Definition: UList.C:145
void checkSize(const label size) const
Check size is within valid range (0 ... size)
Definition: UListI.H:84
reverse_iterator rend()
Return reverse_iterator to end reverse traversing the UList.
Definition: UListI.H:272
iterator end()
Return an iterator to end traversing the UList.
Definition: UListI.H:224
T * data()
Return a pointer to the first data element,.
Definition: UListI.H:149
bool operator!=(const UList< T > &) const
The opposite of the equality operation. Takes linear time.
Definition: UList.C:166
void shallowCopy(const UList< T > &)
Copy the pointer held by the given UList.
Definition: UListI.H:156
T & last()
Return the last element of the list.
Definition: UListI.H:128
const T & const_reference
Type that can be used for storing into.
Definition: UList.H:258
void checkStart(const label start) const
Check start is within valid range (0 ... size-1)
Definition: UListI.H:72
bool operator<=(const UList< T > &) const
Return true if !(a > b). Takes linear time.
Definition: UList.C:211
A class for handling words, derived from string.
Definition: word.H:62
A class representing the concept of 0 used to avoid unnecessary manipulations for objects that are kn...
Definition: zero.H:50
volScalarField & b
Definition: createFields.H:27
Namespace for OpenFOAM.
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:59
void writeListEntry(Ostream &os, const ListType &l)
Definition: UListIO.C:35
void T(LagrangianPatchField< Type > &f, const LagrangianPatchField< Type > &f1)
void reverse(UList< T > &, const label n)
Definition: UListI.H:334
void writeEntry(Ostream &os, const HashTable< T, Key, Hash > &ht)
Definition: HashTableIO.C:96
void sort(UList< T > &)
Definition: UList.C:115
Istream & operator>>(Istream &, pistonPointEdgeData &)
void writeListEntries(Ostream &os, const ListType &l)
Definition: UListIO.C:58
Ostream & operator<<(Ostream &os, const fvConstraints &constraints)
void stableSort(UList< T > &)
Definition: UList.C:129
UList< label > labelUList
Definition: UList.H:65