src/hugo/maps.h
author alpar
Mon, 13 Sep 2004 11:24:35 +0000
changeset 835 eb9587f09b42
parent 720 193d881b23ad
child 890 3a48bc350e0f
permissions -rw-r--r--
Remove one remaining range checking.
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@805
    17
  /// Base class of maps.
alpar@805
    18
  /// It provides the necessary <tt>typedef</tt>s required by the map concept.
alpar@720
    19
  template<typename K, typename T>
alpar@720
    20
  class MapBase
alpar@720
    21
  {
alpar@720
    22
  public:
alpar@805
    23
    /// .
alpar@720
    24
    typedef K KeyType;
alpar@805
    25
    /// .
alpar@720
    26
    typedef T ValueType;
alpar@720
    27
  };
alpar@720
    28
alpar@805
    29
  /// Null map. (a.k.a. DoNothingMap)
klao@286
    30
klao@286
    31
  /// If you have to provide a map only for its type definitions,
alpar@805
    32
  /// or if you have to provide a writable map, but
alpar@805
    33
  /// data written to it will sent to <tt>/dev/null</tt>...
klao@286
    34
  template<typename K, typename T>
alpar@720
    35
  class NullMap : public MapBase<K,T>
klao@286
    36
  {
klao@286
    37
  public:
klao@286
    38
alpar@805
    39
    /// Gives back a default constructed element.
klao@286
    40
    T operator[](const K&) const { return T(); }
alpar@805
    41
    /// Absorbs the value.
klao@286
    42
    void set(const K&, const T&) {}
klao@286
    43
  };
klao@286
    44
klao@286
    45
klao@286
    46
  /// Constant map.
klao@286
    47
alpar@805
    48
  /// This is a readable map which assigns a specified value to each key.
alpar@805
    49
  /// In other aspects it is equivalent to the \ref NullMap.
alpar@805
    50
  /// \todo set could be used to set the value.
klao@286
    51
  template<typename K, typename T>
alpar@720
    52
  class ConstMap : public MapBase<K,T>
klao@286
    53
  {
klao@286
    54
    T v;
klao@286
    55
  public:
klao@286
    56
alpar@805
    57
    /// Default constructor
alpar@805
    58
alpar@805
    59
    /// The value of the map will be uninitialized. 
alpar@805
    60
    /// (More exactly it will be default constructed.)
klao@286
    61
    ConstMap() {}
alpar@805
    62
    /// .
alpar@805
    63
alpar@805
    64
    /// \param _v The initial value of the map.
klao@286
    65
    ConstMap(const T &_v) : v(_v) {}
klao@286
    66
klao@286
    67
    T operator[](const K&) const { return v; }
klao@286
    68
    void set(const K&, const T&) {}
klao@286
    69
klao@286
    70
    template<typename T1>
klao@286
    71
    struct rebind {
klao@286
    72
      typedef ConstMap<K,T1> other;
klao@286
    73
    };
klao@286
    74
klao@286
    75
    template<typename T1>
klao@286
    76
    ConstMap(const ConstMap<K,T1> &, const T &_v) : v(_v) {}
klao@286
    77
  };
klao@286
    78
klao@286
    79
klao@286
    80
klao@286
    81
  /// \c std::map wrapper
klao@286
    82
klao@286
    83
  /// This is essentially a wrapper for \c std::map. With addition that
klao@286
    84
  /// you can specify a default value different from \c ValueType() .
klao@286
    85
  ///
klao@286
    86
  /// \todo Provide allocator parameter...
klao@286
    87
  template <typename Key, typename T, typename Compare = std::less<Key> >
klao@286
    88
  class StdMap : public std::map<Key,T,Compare> {
klao@286
    89
    typedef std::map<Key,T,Compare> parent;
klao@286
    90
    T v;
klao@286
    91
    typedef typename parent::value_type PairType;
klao@286
    92
klao@286
    93
  public:
klao@286
    94
    typedef Key KeyType;
klao@286
    95
    typedef T ValueType;
klao@286
    96
    typedef T& ReferenceType;
klao@286
    97
    typedef const T& ConstReferenceType;
klao@286
    98
klao@286
    99
klao@345
   100
    StdMap() : v() {}
klao@286
   101
    /// Constructor with specified default value
klao@286
   102
    StdMap(const T& _v) : v(_v) {}
klao@286
   103
klao@286
   104
    /// \brief Constructs the map from an appropriate std::map.
klao@286
   105
    ///
klao@286
   106
    /// \warning Inefficient: copies the content of \c m !
klao@286
   107
    StdMap(const parent &m) : parent(m) {}
klao@286
   108
    /// \brief Constructs the map from an appropriate std::map, and explicitly
klao@286
   109
    /// specifies a default value.
klao@286
   110
    ///
klao@286
   111
    /// \warning Inefficient: copies the content of \c m !
klao@286
   112
    StdMap(const parent &m, const T& _v) : parent(m), v(_v) {}
klao@286
   113
    
klao@286
   114
    template<typename T1, typename Comp1>
marci@389
   115
    StdMap(const StdMap<Key,T1,Comp1> &m, const T &_v) { 
marci@389
   116
      //FIXME; 
marci@389
   117
    }
klao@286
   118
klao@286
   119
    ReferenceType operator[](const Key &k) {
klao@346
   120
      return insert(PairType(k,v)).first -> second;
klao@286
   121
    }
klao@286
   122
    ConstReferenceType operator[](const Key &k) const {
marci@389
   123
      typename parent::iterator i = lower_bound(k);
beckerjc@391
   124
      if (i == parent::end() || parent::key_comp()(k, (*i).first))
klao@286
   125
	return v;
klao@286
   126
      return (*i).second;
klao@286
   127
    }
klao@345
   128
    void set(const Key &k, const T &t) {
klao@346
   129
      parent::operator[](k) = t;
klao@345
   130
    }
klao@286
   131
klao@286
   132
    /// Changes the default value of the map.
klao@286
   133
    /// \return Returns the previous default value.
klao@286
   134
    ///
alpar@805
   135
    /// \warning The value of some keys (which has already been queried, but
klao@286
   136
    /// the value has been unchanged from the default) may change!
klao@286
   137
    T setDefault(const T &_v) { T old=v; v=_v; return old; }
klao@286
   138
klao@286
   139
    template<typename T1>
klao@286
   140
    struct rebind {
klao@286
   141
      typedef StdMap<Key,T1,Compare> other;
klao@286
   142
    };
klao@286
   143
  };
klao@286
   144
  
klao@286
   145
}
klao@286
   146
#endif // HUGO_MAPS_H