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-------------------------------------------------------------------------------
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#include "ListOps.H" // For identity
30
31// * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
32
33template<class T>
34inline constexpr Foam::SortableList<T>::SortableList() noexcept
35:
36 List<T>(),
37 indices_()
38{}
39
40
41template<class T>
43:
44 List<T>(size)
45{}
46
47
48template<class T>
49inline Foam::SortableList<T>::SortableList(const label size, const Foam::zero)
50:
51 List<T>(size, Zero)
52{}
53
54
55template<class T>
56inline Foam::SortableList<T>::SortableList(const label size, const T& val)
57:
58 List<T>(size, val)
59{}
60
61
62template<class T>
64:
65 List<T>(lst),
66 indices_(lst.indices())
67{}
68
69
70template<class T>
72:
73 List<T>(std::move(lst)),
74 indices_(std::move(lst.indices_))
75{}
76
77
78template<class T>
80:
81 List<T>(values)
82{
83 sort();
84}
85
86
87template<class T>
89:
90 List<T>(std::move(values))
91{
92 sort();
93}
94
95
96template<class T>
97inline Foam::SortableList<T>::SortableList(std::initializer_list<T> values)
98:
99 List<T>(values)
100{
101 sort();
102}
103
104
105// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
106
107template<class T>
109{
111 indices_.clear();
112}
113
114
115template<class T>
117{
118 indices_.clear();
119 return static_cast<List<T>&>(*this);
120}
121
122
123template<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
133template<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
143template<class T>
144void 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 );
157
158 List<T> list(*this, indices_); // Copy with indices for mapping
159 List<T>::transfer(list);
160}
161
162
163template<class T>
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 );
177
178 List<T> list(*this, indices_); // Copy with indices for mapping
179 List<T>::transfer(list);
180}
181
182
183template<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
198template<class T>
199inline void Foam::SortableList<T>::operator=(const T& val)
200{
201 indices_.clear();
203}
204
205
206template<class T>
208{
209 indices_.clear();
211}
212
213
214template<class T>
216{
217 if (this == &lst)
218 {
219 return; // Self-assigment is a no-op
220 }
221
223 indices_ = lst.indices();
224}
225
226
227template<class T>
229{
230 indices_.clear();
232}
233
234
235template<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
248template<class T>
249inline void Foam::SortableList<T>::operator=(std::initializer_list<T> lst)
250{
252 sort();
253}
254
255
256// ************************************************************************* //
Various functions to operate on Lists.
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:77
void operator=(const UList< T > &a)
Assignment to UList operator. Takes linear time.
Definition: List.C:480
void clear()
Clear the list, i.e. set size to zero.
Definition: ListI.H:116
A list that is sorted upon construction or when explicitly requested with the sort() method.
Definition: SortableList.H:63
void partialSort(label n, label start=0)
Forward partial sort the list until the middle point.
Definition: SortableList.C:144
const labelList & indices() const noexcept
Return the list of sorted indices. Updated every sort.
Definition: SortableList.H:108
void operator=(const T &val)
Assignment of all entries to the given value, removing indices.
Definition: SortableList.C:199
void sort()
Forward (stable) sort the list (if changed after construction).
Definition: SortableList.C:124
void reverseSort()
Reverse (stable) sort the list.
Definition: SortableList.C:134
constexpr SortableList() noexcept
Default construct.
Definition: SortableList.C:34
void swap(SortableList< T > &other)
Swap content with another SortableList in constant time.
Definition: SortableList.C:184
List< T > & shrink()
Clear the indices and return a reference to the underlying List.
Definition: SortableList.C:116
void clear()
Clear the list and the indices.
Definition: SortableList.C:108
void partialReverseSort(label n, label start=0)
Reverse partial sort the list until the middle point.
Definition: SortableList.C:164
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:94
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:63
const volScalarField & T
patchWriters clear()
void identity(labelUList &map, label start=0)
Set identity map with (map[i] == i)
Definition: ListOps.C:203
labelList sortedOrder(const UList< T > &input)
Return the (stable) sort order for the list.
void sort(UList< T > &list)
Sort the list.
Definition: UList.C:342
static constexpr const zero Zero
Global zero (0)
Definition: zero.H:131
A list compare binary predicate for reverse sort.
Definition: UList.H:204
A list compare binary predicate for normal sort.
Definition: UList.H:188