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 | www.openfoam.com
6  \\/ M anipulation |
7 -------------------------------------------------------------------------------
8  Copyright (C) 2011-2016 OpenFOAM Foundation
9  Copyright (C) 2015-2019 OpenCFD Ltd.
10 -------------------------------------------------------------------------------
11 License
12  This file is part of OpenFOAM.
13 
14  OpenFOAM is free software: you can redistribute it and/or modify it
15  under the terms of the GNU General Public License as published by
16  the Free Software Foundation, either version 3 of the License, or
17  (at your option) any later version.
18 
19  OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
20  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
21  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
22  for more details.
23 
24  You should have received a copy of the GNU General Public License
25  along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
26 
27 \*---------------------------------------------------------------------------*/
28 
29 #include "UPtrList.H"
30 #include "PtrList.H"
31 #include "Ostream.H"
32 
33 // * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * * //
34 
35 template<class T>
37 :
38  ptrs_(list.ptrs_) // shallow copy (via const reference)
39 {}
40 
41 
42 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
43 
44 template<class T>
46 {
47  const label len = this->size();
48  label newLen = 0;
49 
50  for (label i=0; i < len; ++i)
51  {
52  T* ptr = ptrs_[i];
53  if (ptr)
54  {
55  if (i != newLen)
56  {
57  ptrs_[newLen] = ptr;
58  ptrs_[i] = nullptr;
59  }
60  ++newLen;
61  }
62  }
63 
64  return newLen;
65 }
66 
67 
68 template<class T>
70 (
71  const labelUList& oldToNew,
72  const bool testNull
73 )
74 {
75  const label len = this->size();
76 
77  if (oldToNew.size() != len)
78  {
80  << "Size of map (" << oldToNew.size()
81  << ") not equal to list size (" << len
82  << ") for type " << typeid(T).name() << nl
83  << abort(FatalError);
84  }
85 
86  Detail::PtrListDetail<T> newList(len);
87 
88  for (label i=0; i<len; ++i)
89  {
90  const label idx = oldToNew[i];
91 
92  if (idx < 0 || idx >= len)
93  {
95  << "Illegal index " << idx << nl
96  << "Valid indices are [0," << len << ") for type "
97  << typeid(T).name() << nl
98  << abort(FatalError);
99  }
100 
101  if (newList[idx])
102  {
104  << "reorder map is not unique; element " << idx
105  << " already used for type " << typeid(T).name()
106  << abort(FatalError);
107  }
108  newList[idx] = ptrs_[i];
109  }
110 
111  // Verify that all pointers were indeed set
112  if (testNull)
113  {
114  const label idx = newList.findNull();
115  if (idx >= 0)
116  {
118  << "Element " << idx << " not set after reordering." << nl
119  << abort(FatalError);
120  }
121  }
122 
123  ptrs_.transfer(newList);
124 }
125 
126 
127 template<class T>
129 (
130  const labelUList& order,
131  const bool testNull
132 )
133 {
134  const label len = this->size();
135 
136  if (order.size() != len)
137  {
139  << "Size of map (" << order.size()
140  << ") not equal to list size (" << len
141  << ") for type " << typeid(T).name() << nl
142  << abort(FatalError);
143  }
144 
145  Detail::PtrListDetail<T> newList(len);
146  Detail::PtrListDetail<T> guard(len);
147 
148  for (label i=0; i<len; ++i)
149  {
150  const label idx = order[i];
151 
152  if (idx < 0 || idx >= len)
153  {
155  << "Illegal index " << idx << nl
156  << "Valid indices are [0," << len << ") for type "
157  << typeid(T).name() << nl
158  << abort(FatalError);
159  }
160 
161  if (guard[idx])
162  {
164  << "order map is not unique; element " << idx
165  << " already used for type " << typeid(T).name()
166  << abort(FatalError);
167  }
168 
169  guard[idx] = ptrs_[idx];
170  newList[i] = ptrs_[idx];
171  }
172 
173  // Verify that all pointers were indeed set
174  if (testNull)
175  {
176  const label idx = newList.findNull();
177  if (idx >= 0)
178  {
180  << "Element " << idx << " not set after reordering." << nl
181  << abort(FatalError);
182  }
183  }
184 
185  ptrs_.transfer(newList);
186 }
187 
188 
189 // * * * * * * * * * * * * * * * Ostream Operators * * * * * * * * * * * * * //
190 
191 template<class T>
193 {
194  return list.ptrs_.write(os);
195 }
196 
197 
198 // ************************************************************************* //
Foam::Detail::PtrListDetail::findNull
label findNull() const
Locate the first null entry, -1 if there are not any.
Definition: PtrListDetail.C:51
UPtrList.H
Foam::UPtrList::ptrs_
Detail::PtrListDetail< T > ptrs_
The list of pointers.
Definition: UPtrList.H:77
Foam::operator<<
Ostream & operator<<(Ostream &, const boundaryPatch &p)
Write boundaryPatch as dictionary entries (without surrounding braces)
Definition: boundaryPatch.C:83
Foam::UPtrList
A list of pointers to objects of type <T>, without allocation/deallocation management of the pointers...
Definition: UPtrList.H:62
Foam::PtrList
A list of pointers to objects of type <T>, with allocation/deallocation management of the pointers....
Definition: List.H:59
Foam::UPtrList::UPtrList
constexpr UPtrList() noexcept
Default construct.
Definition: UPtrListI.H:41
Foam::FatalError
error FatalError
Foam::UPtrList::sortOrder
void sortOrder(const labelUList &order, const bool testNull=true)
Definition: UPtrList.C:129
os
OBJstream os(runTime.globalPath()/outputName)
Foam::abort
errorManip< error > abort(error &err)
Definition: errorManip.H:144
T
const volScalarField & T
Definition: createFieldRefs.H:2
Ostream.H
FatalErrorInFunction
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:453
Foam::nl
constexpr char nl
Definition: Ostream.H:404
Foam::UPtrList::squeezeNull
label squeezeNull()
Squeeze out intermediate nullptr entries in the list of pointers.
Definition: UPtrList.C:45
Foam::UList< label >
Foam::name
word name(const expressions::valueTypeCode typeCode)
A word representation of a valueTypeCode. Empty for INVALID.
Definition: exprTraits.C:59
PtrList.H
Foam::Detail::PtrListDetail
A rudimentary list of pointers used for PtrList, UPtrList, etc. This class is considered implementati...
Definition: PtrListDetail.H:61
Foam::UList::size
void size(const label n)
Older name for setAddressableSize.
Definition: UList.H:114
Foam::Ostream
An Ostream is an abstract base class for all output systems (streams, files, token lists,...
Definition: Ostream.H:56
Foam::UPtrList::reorder
void reorder(const labelUList &oldToNew, const bool testNull=true)
Definition: UPtrList.C:70