00001 // **************************************************************************** 00002 // This file is part of the Integrating Vision Toolkit (IVT). 00003 // 00004 // The IVT is maintained by the Karlsruhe Institute of Technology (KIT) 00005 // (www.kit.edu) in cooperation with the company Keyetech (www.keyetech.de). 00006 // 00007 // Copyright (C) 2014 Karlsruhe Institute of Technology (KIT). 00008 // All rights reserved. 00009 // 00010 // Redistribution and use in source and binary forms, with or without 00011 // modification, are permitted provided that the following conditions 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 // 00016 // 2. Redistributions in binary form must reproduce the above copyright 00017 // notice, this list of conditions and the following disclaimer in the 00018 // documentation and/or other materials provided with the distribution. 00019 // 00020 // 3. Neither the name of the KIT nor the names of its contributors may be 00021 // used to endorse or promote products derived from this software 00022 // without specific prior written permission. 00023 // 00024 // THIS SOFTWARE IS PROVIDED BY THE KIT AND CONTRIBUTORS “AS IS” AND ANY 00025 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 00026 // WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 00027 // DISCLAIMED. IN NO EVENT SHALL THE KIT OR CONTRIBUTORS BE LIABLE FOR ANY 00028 // DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 00029 // (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 00030 // LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 00031 // ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 00032 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 00033 // THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 00034 // **************************************************************************** 00035 // **************************************************************************** 00036 // Filename: KdPriorityQueue.h 00037 // Author: Kai Welke 00038 // Date: 14.04.2005 00039 // **************************************************************************** 00040 00041 #ifndef _KD_PRIORITY_QUEUE_H_ 00042 #define _KD_PRIORITY_QUEUE_H_ 00043 00044 00045 // **************************************************************************** 00046 // Necessary includes 00047 // **************************************************************************** 00048 00049 #include <new> // for explicitly using correct new/delete operators on VC DSPs 00050 00051 00052 00053 // **************************************************************************** 00054 // CKdPriorityQueue 00055 // **************************************************************************** 00056 00057 class CKdPriorityQueue 00058 { 00059 private: 00060 // structure for queue entry 00061 struct KdQueueEntry 00062 { 00063 float fValue; 00064 void *pMeta; 00065 }; 00066 00067 00068 public: 00069 // constructor 00070 CKdPriorityQueue(int nMaxSize) 00071 { 00072 m_nMaxSize = nMaxSize; 00073 m_nSize = 0; 00074 00075 // queue is array [1..max] of nodes 00076 m_pQueue = new KdQueueEntry[nMaxSize + 1]; 00077 } 00078 00079 // destructor 00080 ~CKdPriorityQueue() 00081 { 00082 delete [] m_pQueue; 00083 } 00084 00085 00086 // public methods 00087 void Empty() 00088 { 00089 m_nSize = 0; 00090 } 00091 00092 int GetSize() 00093 { 00094 return m_nSize; 00095 } 00096 00097 inline void Push(float fValue, void* pMeta) 00098 { 00099 // check for oveflow 00100 if (++m_nSize > m_nMaxSize) 00101 { 00102 //printf("CKdPriorityQueue: Overflow!!\n"); 00103 return; 00104 } 00105 00106 register int nPos = m_nSize; 00107 00108 while (nPos > 1) { 00109 register int nHalf = nPos / 2; 00110 00111 // stop iterating, if queue value is smaller than insert position 00112 if (m_pQueue[nHalf].fValue <= fValue) 00113 break; 00114 00115 // swap elements in queue 00116 m_pQueue[nPos] = m_pQueue[nHalf]; 00117 nPos = nHalf; 00118 } 00119 00120 // insert element 00121 m_pQueue[nPos].fValue = fValue; 00122 m_pQueue[nPos].pMeta = pMeta; 00123 } 00124 00125 inline void Pop(float& fValue,void*& pMeta) 00126 { 00127 // read minimum value 00128 fValue = m_pQueue[1].fValue; 00129 pMeta = m_pQueue[1].pMeta; 00130 00131 register float fLast = m_pQueue[m_nSize--].fValue; 00132 00133 register int nPos = 1; 00134 register int nDouble = nPos << 1; 00135 00136 // while r is still within the heap 00137 while (nDouble <= m_nSize) { 00138 // set r to smaller child of p 00139 00140 if (nDouble < m_nSize && m_pQueue[nDouble].fValue > m_pQueue[nDouble + 1].fValue) 00141 nDouble++; 00142 00143 // stop if we arrived at last 00144 if (fLast <= m_pQueue[nDouble].fValue) 00145 break; 00146 00147 // swap elements 00148 m_pQueue[nPos] = m_pQueue[nDouble]; 00149 00150 nPos = nDouble; 00151 nDouble = nPos << 1; 00152 } 00153 00154 // adjust last item 00155 m_pQueue[nPos] = m_pQueue[m_nSize + 1]; 00156 } 00157 00158 00159 private: 00160 // private attributes 00161 int m_nSize; 00162 int m_nMaxSize; 00163 00164 KdQueueEntry *m_pQueue; 00165 }; 00166 00167 00168 00169 #endif /* _KD_PRIORITY_QUEUE_H_ */