FreeFOAM The Cross-Platform CFD Toolkit
Hosted by SourceForge:
Get FreeFOAM at SourceForge.net.
            Fast, secure and Free Open Source software downloads

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