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 *****
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
51 #include "MEM_NonCopyable.h"
99 template <class HeapType>
100 class CTR_UHeap : public MEM_NonCopyable
109 return new CTR_UHeap();
117 int start = Parent(m_vector.size()-1);
118 for (i = start; i >=0; --i) {
128 // add element to vector
129 m_vector.push_back(elem);
130 base[elem].HeapPos() = m_vector.size()-1;
132 // push the element up the heap
133 UpHeap(base,m_vector.size()-1);
136 // access to the vector for initial loading of elements
151 // exchange with last element - pop last
152 // element and move up or down the heap as appropriate
153 if (m_vector.empty()) {
157 if (i != int(m_vector.size())-1) {
159 Swap(base,i,m_vector.size() - 1);
162 if (!m_vector.empty()) {
174 if (m_vector.empty()) return -1;
184 for (i = 1; i < int(m_vector.size()) ; i++) {
186 CTR_UHeapable * elem = base + m_vector[i];
187 CTR_UHeapable * p_elem = base + m_vector[Parent(i)];
189 assert(p_elem->HeapKey() >= elem->HeapKey());
190 assert(elem->HeapPos() == i);
208 std::vector<int> m_vector;
217 std::swap(m_vector[i],m_vector[j]);
219 CTR_UHeapable *heap_i = base + m_vector[i];
220 CTR_UHeapable *heap_j = base + m_vector[j];
222 // Exchange heap positions
223 heap_i->HeapPos() = i;
224 heap_j->HeapPos() = j;
252 return base[m_vector[i]].HeapKey();
260 int heap_size = m_vector.size();
266 if (l < heap_size && HeapVal(base,l) > HeapVal(base,i)) {
272 if (r < heap_size && HeapVal(base,r) > HeapVal(base,largest)) {
277 // exchange i and largest
278 Swap(base,i,largest);
279 DownHeap(base,largest);
289 // swap parents untill it's found a place in the heap < it's parent or
294 if (HeapVal(base,i) < HeapVal(base,p)) {