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-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 #include "ListOps.H" // For identity
30 
31 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
32 
33 template<class T>
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 :
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 inline Foam::SortableList<T>::SortableList(std::initializer_list<T> values)
98 :
99  List<T>(values)
100 {
101  sort();
102 }
103 
104 
105 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
106 
107 template<class T>
108 inline void Foam::SortableList<T>::clear()
109 {
110  List<T>::clear();
111  indices_.clear();
112 }
113 
114 
115 template<class T>
117 {
118  indices_.clear();
119  return static_cast<List<T>&>(*this);
120 }
121 
122 
123 template<class T>
125 {
126  Foam::sortedOrder(*this, indices_, typename UList<T>::less(*this));
128  List<T> list(*this, indices_); // Copy with indices for mapping
129  List<T>::transfer(list);
130 }
131 
132 
133 template<class T>
135 {
136  Foam::sortedOrder(*this, indices_, typename UList<T>::greater(*this));
138  List<T> list(*this, indices_); // Copy with indices for mapping
139  List<T>::transfer(list);
140 }
141 
142 
143 template<class T>
144 void Foam::SortableList<T>::partialSort(label n, label start)
145 {
146  indices_.resize_nocopy(this->size());
147  ListOps::identity(indices_);
148 
149  // Forward partial sort of indices
150  std::partial_sort
151  (
152  indices_.begin() + start,
153  indices_.begin() + start + n,
154  indices_.end(),
155  typename UList<T>::less(*this)
156  );
158  List<T> list(*this, indices_); // Copy with indices for mapping
159  List<T>::transfer(list);
160 }
161 
162 
163 template<class T>
164 void Foam::SortableList<T>::partialReverseSort(label n, label start)
165 {
166  indices_.resize_nocopy(this->size());
167  ListOps::identity(indices_);
168 
169  // Reverse partial sort of indices
170  std::partial_sort
171  (
172  indices_.begin() + start,
173  indices_.begin() + start + n,
174  indices_.end(),
175  typename UList<T>::greater(*this)
176  );
178  List<T> list(*this, indices_); // Copy with indices for mapping
179  List<T>::transfer(list);
180 }
181 
182 
183 template<class T>
185 {
186  if (this == &other)
187  {
188  return; // Self-swap is a no-op
189  }
190 
191  List<T>::swap(other);
192  indices_.swap(other.indices_);
193 }
194 
195 
196 // * * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * //
197 
198 template<class T>
199 inline void Foam::SortableList<T>::operator=(const T& val)
200 {
201  indices_.clear();
202  UList<T>::operator=(val);
203 }
204 
205 
206 template<class T>
207 inline void Foam::SortableList<T>::operator=(const UList<T>& lst)
208 {
209  indices_.clear();
210  List<T>::operator=(lst);
211 }
212 
213 
214 template<class T>
216 {
217  if (this == &lst)
218  {
219  return; // Self-assigment is a no-op
220  }
222  List<T>::operator=(lst);
223  indices_ = lst.indices();
224 }
225 
226 
227 template<class T>
228 inline void Foam::SortableList<T>::operator=(List<T>&& lst)
229 {
230  indices_.clear();
231  List<T>::transfer(lst);
232 }
233 
234 
235 template<class T>
237 {
238  if (this == &lst)
239  {
240  return; // Self-assigment is a no-op
241  }
243  clear();
244  this->swap(lst);
245 }
246 
247 
248 template<class T>
249 inline void Foam::SortableList<T>::operator=(std::initializer_list<T> lst)
250 {
251  List<T>::operator=(lst);
252  sort();
253 }
254 
255 
256 // ************************************************************************* //
void sort()
Forward (stable) sort the list (if changed after construction).
Definition: SortableList.C:117
void transfer(List< T > &list)
Transfer the contents of the argument List into this list and annul the argument list.
Definition: List.C:452
List< T > & shrink()
Clear the indices and return a reference to the underlying List.
Definition: SortableList.C:109
labelList sortedOrder(const UList< T > &input)
Return the (stable) sort order for the 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:56
A list that is sorted upon construction or when explicitly requested with the sort() method...
Definition: SortableList.H:56
A list compare binary predicate for reverse sort.
Definition: UList.H:244
void operator=(const UList< T > &a)
Assignment to UList operator. Takes linear time.
Definition: List.C:485
constexpr SortableList() noexcept
Default construct.
Definition: SortableList.C:27
static bool less(const vector &x, const vector &y)
To compare normals.
void swap(UList< T > &list)
Swap content with another UList of the same type in constant time.
Definition: UListI.H:435
Various functions to operate on Lists.
UList< T > & operator=(const UList< T > &)=delete
No copy assignment (default: shallow copy)
List< T > values(const HashTable< T, Key, Hash > &tbl, const bool doSort=false)
List of values from HashTable, optionally sorted.
Definition: HashOps.H:164
void clear()
Clear the list, i.e. set size to zero.
Definition: ListI.H:109
void sort(UList< T > &list)
Sort the list.
Definition: UList.C:348
patchWriters clear()
void operator=(const T &val)
Assignment of all entries to the given value, removing indices.
Definition: SortableList.C:192
void partialReverseSort(label n, label start=0)
Reverse partial sort the list until the middle point.
Definition: SortableList.C:157
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:99
const direction noexcept
Definition: Scalar.H:258
void T(FieldField< Field, Type > &f1, const FieldField< Field, Type > &f2)
const volScalarField & T
void reverseSort()
Reverse (stable) sort the list.
Definition: SortableList.C:127
const labelList & indices() const noexcept
Return the list of sorted indices. Updated every sort.
Definition: SortableList.H:129
A class representing the concept of 0 (zero) that can be used to avoid manipulating objects known to ...
Definition: zero.H:57
label n
void swap(SortableList< T > &other)
Swap content with another SortableList in constant time.
Definition: SortableList.C:177
void identity(labelUList &map, label start=0)
Set identity map with (map[i] == i)
Definition: ListOps.C:196
void clear()
Clear the list and the indices.
Definition: SortableList.C:101
A list compare binary predicate for normal sort.
Definition: UList.H:226
void partialSort(label n, label start=0)
Forward partial sort the list until the middle point.
Definition: SortableList.C:137
static constexpr const zero Zero
Global zero (0)
Definition: zero.H:133