FreeFOAM The Cross-Platform CFD Toolkit
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) 1991-2010 OpenCFD Ltd.
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 <OpenFOAM/bool.H>
46 #include <OpenFOAM/label.H>
47 #include <OpenFOAM/uLabel.H>
48 
49 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
50 
51 namespace Foam
52 {
53 
54 // Forward declaration of friend classes
55 template<class T> class List;
56 template<class T> class SubList;
57 
58 // Forward declaration of friend functions and operators
59 template<class T> class UList;
60 template<class T> Ostream& operator<<(Ostream&, const UList<T>&);
61 
62 
63 /*---------------------------------------------------------------------------*\
64  Class UList Declaration
65 \*---------------------------------------------------------------------------*/
66 
67 template<class T>
68 class UList
69 {
70  // Private data
71 
72  //- Number of elements in UList.
73  label size_;
74 
75  //- Vector of values of type T.
76  T* __restrict__ v_;
77 
78 
79 public:
80 
81  // Related types
82 
83  //- Declare friendship with the List class
84  friend class List<T>;
85 
86  //- Declare friendship with the SubList class
87  friend class SubList<T>;
88 
89  // Static Member Functions
90 
91  //- Return a null UList
92  inline static const UList<T>& null();
93 
94  // Public classes
95 
96  //- Less function class that can be used for sorting
97  class less
98  {
99  const UList<T>& values_;
100 
101  public:
102 
103  less(const UList<T>& values)
104  :
105  values_(values)
106  {}
107 
108  bool operator()(const label a, const label b)
109  {
110  return values_[a] < values_[b];
111  }
112  };
113 
114 
115  // Constructors
116 
117  //- Null constructor.
118  inline UList();
119 
120  //- Construct from components
121  inline UList(T* __restrict__ v, label size);
122 
123 
124  // Member Functions
125 
126 
127  // Access
128 
129  //- Return the forward circular index, i.e. the next index
130  // which returns to the first at the end of the list
131  inline label fcIndex(const label i) const;
132 
133  //- Return the reverse circular index, i.e. the previous index
134  // which returns to the last at the begining of the list
135  inline label rcIndex(const label i) const;
136 
137  //- Return the binary size in number of characters of the UList
138  // if the element is a primitive type
139  // i.e. contiguous<T>() == true
140  label byteSize() const;
141 
142 
143  //- Return a const pointer to the first data element,
144  // similar to the STL front() method and the string::data() method
145  // This can be used (with caution) when interfacing with C code.
146  inline const T* cdata() const;
147 
148  //- Return a pointer to the first data element,
149  // similar to the STL front() method and the string::data() method
150  // This can be used (with caution) when interfacing with C code.
151  inline T* data();
152 
153 
154  // Check
155 
156  //- Check start is within valid range (0 ... size-1).
157  inline void checkStart(const label start) const;
158 
159  //- Check size is within valid range (0 ... size).
160  inline void checkSize(const label size) const;
161 
162  //- Check index i is within valid range (0 ... size-1).
163  inline void checkIndex(const label i) const;
164 
165 
166  //- Write the UList as a dictionary entry.
167  void writeEntry(Ostream&) const;
168 
169  //- Write the UList as a dictionary entry with keyword.
170  void writeEntry(const word& keyword, Ostream&) const;
171 
172  //- Assign elements to those from UList.
173  void assign(const UList<T>&);
174 
175 
176  // Member operators
177 
178  //- Return element of UList.
179  inline T& operator[](const label);
180 
181  //- Return element of constant UList.
182  // Note that the bool specialization adds lazy evaluation so reading
183  // an out-of-range element returns false without any ill-effects
184  inline const T& operator[](const label) const;
185 
186  //- Allow cast to a const List<T>&
187  inline operator const Foam::List<T>&() const;
188 
189  //- Assignment of all entries to the given value
190  void operator=(const T&);
191 
192 
193  // STL type definitions
194 
195  //- Type of values the UList contains.
196  typedef T value_type;
197 
198  //- Type that can be used for storing into
199  // UList::value_type objects.
200  typedef T& reference;
201 
202  //- Type that can be used for storing into
203  // constant UList::value_type objects
204  typedef const T& const_reference;
205 
206  //- The type that can represent the difference between any two
207  // UList iterator objects.
208  typedef label difference_type;
209 
210  //- The type that can represent the size of a UList.
211  typedef label size_type;
212 
213 
214  // STL iterator
215 
216  //- Random access iterator for traversing UList.
217  typedef T* iterator;
218 
219  //- Return an iterator to begin traversing the UList.
220  inline iterator begin();
221 
222  //- Return an iterator to end traversing the UList.
223  inline iterator end();
224 
225 
226  // STL const_iterator
227 
228  //- Random access iterator for traversing UList.
229  typedef const T* const_iterator;
230 
231  //- Return const_iterator to begin traversing the constant UList.
232  inline const_iterator cbegin() const;
233 
234  //- Return const_iterator to end traversing the constant UList.
235  inline const_iterator cend() const;
236 
237  //- Return const_iterator to begin traversing the constant UList.
238  inline const_iterator begin() const;
239 
240  //- Return const_iterator to end traversing the constant UList.
241  inline const_iterator end() const;
242 
243 
244  // STL reverse_iterator
245 
246  //- Reverse iterator for reverse traversal of UList.
247  typedef T* reverse_iterator;
248 
249  //- Return reverse_iterator to begin reverse traversing the UList.
250  inline reverse_iterator rbegin();
251 
252  //- Return reverse_iterator to end reverse traversing the UList.
253  inline reverse_iterator rend();
254 
255 
256  // STL const_reverse_iterator
257 
258  //- Reverse iterator for reverse traversal of constant UList.
259  typedef const T* const_reverse_iterator;
260 
261  //- Return const_reverse_iterator to begin reverse traversing the UList.
262  inline const_reverse_iterator crbegin() const;
263 
264  //- Return const_reverse_iterator to end reverse traversing the UList.
265  inline const_reverse_iterator crend() const;
266 
267  //- Return const_reverse_iterator to begin reverse traversing the UList.
268  inline const_reverse_iterator rbegin() const;
269 
270  //- Return const_reverse_iterator to end reverse traversing the UList.
271  inline const_reverse_iterator rend() const;
272 
273 
274  // STL member functions
275 
276  //- Return the number of elements in the UList.
277  inline label size() const;
278 
279  //- Return size of the largest possible UList.
280  inline label max_size() const;
281 
282  //- Return true if the UList is empty (ie, size() is zero).
283  inline bool empty() const;
284 
285  //- Swap two ULists of the same type in constant time.
286  void swap(UList<T>&);
287 
288 
289  // STL member operators
290 
291  //- Equality operation on ULists of the same type.
292  // Returns true when the ULists are elementwise equal
293  // (using UList::value_type::operator==). Takes linear time.
294  bool operator==(const UList<T>&) const;
295 
296  //- The opposite of the equality operation. Takes linear time.
297  bool operator!=(const UList<T>&) const;
298 
299  //- Compare two ULists lexicographically. Takes linear time.
300  bool operator<(const UList<T>&) const;
301 
302  //- Compare two ULists lexicographically. Takes linear time.
303  bool operator>(const UList<T>&) const;
304 
305  //- Return true if !(a > b). Takes linear time.
306  bool operator<=(const UList<T>&) const;
307 
308  //- Return true if !(a < b). Takes linear time.
309  bool operator>=(const UList<T>&) const;
310 
311 
312  // Ostream operator
313 
314  // Write UList to Ostream.
315  friend Ostream& operator<< <T>
316  (
317  Ostream&,
318  const UList<T>&
319  );
320 };
321 
322 template<class T>
323 void sort(UList<T>&);
324 
325 template<class T, class Cmp>
326 void sort(UList<T>&, const Cmp&);
327 
328 template<class T>
329 void stableSort(UList<T>&);
330 
331 template<class T, class Cmp>
332 void stableSort(UList<T>&, const Cmp&);
333 
334 // Reverse the first n elements of the list
335 template<class T>
336 inline void reverse(UList<T>&, const label n);
337 
338 // Reverse all the elements of the list
339 template<class T>
340 inline void reverse(UList<T>&);
341 
342 
343 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
344 
345 } // End namespace Foam
346 
347 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
348 
349 # include "UListI.H"
350 
351 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
352 
377 #define forAll(list, i) \
378  for (Foam::label i=0; i<(list).size(); i++)
379 
380 #define forAllReverse(list, i) \
381  for (Foam::label i=(list).size()-1; i>=0; i--)
382 
396 #define forAllIter(Container,container,iter) \
397  for \
398  ( \
399  Container::iterator iter = (container).begin(); \
400  iter != (container).end(); \
401  ++iter \
402  )
403 
417 #define forAllConstIter(Container,container,iter) \
418  for \
419  ( \
420  Container::const_iterator iter = (container).begin(); \
421  iter != (container).end(); \
422  ++iter \
423  )
424 
425 
426 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
427 
428 #ifdef NoRepository
429 # include "UList.C"
430 #endif
431 
432 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
433 
434 #endif
435 
436 // ************************ vim: set sw=4 sts=4 et: ************************ //