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 |
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>
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 (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 template<class T>
97 {
98  ptrs_.transfer(a.ptrs_);
99 }
100 
101 
102 template<class T>
104 {
105  if (oldToNew.size() != size())
106  {
107  FatalErrorIn("UPtrList<T>::reorder(const labelUList&)")
108  << "Size of map (" << oldToNew.size()
109  << ") not equal to list size (" << size()
110  << ")." << abort(FatalError);
111  }
112 
113  List<T*> newPtrs_(ptrs_.size(), reinterpret_cast<T*>(0));
114 
115  forAll(*this, i)
116  {
117  label newI = oldToNew[i];
118 
119  if (newI < 0 || newI >= size())
120  {
121  FatalErrorIn("UPtrList<T>::reorder(const labelUList&)")
122  << "Illegal index " << newI << nl
123  << "Valid indices are 0.." << size()-1
124  << abort(FatalError);
125  }
126 
127  if (newPtrs_[newI])
128  {
129  FatalErrorIn("UPtrList<T>::reorder(const labelUList&)")
130  << "reorder map is not unique; element " << newI
131  << " already set." << abort(FatalError);
132  }
133  newPtrs_[newI] = ptrs_[i];
134  }
135 
136  forAll(newPtrs_, i)
137  {
138  if (!newPtrs_[i])
139  {
140  FatalErrorIn("UPtrList<T>::reorder(const labelUList&)")
141  << "Element " << i << " not set after reordering." << nl
142  << abort(FatalError);
143  }
144  }
145 
146  ptrs_.transfer(newPtrs_);
147 }
148 
149 
150 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
151 
152 #include "UPtrListIO.C"
153 
154 // ************************************************************************* //
A templated 1D list of pointers to objects of type <T>, where the size of the array is known and used...
Definition: UPtrList.H:52
void reorder(const labelUList &)
Reorders elements. Ordering does not have to be done in.
Definition: UPtrList.C:103
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 transfer(List< T > &)
Transfer the contents of the argument List into this list.
Definition: List.C:390
void clear()
Clear the UPtrList, i.e. set size to zero.
Definition: UPtrList.C:89
void T(FieldField< Field, Type > &f1, const FieldField< Field, Type > &f2)
A simple container for copying or transferring objects of type <T>.
Definition: Xfer.H:85
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
void size(const label)
Override size to be inconsistent with allocated storage.
Definition: ListI.H:76
UPtrList()
Null Constructor.
Definition: UPtrList.C:33
void clear()
Clear the list, i.e. set size to zero.
Definition: List.C:379
static const char nl
Definition: Ostream.H:260
void setSize(const label)
Reset size of List.
Definition: List.C:318
#define forAll(list, i)
Definition: UList.H:421
label size() const
Return the number of elements in the UList.
Definition: UListI.H:299
errorManip< error > abort(error &err)
Definition: errorManip.H:131
#define FatalErrorIn(functionName)
Report an error message using Foam::FatalError.
Definition: error.H:314
label size() const
Return the number of elements in the UPtrList.
Definition: UPtrListI.H:31
error FatalError
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
const Foam::edgeFaceCirculator Foam::edgeFaceCirculator::endConstIter * reinterpret_cast(0), -1, false, -1, false
void transfer(UPtrList< T > &)
Transfer the contents of the argument UPtrList into this.
Definition: UPtrList.C:96
void setSize(const label)
Reset size of UPtrList. This can only be used to set the size.
Definition: UPtrList.C:63