initial commit for version 1.6.x patch release
[OpenFOAM-1.6.x.git] / src / OpenFOAM / containers / Lists / UPtrList / UPtrList.C
blobbed0214749efe46fecb7de60e3e063c612fbf7d7
1 /*---------------------------------------------------------------------------*\
2   =========                 |
3   \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
4    \\    /   O peration     |
5     \\  /    A nd           | Copyright (C) 1991-2009 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"
31 // * * * * * * * * * * * * * * * Constructors  * * * * * * * * * * * * * * * //
33 template<class T>
34 Foam::UPtrList<T>::UPtrList()
36     ptrs_()
40 template<class T>
41 Foam::UPtrList<T>::UPtrList(const label s)
43     ptrs_(s, reinterpret_cast<T*>(0))
47 template<class T>
48 Foam::UPtrList<T>::UPtrList(const Xfer<UPtrList<T> >& lst)
50     transfer(lst());
54 template<class T>
55 Foam::UPtrList<T>::UPtrList(UPtrList<T>& a, bool reUse)
57     ptrs_(a.ptrs_, reUse)
61 // * * * * * * * * * * * * * * * Member Functions  * * * * * * * * * * * * * //
63 template<class T>
64 void Foam::UPtrList<T>::setSize(const label newSize)
66     label oldSize = size();
68     if (newSize <= 0)
69     {
70         clear();
71     }
72     else if (newSize < oldSize)
73     {
74         ptrs_.setSize(newSize);
75     }
76     else if (newSize > oldSize)
77     {
78         ptrs_.setSize(newSize);
80         // set new elements to NULL
81         for (register label i=oldSize; i<newSize; i++)
82         {
83             ptrs_[i] = NULL;
84         }
85     }
89 template<class T>
90 void Foam::UPtrList<T>::clear()
92     ptrs_.clear();
96 // Transfer the contents of the argument List into this List
97 // and anull the argument list
98 template<class T>
99 void Foam::UPtrList<T>::transfer(UPtrList<T>& a)
101     ptrs_.transfer(a.ptrs_);
105 template<class T>
106 void Foam::UPtrList<T>::reorder(const UList<label>& oldToNew)
108     if (oldToNew.size() != size())
109     {
110         FatalErrorIn("UPtrList<T>::reorder(const UList<label>&)")
111             << "Size of map (" << oldToNew.size()
112             << ") not equal to list size (" << size()
113             << ")." << abort(FatalError);
114     }
116     List<T*> newPtrs_(ptrs_.size(), reinterpret_cast<T*>(0));
118     forAll(*this, i)
119     {
120         label newI = oldToNew[i];
122         if (newI < 0 || newI >= size())
123         {
124             FatalErrorIn("UPtrList<T>::reorder(const UList<label>&)")
125                 << "Illegal index " << newI << nl
126                 << "Valid indices are 0.." << size()-1
127                 << abort(FatalError);
128         }
130         if (newPtrs_[newI])
131         {
132             FatalErrorIn("UPtrList<T>::reorder(const UList<label>&)")
133                 << "reorder map is not unique; element " << newI
134                 << " already set." << abort(FatalError);
135         }
136         newPtrs_[newI] = ptrs_[i];
137     }
139     forAll(newPtrs_, i)
140     {
141         if (!newPtrs_[i])
142         {
143             FatalErrorIn("UPtrList<T>::reorder(const UList<label>&)")
144                 << "Element " << i << " not set after reordering." << nl
145                 << abort(FatalError);
146         }
147     }
149     ptrs_.transfer(newPtrs_);
153 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
155 #include "UPtrListIO.C"
157 // ************************************************************************* //