UListI.H 6.59 KB
Newer Older
1
2
3
4
/*---------------------------------------------------------------------------*\
  =========                 |
  \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
   \\    /   O peration     |
Mark Olesen's avatar
Mark Olesen committed
5
    \\  /    A nd           | Copyright (C) 1991-2009 OpenCFD Ltd.
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
     \\/     M anipulation  |
-------------------------------------------------------------------------------
License
    This file is part of OpenFOAM.

    OpenFOAM is free software; you can redistribute it and/or modify it
    under the terms of the GNU General Public License as published by the
    Free Software Foundation; either version 2 of the License, or (at your
    option) any later version.

    OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
    ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    for more details.

    You should have received a copy of the GNU General Public License
    along with OpenFOAM; if not, write to the Free Software Foundation,
    Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA

\*---------------------------------------------------------------------------*/

#include "error.H"
Mark Olesen's avatar
Mark Olesen committed
28
#include "pTraits.H"
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#include "Swap.H"

// * * * * * * * * * * * * * * * * Constructors  * * * * * * * * * * * * * * //

template<class T>
inline Foam::UList<T>::UList()
:
    size_(0),
    v_(0)
{}


template<class T>
inline Foam::UList<T>::UList(T* __restrict__ v, label size)
:
    size_(size),
    v_(v)
{}


// * * * * * * * * * * * * * * * Member Functions  * * * * * * * * * * * * * //

51
52
53
54
55
56
57
template<class T>
inline const Foam::UList<T>& Foam::UList<T>::null()
{
    return *reinterpret_cast< UList<T>* >(0);
}


58
59
60
template<class T>
inline Foam::label Foam::UList<T>::fcIndex(const label i) const
{
Mark Olesen's avatar
Mark Olesen committed
61
    return (i == size()-1 ? 0 : i+1);
62
63
64
65
66
67
}


template<class T>
inline Foam::label Foam::UList<T>::rcIndex(const label i) const
{
Mark Olesen's avatar
Mark Olesen committed
68
    return (i ? i-1 : size()-1);
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
}


// Check start is within valid range (0 ... size-1).
template<class T>
inline void Foam::UList<T>::checkStart(const label start) const
{
    if (start<0 || (start && start>=size_))
    {
        FatalErrorIn("UList<T>::checkStart(const label)")
            << "start " << start << " out of range 0 ... " << max(size_-1, 0)
            << abort(FatalError);
    }
}


// Check size is within valid range (0 ... size).
template<class T>
inline void Foam::UList<T>::checkSize(const label size) const
{
    if (size<0 || size>size_)
    {
        FatalErrorIn("UList<T>::checkSize(const label)")
            << "size " << size << " out of range 0 ... " << size_
            << abort(FatalError);
    }
}


// Check index i is within valid range (0 ... size-1).
template<class T>
inline void Foam::UList<T>::checkIndex(const label i) const
{
    if (!size_)
    {
        FatalErrorIn("UList<T>::checkIndex(const label)")
            << "attempt to access element from zero sized list"
            << abort(FatalError);
    }
    else if (i<0 || i>=size_)
    {
        FatalErrorIn("UList<T>::checkIndex(const label)")
            << "index " << i << " out of range 0 ... " << size_-1
            << abort(FatalError);
    }
}


mattijs's avatar
mattijs committed
117
118
119
120
121
122
123
124
125
126
127
128
129
130
template<class T>
inline T& Foam::UList<T>::last()
{
    return this->operator[](this->size()-1);
}


template<class T>
inline const T& Foam::UList<T>::last() const
{
    return this->operator[](this->size()-1);
}


Mark Olesen's avatar
Mark Olesen committed
131
132
133
134
135
136
137
138
139
140
141
142
143
144
template<class T>
inline const T* Foam::UList<T>::cdata() const
{
    return v_;
}


template<class T>
inline T* Foam::UList<T>::data()
{
    return v_;
}


145
146
// * * * * * * * * * * * * * * * Member Operators  * * * * * * * * * * * * * //

