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 :
38  // Copy addressing
41  (
42  values,
44  )
45 {}
46 
47 
48 template<class T>
50 (
51  const UList<T>& values,
52  labelList&& addr
53 )
54 :
55  // Move addressing
56  IndirectListAddressing<labelList>(std::move(addr)),
58  (
59  values,
61  )
62 {}
63 
64 
65 template<class T>
67 (
68  const UList<T>& values,
70 )
71 :
72  IndirectListAddressing<labelList>(labelList()), // zero-size addressing
74  (
76  IndirectListAddressing<labelList>::addressing()
77  )
78 {}
79 
80 
81 template<class T>
83 :
84  // Copy addressing
85  IndirectListAddressing<labelList>(list.addressing()),
87  (
88  list.values(),
89  IndirectListAddressing<labelList>::addressing()
90  )
91 {}
92 
93 
94 template<class T>
96 :
97  // Move addressing
98  IndirectListAddressing<labelList>(std::move(list.addressing())),
100  (
101  list.values(),
102  IndirectListAddressing<labelList>::addressing()
103  )
104 {}
105 
106 
107 template<class T>
109 :
110  // Copy addressing
111  IndirectListAddressing<labelList>(list.addressing()),
113  (
114  list.values(),
115  IndirectListAddressing<labelList>::addressing()
116  )
117 {}
119 
120 // * * * * * * * * * * * * * Static Member Functions * * * * * * * * * * * * //
121 
122 template<class T>
123 template<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);
149 
150  return result;
151 }
152 
153 
154 template<class T>
155 template<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);
182  return result;
183 }
184 
185 
186 template<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 // ************************************************************************* //
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:295
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:28
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:52
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:61
A List with indirect addressing.
Definition: IndirectList.H:60