src/include/maps.h
author marci
Sat, 24 Apr 2004 12:53:46 +0000
changeset 390 8dc830d3f9ef
parent 346 538ff3ce9f68
child 391 ddf25b2fa6ab
permissions -rw-r--r--
jacint mindig modosit, es ezert nekunk is kell
     1 // -*- C++ -*- //
     2 #ifndef HUGO_MAPS_H
     3 #define HUGO_MAPS_H
     4 
     5 ///\file
     6 ///\brief Miscellaneous property maps
     7 ///
     8 ///\todo This file has the same name as the concept file in skeletons,
     9 /// and this is not easily detectable in docs...
    10 
    11 #include <map>
    12 
    13 namespace hugo {
    14 
    15   /// Null map. (aka DoNothingMap)
    16 
    17   /// If you have to provide a map only for its type definitions,
    18   /// or if you have to provide a writable map, but will not use the
    19   /// data written to it...
    20   template<typename K, typename T>
    21   class NullMap
    22   {
    23   public:
    24     typedef K KeyType;
    25     typedef T ValueType;
    26 
    27     T operator[](const K&) const { return T(); }
    28     void set(const K&, const T&) {}
    29   };
    30 
    31 
    32   /// Constant map.
    33 
    34   /// This is a readable map which assignes a specified value to each key.
    35   /// In other aspects it is equivalent to the \ref NullMap
    36   template<typename K, typename T>
    37   class ConstMap
    38   {
    39     T v;
    40   public:
    41     typedef K KeyType;
    42     typedef T ValueType;
    43 
    44     ConstMap() {}
    45     ConstMap(const T &_v) : v(_v) {}
    46 
    47     T operator[](const K&) const { return v; }
    48     void set(const K&, const T&) {}
    49 
    50     template<typename T1>
    51     struct rebind {
    52       typedef ConstMap<K,T1> other;
    53     };
    54 
    55     template<typename T1>
    56     ConstMap(const ConstMap<K,T1> &, const T &_v) : v(_v) {}
    57   };
    58 
    59 
    60 
    61   /// \c std::map wrapper
    62 
    63   /// This is essentially a wrapper for \c std::map. With addition that
    64   /// you can specify a default value different from \c ValueType() .
    65   ///
    66   /// \todo Provide allocator parameter...
    67   template <typename Key, typename T, typename Compare = std::less<Key> >
    68   class StdMap : public std::map<Key,T,Compare> {
    69     typedef std::map<Key,T,Compare> parent;
    70     T v;
    71     typedef typename parent::value_type PairType;
    72 
    73   public:
    74     typedef Key KeyType;
    75     typedef T ValueType;
    76     typedef T& ReferenceType;
    77     typedef const T& ConstReferenceType;
    78 
    79 
    80     StdMap() : v() {}
    81     /// Constructor with specified default value
    82     StdMap(const T& _v) : v(_v) {}
    83 
    84     /// \brief Constructs the map from an appropriate std::map.
    85     ///
    86     /// \warning Inefficient: copies the content of \c m !
    87     StdMap(const parent &m) : parent(m) {}
    88     /// \brief Constructs the map from an appropriate std::map, and explicitly
    89     /// specifies a default value.
    90     ///
    91     /// \warning Inefficient: copies the content of \c m !
    92     StdMap(const parent &m, const T& _v) : parent(m), v(_v) {}
    93     
    94     template<typename T1, typename Comp1>
    95     StdMap(const StdMap<Key,T1,Comp1> &m, const T &_v) { 
    96       //FIXME; 
    97     }
    98 
    99     ReferenceType operator[](const Key &k) {
   100       return insert(PairType(k,v)).first -> second;
   101     }
   102     ConstReferenceType operator[](const Key &k) const {
   103 //marci jav      typename parent::iterator i = lower_bound(__k);
   104       typename parent::iterator i = lower_bound(k);
   105       if (i == end() || key_comp()(k, (*i).first))
   106 	return v;
   107       return (*i).second;
   108     }
   109     void set(const Key &k, const T &t) {
   110       parent::operator[](k) = t;
   111     }
   112 
   113     /// Changes the default value of the map.
   114     /// \return Returns the previous default value.
   115     ///
   116     /// \warning The value of some keys (which has alredy been queried, but
   117     /// the value has been unchanged from the default) may change!
   118     T setDefault(const T &_v) { T old=v; v=_v; return old; }
   119 
   120     template<typename T1>
   121     struct rebind {
   122       typedef StdMap<Key,T1,Compare> other;
   123     };
   124   };
   125   
   126 }
   127 #endif // HUGO_MAPS_H