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-2020 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  //- Null constructor, sort later (eg, after assignment or transfer)
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 given begin/end iterators.
103  // Uses std::distance to determine the size.
104  template<class InputIterator>
105  inline SortableList(InputIterator begIter, InputIterator endIter);
106 
107  //- Construct from an initializer list, sorting immediately
108  inline SortableList(std::initializer_list<T> values);
109 
110 
111  // Member Functions
112 
113  //- Return the list of sorted indices. Updated every sort
114  const labelList& indices() const
115  {
116  return indices_;
117  }
118 
119  //- Return non-const access to the sorted indices. Updated every sort
120  labelList& indices()
121  {
122  return indices_;
123  }
124 
125  //- Clear the list and the indices
126  void clear();
127 
128  //- Clear the indices and return a reference to the underlying List
129  List<T>& shrink();
130 
131  //- Forward (stable) sort the list (if changed after construction).
132  // Resizes the indices as required
133  void sort();
134 
135  //- Reverse (stable) sort the list
136  // Resizes the indices as required
137  void reverseSort();
138 
139  //- Forward partial sort the list until the middle point
140  void partialSort(label n, label start=0);
141 
142  //- Reverse partial sort the list until the middle point
143  void partialReverseSort(label n, label start=0);
144 
145  //- Swap content with another SortableList in constant time
146  inline void swap(SortableList<T>& lst);
147 
148 
149  // Member Operators
150 
151  //- Assignment of all entries to the given value, removing indices.
152  inline void operator=(const T& val);
153 
154  //- Assignment to UList operator, removing indices. Takes linear time
155  inline void operator=(const UList<T>& lst);
156 
157  //- Assignment operator. Takes linear time
158  inline void operator=(const SortableList<T>& lst);
159 
160  //- Move assignment, removing indices. Takes linear time
161  inline void operator=(List<T>&& lst);
162 
163  //- Move operator. Takes linear time
164  inline void operator=(SortableList<T>&& lst);
165 
166  //- Assignment to an initializer list
167  void operator=(std::initializer_list<T> lst);
168 
169 };
170 
171 
172 // Global Functions
173 
174 // Exchange contents of lists - see SortableList::swap().
175 template<class T>
176 inline void Swap(SortableList<T>& a, SortableList<T>& b);
177 
178 
179 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
180 
181 } // End namespace Foam
182 
183 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
184 
185 #ifdef NoRepository
186  #include "SortableList.C"
187 #endif
188 
189 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
190 
191 #endif
192 
193 // ************************************************************************* //
Foam::labelList
List< label > labelList
A List of labels.
Definition: List.H:71
Foam::SortableList::sort
void sort()
Forward (stable) sort the list (if changed after construction).
Definition: SortableList.C:138
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:148
Foam::Swap
void Swap(DynamicList< T, SizeMin1 > &a, DynamicList< T, SizeMin2 > &b)
Definition: DynamicListI.H:913
Foam::SortableList::partialSort
void partialSort(label n, label start=0)
Forward partial sort the list until the middle point.
Definition: SortableList.C:158
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:119
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:178
Foam::SortableList::operator=
void operator=(const T &val)
Assignment of all entries to the given value, removing indices.
Definition: SortableList.C:213
Foam::SortableList::shrink
List< T > & shrink()
Clear the indices and return a reference to the underlying List.
Definition: SortableList.C:130
Foam::SortableList::swap
void swap(SortableList< T > &lst)
Swap content with another SortableList in constant time.
Definition: SortableList.C:198
Foam::SortableList
A list that is sorted upon construction or when explicitly requested with the sort() method.
Definition: List.H:63
Foam::SortableList::SortableList
constexpr SortableList() noexcept
Null constructor, sort later (eg, after assignment or transfer)
Definition: SortableList.C:34
Foam
Namespace for OpenFOAM.
Definition: atmBoundaryLayer.C:33
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:113
Foam::SortableList::clear
void clear()
Clear the list and the indices.
Definition: SortableList.C:122