Map.h revision 10447:a465576671d4
1#ifndef __MAP_H__ 2#define __MAP_H__ 3 4#include <iostream> 5#include <map> 6 7#include "String.h" 8#include "Assert.h" 9 10namespace LibUtil 11{ 12 using std::map; 13 14 template<class T> class Map 15 { 16 public: 17 typedef typename map<String, T>::iterator Iterator; 18 typedef typename map<String, T>::const_iterator ConstIterator; 19 typedef typename map<String, T>::size_type SizeType; 20 21 public: 22 Map(); 23 virtual ~Map(); 24 25 public: 26 // Return a new copy of this Map instance 27 Map* clone() const; 28 // Copy map_ to this instance 29 void copyFrom(const Map<T>* map_); 30 // Return the size of the map 31 SizeType size() const; 32 // Check if the map is empty 33 bool isEmpty() const; 34 // Check if the key exists 35 bool keyExist(const String& key_) const; 36 // Get the value_ corresponding to the key_ 37 const T& get(const String& key_) const; 38 // Get the value_ corresponding to the key_ if the key_ exist, otherwise, the default_value_is returned 39 const T& getIfKeyExist(const String& key_, const T& default_value_ = T()) const; 40 // Add/Update a <key_, value_> entry 41 void set(const String& key_, const T& value_); 42 // Get iterator to the element 43 Iterator find(const String& key_); 44 ConstIterator find(const String& key_) const; 45 // Remove an entry corresponding to key_ 46 void remove(const String& key_); 47 // Remove an entry at 'it' 48 void remove(Iterator it); 49 // Remove all keys 50 void clear(); 51 // Merge a map. Values with same key will be overwritten. 52 void merge(const Map<T>* map_); 53 // Returns a MapIterator referring to the first element in the map 54 Iterator begin(); 55 ConstIterator begin() const; 56 // Returns a MapIterator referring to the past-the-end element in the map 57 Iterator end(); 58 ConstIterator end() const; 59 60 protected: 61 Map(const Map& map_); 62 63 protected: 64 map<String, T> mMap; 65 }; 66 67 template<class T> Map<T>::Map() 68 {} 69 70 template<class T> Map<T>::~Map() 71 {} 72 73 template<class T> Map<T>* Map<T>::clone() const 74 { 75 return new Map<T>(*this); 76 } 77 78 template<class T> void Map<T>::copyFrom(const Map<T>* map_) 79 { 80 // Remove all keys (it won't free the content if T is a pointer) 81 mMap.clear(); 82 83 // Copy the contents 84 mMap = map_->mMap; 85 } 86 87 template<class T> typename Map<T>::SizeType Map<T>::size() const 88 { 89 return mMap.size(); 90 } 91 92 template<class T> bool Map<T>::isEmpty() const 93 { 94 return (mMap.empty()); 95 } 96 97 template<class T> bool Map<T>::keyExist(const String& key_) const 98 { 99 ConstIterator it = mMap.find(key_); 100 return (it != mMap.end()); 101 } 102 103 template<class T> const T& Map<T>::get(const String& key_) const 104 { 105 ConstIterator it; 106 107 it = mMap.find(key_); 108 ASSERT((it != mMap.end()), "Key not found: " + key_); 109 return (it->second); 110 } 111 112 template<class T> const T& Map<T>::getIfKeyExist(const String& key_, const T& default_value_) const 113 { 114 if(keyExist(key_)) 115 { 116 return get(key_); 117 } 118 else 119 { 120 return default_value_; 121 } 122 } 123 124 template<class T> void Map<T>::set(const String& key_, const T& value_) 125 { 126 mMap[key_] = value_; 127 return; 128 } 129 130 template<class T> typename Map<T>::Iterator Map<T>::find(const String& key_) 131 { 132 return mMap.find(key_); 133 } 134 135 template<class T> typename Map<T>::ConstIterator Map<T>::find(const String& key_) const 136 { 137 return mMap.find(key_); 138 } 139 140 template<class T> void Map<T>::remove(const String& key_) 141 { 142 mMap.erase(key_); 143 return; 144 } 145 146 template<class T> void Map<T>::remove(Iterator it) 147 { 148 mMap.erase(it); 149 return; 150 } 151 152 template<class T> void Map<T>::clear() 153 { 154 mMap.clear(); 155 return; 156 } 157 158 template<class T> void Map<T>::merge(const Map<T>* map_) 159 { 160 ConstIterator it; 161 for(it = map_->begin(); it != map_->end(); it++) 162 { 163 const String& key = it->first; 164 const T& value = it->second; 165 set(key, value); 166 } 167 return; 168 } 169 170 template<class T> typename Map<T>::Iterator Map<T>::begin() 171 { 172 return mMap.begin(); 173 } 174 175 template<class T> typename Map<T>::ConstIterator Map<T>::begin() const 176 { 177 return mMap.begin(); 178 } 179 180 template<class T> typename Map<T>::Iterator Map<T>::end() 181 { 182 return mMap.end(); 183 } 184 185 template<class T> typename Map<T>::ConstIterator Map<T>::end() const 186 { 187 return mMap.end(); 188 } 189 190 inline std::ostream& operator<<(std::ostream& ost_, const Map<String>& map_) 191 { 192 Map<String>::ConstIterator it; 193 for(it = map_.begin(); it != map_.end(); it++) 194 { 195 ost_ << it->first << " = " << it->second << std::endl; 196 } 197 return ost_; 198 } 199 200 template<class T> Map<T>::Map(const Map<T>& map_) 201 : mMap(map_.mMap) 202 {} 203 204 typedef Map<String> StringMap; 205 206 207 // Handy function to delete all pointers in a map 208 template<class T> void clearPtrMap(Map<T*>* map_) 209 { 210 for(typename Map<T*>::Iterator it = map_->begin(); it != map_->end(); ++it) 211 { 212 T* temp_T = it->second; 213 delete temp_T; 214 } 215 map_->clear(); 216 return; 217 } 218 219 // Handy function to delete all pointers in a map and the map itself 220 template<class T> void deletePtrMap(Map<T*>* map_) 221 { 222 clearPtrMap<T>(map_); 223 delete map_; 224 return; 225 } 226 227 // Handy function to clone all pointers in a map 228 template<class T> Map<T*>* clonePtrMap(const Map<T*>* map_) 229 { 230 Map<T*>* new_T_map = new Map<T*>; 231 for(typename Map<T*>::ConstIterator it = map_->begin(); it != map_->end(); ++it) 232 { 233 const String& temp_name = it->first; 234 const T* temp_T = it->second; 235 new_T_map->set(temp_name, temp_T->clone()); 236 } 237 return new_T_map; 238 } 239} 240 241#endif // __MAP_H__ 242 243