src/hugo/maps.h
author alpar
Thu, 05 Aug 2004 11:41:36 +0000
changeset 760 49d4fe04fbc0
parent 539 fb261e3a9a0f
child 805 59b8cb2cb2f8
permissions -rw-r--r--
Legal stuff, version 0.2.
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
alpar@720
    15
  /// Base class of maps.
alpar@720
    16
alpar@720
    17
  template<typename K, typename T>
alpar@720
    18
  class MapBase
alpar@720
    19
  {
alpar@720
    20
  public:
alpar@720
    21
    ///
alpar@720
    22
    typedef K KeyType;
alpar@720
    23
    ///
alpar@720
    24
    typedef T ValueType;
alpar@720
    25
  };
alpar@720
    26
klao@286
    27
  /// Null map. (aka DoNothingMap)
klao@286
    28
klao@286
    29
  /// If you have to provide a map only for its type definitions,
klao@286
    30
  /// or if you have to provide a writable map, but will not use the
klao@286
    31
  /// data written to it...
klao@286
    32
  template<typename K, typename T>
alpar@720
    33
  class NullMap : public MapBase<K,T>
klao@286
    34
  {
klao@286
    35
  public:
klao@286
    36
klao@286
    37
    T operator[](const K&) const { return T(); }
klao@286
    38
    void set(const K&, const T&) {}
jacint@529
    39
    ///\bug when update is removed from map concepts by being dynamic
jacint@529
    40
    ///stuffs, this line have to be removed.
jacint@529
    41
    void update() { }
klao@286
    42
  };
klao@286
    43
klao@286
    44
klao@286
    45
  /// Constant map.
klao@286
    46
klao@286
    47
  /// This is a readable map which assignes a specified value to each key.
klao@286
    48
  /// In other aspects it is equivalent to the \ref NullMap
klao@286
    49
  template<typename K, typename T>
alpar@720
    50
  class ConstMap : public MapBase<K,T>
klao@286
    51
  {
klao@286
    52
    T v;
klao@286
    53
  public:
klao@286
    54
klao@286
    55
    ConstMap() {}
klao@286
    56
    ConstMap(const T &_v) : v(_v) {}
klao@286
    57
klao@286
    58
    T operator[](const K&) const { return v; }
klao@286
    59
    void set(const K&, const T&) {}
klao@286
    60
klao@286
    61
    template<typename T1>
klao@286
    62
    struct rebind {
klao@286
    63
      typedef ConstMap<K,T1> other;
klao@286
    64
    };
klao@286
    65
klao@286
    66
    template<typename T1>
klao@286
    67
    ConstMap(const ConstMap<K,T1> &, const T &_v) : v(_v) {}
klao@286
    68
  };
klao@286
    69
klao@286
    70
klao@286
    71
klao@286
    72
  /// \c std::map wrapper
klao@286
    73
klao@286
    74
  /// This is essentially a wrapper for \c std::map. With addition that
klao@286
    75
  /// you can specify a default value different from \c ValueType() .
klao@286
    76
  ///
klao@286
    77
  /// \todo Provide allocator parameter...
klao@286
    78
  template <typename Key, typename T, typename Compare = std::less<Key> >
klao@286
    79
  class StdMap : public std::map<Key,T,Compare> {
klao@286
    80
    typedef std::map<Key,T,Compare> parent;
klao@286
    81
    T v;
klao@286
    82
    typedef typename parent::value_type PairType;
klao@286
    83
klao@286
    84
  public:
klao@286
    85
    typedef Key KeyType;
klao@286
    86
    typedef T ValueType;
klao@286
    87
    typedef T& ReferenceType;
klao@286
    88
    typedef const T& ConstReferenceType;
klao@286
    89
klao@286
    90
klao@345
    91
    StdMap() : v() {}
klao@286
    92
    /// Constructor with specified default value
klao@286
    93
    StdMap(const T& _v) : v(_v) {}
klao@286
    94
klao@286
    95
    /// \brief Constructs the map from an appropriate std::map.
klao@286
    96
    ///
klao@286
    97
    /// \warning Inefficient: copies the content of \c m !
klao@286
    98
    StdMap(const parent &m) : parent(m) {}
klao@286
    99
    /// \brief Constructs the map from an appropriate std::map, and explicitly
klao@286
   100
    /// specifies a default value.
klao@286
   101
    ///
klao@286
   102
    /// \warning Inefficient: copies the content of \c m !
klao@286
   103
    StdMap(const parent &m, const T& _v) : parent(m), v(_v) {}
klao@286
   104
    
klao@286
   105
    template<typename T1, typename Comp1>
marci@389
   106
    StdMap(const StdMap<Key,T1,Comp1> &m, const T &_v) { 
marci@389
   107
      //FIXME; 
marci@389
   108
    }
klao@286
   109
klao@286
   110
    ReferenceType operator[](const Key &k) {
klao@346
   111
      return insert(PairType(k,v)).first -> second;
klao@286
   112
    }
klao@286
   113
    ConstReferenceType operator[](const Key &k) const {
marci@389
   114
      typename parent::iterator i = lower_bound(k);
beckerjc@391
   115
      if (i == parent::end() || parent::key_comp()(k, (*i).first))
klao@286
   116
	return v;
klao@286
   117
      return (*i).second;
klao@286
   118
    }
klao@345
   119
    void set(const Key &k, const T &t) {
klao@346
   120
      parent::operator[](k) = t;
klao@345
   121
    }
klao@286
   122
klao@286
   123
    /// Changes the default value of the map.
klao@286
   124
    /// \return Returns the previous default value.
klao@286
   125
    ///
klao@286
   126
    /// \warning The value of some keys (which has alredy been queried, but
klao@286
   127
    /// the value has been unchanged from the default) may change!
klao@286
   128
    T setDefault(const T &_v) { T old=v; v=_v; return old; }
klao@286
   129
klao@286
   130
    template<typename T1>
klao@286
   131
    struct rebind {
klao@286
   132
      typedef StdMap<Key,T1,Compare> other;
klao@286
   133
    };
klao@286
   134
  };
klao@286
   135
  
klao@286
   136
}
klao@286
   137
#endif // HUGO_MAPS_H