00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033 #if __GNUC__ >= 3
00034 #include <ext/hash_map>
00035 #else
00036 #include <hash_map>
00037 #endif
00038
00039 #include "CollectionSerializers.h"
00040 #include "Traits.h"
00041 #include "RandomUtil.h"
00042 #include "mace_constants.h"
00043 #include "StrUtilNamespace.h"
00044
00045 #ifndef _MACE_HASH_MAP_H
00046 #define _MACE_HASH_MAP_H
00047
00053 namespace mace {
00054
00060
00061
00065 template<class Key, class Data,
00066 class Serial = SerializeMap<Key, Data>,
00067 class HashFcn = __gnu_cxx::hash<Key>,
00068 class EqualKey = std::equal_to<Key>,
00069 class Alloc = std::allocator<Data> >
00070 class hash_map : public __gnu_cxx::hash_map<Key, Data, HashFcn, EqualKey, Alloc>, public virtual Serial, public PrintPrintable {
00071
00072 BOOST_CLASS_REQUIRE2(Serial, Serializer, boost, ConvertibleConcept);
00073
00074 public:
00075 typedef typename __gnu_cxx::hash_map<Key, Data, HashFcn, EqualKey, Alloc> baseType;
00076 typedef typename baseType::iterator iterator;
00077 typedef typename baseType::const_iterator const_iterator;
00078 typedef typename baseType::size_type size_type;
00079
00081 void clear() {
00082 baseType::clear();
00083 }
00084
00086 size_t size() const {
00087 return baseType::size();
00088 }
00089
00090 virtual ~hash_map() { }
00091
00093 bool containsKey(const Key& k) const {
00094 return this->find(k) != this->end();
00095 }
00096
00098 const Data& get(const Key& k) const {
00099
00100 const_iterator i = this->find(k);
00101 if (i != this->end()) {
00102 return i->second;
00103 }
00104 ASSERT(0);
00105
00106 }
00107
00109 Data& get(const Key& k) {
00110 iterator i = this->find(k);
00111 if (i != this->end()) {
00112 return i->second;
00113 }
00114 ASSERT(0);
00115 }
00116
00118 const_iterator lower_bound(const Key& k) const {
00119
00120 ASSERT(0);
00121 }
00122
00124 const_iterator random() const {
00125 if(this->size()==0) { return this->end(); }
00126 int rand = RandomUtil::randInt(this->size());
00127 const_iterator i;
00128 int position;
00129 for(i = this->begin(), position=0; i != this->end() && position < rand; i++, position++);
00130 return i;
00131 }
00132
00134 const std::string& getTypeName() const {
00135 const char* types[] = { "Key", "Data", "Serial", "HashFcn", "EqualKey", "Alloc", 0 };
00136 static const StrUtilNamespace::StdStringList myTypes = StrUtilNamespace::getTypeFromTemplate(__PRETTY_FUNCTION__, types);
00137 static string ret = myTypes[0]+"->"+myTypes[1];
00138 return ret;
00139 }
00140
00141 void print(PrintNode& pr, const std::string& name) const {
00142 mace::printMap(pr, name, "hash_map<" + getTypeName() + ">", baseType::begin(), baseType::end());
00143 }
00144
00145 void print(std::ostream& out) const {
00146 if(mace::PRINT_TYPE_NAMES) {
00147 out << "mace::HashMap<" << getTypeName() << ">";
00148 }
00149 mace::printMap(out, this->begin(), this->end());
00150 }
00151 void printState(std::ostream& out) const {
00152 if(mace::PRINT_TYPE_NAMES) {
00153 out << "mace::HashMap<" << getTypeName() << ">";
00154 }
00155 mace::printMapState(out, this->begin(), this->end());
00156 }
00157
00158 private:
00159 class v_const_iterator : public _SerializeMap<Key, Data>::const_iterator {
00160 private:
00161 const_iterator i;
00162 const_iterator end;
00163
00164 public:
00165 bool isValid() {
00166 return i != end;
00167 }
00168 void next() {
00169 ASSERT(i != end);
00170 i++;
00171 }
00172 const Key& getKey() {
00173 return i->first;
00174 }
00175 const Data& getValue() {
00176 return i->second;
00177 }
00178
00179 v_const_iterator(const_iterator beg, const_iterator end) : i(beg), end(end) {}
00180 ~v_const_iterator() {}
00181 };
00182
00183 protected:
00184 typename _SerializeMap<Key, Data>::const_iterator* getIterator() const {
00185 return new v_const_iterator(this->begin(), this->end());
00186 }
00187 Data& insertKey(const Key& k) {
00188 return (*this)[k];
00189 }
00190 };
00191
00194 }
00195 #endif // _MACE_HASH_MAP_H