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-2021 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 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>
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));
127 
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));
137 
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(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  );
157 
158  List<T> list(*this, indices_); // Copy with indices for mapping
159  List<T>::transfer(list);
160 }
161 
162 
163 template<class T>
165 {
166  indices_.resize(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  );
177 
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>
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  }
221 
222  List<T>::operator=(lst);
223  indices_ = lst.indices();
224 }
225 
226 
227 template<class T>
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  }
242 
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 // ************************************************************************* //
Foam::SortableList::sort
void sort()
Forward (stable) sort the list (if changed after construction).
Definition: SortableList.C:124
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:134
Foam::SortableList::partialSort
void partialSort(label n, label start=0)
Forward partial sort the list until the middle point.
Definition: SortableList.C:144
n
label n
Definition: TABSMDCalcMethod2.H:31
Foam::UList::greater
A list compare binary predicate for reverse sort.
Definition: UList.H:203
Foam::List::transfer
void transfer(List< T > &list)
Definition: List.C:456
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:261
Foam::SortableList::partialReverseSort
void partialReverseSort(label n, label start=0)
Reverse partial sort the list until the middle point.
Definition: SortableList.C:164
Foam::SortableList::operator=
void operator=(const T &val)
Assignment of all entries to the given value, removing indices.
Definition: SortableList.C:199
Foam::UList::less
A list compare binary predicate for normal sort.
Definition: UList.H:187
Foam::List::operator=
void operator=(const UList< T > &a)
Assignment to UList operator. Takes linear time.
Definition: List.C:498
Foam::SortableList::shrink
List< T > & shrink()
Clear the indices and return a reference to the underlying List.
Definition: SortableList.C:116
Foam::SortableList
A list that is sorted upon construction or when explicitly requested with the sort() method.
Definition: List.H:60
Foam::SortableList::SortableList
constexpr SortableList() noexcept
Default construct.
Definition: SortableList.C:34
Foam::SortableList::swap
void swap(SortableList< T > &other)
Swap content with another SortableList in constant time.
Definition: SortableList.C:184
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:116
Foam::SortableList::indices
const labelList & indices() const
Return the list of sorted indices. Updated every sort.
Definition: SortableList.H:108
ListOps.H
Various functions to operate on Lists.
Foam::SortableList::clear
void clear()
Clear the list and the indices.
Definition: SortableList.C:108
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