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) 2011-2015 OpenFOAM Foundation
6  \\/ M anipulation | Copyright (C) 2015 OpenCFD Ltd.
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 "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>
47 Foam::UPtrList<T>::UPtrList(UList<T>& lst)
48 :
49  ptrs_(lst.size())
50 {
51  forAll(lst, i)
52  {
53  ptrs_[i] = &lst[i];
54  }
55 }
56 
57 
58 template<class T>
59 Foam::UPtrList<T>::UPtrList(PtrList<T>& lst)
60 :
61  ptrs_(lst.size())
62 {
63  forAll(lst, i)
64  {
65  ptrs_[i] = &lst[i];
66  }
67 }
68 
69 
70 template<class T>
71 Foam::UPtrList<T>::UPtrList(const Xfer<UPtrList<T> >& lst)
72 {
73  transfer(lst());
74 }
75 
76 
77 template<class T>
78 Foam::UPtrList<T>::UPtrList(UPtrList<T>& a, bool reUse)
79 :
80  ptrs_(a.ptrs_, reUse)
81 {}
82 
83 
84 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
85 
86 template<class T>
87 void Foam::UPtrList<T>::setSize(const label newSize)
88 {
89  label oldSize = size();
90 
91  if (newSize <= 0)
92  {
93  clear();
94  }
95  else if (newSize < oldSize)
96  {
97  ptrs_.setSize(newSize);
98  }
99  else if (newSize > oldSize)
100  {
101  ptrs_.setSize(newSize);
102 
103  // set new elements to NULL
104  for (label i=oldSize; i<newSize; i++)
105  {
106  ptrs_[i] = NULL;
107  }
108  }
109 }
110 
111 
112 template<class T>
114 {
115  ptrs_.clear();
116 }
117 
118 
119 template<class T>
121 {
122  ptrs_.transfer(a.ptrs_);
123 }
124 
125 
126 template<class T>
128 {
129  if (oldToNew.size() != size())
130  {
132  << "Size of map (" << oldToNew.size()
133  << ") not equal to list size (" << size()
134  << ")." << abort(FatalError);
135  }
136 
137  List<T*> newPtrs_(ptrs_.size(), reinterpret_cast<T*>(0));
138 
139  forAll(*this, i)
140  {
141  label newI = oldToNew[i];
142 
143  if (newI < 0 || newI >= size())
144  {
146  << "Illegal index " << newI << nl
147  << "Valid indices are 0.." << size()-1
148  << abort(FatalError);
149  }
150 
151  if (newPtrs_[newI])
152  {
154  << "reorder map is not unique; element " << newI
155  << " already set." << abort(FatalError);
156  }
157  newPtrs_[newI] = ptrs_[i];
158  }
159 
160  forAll(newPtrs_, i)
161  {
162  if (!newPtrs_[i])
163  {
165  << "Element " << i << " not set after reordering." << nl
166  << abort(FatalError);
167  }
168  }
169 
170  ptrs_.transfer(newPtrs_);
171 }
172 
173 
174 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
175 
176 #include "UPtrListIO.C"
177 
178 // ************************************************************************* //
reinterpret_cast
const Foam::edgeFaceCirculator Foam::edgeFaceCirculator::endConstIter * reinterpret_cast(0), -1, false, -1, false
UPtrList.H
Foam::UPtrList::clear
void clear()
Clear the UPtrList, i.e. set size to zero.
Definition: UPtrList.C:113
clear
UEqn clear()
forAll
#define forAll(list, i)
Loop across all elements in list.
Definition: UList.H:406
Foam::UPtrList::UPtrList
UPtrList()
Null Constructor.
Definition: UPtrList.C:33
Foam::UPtrList::setSize
void setSize(const label)
Reset size of UPtrList. This can only be used to set the size.
Definition: UPtrList.C:87
Foam::label
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.H
Foam::nl
static const char nl
Definition: Ostream.H:260
Foam::UPtrList::ptrs_
List< T * > ptrs_
Definition: UPtrList.H:99
Foam::UPtrList
A templated 1D list of pointers to objects of type <T>, where the size of the array is known and used...
Definition: UPtrList.H:53
Foam::FatalError
error FatalError
Foam::UPtrList::reorder
void reorder(const labelUList &)
Reorders elements. Ordering does not have to be done in.
Definition: UPtrList.C:127
Foam::abort
errorManip< error > abort(error &err)
Definition: errorManip.H:131
s
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))
UPtrListIO.C
FatalErrorInFunction
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:318
T
const volScalarField & T
Definition: createFields.H:25
Foam::List< T * >
Foam::UList
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
Foam::UList::size
label size() const
Return the number of elements in the UList.
Definition: UListI.H:299
Foam::UPtrList::transfer
void transfer(UPtrList< T > &)
Transfer the contents of the argument UPtrList into this.
Definition: UPtrList.C:120