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 | Copyright (C) 2011-2013 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 
88  // Member Functions
89 
90  //- Return the list of sorted indices. Updated every sort.
91  const labelList& indices() const
92  {
93  return indices_;
94  }
95 
96  //- Return non-const access to the sorted indices. Updated every sort.
98  {
99  return indices_;
100  }
101 
102  //- Clear the list and the indices
103  void clear();
104 
105  //- Clear the indices and return a reference to the underlying List
106  List<T>& shrink();
107 
108  //- (stable) sort the list (if changed after construction time)
109  // also resizes the indices as required
110  void sort();
111 
112  //- Reverse (stable) sort the list
113  void reverseSort();
114 
115  //- Transfer contents to the Xfer container as a plain List
116  inline Xfer<List<T> > xfer();
117 
118 
119  // Member Operators
120 
121  //- Assignment of all entries to the given value
122  inline void operator=(const T&);
123 
124  //- Assignment from UList operator. Takes linear time.
125  inline void operator=(const UList<T>&);
126 
127  //- Assignment operator. Takes linear time.
128  inline void operator=(const SortableList<T>&);
129 
130 };
131 
132 
133 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
134 
135 } // End namespace Foam
136 
137 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
138 
139 #ifdef NoRepository
140 # include "SortableList.C"
141 #endif
142 
143 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
144 
145 #endif
146 
147 // ************************************************************************* //
const labelList & indices() const
Return the list of sorted indices. Updated every sort.
Definition: SortableList.H:90
void T(FieldField< Field, Type > &f1, const FieldField< Field, Type > &f2)
A simple container for copying or transferring objects of type <T>.
Definition: Xfer.H:85
void operator=(const T &)
Assignment of all entries to the given value.
Definition: SortableList.C:134
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
void sort()
(stable) sort the list (if changed after construction time)
Definition: SortableList.C:95
Namespace for OpenFOAM.
void reverseSort()
Reverse (stable) sort the list.
Definition: SortableList.C:110
void clear()
Clear the list and the indices.
Definition: SortableList.C:79
A list that is sorted upon construction or when explicitly requested with the sort() method...
Definition: List.H:65
Xfer< List< T > > xfer()
Transfer contents to the Xfer container as a plain List.
Definition: SortableList.C:125
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
label size() const
Return the number of elements in the UList.
Definition: ListI.H:83
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:87
friend class List< T >
Declare friendship with the List class.
Definition: UList.H:86