COIN-OR::LEMON - Graph Library

source: lemon-0.x/src/hugo/maps.h @ 732:33cbc0635e92

Last change on this file since 732:33cbc0635e92 was 720:193d881b23ad, checked in by Alpar Juttner, 20 years ago

MapBase? added

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