Go to Overview over all GrAL packages.
Main Page   Modules   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Namespace Members   Compound Members   File Members   Related Pages  

Container/partial-mapping.h

Go to the documentation of this file.
00001 #ifndef NMWR_GB_PARTIAL_MAPPING_H
00002 #define NMWR_GB_PARTIAL_MAPPING_H
00003 
00004 
00005 // $LICENSE
00006 
00007 #include "my-hash-map.h" // STL
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   // STL unary function conformance
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   //typedef hash_map<T1,T2,hash<T1>,equal_to<T1> >  map_table_type;
00081   typedef hash_map<T1,T2,hasher_type<T1>,equal_to<T1> >  map_table_type;
00082 
00083   //   typedef write_only_proxy<T2>                    proxy_type;
00084 
00085   //------ DATA --------
00086   map_table_type mapping;
00087   T2             default_val;
00088 public:
00089   //---------------------- construction --------------------------
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   //---------------------- queries -------------------------------
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   //---------------------- data access ---------------------------
00104 
00105   typedef typename map_table_type::iterator       iterator;
00106   typedef typename map_table_type::const_iterator const_iterator;
00107 
00108   // should use domain/image types
00109   const_iterator begin() const { return mapping.begin();}
00110   const_iterator end()   const { return mapping.end();}
00111   // no non-const versions of begin/end: writing should occur
00112   // only via mapping interface: op[]/() below.
00113 
00114   //   proxy_type operator[](const T1& t1) { return proxy_type(mapping[t1]);}
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 

Copyright (c) Guntram Berti 1997-2002. See the GrAL Homepage for up-to-date information.

Generated at Tue Feb 26 15:57:14 2002 for Sequence by doxygen 1.2.11-20011104 written by Dimitri van Heesch, © 1997-2000