SortableList.H
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-2021 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 Class
25  Foam::SortableList
26 
27 Description
28  A list that is sorted upon construction or when explicitly requested
29  with the sort() method.
30 
31  Uses the Foam::stableSort() algorithm.
32 
33 SourceFiles
34  SortableList.C
35 
36 \*---------------------------------------------------------------------------*/
37 
38 #ifndef SortableList_H
39 #define SortableList_H
40 
41 #include "labelList.H"
42 
43 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
44 
45 namespace Foam
46 {
47 
48 /*---------------------------------------------------------------------------*\
49  Class SortableList Declaration
50 \*---------------------------------------------------------------------------*/
51 
52 template<class T>
53 class SortableList
54 :
55  public List<T>
56 {
57  // Private Data
58 
59  //- Original indices
60  labelList indices_;
61 
62 
63 public:
64 
65  // Constructors
66 
67  //- Null constructor, sort later (eg, after assignment or transfer)
68  SortableList();
69 
70  //- Construct from UList, sorting immediately
71  explicit SortableList(const UList<T>&);
72 
73  //- Move constructor transferring List, sorting immediately
74  explicit SortableList(List<T>&&);
75 
76  //- Construct given size. Sort later on
77  // The indices remain empty until the list is sorted
78  explicit SortableList(const label size);
79 
80  //- Construct given size and initial value. Sort later on
81  // The indices remain empty until the list is sorted
82  SortableList(const label size, const T&);
83 
84  //- Copy constructor
85  SortableList(const SortableList<T>&);
86 
87  //- Move constructor
88  SortableList(SortableList<T>&&);
89 
90  //- Construct from an initialiser list, sorting immediately
91  SortableList(std::initializer_list<T>);
92 
93 
94  // Member Functions
95 
96  //- Return the list of sorted indices. Updated every sort
97  const labelList& indices() const
98  {
99  return indices_;
100  }
101 
102  //- Return non-const access to the sorted indices. Updated every sort
103  labelList& indices()
104  {
105  return indices_;
106  }
107 
108  //- Clear the list and the indices
109  void clear();
110 
111  //- Clear the indices and return a reference to the underlying List
112  List<T>& shrink();
113 
114  //- (stable) sort the list (if changed after construction time)
115  // also resizes the indices as required
116  void sort();
117 
118  //- Reverse (stable) sort the list
119  void reverseSort();
120 
121 
122  // Member Operators
123 
124  //- Assignment of all entries to the given value
125  inline void operator=(const T&);
126 
127  //- Assignment to UList operator. Takes linear time
128  inline void operator=(const UList<T>&);
129 
130  //- Assignment operator. Takes linear time
131  inline void operator=(const SortableList<T>&);
132 
133  //- Move assignment operator
134  inline void operator=(SortableList<T>&&);
135 
136  //- Assignment to an initialiser list
137  void operator=(std::initializer_list<T>);
138 };
139 
140 
141 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
142 
143 } // End namespace Foam
144 
145 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
146 
147 #ifdef NoRepository
148  #include "SortableList.C"
149 #endif
150 
151 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
152 
153 #endif
154 
155 // ************************************************************************* //
void sort()
(stable) sort the list (if changed after construction time)
Definition: SortableList.C:112
intWM_LABEL_SIZE_t label
A label is an int32_t or int64_t as specified by the pre-processor macro WM_LABEL_SIZE.
Definition: label.H:59
SortableList()
Null constructor, sort later (eg, after assignment or transfer)
Definition: SortableList.C:31
List< T > & shrink()
Clear the indices and return a reference to the underlying List.
Definition: SortableList.C:104
A list that is sorted upon construction or when explicitly requested with the sort() method...
Definition: List.H:80
friend class List< T >
Declare friendship with the List class.
Definition: UList.H:102
void operator=(const T &)
Assignment of all entries to the given value.
Definition: SortableList.C:144
List< label > labelList
A List of labels.
Definition: labelList.H:56
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 reverseSort()
Reverse (stable) sort the list.
Definition: SortableList.C:127
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
Namespace for OpenFOAM.