UList.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 | Copyright (C) 2011-2015 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 
50 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
51 
52 namespace Foam
53 {
54 
55 // Forward declaration of friend classes
56 template<class T> class List;
57 template<class T> class SubList;
58 
59 // Forward declaration of friend functions and operators
60 template<class T> class UList;
61 template<class T> Ostream& operator<<(Ostream&, const UList<T>&);
62 template<class T> Istream& operator>>(Istream&, UList<T>&);
63 
64 typedef UList<label> labelUList;
65 
66 /*---------------------------------------------------------------------------*\
67  Class UList Declaration
68 \*---------------------------------------------------------------------------*/
69 
70 template<class T>
71 class UList
72 {
73  // Private data
74 
75  //- Number of elements in UList.
76  label size_;
77 
78  //- Vector of values of type T.
79  T* __restrict__ v_;
80 
81 
82 public:
83 
84  // Related types
85 
86  //- Declare friendship with the List class
87  friend class List<T>;
88 
89  //- Declare friendship with the SubList class
90  friend class SubList<T>;
91 
92  // Static Member Functions
93 
94  //- Return a null UList
95  inline static const UList<T>& null();
96 
97  // Public classes
98 
99  //- Less function class that can be used for sorting
100  class less
101  {
102  const UList<T>& values_;
103 
104  public:
105 
106  less(const UList<T>& values)
107  :
108  values_(values)
109  {}
110 
111  bool operator()(const label a, const label b)
112  {
113  return values_[a] < values_[b];
114  }
115  };
116 
117  //- Greater function class that can be used for sorting
118  class greater
119  {
120  const UList<T>& values_;
121 
122  public:
123 
124  greater(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 
136  // Constructors
137 
138  //- Null constructor.
139  inline UList();
140 
141  //- Construct from components
142  inline UList(T* __restrict__ v, label size);
143 
144 
145  // Member Functions
146 
147 
148  // Access
149 
150  //- Return the forward circular index, i.e. the next index
151  // which returns to the first at the end of the list
152  inline label fcIndex(const label i) const;
153 
154  //- Return the reverse circular index, i.e. the previous index
155  // which returns to the last at the beginning of the list
156  inline label rcIndex(const label i) const;
157 
158  //- Return the binary size in number of characters of the UList
159  // if the element is a primitive type
160  // i.e. contiguous<T>() == true.
161  // Note that is of type streamsize since used in stream ops
162  std::streamsize byteSize() const;
163 
164 
165  //- Return a const pointer to the first data element,
166  // similar to the STL front() method and the string::data() method
167  // This can be used (with caution) when interfacing with C code.
168  inline const T* cdata() const;
169 
170  //- Return a pointer to the first data element,
171  // similar to the STL front() method and the string::data() method
172  // This can be used (with caution) when interfacing with C code.
173  inline T* data();
174 
175  //- Return the first element of the list.
176  inline T& first();
177 
178  //- Return first element of the list.
179  inline const T& first() const;
180 
181  //- Return the last element of the list.
182  inline T& last();
183 
184  //- Return the last element of the list.
185  inline const T& last() const;
186 
187 
188  // Check
189 
190  //- Check start is within valid range (0 ... size-1).
191  inline void checkStart(const label start) const;
192 
193  //- Check size is within valid range (0 ... size).
194  inline void checkSize(const label size) const;
195 
196  //- Check index i is within valid range (0 ... size-1).
197  inline void checkIndex(const label i) const;
198 
199 
200  //- Write the UList as a dictionary entry.
201  void writeEntry(Ostream&) const;
202 
203  //- Write the UList as a dictionary entry with keyword.
204  void writeEntry(const word& keyword, Ostream&) const;
205 
206  //- Assign elements to those from UList.
207  void assign(const UList<T>&);
208 
209 
210  // Member operators
211 
212  //- Return element of UList.
213  inline T& operator[](const label);
214 
215  //- Return element of constant UList.
216  // Note that the bool specialization adds lazy evaluation so reading
217  // an out-of-range element returns false without any ill-effects
218  inline const T& operator[](const label) const;
219 
220  //- Allow cast to a const List<T>&
221  inline operator const Foam::List<T>&() const;
222 
223  //- Assignment of all entries to the given value
224  void operator=(const T&);
225 
226 
227  // STL type definitions
228 
229  //- Type of values the UList contains.
230  typedef T value_type;
231 
232  //- Type that can be used for storing into
233  // UList::value_type objects.
234  typedef T& reference;
235 
236  //- Type that can be used for storing into
237  // constant UList::value_type objects
238  typedef const T& const_reference;
239 
240  //- The type that can represent the difference between any two
241  // UList iterator objects.
242  typedef label difference_type;
243 
244  //- The type that can represent the size of a UList.
245  typedef label size_type;
246 
247 
248  // STL iterator
249 
250  //- Random access iterator for traversing UList.
251  typedef T* iterator;
252 
253  //- Return an iterator to begin traversing the UList.
254  inline iterator begin();
255 
256  //- Return an iterator to end traversing the UList.
257  inline iterator end();
258 
259 
260  // STL const_iterator
261 
262  //- Random access iterator for traversing UList.
263  typedef const T* const_iterator;
264 
265  //- Return const_iterator to begin traversing the constant UList.
266  inline const_iterator cbegin() const;
267 
268  //- Return const_iterator to end traversing the constant UList.
269  inline const_iterator cend() const;
270 
271  //- Return const_iterator to begin traversing the constant UList.
272  inline const_iterator begin() const;
273 
274  //- Return const_iterator to end traversing the constant UList.
275  inline const_iterator end() const;
276 
277 
278  // STL reverse_iterator
279 
280  //- Reverse iterator for reverse traversal of UList.
281  typedef T* reverse_iterator;
282 
283  //- Return reverse_iterator to begin reverse traversing the UList.
284  inline reverse_iterator rbegin();
285 
286  //- Return reverse_iterator to end reverse traversing the UList.
287  inline reverse_iterator rend();
288 
289 
290  // STL const_reverse_iterator
291 
292  //- Reverse iterator for reverse traversal of constant UList.
293  typedef const T* const_reverse_iterator;
294 
295  //- Return const_reverse_iterator to begin reverse traversing the UList.
296  inline const_reverse_iterator crbegin() const;
297 
298  //- Return const_reverse_iterator to end reverse traversing the UList.
299  inline const_reverse_iterator crend() const;
300 
301  //- Return const_reverse_iterator to begin reverse traversing the UList.
302  inline const_reverse_iterator rbegin() const;
303 
304  //- Return const_reverse_iterator to end reverse traversing the UList.
305  inline const_reverse_iterator rend() const;
306 
307 
308  // STL member functions
309 
310  //- Return the number of elements in the UList.
311  inline label size() const;
312 
313  //- Return size of the largest possible UList.
314  inline label max_size() const;
315 
316  //- Return true if the UList is empty (ie, size() is zero).
317  inline bool empty() const;
318 
319  //- Swap two ULists of the same type in constant time.
320  void swap(UList<T>&);
321 
322 
323  // STL member operators
324 
325  //- Equality operation on ULists of the same type.
326  // Returns true when the ULists are elementwise equal
327  // (using UList::value_type::operator==). Takes linear time.
328  bool operator==(const UList<T>&) const;
329 
330  //- The opposite of the equality operation. Takes linear time.
331  bool operator!=(const UList<T>&) const;
332 
333  //- Compare two ULists lexicographically. Takes linear time.
334  bool operator<(const UList<T>&) const;
335 
336  //- Compare two ULists lexicographically. Takes linear time.
337  bool operator>(const UList<T>&) const;
338 
339  //- Return true if !(a > b). Takes linear time.
340  bool operator<=(const UList<T>&) const;
341 
342  //- Return true if !(a < b). Takes linear time.
343  bool operator>=(const UList<T>&) const;
344 
345 
346  // Ostream operator
347 
348  // Write UList to Ostream.
349  friend Ostream& operator<< <T>
350  (
351  Ostream&,
352  const UList<T>&
353  );
354 
355  //- Read UList contents from Istream. Requires size to have been set
356  // before.
357  friend Istream& operator>> <T>
358  (
359  Istream&,
360  UList<T>&
361  );
362 };
363 
364 template<class T>
365 void sort(UList<T>&);
366 
367 template<class T, class Cmp>
368 void sort(UList<T>&, const Cmp&);
369 
370 template<class T>
371 void stableSort(UList<T>&);
372 
373 template<class T, class Cmp>
374 void stableSort(UList<T>&, const Cmp&);
375 
376 template<class T>
377 void shuffle(UList<T>&);
378 
379 // Reverse the first n elements of the list
380 template<class T>
381 inline void reverse(UList<T>&, const label n);
382 
383 // Reverse all the elements of the list
384 template<class T>
385 inline void reverse(UList<T>&);
386 
387 
388 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
389 
390 } // End namespace Foam
391 
392 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
393 
394 # include "UListI.H"
395 
396 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
397 
398 //- Loop across all elements in \a list
399 // \par Usage
400 // \code
401 // forAll(anyList, i)
402 // {
403 // statements;
404 // }
405 // \endcode
406 // \sa forAllReverse
407 #define forAll(list, i) \
408  for (Foam::label i=0; i<(list).size(); i++)
409 
410 //- Reverse loop across all elements in \a list
411 // \par Usage
412 // \code
413 // forAllReverse(anyList, i)
414 // {
415 // statements;
416 // }
417 // \endcode
418 // \sa forAll
419 #define forAllReverse(list, i) \
420  for (Foam::label i=(list).size()-1; i>=0; i--)
421 
422 //- Iterate across all elements in the \a container object of type
423 // \a Container.
424 // \par Usage
425 // \code
426 // forAll(ContainerType, container, iter)
427 // {
428 // statements;
429 // }
430 // \endcode
431 // \sa forAllConstIter
432 #define forAllIter(Container,container,iter) \
433  for \
434  ( \
435  Container::iterator iter = (container).begin(); \
436  iter != (container).end(); \
437  ++iter \
438  )
439 
440 //- Iterate across all elements in the \a container object of type
441 // \a Container with const access.
442 // \par Usage
443 // \code
444 // forAllConstIter(ContainerType, container, iter)
445 // {
446 // statements;
447 // }
448 // \endcode
449 // \sa forAllIter
450 #define forAllConstIter(Container,container,iter) \
451  for \
452  ( \
453  Container::const_iterator iter = (container).begin(); \
454  iter != (container).end(); \
455  ++iter \
456  )
457 
458 
459 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
460 
461 #ifdef NoRepository
462 # include "UList.C"
463 #endif
464 
465 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
466 
467 #endif
468 
469 // ************************************************************************* //
Foam::UList::crbegin
const_reverse_iterator crbegin() const
Return const_reverse_iterator to begin reverse traversing the UList.
Definition: UListI.H:272
Foam::UList::cbegin
const_iterator cbegin() const
Return const_iterator to begin traversing the constant UList.
Definition: UListI.H:230
Foam::UList::swap
void swap(UList< T > &)
Swap two ULists of the same type in constant time.
Definition: UList.C:82
Foam::word
A class for handling words, derived from string.
Definition: word.H:59
forAllIter
#define forAllIter(Container, container, iter)
Iterate across all elements in the container object of type.
Definition: UList.H:431
Foam::UList::operator[]
T & operator[](const label)
Return element of UList.
Foam::UList::cend
const_iterator cend() const
Return const_iterator to end traversing the constant UList.
Definition: UListI.H:251
Foam::UList::first
T & first()
Return the first element of the list.
Definition: UListI.H:117
Foam::UList::const_reference
const typedef T & const_reference
Type that can be used for storing into.
Definition: UList.H:237
forAll
#define forAll(list, i)
Loop across all elements in list.
Definition: UList.H:406
Foam::UList::operator<=
bool operator<=(const UList< T > &) const
Return true if !(a > b). Takes linear time.
Definition: UList.C:210
Foam::UList::max_size
label max_size() const
Return size of the largest possible UList.
Definition: UListI.H:306
Foam::SubList
A List obtained as a section of another List.
Definition: SubList.H:53
Foam::UList::data
T * data()
Return a pointer to the first data element,.
Definition: UListI.H:152
Foam::UList::operator>=
bool operator>=(const UList< T > &) const
Return true if !(a < b). Takes linear time.
Definition: UList.C:217
Foam::UList::v_
T *__restrict__ v_
Vector of values of type T.
Definition: UList.H:78
Foam::UList::checkSize
void checkSize(const label size) const
Check size is within valid range (0 ... size).
Definition: UListI.H:86
Foam::UList::const_reverse_iterator
const typedef T * const_reverse_iterator
Reverse iterator for reverse traversal of constant UList.
Definition: UList.H:292
Foam::UList::size_type
label size_type
The type that can represent the size of a UList.
Definition: UList.H:244
Foam::shuffle
void shuffle(UList< T > &)
Definition: UList.C:135
Foam::UList::rcIndex
label rcIndex(const label i) const
Return the reverse circular index, i.e. the previous index.
Definition: UListI.H:65
Foam::UList::rbegin
reverse_iterator rbegin()
Return reverse_iterator to begin reverse traversing the UList.
Definition: UListI.H:258
Foam::UList::begin
iterator begin()
Return an iterator to begin traversing the UList.
Definition: UListI.H:216
forAllConstIter
#define forAllConstIter(Container, container, iter)
Iterate across all elements in the container object of type.
Definition: UList.H:449
Foam::UList::crend
const_reverse_iterator crend() const
Return const_reverse_iterator to end reverse traversing the UList.
Definition: UListI.H:293
Foam::UList::checkStart
void checkStart(const label start) const
Check start is within valid range (0 ... size-1).
Definition: UListI.H:73
UList.C
n
label n
Definition: TABSMDCalcMethod2.H:31
Foam::stableSort
void stableSort(UList< T > &)
Definition: UList.C:121
Foam::UList::operator!=
bool operator!=(const UList< T > &) const
The opposite of the equality operation. Takes linear time.
Definition: UList.C:165
Foam::UList::reverse_iterator
T * reverse_iterator
Reverse iterator for reverse traversal of UList.
Definition: UList.H:280
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:59
Foam::constant::physicoChemical::b
const dimensionedScalar b
Wien displacement law constant: default SI units: [m.K].
Definition: createFields.H:28
Foam::Istream
An Istream is an abstract base class for all input systems (streams, files, token lists etc)....
Definition: Istream.H:57
Foam::UList::iterator
T * iterator
Random access iterator for traversing UList.
Definition: UList.H:250
Foam::UList::last
T & last()
Return the last element of the list.
Definition: UListI.H:131
Foam::UList::assign
void assign(const UList< T > &)
Assign elements to those from UList.
Definition: UList.C:37
Foam::UList::reference
T & reference
Type that can be used for storing into.
Definition: UList.H:233
Foam::UList::greater::operator()
bool operator()(const label a, const label b)
Definition: UList.H:128
Foam::T
void T(FieldField< Field, Type > &f1, const FieldField< Field, Type > &f2)
Definition: FieldFieldFunctions.C:55
bool.H
System bool.
Foam::UList::rend
reverse_iterator rend()
Return reverse_iterator to end reverse traversing the UList.
Definition: UListI.H:279
Foam::operator<<
Ostream & operator<<(Ostream &, const edgeMesh &)
Definition: edgeMeshIO.C:130
Foam::UList::operator==
bool operator==(const UList< T > &) const
Equality operation on ULists of the same type.
Definition: UList.C:144
forAllReverse
#define forAllReverse(list, i)
Reverse loop across all elements in list.
Definition: UList.H:418
Foam::UList::operator<
bool operator<(const UList< T > &) const
Compare two ULists lexicographically. Takes linear time.
Definition: UList.C:172
Foam
Namespace for OpenFOAM.
Definition: combustionModel.C:30
Foam::UList::UList
UList()
Null constructor.
Definition: UListI.H:33
Foam::UList::less
Less function class that can be used for sorting.
Definition: UList.H:99
Foam::UList::operator>
bool operator>(const UList< T > &) const
Compare two ULists lexicographically. Takes linear time.
Definition: UList.C:203
Foam::UList::size_
label size_
Number of elements in UList.
Definition: UList.H:75
Foam::UList::greater::values_
const UList< T > & values_
Definition: UList.H:119
Foam::UList::greater::greater
greater(const UList< T > &values)
Definition: UList.H:123
Foam::UList::difference_type
label difference_type
The type that can represent the difference between any two.
Definition: UList.H:241
Foam::UList::fcIndex
label fcIndex(const label i) const
Return the forward circular index, i.e. the next index.
Definition: UListI.H:58
label.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:59
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:60
Foam::operator>>
Istream & operator>>(Istream &, edgeMesh &)
Definition: edgeMeshIO.C:141
Foam::UList::writeEntry
void writeEntry(Ostream &) const
Write the UList as a dictionary entry.
Foam::UList::byteSize
std::streamsize byteSize() const
Return the binary size in number of characters of the UList.
Definition: UList.C:92
Foam::UList::cdata
const T * cdata() const
Return a const pointer to the first data element,.
Definition: UListI.H:145
Foam::UList::const_iterator
const typedef T * const_iterator
Random access iterator for traversing UList.
Definition: UList.H:262
Foam::UList::empty
bool empty() const
Return true if the UList is empty (ie, size() is zero).
Definition: UListI.H:313
nullObject.H
Foam::UList::less::values_
const UList< T > & values_
Definition: UList.H:101
List
Definition: Test.C:19
Foam::UList::greater
Greater function class that can be used for sorting.
Definition: UList.H:117
Foam::sort
void sort(UList< T > &)
Definition: UList.C:107
Foam::Ostream
An Ostream is an abstract base class for all output systems (streams, files, token lists,...
Definition: Ostream.H:53
Foam::UList::less::operator()
bool operator()(const label a, const label b)
Definition: UList.H:110
uLabel.H
Foam::labelUList
UList< label > labelUList
Definition: UList.H:63
Foam::UList::end
iterator end()
Return an iterator to end traversing the UList.
Definition: UListI.H:237
Foam::UList::size
label size() const
Return the number of elements in the UList.
Definition: UListI.H:299
Foam::UList::checkIndex
void checkIndex(const label i) const
Check index i is within valid range (0 ... size-1).
Definition: UListI.H:99
Foam::UList::value_type
T value_type
Type of values the UList contains.
Definition: UList.H:229
Foam::UList::operator=
void operator=(const T &)
Assignment of all entries to the given value.
Definition: UList.C:70
Foam::UList::less::less
less(const UList< T > &values)
Definition: UList.H:105
UListI.H
Foam::reverse
void reverse(UList< T > &, const label n)
Definition: UListI.H:322