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