00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019 #ifndef __UT_AssocArray__
00020 #define __UT_AssocArray__
00021
00022 #include "UT_API.h"
00023
00024
00025 #ifdef WIN32
00026 #include <hash_map>
00027 using stdext::hash_map;
00028 #endif
00029
00030 #if defined(LINUX) || defined(MBSD)
00031 #include <ext/hash_map>
00032 using __gnu_cxx::hash_map;
00033
00034 namespace __gnu_cxx
00035 {
00036 template<typename T>
00037 struct hash<T *>
00038 {
00039 size_t operator()(T *x) const
00040 { return reinterpret_cast<size_t>(x); }
00041 };
00042 };
00043 #endif
00044
00045 template<typename Key, typename Value>
00046 class UT_AssocArray
00047 {
00048 public:
00049
00050 typedef typename hash_map<Key, Value>::iterator iterator;
00051 typedef typename hash_map<Key, Value>::const_iterator const_iterator;
00052
00053 UT_AssocArray() {}
00054 ~UT_AssocArray() {}
00055
00056
00057 unsigned entries() const
00058 { return myHashMap.size(); }
00059
00060
00061 bool empty() const
00062 { return myHashMap.empty(); }
00063
00064
00065 void clear()
00066 { myHashMap.clear(); }
00067
00068
00069
00070
00071
00072 Value &operator[](const Key & key)
00073 {
00074 return myHashMap[key];
00075 }
00076
00077
00078
00079 const Value &operator()(const Key & key) const
00080 {
00081 typename hash_map<Key, Value>::const_iterator elem = myHashMap.find(key);
00082 UT_ASSERT_P(elem != myHashMap.end());
00083 return elem->second;
00084 }
00085 Value &operator()(const Key & key)
00086 {
00087 typename hash_map<Key, Value>::const_iterator elem = myHashMap.find(key);
00088 UT_ASSERT_P(elem != myHashMap.end());
00089 return elem->second;
00090 }
00091
00092
00093 void set(const Key & key, const Value & data)
00094 {
00095 if (myHashMap.find(key) != myHashMap.end())
00096 {
00097
00098 }
00099 myHashMap[key] = data;
00100 }
00101
00102
00103
00104
00105
00106 bool find(const Key & key, Value & value) const
00107 {
00108 const_iterator elem = myHashMap.find(key);
00109 if (elem != myHashMap.end())
00110 {
00111 value = elem->second;
00112 return true;
00113 }
00114 return false;
00115 }
00116
00117
00118
00119 const_iterator find(const Key &key) const
00120 { return myHashMap.find(key); }
00121
00122
00123
00124 bool remove(const Key & key)
00125 { return myHashMap.erase(key) != 0; }
00126
00127
00128
00129
00130
00131
00132
00133
00134
00135
00136
00137
00138 iterator begin()
00139 { return myHashMap.begin(); }
00140 iterator end()
00141 { return myHashMap.end(); }
00142
00143 const_iterator begin() const
00144 { return myHashMap.begin(); }
00145 const_iterator end() const
00146 { return myHashMap.end(); }
00147
00148 private:
00149 hash_map<Key, Value> myHashMap;
00150 };
00151
00152 #endif
00153