2 * $Id: CTR_UHeap.h 228 2002-12-26 18:25:17Z mein $
3 * ***** BEGIN GPL/BL DUAL LICENSE BLOCK *****
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * as published by the Free Software Foundation; either version 2
8 * of the License, or (at your option) any later version. The Blender
9 * Foundation also sells licenses for use in proprietary software under
10 * the Blender License. See http://www.blender.org/BL/ for information
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software Foundation,
20 * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
22 * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
23 * All rights reserved.
25 * The Original Code is: all of this file.
27 * Contributor(s): none yet.
29 * ***** END GPL/BL DUAL LICENSE BLOCK *****
34 * $Id: CTR_UHeap.h 228 2002-12-26 18:25:17Z mein $
35 * Copyright (C) 2001 NaN Technologies B.V.
38 * @mainpage CTR_UHeap an updatable heap template class (also
39 * known as an updatable priority queue)
41 * Todo: Make CTR_UHeapable a template class with m_key the
42 * template parameter, so that arbitrary value types with
43 * operators (=,>) defined can be used.
47 #ifndef NAN_INCLUDED_CTR_UHeap_h
48 #define NAN_INCLUDED_CTR_UHeap_h
52 #include "MEM_NonCopyable.h"
100 template <class HeapType
>
101 class CTR_UHeap
: public MEM_NonCopyable
110 return new CTR_UHeap();
118 int start
= Parent(m_vector
.size()-1);
119 for (i
= start
; i
>=0; --i
) {
129 // add element to vector
130 m_vector
.push_back(elem
);
131 base
[elem
].HeapPos() = m_vector
.size()-1;
133 // push the element up the heap
134 UpHeap(base
,m_vector
.size()-1);
137 // access to the vector for initial loading of elements
152 // exchange with last element - pop last
153 // element and move up or down the heap as appropriate
154 if (m_vector
.empty()) {
158 if (i
!= int(m_vector
.size())-1) {
160 Swap(base
,i
,m_vector
.size() - 1);
163 if (!m_vector
.empty()) {
175 if (m_vector
.empty()) return -1;
185 for (i
= 1; i
< int(m_vector
.size()) ; i
++) {
187 CTR_UHeapable
* elem
= base
+ m_vector
[i
];
188 CTR_UHeapable
* p_elem
= base
+ m_vector
[Parent(i
)];
190 assert(p_elem
->HeapKey() >= elem
->HeapKey());
191 assert(elem
->HeapPos() == i
);
209 std::vector
<int> m_vector
;
218 std::swap(m_vector
[i
],m_vector
[j
]);
220 CTR_UHeapable
*heap_i
= base
+ m_vector
[i
];
221 CTR_UHeapable
*heap_j
= base
+ m_vector
[j
];
223 // Exchange heap positions
224 heap_i
->HeapPos() = i
;
225 heap_j
->HeapPos() = j
;
253 return base
[m_vector
[i
]].HeapKey();
261 int heap_size
= m_vector
.size();
267 if (l
< heap_size
&& HeapVal(base
,l
) > HeapVal(base
,i
)) {
273 if (r
< heap_size
&& HeapVal(base
,r
) > HeapVal(base
,largest
)) {
278 // exchange i and largest
279 Swap(base
,i
,largest
);
280 DownHeap(base
,largest
);
290 // swap parents untill it's found a place in the heap < it's parent or
295 if (HeapVal(base
,i
) < HeapVal(base
,p
)) {