1 /*---------------------------------------------------------------------------*\
3 \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
5 \\ / A nd | Copyright (C) 1991-2009 OpenCFD Ltd.
7 -------------------------------------------------------------------------------
9 This file is part of OpenFOAM.
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 the
13 Free Software Foundation; either version 2 of the License, or (at your
14 option) any later version.
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
21 You should have received a copy of the GNU General Public License
22 along with OpenFOAM; if not, write to the Free Software Foundation,
23 Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
29 A 1D vector of objects of type \<T\>, where the size of the vector is
30 known and can be used for subscript bounds checking, etc.
32 Storage is not allocated during construction or use but is supplied to
33 the constructor as an argument. This type of list is particularly useful
34 for lists that refer to parts of existing lists such as SubList.
41 \*---------------------------------------------------------------------------*/
46 #include <OpenFOAM/bool.H>
47 #include <OpenFOAM/label.H>
48 #include <OpenFOAM/uLabel.H>
50 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
55 // Forward declaration of friend classes
56 template<class T> class List;
57 template<class T> class SubList;
59 // Forward declaration of friend functions and operators
60 template<class T> class UList;
61 template<class T> Ostream& operator<<(Ostream&, const UList<T>&);
64 /*---------------------------------------------------------------------------*\
65 Class UList Declaration
66 \*---------------------------------------------------------------------------*/
73 //- Number of elements in UList.
76 //- Vector of values of type T.
84 //- Declare friendship with the List class
87 //- Declare friendship with the SubList class
88 friend class SubList<T>;
90 // Static Member Functions
92 //- Return a null UList
93 inline static const UList<T>& null();
97 //- Less function class that can be used for sorting
100 const UList<T>& values_;
104 less(const UList<T>& values)
109 bool operator()(const label a, const label b)
111 return values_[a] < values_[b];
118 //- Null constructor.
121 //- Construct from components
122 inline UList(T* __restrict__ v, label size);
130 //- Return the forward circular index, i.e. the next index
131 // which returns to the first at the end of the list
132 inline label fcIndex(const label i) const;
134 //- Return the reverse circular index, i.e. the previous index
135 // which returns to the last at the begining of the list
136 inline label rcIndex(const label i) const;
138 //- Return the binary size in number of characters of the UList
139 // if the element is a primitive type
140 // i.e. contiguous<T>() == true
141 label byteSize() const;
144 //- Return a const pointer to the first data element,
145 // similar to the STL front() method and the string::data() method
146 // This can be used (with caution) when interfacing with C code.
147 inline const T* cdata() const;
149 //- Return a pointer to the first data element,
150 // similar to the STL front() method and the string::data() method
151 // This can be used (with caution) when interfacing with C code.
157 //- Check start is within valid range (0 ... size-1).
158 inline void checkStart(const label start) const;
160 //- Check size is within valid range (0 ... size).
161 inline void checkSize(const label size) const;
163 //- Check index i is within valid range (0 ... size-1).
164 inline void checkIndex(const label i) const;
167 //- Write the UList as a dictionary entry.
168 void writeEntry(Ostream&) const;
170 //- Write the UList as a dictionary entry with keyword.
171 void writeEntry(const word& keyword, Ostream&) const;
173 //- Assign elements to those from UList.
174 void assign(const UList<T>&);
179 //- Return element of UList.
180 inline T& operator[](const label);
182 //- Return element of constant UList.
183 // Note that the bool specialization adds lazy evaluation so reading
184 // an out-of-range element returns false without any ill-effects
185 inline const T& operator[](const label) const;
187 //- Allow cast to a const List<T>&
188 inline operator const Foam::List<T>&() const;
190 //- Assignment of all entries to the given value
191 void operator=(const T&);
194 // STL type definitions
196 //- Type of values the UList contains.
197 typedef T value_type;
199 //- Type that can be used for storing into
200 // UList::value_type objects.
201 typedef T& reference;
203 //- Type that can be used for storing into
204 // constant UList::value_type objects
205 typedef const T& const_reference;
207 //- The type that can represent the difference between any two
208 // UList iterator objects.
209 typedef label difference_type;
211 //- The type that can represent the size of a UList.
212 typedef label size_type;
217 //- Random access iterator for traversing UList.
220 //- Return an iterator to begin traversing the UList.
221 inline iterator begin();
223 //- Return an iterator to end traversing the UList.
224 inline iterator end();
227 // STL const_iterator
229 //- Random access iterator for traversing UList.
230 typedef const T* const_iterator;
232 //- Return const_iterator to begin traversing the constant UList.
233 inline const_iterator cbegin() const;
235 //- Return const_iterator to end traversing the constant UList.
236 inline const_iterator cend() const;
238 //- Return const_iterator to begin traversing the constant UList.
239 inline const_iterator begin() const;
241 //- Return const_iterator to end traversing the constant UList.
242 inline const_iterator end() const;
245 // STL reverse_iterator
247 //- Reverse iterator for reverse traversal of UList.
248 typedef T* reverse_iterator;
250 //- Return reverse_iterator to begin reverse traversing the UList.
251 inline reverse_iterator rbegin();
253 //- Return reverse_iterator to end reverse traversing the UList.
254 inline reverse_iterator rend();
257 // STL const_reverse_iterator
259 //- Reverse iterator for reverse traversal of constant UList.
260 typedef const T* const_reverse_iterator;
262 //- Return const_reverse_iterator to begin reverse traversing the UList.
263 inline const_reverse_iterator crbegin() const;
265 //- Return const_reverse_iterator to end reverse traversing the UList.
266 inline const_reverse_iterator crend() const;
268 //- Return const_reverse_iterator to begin reverse traversing the UList.
269 inline const_reverse_iterator rbegin() const;
271 //- Return const_reverse_iterator to end reverse traversing the UList.
272 inline const_reverse_iterator rend() const;
275 // STL member functions
277 //- Return the number of elements in the UList.
278 inline label size() const;
280 //- Return size of the largest possible UList.
281 inline label max_size() const;
283 //- Return true if the UList is empty (ie, size() is zero).
284 inline bool empty() const;
286 //- Swap two ULists of the same type in constant time.
287 void swap(UList<T>&);
290 // STL member operators
292 //- Equality operation on ULists of the same type.
293 // Returns true when the ULists are elementwise equal
294 // (using UList::value_type::operator==). Takes linear time.
295 bool operator==(const UList<T>&) const;
297 //- The opposite of the equality operation. Takes linear time.
298 bool operator!=(const UList<T>&) const;
300 //- Compare two ULists lexicographically. Takes linear time.
301 bool operator<(const UList<T>&) const;
303 //- Compare two ULists lexicographically. Takes linear time.
304 bool operator>(const UList<T>&) const;
306 //- Return true if !(a > b). Takes linear time.
307 bool operator<=(const UList<T>&) const;
309 //- Return true if !(a < b). Takes linear time.
310 bool operator>=(const UList<T>&) const;
315 // Write UList to Ostream.
316 friend Ostream& operator<< <T>
324 void sort(UList<T>&);
326 template<class T, class Cmp>
327 void sort(UList<T>&, const Cmp&);
330 void stableSort(UList<T>&);
332 template<class T, class Cmp>
333 void stableSort(UList<T>&, const Cmp&);
335 // Reverse the first n elements of the list
337 inline void reverse(UList<T>&, const label n);
339 // Reverse all the elements of the list
341 inline void reverse(UList<T>&);
344 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
346 } // End namespace Foam
348 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
352 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
355 * @def forAll(list, i)
356 * Loop across all elements in @a list
367 * @def forAllReverse(list, i)
368 * Reverse loop across all elements in @a list
371 * forAllReverse(anyList, i)
378 #define forAll(list, i) \
379 for (Foam::label i=0; i<(list).size(); i++)
381 #define forAllReverse(list, i) \
382 for (Foam::label i=(list).size()-1; i>=0; i--)
385 * @def forAllIter(Container, container, iter)
386 * Iterate across all elements in the @a container object of type
390 * forAll(ContainerType, container, iter)
395 * @sa forAllConstIter
397 #define forAllIter(Container,container,iter) \
400 Container::iterator iter = (container).begin(); \
401 iter != (container).end(); \
406 * @def forAllConstIter(Container, container, iter)
407 * Iterate across all elements in the @a container object of type
408 * @a Container with const access.
411 * forAllConstIter(ContainerType, container, iter)
418 #define forAllConstIter(Container,container,iter) \
421 Container::const_iterator iter = (container).begin(); \
422 iter != (container).end(); \
427 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
433 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
437 // ************************ vim: set sw=4 sts=4 et: ************************ //