initial commit for version 1.6.x patch release
[OpenFOAM-1.6.x.git] / src / OpenFOAM / containers / Lists / UList / UListI.H
blob5c1df4be15c318df142cb195940dd5643b7db0d7
1 /*---------------------------------------------------------------------------*\
2   =========                 |
3   \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
4    \\    /   O peration     |
5     \\  /    A nd           | Copyright (C) 1991-2009 OpenCFD Ltd.
6      \\/     M anipulation  |
7 -------------------------------------------------------------------------------
8 License
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
19     for more details.
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
25 \*---------------------------------------------------------------------------*/
27 #include "error.H"
28 #include "pTraits.H"
29 #include "Swap.H"
31 // * * * * * * * * * * * * * * * * Constructors  * * * * * * * * * * * * * * //
33 template<class T>
34 inline Foam::UList<T>::UList()
36     size_(0),
37     v_(0)
41 template<class T>
42 inline Foam::UList<T>::UList(T* __restrict__ v, label size)
44     size_(size),
45     v_(v)
49 // * * * * * * * * * * * * * * * Member Functions  * * * * * * * * * * * * * //
51 template<class T>
52 inline const Foam::UList<T>& Foam::UList<T>::null()
54     return *reinterpret_cast< UList<T>* >(0);
58 template<class T>
59 inline Foam::label Foam::UList<T>::fcIndex(const label i) const
61     return (i == size()-1 ? 0 : i+1);
65 template<class T>
66 inline Foam::label Foam::UList<T>::rcIndex(const label i) const
68     return (i ? i-1 : size()-1);
72 // Check start is within valid range (0 ... size-1).
73 template<class T>
74 inline void Foam::UList<T>::checkStart(const label start) const
76     if (start<0 || (start && start>=size_))
77     {
78         FatalErrorIn("UList<T>::checkStart(const label)")
79             << "start " << start << " out of range 0 ... " << max(size_-1, 0)
80             << abort(FatalError);
81     }
85 // Check size is within valid range (0 ... size).
86 template<class T>
87 inline void Foam::UList<T>::checkSize(const label size) const
89     if (size<0 || size>size_)
90     {
91         FatalErrorIn("UList<T>::checkSize(const label)")
92             << "size " << size << " out of range 0 ... " << size_
93             << abort(FatalError);
94     }
98 // Check index i is within valid range (0 ... size-1).
99 template<class T>
100 inline void Foam::UList<T>::checkIndex(const label i) const
102     if (!size_)
103     {
104         FatalErrorIn("UList<T>::checkIndex(const label)")
105             << "attempt to access element from zero sized list"
106             << abort(FatalError);
107     }
108     else if (i<0 || i>=size_)
109     {
110         FatalErrorIn("UList<T>::checkIndex(const label)")
111             << "index " << i << " out of range 0 ... " << size_-1
112             << abort(FatalError);
113     }
117 template<class T>
118 inline const T* Foam::UList<T>::cdata() const
120     return v_;
124 template<class T>
125 inline T* Foam::UList<T>::data()
127     return v_;
131 // * * * * * * * * * * * * * * * Member Operators  * * * * * * * * * * * * * //
134 // element access
135 template<class T>
136 inline T& Foam::UList<T>::operator[](const label i)
138 #   ifdef FULLDEBUG
139     checkIndex(i);
140 #   endif
141     return v_[i];
145 namespace Foam
148     // Template specialization for bool
149     template<>
150     inline const bool& Foam::UList<bool>::operator[](const label i) const
151     {
152         // lazy evaluation - return false for out-of-range
153         if (i < size_)
154         {
155             return v_[i];
156         }
157         else
158         {
159             return Foam::pTraits<bool>::zero;
160         }
161     }
163 } // end of namespace Foam
166 // const element access
167 template<class T>
168 inline const T& Foam::UList<T>::operator[](const label i) const
170 #   ifdef FULLDEBUG
171     checkIndex(i);
172 #   endif
173     return v_[i];
177 // Allow cast to a const List<T>&
178 template<class T>
179 inline Foam::UList<T>::operator const Foam::List<T>&() const
181     return *reinterpret_cast<const List<T>*>(this);
185 // * * * * * * * * * * * * * * STL Member Functions  * * * * * * * * * * * * //
187 template<class T>
188 inline typename Foam::UList<T>::iterator
189 Foam::UList<T>::begin()
191     return v_;
194 template<class T>
195 inline typename Foam::UList<T>::const_iterator
196 Foam::UList<T>::begin() const
198     return v_;
201 template<class T>
202 inline typename Foam::UList<T>::const_iterator
203 Foam::UList<T>::cbegin() const
205     return v_;
208 template<class T>
209 inline typename Foam::UList<T>::iterator
210 Foam::UList<T>::end()
212     return &v_[size_];
215 template<class T>
216 inline typename Foam::UList<T>::const_iterator
217 Foam::UList<T>::end() const
219     return &v_[size_];
222 template<class T>
223 inline typename Foam::UList<T>::const_iterator
224 Foam::UList<T>::cend() const
226     return &v_[size_];
229 template<class T>
230 inline typename Foam::UList<T>::iterator
231 Foam::UList<T>::rbegin()
233     return &v_[size_-1];
236 template<class T>
237 inline typename Foam::UList<T>::const_iterator
238 Foam::UList<T>::rbegin() const
240     return &v_[size_-1];
243 template<class T>
244 inline typename Foam::UList<T>::const_iterator
245 Foam::UList<T>::crbegin() const
247     return &v_[size_-1];
250 template<class T>
251 inline typename Foam::UList<T>::iterator
252 Foam::UList<T>::rend()
254     return &v_[-1];
257 template<class T>
258 inline typename Foam::UList<T>::const_iterator
259 Foam::UList<T>::rend() const
261     return &v_[-1];
264 template<class T>
265 inline typename Foam::UList<T>::const_iterator
266 Foam::UList<T>::crend() const
268     return &v_[-1];
271 template<class T>
272 inline Foam::label Foam::UList<T>::size() const
274     return size_;
278 template<class T>
279 inline Foam::label Foam::UList<T>::max_size() const
281     return labelMax;
285 template<class T>
286 inline bool Foam::UList<T>::empty() const
288     return !size_;
292 // * * * * * * * * * * * * * * * Global Functions  * * * * * * * * * * * * * //
294 template<class T>
295 inline void Foam::reverse(UList<T>& ul, const label n)
297     for (int i=0; i<n/2; i++)
298     {
299         Swap(ul[i], ul[n-1-i]);
300     }
303 template<class T>
304 inline void Foam::reverse(UList<T>& ul)
306     reverse(ul, ul.size());
310 // ************************************************************************* //