FreeFOAM The Cross-Platform CFD Toolkit
UPtrList.C
Go to the documentation of this file.
1 /*---------------------------------------------------------------------------*\
2  ========= |
3  \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
4  \\ / O peration |
5  \\ / A nd | Copyright (C) 1991-2010 OpenCFD Ltd.
6  \\/ M anipulation |
7 -------------------------------------------------------------------------------
8 License
9  This file is part of OpenFOAM.
10 
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
13  the Free Software Foundation, either version 3 of the License, or
14  (at your option) any later version.
15 
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.
20 
21  You should have received a copy of the GNU General Public License
22  along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
23 
24 \*---------------------------------------------------------------------------*/
25 
26 #include <OpenFOAM/error.H>
27 
28 #include "UPtrList.H"
29 
30 // * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * * //
31 
32 template<class T>
34 :
35  ptrs_()
36 {}
37 
38 
39 template<class T>
41 :
42  ptrs_(s, reinterpret_cast<T*>(0))
43 {}
44 
45 
46 template<class T>
48 {
49  transfer(lst());
50 }
51 
52 
53 template<class T>
55 :
56  ptrs_(a.ptrs_, reUse)
57 {}
58 
59 
60 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
61 
62 template<class T>
63 void Foam::UPtrList<T>::setSize(const label newSize)
64 {
65  label oldSize = size();
66 
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);
78 
79  // set new elements to NULL
80  for (register label i=oldSize; i<newSize; i++)
81  {
82  ptrs_[i] = NULL;
83  }
84  }
85 }
86 
87 
88 template<class T>
90 {
91  ptrs_.clear();
92 }
93 
94 
95 // Transfer the contents of the argument List into this List
96 // and anull the argument list
97 template<class T>
99 {
100  ptrs_.transfer(a.ptrs_);
101 }
102 
103 
104 template<class T>
106 {
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  }
114 
115  List<T*> newPtrs_(ptrs_.size(), reinterpret_cast<T*>(0));
116 
117  forAll(*this, i)
118  {
119  label newI = oldToNew[i];
120 
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  }
128 
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  }
137 
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  }
147 
148  ptrs_.transfer(newPtrs_);
149 }
150 
151 
152 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
153 
154 #include "UPtrListIO.C"
155 
156 // ************************ vim: set sw=4 sts=4 et: ************************ //