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

UPtrList.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/error.H>
00027 
00028 #include "UPtrList.H"
00029 
00030 // * * * * * * * * * * * * * * * Constructors  * * * * * * * * * * * * * * * //
00031 
00032 template<class T>
00033 Foam::UPtrList<T>::UPtrList()
00034 :
00035     ptrs_()
00036 {}
00037 
00038 
00039 template<class T>
00040 Foam::UPtrList<T>::UPtrList(const label s)
00041 :
00042     ptrs_(s, reinterpret_cast<T*>(0))
00043 {}
00044 
00045 
00046 template<class T>
00047 Foam::UPtrList<T>::UPtrList(const Xfer<UPtrList<T> >& lst)
00048 {
00049     transfer(lst());
00050 }
00051 
00052 
00053 template<class T>
00054 Foam::UPtrList<T>::UPtrList(UPtrList<T>& a, bool reUse)
00055 :
00056     ptrs_(a.ptrs_, reUse)
00057 {}
00058 
00059 
00060 // * * * * * * * * * * * * * * * Member Functions  * * * * * * * * * * * * * //
00061 
00062 template<class T>
00063 void Foam::UPtrList<T>::setSize(const label newSize)
00064 {
00065     label oldSize = size();
00066 
00067     if (newSize <= 0)
00068     {
00069         clear();
00070     }
00071     else if (newSize < oldSize)
00072     {
00073         ptrs_.setSize(newSize);
00074     }
00075     else if (newSize > oldSize)
00076     {
00077         ptrs_.setSize(newSize);
00078 
00079         // set new elements to NULL
00080         for (register label i=oldSize; i<newSize; i++)
00081         {
00082             ptrs_[i] = NULL;
00083         }
00084     }
00085 }
00086 
00087 
00088 template<class T>
00089 void Foam::UPtrList<T>::clear()
00090 {
00091     ptrs_.clear();
00092 }
00093 
00094 
00095 // Transfer the contents of the argument List into this List
00096 // and anull the argument list
00097 template<class T>
00098 void Foam::UPtrList<T>::transfer(UPtrList<T>& a)
00099 {
00100     ptrs_.transfer(a.ptrs_);
00101 }
00102 
00103 
00104 template<class T>
00105 void Foam::UPtrList<T>::reorder(const UList<label>& oldToNew)
00106 {
00107     if (oldToNew.size() != size())
00108     {
00109         FatalErrorIn("UPtrList<T>::reorder(const UList<label>&)")
00110             << "Size of map (" << oldToNew.size()
00111             << ") not equal to list size (" << size()
00112             << ")." << abort(FatalError);
00113     }
00114 
00115     List<T*> newPtrs_(ptrs_.size(), reinterpret_cast<T*>(0));
00116 
00117     forAll(*this, i)
00118     {
00119         label newI = oldToNew[i];
00120 
00121         if (newI < 0 || newI >= size())
00122         {
00123             FatalErrorIn("UPtrList<T>::reorder(const UList<label>&)")
00124                 << "Illegal index " << newI << nl
00125                 << "Valid indices are 0.." << size()-1
00126                 << abort(FatalError);
00127         }
00128 
00129         if (newPtrs_[newI])
00130         {
00131             FatalErrorIn("UPtrList<T>::reorder(const UList<label>&)")
00132                 << "reorder map is not unique; element " << newI
00133                 << " already set." << abort(FatalError);
00134         }
00135         newPtrs_[newI] = ptrs_[i];
00136     }
00137 
00138     forAll(newPtrs_, i)
00139     {
00140         if (!newPtrs_[i])
00141         {
00142             FatalErrorIn("UPtrList<T>::reorder(const UList<label>&)")
00143                 << "Element " << i << " not set after reordering." << nl
00144                 << abort(FatalError);
00145         }
00146     }
00147 
00148     ptrs_.transfer(newPtrs_);
00149 }
00150 
00151 
00152 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
00153 
00154 #include "UPtrListIO.C"
00155 
00156 // ************************ vim: set sw=4 sts=4 et: ************************ //
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines