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@286
|
80 |
StdMap() {}
|
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>
|
klao@286
|
95 |
StdMap(const StdMap<Key,T1,Comp1> &m, const T &_v) { FIXME; }
|
klao@286
|
96 |
|
klao@286
|
97 |
ReferenceType operator[](const Key &k) {
|
klao@286
|
98 |
return ( *( (insert(PairType(k,v))).first ) ).second;
|
klao@286
|
99 |
}
|
klao@286
|
100 |
ConstReferenceType operator[](const Key &k) const {
|
klao@286
|
101 |
typename parent::iterator i = lower_bound(__k);
|
klao@286
|
102 |
if (i == end() || key_comp()(k, (*i).first))
|
klao@286
|
103 |
return v;
|
klao@286
|
104 |
return (*i).second;
|
klao@286
|
105 |
}
|
klao@286
|
106 |
void set(const Key &k, const T &t) { parent::operator[](k)=t; }
|
klao@286
|
107 |
|
klao@286
|
108 |
/// Changes the default value of the map.
|
klao@286
|
109 |
/// \return Returns the previous default value.
|
klao@286
|
110 |
///
|
klao@286
|
111 |
/// \warning The value of some keys (which has alredy been queried, but
|
klao@286
|
112 |
/// the value has been unchanged from the default) may change!
|
klao@286
|
113 |
T setDefault(const T &_v) { T old=v; v=_v; return old; }
|
klao@286
|
114 |
|
klao@286
|
115 |
template<typename T1>
|
klao@286
|
116 |
struct rebind {
|
klao@286
|
117 |
typedef StdMap<Key,T1,Compare> other;
|
klao@286
|
118 |
};
|
klao@286
|
119 |
};
|
klao@286
|
120 |
|
klao@286
|
121 |
}
|
klao@286
|
122 |
#endif // HUGO_MAPS_H
|