Mark Olesen's avatar
Mark Olesen committed
147
148

// element access
149
150
151
152
153
154
155
156
157
158
template<class T>
inline T& Foam::UList<T>::operator[](const label i)
{
#   ifdef FULLDEBUG
    checkIndex(i);
#   endif
    return v_[i];
}


Mark Olesen's avatar
Mark Olesen committed
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
namespace Foam
{

    // Template specialization for bool
    template<>
    inline const bool& Foam::UList<bool>::operator[](const label i) const
    {
        // lazy evaluation - return false for out-of-range
        if (i < size_)
        {
            return v_[i];
        }
        else
        {
            return Foam::pTraits<bool>::zero;
        }
    }

} // end of namespace Foam


// const element access
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
template<class T>
inline const T& Foam::UList<T>::operator[](const label i) const
{
#   ifdef FULLDEBUG
    checkIndex(i);
#   endif
    return v_[i];
}


// Allow cast to a const List<T>&
template<class T>
inline Foam::UList<T>::operator const Foam::List<T>&() const
{
    return *reinterpret_cast<const List<T>*>(this);
}


// * * * * * * * * * * * * * * STL Member Functions  * * * * * * * * * * * * //

template<class T>
inline typename Foam::UList<T>::iterator
Foam::UList<T>::begin()
{
    return v_;
}

template<class T>
inline typename Foam::UList<T>::const_iterator
Foam::UList<T>::begin() const
{
    return v_;
}

215
216
217
218
219
220
221
template<class T>
inline typename Foam::UList<T>::const_iterator
Foam::UList<T>::cbegin() const
{
    return v_;
}

222
223
224
225
226
227
228
229
230
231
232
233
234
235
template<class T>
inline typename Foam::UList<T>::iterator
Foam::UList<T>::end()
{
    return &v_[size_];
}

template<class T>
inline typename Foam::UList<T>::const_iterator
Foam::UList<T>::end() const
{
    return &v_[size_];
}

236
237
238
239
240
241
242
template<class T>
inline typename Foam::UList<T>::const_iterator
Foam::UList<T>::cend() const
{
    return &v_[size_];
}

243
244
245
246
247
248
249
250
251
252
253
254
255
256
template<class T>
inline typename Foam::UList<T>::iterator
Foam::UList<T>::rbegin()
{
    return &v_[size_-1];
}

template<class T>
inline typename Foam::UList<T>::const_iterator
Foam::UList<T>::rbegin() const
{
    return &v_[size_-1];
}

257
258
259
260
261
262
263
template<class T>
inline typename Foam::UList<T>::const_iterator
Foam::UList<T>::crbegin() const
{
    return &v_[size_-1];
}

264
265
266
267
268
269
270
271
272
273
274
275
276
277
template<class T>
inline typename Foam::UList<T>::iterator
Foam::UList<T>::rend()
{
    return &v_[-1];
}

template<class T>
inline typename Foam::UList<T>::const_iterator
Foam::UList<T>::rend() const
{
    return &v_[-1];
}

278
279
280
281
282
283
284
template<class T>
inline typename Foam::UList<T>::const_iterator
Foam::UList<T>::crend() const
{
    return &v_[-1];
}

285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
template<class T>
inline Foam::label Foam::UList<T>::size() const
{
    return size_;
}


template<class T>
inline Foam::label Foam::UList<T>::max_size() const
{
    return labelMax;
}


template<class T>
inline bool Foam::UList<T>::empty() const
{
Mark Olesen's avatar
Mark Olesen committed
302
    return !size_;
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
}


// * * * * * * * * * * * * * * * Global Functions  * * * * * * * * * * * * * //

template<class T>
inline void Foam::reverse(UList<T>& ul, const label n)
{
    for (int i=0; i<n/2; i++)
    {
        Swap(ul[i], ul[n-1-i]);
    }
}

template<class T>
inline void Foam::reverse(UList<T>& ul)
{
    reverse(ul, ul.size());
}


// ************************************************************************* //