1 /*---------------------------------------------------------------------------*\
3 \\ / F ield | foam-extend: Open Source CFD
4 \\ / O peration | Version: 3.2
5 \\ / A nd | Web: http://www.foam-extend.org
6 \\/ M anipulation | For copyright notice see file Copyright
7 -------------------------------------------------------------------------------
9 This file is part of foam-extend.
11 foam-extend is free software: you can redistribute it and/or modify it
12 under the terms of the GNU General Public License as published by the
13 Free Software Foundation, either version 3 of the License, or (at your
14 option) any later version.
16 foam-extend is distributed in the hope that it will be useful, but
17 WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 General Public License for more details.
21 You should have received a copy of the GNU General Public License
22 along with foam-extend. If not, see <http://www.gnu.org/licenses/>.
28 Priority list with changing priorities for inserted elements.
31 Hrvoje Jasak, Wikki Ltd. All rights reserved
33 \*---------------------------------------------------------------------------*/
35 #include "PriorityList.H"
37 // * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * //
40 void Foam::PriorityList<Type>::sortList()
42 // Reset indices and sorted indices
43 for (label i = 0; i < size_; i++)
46 sortedIndices_[i] = i;
49 for (label i = size_/2 - 1; i >= 0; i--)
51 this->bisectionSort(i);
59 void Foam::PriorityList<Type>::bisectionSort(const label startIndex)
65 // Find largest node and left and right children
73 && greater(weights_[indices_[il]], weights_[indices_[n]])
82 && greater(weights_[indices_[ir]], weights_[indices_[n]])
91 // Swap i with largest n
92 Foam::Swap(indices_[i], indices_[n]);
94 // Swap positions in sorted index list
95 sortedIndices_[indices_[i]] = i;
96 sortedIndices_[indices_[n]] = n;
98 // Update for next position
105 void Foam::PriorityList<Type>::sortUpwards(const label startIndex)
108 label i = startIndex;
112 while (i > 0 && greater(weights_[indices_[i]], weights_[indices_[n]]))
114 // Swap node i and n if not in proper order
115 Foam::Swap(indices_[i], indices_[n]);
117 // Swap positions in sorted index list
118 sortedIndices_[indices_[i]] = i;
119 sortedIndices_[indices_[n]] = n;
121 // Update for next position
128 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
130 // Construct given capacity
132 Foam::PriorityList<Type>::PriorityList(const label capacity)
136 sortedIndices_(capacity),
142 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
145 Foam::label Foam::PriorityList<Type>::removeHead()
152 label maxIndex = indices_[0];
156 Foam::Swap(indices_[0], indices_[size_]);
157 sortedIndices_[indices_[0]] = 0;
158 this->bisectionSort(0);
166 void Foam::PriorityList<Type>::set
174 // List is no longer sorted
180 void Foam::PriorityList<Type>::updateWeight
183 const Type& newWeight
191 Type delta = newWeight - weights_[i];
193 weights_[i] = newWeight;
195 if (greater(delta, pTraits<Type>::zero))
197 this->sortUpwards(sortedIndices_[i]);
201 this->bisectionSort(sortedIndices_[i]);
206 // ************************************************************************* //