PixelLightAPI
.
|
00001 /*********************************************************\ 00002 * File: Heap.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_HEAP_H__ 00024 #define __PLCORE_CONTAINER_HEAP_H__ 00025 #pragma once 00026 00027 00028 //[-------------------------------------------------------] 00029 //[ Includes ] 00030 //[-------------------------------------------------------] 00031 #include "PLCore/Container/Iterable.h" 00032 00033 00034 //[-------------------------------------------------------] 00035 //[ Namespace ] 00036 //[-------------------------------------------------------] 00037 namespace PLCore { 00038 00039 00040 //[-------------------------------------------------------] 00041 //[ Classes ] 00042 //[-------------------------------------------------------] 00043 /** 00044 * @brief 00045 * Abstract heap class template (priority queue) 00046 * 00047 * @remarks 00048 * @verbatim 00049 * Usage example: 00050 * // Create binary heap object with integer as key and pointer to characters 00051 * // as value 00052 * Heap<int, char*> cHeap; 00053 * // Fill heap 00054 * cHeap.Insert(1, "Second"); // Insert "Second" 00055 * cHeap.Insert(0, "First"); // Insert "First" 00056 * cHeap.Insert(2, "Third"); // Insert "Third" 00057 * // Read out heap 00058 * char *pT; 00059 * cHeap.Remove(pT, 0); // Returns "First" 00060 * cHeap.Remove(pT, 0); // Returns "Second" 00061 * cHeap.Remove(pT, 0); // Returns "Third" 00062 * @endverbatim 00063 */ 00064 template <class KeyType, class ValueType> 00065 class Heap : public Iterable<ValueType> { 00066 00067 00068 //[-------------------------------------------------------] 00069 //[ Public virtual Heap functions ] 00070 //[-------------------------------------------------------] 00071 public: 00072 /** 00073 * @brief 00074 * Clears the heap 00075 */ 00076 virtual void Clear() = 0; 00077 00078 /** 00079 * @brief 00080 * Checks whether the heap is complete empty 00081 * 00082 * @return 00083 * 'true' if the heap is empty, else 'false' 00084 */ 00085 virtual bool IsEmpty() const = 0; 00086 00087 /** 00088 * @brief 00089 * Returns the number of elements 00090 * 00091 * @return 00092 * Number of container elements 00093 */ 00094 virtual uint32 GetNumOfElements() const = 0; 00095 00096 /** 00097 * @brief 00098 * Adds a new element to the heap 00099 * 00100 * @param[in] Key 00101 * The key of the new element 00102 * @param[in] Value 00103 * The value of the new element 00104 * 00105 * @return 00106 * 'true' if all went fine, else 'false' 00107 * 00108 * @note 00109 * - You have to check by yourself whether there's already such a key within the heap 00110 */ 00111 virtual bool Add(const KeyType &Key, const ValueType &Value) = 0; 00112 00113 /** 00114 * @brief 00115 * Returns the value of the top element 00116 * 00117 * @param[out] pValue 00118 * If not a null pointer, this will receive the value of the top element 00119 * @param[out] pKey 00120 * If not a null pointer, this will receive the key of the top element 00121 * 00122 * @return 00123 * 'true' if all went fine, else 'false' 00124 */ 00125 virtual bool GetTop(ValueType *pValue = nullptr, KeyType *pKey = nullptr) const = 0; 00126 00127 /** 00128 * @brief 00129 * Returns the value of the top element and removes it from the heap 00130 * 00131 * @param[out] pValue 00132 * If not a null pointer, this will receive the value of the top element 00133 * @param[out] pKey 00134 * If not a null pointer, this will receive the key of the top element 00135 * 00136 * @return 00137 * 'true' if all went fine, else 'false' 00138 */ 00139 virtual bool ExtractTop(ValueType *pValue = nullptr, KeyType *pKey = nullptr) = 0; 00140 00141 00142 //[-------------------------------------------------------] 00143 //[ Private virtual Heap functions ] 00144 //[-------------------------------------------------------] 00145 private: 00146 /** 00147 * @brief 00148 * Makes this heap to a copy of another heap 00149 * 00150 * @param[in] cHeap 00151 * Heap to copy from 00152 * 00153 * @return 00154 * Reference to this instance 00155 */ 00156 virtual Heap<KeyType, ValueType> &operator =(const Heap<KeyType, ValueType> &cHeap) = 0; 00157 00158 00159 }; 00160 00161 00162 //[-------------------------------------------------------] 00163 //[ Namespace ] 00164 //[-------------------------------------------------------] 00165 } // PLCore 00166 00167 00168 #endif // __PLCORE_CONTAINER_HEAP_H__
|