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 -------------------------------------------------------------------------------
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 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
30 
31 template<class T>
33 (
34  const UList<T>& values,
35  const labelUList& addr
36 )
37 :
40  (
41  values,
43  )
44 {}
45 
46 
47 template<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 
63 template<class T>
65 (
66  const UList<T>& values,
67  const Foam::zero
68 )
69 :
70  IndirectListAddressing<labelList>(labelList()), // zero-size addressing
72  (
74  IndirectListAddressing<labelList>::addressing()
75  )
76 {}
77 
78 
79 template<class T>
81 :
82  // Copy addressing
83  IndirectListAddressing<labelList>(list.addressing()),
85  (
86  list.values(),
87  IndirectListAddressing<labelList>::addressing()
88  )
89 {}
90 
91 
92 template<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 
105 template<class T>
107 :
108  // Copy addressing
109  IndirectListAddressing<labelList>(list.addressing()),
111  (
112  list.values(),
113  IndirectListAddressing<labelList>::addressing()
114  )
115 {}
117 
118 // * * * * * * * * * * * * * Static Member Functions * * * * * * * * * * * * //
119 
120 template<class T>
121 template<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 
152 template<class T>
153 template<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);
180  return result;
181 }
182 
183 
184 template<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 // ************************************************************************* //
labelList sortedOrder(const UList< T > &input)
Return the (stable) sort order for the list.
List< bool > select(const label n, const labelUList &locations)
Construct a selection list of bools (all false) with the given pre-size, subsequently add specified l...
Definition: BitOps.C:134
IndirectList(const UList< T > &values, const labelUList &addr)
Copy construct addressing, shallow copy values reference.
Definition: IndirectListI.H:26
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...
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. ...
List< T > values(const HashTable< T, Key, Hash > &tbl, const bool doSort=false)
List of values from HashTable, optionally sorted.
Definition: HashOps.H:164
unsigned int count(const UList< bool > &bools, const bool val=true)
Count number of &#39;true&#39; entries.
Definition: BitOps.H:73
void sort(UList< T > &list)
Sort the list.
Definition: UList.C:296
A 1D vector of objects of type <T>, where the size of the vector is known and can be used for subscri...
Definition: HashTable.H:105
void T(FieldField< Field, Type > &f1, const FieldField< Field, Type > &f2)
const volScalarField & T
labelList invert(const label len, const labelUList &map)
Create an inverse one-to-one mapping.
Definition: ListOps.C:29
A class for storing list addressing (labels, slices etc), which are normally to used by IndirectList...
A List with indirect addressing. Like IndirectList but does not store addressing. ...
Definition: faMatrix.H:56
A class representing the concept of 0 (zero) that can be used to avoid manipulating objects known to ...
Definition: zero.H:57
static IndirectList< T > uniq(const UList< T > &values, const bool sorted=false)
Return an IndirectList with duplicate entries filtered out.
List< label > labelList
A List of labels.
Definition: List.H:62
A List with indirect addressing.
Definition: IndirectList.H:60