00001 /*********************************************************************** 00002 * Software License Agreement (BSD License) 00003 * 00004 * Copyright 2008-2009 Marius Muja (mariusm@cs.ubc.ca). All rights reserved. 00005 * Copyright 2008-2009 David G. Lowe (lowe@cs.ubc.ca). All rights reserved. 00006 * 00007 * THE BSD LICENSE 00008 * 00009 * Redistribution and use in source and binary forms, with or without 00010 * modification, are permitted provided that the following conditions 00011 * are met: 00012 * 00013 * 1. Redistributions of source code must retain the above copyright 00014 * notice, this list of conditions and the following disclaimer. 00015 * 2. Redistributions in binary form must reproduce the above copyright 00016 * notice, this list of conditions and the following disclaimer in the 00017 * documentation and/or other materials provided with the distribution. 00018 * 00019 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 00020 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 00021 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 00022 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 00023 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 00024 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 00025 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 00026 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 00027 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 00028 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 00029 *************************************************************************/ 00030 00031 #ifndef _OPENCV_HEAP_H_ 00032 #define _OPENCV_HEAP_H_ 00033 00034 00035 #include <algorithm> 00036 using namespace std; 00037 00038 namespace cvflann 00039 { 00040 00050 template <typename T> 00051 class Heap { 00052 00057 T* heap; 00058 int length; 00059 00063 int count; 00064 00065 00066 00067 public: 00075 Heap(int size) 00076 { 00077 length = size+1; 00078 heap = new T[length]; // heap uses 1-based indexing 00079 count = 0; 00080 } 00081 00082 00087 ~Heap() 00088 { 00089 delete[] heap; 00090 } 00091 00096 int size() 00097 { 00098 return count; 00099 } 00100 00106 bool empty() 00107 { 00108 return size()==0; 00109 } 00110 00114 void clear() 00115 { 00116 count = 0; 00117 } 00118 00119 00129 void insert(T value) 00130 { 00131 /* If heap is full, then return without adding this element. */ 00132 if (count == length-1) { 00133 return; 00134 } 00135 00136 int loc = ++(count); /* Remember 1-based indexing. */ 00137 00138 /* Keep moving parents down until a place is found for this node. */ 00139 int par = loc / 2; /* Location of parent. */ 00140 while (par > 0 && value < heap[par]) { 00141 heap[loc] = heap[par]; /* Move parent down to loc. */ 00142 loc = par; 00143 par = loc / 2; 00144 } 00145 /* Insert the element at the determined location. */ 00146 heap[loc] = value; 00147 } 00148 00149 00150 00158 bool popMin(T& value) 00159 { 00160 if (count == 0) { 00161 return false; 00162 } 00163 00164 /* Switch first node with last. */ 00165 swap(heap[1],heap[count]); 00166 00167 count -= 1; 00168 heapify(1); /* Move new node 1 to right position. */ 00169 00170 value = heap[count + 1]; 00171 return true; /* Return old last node. */ 00172 } 00173 00174 00182 void heapify(int parent) 00183 { 00184 int minloc = parent; 00185 00186 /* Check the left child */ 00187 int left = 2 * parent; 00188 if (left <= count && heap[left] < heap[parent]) { 00189 minloc = left; 00190 } 00191 00192 /* Check the right child */ 00193 int right = left + 1; 00194 if (right <= count && heap[right] < heap[minloc]) { 00195 minloc = right; 00196 } 00197 00198 /* If a child was smaller, than swap parent with it and Heapify. */ 00199 if (minloc != parent) { 00200 swap(heap[parent],heap[minloc]); 00201 heapify(minloc); 00202 } 00203 } 00204 00205 }; 00206 00207 } // namespace cvflann 00208 00209 #endif //_OPENCV_HEAP_H_