SortableList.C
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-2016 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 \*---------------------------------------------------------------------------*/
25 
26 #include "ListOps.H"
27 
28 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
29 
30 template<class T>
32 {}
33 
34 
35 template<class T>
37 :
38  List<T>(values)
39 {
40  sort();
41 }
42 
43 
44 template<class T>
46 :
47  List<T>(values)
48 {
49  sort();
50 }
51 
52 
53 template<class T>
55 :
56  List<T>(size)
57 {}
58 
59 
60 template<class T>
62 :
63  List<T>(size, val)
64 {}
65 
66 
67 template<class T>
69 :
70  List<T>(lst),
71  indices_(lst.indices())
72 {}
73 
74 
75 template<class T>
76 Foam::SortableList<T>::SortableList(std::initializer_list<T> values)
77 :
78  List<T>(values)
79 {
80  sort();
81 }
82 
83 
84 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
85 
86 
87 template<class T>
89 {
91  indices_.clear();
92 }
93 
94 
95 template<class T>
97 {
98  indices_.clear();
99  return static_cast<List<T>&>(*this);
100 }
101 
102 
103 template<class T>
105 {
106  sortedOrder(*this, indices_);
107 
108  List<T> lst(this->size());
109  forAll(indices_, i)
110  {
111  lst[i] = this->operator[](indices_[i]);
112  }
113 
114  List<T>::transfer(lst);
115 }
116 
117 
118 template<class T>
120 {
121  sortedOrder(*this, indices_, typename UList<T>::greater(*this));
122 
123  List<T> lst(this->size());
124  forAll(indices_, i)
125  {
126  lst[i] = this->operator[](indices_[i]);
127  }
128 
129  List<T>::transfer(lst);
130 }
131 
132 
133 template<class T>
135 {
136  return xferMoveTo<List<T>>(*this);
137 }
138 
139 
140 // * * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * //
141 
142 template<class T>
143 inline void Foam::SortableList<T>::operator=(const T& t)
144 {
146 }
147 
148 
149 template<class T>
151 {
152  List<T>::operator=(lst);
153  indices_.clear();
154 }
155 
156 
157 template<class T>
159 {
160  List<T>::operator=(lst);
161  indices_ = lst.indices();
162 }
163 
164 
165 template<class T>
166 inline void Foam::SortableList<T>::operator=(std::initializer_list<T> lst)
167 {
168  List<T>::operator=(lst);
169  sort();
170 }
171 
172 
173 // ************************************************************************* //
A simple container for copying or transferring objects of type <T>.
Definition: Xfer.H:85
#define forAll(list, i)
Loop across all elements in list.
Definition: UList.H:428
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
void sortedOrder(const UList< T > &, labelList &order)
Generate the (stable) sort order for the list.
SortableList()
Null constructor, sort later (eg, after assignment or transfer)
Definition: SortableList.C:31
T & operator[](const label)
Return element of UList.
Definition: UListI.H:167
List< T > & shrink()
Clear the indices and return a reference to the underlying List.
Definition: SortableList.C:96
A 1D array of objects of type <T>, where the size of the vector is known and used for subscript bound...
Definition: HashTable.H:60
A list that is sorted upon construction or when explicitly requested with the sort() method...
Definition: List.H:73
Various functions to operate on Lists.
void operator=(const T &)
Assignment of all entries to the given value.
Definition: SortableList.C:143
void clear()
Clear the list, i.e. set size to zero.
Definition: ListI.H:124
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)
const volScalarField & T
void operator=(const UList< T > &)
Assignment to UList operator. Takes linear time.
Definition: List.C:376
void reverseSort()
Reverse (stable) sort the list.
Definition: SortableList.C:119
Greater function class that can be used for sorting.
Definition: UList.H:133
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
void transfer(List< T > &)
Transfer the contents of the argument List into this list.
Definition: List.C:342
Xfer< List< T > > xfer()
Transfer contents to the Xfer container as a plain List.
Definition: SortableList.C:134