PixelLightAPI
.
|
00001 /*********************************************************\ 00002 * File: BinaryHeap.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_BINARYHEAP_H__ 00024 #define __PLCORE_CONTAINER_BINARYHEAP_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 BinaryHeapIterator; 00045 00046 00047 //[-------------------------------------------------------] 00048 //[ Classes ] 00049 //[-------------------------------------------------------] 00050 /** 00051 * @brief 00052 * Binary heap (arrayed) 00053 * 00054 * @remarks 00055 * Node: Elements[i*2] Left child: Elements[i*2+1] Right child: Elements[i*2+2] 00056 */ 00057 template <class KeyType, class ValueType, class Comparer = CompareFunction > 00058 class BinaryHeap : public Heap<KeyType, ValueType> { 00059 00060 00061 //[-------------------------------------------------------] 00062 //[ Friends ] 00063 //[-------------------------------------------------------] 00064 friend class BinaryHeapIterator<KeyType, ValueType, Comparer>; 00065 00066 00067 //[-------------------------------------------------------] 00068 //[ Public functions ] 00069 //[-------------------------------------------------------] 00070 public: 00071 /** 00072 * @brief 00073 * Constructor 00074 * 00075 * @param[in] nResizeCount 00076 * Resize count 00077 */ 00078 BinaryHeap(uint32 nResizeCount = 16); 00079 00080 /** 00081 * @brief 00082 * Destructor 00083 */ 00084 virtual ~BinaryHeap(); 00085 00086 /** 00087 * @brief 00088 * Returns the number of elements automatically added if the binary heap 00089 * size is to small 00090 * 00091 * @return 00092 * Number of elements automatically added if the binary heap size is to small 00093 */ 00094 uint32 GetResizeCount() const; 00095 00096 /** 00097 * @brief 00098 * Sets the number of elements automatically added if the binary heap 00099 * size is to small 00100 * 00101 * @param[in] nCount 00102 * Number of elements automatically added if the binary heap size is to small 00103 * 00104 * @return 00105 * 'true' if all went fine, else 'false' 00106 * 00107 * @note 00108 * - If nCount is 0, the binary heap size isn't changed automatically 00109 */ 00110 bool SetResizeCount(uint32 nCount = 16); 00111 00112 /** 00113 * @brief 00114 * Resets the binary heap 00115 * 00116 * @remarks 00117 * While the Clear() function destroys also the data, this function will only 00118 * reset the current number of elements within the array to 0. 00119 */ 00120 void Reset(); 00121 00122 00123 //[-------------------------------------------------------] 00124 //[ Private functions ] 00125 //[-------------------------------------------------------] 00126 private: 00127 /** 00128 * @brief 00129 * Copy constructor 00130 * 00131 * @param[in] cSource 00132 * Source to copy from 00133 */ 00134 BinaryHeap(const BinaryHeap<KeyType, ValueType, Comparer> &cSource); 00135 00136 /** 00137 * @brief 00138 * Makes this heap to a copy of another heap 00139 * 00140 * @param[in] cHeap 00141 * 'BinaryHeap' to copy from 00142 * 00143 * @return 00144 * Reference to this instance 00145 */ 00146 Heap<KeyType, ValueType> &operator =(const BinaryHeap<KeyType, ValueType, Comparer> &cHeap); 00147 00148 /** 00149 * @brief 00150 * Returns the index of the parent 00151 * 00152 * @param[in] nIndex 00153 * Index of the current element 00154 * 00155 * @return 00156 * Index of the parent, < 0 if there's no parent 00157 */ 00158 int GetParent(uint32 nIndex) const; 00159 00160 /** 00161 * @brief 00162 * Returns the index of the left child 00163 * 00164 * @param[in] nIndex 00165 * Index of the current element 00166 * 00167 * @return 00168 * Index of the left child 00169 */ 00170 uint32 GetLeft(uint32 nIndex) const; 00171 00172 /** 00173 * @brief 00174 * Returns the index of the right child 00175 * 00176 * @param[in] nIndex 00177 * Index of the current element 00178 * 00179 * @return 00180 * Index of the right child 00181 */ 00182 uint32 GetRight(uint32 nIndex) const; 00183 00184 /** 00185 * @brief 00186 * Shift up 00187 * 00188 * @param[in] nIndex 00189 * Current index 00190 * 00191 * @return 00192 * The new index of the element 00193 */ 00194 uint32 UpHeap(uint32 nIndex); 00195 00196 /** 00197 * @brief 00198 * Shift down 00199 * 00200 * @param[in] nIndex 00201 * Current index 00202 */ 00203 void DownHeap(uint32 nIndex); 00204 00205 00206 //[-------------------------------------------------------] 00207 //[ Private structures ] 00208 //[-------------------------------------------------------] 00209 private: 00210 /** 00211 * @brief 00212 * Internal heap element 00213 */ 00214 class Element { 00215 public: 00216 KeyType Key; 00217 ValueType Value; 00218 Element &operator =(const Element &cSource) { 00219 Key = cSource.Key; 00220 Value = cSource.Value; 00221 return *this; 00222 } 00223 }; 00224 00225 00226 //[-------------------------------------------------------] 00227 //[ Private data ] 00228 //[-------------------------------------------------------] 00229 private: 00230 uint32 m_nMaxNumOfElements; /**< Maximum number of elements */ 00231 uint32 m_nNumOfElements; /**< Current number of elements */ 00232 uint32 m_nResizeCount; /**< Automatic resize count */ 00233 Element *m_pData; /**< Heap data, can be a null pointer */ 00234 00235 00236 //[-------------------------------------------------------] 00237 //[ Public virtual Iterable functions ] 00238 //[-------------------------------------------------------] 00239 public: 00240 virtual Iterator<ValueType> GetIterator(uint32 nIndex = 0) const override; 00241 virtual ConstIterator<ValueType> GetConstIterator(uint32 nIndex = 0) const override; 00242 virtual Iterator<ValueType> GetEndIterator() const override; 00243 virtual ConstIterator<ValueType> GetConstEndIterator() const override; 00244 00245 00246 //[-------------------------------------------------------] 00247 //[ Public virtual Heap functions ] 00248 //[-------------------------------------------------------] 00249 public: 00250 virtual void Clear() override; 00251 virtual bool IsEmpty() const override; 00252 virtual uint32 GetNumOfElements() const override; 00253 virtual bool Add(const KeyType &Key, const ValueType &Value) override; 00254 virtual bool GetTop(ValueType *pValue = nullptr, KeyType *pKey = nullptr) const override; 00255 virtual bool ExtractTop(ValueType *pValue = nullptr, KeyType *pKey = nullptr) override; 00256 00257 00258 //[-------------------------------------------------------] 00259 //[ Private virtual Heap functions ] 00260 //[-------------------------------------------------------] 00261 private: 00262 virtual Heap<KeyType, ValueType> &operator =(const Heap<KeyType, ValueType> &cHeap) override; 00263 00264 00265 }; 00266 00267 00268 //[-------------------------------------------------------] 00269 //[ Namespace ] 00270 //[-------------------------------------------------------] 00271 } // PLCore 00272 00273 00274 //[-------------------------------------------------------] 00275 //[ Implementation ] 00276 //[-------------------------------------------------------] 00277 #include "PLCore/Container/BinaryHeap.inl" 00278 00279 00280 #endif // __PLCORE_CONTAINER_BINARYHEAP_H__
|