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-2022 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 }
112
113 ptrs_.transfer(newList);
114}
115
116
117template<class T>
118void Foam::UPtrList<T>::sortOrder(const labelUList& order, const bool check)
119{
120 const label len = this->size();
121
122 if (order.size() != len)
123 {
125 << "Size of map (" << order.size()
126 << ") not equal to list size (" << len
127 << ") for type " << typeid(T).name() << nl
128 << abort(FatalError);
129 }
130
131 Detail::PtrListDetail<T> newList(len);
132 Detail::PtrListDetail<T> guard(len);
133
134 for (label i=0; i<len; ++i)
135 {
136 const label oldIdx = order[i];
137
138 if (oldIdx < 0 || oldIdx >= len)
139 {
141 << "Illegal index " << oldIdx << nl
142 << "Valid indices are [0," << len << ") for type "
143 << typeid(T).name() << nl
144 << abort(FatalError);
145 }
146
147 if (guard[oldIdx])
148 {
150 << "order map is not unique; element " << oldIdx
151 << " already used for type " << typeid(T).name()
152 << abort(FatalError);
153 }
154
155 guard[oldIdx] = ptrs_[oldIdx];
156 newList[i] = ptrs_[oldIdx];
157 }
158
159 // Verify that all pointers were indeed set
160 if (check)
161 {
162 newList.checkNonNull();
163 }
164
165 ptrs_.transfer(newList);
166}
167
168
169// * * * * * * * * * * * * * * * Ostream Operators * * * * * * * * * * * * * //
170
171template<class T>
173{
174 return list.ptrs_.write(os);
175}
176
177
178// * * * * * * * * * * * * * * * Global Functions * * * * * * * * * * * * * //
179
180template<class T, class Compare>
181void Foam::sort(UPtrList<T>& list, const Compare& comp)
182{
183 std::stable_sort
184 (
185 list.begin_ptr(),
186 list.end_ptr(),
187 typename UPtrList<T>::template value_compare<Compare>(comp)
188 );
189}
190
191
192template<class T>
194{
195 // ie, lessOp<T>() or std::less<T>()
196 Foam::sort(list, [](const T& a, const T& b) { return (a < b); });
197}
199
200// ************************************************************************* //
A rudimentary list of pointers used for PtrList, UPtrList, etc. This class is considered implementati...
Definition: PtrListDetail.H:64
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:62
A list of pointers to objects of type <T>, with allocation/deallocation management of the pointers....
Definition: PtrList.H:73
void size(const label n)
Older name for setAddressableSize.
Definition: UList.H:114
A list of pointers to objects of type <T>, without allocation/deallocation management of the pointers...
Definition: UPtrList.H:71
T ** begin_ptr() noexcept
Iterator to begin of raw pointers traversal (use with caution)
Definition: UPtrList.H:440
void reorder(const labelUList &oldToNew, const bool check=false)
Definition: UPtrList.C:69
constexpr UPtrList() noexcept
Default construct.
Definition: UPtrListI.H:41
void sortOrder(const labelUList &order, const bool check=false)
Definition: UPtrList.C:118
T ** end_ptr() noexcept
Iterator beyond end of raw pointers traversal (use with caution)
Definition: UPtrList.H:443
label squeezeNull()
Squeeze out intermediate nullptr entries in the list of pointers.
Definition: UPtrList.C:45
Detail::PtrListDetail< T > ptrs_
The list of pointers.
Definition: UPtrList.H:77
const volScalarField & T
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:453
OBJstream os(runTime.globalPath()/outputName)
static void check(const int retVal, const char *what)
Ostream & operator<<(Ostream &, const boundaryPatch &p)
Write boundaryPatch as dictionary entries (without surrounding braces)
Definition: boundaryPatch.C:83
void sort(UList< T > &list)
Sort the list.
Definition: UList.C:342
errorManip< error > abort(error &err)
Definition: errorManip.H:144
error FatalError
word name(const expressions::valueTypeCode typeCode)
A word representation of a valueTypeCode. Empty for INVALID.
Definition: exprTraits.C:59
constexpr char nl
The newline '\n' character (0x0a)
Definition: Ostream.H:53
volScalarField & b
Definition: createFields.H:27