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-2018 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  transfer(lst());
48 }
49 
50 
51 template<class T>
53 :
54  ptrs_(a.ptrs_, reuse)
55 {}
56 
57 
58 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
59 
60 template<class T>
61 void Foam::UPtrList<T>::setSize(const label newSize)
62 {
63  label oldSize = size();
64 
65  if (newSize <= 0)
66  {
67  clear();
68  }
69  else if (newSize < oldSize)
70  {
71  ptrs_.setSize(newSize);
72  }
73  else if (newSize > oldSize)
74  {
75  ptrs_.setSize(newSize);
76 
77  // set new elements to nullptr
78  for (label i=oldSize; i<newSize; i++)
79  {
80  ptrs_[i] = nullptr;
81  }
82  }
83 }
84 
85 
86 template<class T>
88 {
89  ptrs_.clear();
90 }
91 
92 
93 template<class T>
95 {
96  ptrs_.transfer(a.ptrs_);
97 }
98 
99 
100 template<class T>
102 {
103  if (oldToNew.size() != size())
104  {
106  << "Size of map (" << oldToNew.size()
107  << ") not equal to list size (" << size()
108  << ")." << abort(FatalError);
109  }
110 
111  List<T*> newPtrs_(ptrs_.size(), reinterpret_cast<T*>(0));
112 
113  forAll(*this, i)
114  {
115  label newI = oldToNew[i];
116 
117  if (newI < 0 || newI >= size())
118  {
120  << "Illegal index " << newI << nl
121  << "Valid indices are 0.." << size()-1
122  << abort(FatalError);
123  }
124 
125  if (newPtrs_[newI])
126  {
128  << "reorder map is not unique; element " << newI
129  << " already set." << abort(FatalError);
130  }
131  newPtrs_[newI] = ptrs_[i];
132  }
133 
134  forAll(newPtrs_, i)
135  {
136  if (!newPtrs_[i])
137  {
139  << "Element " << i << " not set after reordering." << nl
140  << abort(FatalError);
141  }
142  }
143 
144  ptrs_.transfer(newPtrs_);
145 }
146 
147 
148 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
149 
150 #include "UPtrListIO.C"
151 
152 // ************************************************************************* //
A simple container for copying or transferring objects of type <T>.
Definition: Xfer.H:85
#define forAll(list, i)
Loop across all elements in list.
Definition: UList.H:428
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
error FatalError
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:319
tUEqn clear()
A 1D array of objects of type <T>, where the size of the vector is known and used for subscript bound...
Definition: HashTable.H:60
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:61
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
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:61
static const char nl
Definition: Ostream.H:265
const volScalarField & T
void transfer(UPtrList< T > &)
Transfer the contents of the argument UPtrList into this.
Definition: UPtrList.C:94
void clear()
Clear the UPtrList, i.e. set size to zero.
Definition: UPtrList.C:87
label size() const
Return the number of elements in the UList.
Definition: UListI.H:299
void reorder(const labelUList &)
Reorders elements. Ordering does not have to be done in.
Definition: UPtrList.C:101