1 /*---------------------------------------------------------------------------*\
3 \\ / F ield | cfMesh: A library for mesh generation
5 \\ / A nd | Author: Franjo Juretic (franjo.juretic@c-fields.com)
6 \\/ M anipulation | Copyright (C) Creative Fields, Ltd.
7 -------------------------------------------------------------------------------
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
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_) )
35 "void Foam::LongList<T, label, Offset>::"
36 "checkIndex(const label i) const"
37 ) << "Index " << Foam::label(i) << " is not in range " << Foam::label(0)
38 << " and " << Foam::label(nextFree_) << abort(FatalError);
42 template<class T, Foam::label Offset>
43 void Foam::LongList<T, Offset>::initializeParameters()
45 unsigned int t = sizeof(T);
54 shift_ = Foam::max(10, Offset - it);
59 template<class T, Foam::label Offset>
60 inline void Foam::LongList<T, Offset>::allocateSize(const label s)
68 const label numblock1 = ((s-1)>>shift_) + 1;
69 const label blockSize = 1<<shift_;
71 if( numblock1 < numBlocks_ )
73 for(register label i=numblock1;i<numBlocks_;++i)
74 delete [] dataPtr_[i];
76 else if( numblock1 > numBlocks_ )
78 if( numblock1 >= numAllocatedBlocks_ )
82 numAllocatedBlocks_ += 64;
83 } while( numblock1 > numAllocatedBlocks_ );
85 T** dataptr1 = new T*[numAllocatedBlocks_];
86 for(register label i=0;i<numBlocks_;++i)
87 dataptr1[i] = dataPtr_[i];
94 for(register label i=numBlocks_;i<numblock1;++i)
95 dataPtr_[i] = new T[blockSize];
98 numBlocks_ = numblock1;
99 N_ = numBlocks_ * blockSize;
102 template<class T, Foam::label Offset>
103 void Foam::LongList<T, Offset>::clearOut()
105 for(register label i=0;i<numBlocks_;++i)
106 delete [] dataPtr_[i];
116 numAllocatedBlocks_ = 0;
120 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
123 template<class T, Foam::label Offset>
124 inline Foam::LongList<T, Offset>::LongList()
129 numAllocatedBlocks_(0),
134 initializeParameters();
137 //- Construct given size
138 template<class T, Foam::label Offset>
139 inline Foam::LongList<T, Offset>::LongList(const label s)
144 numAllocatedBlocks_(0),
149 initializeParameters();
154 //- Construct given size
155 template<class T, Foam::label Offset>
156 inline Foam::LongList<T, Offset>::LongList(const label s, const T& t)
161 numAllocatedBlocks_(0),
166 initializeParameters();
171 template<class T, Foam::label Offset>
172 inline Foam::LongList<T, Offset>::LongList(const LongList<T, Offset>& ol)
177 numAllocatedBlocks_(0),
185 template<class T, Foam::label Offset>
186 inline Foam::LongList<T, Offset>::~LongList()
191 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
193 template<class T, Foam::label Offset>
194 inline Foam::label Foam::LongList<T, Offset>::size() const
199 template<class T, Foam::label Offset>
200 inline Foam::label Foam::LongList<T, Offset>::byteSize() const
202 if( !contiguous<T>() )
204 FatalErrorIn("LongList<T, Offset>::byteSize()")
205 << "Cannot return the binary size of a list of "
206 "non-primitive elements"
207 << abort(FatalError);
210 return nextFree_*sizeof(T);
213 template<class T, Foam::label Offset>
214 inline void Foam::LongList<T, Offset>::setSize(const label i)
220 template<class T, Foam::label Offset>
221 inline void Foam::LongList<T, Offset>::clear()
227 template<class T, Foam::label Offset>
228 inline Foam::LongList<T, Offset>&
229 Foam::LongList<T, Offset>::shrink()
235 template<class T, Foam::label Offset>
236 inline void Foam::LongList<T, Offset>::transfer(LongList<T, Offset>& ol)
239 dataPtr_ = ol.dataPtr_;
241 nextFree_ = ol.nextFree_;
242 numBlocks_ = ol.numBlocks_;
243 numAllocatedBlocks_ = ol.numAllocatedBlocks_;
251 ol.numAllocatedBlocks_ = 0;
255 template<class T, Foam::label Offset>
256 inline void Foam::LongList<T, Offset>::append(const T& e)
258 if( nextFree_ >= N_ )
260 allocateSize(nextFree_+1);
263 operator[](nextFree_++) = e;
266 template<class T, Foam::label Offset>
267 inline void Foam::LongList<T, Offset>::appendIfNotIn(const T& e)
273 template<class T, Foam::label Offset>
274 inline bool Foam::LongList<T, Offset>::contains(const T& e) const
276 for(register label i=0;i<nextFree_;++i)
277 if( (*this)[i] == e )
283 template<class T, Foam::label Offset>
284 inline Foam::label Foam::LongList<T, Offset>::containsAtPosition
289 for(register label i=0;i<nextFree_;++i)
290 if( (*this)[i] == e )
296 template<class T, Foam::label Offset>
297 inline T Foam::LongList<T, Offset>::remove(const label i)
303 "void Foam::LongList<T, Offset>::remove()"
304 ) << "List is empty" << abort(FatalError);
307 T el = operator[](i);
308 operator[](i) = operator[](nextFree_-1);
313 template<class T, Foam::label Offset>
314 inline T Foam::LongList<T, Offset>::removeLastElement()
320 "void Foam::LongList<T, Offset>::remove()"
321 ) << "List is empty" << abort(FatalError);
324 T lastEl = operator[](nextFree_-1);
330 // * * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * //
332 template<class T, Foam::label Offset>
333 inline const T& Foam::LongList<T, Offset>::operator[](const label i) const
339 return dataPtr_[i>>shift_][i&mask_];
342 template<class T, Foam::label Offset>
343 inline T& Foam::LongList<T, Offset>::operator[](const label i)
349 return dataPtr_[i>>shift_][i&mask_];
352 template<class T, Foam::label Offset>
353 inline T& Foam::LongList<T, Offset>::operator()(const label i)
358 return operator[](i);
362 template<class T, Foam::label Offset>
363 inline T& Foam::LongList<T, Offset>::newElmt(const label i)
365 return operator()(i);
368 template<class T, Foam::label Offset>
369 inline void Foam::LongList<T, Offset>::operator=(const T& t)
371 for(register label i=0;i<nextFree_;++i)
375 template<class T, Foam::label Offset>
376 inline void Foam::LongList<T, Offset>::operator=(const LongList<T, Offset>& l)
379 for(register label i=0;i<l.nextFree_;++i)
380 operator[](i) = l[i];
384 // ************************************************************************* //