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-2014 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:
106  less(const UList<T>& values)
107  :
108  values_(values)
109  {}
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:
124  greater(const UList<T>& values)
125  :
126  values_(values)
127  {}
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 
422 #define forAll(list, i) \
423  for (Foam::label i=0; i<(list).size(); i++)
425 #define forAllReverse(list, i) \
426  for (Foam::label i=(list).size()-1; i>=0; i--)
427 
441 #define forAllIter(Container,container,iter) \
442  for \
443  ( \
444  Container::iterator iter = (container).begin(); \
445  iter != (container).end(); \
446  ++iter \
447  )
448 
462 #define forAllConstIter(Container,container,iter) \
463  for \
464  ( \
465  Container::const_iterator iter = (container).begin(); \
466  iter != (container).end(); \
467  ++iter \
468  )
469 
470 
471 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
472 
473 #ifdef NoRepository
474 # include "UList.C"
475 #endif
476 
477 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
478 
479 #endif
480 
481 // ************************************************************************* //
iterator end()
Return an iterator to end traversing the UList.
Definition: UListI.H:237
static const UList< T > & null()
Return a null UList.
Definition: UListI.H:51
UList()
Null constructor.
Definition: UListI.H:33
std::streamsize byteSize() const
Return the binary size in number of characters of the UList.
Definition: UList.C:92
UList< label > labelUList
Definition: UList.H:63
bool operator==(const UList< T > &) const
Equality operation on ULists of the same type.
Definition: UList.C:144
const T * const_reverse_iterator
Reverse iterator for reverse traversal of constant UList.
Definition: UList.H:292
Greater function class that can be used for sorting.
Definition: UList.H:117
less(const UList< T > &values)
Definition: UList.H:105
void sort(UList< T > &)
Definition: UList.C:107
const dimensionedScalar b
Wien displacement law constant: default SI units: [m.K].
Definition: createFields.H:28
void writeEntry(Ostream &) const
Write the UList as a dictionary entry.
Definition: UListIO.C:35
void T(FieldField< Field, Type > &f1, const FieldField< Field, Type > &f2)
const_iterator cend() const
Return const_iterator to end traversing the constant UList.
Definition: UListI.H:251
void assign(const UList< T > &)
Assign elements to those from UList.
Definition: UList.C:37
bool empty() const
Return true if the UList is empty (ie, size() is zero).
Definition: UListI.H:313
const_reverse_iterator crbegin() const
Return const_reverse_iterator to begin reverse traversing the UList.
Definition: UListI.H:272
bool operator!=(const UList< T > &) const
The opposite of the equality operation. Takes linear time.
Definition: UList.C:165
T & last()
Return the last element of the list.
Definition: UListI.H:131
An Istream is an abstract base class for all input systems (streams, files, token lists etc)...
Definition: Istream.H:57
T * iterator
Random access iterator for traversing UList.
Definition: UList.H:250
A class for handling words, derived from string.
Definition: word.H:59
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
label fcIndex(const label i) const
Return the forward circular index, i.e. the next index.
Definition: UListI.H:58
void shuffle(UList< T > &)
Definition: UList.C:135
void checkIndex(const label i) const
Check index i is within valid range (0 ... size-1).
Definition: UListI.H:99
reverse_iterator rend()
Return reverse_iterator to end reverse traversing the UList.
Definition: UListI.H:279
T & first()
Return the first element of the list.
Definition: UListI.H:117
Namespace for OpenFOAM.
T * data()
Return a pointer to the first data element,.
Definition: UListI.H:152
void checkSize(const label size) const
Check size is within valid range (0 ... size).
Definition: UListI.H:86
label n
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
iterator begin()
Return an iterator to begin traversing the UList.
Definition: UListI.H:216
T & operator[](const label)
Return element of UList.
Definition: UListI.H:163
void checkStart(const label start) const
Check start is within valid range (0 ... size-1).
Definition: UListI.H:73
label max_size() const
Return size of the largest possible UList.
Definition: UListI.H:306
void swap(UList< T > &)
Swap two ULists of the same type in constant time.
Definition: UList.C:82
label size() const
Return the number of elements in the UList.
Definition: UListI.H:299
const T & const_reference
Type that can be used for storing into.
Definition: UList.H:237
reverse_iterator rbegin()
Return reverse_iterator to begin reverse traversing the UList.
Definition: UListI.H:258
T & reference
Type that can be used for storing into.
Definition: UList.H:233
T * reverse_iterator
Reverse iterator for reverse traversal of UList.
Definition: UList.H:280
T value_type
Type of values the UList contains.
Definition: UList.H:229
System bool.
void operator=(const T &)
Assignment of all entries to the given value.
Definition: UList.C:70
const_iterator cbegin() const
Return const_iterator to begin traversing the constant UList.
Definition: UListI.H:230
bool operator()(const label a, const label b)
Definition: UList.H:110
bool operator>(const UList< T > &) const
Compare two ULists lexicographically. Takes linear time.
Definition: UList.C:203
label difference_type
The type that can represent the difference between any two.
Definition: UList.H:241
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
const T * cdata() const
Return a const pointer to the first data element,.
Definition: UListI.H:145
Istream & operator>>(Istream &, edgeMesh &)
Definition: edgeMeshIO.C:144
An Ostream is an abstract base class for all output systems (streams, files, token lists...
Definition: Ostream.H:53
void reverse(UList< T > &, const label n)
Definition: UListI.H:322
void stableSort(UList< T > &)
Definition: UList.C:121
label rcIndex(const label i) const
Return the reverse circular index, i.e. the previous index.
Definition: UListI.H:65
A List obtained as a section of another List.
Definition: SubList.H:53
const_reverse_iterator crend() const
Return const_reverse_iterator to end reverse traversing the UList.
Definition: UListI.H:293
bool operator>=(const UList< T > &) const
Return true if !(a < b). Takes linear time.
Definition: UList.C:217
const T * const_iterator
Random access iterator for traversing UList.
Definition: UList.H:262
Less function class that can be used for sorting.
Definition: UList.H:99
label size_type
The type that can represent the size of a UList.
Definition: UList.H:244