initial commit for version 1.5.x patch release
[OpenFOAM-1.5.x.git] / src / OpenFOAM / containers / Lists / UPtrList / UPtrList.C
blob29089c4660163debb12d59b20a7046e413cdc012
1 /*---------------------------------------------------------------------------*\
2   =========                 |
3   \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
4    \\    /   O peration     |
5     \\  /    A nd           | Copyright (C) 1991-2008 OpenCFD Ltd.
6      \\/     M anipulation  |
7 -------------------------------------------------------------------------------
8 License
9     This file is part of OpenFOAM.
11     OpenFOAM is free software; you can redistribute it and/or modify it
12     under the terms of the GNU General Public License as published by the
13     Free Software Foundation; either version 2 of the License, or (at your
14     option) any later version.
16     OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
17     ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
18     FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
19     for more details.
21     You should have received a copy of the GNU General Public License
22     along with OpenFOAM; if not, write to the Free Software Foundation,
23     Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
25 \*---------------------------------------------------------------------------*/
27 #include "error.H"
29 #include "UPtrList.H"
30 #include "PtrListLoopM.H"
32 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
34 namespace Foam
37 // * * * * * * * * * * * * * * * Constructors  * * * * * * * * * * * * * * * //
39 template<class T>
40 UPtrList<T>::UPtrList()
42     ptrs_()
46 template<class T>
47 UPtrList<T>::UPtrList(const label s)
49     ptrs_(s, reinterpret_cast<T*>(NULL))
53 template<class T>
54 UPtrList<T>::UPtrList(UPtrList<T>& a, bool reUse)
56     ptrs_(a.ptrs_, reUse)
60 // * * * * * * * * * * * * * * * Member Functions  * * * * * * * * * * * * * //
62 template<class T>
63 void UPtrList<T>::setSize(const label newSize)
65     label oldSize = size();
67     if (newSize == 0)
68     {
69         clear();
70     }
71     else if (newSize < oldSize)
72     {
73         ptrs_.setSize(newSize);
74     }
75     else if (newSize > oldSize)
76     {
77         ptrs_.setSize(newSize);
79         register label i;
80         for (i=oldSize; i<newSize; i++)
81         {
82             ptrs_[i] = NULL;
83         }
84     }
88 template<class T>
89 void UPtrList<T>::clear()
91     ptrs_.clear();
95 // Transfer the contents of the argument List into this List
96 // and anull the argument list
97 template<class T>
98 void UPtrList<T>::transfer(UPtrList<T>& a)
100     ptrs_.transfer(a.ptrs_);
104 template<class T>
105 void UPtrList<T>::reorder(const UList<label>& oldToNew)
107     if (oldToNew.size() != size())
108     {
109         FatalErrorIn("UPtrList<T>::reorder(const UList<label>&)")
110             << "Size of map (" << oldToNew.size()
111             << ") not equal to list size (" << size()
112             << ")." << abort(FatalError);
113     }
115     List<T*> newPtrs_(ptrs_.size(), reinterpret_cast<T*>(NULL));
117     forAll(*this, i)
118     {
119         label newI = oldToNew[i];
121         if (newI < 0 || newI >= size())
122         {
123             FatalErrorIn("UPtrList<T>::reorder(const UList<label>&)")
124                 << "Illegal index " << newI << nl
125                 << "Valid indices are 0.." << size()-1
126                 << abort(FatalError);
127         }
129         if (newPtrs_[newI])
130         {
131             FatalErrorIn("UPtrList<T>::reorder(const UList<label>&)")
132                 << "reorder map is not unique; element " << newI
133                 << " already set." << abort(FatalError);
134         }
135         newPtrs_[newI] = ptrs_[i];
136     }
138     forAll(newPtrs_, i)
139     {
140         if (!newPtrs_[i])
141         {
142             FatalErrorIn("UPtrList<T>::reorder(const UList<label>&)")
143                 << "Element " << i << " not set after reordering." << nl
144                 << abort(FatalError);
145         }
146     }
148     ptrs_.transfer(newPtrs_);
152 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
154 } // End namespace Foam
156 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
158 #include "UPtrListIO.C"
160 // ************************************************************************* //