00001
00002
00003 #ifndef _PROTO_MAP_H
00004 #define _PROTO_MAP_H
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 #include "TypedBaseMap.h"
00030 #include "KeyIterator.h"
00031
00032 template <class S, class T> class ProtoMap : public TypedBaseMap<S,T> {
00033 protected:
00034 virtual INLINE Boolean _less(const BMKey &key1, const BMKey &key2) const;
00035
00036
00037 virtual INLINE Boolean _equal(const BMKey &key1, const BMKey &key2) const;
00038
00039
00040 virtual INLINE void _print(ostream &o, const BMKey &key) const;
00041
00042
00043 public:
00044
00045
00046
00047 friend ostream & operator << <> (ostream & o, const ProtoMap<S, T> &m);
00048
00049
00050 INLINE ProtoMap();
00051 INLINE ProtoMap(const ProtoMap<S, T> &m);
00052
00053 virtual INLINE ~ProtoMap();
00054
00055 INLINE const T *find_ref(const S &key) const;
00056 INLINE T *find_ref(const S &key);
00057
00058
00059 INLINE Boolean member(const S &key) const;
00060
00061
00062 INLINE void print(ostream & out, char *sep1, char *sep2) const;
00063
00064
00065
00066 INLINE const T &operator[] (const S &key) const;
00067 INLINE T &operator[] (const S &key);
00068
00069 };
00070
00071
00072
00073
00074 template <class S, class T> ostream &
00075 operator << (ostream & o, const ProtoMap<S, T> &m)
00076 { m.print(o, ": ", ", "); return o; }
00077
00078 template <class S, class T>
00079 INLINE
00080 ProtoMap<S, T>::ProtoMap()
00081 {
00082 #ifdef MEMORY_LEAK_PRINT
00083 cout << "ProtoMap ctor: " << this << " : size=" << sizeof(ProtoMap) << endl;
00084 #endif
00085
00086 }
00087
00088 template <class S, class T>
00089 INLINE
00090 ProtoMap<S, T>::ProtoMap(const ProtoMap<S, T> &m)
00091 {
00092 #ifdef MEMORY_LEAK_PRINT
00093 cout << "ProtoMap ctor: " << this << " : size=" << sizeof(ProtoMap) << endl;
00094 #endif
00095
00096
00097 (*this) = m;
00098 }
00099
00100 template <class S, class T>
00101 INLINE
00102 ProtoMap<S, T>::~ProtoMap()
00103 {
00104 #ifdef MEMORY_LEAK_PRINT
00105 cout << "ProtoMap dtor: " << this << " : size=" << sizeof(ProtoMap) << endl;
00106 #endif
00107
00108
00109 }
00110
00111
00112 template <class S, class T>
00113 INLINE Boolean
00114 ProtoMap<S, T>::_less(const BMKey &key1, const BMKey &key2) const
00115 {
00116 return (key1._keyval < key2._keyval);
00117 }
00118
00119 template <class S, class T>
00120 INLINE Boolean
00121 ProtoMap<S, T>::_equal(const BMKey &key1, const BMKey &key2) const
00122 {
00123 return (key1._keyval == key2._keyval);
00124 }
00125
00126 template <class S, class T>
00127 INLINE void
00128 ProtoMap<S, T>::_print(ostream & o, const BMKey &key) const
00129 {
00130 const S *key_ptr = (S *) key._keyptr;
00131
00132 o << (*key_ptr);
00133 }
00134
00135
00136 template <class S, class T>
00137 INLINE const T *
00138 ProtoMap<S, T>::find_ref(const S &key) const
00139 {
00140 BMKey findkey;
00141
00142 findkey._keyptr = (void *) &key;
00143
00144 return (const T *) TypedBaseMap<S,T>::find_ref(findkey);
00145 }
00146
00147 template <class S, class T>
00148 INLINE T *
00149 ProtoMap<S, T>::find_ref(const S &key)
00150 {
00151 BMKey findkey;
00152
00153 findkey._keyptr = (void *) &key;
00154
00155 return (T *) TypedBaseMap<S,T>::find_ref(findkey);
00156 }
00157
00158 template <class S, class T>
00159 INLINE Boolean
00160 ProtoMap<S, T>::member(const S &key) const
00161 {
00162 BMKey memkey;
00163
00164 memkey._keyptr = (void *) &key;
00165
00166 return TypedBaseMap<S,T>::member(memkey);
00167 }
00168
00169 template <class S, class T>
00170 INLINE const T &
00171 ProtoMap<S, T>::operator[] (const S &key) const
00172 {
00173 return *find_ref(key);
00174 }
00175
00176 template <class S, class T>
00177 INLINE T &
00178 ProtoMap<S, T>::operator[] (const S &key)
00179 {
00180 return *find_ref(key);
00181 }
00182
00183 template <class S, class T>
00184 INLINE void
00185 ProtoMap<S,T>::print(ostream & out, char *sep1, char *sep2) const
00186 {
00187 out << "{";
00188
00189 for (KeyIterator<S,T> map_iter = *this; map_iter.valid(); ++map_iter) {
00190 BMKey printkey;
00191 printkey._keyptr = (void *) &map_iter.current_key();
00192 _print(out, printkey);
00193 out << sep1 << map_iter.current_data() << sep2;
00194 }
00195
00196 out << "}";
00197 }
00198
00199 #endif