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-2018 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  //- Construct from transferred List, sorting immediately
74  explicit SortableList(const Xfer<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  //- Construct as copy
85  SortableList(const SortableList<T>&);
86 
87  //- Construct from an initializer list, sorting immediately
88  SortableList(std::initializer_list<T>);
89 
90 
91  // Member Functions
92 
93  //- Return the list of sorted indices. Updated every sort
94  const labelList& indices() const
95  {
96  return indices_;
97  }
98 
99  //- Return non-const access to the sorted indices. Updated every sort
100  labelList& indices()
101  {
102  return indices_;
103  }
104 
105  //- Clear the list and the indices
106  void clear();
107 
108  //- Clear the indices and return a reference to the underlying List
109  List<T>& shrink();
110 
111  //- (stable) sort the list (if changed after construction time)
112  // also resizes the indices as required
113  void sort();
114 
115  //- Reverse (stable) sort the list
116  void reverseSort();
117 
118  //- Transfer contents to the Xfer container as a plain List
119  inline Xfer<List<T>> xfer();
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  //- Assignment to an initializer list
134  void operator=(std::initializer_list<T>);
135 };
136 
137 
138 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
139 
140 } // End namespace Foam
141 
142 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
143 
144 #ifdef NoRepository
145  #include "SortableList.C"
146 #endif
147 
148 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
149 
150 #endif
151 
152 // ************************************************************************* //
A simple container for copying or transferring objects of type <T>.
Definition: Xfer.H:85
void sort()
(stable) sort the list (if changed after construction time)
Definition: SortableList.C:104
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:96
A list that is sorted upon construction or when explicitly requested with the sort() method...
Definition: List.H:73
friend class List< T >
Declare friendship with the List class.
Definition: UList.H:100
void operator=(const T &)
Assignment of all entries to the given value.
Definition: SortableList.C:143
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:61
const labelList & indices() const
Return the list of sorted indices. Updated every sort.
Definition: SortableList.H:93
void T(FieldField< Field, Type > &f1, const FieldField< Field, Type > &f2)
void reverseSort()
Reverse (stable) sort the list.
Definition: SortableList.C:119
label size() const
Return the number of elements in the UList.
Definition: ListI.H:170
void clear()
Clear the list and the indices.
Definition: SortableList.C:88
Xfer< List< T > > xfer()
Transfer contents to the Xfer container as a plain List.
Definition: SortableList.C:134
Namespace for OpenFOAM.