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 OpenCFD Ltd.
9 -------------------------------------------------------------------------------
10 License
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"
29 
30 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
31 
32 template<class T>
34 :
36 {
37  sort();
38 }
39 
40 
41 template<class T>
42 template<class Compare>
43 inline Foam::SortList<T>::SortList(const UList<T>& values, const Compare& comp)
44 :
46 {
47  sort<Compare>(comp);
48 }
49 
50 
51 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
52 
53 template<class T>
55 {
56  return this->addressing();
57 }
58 
59 
60 template<class T>
62 {
63  return this->addressing();
64 }
65 
66 
67 template<class T>
69 {
70  Foam::reverse(this->indices());
71 }
72 
73 
74 template<class T>
76 {
77  const label len = this->values().size();
78 
79  labelList& addr = this->indices();
80  addr.resize(len);
81  ListOps::identity(addr);
82 }
83 
84 
85 template<class T>
86 template<class Compare>
87 inline void Foam::SortList<T>::sort(const Compare& comp)
88 {
89  UList<T>& vals = this->values();
90  labelList& addr = this->indices();
91 
92  if (addr.size() != vals.size())
93  {
94  addr.resize(vals.size());
95  ListOps::identity(addr);
96  }
97 
98  std::stable_sort
99  (
100  addr.begin(),
101  addr.end(),
102  [&](label a, label b) -> bool { return comp(vals[a], vals[b]); }
103  );
104 }
105 
106 
107 template<class T>
109 {
110  UList<T>& vals = this->values();
111  labelList& addr = this->indices();
112 
113  if (addr.size() != vals.size())
114  {
115  addr.resize(vals.size());
116  ListOps::identity(addr);
117  }
118 
119  // Forward sort of indices
120  std::stable_sort
121  (
122  addr.begin(),
123  addr.end(),
124  [&](label a, label b) -> bool { return vals[a] < vals[b]; }
125  );
126 }
127 
128 
129 template<class T>
131 {
132  UList<T>& vals = this->values();
133  labelList& addr = this->indices();
134 
135  if (addr.size() != vals.size())
136  {
137  addr.resize(vals.size());
138  ListOps::identity(addr);
139  }
140 
141  // Reverse sort of indices
142  std::stable_sort
143  (
144  addr.begin(),
145  addr.end(),
146  [&](label a, label b) -> bool { return vals[b] < vals[a]; }
147  );
148 }
149 
150 
151 // ************************************************************************* //
Foam::reverse
void reverse(UList< T > &list, const label n)
Definition: UListI.H:449
Foam::List::resize
void resize(const label len)
Adjust allocated size of list.
Definition: ListI.H:139
Foam::SortList::reverseSort
void reverseSort()
Reverse (stable) sort the list.
Definition: SortListI.H:130
Foam::SortList::indices
const labelUList & indices() const
Return the list of sorted indices. Updated every sort.
Definition: SortListI.H:54
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::SortList::SortList
SortList(const UList< T > &values)
Shallow copy values list reference, sort immediately.
Definition: SortListI.H:33
Foam::constant::physicoChemical::b
const dimensionedScalar b
Wien displacement law constant: default SI units: [m.K].
Definition: createFields.H:27
Foam::T
void T(FieldField< Field, Type > &f1, const FieldField< Field, Type > &f2)
Definition: FieldFieldFunctions.C:58
Foam::IndirectList
A List with indirect addressing.
Definition: IndirectList.H:56
T
const volScalarField & T
Definition: createFieldRefs.H:2
Foam::SortList::reset
void reset()
Reset list indices to identity.
Definition: SortListI.H:75
Foam::List< label >
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::identity
labelList identity(const label len, label start=0)
Create identity map of the given length with (map[i] == i)
Definition: labelList.C:38
ListOps.H
Various functions to operate on Lists.
Foam::UList::size
void size(const label n)
Older name for setAddressableSize.
Definition: UList.H:114
Foam::SortList::sort
void sort()
Definition: SortListI.H:108
Foam::SortList::reverse
void reverse()
Reverse the indices.
Definition: SortListI.H:68