OpenFOAM logo
The Open Source CFD Toolbox
  Source Guide OpenCFD Solutions Contact OpenFOAM

DynamicListI.H

Go to the documentation of this file.
00001 /*---------------------------------------------------------------------------*\
00002   =========                 |
00003   \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
00004    \\    /   O peration     |
00005     \\  /    A nd           | Copyright (C) 1991-2009 OpenCFD Ltd.
00006      \\/     M anipulation  |
00007 -------------------------------------------------------------------------------
00008 License
00009     This file is part of OpenFOAM.
00010 
00011     OpenFOAM is free software; you can redistribute it and/or modify it
00012     under the terms of the GNU General Public License as published by the
00013     Free Software Foundation; either version 2 of the License, or (at your
00014     option) any later version.
00015 
00016     OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
00017     ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
00018     FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
00019     for more details.
00020 
00021     You should have received a copy of the GNU General Public License
00022     along with OpenFOAM; if not, write to the Free Software Foundation,
00023     Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
00024 
00025 \*---------------------------------------------------------------------------*/
00026 
00027 // * * * * * * * * * * * * * * * * Constructors  * * * * * * * * * * * * * * //
00028 
00029 template<class T, unsigned SizeInc, unsigned SizeMult, unsigned SizeDiv>
00030 inline Foam::DynamicList<T, SizeInc, SizeMult, SizeDiv>::DynamicList()
00031 :
00032     List<T>(SizeInc),
00033     capacity_(SizeInc)
00034 {
00035     List<T>::size(0);
00036 }
00037 
00038 
00039 template<class T, unsigned SizeInc, unsigned SizeMult, unsigned SizeDiv>
00040 inline Foam::DynamicList<T, SizeInc, SizeMult, SizeDiv>::DynamicList
00041 (
00042     const label nElem
00043 )
00044 :
00045     List<T>(nElem),
00046     capacity_(nElem)
00047 {
00048     // we could also enforce SizeInc granularity when (!SizeMult || !SizeDiv)
00049     List<T>::size(0);
00050 }
00051 
00052 
00053 template<class T, unsigned SizeInc, unsigned SizeMult, unsigned SizeDiv>
00054 inline Foam::DynamicList<T, SizeInc, SizeMult, SizeDiv>::DynamicList
00055 (
00056     const DynamicList<T, SizeInc, SizeMult, SizeDiv>& lst
00057 )
00058 :
00059     List<T>(lst),
00060     capacity_(lst.size())
00061 {}
00062 
00063 
00064 template<class T, unsigned SizeInc, unsigned SizeMult, unsigned SizeDiv>
00065 inline Foam::DynamicList<T, SizeInc, SizeMult, SizeDiv>::DynamicList
00066 (
00067     const UList<T>& lst
00068 )
00069 :
00070     List<T>(lst),
00071     capacity_(lst.size())
00072 {}
00073 
00074 
00075 template<class T, unsigned SizeInc, unsigned SizeMult, unsigned SizeDiv>
00076 inline Foam::DynamicList<T, SizeInc, SizeMult, SizeDiv>::DynamicList
00077 (
00078     const UIndirectList<T>& lst
00079 )
00080 :
00081     List<T>(lst),
00082     capacity_(lst.size())
00083 {}
00084 
00085 
00086 template<class T, unsigned SizeInc, unsigned SizeMult, unsigned SizeDiv>
00087 inline Foam::DynamicList<T, SizeInc, SizeMult, SizeDiv>::DynamicList
00088 (
00089     const Xfer<List<T> >& lst
00090 )
00091 :
00092     List<T>(lst),
00093     capacity_(List<T>::size())
00094 {}
00095 
00096 
00097 
00098 // * * * * * * * * * * * * * * * Member Functions  * * * * * * * * * * * * * //
00099 
00100 template<class T, unsigned SizeInc, unsigned SizeMult, unsigned SizeDiv>
00101 inline Foam::label Foam::DynamicList<T, SizeInc, SizeMult, SizeDiv>::capacity()
00102 const
00103 {
00104     return capacity_;
00105 }
00106 
00107 
00108 template<class T, unsigned SizeInc, unsigned SizeMult, unsigned SizeDiv>
00109 inline void Foam::DynamicList<T, SizeInc, SizeMult, SizeDiv>::setCapacity
00110 (
00111     const label nElem
00112 )
00113 {
00114     label nextFree = List<T>::size();
00115     capacity_ = nElem;
00116 
00117     if (nextFree > capacity_)
00118     {
00119         // truncate addressed sizes too
00120         nextFree = capacity_;
00121     }
00122     // we could also enforce SizeInc granularity when (!SizeMult || !SizeDiv)
00123 
00124     List<T>::setSize(capacity_);
00125     List<T>::size(nextFree);
00126 }
00127 
00128 
00129 template<class T, unsigned SizeInc, unsigned SizeMult, unsigned SizeDiv>
00130 inline void Foam::DynamicList<T, SizeInc, SizeMult, SizeDiv>::reserve
00131 (
00132     const label nElem
00133 )
00134 {
00135     // allocate more capacity?
00136     if (nElem > capacity_)
00137     {
00138 // TODO: convince the compiler that division by zero does not occur
00139 //        if (SizeInc && (!SizeMult || !SizeDiv))
00140 //        {
00141 //            // resize with SizeInc as the granularity
00142 //            capacity_ = nElem;
00143 //            unsigned pad = SizeInc - (capacity_ % SizeInc);
00144 //            if (pad != SizeInc)
00145 //            {
00146 //                capacity_ += pad;
00147 //            }
00148 //        }
00149 //        else
00150         {
00151             capacity_ = max
00152             (
00153                 nElem,
00154                 label(SizeInc + capacity_ * SizeMult / SizeDiv)
00155             );
00156         }
00157 
00158         // adjust allocated size, leave addressed size untouched
00159         label nextFree = List<T>::size();
00160         List<T>::setSize(capacity_);
00161         List<T>::size(nextFree);
00162     }
00163 }
00164 
00165 
00166 template<class T, unsigned SizeInc, unsigned SizeMult, unsigned SizeDiv>
00167 inline void Foam::DynamicList<T, SizeInc, SizeMult, SizeDiv>::setSize
00168 (
00169     const label nElem
00170 )
00171 {
00172     // allocate more capacity?
00173     if (nElem > capacity_)
00174     {
00175 // TODO: convince the compiler that division by zero does not occur
00176 //        if (SizeInc && (!SizeMult || !SizeDiv))
00177 //        {
00178 //            // resize with SizeInc as the granularity
00179 //            capacity_ = nElem;
00180 //            unsigned pad = SizeInc - (capacity_ % SizeInc);
00181 //            if (pad != SizeInc)
00182 //            {
00183 //                capacity_ += pad;
00184 //            }
00185 //        }
00186 //        else
00187         {
00188             capacity_ = max
00189             (
00190                 nElem,
00191                 label(SizeInc + capacity_ * SizeMult / SizeDiv)
00192             );
00193         }
00194 
00195         List<T>::setSize(capacity_);
00196     }
00197 
00198     // adjust addressed size
00199     List<T>::size(nElem);
00200 }
00201 
00202 
00203 template<class T, unsigned SizeInc, unsigned SizeMult, unsigned SizeDiv>
00204 inline void Foam::DynamicList<T, SizeInc, SizeMult, SizeDiv>::setSize
00205 (
00206     const label nElem,
00207     const T& t
00208 )
00209 {
00210     label nextFree = List<T>::size();
00211     setSize(nElem);
00212 
00213     // set new elements to constant value
00214     while (nextFree < nElem)
00215     {
00216         this->operator[](nextFree++) = t;
00217     }
00218 }
00219 
00220 
00221 template<class T, unsigned SizeInc, unsigned SizeMult, unsigned SizeDiv>
00222 inline void Foam::DynamicList<T, SizeInc, SizeMult, SizeDiv>::resize
00223 (
00224     const label nElem
00225 )
00226 {
00227     this->setSize(nElem);
00228 }
00229 
00230 
00231 template<class T, unsigned SizeInc, unsigned SizeMult, unsigned SizeDiv>
00232 inline void Foam::DynamicList<T, SizeInc, SizeMult, SizeDiv>::resize
00233 (
00234     const label nElem,
00235     const T& t
00236 )
00237 {
00238     this->setSize(nElem, t);
00239 }
00240 
00241 
00242 template<class T, unsigned SizeInc, unsigned SizeMult, unsigned SizeDiv>
00243 inline void Foam::DynamicList<T, SizeInc, SizeMult, SizeDiv>::clear()
00244 {
00245     List<T>::size(0);
00246 }
00247 
00248 
00249 template<class T, unsigned SizeInc, unsigned SizeMult, unsigned SizeDiv>
00250 inline void Foam::DynamicList<T, SizeInc, SizeMult, SizeDiv>::clearStorage()
00251 {
00252     List<T>::clear();
00253     capacity_ = 0;
00254 }
00255 
00256 
00257 template<class T, unsigned SizeInc, unsigned SizeMult, unsigned SizeDiv>
00258 inline Foam::DynamicList<T, SizeInc, SizeMult, SizeDiv>&
00259 Foam::DynamicList<T, SizeInc, SizeMult, SizeDiv>::shrink()
00260 {
00261     label nextFree = List<T>::size();
00262     if (capacity_ > nextFree)
00263     {
00264         // use the full list when resizing
00265         List<T>::size(capacity_);
00266 
00267         // the new size
00268         capacity_ = nextFree;
00269         List<T>::setSize(capacity_);
00270         List<T>::size(nextFree);
00271     }
00272     return *this;
00273 }
00274 
00275 
00276 template<class T, unsigned SizeInc, unsigned SizeMult, unsigned SizeDiv>
00277 inline void
00278 Foam::DynamicList<T, SizeInc, SizeMult, SizeDiv>::transfer(List<T>& lst)
00279 {
00280     capacity_ = lst.size();
00281     List<T>::transfer(lst);   // take over storage, clear addressing for lst.
00282 }
00283 
00284 
00285 template<class T, unsigned SizeInc, unsigned SizeMult, unsigned SizeDiv>
00286 inline void
00287 Foam::DynamicList<T, SizeInc, SizeMult, SizeDiv>::transfer
00288 (
00289     DynamicList<T, SizeInc, SizeMult, SizeDiv>& lst
00290 )
00291 {
00292     // take over storage as-is (without shrink), clear addressing for lst.
00293     capacity_ = lst.capacity_;
00294     lst.capacity_ = 0;
00295 
00296     List<T>::transfer(static_cast<List<T>&>(lst));
00297 }
00298 
00299 
00300 template<class T, unsigned SizeInc, unsigned SizeMult, unsigned SizeDiv>
00301 inline Foam::Xfer< Foam::List<T> >
00302 Foam::DynamicList<T, SizeInc, SizeMult, SizeDiv>::xfer()
00303 {
00304     return xferMoveTo< List<T> >(*this);
00305 }
00306 
00307 
00308 template<class T, unsigned SizeInc, unsigned SizeMult, unsigned SizeDiv>
00309 inline void Foam::DynamicList<T, SizeInc, SizeMult, SizeDiv>::append
00310 (
00311     const T& t
00312 )
00313 {
00314     label elemI = List<T>::size();
00315     setSize(elemI + 1);
00316 
00317     this->operator[](elemI) = t;
00318 }
00319 
00320 
00321 template<class T, unsigned SizeInc, unsigned SizeMult, unsigned SizeDiv>
00322 inline void Foam::DynamicList<T, SizeInc, SizeMult, SizeDiv>::append
00323 (
00324     const UList<T>& lst
00325 )
00326 {
00327     if (this == &lst)
00328     {
00329         FatalErrorIn
00330         (
00331             "DynamicList<T, SizeInc, SizeMult, SizeDiv>::append"
00332             "(const UList<T>&)"
00333         )   << "attempted appending to self" << abort(FatalError);
00334     }
00335 
00336     label nextFree = List<T>::size();
00337     setSize(nextFree + lst.size());
00338 
00339     forAll(lst, elemI)
00340     {
00341         this->operator[](nextFree++) = lst[elemI];
00342     }
00343 }
00344 
00345 
00346 template<class T, unsigned SizeInc, unsigned SizeMult, unsigned SizeDiv>
00347 inline void Foam::DynamicList<T, SizeInc, SizeMult, SizeDiv>::append
00348 (
00349     const UIndirectList<T>& lst
00350 )
00351 {
00352     label nextFree = List<T>::size();
00353     setSize(nextFree + lst.size());
00354 
00355     forAll(lst, elemI)
00356     {
00357         this->operator[](nextFree++) = lst[elemI];
00358     }
00359 }
00360 
00361 
00362 template<class T, unsigned SizeInc, unsigned SizeMult, unsigned SizeDiv>
00363 inline T Foam::DynamicList<T, SizeInc, SizeMult, SizeDiv>::remove()
00364 {
00365     label elemI = List<T>::size() - 1;
00366 
00367     if (elemI < 0)
00368     {
00369         FatalErrorIn
00370         (
00371             "Foam::DynamicList<T, SizeInc, SizeMult, SizeDiv>::remove()"
00372         )   << "List is empty" << abort(FatalError);
00373     }
00374 
00375     const T& val = List<T>::operator[](elemI);
00376 
00377     List<T>::size(elemI);
00378 
00379     return val;
00380 }
00381 
00382 
00383 // * * * * * * * * * * * * * * * Member Operators  * * * * * * * * * * * * * //
00384 
00385 template<class T, unsigned SizeInc, unsigned SizeMult, unsigned SizeDiv>
00386 inline T& Foam::DynamicList<T, SizeInc, SizeMult, SizeDiv>::operator()
00387 (
00388     const label elemI
00389 )
00390 {
00391     if (elemI >= List<T>::size())
00392     {
00393         setSize(elemI + 1);
00394     }
00395 
00396     return this->operator[](elemI);
00397 }
00398 
00399 
00400 template<class T, unsigned SizeInc, unsigned SizeMult, unsigned SizeDiv>
00401 inline void Foam::DynamicList<T, SizeInc, SizeMult, SizeDiv>::operator=
00402 (
00403     const T& t
00404 )
00405 {
00406     UList<T>::operator=(t);
00407 }
00408 
00409 
00410 template<class T, unsigned SizeInc, unsigned SizeMult, unsigned SizeDiv>
00411 inline void Foam::DynamicList<T, SizeInc, SizeMult, SizeDiv>::operator=
00412 (
00413     const UList<T>& lst
00414 )
00415 {
00416     if (capacity_ >= lst.size())
00417     {
00418         // can copy w/o reallocating, match initial size to avoid reallocation
00419         List<T>::size(lst.size());
00420         List<T>::operator=(lst);
00421     }
00422     else
00423     {
00424         // make everything available for the copy operation
00425         List<T>::size(capacity_);
00426 
00427         List<T>::operator=(lst);
00428         capacity_ = List<T>::size();
00429     }
00430 }
00431 
00432 
00433 template<class T, unsigned SizeInc, unsigned SizeMult, unsigned SizeDiv>
00434 inline void Foam::DynamicList<T, SizeInc, SizeMult, SizeDiv>::operator=
00435 (
00436     const DynamicList<T, SizeInc, SizeMult, SizeDiv>& lst
00437 )
00438 {
00439     if (this == &lst)
00440     {
00441         FatalErrorIn
00442         (
00443             "DynamicList<T, SizeInc, SizeMult, SizeDiv>::operator="
00444             "(const DynamicList<T, SizeInc, SizeMult, SizeDiv>&)"
00445         )   << "attempted assignment to self" << abort(FatalError);
00446     }
00447 
00448     if (capacity_ >= lst.size())
00449     {
00450         // can copy w/o reallocating, match initial size to avoid reallocation
00451         List<T>::size(lst.size());
00452         List<T>::operator=(lst);
00453     }
00454     else
00455     {
00456         // make everything available for the copy operation
00457         List<T>::size(capacity_);
00458 
00459         List<T>::operator=(lst);
00460         capacity_ = List<T>::size();
00461     }
00462 }
00463 
00464 
00465 // ************************************************************************* //
Copyright © 2000-2009 OpenCFD Ltd