UPtrList.C
Go to the documentation of this file.
1 /*---------------------------------------------------------------------------*\
2  ========= |
3  \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
4  \\ / O peration | Website: https://openfoam.org
5  \\ / A nd | Copyright (C) 2011-2022 OpenFOAM Foundation
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 "UPtrList.H"
27 
28 // * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * * //
29 
30 template<class T>
32 :
33  ptrs_()
34 {}
35 
36 
37 template<class T>
39 :
40  ptrs_(s, reinterpret_cast<T*>(0))
41 {}
42 
43 
44 template<class T>
46 :
47  ptrs_(a.ptrs_, reuse)
48 {}
49 
50 
51 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
52 
53 template<class T>
54 void Foam::UPtrList<T>::setSize(const label newSize)
55 {
56  label oldSize = size();
57 
58  if (newSize <= 0)
59  {
60  clear();
61  }
62  else if (newSize < oldSize)
63  {
64  ptrs_.setSize(newSize);
65  }
66  else if (newSize > oldSize)
67  {
68  ptrs_.setSize(newSize);
69 
70  // set new elements to nullptr
71  for (label i=oldSize; i<newSize; i++)
72  {
73  ptrs_[i] = nullptr;
74  }
75  }
76 }
77 
78 
79 template<class T>
81 {
82  ptrs_.clear();
83 }
84 
85 
86 template<class T>
88 {
89  ptrs_.transfer(a.ptrs_);
90 }
91 
92 
93 template<class T>
95 {
96  if (oldToNew.size() != size())
97  {
99  << "Size of map (" << oldToNew.size()
100  << ") not equal to list size (" << size()
101  << ")." << abort(FatalError);
102  }
103 
104  List<T*> newPtrs_(ptrs_.size(), reinterpret_cast<T*>(0));
105 
106  forAll(*this, i)
107  {
108  label newI = oldToNew[i];
109 
110  if (newI < 0 || newI >= size())
111  {
113  << "Illegal index " << newI << nl
114  << "Valid indices are 0.." << size()-1
115  << abort(FatalError);
116  }
117 
118  if (newPtrs_[newI])
119  {
121  << "reorder map is not unique; element " << newI
122  << " already set." << abort(FatalError);
123  }
124  newPtrs_[newI] = ptrs_[i];
125  }
126 
127  forAll(newPtrs_, i)
128  {
129  if (!newPtrs_[i])
130  {
132  << "Element " << i << " not set after reordering." << nl
133  << abort(FatalError);
134  }
135  }
136 
137  ptrs_.transfer(newPtrs_);
138 }
139 
140 
141 template<class T>
143 {
144  List<T*> newPtrs_(newToOld.size(), reinterpret_cast<T*>(0));
145 
146  forAll(newToOld, newI)
147  {
148  label oldI = newToOld[newI];
149 
150  if (oldI >= 0 && oldI < this->size())
151  {
152  newPtrs_[newI] = this->ptrs_[oldI];
153  }
154  }
155 
156  this->ptrs_.transfer(newPtrs_);
157 }
158 
159 
160 
161 template<class T>
162 template<class T2>
164 {
165  UPtrList<T2> result(size());
166  forAll(ptrs_, i)
167  {
168  result.set(i, ptrs_[i]);
169  }
170  return result;
171 }
172 
173 
174 template<class T>
175 template<class T2>
177 {
178  UPtrList<const T2> result(size());
179  forAll(ptrs_, i)
180  {
181  result.set(i, ptrs_[i]);
182  }
183  return result;
184 }
185 
186 
187 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
188 
189 #include "UPtrListIO.C"
190 
191 // ************************************************************************* //
#define forAll(list, i)
Loop across all elements in list.
Definition: UList.H:434
tUEqn clear()
error FatalError
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:306
A 1D array of objects of type <T>, where the size of the vector is known and used for subscript bound...
Definition: HashTable.H:59
UPtrList()
Null Constructor.
Definition: UPtrList.C:31
gmvFile<< "tracers "<< particles.size()<< nl;forAllConstIter(Cloud< passiveParticle >, particles, iter){ gmvFile<< iter().position().x()<< " ";}gmvFile<< nl;forAllConstIter(Cloud< passiveParticle >, particles, iter){ gmvFile<< iter().position().y()<< " ";}gmvFile<< nl;forAllConstIter(Cloud< passiveParticle >, particles, iter){ gmvFile<< iter().position().z()<< " ";}gmvFile<< nl;forAll(lagrangianScalarNames, i){ word name=lagrangianScalarNames[i];IOField< scalar > s(IOobject(name, runTime.timeName(), cloud::prefix, mesh, IOobject::MUST_READ, IOobject::NO_WRITE))
void setSize(const label)
Reset size of UPtrList. This can only be used to set the size.
Definition: UPtrList.C:54
A templated 1D list of pointers to objects of type <T>, where the size of the array is known and used...
Definition: UPtrList.H:54
errorManip< error > abort(error &err)
Definition: errorManip.H:131
void shuffle(const labelUList &newToOld)
Reorders elements. Ordering does not have to be done in.
Definition: UPtrList.C:142
A 1D vector of objects of type <T>, where the size of the vector is known and can be used for subscri...
Definition: HashTable.H:60
static const char nl
Definition: Ostream.H:260
bool set(const label) const
Is element set.
Definition: UPtrListI.H:78
UPtrList< T2 > convert()
Convert to list of different pointer type.
const volScalarField & T
void transfer(UPtrList< T > &)
Transfer the contents of the argument UPtrList into this.
Definition: UPtrList.C:87
void clear()
Clear the UPtrList, i.e. set size to zero.
Definition: UPtrList.C:80
label size() const
Return the number of elements in the UList.
Definition: UListI.H:311
void transfer(List< T > &)
Transfer the contents of the argument List into this list.
Definition: List.C:342
void reorder(const labelUList &oldToNew)
Reorders elements. Ordering does not have to be done in.
Definition: UPtrList.C:94