BALL
1.4.1
|
00001 // -*- Mode: C++; tab-width: 2; -*- 00002 // vi: set ts=2: 00003 // 00004 // $Id: stringHashMap.h,v 1.25 2004/05/06 21:39:37 oliver Exp $ 00005 // 00006 00007 #ifndef BALL_DATATYPE_STRINGHASHMAP_H 00008 #define BALL_DATATYPE_STRINGHASHMAP_H 00009 00010 #ifndef BALL_COMMON_H 00011 # include <BALL/common.h> 00012 #endif 00013 00014 #ifndef BALL_COMMON_HASH_H 00015 # include <BALL/COMMON/hash.h> 00016 #endif 00017 00018 #ifndef BALL_CONCEPT_VISITOR_H 00019 # include <BALL/CONCEPT/visitor.h> 00020 #endif 00021 00022 #ifndef BALL_CONCEPT_PROCESSOR_H 00023 # include <BALL/CONCEPT/processor.h> 00024 #endif 00025 00026 #ifndef BALL_DATATYPE_HASHMAP_H 00027 # include <BALL/DATATYPE/hashMap.h> 00028 #endif 00029 00030 #include <algorithm> 00031 00032 namespace BALL 00033 { 00034 00035 00039 template <typename Value> 00040 class StringHashMap 00041 : public HashMap<String, Value> 00042 { 00043 public: 00044 00045 BALL_CREATE(StringHashMap) 00046 00047 00050 00053 typedef typename HashMap<String, Value>::Iterator Iterator; 00054 00057 typedef typename HashMap<String, Value>::ConstIterator ConstIterator; 00058 00061 typedef typename HashMap<String, Value>::ValueType ValueType; 00063 00066 00070 StringHashMap() 00071 : HashMap<String, Value>() 00072 { 00073 } 00074 00079 StringHashMap(const StringHashMap& map) 00080 : HashMap<String, Value>(map) 00081 { 00082 } 00083 00087 virtual ~StringHashMap() 00088 { 00089 } 00090 00094 void destroy() 00095 { 00096 HashMap<String, Value>::clear(); 00097 } 00098 00100 00103 00108 void set(const StringHashMap& hash_map) 00109 { 00110 HashMap<String, Value>::clear(); 00111 00112 ConstIterator it = hash_map.begin(); 00113 for ( ; it != hash_map.end(); ++it) 00114 { 00115 insert(*it); 00116 } 00117 } 00118 00120 const StringHashMap& operator = (const StringHashMap& hash_map) 00121 { 00122 set(hash_map); 00123 return *this; 00124 } 00125 00127 void get(StringHashMap& hash_map) const 00128 { 00129 hash_map.set(*this); 00130 } 00131 00133 void swap(StringHashMap& hash_map) 00134 { 00135 std::swap(*this, hash_map); 00136 } 00137 00139 00142 00145 std::pair<Iterator, bool> insert(const ValueType& obj) 00146 { 00147 return HashMap<String, Value>::insert(obj); 00148 } 00149 00154 ::std::pair<Iterator, bool> insert(const String& key, const Value& value) 00155 { 00156 return HashMap<String, Value>::insert(::std::pair<String, Value>(key, value)); 00157 } 00158 00159 00164 bool remove(const String& key) 00165 { 00166 // search the key 00167 Iterator it = HashMap<String, Value>::find(key); 00168 if (it == HashMap<String, Value>::end()) 00169 { 00170 // we didn't find it.. 00171 return false; 00172 } 00173 00174 // found it: delete it 00175 HashMap<String, Value>::erase(it); 00176 00177 return true; 00178 } 00179 00182 Size getSize() const 00183 { 00184 return HashMap<String, Value>::size(); 00185 } 00186 00188 00191 00194 bool operator == (const StringHashMap<Value>& hash_map) const 00195 { 00196 return HashMap<String, Value>::operator == (hash_map); 00197 } 00198 00201 bool operator != (const StringHashMap<Value>& hash_map) const 00202 { 00203 return !(HashMap<String, Value>::operator == (hash_map)); 00204 } 00205 00208 bool has(const String& key) const 00209 { 00210 return !(HashMap<String, Value>::find(key) == HashMap<String, Value>::end()); 00211 } 00212 00216 bool isEmpty() const 00217 { 00218 return (HashMap<String, Value>::size() == 0); 00219 } 00221 00224 00229 void host(Visitor<StringHashMap<Value> >& visitor) 00230 { 00231 visitor.visit(*this); 00232 } 00234 00235 }; 00236 00237 // required for visual studio 00238 #ifdef BALL_COMPILER_MSVC 00239 template class BALL_EXPORT StringHashMap<String>; 00240 #endif 00241 00242 }// namespace BALL 00243 00244 #endif // BALL_DATATYPE_HASHMAP_H