Loading...
Searching...
No Matches
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-2025 OpenCFD Ltd.
10-------------------------------------------------------------------------------
11License
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
35template<class T>
37:
38 ptrs_(list.ptrs_) // shallow copy (via const reference)
39{}
40
41
42// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
43
44template<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
68template<class T>
69void Foam::UPtrList<T>::reorder(const labelUList& oldToNew, const bool check)
70{
71 const label len = this->size();
72
73 if (oldToNew.size() != len)
74 {
76 << "Size of map (" << oldToNew.size()
77 << ") not equal to list size (" << len
78 << ") for type " << typeid(T).name() << nl
79 << abort(FatalError);
80 }
81
82 Detail::PtrListDetail<T> newList(len);
83
84 for (label i = 0; i < len; ++i)
85 {
86 const label newIdx = oldToNew[i];
87
88 if (newIdx < 0 || newIdx >= len)
89 {
91 << "Illegal index " << newIdx << nl
92 << "Valid indices are [0," << len << ") for type "
93 << typeid(T).name() << nl
94 << abort(FatalError);
95 }
96
97 if (newList[newIdx])
98 {
100 << "reorder map is not unique; element " << newIdx
101 << " already used for type " << typeid(T).name()
102 << abort(FatalError);
103 }
104 newList[newIdx] = ptrs_[i];
105 }
106
107 // Verify all pointers were indeed set
108 if (check)
109 {
110 newList.checkNonNull();
111 }
113 // Copy the pointers, do not swap or transfer lists!
114 ptrs_ = newList;
115}
116
117
118template<class T>
119void Foam::UPtrList<T>::sortOrder(const labelUList& order, const bool check)
120{
121 const label len = this->size();
122
123 if (order.size() != len)
124 {
126 << "Size of map (" << order.size()
127 << ") not equal to list size (" << len
128 << ") for type " << typeid(T).name() << nl
129 << abort(FatalError);
130 }
131
132 Detail::PtrListDetail<T> newList(len);
133 Detail::PtrListDetail<T> guard(len);
134
135 for (label i = 0; i < len; ++i)
136 {
137 const label oldIdx = order[i];
138
139 if (oldIdx < 0 || oldIdx >= len)
140 {
142 << "Illegal index " << oldIdx << nl
143 << "Valid indices are [0," << len << ") for type "
144 << typeid(T).name() << nl
145 << abort(FatalError);
146 }
147
148 if (guard[oldIdx])
149 {
151 << "order map is not unique; element " << oldIdx
152 << " already used for type " << typeid(T).name()
153 << abort(FatalError);
154 }
155
156 guard[oldIdx] = ptrs_[oldIdx];
157 newList[i] = ptrs_[oldIdx];
158 }
159
160 // Verify that all pointers were indeed set
161 if (check)
162 {
163 newList.checkNonNull();
164 }
165
166 // Copy the pointers, do not swap or transfer lists!
167 ptrs_ = newList;
168}
169
170
171// * * * * * * * * * * * * * * * Ostream Operators * * * * * * * * * * * * * //
172
173template<class T>
175{
176 return ptrs_.printAddresses(os);
177}
178
179
180template<class T>
182(
183 Ostream& os,
184 const bool trimNull
185) const
186{
187 return ptrs_.write(os, trimNull);
188}
189
190
191template<class T>
192Foam::Ostream& Foam::operator<<(Ostream& os, const UPtrList<T>& list)
193{
194 return list.writeList(os, false); // Do not ignore null
195}
196
197
198// * * * * * * * * * * * * * * * Global Functions * * * * * * * * * * * * * //
199
200template<class T>
201void Foam::sort(UPtrList<T>& list)
202{
203 std::stable_sort
204 (
205 list.begin_ptr(),
206 list.end_ptr(),
207 // Compare less, with nullptr protect and sort nullptr to end
208 [](const T* const a, const T* const b) -> bool
209 {
210 return (a && b) ? (*a < *b) : !b;
211 }
212 );
213}
214
215
216template<class T, class Compare>
217void Foam::sort(UPtrList<T>& list, const Compare& comp)
218{
219 std::stable_sort
220 (
221 list.begin_ptr(),
222 list.end_ptr(),
223 typename UPtrList<T>::template value_compare<Compare>(comp)
224 );
225}
226
227
228// ************************************************************************* //
A rudimentary list of pointers used for PtrList, UPtrList, etc. This class is considered implementati...
void checkNonNull() const
FatalError if any null exists in the list.
An Ostream is an abstract base class for all output systems (streams, files, token lists,...
Definition Ostream.H:59
virtual bool write(const token &tok)=0
Write token to stream or otherwise handle it.
A list of pointers to objects of type <T>, with allocation/deallocation management of the pointers....
Definition PtrList.H:67
void size(const label n)
Older name for setAddressableSize.
Definition UList.H:118
A list of pointers to objects of type <T>, without allocation/deallocation management of the pointers...
Definition UPtrList.H:101
T ** begin_ptr() noexcept
Iterator to begin of raw pointers traversal (use with caution).
Definition UPtrList.H:798
void reorder(const labelUList &oldToNew, const bool check=false)
Reorder elements. Reordering must be unique (ie, shuffle).
Definition UPtrList.C:62
Ostream & writeList(Ostream &os, const bool trimNull=false) const
Write UPtrList to Ostream, optionally ignoring null entries.
Definition UPtrList.C:175
Ostream & printAddresses(Ostream &os) const
Print pointer addresses to Ostream (debugging only).
Definition UPtrList.C:167
void sortOrder(const labelUList &order, const bool check=false)
Reorder elements according to new order mapping (newToOld). Reordering must be unique (ie,...
Definition UPtrList.C:112
T ** end_ptr() noexcept
Iterator beyond end of raw pointers traversal (use with caution).
Definition UPtrList.H:803
label size() const noexcept
The number of entries in the list.
Definition UPtrListI.H:106
constexpr UPtrList() noexcept=default
Default construct.
label squeezeNull()
Squeeze out nullptr entries in the list of pointers after which any null pointers will be at the end ...
Definition UPtrList.C:38
Detail::PtrListDetail< T > ptrs_
The list of pointers.
Definition UPtrList.H:109
const volScalarField & T
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition error.H:600
OBJstream os(runTime.globalPath()/outputName)
auto & name
static void check(const int retVal, const char *what)
Ostream & operator<<(Ostream &, const boundaryPatch &p)
Write boundaryPatch as dictionary entries (without surrounding braces).
void sort(UList< T > &list)
Sort the list.
Definition UList.C:283
errorManip< error > abort(error &err)
Definition errorManip.H:139
error FatalError
Error stream (stdout output on all processes), with additional 'FOAM FATAL ERROR' header text and sta...
UList< label > labelUList
A UList of labels.
Definition UList.H:75
void T(FieldField< Field, Type > &f1, const FieldField< Field, Type > &f2)
constexpr char nl
The newline '\n' character (0x0a).
Definition Ostream.H:50
volScalarField & b