Loading...
Searching...
No Matches
IndirectListI.H
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) 2019-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// * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
30
31template<class T>
33(
34 const UList<T>& values,
35 const labelUList& addr
36)
37:
38 // Copy addressing
41 (
44 )
45{}
46
47
48template<class T>
50(
51 const UList<T>& values,
52 labelList&& addr
53)
54:
55 // Move addressing
56 IndirectListAddressing<labelList>(std::move(addr)),
58 (
61 )
62{}
63
64
65template<class T>
67(
68 const UList<T>& values,
70)
71:
72 IndirectListAddressing<labelList>(labelList()), // zero-size addressing
74 (
77 )
78{}
79
80
81template<class T>
83:
84 // Copy addressing
85 IndirectListAddressing<labelList>(list.addressing()),
87 (
90 )
91{}
92
93
94template<class T>
96:
97 // Move addressing
98 IndirectListAddressing<labelList>(std::move(list.addressing())),
100 (
103 )
104{}
105
106
107template<class T>
109:
110 // Copy addressing
111 IndirectListAddressing<labelList>(list.addressing()),
113 (
114 list.values(),
115 IndirectListAddressing<labelList>::addressing()
116 )
118
119
120// * * * * * * * * * * * * * Static Member Functions * * * * * * * * * * * * //
121
122template<class T>
123template<class UnaryCondition>
125(
126 const UList<T>& values,
127 const UnaryCondition& select,
128 const bool invert
129)
130{
131 const label len = values.size();
132
133 IndirectList<T> result(values, Foam::zero{});
134 labelList& addr = result.addressing();
135
136 addr.resize_nocopy(len);
137
138 label count = 0;
139 for (label i=0; i < len; ++i)
140 {
141 // Test on position
142 if (select(i) ? !invert : invert)
143 {
144 addr[count] = i;
145 ++count;
146 }
147 }
148 addr.resize(count);
150 return result;
151}
152
153
154template<class T>
155template<class UnaryPredicate>
157(
158 const UList<T>& values,
159 const UnaryPredicate& pred,
160 const bool invert
161)
162{
163 const label len = values.size();
164
165 IndirectList<T> result(values, Foam::zero{});
166 labelList& addr = result.addressing();
167
168 addr.resize_nocopy(len);
169
170 label count = 0;
171 for (label i=0; i < len; ++i)
172 {
173 // Test on value
174 if (pred(values[i]) ? !invert : invert)
175 {
176 addr[count] = i;
177 ++count;
178 }
179 }
180 addr.resize(count);
181
182 return result;
183}
184
185
186template<class T>
188(
189 const UList<T>& values,
190 const bool sorted
191)
192{
193 const label len = values.size();
194
195 IndirectList<T> result(values, Foam::zero{});
196 labelList& order = result.addressing();
197
198 // Start from sorted order
199 Foam::sortedOrder(values, order);
200
201 if (len > 1)
202 {
203 label count = 0;
204 for (label i = 1; i < len; ++i)
205 {
206 // Eliminate duplicates
207 if (values[order[count]] != values[order[i]])
208 {
209 ++count;
210 order[count] = order[i];
211 }
212 }
213 ++count;
214 order.resize(count);
215
216 // Recover the original input order
217 if (!sorted)
218 {
219 Foam::sort(order);
220 }
221 }
222
223 return result;
224}
225
226
227// ************************************************************************* //
A class for storing list addressing (labels, slices etc), which are normally to used by IndirectList....
const UList< T > & values() const noexcept
A List with indirect addressing.
IndirectList(const UList< T > &values, const labelUList &addr)
Copy construct addressing, shallow copy values reference.
const Addr & addressing() const noexcept
The list addressing.
static IndirectList< T > uniq(const UList< T > &values, const bool sorted=false)
Return an IndirectList with duplicate entries filtered out.
static IndirectList< T > subset_if(const UList< T > &values, const UnaryPredicate &pred, const bool invert=false)
Return an IndirectList comprising entries with values that satisfy the predicate.
static IndirectList< T > subset(const UList< T > &values, const UnaryCondition &select, const bool invert=false)
Return an IndirectList comprising entries with positions that satisfy the condition predicate.
void resize_nocopy(const label len)
Adjust allocated size of list without necessarily.
Definition ListI.H:171
A List with indirect addressing. Like IndirectList but does not store addressing.
UIndirectList(const UList< T > &values, const labelUList &addr)
Shallow copy values and addressing.
A 1D vector of objects of type <T>, where the size of the vector is known and can be used for subscri...
Definition UList.H:89
A class representing the concept of 0 (zero) that can be used to avoid manipulating objects known to ...
Definition zero.H:58
unsigned int count(const UList< bool > &bools, const bool val=true)
Count number of 'true' entries.
Definition BitOps.H:73
List< label > labelList
A List of labels.
Definition List.H:62
labelList sortedOrder(const UList< T > &input)
Return the (stable) sort order for the list.
void sort(UList< T > &list)
Sort the list.
Definition UList.C:283
labelList invert(const label len, const labelUList &map)
Create an inverse one-to-one mapping.
Definition ListOps.C:28
UList< label > labelUList
A UList of labels.
Definition UList.H:75
void T(FieldField< Field, Type > &f1, const FieldField< Field, Type > &f2)