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-2023 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 template<class T>
161 template<class T2>
163 {
164  UPtrList<T2> result(size());
165  forAll(ptrs_, i)
166  {
167  result.set(i, ptrs_[i]);
168  }
169  return result;
170 }
171 
172 
173 template<class T>
174 template<class T2>
176 {
177  UPtrList<const T2> result(size());
178  forAll(ptrs_, i)
179  {
180  result.set(i, ptrs_[i]);
181  }
182  return result;
183 }
184 
185 
186 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
187 
188 #include "UPtrListIO.C"
189 
190 // ************************************************************************* //
#define forAll(list, i)
Loop across all elements in list.
Definition: UList.H:434
void transfer(List< T > &)
Transfer the contents of the argument List into this list.
Definition: List.C:342
label size() const
Return the number of elements in the UList.
Definition: UListI.H:311
A templated 1D list of pointers to objects of type <T>, where the size of the array is known and used...
Definition: UPtrList.H:66
UPtrList< T2 > convert()
Convert to list of different pointer type.
bool set(const label) const
Is element set.
Definition: UPtrListI.H:87
void transfer(UPtrList< T > &)
Transfer the contents of the argument UPtrList into this.
Definition: UPtrList.C:87
UPtrList()
Null Constructor.
Definition: UPtrList.C:31
void reorder(const labelUList &oldToNew)
Reorders elements. Ordering does not have to be done in.
Definition: UPtrList.C:94
void shuffle(const labelUList &newToOld)
Reorders elements. Ordering does not have to be done in.
Definition: UPtrList.C:142
void clear()
Clear the UPtrList, i.e. set size to zero.
Definition: UPtrList.C:80
void setSize(const label)
Reset size of UPtrList. This can only be used to set the size.
Definition: UPtrList.C:54
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:334
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.name(), cloud::prefix, mesh, IOobject::MUST_READ, IOobject::NO_WRITE))
tUEqn clear()
intWM_LABEL_SIZE_t label
A label is an int32_t or int64_t as specified by the pre-processor macro WM_LABEL_SIZE.
Definition: label.H:59
errorManip< error > abort(error &err)
Definition: errorManip.H:131
error FatalError
static const char nl
Definition: Ostream.H:266
void T(FieldField< Field, Type > &f1, const FieldField< Field, Type > &f2)