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-2022 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 Foam_SortableList_H
47 #define Foam_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  SortableList() noexcept = default;
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 noexcept
110  {
111  return indices_;
112  }
113 
114  //- Return non-const access to the sorted indices. Updated every sort
116  {
117  return indices_;
118  }
119 
120  //- Clear the list and the indices
121  inline void clear();
122 
123  //- Clear the indices and return a reference to the underlying List
124  inline List<T>& shrink();
125 
126  //- Forward (stable) sort the list (if changed after construction).
127  // Resizes the indices as required
128  void sort();
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 // Does not need std::swap or Foam::Swap() specialization
169 // since SortableList is MoveConstructible and MoveAssignable
170 
171 
172 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
173 
174 } // End namespace Foam
175 
176 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
177 
178 #ifdef NoRepository
179  #include "SortableList.C"
180 #endif
181 
182 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
183 
184 #endif
185 
186 // ************************************************************************* //
SortableList() noexcept=default
Default construct.
void sort()
Forward (stable) sort the list (if changed after construction).
Definition: SortableList.C:107
List< T > & shrink()
Clear the indices and return a reference to the underlying List.
Definition: SortableList.C:99
A 1D array of objects of type <T>, where the size of the vector is known and used for subscript bound...
Definition: BitOps.H:56
A list that is sorted upon construction or when explicitly requested with the sort() method...
Definition: SortableList.H:56
List< T > values(const HashTable< T, Key, Hash > &tbl, const bool doSort=false)
List of values from HashTable, optionally sorted.
Definition: HashOps.H:164
void operator=(const T &val)
Assignment of all entries to the given value, removing indices.
Definition: SortableList.C:182
void partialReverseSort(label n, label start=0)
Reverse partial sort the list until the middle point.
Definition: SortableList.C:147
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:105
const direction noexcept
Definition: Scalar.H:258
label size() const noexcept
The number of elements in the container.
Definition: UList.H:671
void T(FieldField< Field, Type > &f1, const FieldField< Field, Type > &f2)
void reverseSort()
Reverse (stable) sort the list.
Definition: SortableList.C:117
const labelList & indices() const noexcept
Return the list of sorted indices. Updated every sort.
Definition: SortableList.H:129
A class representing the concept of 0 (zero) that can be used to avoid manipulating objects known to ...
Definition: zero.H:57
label n
void swap(SortableList< T > &other)
Swap content with another SortableList in constant time.
Definition: SortableList.C:167
void clear()
Clear the list and the indices.
Definition: SortableList.C:91
void partialSort(label n, label start=0)
Forward partial sort the list until the middle point.
Definition: SortableList.C:127
Namespace for OpenFOAM.