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>::"
36 "checkIndex(const label i) const"
37 ) << "Index " << i << " is not in range " << 0
38 << " and " << 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)
71 "template<class T, Foam::label Offset>\n"
72 "inline void Foam::LongList<T, Offset>::allocateSize(const label)"
73 ) << "Negative size requested." << abort(FatalError);
76 const label numblock1 = ((s-1)>>shift_) + 1;
77 const label blockSize = 1<<shift_;
79 if( numblock1 < numBlocks_ )
81 for(register label i=numblock1;i<numBlocks_;++i)
82 delete [] dataPtr_[i];
84 else if( numblock1 > numBlocks_ )
86 if( numblock1 >= numAllocatedBlocks_ )
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];
102 for(register label i=numBlocks_;i<numblock1;++i)
103 dataPtr_[i] = new T[blockSize];
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];
124 numAllocatedBlocks_ = 0;
128 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
131 template<class T, Foam::label Offset>
132 inline Foam::LongList<T, Offset>::LongList()
137 numAllocatedBlocks_(0),
142 initializeParameters();
145 //- Construct given size
146 template<class T, Foam::label Offset>
147 inline Foam::LongList<T, Offset>::LongList(const label s)
152 numAllocatedBlocks_(0),
157 initializeParameters();
162 //- Construct given size
163 template<class T, Foam::label Offset>
164 inline Foam::LongList<T, Offset>::LongList(const label s, const T& t)
169 numAllocatedBlocks_(0),
174 initializeParameters();
179 template<class T, Foam::label Offset>
180 inline Foam::LongList<T, Offset>::LongList(const LongList<T, Offset>& ol)
185 numAllocatedBlocks_(0),
193 template<class T, Foam::label Offset>
194 inline Foam::LongList<T, Offset>::~LongList()
199 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
201 template<class T, Foam::label Offset>
202 inline Foam::label Foam::LongList<T, Offset>::size() const
207 template<class T, Foam::label Offset>
208 inline Foam::label Foam::LongList<T, Offset>::byteSize() const
210 if( !contiguous<T>() )
212 FatalErrorIn("LongList<T, Offset>::byteSize()")
213 << "Cannot return the binary size of a list of "
214 "non-primitive elements"
215 << abort(FatalError);
218 return nextFree_*sizeof(T);
221 template<class T, Foam::label Offset>
222 inline void Foam::LongList<T, Offset>::setSize(const label i)
228 template<class T, Foam::label Offset>
229 inline void Foam::LongList<T, Offset>::clear()
235 template<class T, Foam::label Offset>
236 inline Foam::LongList<T, Offset>&
237 Foam::LongList<T, Offset>::shrink()
243 template<class T, Foam::label Offset>
244 inline void Foam::LongList<T, Offset>::transfer(LongList<T, Offset>& ol)
247 dataPtr_ = ol.dataPtr_;
249 nextFree_ = ol.nextFree_;
250 numBlocks_ = ol.numBlocks_;
251 numAllocatedBlocks_ = ol.numAllocatedBlocks_;
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_ )
268 allocateSize(nextFree_+1);
271 operator[](nextFree_++) = e;
274 template<class T, Foam::label Offset>
275 inline void Foam::LongList<T, Offset>::appendIfNotIn(const T& 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 )
291 template<class T, Foam::label Offset>
292 inline Foam::label Foam::LongList<T, Offset>::containsAtPosition
297 for(register label i=0;i<nextFree_;++i)
298 if( (*this)[i] == e )
304 template<class T, Foam::label Offset>
305 inline T Foam::LongList<T, Offset>::remove(const label i)
311 "void Foam::LongList<T, Offset>::remove()"
312 ) << "List is empty" << abort(FatalError);
315 T el = operator[](i);
316 operator[](i) = operator[](nextFree_-1);
321 template<class T, Foam::label Offset>
322 inline T Foam::LongList<T, Offset>::removeLastElement()
328 "void Foam::LongList<T, Offset>::remove()"
329 ) << "List is empty" << abort(FatalError);
332 T lastEl = operator[](nextFree_-1);
338 // * * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * //
340 template<class T, Foam::label Offset>
341 inline const T& Foam::LongList<T, Offset>::operator[](const label i) const
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)
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)
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)
383 template<class T, Foam::label Offset>
384 inline void Foam::LongList<T, Offset>::operator=(const LongList<T, Offset>& l)
387 for(register label i=0;i<l.nextFree_;++i)
388 operator[](i) = l[i];
392 // ************************************************************************* //