Loading...
Searching...
No Matches
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-------------------------------------------------------------------------------
11License
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
27Class
28 Foam::SortableList
29
30Description
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
36Note
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
41SourceFiles
42 SortableList.C
43
44\*---------------------------------------------------------------------------*/
45
46#ifndef Foam_SortableList_H
47#define Foam_SortableList_H
48
49#include "labelList.H"
50
51// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
52
53namespace Foam
54{
55
56/*---------------------------------------------------------------------------*\
57 Class SortableList Declaration
58\*---------------------------------------------------------------------------*/
59
60template<class T>
61class SortableList
62:
63 public List<T>
64{
65 // Private Data
66
67 //- Indices from last sort()
68 labelList indices_;
69
70
71public:
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, 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// ************************************************************************* //
label n
A 1D array of objects of type <T>, where the size of the vector is known and used for subscript bound...
Definition List.H:72
A list that is sorted upon construction or when explicitly requested with the sort() method.
void partialSort(label n, label start=0)
Forward partial sort the list until the middle point.
const labelList & indices() const noexcept
Return the list of sorted indices. Updated every sort.
labelList & indices() noexcept
Return non-const access to the sorted indices. Updated every sort.
SortableList() noexcept=default
Default construct.
void operator=(const T &val)
Assignment of all entries to the given value, removing indices.
void sort()
Forward (stable) sort the list (if changed after construction).
void reverseSort()
Reverse (stable) sort the list.
void swap(SortableList< T > &other)
Swap content with another SortableList in constant time.
List< T > & shrink()
Clear the indices and return a reference to the underlying List.
void clear()
Clear the list and the indices.
void partialReverseSort(label n, label start=0)
Reverse partial sort the list until the middle point.
A 1D vector of objects of type <T>, where the size of the vector is known and can be used for subscri...
Definition UList.H:89
void size(const label n)
Older name for setAddressableSize.
Definition UList.H:118
A class representing the concept of 0 (zero) that can be used to avoid manipulating objects known to ...
Definition zero.H:58
Namespace for OpenFOAM.
List< label > labelList
A List of labels.
Definition List.H:62
const direction noexcept
Definition scalarImpl.H:265
void T(FieldField< Field, Type > &f1, const FieldField< Field, Type > &f2)