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

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