00001 #ifndef _TEMPLATE_MAP_H
00002 #define _TEMPLATE_MAP_H
00003
00004 #include <cassert>
00005 #include "basemap.h"
00006 #include "massert.h"
00007
00013 namespace mace {
00014
00020
00021 template<typename T>
00022 class TemplateMap : public T,
00023 public virtual Map<typename T::key_type,
00024 typename T::mapped_type> {
00025 public:
00026 typedef typename T::key_type Key;
00027 typedef typename T::mapped_type Data;
00028 typedef Map<Key, Data> baseMap;
00029 typedef typename baseMap::const_iterator const_iterator;
00030 typedef typename baseMap::inner_iterator inner_iterator;
00031 typedef typename T::const_iterator base_const_iterator;
00032
00033 public:
00034 class _inner_iterator : public virtual inner_iterator {
00035 friend class TemplateMap<T>;
00036
00037 public:
00038 typedef typename baseMap::ipair ipair;
00039 typedef typename baseMap::const_key_ipair const_key_ipair;
00040
00041 private:
00042 mutable base_const_iterator i;
00043
00044 private:
00045 _inner_iterator(base_const_iterator _i) : i(_i) { }
00046
00047 protected:
00048 const const_key_ipair* arrow() const {
00049 return &(*i);
00050 }
00051
00052 const const_key_ipair& star() const {
00053 return *i;
00054 }
00055
00056 void next() const {
00057 i++;
00058 }
00059
00060 bool equals(const inner_iterator& right) const {
00061 return i == dynamic_cast<const _inner_iterator&>(right).i;
00062 }
00063
00064 void assign(const inner_iterator& other) {
00065 i = dynamic_cast<const _inner_iterator&>(other).i;
00066 }
00067
00068 inner_iterator* clone() const {
00069 return new _inner_iterator(i);
00070 }
00071
00072 void erase() {
00073 ABORT("erase not supported");
00074 }
00075
00076 public:
00077 virtual ~_inner_iterator() { }
00078 };
00079
00080 bool containsKey(const Key& k) const {
00081 return T::find(k) != T::end();
00082 }
00083
00084 bool get(const Key& k, Data& d) const {
00085 base_const_iterator i = T::find(k);
00086 if (i == T::end()) {
00087 return false;
00088 }
00089 d = i->second;
00090 return true;
00091 }
00092
00093 void put(const Key& k, const Data& d) {
00094 (*this)[k] = d;
00095 }
00096
00097 size_t erase(const Key& k) {
00098 return T::erase(k);
00099 }
00100
00101 const_iterator erase(const_iterator i) {
00102 Key k = i->first;
00103 i++;
00104 if (i == end()) {
00105 T::erase(k);
00106 return end();
00107 }
00108 else {
00109 Key next = i->first;
00110 T::erase(k);
00111 return find(next);
00112 }
00113 }
00114
00115 void sync() { }
00116
00117 bool empty() const {
00118 return T::empty();
00119 }
00120
00121 size_t size() const {
00122 return T::size();
00123 }
00124
00125 void close() { }
00126
00127 void clear() {
00128 T::clear();
00129 }
00130
00131 Map<Key, Data>& assign(const Map<Key, Data>& other) {
00132 T::operator=(dynamic_cast<const T&>(other));
00133 return *this;
00134 }
00135
00136 const_iterator begin() const {
00137 return const_iterator(typename const_iterator::IteratorPtr(new _inner_iterator(T::begin())));
00138 }
00139
00140 const_iterator end() const {
00141 return const_iterator(typename const_iterator::IteratorPtr(new _inner_iterator(T::end())));
00142 }
00143
00144 const_iterator find(const Key& k) const {
00145 return const_iterator(typename const_iterator::IteratorPtr(new _inner_iterator(T::find(k))));
00146 }
00147
00148 const_iterator lower_bound(const Key& k) const {
00149 return const_iterator(typename const_iterator::IteratorPtr(new _inner_iterator(T::lower_bound(k))));
00150 }
00151
00152 bool equals(const baseMap& other) const {
00153 return *this == dynamic_cast<const T&>(other);
00154 }
00155
00156 };
00157
00160 }
00161
00162 #endif // _TEMPLATE_MAP