3 * ***** BEGIN GPL 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.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software Foundation,
17 * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19 * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
20 * All rights reserved.
22 * The Original Code is: all of this file.
24 * Contributor(s): none yet.
26 * ***** END GPL LICENSE BLOCK *****
32 * Copyright (C) 2001 NaN Technologies B.V.
35 * @mainpage CTR_UHeap an updatable heap template class (also
36 * known as an updatable priority queue)
38 * Todo: Make CTR_UHeapable a template class with m_key the
39 * template parameter, so that arbitrary value types with
40 * operators (=,>) defined can be used.
44 #ifndef NAN_INCLUDED_CTR_UHeap_h
45 #define NAN_INCLUDED_CTR_UHeap_h
49 #include "MEM_NonCopyable.h"
97 template <class HeapType>
98 class CTR_UHeap : public MEM_NonCopyable
107 return new CTR_UHeap();
115 int start = Parent(m_vector.size()-1);
116 for (i = start; i >=0; --i) {
126 // add element to vector
127 m_vector.push_back(elem);
128 base[elem].HeapPos() = m_vector.size()-1;
130 // push the element up the heap
131 UpHeap(base,m_vector.size()-1);
134 // access to the vector for initial loading of elements
149 // exchange with last element - pop last
150 // element and move up or down the heap as appropriate
151 if (m_vector.empty()) {
155 if (i != int(m_vector.size())-1) {
157 Swap(base,i,m_vector.size() - 1);
160 if (!m_vector.empty()) {
172 if (m_vector.empty()) return -1;
182 for (i = 1; i < int(m_vector.size()) ; i++) {
184 CTR_UHeapable * elem = base + m_vector[i];
185 CTR_UHeapable * p_elem = base + m_vector[Parent(i)];
187 assert(p_elem->HeapKey() >= elem->HeapKey());
188 assert(elem->HeapPos() == i);
206 std::vector<int> m_vector;
215 std::swap(m_vector[i],m_vector[j]);
217 CTR_UHeapable *heap_i = base + m_vector[i];
218 CTR_UHeapable *heap_j = base + m_vector[j];
220 // Exchange heap positions
221 heap_i->HeapPos() = i;
222 heap_j->HeapPos() = j;
250 return base[m_vector[i]].HeapKey();
258 int heap_size = m_vector.size();
264 if (l < heap_size && HeapVal(base,l) > HeapVal(base,i)) {
270 if (r < heap_size && HeapVal(base,r) > HeapVal(base,largest)) {
275 // exchange i and largest
276 Swap(base,i,largest);
277 DownHeap(base,largest);
287 // swap parents untill it's found a place in the heap < it's parent or
292 if (HeapVal(base,i) < HeapVal(base,p)) {