FreeFOAM The Cross-Platform CFD Toolkit
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) 1991-2010 OpenCFD Ltd.
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 // * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * * //
27 
28 template<class T>
29 void Foam::SortableList<T>::sortIndices(List<label>& order) const
30 {
31  // list lengths must be identical
32  if (order.size() != this->size())
33  {
34  // avoid copying any elements, they are overwritten anyhow
35  order.clear();
36  order.setSize(this->size());
37  }
38 
39  forAll(order, elemI)
40  {
41  order[elemI] = elemI;
42  }
43  Foam::stableSort(order, typename UList<T>::less(*this));
44 }
45 
46 
47 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
48 
49 template<class T>
51 {}
52 
53 
54 template<class T>
56 :
57  List<T>(values)
58 {
59  sort();
60 }
61 
62 
63 template<class T>
65 :
66  List<T>(values)
67 {
68  sort();
69 }
70 
71 
72 template<class T>
74 :
75  List<T>(size)
76 {}
77 
78 
79 template<class T>
80 Foam::SortableList<T>::SortableList(const label size, const T& val)
81 :
82  List<T>(size, val)
83 {}
84 
85 
86 template<class T>
88 :
89  List<T>(lst),
90  indices_(lst.indices())
91 {}
92 
93 
94 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
95 
96 
97 template<class T>
99 {
100  List<T>::clear();
101  indices_.clear();
102 }
103 
104 
105 template<class T>
107 {
108  indices_.clear();
109  return static_cast<List<T>&>(*this);
110 }
111 
112 
113 template<class T>
115 {
116  sortIndices(indices_);
117 
118  List<T> lst(this->size());
119  forAll(indices_, i)
120  {
121  lst[i] = this->operator[](indices_[i]);
122  }
123 
124  List<T>::transfer(lst);
125 }
126 
127 
128 template<class T>
130 {
131  sortIndices(indices_);
132 
133  List<T> lst(this->size());
134  label endI = indices_.size();
135  forAll(indices_, i)
136  {
137  lst[--endI] = this->operator[](indices_[i]);
138  }
139 
140  List<T>::transfer(lst);
141 }
142 
143 
144 template<class T>
146 {
147  return xferMoveTo<List<T> >(*this);
148 }
149 
150 
151 // * * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * //
152 
153 template<class T>
154 inline void Foam::SortableList<T>::operator=(const T& t)
155 {
157 }
158 
159 
160 template<class T>
162 {
163  List<T>::operator=(rhs);
164  indices_.clear();
165 }
166 
167 
168 template<class T>
170 {
171  List<T>::operator=(rhs);
172  indices_ = rhs.indices();
173 }
174 
175 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
176 
177 // ************************ vim: set sw=4 sts=4 et: ************************ //