alpar@906
|
1 |
/* -*- C++ -*-
|
alpar@906
|
2 |
* src/hugo/maps.h - Part of HUGOlib, a generic C++ optimization library
|
alpar@906
|
3 |
*
|
alpar@906
|
4 |
* Copyright (C) 2004 Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
|
alpar@906
|
5 |
* (Egervary Combinatorial Optimization Research Group, EGRES).
|
alpar@906
|
6 |
*
|
alpar@906
|
7 |
* Permission to use, modify and distribute this software is granted
|
alpar@906
|
8 |
* provided that this copyright notice appears in all copies. For
|
alpar@906
|
9 |
* precise terms see the accompanying LICENSE file.
|
alpar@906
|
10 |
*
|
alpar@906
|
11 |
* This software is provided "AS IS" with no warranty of any kind,
|
alpar@906
|
12 |
* express or implied, and with no claim as to its suitability for any
|
alpar@906
|
13 |
* purpose.
|
alpar@906
|
14 |
*
|
alpar@906
|
15 |
*/
|
alpar@906
|
16 |
|
klao@286
|
17 |
#ifndef HUGO_MAPS_H
|
klao@286
|
18 |
#define HUGO_MAPS_H
|
klao@286
|
19 |
|
klao@286
|
20 |
///\file
|
klao@286
|
21 |
///\brief Miscellaneous property maps
|
klao@286
|
22 |
///
|
klao@286
|
23 |
///\todo This file has the same name as the concept file in skeletons,
|
klao@286
|
24 |
/// and this is not easily detectable in docs...
|
klao@286
|
25 |
|
klao@286
|
26 |
#include <map>
|
klao@286
|
27 |
|
klao@286
|
28 |
namespace hugo {
|
klao@286
|
29 |
|
alpar@720
|
30 |
/// Base class of maps.
|
alpar@720
|
31 |
|
alpar@805
|
32 |
/// Base class of maps.
|
alpar@805
|
33 |
/// It provides the necessary <tt>typedef</tt>s required by the map concept.
|
alpar@720
|
34 |
template<typename K, typename T>
|
alpar@720
|
35 |
class MapBase
|
alpar@720
|
36 |
{
|
alpar@720
|
37 |
public:
|
alpar@805
|
38 |
/// .
|
alpar@720
|
39 |
typedef K KeyType;
|
alpar@805
|
40 |
/// .
|
alpar@720
|
41 |
typedef T ValueType;
|
alpar@720
|
42 |
};
|
alpar@720
|
43 |
|
alpar@805
|
44 |
/// Null map. (a.k.a. DoNothingMap)
|
klao@286
|
45 |
|
klao@286
|
46 |
/// If you have to provide a map only for its type definitions,
|
alpar@805
|
47 |
/// or if you have to provide a writable map, but
|
alpar@805
|
48 |
/// data written to it will sent to <tt>/dev/null</tt>...
|
klao@286
|
49 |
template<typename K, typename T>
|
alpar@720
|
50 |
class NullMap : public MapBase<K,T>
|
klao@286
|
51 |
{
|
klao@286
|
52 |
public:
|
klao@286
|
53 |
|
alpar@805
|
54 |
/// Gives back a default constructed element.
|
klao@286
|
55 |
T operator[](const K&) const { return T(); }
|
alpar@805
|
56 |
/// Absorbs the value.
|
klao@286
|
57 |
void set(const K&, const T&) {}
|
klao@286
|
58 |
};
|
klao@286
|
59 |
|
klao@286
|
60 |
|
klao@286
|
61 |
/// Constant map.
|
klao@286
|
62 |
|
alpar@805
|
63 |
/// This is a readable map which assigns a specified value to each key.
|
alpar@805
|
64 |
/// In other aspects it is equivalent to the \ref NullMap.
|
alpar@805
|
65 |
/// \todo set could be used to set the value.
|
klao@286
|
66 |
template<typename K, typename T>
|
alpar@720
|
67 |
class ConstMap : public MapBase<K,T>
|
klao@286
|
68 |
{
|
klao@286
|
69 |
T v;
|
klao@286
|
70 |
public:
|
klao@286
|
71 |
|
alpar@805
|
72 |
/// Default constructor
|
alpar@805
|
73 |
|
alpar@805
|
74 |
/// The value of the map will be uninitialized.
|
alpar@805
|
75 |
/// (More exactly it will be default constructed.)
|
klao@286
|
76 |
ConstMap() {}
|
alpar@805
|
77 |
/// .
|
alpar@805
|
78 |
|
alpar@805
|
79 |
/// \param _v The initial value of the map.
|
klao@286
|
80 |
ConstMap(const T &_v) : v(_v) {}
|
klao@286
|
81 |
|
klao@286
|
82 |
T operator[](const K&) const { return v; }
|
klao@286
|
83 |
void set(const K&, const T&) {}
|
klao@286
|
84 |
|
klao@286
|
85 |
template<typename T1>
|
klao@286
|
86 |
struct rebind {
|
klao@286
|
87 |
typedef ConstMap<K,T1> other;
|
klao@286
|
88 |
};
|
klao@286
|
89 |
|
klao@286
|
90 |
template<typename T1>
|
klao@286
|
91 |
ConstMap(const ConstMap<K,T1> &, const T &_v) : v(_v) {}
|
klao@286
|
92 |
};
|
klao@286
|
93 |
|
marci@890
|
94 |
//to document later
|
marci@890
|
95 |
template<typename T, T v>
|
marci@890
|
96 |
struct Const { };
|
marci@890
|
97 |
//to document later
|
marci@890
|
98 |
template<typename K, typename V, V v>
|
marci@890
|
99 |
class ConstMap<K, Const<V, v> > : public MapBase<K, V>
|
marci@890
|
100 |
{
|
marci@890
|
101 |
public:
|
marci@890
|
102 |
ConstMap() { }
|
marci@890
|
103 |
V operator[](const K&) const { return v; }
|
marci@890
|
104 |
void set(const K&, const V&) { }
|
marci@890
|
105 |
};
|
marci@890
|
106 |
//to document later
|
marci@890
|
107 |
typedef Const<bool, true> True;
|
marci@890
|
108 |
typedef Const<bool, false> False;
|
klao@286
|
109 |
|
klao@286
|
110 |
/// \c std::map wrapper
|
klao@286
|
111 |
|
klao@286
|
112 |
/// This is essentially a wrapper for \c std::map. With addition that
|
klao@286
|
113 |
/// you can specify a default value different from \c ValueType() .
|
klao@286
|
114 |
///
|
klao@286
|
115 |
/// \todo Provide allocator parameter...
|
klao@286
|
116 |
template <typename Key, typename T, typename Compare = std::less<Key> >
|
klao@286
|
117 |
class StdMap : public std::map<Key,T,Compare> {
|
klao@286
|
118 |
typedef std::map<Key,T,Compare> parent;
|
klao@286
|
119 |
T v;
|
klao@286
|
120 |
typedef typename parent::value_type PairType;
|
klao@286
|
121 |
|
klao@286
|
122 |
public:
|
klao@286
|
123 |
typedef Key KeyType;
|
klao@286
|
124 |
typedef T ValueType;
|
klao@286
|
125 |
typedef T& ReferenceType;
|
klao@286
|
126 |
typedef const T& ConstReferenceType;
|
klao@286
|
127 |
|
klao@286
|
128 |
|
klao@345
|
129 |
StdMap() : v() {}
|
klao@286
|
130 |
/// Constructor with specified default value
|
klao@286
|
131 |
StdMap(const T& _v) : v(_v) {}
|
klao@286
|
132 |
|
klao@286
|
133 |
/// \brief Constructs the map from an appropriate std::map.
|
klao@286
|
134 |
///
|
klao@286
|
135 |
/// \warning Inefficient: copies the content of \c m !
|
klao@286
|
136 |
StdMap(const parent &m) : parent(m) {}
|
klao@286
|
137 |
/// \brief Constructs the map from an appropriate std::map, and explicitly
|
klao@286
|
138 |
/// specifies a default value.
|
klao@286
|
139 |
///
|
klao@286
|
140 |
/// \warning Inefficient: copies the content of \c m !
|
klao@286
|
141 |
StdMap(const parent &m, const T& _v) : parent(m), v(_v) {}
|
klao@286
|
142 |
|
klao@286
|
143 |
template<typename T1, typename Comp1>
|
marci@389
|
144 |
StdMap(const StdMap<Key,T1,Comp1> &m, const T &_v) {
|
marci@389
|
145 |
//FIXME;
|
marci@389
|
146 |
}
|
klao@286
|
147 |
|
klao@286
|
148 |
ReferenceType operator[](const Key &k) {
|
klao@346
|
149 |
return insert(PairType(k,v)).first -> second;
|
klao@286
|
150 |
}
|
klao@286
|
151 |
ConstReferenceType operator[](const Key &k) const {
|
marci@389
|
152 |
typename parent::iterator i = lower_bound(k);
|
beckerjc@391
|
153 |
if (i == parent::end() || parent::key_comp()(k, (*i).first))
|
klao@286
|
154 |
return v;
|
klao@286
|
155 |
return (*i).second;
|
klao@286
|
156 |
}
|
klao@345
|
157 |
void set(const Key &k, const T &t) {
|
klao@346
|
158 |
parent::operator[](k) = t;
|
klao@345
|
159 |
}
|
klao@286
|
160 |
|
klao@286
|
161 |
/// Changes the default value of the map.
|
klao@286
|
162 |
/// \return Returns the previous default value.
|
klao@286
|
163 |
///
|
alpar@805
|
164 |
/// \warning The value of some keys (which has already been queried, but
|
klao@286
|
165 |
/// the value has been unchanged from the default) may change!
|
klao@286
|
166 |
T setDefault(const T &_v) { T old=v; v=_v; return old; }
|
klao@286
|
167 |
|
klao@286
|
168 |
template<typename T1>
|
klao@286
|
169 |
struct rebind {
|
klao@286
|
170 |
typedef StdMap<Key,T1,Compare> other;
|
klao@286
|
171 |
};
|
klao@286
|
172 |
};
|
klao@286
|
173 |
|
klao@286
|
174 |
}
|
klao@286
|
175 |
#endif // HUGO_MAPS_H
|