SortableList.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) 2011-2016 OpenFOAM Foundation
9  Copyright (C) 2017-2021 OpenCFD Ltd.
10 -------------------------------------------------------------------------------
11 License
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 Class
28  Foam::SortableList
29 
30 Description
31  A list that is sorted upon construction or when explicitly requested
32  with the sort() method.
33 
34  Uses the std::stable_sort() algorithm.
35 
36 Note
37  In many cases you may wish to reuse list storage.
38  The Foam::sortedOrder() function and the Foam::SortList container
39  provide two other alternatives.
40 
41 SourceFiles
42  SortableList.C
43 
44 \*---------------------------------------------------------------------------*/
45 
46 #ifndef SortableList_H
47 #define SortableList_H
48 
49 #include "labelList.H"
50 
51 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
52 
53 namespace Foam
54 {
55 
56 /*---------------------------------------------------------------------------*\
57  Class SortableList Declaration
58 \*---------------------------------------------------------------------------*/
59 
60 template<class T>
61 class SortableList
62 :
63  public List<T>
64 {
65  // Private Data
66 
67  //- Indices from last sort()
68  labelList indices_;
69 
70 
71 public:
72 
73  // Constructors
74 
75  //- Default construct
76  inline constexpr SortableList() noexcept;
77 
78  //- Construct given size, sort later.
79  // The indices remain empty until the list is sorted
80  inline explicit SortableList(const label size);
81 
82  //- Construct zero-initialized with given size, sort later.
83  // The indices remain empty until the list is sorted
84  inline SortableList(const label size, const Foam::zero);
85 
86  //- Construct given size and initial value, sorting later.
87  // The indices remain empty until the list is sorted
88  inline SortableList(const label size, const T& val);
89 
90  //- Copy construct
91  inline SortableList(const SortableList<T>& lst);
92 
93  //- Move construct
94  inline SortableList(SortableList<T>&& lst);
95 
96  //- Copy construct from UList, sorting immediately
97  explicit inline SortableList(const UList<T>& values);
98 
99  //- Move construct from List, sorting immediately
100  inline SortableList(List<T>&& values);
101 
102  //- Construct from an initializer list, sorting immediately
103  inline SortableList(std::initializer_list<T> values);
104 
105 
106  // Member Functions
107 
108  //- Return the list of sorted indices. Updated every sort
109  const labelList& indices() const
110  {
111  return indices_;
112  }
113 
114  //- Return non-const access to the sorted indices. Updated every sort
115  labelList& indices()
116  {
117  return indices_;
118  }
119 
120  //- Clear the list and the indices
121  void clear();
122 
123  //- Clear the indices and return a reference to the underlying List
124  List<T>& shrink();
125 
126  //- Forward (stable) sort the list (if changed after construction).
127  // Resizes the indices as required
128  void sort();
129 
130  //- Reverse (stable) sort the list
131  // Resizes the indices as required
132  void reverseSort();
133 
134  //- Forward partial sort the list until the middle point
135  void partialSort(label n, label start=0);
136 
137  //- Reverse partial sort the list until the middle point
138  void partialReverseSort(label n, label start=0);
139 
140  //- Swap content with another SortableList in constant time
141  inline void swap(SortableList<T>& other);
142 
143 
144  // Member Operators
145 
146  //- Assignment of all entries to the given value, removing indices.
147  inline void operator=(const T& val);
148 
149  //- Assignment to UList operator, removing indices. Takes linear time
150  inline void operator=(const UList<T>& lst);
151 
152  //- Assignment operator. Takes linear time
153  inline void operator=(const SortableList<T>& lst);
154 
155  //- Move assignment, removing indices. Takes linear time
156  inline void operator=(List<T>&& lst);
157 
158  //- Move operator (constant time)
159  inline void operator=(SortableList<T>&& lst);
160 
161  //- Assignment to an initializer list
162  void operator=(std::initializer_list<T> lst);
163 };
164 
165 
166 // * * * * * * * * * * * * * * * Global Functions * * * * * * * * * * * * * //
167 
168 // Exchange contents of lists - see SortableList::swap().
169 template<class T>
170 inline void Swap(SortableList<T>& a, SortableList<T>& b)
171 {
172  a.swap(b);
173 }
174 
175 
176 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
177 
178 } // End namespace Foam
179 
180 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
181 
182 #ifdef NoRepository
183  #include "SortableList.C"
184 #endif
185 
186 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
187 
188 #endif
189 
190 // ************************************************************************* //
Foam::labelList
List< label > labelList
A List of labels.
Definition: List.H:67
Foam::Swap
void Swap(DynamicList< T, SizeMinA > &a, DynamicList< T, SizeMinB > &b)
Definition: DynamicList.H:429
Foam::SortableList::sort
void sort()
Forward (stable) sort the list (if changed after construction).
Definition: SortableList.C:124
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::SortableList::reverseSort
void reverseSort()
Reverse (stable) sort the list.
Definition: SortableList.C:134
Foam::SortableList::partialSort
void partialSort(label n, label start=0)
Forward partial sort the list until the middle point.
Definition: SortableList.C:144
n
label n
Definition: TABSMDCalcMethod2.H:31
Foam::SortableList::indices
labelList & indices()
Return non-const access to the sorted indices. Updated every sort.
Definition: SortableList.H:114
Foam::constant::physicoChemical::b
const dimensionedScalar b
Wien displacement law constant: default SI units: [m.K].
Definition: createFields.H:27
labelList.H
Foam::T
void T(FieldField< Field, Type > &f1, const FieldField< Field, Type > &f2)
Definition: FieldFieldFunctions.C:58
Foam::SortableList::partialReverseSort
void partialReverseSort(label n, label start=0)
Reverse partial sort the list until the middle point.
Definition: SortableList.C:164
Foam::SortableList::operator=
void operator=(const T &val)
Assignment of all entries to the given value, removing indices.
Definition: SortableList.C:199
Foam::SortableList::shrink
List< T > & shrink()
Clear the indices and return a reference to the underlying List.
Definition: SortableList.C:116
Foam::SortableList
A list that is sorted upon construction or when explicitly requested with the sort() method.
Definition: List.H:60
Foam::SortableList::SortableList
constexpr SortableList() noexcept
Default construct.
Definition: SortableList.C:34
Foam
Namespace for OpenFOAM.
Definition: atmBoundaryLayer.C:33
Foam::SortableList::swap
void swap(SortableList< T > &other)
Swap content with another SortableList in constant time.
Definition: SortableList.C:184
SortableList.C
Foam::List< label >
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::SortableList::indices
const labelList & indices() const
Return the list of sorted indices. Updated every sort.
Definition: SortableList.H:108
Foam::SortableList::clear
void clear()
Clear the list and the indices.
Definition: SortableList.C:108