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