SortListI.H
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) 2019-2022 OpenCFD Ltd.
9-------------------------------------------------------------------------------
10License
11 This file is part of OpenFOAM.
12
13 OpenFOAM is free software: you can redistribute it and/or modify it
14 under the terms of the GNU General Public License as published by
15 the Free Software Foundation, either version 3 of the License, or
16 (at your option) any later version.
17
18 OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
19 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
20 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
21 for more details.
22
23 You should have received a copy of the GNU General Public License
24 along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
25
26\*---------------------------------------------------------------------------*/
27
28#include "ListOps.H" // For identity
29
30// * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
31
32template<class T>
34:
35 IndirectList<T>(values, labelList())
36{
37 sort();
38}
39
40
41template<class T>
42template<class Compare>
43inline Foam::SortList<T>::SortList(const UList<T>& values, const Compare& comp)
44:
45 IndirectList<T>(values, labelList())
46{
47 sort<Compare>(comp);
48}
49
50
51// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
52
53template<class T>
55{
56 return this->addressing();
57}
58
59
60template<class T>
62{
63 return this->addressing();
64}
65
66
67template<class T>
69{
70 Foam::reverse(this->indices());
71}
72
73
74template<class T>
76{
77 auto& addr = this->indices();
78
79 addr.resize_nocopy(this->values().size());
81}
82
83
84template<class T>
85template<class Compare>
86inline void Foam::SortList<T>::sort(const Compare& comp)
87{
88 auto& vals = this->values();
89 auto& addr = this->indices();
90
91 addr.resize_nocopy(vals.size());
93
94 std::stable_sort
95 (
96 addr.begin(),
97 addr.end(),
98 [&](label a, label b) -> bool { return comp(vals[a], vals[b]); }
99 );
100}
101
102
103template<class T>
105{
106 Foam::sortedOrder(this->values(), this->indices());
107}
108
109
110template<class T>
112{
113 Foam::uniqueOrder(this->values(), this->indices());
114}
115
116
117template<class T>
119{
120 // Reverse sorted order for indices
122 (
123 this->values(),
124 this->indices(),
125 typename UList<T>::greater(this->values())
126 );
127}
128
129
130// ************************************************************************* //
Various functions to operate on Lists.
A List with indirect addressing.
Definition: IndirectList.H:119
An indirect list with addressing based on sorting. The list is sorted upon construction or when expli...
Definition: SortList.H:57
void reverse()
Reverse the indices.
Definition: SortListI.H:68
const labelUList & indices() const noexcept
Return the list of sorted indices (updated every sort).
Definition: SortListI.H:54
void reverseSort()
Definition: SortListI.H:118
void uniqueSort()
Sort the list, only retaining unique entries.
Definition: SortListI.H:111
void reset()
Reset list indices to identity.
Definition: SortListI.H:75
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
const volScalarField & T
void identity(labelUList &map, label start=0)
Set identity map with (map[i] == i)
Definition: ListOps.C:203
void reverse(UList< T > &list, const label n)
Reverse the first n elements of the list.
Definition: UListI.H:449
labelList sortedOrder(const UList< T > &input)
Return the (stable) sort order for the list.
const direction noexcept
Definition: Scalar.H:223
labelList uniqueOrder(const UList< T > &input)
Return (sorted) indices corresponding to unique list values.
volScalarField & b
Definition: createFields.H:27
A list compare binary predicate for reverse sort.
Definition: UList.H:204