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-2023 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>
32 inline Foam::SortableList<T>::SortableList(const label size)
33 :
34  List<T>(size)
35 {}
36 
37 
38 template<class T>
39 inline Foam::SortableList<T>::SortableList(const label size, const Foam::zero)
40 :
41  List<T>(size, Foam::zero{})
42 {}
43 
44 
45 template<class T>
46 inline Foam::SortableList<T>::SortableList(const label size, const T& val)
47 :
48  List<T>(size, val)
49 {}
50 
51 
52 template<class T>
54 :
55  List<T>(lst),
56  indices_(lst.indices())
57 {}
58 
59 
60 template<class T>
62 :
63  List<T>(std::move(lst)),
64  indices_(std::move(lst.indices_))
65 {}
66 
67 
68 template<class T>
70 :
72 {
73  sort();
74 }
75 
76 
77 template<class T>
79 :
80  List<T>(std::move(values))
81 {
82  sort();
83 }
84 
85 
86 template<class T>
87 inline Foam::SortableList<T>::SortableList(std::initializer_list<T> values)
88 :
89  List<T>(values)
90 {
91  sort();
92 }
93 
94 
95 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
96 
97 template<class T>
98 inline void Foam::SortableList<T>::clear()
99 {
100  List<T>::clear();
101  indices_.clear();
102 }
103 
104 
105 template<class T>
107 {
108  indices_.clear();
109  return static_cast<List<T>&>(*this);
110 }
111 
112 
113 template<class T>
115 {
116  Foam::sortedOrder(*this, indices_, typename UList<T>::less(*this));
118  List<T> list(*this, indices_); // Copy with indices for mapping
119  List<T>::transfer(list);
120 }
121 
122 
123 template<class T>
125 {
126  Foam::sortedOrder(*this, indices_, typename UList<T>::greater(*this));
128  List<T> list(*this, indices_); // Copy with indices for mapping
129  List<T>::transfer(list);
130 }
131 
132 
133 template<class T>
134 void Foam::SortableList<T>::partialSort(label n, label start)
135 {
136  indices_.resize_nocopy(this->size());
137  Foam::identity(indices_, 0);
138 
139  // Forward partial sort of indices
140  std::partial_sort
141  (
142  indices_.begin() + start,
143  indices_.begin() + start + n,
144  indices_.end(),
145  typename UList<T>::less(*this)
146  );
148  List<T> list(*this, indices_); // Copy with indices for mapping
149  List<T>::transfer(list);
150 }
151 
152 
153 template<class T>
154 void Foam::SortableList<T>::partialReverseSort(label n, label start)
155 {
156  indices_.resize_nocopy(this->size());
157  Foam::identity(indices_, 0);
158 
159  // Reverse partial sort of indices
160  std::partial_sort
161  (
162  indices_.begin() + start,
163  indices_.begin() + start + n,
164  indices_.end(),
165  typename UList<T>::greater(*this)
166  );
168  List<T> list(*this, indices_); // Copy with indices for mapping
169  List<T>::transfer(list);
170 }
171 
172 
173 template<class T>
175 {
176  if (this == &other)
177  {
178  return; // Self-swap is a no-op
179  }
180 
181  List<T>::swap(other);
182  indices_.swap(other.indices_);
183 }
184 
185 
186 // * * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * //
187 
188 template<class T>
189 inline void Foam::SortableList<T>::operator=(const T& val)
190 {
191  indices_.clear();
192  UList<T>::operator=(val);
193 }
194 
195 
196 template<class T>
197 inline void Foam::SortableList<T>::operator=(const UList<T>& lst)
198 {
199  indices_.clear();
200  List<T>::operator=(lst);
201 }
202 
203 
204 template<class T>
206 {
207  if (this == &lst)
208  {
209  return; // Self-assigment is a no-op
210  }
212  List<T>::operator=(lst);
213  indices_ = lst.indices();
214 }
215 
216 
217 template<class T>
218 inline void Foam::SortableList<T>::operator=(List<T>&& lst)
219 {
220  indices_.clear();
221  List<T>::transfer(lst);
222 }
223 
224 
225 template<class T>
227 {
228  if (this == &lst)
229  {
230  return; // Self-assigment is a no-op
231  }
233  clear();
234  this->swap(lst);
235 }
236 
237 
238 template<class T>
239 inline void Foam::SortableList<T>::operator=(std::initializer_list<T> lst)
240 {
241  List<T>::operator=(lst);
242  sort();
243 }
244 
245 
246 // ************************************************************************* //
SortableList() noexcept=default
Default construct.
void swap(UList< T > &list) noexcept
Swap content with another UList of the same type in constant time.
Definition: UListI.H:505
void sort()
Forward (stable) sort the list (if changed after construction).
Definition: SortableList.C:107
void transfer(List< T > &list)
Transfer the contents of the argument List into this list and annul the argument list.
Definition: List.C:326
List< T > & shrink()
Clear the indices and return a reference to the underlying List.
Definition: SortableList.C:99
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:258
static bool less(const vector &x, const vector &y)
To compare normals.
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 operator=(const UList< T > &list)
Assignment to UList operator. Takes linear time.
Definition: List.C:360
labelList identity(const label len, label start=0)
Return an identity map of the given length with (map[i] == i), works like std::iota() but returning a...
Definition: labelLists.C:44
void clear()
Clear the list, i.e. set size to zero.
Definition: ListI.H:137
void sort(UList< T > &list)
Sort the list.
Definition: UList.C:296
patchWriters clear()
void operator=(const T &val)
Assignment of all entries to the given value, removing indices.
Definition: SortableList.C:182
void partialReverseSort(label n, label start=0)
Reverse partial sort the list until the middle point.
Definition: SortableList.C:147
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
void reverseSort()
Reverse (stable) sort the list.
Definition: SortableList.C:117
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:167
void clear()
Clear the list and the indices.
Definition: SortableList.C:91
A list compare binary predicate for normal sort.
Definition: UList.H:240
void partialSort(label n, label start=0)
Forward partial sort the list until the middle point.
Definition: SortableList.C:127
Namespace for OpenFOAM.