initial commit for version 1.5.x patch release
[OpenFOAM-1.5.x.git] / src / OpenFOAM / containers / Lists / UList / UListI.H
blob0e05218ca475b897c0d1a5d4ab2af852d0878998
1 /*---------------------------------------------------------------------------*\
2   =========                 |
3   \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
4    \\    /   O peration     |
5     \\  /    A nd           | Copyright (C) 1991-2008 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 "Swap.H"
30 // * * * * * * * * * * * * * * * * Constructors  * * * * * * * * * * * * * * //
32 template<class T>
33 inline Foam::UList<T>::UList()
35     size_(0),
36     v_(0)
40 template<class T>
41 inline Foam::UList<T>::UList(T* __restrict__ v, label size)
43     size_(size),
44     v_(v)
48 // * * * * * * * * * * * * * * * Member Functions  * * * * * * * * * * * * * //
50 template<class T>
51 inline Foam::label Foam::UList<T>::fcIndex(const label i) const
53     return (i == size()-1 ? 0 : i+1); 
57 template<class T>
58 inline Foam::label Foam::UList<T>::rcIndex(const label i) const
60     return (i == 0 ? size()-1 : i-1); 
64 // Check start is within valid range (0 ... size-1).
65 template<class T>
66 inline void Foam::UList<T>::checkStart(const label start) const
68     if (start<0 || (start && start>=size_))
69     {
70         FatalErrorIn("UList<T>::checkStart(const label)")
71             << "start " << start << " out of range 0 ... " << max(size_-1, 0)
72             << abort(FatalError);
73     }
77 // Check size is within valid range (0 ... size).
78 template<class T>
79 inline void Foam::UList<T>::checkSize(const label size) const
81     if (size<0 || size>size_)
82     {
83         FatalErrorIn("UList<T>::checkSize(const label)")
84             << "size " << size << " out of range 0 ... " << size_
85             << abort(FatalError);
86     }
90 // Check index i is within valid range (0 ... size-1).
91 template<class T>
92 inline void Foam::UList<T>::checkIndex(const label i) const
94     if (!size_)
95     {
96         FatalErrorIn("UList<T>::checkIndex(const label)")
97             << "attempt to access element from zero sized list"
98             << abort(FatalError);
99     }
100     else if (i<0 || i>=size_)
101     {
102         FatalErrorIn("UList<T>::checkIndex(const label)")
103             << "index " << i << " out of range 0 ... " << size_-1
104             << abort(FatalError);
105     }
109 // * * * * * * * * * * * * * * * Member Operators  * * * * * * * * * * * * * //
111 // Return subscript-checked element access
112 template<class T>
113 inline T& Foam::UList<T>::operator[](const label i)
115 #   ifdef FULLDEBUG
116     checkIndex(i);
117 #   endif
118     return v_[i];
122 // Return subscript-checked const element access
123 template<class T>
124 inline const T& Foam::UList<T>::operator[](const label i) const
126 #   ifdef FULLDEBUG
127     checkIndex(i);
128 #   endif
129     return v_[i];
133 // Allow cast to a const List<T>&
134 template<class T>
135 inline Foam::UList<T>::operator const Foam::List<T>&() const
137     return *reinterpret_cast<const List<T>*>(this);
141 // * * * * * * * * * * * * * * STL Member Functions  * * * * * * * * * * * * //
143 template<class T>
144 inline typename Foam::UList<T>::iterator
145 Foam::UList<T>::begin()
147     return v_;
150 template<class T>
151 inline typename Foam::UList<T>::const_iterator
152 Foam::UList<T>::begin() const
154     return v_;
157 template<class T>
158 inline typename Foam::UList<T>::iterator
159 Foam::UList<T>::end()
161     return &v_[size_];
164 template<class T>
165 inline typename Foam::UList<T>::const_iterator
166 Foam::UList<T>::end() const
168     return &v_[size_];
171 template<class T>
172 inline typename Foam::UList<T>::iterator
173 Foam::UList<T>::rbegin()
175     return &v_[size_-1];
178 template<class T>
179 inline typename Foam::UList<T>::const_iterator
180 Foam::UList<T>::rbegin() const
182     return &v_[size_-1];
185 template<class T>
186 inline typename Foam::UList<T>::iterator
187 Foam::UList<T>::rend()
189     return &v_[-1];
192 template<class T>
193 inline typename Foam::UList<T>::const_iterator
194 Foam::UList<T>::rend() const
196     return &v_[-1];
199 template<class T>
200 inline Foam::label Foam::UList<T>::size() const
202     return size_;
206 template<class T>
207 inline Foam::label Foam::UList<T>::max_size() const
209     return labelMax;
213 template<class T>
214 inline bool Foam::UList<T>::empty() const
216     return (size_ == 0);
220 // * * * * * * * * * * * * * * * Global Functions  * * * * * * * * * * * * * //
222 template<class T>
223 inline void Foam::reverse(UList<T>& ul, const label n)
225     for (int i=0; i<n/2; i++)
226     {
227         Swap(ul[i], ul[n-1-i]);
228     }
231 template<class T>
232 inline void Foam::reverse(UList<T>& ul)
234     reverse(ul, ul.size());
238 // ************************************************************************* //