SortableList.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) 2017-2020 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 #include "ListOps.H"
30 
31 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
32 
33 template<class T>
34 inline constexpr Foam::SortableList<T>::SortableList() noexcept
35 :
36  List<T>(),
37  indices_()
38 {}
39 
40 
41 template<class T>
42 inline Foam::SortableList<T>::SortableList(const label size)
43 :
44  List<T>(size)
45 {}
46 
47 
48 template<class T>
49 inline Foam::SortableList<T>::SortableList(const label size, const Foam::zero)
50 :
51  List<T>(size, Zero)
52 {}
53 
54 
55 template<class T>
56 inline Foam::SortableList<T>::SortableList(const label size, const T& val)
57 :
58  List<T>(size, val)
59 {}
60 
61 
62 template<class T>
64 :
65  List<T>(lst),
66  indices_(lst.indices())
67 {}
68 
69 
70 template<class T>
72 :
73  List<T>(std::move(lst)),
74  indices_(std::move(lst.indices_))
75 {}
76 
77 
78 template<class T>
80 :
81  List<T>(values)
82 {
83  sort();
84 }
85 
86 
87 template<class T>
89 :
90  List<T>(std::move(values))
91 {
92  sort();
93 }
94 
95 
96 template<class T>
97 template<class InputIterator>
99 (
100  InputIterator begIter,
101  InputIterator endIter
102 )
103 :
104  List<T>(begIter, endIter)
105 {
106  sort();
107 }
108 
109 
110 template<class T>
111 inline Foam::SortableList<T>::SortableList(std::initializer_list<T> values)
112 :
113  List<T>(values)
114 {
115  sort();
116 }
117 
118 
119 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
120 
121 template<class T>
123 {
124  List<T>::clear();
125  indices_.clear();
126 }
127 
128 
129 template<class T>
131 {
132  indices_.clear();
133  return static_cast<List<T>&>(*this);
134 }
135 
136 
137 template<class T>
139 {
140  Foam::sortedOrder(*this, indices_, typename UList<T>::less(*this));
141 
142  List<T> list(*this, indices_); // Copy with indices for mapping
143  List<T>::transfer(list);
144 }
145 
146 
147 template<class T>
149 {
150  Foam::sortedOrder(*this, indices_, typename UList<T>::greater(*this));
151 
152  List<T> list(*this, indices_); // Copy with indices for mapping
153  List<T>::transfer(list);
154 }
155 
156 
157 template<class T>
158 void Foam::SortableList<T>::partialSort(label n, label start)
159 {
160  indices_.resize(this->size());
161  ListOps::identity(indices_);
162 
163  // Forward partial sort of indices
164  std::partial_sort
165  (
166  indices_.begin() + start,
167  indices_.begin() + start + n,
168  indices_.end(),
169  typename UList<T>::less(*this)
170  );
171 
172  List<T> list(*this, indices_); // Copy with indices for mapping
173  List<T>::transfer(list);
174 }
175 
176 
177 template<class T>
179 {
180  indices_.resize(this->size());
181  ListOps::identity(indices_);
182 
183  // Reverse partial sort of indices
184  std::partial_sort
185  (
186  indices_.begin() + start,
187  indices_.begin() + start + n,
188  indices_.end(),
189  typename UList<T>::greater(*this)
190  );
191 
192  List<T> list(*this, indices_); // Copy with indices for mapping
193  List<T>::transfer(list);
194 }
195 
196 
197 template<class T>
199 {
200  if (this == &lst)
201  {
202  return; // Self-swap is a no-op
203  }
204 
205  List<T>::swap(lst);
206  indices_.swap(lst.indices_);
207 }
208 
209 
210 // * * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * //
211 
212 template<class T>
213 inline void Foam::SortableList<T>::operator=(const T& val)
214 {
215  indices_.clear();
216  UList<T>::operator=(val);
217 }
218 
219 
220 template<class T>
222 {
223  indices_.clear();
224  List<T>::operator=(lst);
225 }
226 
227 
228 template<class T>
230 {
231  if (this == &lst)
232  {
233  return; // Self-assigment is a no-op
234  }
235 
236  List<T>::operator=(lst);
237  indices_ = lst.indices();
238 }
239 
240 
241 template<class T>
243 {
244  indices_.clear();
245  List<T>::transfer(lst);
246 }
247 
248 
249 template<class T>
251 {
252  if (this == &lst)
253  {
254  return; // Self-assigment is a no-op
255  }
256 
257  clear();
258  this->swap(lst);
259 }
260 
261 
262 template<class T>
263 inline void Foam::SortableList<T>::operator=(std::initializer_list<T> lst)
264 {
265  List<T>::operator=(lst);
266  sort();
267 }
268 
269 
270 // * * * * * * * * * * * * * * * Global Functions * * * * * * * * * * * * * //
271 
272 template<class T>
274 {
275  a.swap(b);
276 }
277 
278 
279 // ************************************************************************* //
Foam::SortableList::sort
void sort()
Forward (stable) sort the list (if changed after construction).
Definition: SortableList.C:138
Foam::Zero
static constexpr const zero Zero
Global zero (0)
Definition: zero.H:131
Foam::HashTableOps::values
List< T > values(const HashTable< T, Key, Hash > &tbl, const bool doSort=false)
List of values from HashTable, optionally sorted.
Definition: HashOps.H:149
Foam::less
static bool less(const vector &x, const vector &y)
To compare normals.
Definition: meshRefinementRefine.C:57
Foam::SortableList::reverseSort
void reverseSort()
Reverse (stable) sort the list.
Definition: SortableList.C:148
Foam::Swap
void Swap(DynamicList< T, SizeMin1 > &a, DynamicList< T, SizeMin2 > &b)
Definition: DynamicListI.H:913
Foam::SortableList::partialSort
void partialSort(label n, label start=0)
Forward partial sort the list until the middle point.
Definition: SortableList.C:158
n
label n
Definition: TABSMDCalcMethod2.H:31
Foam::UList::greater
A list compare binary predicate for reverse sort.
Definition: UList.H:195
Foam::constant::physicoChemical::b
const dimensionedScalar b
Wien displacement law constant: default SI units: [m.K].
Definition: createFields.H:27
Foam::List::transfer
void transfer(List< T > &list)
Definition: List.C:459
Foam::T
void T(FieldField< Field, Type > &f1, const FieldField< Field, Type > &f2)
Definition: FieldFieldFunctions.C:58
Foam::sort
void sort(UList< T > &a)
Definition: UList.C:254
Foam::SortableList::partialReverseSort
void partialReverseSort(label n, label start=0)
Reverse partial sort the list until the middle point.
Definition: SortableList.C:178
Foam::SortableList::operator=
void operator=(const T &val)
Assignment of all entries to the given value, removing indices.
Definition: SortableList.C:213
Foam::UList::less
A list compare binary predicate for normal sort.
Definition: UList.H:179
Foam::List::operator=
void operator=(const UList< T > &a)
Assignment to UList operator. Takes linear time.
Definition: List.C:501
Foam::SortableList::shrink
List< T > & shrink()
Clear the indices and return a reference to the underlying List.
Definition: SortableList.C:130
Foam::SortableList::swap
void swap(SortableList< T > &lst)
Swap content with another SortableList in constant time.
Definition: SortableList.C:198
Foam::SortableList
A list that is sorted upon construction or when explicitly requested with the sort() method.
Definition: List.H:63
Foam::SortableList::SortableList
constexpr SortableList() noexcept
Null constructor, sort later (eg, after assignment or transfer)
Definition: SortableList.C:34
T
const volScalarField & T
Definition: createFieldRefs.H:2
clear
patchWriters clear()
Foam::List
A 1D array of objects of type <T>, where the size of the vector is known and used for subscript bound...
Definition: BitOps.H:63
Foam::UList::operator=
UList< T > & operator=(const UList< T > &)=delete
No copy assignment (default: shallow copy)
Foam::ListOps::identity
void identity(labelUList &map, label start=0)
Set identity map with (map[i] == i)
Definition: ListOps.C:203
Foam::UList
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:103
Foam::List::clear
void clear()
Clear the list, i.e. set size to zero.
Definition: ListI.H:115
Foam::SortableList::indices
const labelList & indices() const
Return the list of sorted indices. Updated every sort.
Definition: SortableList.H:113
ListOps.H
Various functions to operate on Lists.
Foam::SortableList::clear
void clear()
Clear the list and the indices.
Definition: SortableList.C:122
Foam::sortedOrder
labelList sortedOrder(const UList< T > &input)
Return the (stable) sort order for the list.
Foam::zero
A class representing the concept of 0 (zero) that can be used to avoid manipulating objects known to ...
Definition: zero.H:62