SortableList.C
Go to the documentation of this file.
1 /*---------------------------------------------------------------------------*\
2  ========= |
3  \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
4  \\ / O peration | Website: https://openfoam.org
5  \\ / A nd | Copyright (C) 2011-2019 OpenFOAM Foundation
6  \\/ M anipulation |
7 -------------------------------------------------------------------------------
8 License
9  This file is part of OpenFOAM.
10 
11  OpenFOAM is free software: you can redistribute it and/or modify it
12  under the terms of the GNU General Public License as published by
13  the Free Software Foundation, either version 3 of the License, or
14  (at your option) any later version.
15 
16  OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
17  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
18  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
19  for more details.
20 
21  You should have received a copy of the GNU General Public License
22  along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
23 
24 \*---------------------------------------------------------------------------*/
25 
26 #include "ListOps.H"
27 
28 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
29 
30 template<class T>
32 {}
33 
34 
35 template<class T>
37 :
38  List<T>(values)
39 {
40  sort();
41 }
42 
43 
44 template<class T>
46 :
47  List<T>(move(values))
48 {
49  sort();
50 }
51 
52 
53 template<class T>
55 :
56  List<T>(size)
57 {}
58 
59 
60 template<class T>
62 :
63  List<T>(size, val)
64 {}
65 
66 
67 template<class T>
69 :
70  List<T>(lst),
71  indices_(lst.indices())
72 {}
73 
74 
75 template<class T>
77 :
78  List<T>(move(lst)),
79  indices_(move(lst.indices()))
80 {}
81 
82 
83 template<class T>
84 Foam::SortableList<T>::SortableList(std::initializer_list<T> values)
85 :
86  List<T>(values)
87 {
88  sort();
89 }
90 
91 
92 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
93 
94 
95 template<class T>
97 {
99  indices_.clear();
100 }
101 
102 
103 template<class T>
105 {
106  indices_.clear();
107  return static_cast<List<T>&>(*this);
108 }
109 
110 
111 template<class T>
113 {
114  sortedOrder(*this, indices_);
115 
116  List<T> lst(this->size());
117  forAll(indices_, i)
118  {
119  lst[i] = this->operator[](indices_[i]);
120  }
121 
122  List<T>::transfer(lst);
123 }
124 
125 
126 template<class T>
128 {
129  sortedOrder(*this, indices_, typename UList<T>::greater(*this));
130 
131  List<T> lst(this->size());
132  forAll(indices_, i)
133  {
134  lst[i] = this->operator[](indices_[i]);
135  }
136 
137  List<T>::transfer(lst);
138 }
139 
140 
141 // * * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * //
142 
143 template<class T>
144 inline void Foam::SortableList<T>::operator=(const T& t)
145 {
147 }
148 
149 
150 template<class T>
152 {
153  List<T>::operator=(lst);
154  indices_.clear();
155 }
156 
157 
158 template<class T>
160 {
161  List<T>::operator=(lst);
162  indices_ = lst.indices();
163 }
164 
165 
166 template<class T>
168 {
169  List<T>::operator=(move(lst));
170  indices_ = move(lst.indices());
171 }
172 
173 
174 template<class T>
175 inline void Foam::SortableList<T>::operator=(std::initializer_list<T> lst)
176 {
177  List<T>::operator=(lst);
178  sort();
179 }
180 
181 
182 // ************************************************************************* //
#define forAll(list, i)
Loop across all elements in list.
Definition: UList.H:434
void sort()
(stable) sort the list (if changed after construction time)
Definition: SortableList.C:112
void sortedOrder(const UList< T > &, labelList &order)
Generate the (stable) sort order for the list.
SortableList()
Null constructor, sort later (eg, after assignment or transfer)
Definition: SortableList.C:31
T & operator[](const label)
Return element of UList.
Definition: UListI.H:167
List< T > & shrink()
Clear the indices and return a reference to the underlying List.
Definition: SortableList.C:104
A 1D array of objects of type <T>, where the size of the vector is known and used for subscript bound...
Definition: HashTable.H:59
A list that is sorted upon construction or when explicitly requested with the sort() method...
Definition: List.H:80
Various functions to operate on Lists.
void operator=(const T &)
Assignment of all entries to the given value.
Definition: SortableList.C:144
void clear()
Clear the list, i.e. set size to zero.
Definition: ListI.H:125
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:60
const labelList & indices() const
Return the list of sorted indices. Updated every sort.
Definition: SortableList.H:96
void T(FieldField< Field, Type > &f1, const FieldField< Field, Type > &f2)
void operator=(const UList< T > &)
Assignment to UList operator. Takes linear time.
Definition: List.C:376
const volScalarField & T
void reverseSort()
Reverse (stable) sort the list.
Definition: SortableList.C:127
Greater function class that can be used for sorting.
Definition: UList.H:135
label size() const
Return the number of elements in the UList.
Definition: ListI.H:171
void clear()
Clear the list and the indices.
Definition: SortableList.C:96
void transfer(List< T > &)
Transfer the contents of the argument List into this list.
Definition: List.C:342