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

CompactListList.C

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 #include "CompactListList.H"
00028 
00029 // * * * * * * * * * * * * * * * Constructors  * * * * * * * * * * * * * * * //
00030 
00031 template<class T>
00032 Foam::CompactListList<T>::CompactListList(const List<List<T> >& ll)
00033 :
00034     offsets_(ll.size())
00035 {
00036     label sumSize = 0;
00037     forAll(ll, i)
00038     {
00039         sumSize += ll[i].size();
00040         offsets_[i] = sumSize;
00041     }
00042 
00043     m_.setSize(sumSize);
00044 
00045     label k = 0;
00046     forAll(ll, i)
00047     {
00048         const List<T>& lli = ll[i];
00049 
00050         forAll(lli, j)
00051         {
00052             m_[k++] = lli[j];
00053         }
00054     }
00055 }
00056 
00057 
00058 template<class T>
00059 Foam::CompactListList<T>::CompactListList
00060 (
00061     const UList<label>& rowSizes
00062 )
00063 :
00064     offsets_(rowSizes.size())
00065 {
00066     label sumSize = 0;
00067     forAll(rowSizes, i)
00068     {
00069         sumSize += rowSizes[i];
00070         offsets_[i] = sumSize;
00071     }
00072 
00073     m_.setSize(sumSize);
00074 }
00075 
00076 
00077 template<class T>
00078 Foam::CompactListList<T>::CompactListList
00079 (
00080     const UList<label>& rowSizes,
00081     const T& t
00082 )
00083 :
00084     offsets_(rowSizes.size())
00085 {
00086     label sumSize = 0;
00087     forAll(rowSizes, i)
00088     {
00089         sumSize += rowSizes[i];
00090         offsets_[i] = sumSize;
00091     }
00092 
00093     m_.setSize(sumSize, t);
00094 }
00095 
00096 
00097 template<class T>
00098 Foam::CompactListList<T>::CompactListList
00099 (
00100     const Xfer<CompactListList<T> >& lst
00101 )
00102 {
00103     transfer(lst());
00104 }
00105 
00106 
00107 template<class T>
00108 Foam::CompactListList<T>::CompactListList
00109 (
00110     CompactListList<T>& lst,
00111     bool reUse
00112 )
00113 :
00114     offsets_(lst.offsets_, reUse),
00115     m_(lst.m_, reUse)
00116 {}
00117 
00118 
00119 // * * * * * * * * * * * * * * * Member Functions  * * * * * * * * * * * * * //
00120 
00121 template<class T>
00122 void Foam::CompactListList<T>::setSize(const label nRows)
00123 {
00124     if (nRows == 0)
00125     {
00126         clear();
00127     }
00128     if (nRows < offsets_.size())
00129     {
00130         offsets_.setSize(nRows);
00131         m_.setSize(offsets_[nRows - 1]);
00132     }
00133     else if (nRows > offsets_.size())
00134     {
00135         FatalErrorIn("CompactListList<T>::setSize(const label nRows)")
00136             << "Cannot be used to extend the list from " << offsets_.size()
00137             << " to " << nRows << nl
00138             << "    Please use one of the other setSize member functions"
00139             << abort(FatalError);
00140     }
00141 }
00142 
00143 
00144 template<class T>
00145 void Foam::CompactListList<T>::setSize
00146 (
00147     const label nRows,
00148     const label nData
00149 )
00150 {
00151     offsets_.setSize(nRows);
00152     m_.setSize(nData);
00153 }
00154 
00155 
00156 template<class T>
00157 void Foam::CompactListList<T>::setSize
00158 (
00159     const label nRows,
00160     const label nData,
00161     const T& t
00162 )
00163 {
00164     offsets_.setSize(nRows);
00165     m_.setSize(nData, t);
00166 }
00167 
00168 
00169 template<class T>
00170 void Foam::CompactListList<T>::setSize(const UList<label>& rowSizes)
00171 {
00172     offsets_.setSize(rowSizes.size());
00173 
00174     label sumSize = 0;
00175     forAll(rowSizes, i)
00176     {
00177         sumSize += rowSizes[i];
00178         offsets_[i] = sumSize;
00179     }
00180 
00181     m_.setSize(sumSize);
00182 }
00183 
00184 
00185 template<class T>
00186 Foam::labelList Foam::CompactListList<T>::sizes() const
00187 {
00188     labelList rowSizes(offsets_.size());
00189 
00190     label prevOffset = 0;
00191     forAll(offsets_, i)
00192     {
00193         rowSizes[i] = offsets_[i]-prevOffset;
00194         prevOffset = offsets_[i];
00195     }
00196     return rowSizes;
00197 }
00198 
00199 
00200 template<class T>
00201 void Foam::CompactListList<T>::clear()
00202 {
00203     offsets_.clear();
00204     m_.clear();
00205 }
00206 
00207 
00208 template<class T>
00209 void Foam::CompactListList<T>::transfer(CompactListList<T>& a)
00210 {
00211     offsets_.transfer(a.offsets_);
00212     m_.transfer(a.m_);
00213 }
00214 
00215 
00216 // * * * * * * * * * * * * * * * Member Operators  * * * * * * * * * * * * * //
00217 
00218 template<class T>
00219 Foam::List<Foam::List<T> > Foam::CompactListList<T>::operator()() const
00220 {
00221     List<List<T> > ll(offsets_.size());
00222 
00223     label offsetPrev = 0;
00224     forAll(offsets_, i)
00225     {
00226         List<T>& lst = ll[i];
00227 
00228         lst.setSize(offsets_[i] - offsetPrev);
00229 
00230         forAll(lst, j)
00231         {
00232             lst[j] = m_[offsetPrev + j];
00233         }
00234 
00235         offsetPrev = offsets_[i];
00236     }
00237 
00238     return ll;
00239 }
00240 
00241 
00242 // * * * * * * * * * * * * * * * *  IOStream operators * * * * * * * * * * * //
00243 
00244 #include "CompactListListIO.C"
00245 
00246 // ************************************************************************* //
Copyright © 2000-2009 OpenCFD Ltd