Forward compatibility: flex
[foam-extend-3.2.git] / src / mesh / cfMesh / utilities / containers / LongList / LongListI.H
blobd590a3006ac92dcd5ac6ae7ee9f0422b58d8a0c1
1 /*---------------------------------------------------------------------------*\
2   =========                 |
3   \\      /  F ield         | cfMesh: A library for mesh generation
4    \\    /   O peration     |
5     \\  /    A nd           | Author: Franjo Juretic (franjo.juretic@c-fields.com)
6      \\/     M anipulation  | Copyright (C) Creative Fields, Ltd.
7 -------------------------------------------------------------------------------
8 License
9     This file is part of cfMesh.
11     cfMesh 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 3 of the License, or (at your
14     option) any later version.
16     cfMesh 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 cfMesh.  If not, see <http://www.gnu.org/licenses/>.
24 \*---------------------------------------------------------------------------*/
26 #include "contiguous.H"
28 template<class T, Foam::label Offset>
29 void Foam::LongList<T, Offset>::checkIndex(const label i) const
31     if( (i < 0) || (i >= nextFree_) )
32     {
33         FatalErrorIn
34         (
35             "void Foam::LongList<T, label>::"
36             "checkIndex(const label i) const"
37         ) << "Index " << i << " is not in range " << 0
38             << " and " << nextFree_ << abort(FatalError);
39     }
42 template<class T, Foam::label Offset>
43 void Foam::LongList<T, Offset>::initializeParameters()
45     unsigned int t = sizeof(T);
46     label it(0);
48     while( t > 1 )
49     {
50         t >>= 1;
51         ++it;
52     }
54     shift_ = Foam::max(10, Offset - it);
55     mask_ = 1<<shift_;
56     mask_ -= 1;
59 template<class T, Foam::label Offset>
60 inline void Foam::LongList<T, Offset>::allocateSize(const label s)
62     if( s == 0 )
63     {
64         clearOut();
65         return;
66     }
67     else if( s < 0 )
68     {
69         FatalErrorIn
70         (
71             "template<class T, Foam::label Offset>\n"
72             "inline void Foam::LongList<T, Offset>::allocateSize(const label)"
73         ) << "Negative size requested." << abort(FatalError);
74     }
76     const label numblock1 = ((s-1)>>shift_) + 1;
77     const label blockSize = 1<<shift_;
79     if( numblock1 < numBlocks_ )
80     {
81         for(register label i=numblock1;i<numBlocks_;++i)
82             delete [] dataPtr_[i];
83     }
84     else if( numblock1 > numBlocks_ )
85     {
86         if( numblock1 >= numAllocatedBlocks_ )
87         {
88             do
89             {
90                 numAllocatedBlocks_ += 64;
91             } while( numblock1 > numAllocatedBlocks_ );
93             T** dataptr1 = new T*[numAllocatedBlocks_];
94             for(register label i=0;i<numBlocks_;++i)
95                 dataptr1[i] = dataPtr_[i];
97             if( dataPtr_ )
98                 delete [] dataPtr_;
99             dataPtr_ = dataptr1;
100         }
102         for(register label i=numBlocks_;i<numblock1;++i)
103             dataPtr_[i] = new T[blockSize];
104     }
106     numBlocks_ = numblock1;
107     N_ = numBlocks_ * blockSize;
110 template<class T, Foam::label Offset>
111 void Foam::LongList<T, Offset>::clearOut()
113     for(register label i=0;i<numBlocks_;++i)
114         delete [] dataPtr_[i];
116     if( dataPtr_ )
117     {
118         delete [] dataPtr_;
119         dataPtr_ = NULL;
120     }
122     N_ = 0;
123     numBlocks_ = 0;
124     numAllocatedBlocks_ = 0;
125     nextFree_ = 0;
128 // * * * * * * * * * * * * * * * * Constructors  * * * * * * * * * * * * * * //
130 //- Construct null
131 template<class T, Foam::label Offset>
132 inline Foam::LongList<T, Offset>::LongList()
134     N_(0),
135     nextFree_(0),
136     numBlocks_(0),
137     numAllocatedBlocks_(0),
138     shift_(),
139     mask_(),
140     dataPtr_(NULL)
142     initializeParameters();
145 //- Construct given size
146 template<class T, Foam::label Offset>
147 inline Foam::LongList<T, Offset>::LongList(const label s)
149     N_(0),
150     nextFree_(0),
151     numBlocks_(0),
152     numAllocatedBlocks_(0),
153     shift_(),
154     mask_(),
155     dataPtr_(NULL)
157     initializeParameters();
158     setSize(s);
162 //- Construct given size
163 template<class T, Foam::label Offset>
164 inline Foam::LongList<T, Offset>::LongList(const label s, const T& t)
166     N_(0),
167     nextFree_(0),
168     numBlocks_(0),
169     numAllocatedBlocks_(0),
170     shift_(),
171     mask_(),
172     dataPtr_(NULL)
174     initializeParameters();
175     setSize(s);
176     *this = t;
179 template<class T, Foam::label Offset>
180 inline Foam::LongList<T, Offset>::LongList(const LongList<T, Offset>& ol)
182     N_(0),
183     nextFree_(0),
184     numBlocks_(0),
185     numAllocatedBlocks_(0),
186     shift_(ol.shift_),
187     mask_(ol.mask_),
188     dataPtr_(NULL)
190     *this = ol;
193 template<class T, Foam::label Offset>
194 inline Foam::LongList<T, Offset>::~LongList()
196     clearOut();
199 // * * * * * * * * * * * * * * * Member Functions  * * * * * * * * * * * * * //
201 template<class T, Foam::label Offset>
202 inline Foam::label Foam::LongList<T, Offset>::size() const
204     return nextFree_;
207 template<class T, Foam::label Offset>
208 inline Foam::label Foam::LongList<T, Offset>::byteSize() const
210     if( !contiguous<T>() )
211     {
212         FatalErrorIn("LongList<T, Offset>::byteSize()")
213             << "Cannot return the binary size of a list of "
214                "non-primitive elements"
215             << abort(FatalError);
216     }
218     return nextFree_*sizeof(T);
221 template<class T, Foam::label Offset>
222 inline void Foam::LongList<T, Offset>::setSize(const label i)
224     allocateSize(i);
225     nextFree_ = i;
228 template<class T, Foam::label Offset>
229 inline void Foam::LongList<T, Offset>::clear()
231     nextFree_ = 0;
235 template<class T, Foam::label Offset>
236 inline Foam::LongList<T, Offset>&
237 Foam::LongList<T, Offset>::shrink()
239     setSize(nextFree_);
240     return *this;
243 template<class T, Foam::label Offset>
244 inline void Foam::LongList<T, Offset>::transfer(LongList<T, Offset>& ol)
246     clearOut();
247     dataPtr_ = ol.dataPtr_;
248     N_ = ol.N_;
249     nextFree_ = ol.nextFree_;
250     numBlocks_ = ol.numBlocks_;
251     numAllocatedBlocks_ = ol.numAllocatedBlocks_;
252     shift_ = ol.shift_;
253     mask_ = ol.mask_;
255     ol.dataPtr_ = NULL;
256     ol.N_ = 0;
257     ol.nextFree_ = 0;
258     ol.numBlocks_ = 0;
259     ol.numAllocatedBlocks_ = 0;
263 template<class T, Foam::label Offset>
264 inline void Foam::LongList<T, Offset>::append(const T& e)
266     if( nextFree_ >= N_ )
267     {
268         allocateSize(nextFree_+1);
269     }
271     operator[](nextFree_++) = e;
274 template<class T, Foam::label Offset>
275 inline void Foam::LongList<T, Offset>::appendIfNotIn(const T& e)
277     if( !contains(e) )
278          append(e);
281 template<class T, Foam::label Offset>
282 inline bool Foam::LongList<T, Offset>::contains(const T& e) const
284     for(register label i=0;i<nextFree_;++i)
285         if( (*this)[i] == e )
286             return true;
288     return false;
291 template<class T, Foam::label Offset>
292 inline Foam::label Foam::LongList<T, Offset>::containsAtPosition
294     const T& e
295 ) const
297     for(register label i=0;i<nextFree_;++i)
298         if( (*this)[i] == e )
299             return i;
301     return -1;
304 template<class T, Foam::label Offset>
305 inline T Foam::LongList<T, Offset>::remove(const label i)
307     if( nextFree_ == 0 )
308     {
309         FatalErrorIn
310         (
311             "void Foam::LongList<T, Offset>::remove()"
312         )   << "List is empty" << abort(FatalError);
313     }
315     T el = operator[](i);
316     operator[](i) = operator[](nextFree_-1);
317     --nextFree_;
318     return el;
321 template<class T, Foam::label Offset>
322 inline T Foam::LongList<T, Offset>::removeLastElement()
324     if( nextFree_ == 0 )
325     {
326         FatalErrorIn
327         (
328             "void Foam::LongList<T, Offset>::remove()"
329         )   << "List is empty" << abort(FatalError);
330     }
332     T lastEl = operator[](nextFree_-1);
333     --nextFree_;
334     return lastEl;
338 // * * * * * * * * * * * * * * * Member Operators  * * * * * * * * * * * * * //
340 template<class T, Foam::label Offset>
341 inline const T& Foam::LongList<T, Offset>::operator[](const label i) const
343     #ifdef FULLDEBUG
344     checkIndex(i);
345     #endif
347     return dataPtr_[i>>shift_][i&mask_];
350 template<class T, Foam::label Offset>
351 inline T& Foam::LongList<T, Offset>::operator[](const label i)
353     #ifdef FULLDEBUG
354     checkIndex(i);
355     #endif
357     return dataPtr_[i>>shift_][i&mask_];
360 template<class T, Foam::label Offset>
361 inline T& Foam::LongList<T, Offset>::operator()(const label i)
363     if( i >= nextFree_ )
364         setSize(i+1);
366     return operator[](i);
370 template<class T, Foam::label Offset>
371 inline T& Foam::LongList<T, Offset>::newElmt(const label i)
373     return operator()(i);
376 template<class T, Foam::label Offset>
377 inline void Foam::LongList<T, Offset>::operator=(const T& t)
379     for(register label i=0;i<nextFree_;++i)
380         operator[](i) = t;
383 template<class T, Foam::label Offset>
384 inline void Foam::LongList<T, Offset>::operator=(const LongList<T, Offset>& l)
386     setSize(l.size());
387     for(register label i=0;i<l.nextFree_;++i)
388         operator[](i) = l[i];
392 // ************************************************************************* //