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