OpenFOAM logo
The Open Source CFD Toolbox
  Programmer’s C++ Doxygen 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-2007 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 //- Construct null
00030 template<class T, unsigned SizeInc, unsigned SizeMult, unsigned SizeDiv>
00031 inline Foam::DynamicList<T, SizeInc, SizeMult, SizeDiv>::DynamicList()
00032 :
00033     List<T>(SizeInc),
00034     nextFree_(0)
00035 {}
00036 
00037 
00038 //- Construct given size
00039 template<class T, unsigned SizeInc, unsigned SizeMult, unsigned SizeDiv>
00040 inline Foam::DynamicList<T, SizeInc, SizeMult, SizeDiv>::DynamicList
00041 (
00042     const label s
00043 )
00044 :
00045     List<T>(s),
00046     nextFree_(0)
00047 {}
00048 
00049 
00050 //- Construct given size
00051 template<class T, unsigned SizeInc, unsigned SizeMult, unsigned SizeDiv>
00052 inline Foam::DynamicList<T, SizeInc, SizeMult, SizeDiv>::DynamicList
00053 (
00054     const UList<T>& s
00055 )
00056 :
00057     List<T>(s),
00058     nextFree_(s.size())
00059 {}
00060 
00061 
00062 // * * * * * * * * * * * * * * * Member Functions  * * * * * * * * * * * * * //
00063 
00064 template<class T, unsigned SizeInc, unsigned SizeMult, unsigned SizeDiv>
00065 inline Foam::label Foam::DynamicList<T, SizeInc, SizeMult, SizeDiv>::size()
00066 const
00067 {
00068     return nextFree_;
00069 }
00070 
00071 
00072 template<class T, unsigned SizeInc, unsigned SizeMult, unsigned SizeDiv>
00073 inline void Foam::DynamicList<T, SizeInc, SizeMult, SizeDiv>::setSize
00074 (
00075     const label s
00076 )
00077 {
00078     if (s < nextFree_)
00079     {
00080         nextFree_ = s;
00081     }
00082     else
00083     {
00084         List<T>::setSize(s);
00085     }
00086 }
00087 
00088 
00089 template<class T, unsigned SizeInc, unsigned SizeMult, unsigned SizeDiv>
00090 inline void Foam::DynamicList<T, SizeInc, SizeMult, SizeDiv>::setSize
00091 (
00092     const label s,
00093     const T& t
00094 )
00095 {
00096     if (s < nextFree_)
00097     {
00098         nextFree_ = s;
00099     }
00100     else
00101     {
00102         List<T>::setSize(s, t);
00103         nextFree_ = s;
00104     }
00105 }
00106 
00107 
00108 template<class T, unsigned SizeInc, unsigned SizeMult, unsigned SizeDiv>
00109 inline void Foam::DynamicList<T, SizeInc, SizeMult, SizeDiv>::clear()
00110 {
00111     nextFree_ = 0;
00112 }
00113 
00114 
00115 template<class T, unsigned SizeInc, unsigned SizeMult, unsigned SizeDiv>
00116 inline Foam::DynamicList<T, SizeInc, SizeMult, SizeDiv>&
00117 Foam::DynamicList<T, SizeInc, SizeMult, SizeDiv>::shrink()
00118 {
00119     List<T>::setSize(nextFree_);
00120     return *this;
00121 }
00122 
00123 
00124 template<class T, unsigned SizeInc, unsigned SizeMult, unsigned SizeDiv>
00125 inline void
00126 Foam::DynamicList<T, SizeInc, SizeMult, SizeDiv>::transfer(List<T>& l)
00127 {
00128     if (l.size() < nextFree_)
00129     {
00130         FatalErrorIn
00131         (
00132             "void DynamicList<T, SizeInc, SizeMult"
00133             ", SizeDiv>::transfer(List<T>&)"
00134         )   << "Cannot replace the underlying storage of this DynamicList"
00135             << " of which " << nextFree_ << " elements are used" << nl
00136             << "with a List of size " << l.size() << abort(FatalError);
00137     }
00138     else
00139     {
00140         List<T>::transfer(l);   // take over storage
00141         l.clear();              // set nextFree of l to 0
00142     }
00143 }
00144 
00145 
00146 template<class T, unsigned SizeInc, unsigned SizeMult, unsigned SizeDiv>
00147 inline void
00148 Foam::DynamicList<T, SizeInc, SizeMult, SizeDiv>::transfer
00149 (
00150     DynamicList<T, SizeInc, SizeMult, SizeDiv>& l
00151 )
00152 {
00153     List<T>::transfer(l);       // take over storage
00154     nextFree_ = l.size();       // take over used size
00155     l.clear();                  // set nextFree of l to 0
00156 }
00157 
00158 
00159 template<class T, unsigned SizeInc, unsigned SizeMult, unsigned SizeDiv>
00160 inline void Foam::DynamicList<T, SizeInc, SizeMult, SizeDiv>::append
00161 (
00162     const T& e
00163 )
00164 {
00165     operator()(size()) = e;
00166 }
00167 
00168 
00169 template<class T, unsigned SizeInc, unsigned SizeMult, unsigned SizeDiv>
00170 inline T Foam::DynamicList<T, SizeInc, SizeMult, SizeDiv>::remove()
00171 {
00172     if (nextFree_ == 0)
00173     {
00174         FatalErrorIn
00175         (
00176             "void Foam::DynamicList<T, SizeInc, SizeMult, SizeDiv>::remove()"
00177         )   << "List is empty" << abort(FatalError);
00178     }
00179 
00180     return List<T>::operator[](--nextFree_);
00181 }
00182 
00183 
00184 // * * * * * * * * * * * * * * * Member Operators  * * * * * * * * * * * * * //
00185 
00186 template<class T, unsigned SizeInc, unsigned SizeMult, unsigned SizeDiv>
00187 inline T& Foam::DynamicList<T, SizeInc, SizeMult, SizeDiv>::operator()
00188 (
00189     const Foam::label i
00190 )
00191 {
00192     nextFree_ = max(nextFree_, i + 1);
00193 
00194     if (nextFree_ > List<T>::size())
00195     {
00196         List<T>::setSize
00197         (
00198             max
00199             (
00200                 nextFree_,
00201                 label(SizeMult*List<T>::size()/SizeDiv + SizeInc)
00202             )
00203         );
00204     }
00205 
00206     return this->operator[](i);
00207 }
00208 
00209 
00210 template<class T, unsigned SizeInc, unsigned SizeMult, unsigned SizeDiv>
00211 inline void Foam::DynamicList<T, SizeInc, SizeMult, SizeDiv>::operator=
00212 (
00213     const T& t
00214 )
00215 {
00216     List<T>::operator=(t);
00217     nextFree_ = List<T>::size();
00218 }
00219 
00220 
00221 template<class T, unsigned SizeInc, unsigned SizeMult, unsigned SizeDiv>
00222 inline void Foam::DynamicList<T, SizeInc, SizeMult, SizeDiv>::operator=
00223 (
00224     const List<T>& l
00225 )
00226 {
00227     List<T>::operator=(l);
00228     nextFree_ = l.size();
00229 }
00230 
00231 
00232 // ************************************************************************* //
Copyright © 2000-2007 OpenCFD Ltd