PixelLightAPI
.
|
00001 /*********************************************************\ 00002 * File: Map.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_MAP_H__ 00024 #define __PLCORE_CONTAINER_MAP_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 map (associative array) base template class 00046 * 00047 * @verbatim 00048 * Usage example: 00049 * HashMap<String, MyClass*> mapHashMap; // Hash table which manages MyClass pointers 00050 * MyClass *pT1=new MyClass(), *pT2=new MyClass(), *pT3=new MyClass(), *pT; // Test instances 00051 * mapHashMap.Add("Lenny", pT1); // Give pointer to T1 the name 'Lenny' 00052 * mapHashMap.Add("Barny", pT2); // Give pointer to T2 the name 'Barny' 00053 * mapHashMap.Set("Homer", pT3); // Give pointer to T3 the name 'Homer' by using the set function 00054 * pT = mapHashMap.Get("Barny"); // Give us the pointer to 'Barny' 00055 * pT = mapHashMap.Get("Homer"); // Give us the pointer to 'Homer' 00056 * pT = mapHashMap.Get("Lenny"); // Give us the pointer to 'Lenny' 00057 * @endverbatim 00058 * 00059 * @note 00060 * - The standard container iterator iterates over all values (NOT the keys!) within the map, use 00061 * the special key iterator for iterating over all keys 00062 * - Within a map, you can't expect that the iterator returns the elements always in exact the same order 00063 * - Usually map iterators are not that performance by concept, so, try to avoid to use map iterators 00064 */ 00065 template <class KeyType, class ValueType> 00066 class Map : public Iterable<ValueType> { 00067 00068 00069 //[-------------------------------------------------------] 00070 //[ Public static variables ] 00071 //[-------------------------------------------------------] 00072 public: 00073 static KeyType NullKey; /**< 'Null' key object, do NEVER EVER manipulate this object! */ 00074 00075 00076 //[-------------------------------------------------------] 00077 //[ Public virtual Map functions ] 00078 //[-------------------------------------------------------] 00079 public: 00080 /** 00081 * @brief 00082 * Clears the map 00083 */ 00084 virtual void Clear() = 0; 00085 00086 /** 00087 * @brief 00088 * Checks whether the map is complete empty 00089 * 00090 * @return 00091 * 'true' if the map is empty, else 'false' 00092 */ 00093 virtual bool IsEmpty() const = 0; 00094 00095 /** 00096 * @brief 00097 * Returns the number of elements/entries 00098 * 00099 * @return 00100 * Number of container elements/entries 00101 */ 00102 virtual uint32 GetNumOfElements() const = 0; 00103 00104 /** 00105 * @brief 00106 * Adds/puts a new element to the map 00107 * 00108 * @param[in] Key 00109 * The key of the new element 00110 * @param[in] Value 00111 * The value of the new element 00112 * 00113 * @return 00114 * 'true' if all went fine, else 'false' 00115 * 00116 * @note 00117 * - You have to check by yourself whether there's already such a key within the map 00118 * 00119 * @see 00120 * - Set() 00121 */ 00122 virtual bool Add(const KeyType &Key, const ValueType &Value) = 0; 00123 00124 /** 00125 * @brief 00126 * Replaces the value of a map element 00127 * 00128 * @param[in] Key 00129 * The key of the element 00130 * @param[in] NewValue 00131 * The new value of the element 00132 * 00133 * @return 00134 * 'true' if all went fine, else 'false' (key not found?) 00135 * 00136 * @see 00137 * - Set() 00138 */ 00139 virtual bool Replace(const KeyType &Key, const ValueType &NewValue) = 0; 00140 00141 /** 00142 * @brief 00143 * Sets (adds or replaces) the value of a map element 00144 * 00145 * @param[in] Key 00146 * The key of the element 00147 * @param[in] Value 00148 * The set value of the element 00149 * 00150 * @return 00151 * 'false' if a new element was added, 'true' if the value was replaced 00152 * 00153 * @remarks 00154 * This function is a combination of the add and replace function and 'just' assigns 00155 * a value to a given key without taking care of whether or not there's already an 00156 * element with the given key inside the map. 00157 * 00158 * @see 00159 * - Add() 00160 * - Replace() 00161 */ 00162 virtual bool Set(const KeyType &Key, const ValueType &Value) = 0; 00163 00164 /** 00165 * @brief 00166 * Removes an element from the map 00167 * 00168 * @param[in] Key 00169 * Key of the element which should be removed 00170 * 00171 * @return 00172 * 'true' if all went fine, else 'false' 00173 */ 00174 virtual bool Remove(const KeyType &Key) = 0; 00175 00176 /** 00177 * @brief 00178 * Removes all elements with a certain value from the map 00179 * 00180 * @param[in] Value 00181 * Value to look for 00182 * 00183 * @return 00184 * Number of removed elements 00185 * 00186 * @note 00187 * - For performance reasons, try to use the Remove(<Key>) function 00188 * whenever possible! 00189 */ 00190 virtual uint32 RemoveValue(const ValueType &Value) = 0; 00191 00192 /** 00193 * @brief 00194 * Returns the value of a given key 00195 * 00196 * @param[in] Key 00197 * Key to use 00198 * 00199 * @return 00200 * Reference of the value of the given key, reference to the 'Null'-object on error 00201 */ 00202 virtual const ValueType &Get(const KeyType &Key) const = 0; 00203 00204 /** 00205 * @brief 00206 * Returns the value of a given key 00207 * 00208 * @param[in] Key 00209 * Key to use 00210 * 00211 * @return 00212 * Reference of the value of the given key, reference to the 'Null'-object on error 00213 */ 00214 virtual ValueType &Get(const KeyType &Key) = 0; 00215 00216 /** 00217 * @brief 00218 * Returns a key iterator operating on the derived data structure 00219 * 00220 * @param[in] nIndex 00221 * Start index, if >= 'total number of elements' the index is set to the last valid index 00222 * 00223 * @return 00224 * Key iterator operating on the derived 00225 */ 00226 virtual Iterator<KeyType> GetKeyIterator(uint32 nIndex = 0) const = 0; 00227 00228 /** 00229 * @brief 00230 * Returns a constant key iterator operating on the derived data structure 00231 * 00232 * @param[in] nIndex 00233 * Start index, if >= 'total number of elements' the index is set to the last valid index 00234 * 00235 * @return 00236 * Constant key iterator operating on the derived 00237 */ 00238 virtual ConstIterator<KeyType> GetConstKeyIterator(uint32 nIndex = 0) const = 0; 00239 00240 /** 00241 * @brief 00242 * Returns a key iterator operating on the derived data structure and starting at the end 00243 * 00244 * @return 00245 * Key iterator operating on the derived, 00246 * 00247 * @remarks 00248 * Use this function to get a key iterator if you want to iterate in reversed order starting 00249 * at the end last element. 00250 */ 00251 virtual Iterator<KeyType> GetEndKeyIterator() const = 0; 00252 00253 /** 00254 * @brief 00255 * Returns a constant key iterator operating on the derived data structure and starting at the end 00256 * 00257 * @return 00258 * Constant key iterator operating on the derived, 00259 * 00260 * @remarks 00261 * Use this function to get a constant key iterator if you want to iterate in reversed order 00262 * starting at the end last element. 00263 */ 00264 virtual ConstIterator<KeyType> GetConstEndKeyIterator() const = 0; 00265 00266 00267 //[-------------------------------------------------------] 00268 //[ Private virtual Map functions ] 00269 //[-------------------------------------------------------] 00270 private: 00271 /** 00272 * @brief 00273 * Makes this map to a copy of another map 00274 * 00275 * @param[in] cMap 00276 * Map to copy from 00277 * 00278 * @return 00279 * Reference to this instance 00280 */ 00281 virtual Map<KeyType, ValueType> &operator =(const Map<KeyType, ValueType> &cMap) = 0; 00282 00283 00284 }; 00285 00286 00287 //[-------------------------------------------------------] 00288 //[ Public static variables ] 00289 //[-------------------------------------------------------] 00290 template <class KeyType, class ValueType> KeyType Map<KeyType, ValueType>::NullKey; 00291 00292 00293 //[-------------------------------------------------------] 00294 //[ Namespace ] 00295 //[-------------------------------------------------------] 00296 } // PLCore 00297 00298 00299 #endif // __PLCORE_CONTAINER_MAP_H__
|