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 \*---------------------------------------------------------------------------*/
27 template<class T, Foam::label staticSize>
28 inline void Foam::DynList<T, staticSize>::allocateSize(const label s)
30 if( s > UList<T>::size() )
32 T* newData = new T[s];
34 for(label i=0;i<nextFree_;++i)
35 newData[i] = this->operator[](i);
37 T* data = UList<T>::begin();
38 if( data && (data != staticData_) )
41 //UList<T>::reset(newData, s);
42 this->UList<T>::operator=(UList<T>(newData, s));
44 else if( (s > staticSize) && (s < UList<T>::size()) )
46 T* newData = new T[s];
48 for(label i=0;i<s;++i)
49 newData[i] = this->operator[](i);
51 T* data = UList<T>::begin();
54 //UList<T>::reset(newData, s);
55 this->UList<T>::operator=(UList<T>(newData, s));
57 else if( (s <= staticSize) && (UList<T>::size() > staticSize) )
59 for(label i=0;i<s;++i)
60 staticData_[i] = UList<T>::operator[](i);
62 T* data = UList<T>::begin();
63 if( data && (data != staticData_) )
66 //UList<T>::reset(staticData_, staticSize);
67 this->UList<T>::operator=(UList<T>(staticData_, staticSize));
71 template<class T, Foam::label staticSize>
72 inline void Foam::DynList<T, staticSize>::checkIndex(const label i) const
74 if( (i < 0) || (i >= nextFree_) )
78 "void Foam::DynList<T, label, Offset>::"
79 "checkIndex(const label i) const"
80 ) << "Index " << i << " is not in range " << 0
81 << " and " << nextFree_ << abort(FatalError);
85 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
88 template<class T, Foam::label staticSize>
89 inline Foam::DynList<T, staticSize>::DynList()
91 UList<T>(staticData_, staticSize),
96 template<class T, Foam::label staticSize>
97 inline Foam::DynList<T, staticSize>::DynList(const label s)
99 UList<T>(staticData_, staticSize),
105 template<class T, Foam::label staticSize>
106 inline Foam::DynList<T, staticSize>::DynList(const label s, const T& val)
108 UList<T>(staticData_, staticSize),
113 for(label i=0;i<s;++i)
114 this->operator[](i) = val;
117 template<class T, Foam::label staticSize>
118 inline Foam::DynList<T, staticSize>::DynList(const UList<T>& ul)
120 UList<T>(staticData_, staticSize),
126 this->operator[](i) = ul[i];
129 template<class T, Foam::label staticSize>
130 template<class ListType>
131 inline Foam::DynList<T, staticSize>::DynList(const ListType& l)
133 UList<T>(staticData_, staticSize),
137 for(label i=0;i<nextFree_;++i)
138 this->operator[](i) = l[i];
142 template<class T, Foam::label staticSize>
143 inline Foam::DynList<T, staticSize>::DynList
145 const DynList<T, staticSize>& dl
148 UList<T>(staticData_, staticSize),
152 for(label i=0;i<nextFree_;++i)
153 this->operator[](i) = dl[i];
156 template<class T, Foam::label staticSize>
157 inline Foam::DynList<T, staticSize>::~DynList()
160 //UList<T>::reset(NULL, 0);
164 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
166 template<class T, Foam::label staticSize>
167 inline Foam::label Foam::DynList<T, staticSize>::size() const
172 template<class T, Foam::label staticSize>
173 inline Foam::label Foam::DynList<T, staticSize>::byteSize() const
175 if( !contiguous<T>() )
177 FatalErrorIn("DynList<T>::byteSize()")
178 << "Cannot return the binary size of a list of "
179 "non-primitive elements"
180 << abort(FatalError);
183 return nextFree_*sizeof(T);
187 template<class T, Foam::label staticSize>
188 inline void Foam::DynList<T, staticSize>::setSize(const label s)
195 template<class T, Foam::label staticSize>
196 inline void Foam::DynList<T, staticSize>::clear()
202 template<class T, Foam::label staticSize>
203 void Foam::DynList<T, staticSize>::shrink()
205 allocateSize(nextFree_);
208 template<class T, Foam::label staticSize>
209 inline void Foam::DynList<T, staticSize>::append(const T& e)
211 if( nextFree_ >= UList<T>::size() )
213 const label newSize = 2*UList<T>::size()+2;
214 allocateSize(newSize);
217 UList<T>::operator[](nextFree_++) = e;
220 template<class T, Foam::label staticSize>
221 inline void Foam::DynList<T, staticSize>::appendIfNotIn(const T& e)
227 template<class T, Foam::label staticSize>
228 inline bool Foam::DynList<T, staticSize>::contains(const T& e) const
230 for(label i=0;i<nextFree_;++i)
232 if( UList<T>::operator[](i) == e )
239 template<class T, Foam::label staticSize>
240 inline Foam::label Foam::DynList<T, staticSize>::containsAtPosition
245 for(label i=0;i<nextFree_;++i)
247 if( UList<T>::operator[](i) == e )
254 template<class T, Foam::label staticSize>
255 inline const T& Foam::DynList<T, staticSize>::lastElement() const
257 return this->operator[](nextFree_-1);
260 template<class T, Foam::label staticSize>
261 inline T Foam::DynList<T, staticSize>::removeLastElement()
267 "void Foam::DynList<T, staticSize>::remove()"
268 ) << "List is empty" << abort(FatalError);
271 T el = UList<T>::operator[](--nextFree_);
275 template<class T, Foam::label staticSize>
276 inline T Foam::DynList<T, staticSize>::removeElement(const label i)
282 "void Foam::DynList<T, staticSize>::remove()"
283 ) << "List is empty" << abort(FatalError);
286 T el = this->operator[](i);
287 this->operator[](i) = this->operator[](nextFree_-1);
293 template<class T, Foam::label staticSize>
294 inline T& Foam::DynList<T, staticSize>::newElmt(const label i)
296 return this->operator()(i);
299 // * * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * //
301 template<class T, Foam::label staticSize>
302 inline T& Foam::DynList<T, staticSize>::operator()(const label i)
304 nextFree_ = Foam::max(nextFree_, i + 1);
306 if( nextFree_ >= UList<T>::size() )
308 allocateSize(2 * nextFree_+1);
311 return this->operator[](i);
314 template<class T, Foam::label staticSize>
315 inline const T& Foam::DynList<T, staticSize>::operator[](const label i) const
321 return UList<T>::operator[](i);
324 template<class T, Foam::label staticSize>
325 inline T& Foam::DynList<T, staticSize>::operator[](const label i)
331 return UList<T>::operator[](i);
334 template<class T, Foam::label staticSize>
335 inline Foam::label Foam::DynList<T, staticSize>::fcIndex
341 return (index + offset) % nextFree_;
344 template<class T, Foam::label staticSize>
345 inline Foam::label Foam::DynList<T, staticSize>::rcIndex
351 return (index + nextFree_ - offset) % nextFree_;
354 template<class T, Foam::label staticSize>
355 inline const T& Foam::DynList<T, staticSize>::fcElement
361 return operator[](fcIndex(index, offset));
364 template<class T, Foam::label staticSize>
365 inline const T& Foam::DynList<T, staticSize>::rcElement
371 return operator[](rcIndex(index, offset));
374 template<class T, Foam::label staticSize>
375 inline void Foam::DynList<T, staticSize>::operator=(const T& t)
377 UList<T>::operator=(t);
380 template<class T, Foam::label staticSize>
381 inline void Foam::DynList<T, staticSize>::operator=
383 const DynList<T, staticSize>& dl
386 allocateSize(dl.size());
387 nextFree_ = dl.size();
389 for(label i=0;i<nextFree_;++i)
390 this->operator[](i) = dl[i];
393 template<class T, Foam::label staticSize>
394 template<class ListType>
395 inline void Foam::DynList<T, staticSize>::operator=(const ListType& l)
397 allocateSize(l.size());
398 nextFree_ = l.size();
400 for(label i=0;i<nextFree_;++i)
401 this->operator[](i) = l[i];
405 // ************************************************************************* //