Loading...
Searching...
No Matches
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-------------------------------------------------------------------------------
11License
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
31template<class T>
33:
34 List<T>(size)
35{}
36
37
38template<class T>
40:
41 List<T>(size, Foam::zero{})
42{}
43
44
45template<class T>
46inline Foam::SortableList<T>::SortableList(const label size, const T& val)
47:
48 List<T>(size, val)
49{}
50
51
52template<class T>
55 List<T>(lst),
56 indices_(lst.indices())
57{}
58
59
60template<class T>
63 List<T>(std::move(lst)),
64 indices_(std::move(lst.indices_))
65{}
66
67
68template<class T>
70:
71 List<T>(values)
72{
73 sort();
74}
75
76
77template<class T>
79:
80 List<T>(std::move(values))
81{
82 sort();
83}
84
85
86template<class T>
87inline Foam::SortableList<T>::SortableList(std::initializer_list<T> values)
88:
89 List<T>(values)
90{
92}
93
94
95// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
96
97template<class T>
101 indices_.clear();
102}
103
104
105template<class T>
108 indices_.clear();
109 return static_cast<List<T>&>(*this);
110}
111
112
113template<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
123template<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
133template<class T>
134void 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
153template<class T>
154void 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
173template<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
188template<class T>
189inline void Foam::SortableList<T>::operator=(const T& val)
191 indices_.clear();
193}
194
195
196template<class T>
197inline void Foam::SortableList<T>::operator=(const UList<T>& lst)
199 indices_.clear();
201}
202
203
204template<class T>
206{
207 if (this == &lst)
208 {
209 return; // Self-assigment is a no-op
210 }
213 indices_ = lst.indices();
214}
215
216
217template<class T>
220 indices_.clear();
222}
223
224
225template<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
238template<class T>
239inline void Foam::SortableList<T>::operator=(std::initializer_list<T> lst)
240{
242 sort();
243}
244
245
246// ************************************************************************* //
label n
A 1D array of objects of type <T>, where the size of the vector is known and used for subscript bound...
Definition List.H:72
void transfer(List< T > &list)
Transfer the contents of the argument List into this list and annul the argument list.
Definition List.C:347
void operator=(const UList< T > &list)
Assignment to UList operator. Takes linear time.
Definition List.C:381
void clear()
Clear the list, i.e. set size to zero.
Definition ListI.H:133
A list that is sorted upon construction or when explicitly requested with the sort() method.
void partialSort(label n, label start=0)
Forward partial sort the list until the middle point.
const labelList & indices() const noexcept
Return the list of sorted indices. Updated every sort.
SortableList() noexcept=default
Default construct.
void operator=(const T &val)
Assignment of all entries to the given value, removing indices.
void sort()
Forward (stable) sort the list (if changed after construction).
void reverseSort()
Reverse (stable) sort the list.
void swap(SortableList< T > &other)
Swap content with another SortableList in constant time.
List< T > & shrink()
Clear the indices and return a reference to the underlying List.
void clear()
Clear the list and the indices.
void partialReverseSort(label n, label start=0)
Reverse partial sort the list until the middle point.
A 1D vector of objects of type <T>, where the size of the vector is known and can be used for subscri...
Definition UList.H:89
void swap(UList< T > &list) noexcept
Swap content with another UList of the same type in constant time.
Definition UListI.H:524
void size(const label n)
Older name for setAddressableSize.
Definition UList.H:118
UList< T > & operator=(const UList< T > &)=delete
No copy assignment (default: shallow copy).
A class representing the concept of 0 (zero) that can be used to avoid manipulating objects known to ...
Definition zero.H:58
surface1 clear()
Namespace for OpenFOAM.
labelList sortedOrder(const UList< T > &input)
Return the (stable) sort order for the list.
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...
void sort(UList< T > &list)
Sort the list.
Definition UList.C:283
void T(FieldField< Field, Type > &f1, const FieldField< Field, Type > &f2)
A list compare binary predicate for reverse sort.
Definition UList.H:255
A list compare binary predicate for normal sort.
Definition UList.H:237