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:
40 (
41 values,
43 )
44{}
45
46
47template<class T>
49(
50 const UList<T>& values,
51 labelList&& addr
52)
53:
54 IndirectListAddressing<labelList>(std::move(addr)),
56 (
57 values,
59 )
60{}
61
62
63template<class T>
65(
66 const UList<T>& values,
67 const Foam::zero
68)
69:
70 IndirectListAddressing<labelList>(labelList()), // zero-size addressing
72 (
73 values,
75 )
76{}
77
78
79template<class T>
81:
82 // Copy addressing
83 IndirectListAddressing<labelList>(list.addressing()),
85 (
86 list.values(),
88 )
89{}
90
91
92template<class T>
94:
95 // Move addressing
96 IndirectListAddressing<labelList>(std::move(list.addressing())),
98 (
99 list.values(),
100 IndirectListAddressing<labelList>::addressing()
101 )
102{}
103
104
105template<class T>
107:
108 // Copy addressing
109 IndirectListAddressing<labelList>(list.addressing()),
111 (
112 list.values(),
113 IndirectListAddressing<labelList>::addressing()
114 )
115{}
116
117
118// * * * * * * * * * * * * * Static Member Functions * * * * * * * * * * * * //
119
120template<class T>
121template<class UnaryCondition>
123(
124 const UList<T>& values,
125 const UnaryCondition& select,
126 const bool invert
127)
128{
129 const label len = values.size();
130
131 IndirectList<T> result(values, Foam::zero{});
132 labelList& addr = result.addressing();
133
134 addr.resize_nocopy(len);
135
136 label count = 0;
137 for (label i=0; i < len; ++i)
138 {
139 // Test on position
140 if (select(i) ? !invert : invert)
141 {
142 addr[count] = i;
143 ++count;
144 }
145 }
146 addr.resize(count);
147
148 return result;
149}
150
151
152template<class T>
153template<class UnaryPredicate>
155(
156 const UList<T>& values,
157 const UnaryPredicate& pred,
158 const bool invert
159)
160{
161 const label len = values.size();
162
163 IndirectList<T> result(values, Foam::zero{});
164 labelList& addr = result.addressing();
165
166 addr.resize_nocopy(len);
167
168 label count = 0;
169 for (label i=0; i < len; ++i)
170 {
171 // Test on value
172 if (pred(values[i]) ? !invert : invert)
173 {
174 addr[count] = i;
175 ++count;
176 }
177 }
178 addr.resize(count);
179
180 return result;
181}
182
183
184template<class T>
186(
187 const UList<T>& values,
188 const bool sorted
189)
190{
191 const label len = values.size();
192
193 IndirectList<T> result(values, Foam::zero{});
194 labelList& order = result.addressing();
195
196 // Start from sorted order
197 Foam::sortedOrder(values, order);
198
199 if (len > 1)
200 {
201 label count = 0;
202 for (label i = 1; i < len; ++i)
203 {
204 // Eliminate duplicates
205 if (values[order[count]] != values[order[i]])
206 {
207 ++count;
208 order[count] = order[i];
209 }
210 }
211 ++count;
212 order.resize(count);
213
214 // Recover the original input order
215 if (!sorted)
216 {
217 Foam::sort(order);
218 }
219 }
220
221 return result;
222}
223
224
225// ************************************************************************* //
A class for storing list addressing (labels, slices etc), which are normally to used by IndirectList....
A List with indirect addressing.
Definition: IndirectList.H:119
static IndirectList< T > subset_if(const UList< T > &values, const UnaryPredicate &pred, const bool invert=false)
static IndirectList< T > subset(const UList< T > &values, const UnaryCondition &select, const bool invert=false)
A List with indirect addressing. Like IndirectList but does not store addressing.
Definition: IndirectList.H:79
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:94
A class representing the concept of 0 (zero) that can be used to avoid manipulating objects known to ...
Definition: zero.H:63
const volScalarField & T
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:342
labelList invert(const label len, const labelUList &map)
Create an inverse one-to-one mapping.
Definition: ListOps.C:36