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 |
|
marci@890
|
79 |
//to document later
|
marci@890
|
80 |
template<typename T, T v>
|
marci@890
|
81 |
struct Const { };
|
marci@890
|
82 |
//to document later
|
marci@890
|
83 |
template<typename K, typename V, V v>
|
marci@890
|
84 |
class ConstMap<K, Const<V, v> > : public MapBase<K, V>
|
marci@890
|
85 |
{
|
marci@890
|
86 |
public:
|
marci@890
|
87 |
ConstMap() { }
|
marci@890
|
88 |
V operator[](const K&) const { return v; }
|
marci@890
|
89 |
void set(const K&, const V&) { }
|
marci@890
|
90 |
};
|
marci@890
|
91 |
//to document later
|
marci@890
|
92 |
typedef Const<bool, true> True;
|
marci@890
|
93 |
typedef Const<bool, false> False;
|
klao@286
|
94 |
|
klao@286
|
95 |
/// \c std::map wrapper
|
klao@286
|
96 |
|
klao@286
|
97 |
/// This is essentially a wrapper for \c std::map. With addition that
|
klao@286
|
98 |
/// you can specify a default value different from \c ValueType() .
|
klao@286
|
99 |
///
|
klao@286
|
100 |
/// \todo Provide allocator parameter...
|
klao@286
|
101 |
template <typename Key, typename T, typename Compare = std::less<Key> >
|
klao@286
|
102 |
class StdMap : public std::map<Key,T,Compare> {
|
klao@286
|
103 |
typedef std::map<Key,T,Compare> parent;
|
klao@286
|
104 |
T v;
|
klao@286
|
105 |
typedef typename parent::value_type PairType;
|
klao@286
|
106 |
|
klao@286
|
107 |
public:
|
klao@286
|
108 |
typedef Key KeyType;
|
klao@286
|
109 |
typedef T ValueType;
|
klao@286
|
110 |
typedef T& ReferenceType;
|
klao@286
|
111 |
typedef const T& ConstReferenceType;
|
klao@286
|
112 |
|
klao@286
|
113 |
|
klao@345
|
114 |
StdMap() : v() {}
|
klao@286
|
115 |
/// Constructor with specified default value
|
klao@286
|
116 |
StdMap(const T& _v) : v(_v) {}
|
klao@286
|
117 |
|
klao@286
|
118 |
/// \brief Constructs the map from an appropriate std::map.
|
klao@286
|
119 |
///
|
klao@286
|
120 |
/// \warning Inefficient: copies the content of \c m !
|
klao@286
|
121 |
StdMap(const parent &m) : parent(m) {}
|
klao@286
|
122 |
/// \brief Constructs the map from an appropriate std::map, and explicitly
|
klao@286
|
123 |
/// specifies a default value.
|
klao@286
|
124 |
///
|
klao@286
|
125 |
/// \warning Inefficient: copies the content of \c m !
|
klao@286
|
126 |
StdMap(const parent &m, const T& _v) : parent(m), v(_v) {}
|
klao@286
|
127 |
|
klao@286
|
128 |
template<typename T1, typename Comp1>
|
marci@389
|
129 |
StdMap(const StdMap<Key,T1,Comp1> &m, const T &_v) {
|
marci@389
|
130 |
//FIXME;
|
marci@389
|
131 |
}
|
klao@286
|
132 |
|
klao@286
|
133 |
ReferenceType operator[](const Key &k) {
|
klao@346
|
134 |
return insert(PairType(k,v)).first -> second;
|
klao@286
|
135 |
}
|
klao@286
|
136 |
ConstReferenceType operator[](const Key &k) const {
|
marci@389
|
137 |
typename parent::iterator i = lower_bound(k);
|
beckerjc@391
|
138 |
if (i == parent::end() || parent::key_comp()(k, (*i).first))
|
klao@286
|
139 |
return v;
|
klao@286
|
140 |
return (*i).second;
|
klao@286
|
141 |
}
|
klao@345
|
142 |
void set(const Key &k, const T &t) {
|
klao@346
|
143 |
parent::operator[](k) = t;
|
klao@345
|
144 |
}
|
klao@286
|
145 |
|
klao@286
|
146 |
/// Changes the default value of the map.
|
klao@286
|
147 |
/// \return Returns the previous default value.
|
klao@286
|
148 |
///
|
alpar@805
|
149 |
/// \warning The value of some keys (which has already been queried, but
|
klao@286
|
150 |
/// the value has been unchanged from the default) may change!
|
klao@286
|
151 |
T setDefault(const T &_v) { T old=v; v=_v; return old; }
|
klao@286
|
152 |
|
klao@286
|
153 |
template<typename T1>
|
klao@286
|
154 |
struct rebind {
|
klao@286
|
155 |
typedef StdMap<Key,T1,Compare> other;
|
klao@286
|
156 |
};
|
klao@286
|
157 |
};
|
klao@286
|
158 |
|
klao@286
|
159 |
}
|
klao@286
|
160 |
#endif // HUGO_MAPS_H
|