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 #include <map>
00034 #include "CollectionSerializers.h"
00035 #include "RandomUtil.h"
00036 #include "mace_constants.h"
00037 #include "StrUtilNamespace.h"
00038
00039 #ifndef _MACE_MAP_H
00040 #define _MACE_MAP_H
00041
00047 namespace mace {
00048
00054
00055
00059 template<class Key,
00060 class Data,
00061 class Serial = SerializeMap<Key, Data>,
00062 class Compare = std::less<Key>,
00063 class Alloc = std::allocator<std::pair<const Key, Data> > >
00064 class map : public std::map<Key, Data, Compare, Alloc>, public virtual Serial, public PrintPrintable {
00065
00066 BOOST_CLASS_REQUIRE2(Serial, Serializer, boost, ConvertibleConcept);
00067
00068 public:
00069 typedef typename std::map<Key, Data, Compare, Alloc> baseType;
00070 typedef typename baseType::iterator iterator;
00071 typedef typename baseType::const_iterator const_iterator;
00072 typedef typename baseType::size_type size_type;
00073 typedef typename baseType::key_compare key_compare;
00074
00075 public:
00076 map() : baseType() {
00077 }
00078
00079 map(const key_compare& comp) : baseType(comp) {
00080 }
00081
00082 virtual ~map() { }
00083
00085 void clear() {
00086 baseType::clear();
00087 }
00088
00090 size_t size() const {
00091 return baseType::size();
00092 }
00093
00095 bool containsKey(const Key& k) const {
00096 return baseType::find(k) != baseType::end();
00097 }
00098
00100 const Data& get(const Key& k) const {
00101
00102 const_iterator i = baseType::find(k);
00103 if (i != this->end()) {
00104 return i->second;
00105 }
00106 ABORT("Key not in map");
00107
00108 }
00109
00111 Data& get(const Key& k) {
00112 iterator i = baseType::find(k);
00113 if (i != this->end()) {
00114 return i->second;
00115 }
00116 ABORT("Key not in map");
00117 }
00118
00120 bool get(const Key& k, Data& d) const {
00121 const_iterator i = baseType::find(k);
00122 if (i != this->end()) {
00123 d = i->second;
00124 return true;
00125 }
00126 return false;
00127 }
00128
00130 void put(const Key& k, const Data& d) {
00131 (*this)[k] = d;
00132 }
00133
00135 const_iterator erase(const_iterator i) {
00136 Key k = i->first;
00137 i++;
00138 if (i == baseType::end()) {
00139 baseType::erase(k);
00140 return baseType::end();
00141 }
00142 else {
00143 Key next = i->first;
00144 baseType::erase(k);
00145 return baseType::find(next);
00146 }
00147 }
00148
00150 void erase(iterator i) {
00151 baseType::erase(i);
00152 }
00153
00155 size_t erase(Key k) {
00156 return baseType::erase(k);
00157 }
00158
00160 const std::string& getTypeName() const {
00161 const char* types[] = { "Key", "Data", "Serial", "Compare", "Alloc", 0 };
00162 static const StrUtilNamespace::StdStringList myTypes = StrUtilNamespace::getTypeFromTemplate(__PRETTY_FUNCTION__, types);
00163 static string ret = myTypes[0]+"->"+myTypes[1];
00164 return ret;
00165 }
00166
00167 void print(std::ostream& out) const {
00168 if(mace::PRINT_TYPE_NAMES) {
00169 out << "mace::Map<" << getTypeName() << ">";
00170 }
00171 mace::printMap(out, baseType::begin(), baseType::end());
00172 }
00173 void printState(std::ostream& out) const {
00174 if(mace::PRINT_TYPE_NAMES) {
00175 out << "mace::Map<" << getTypeName() << ">";
00176 }
00177 mace::printMapState(out, baseType::begin(), baseType::end());
00178 }
00179
00180 void print(PrintNode& pr, const std::string& name) const {
00181 mace::printMap(pr, name, "map<" + getTypeName() + ">", baseType::begin(), baseType::end());
00182 }
00183
00185 const_iterator random() const {
00186 if(this->size()==0) { return this->end(); }
00187 int rand = RandomUtil::randInt(this->size());
00188 const_iterator i;
00189 int position;
00190 for(i = this->begin(), position=0; i != this->end() && position < rand; i++, position++);
00191 return i;
00192 }
00193
00195 iterator random() {
00196 if(this->size()==0) { return this->end(); }
00197 int rand = RandomUtil::randInt(this->size());
00198 iterator i;
00199 int position;
00200 for(i = this->begin(), position=0; i != this->end() && position < rand; i++, position++);
00201 return i;
00202 }
00203
00204 private:
00205 class v_const_iterator : public _SerializeMap<Key, Data>::const_iterator {
00206 private:
00207 const_iterator i;
00208 const_iterator end;
00209
00210 public:
00211 bool isValid() {
00212 return i != end;
00213 }
00214 void next() {
00215 ASSERT(i != end);
00216 i++;
00217 }
00218 const Key& getKey() {
00219 return i->first;
00220 }
00221 const Data& getValue() {
00222 return i->second;
00223 }
00224
00225 v_const_iterator(const_iterator beg, const_iterator end) : i(beg), end(end) {}
00226 ~v_const_iterator() {}
00227 };
00228
00229 protected:
00230 typename _SerializeMap<Key, Data>::const_iterator* getIterator() const {
00231 return new v_const_iterator(this->begin(), this->end());
00232 }
00233 Data& insertKey(const Key& k) {
00234 return (*this)[k];
00235 }
00236
00237
00238 };
00239
00242 }
00243 #endif // _MACE_MAP_H