PixelLightAPI
.
|
00001 /*********************************************************\ 00002 * File: FibonacciHeap.h * 00003 * 00004 * Copyright (C) 2002-2012 The PixelLight Team (http://www.pixellight.org/) 00005 * 00006 * This file is part of PixelLight. 00007 * 00008 * PixelLight is free software: you can redistribute it and/or modify 00009 * it under the terms of the GNU Lesser General Public License as published by 00010 * the Free Software Foundation, either version 3 of the License, or 00011 * (at your option) any later version. 00012 * 00013 * PixelLight is distributed in the hope that it will be useful, 00014 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00015 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00016 * GNU Lesser General Public License for more details. 00017 * 00018 * You should have received a copy of the GNU Lesser General Public License 00019 * along with PixelLight. If not, see <http://www.gnu.org/licenses/>. 00020 \*********************************************************/ 00021 00022 00023 #ifndef __PLCORE_CONTAINER_FIBONACCIHEAP_H__ 00024 #define __PLCORE_CONTAINER_FIBONACCIHEAP_H__ 00025 #pragma once 00026 00027 00028 //[-------------------------------------------------------] 00029 //[ Includes ] 00030 //[-------------------------------------------------------] 00031 #include "PLCore/Container/Heap.h" 00032 #include "PLCore/Container/Functions.h" 00033 00034 00035 //[-------------------------------------------------------] 00036 //[ Namespace ] 00037 //[-------------------------------------------------------] 00038 namespace PLCore { 00039 00040 00041 //[-------------------------------------------------------] 00042 //[ Forward declarations ] 00043 //[-------------------------------------------------------] 00044 template <class KeyType, class ValueType, class Comparer> class FibonacciHeapIterator; 00045 00046 00047 //[-------------------------------------------------------] 00048 //[ Classes ] 00049 //[-------------------------------------------------------] 00050 /** 00051 * @brief 00052 * Fibonacci heap 00053 */ 00054 template <class KeyType, class ValueType, class Comparer = CompareFunction > 00055 class FibonacciHeap : public Heap<KeyType, ValueType> { 00056 00057 00058 //[-------------------------------------------------------] 00059 //[ Friends ] 00060 //[-------------------------------------------------------] 00061 friend class FibonacciHeapIterator<KeyType, ValueType, Comparer>; 00062 00063 00064 //[-------------------------------------------------------] 00065 //[ Public functions ] 00066 //[-------------------------------------------------------] 00067 public: 00068 /** 00069 * @brief 00070 * Constructor 00071 */ 00072 FibonacciHeap(); 00073 00074 /** 00075 * @brief 00076 * Destructor 00077 */ 00078 virtual ~FibonacciHeap(); 00079 00080 /** 00081 * @brief 00082 * Returns whether the Fibonacci heap is normalized or not 00083 * 00084 * @return 00085 * 'true' if the Fibonacci heap is normalized, else 'false' 00086 * 00087 * @remarks 00088 * A Fibonacci heap is normalized if there are no tree's within the tree list 00089 * with the same degree. After 'ExtractTop()' the heap is always normalized. 00090 */ 00091 bool IsNormalized(); 00092 00093 /** 00094 * @brief 00095 * Consolidate 00096 * 00097 * @remarks 00098 * 'Cleanup' function. This function is called during the extraction of 00099 * the top element automatically. After the execution of this function the 00100 * heap is 'normalized'. 00101 * 00102 * @return 00103 * 'true' if all went fine, else 'false' (maybe consolidate is not required) 00104 */ 00105 bool Consolidate(); 00106 00107 00108 //[-------------------------------------------------------] 00109 //[ Private functions ] 00110 //[-------------------------------------------------------] 00111 private: 00112 /** 00113 * @brief 00114 * Copy constructor 00115 * 00116 * @param[in] cSource 00117 * Source to copy from 00118 */ 00119 FibonacciHeap(const FibonacciHeap<KeyType, ValueType, Comparer> &cSource); 00120 00121 /** 00122 * @brief 00123 * Makes this heap to a copy of another heap 00124 * 00125 * @param[in] cHeap 00126 * 'FibonacciHeap' to copy from 00127 * 00128 * @return 00129 * Reference to this instance 00130 */ 00131 Heap<KeyType, ValueType> &operator =(const FibonacciHeap<KeyType, ValueType, Comparer> &cHeap); 00132 00133 00134 //[-------------------------------------------------------] 00135 //[ Private classes ] 00136 //[-------------------------------------------------------] 00137 private: 00138 /** 00139 * @brief 00140 * Internal Fibonacci tree 00141 */ 00142 class Tree { 00143 00144 00145 //[-------------------------------------------------------] 00146 //[ Friends ] 00147 //[-------------------------------------------------------] 00148 friend class FibonacciHeap<KeyType, ValueType, Comparer>; 00149 friend class FibonacciHeapIterator<KeyType, ValueType, Comparer>; 00150 00151 00152 //[-------------------------------------------------------] 00153 //[ Public functions ] 00154 //[-------------------------------------------------------] 00155 public: 00156 /** 00157 * @brief 00158 * Constructor 00159 * 00160 * @param[in] Key 00161 * Key 00162 * @param[in] Value 00163 * Value 00164 */ 00165 Tree(KeyType Key, ValueType Value); 00166 00167 /** 00168 * @brief 00169 * Destructor 00170 */ 00171 ~Tree(); 00172 00173 /** 00174 * @brief 00175 * Union 00176 * 00177 * @param[in] cTree 00178 * Tree to union with this tree 00179 */ 00180 void Union(Tree &cTree); 00181 00182 00183 //[-------------------------------------------------------] 00184 //[ Private data ] 00185 //[-------------------------------------------------------] 00186 private: 00187 uint32 m_nDegree; /**< Degree of this tree node */ 00188 Tree *m_pPreviousSibling; /**< Previous sibling tree, can be a null pointer */ 00189 Tree *m_pNextSibling; /**< Next sibling tree, can be a null pointer */ 00190 Tree *m_pChild; /**< Child tree, can be a null pointer */ 00191 KeyType m_Key; /**< Key of this tree node */ 00192 ValueType m_Value; /**< Value of this tree node */ 00193 00194 00195 }; 00196 00197 00198 //[-------------------------------------------------------] 00199 //[ Private data ] 00200 //[-------------------------------------------------------] 00201 private: 00202 // General data 00203 uint32 m_nNumOfElements; /**< Current number of elements */ 00204 Tree *m_pFirst; /**< First Fibonacci tree, can be a null pointer */ 00205 Tree *m_pTop; /**< The current top Fibonacci tree, can be a null pointer (smallest/greatest key) */ 00206 // Data for cleanup 00207 uint32 m_nNumOfTrees; /**< Current number of trees ('m_pFirst' and all it's siblings) */ 00208 uint32 m_nMaxNumOfMarks; /**< Maximum number of marks */ 00209 Tree **m_ppMark; /**< Mark array, can be a null pointer (has 'm_nMaxNumOfMarks' elements) */ 00210 00211 00212 //[-------------------------------------------------------] 00213 //[ Public virtual Iterable functions ] 00214 //[-------------------------------------------------------] 00215 public: 00216 virtual Iterator<ValueType> GetIterator(uint32 nIndex = 0) const override; 00217 virtual ConstIterator<ValueType> GetConstIterator(uint32 nIndex = 0) const override; 00218 virtual Iterator<ValueType> GetEndIterator() const override; 00219 virtual ConstIterator<ValueType> GetConstEndIterator() const override; 00220 00221 00222 //[-------------------------------------------------------] 00223 //[ Public virtual Heap functions ] 00224 //[-------------------------------------------------------] 00225 public: 00226 virtual void Clear() override; 00227 virtual bool IsEmpty() const override; 00228 virtual uint32 GetNumOfElements() const override; 00229 virtual bool Add(const KeyType &Key, const ValueType &Value) override; 00230 virtual bool GetTop(ValueType *pValue = nullptr, KeyType *pKey = nullptr) const override; 00231 virtual bool ExtractTop(ValueType *pValue = nullptr, KeyType *pKey = nullptr) override; 00232 00233 00234 //[-------------------------------------------------------] 00235 //[ Private virtual Heap functions ] 00236 //[-------------------------------------------------------] 00237 private: 00238 virtual Heap<KeyType, ValueType> &operator =(const Heap<KeyType, ValueType> &cHeap) override; 00239 00240 00241 }; 00242 00243 00244 //[-------------------------------------------------------] 00245 //[ Namespace ] 00246 //[-------------------------------------------------------] 00247 } // PLCore 00248 00249 00250 //[-------------------------------------------------------] 00251 //[ Implementation ] 00252 //[-------------------------------------------------------] 00253 #include "PLCore/Container/FibonacciHeap.inl" 00254 00255 00256 #endif // __PLCORE_CONTAINER_FIBONACCIHEAP_H__
|