00001 #ifndef NMWR_GB_PARTIAL_MAPPING_H
00002 #define NMWR_GB_PARTIAL_MAPPING_H
00003
00004
00005
00006
00007 #include "my-hash-map.h"
00008
00009 #include "Container/proxies.h"
00010 #include "Utility/pre-post-conditions.h"
00011
00012
00013
00054
00055
00056
00057 template<class T1, class T2>
00058 class partial_mapping {
00059 public:
00060
00061 typedef T1 argument_type;
00062 typedef T2 result_type;
00063
00064 private:
00065 template<class T>
00066 struct hasher_type {
00067 size_t operator()(T const& t) const {
00068 hash<T> h;
00069 return h(t);
00070 }
00071 };
00072 template<class T>
00073 struct hasher_type<T*> {
00074 size_t operator()(T const* t) const {
00075 hash<unsigned> h;
00076 return h(reinterpret_cast<unsigned>(t));
00077 }
00078 };
00079
00080
00081 typedef hash_map<T1,T2,hasher_type<T1>,equal_to<T1> > map_table_type;
00082
00083
00084
00085
00086 map_table_type mapping;
00087 T2 default_val;
00088 public:
00089
00090
00091 partial_mapping(const T2& def = T2()) : default_val(def) {}
00092
00093 const T2& set_default(const T2& t2) { default_val = t2; return t2;}
00094
00095
00096
00097 int size_of_dom() const { return mapping.size();}
00098 bool defined (const T1& t1) const { return (mapping.find(t1) != mapping.end());}
00099 bool undefined(const T1& t1) const { return (mapping.find(t1) == mapping.end());}
00100
00101 const T2& default_value() const { return default_val;}
00102
00103
00104
00105 typedef typename map_table_type::iterator iterator;
00106 typedef typename map_table_type::const_iterator const_iterator;
00107
00108
00109 const_iterator begin() const { return mapping.begin();}
00110 const_iterator end() const { return mapping.end();}
00111
00112
00113
00114
00115 T2 & operator[](T1 const& t1) {
00116 if(undefined(t1))
00117 mapping[t1] = default_val;
00118 return mapping[t1];
00119 }
00120 T2 const& operator()(T1 const& t1) const {
00121 const_iterator i = mapping.find(t1);
00122 return ( i != mapping.end() ? (*i).second : default_val);
00123 }
00124
00125 };
00126
00127 #endif
00128