1 | 1 |
/* -*- C++ -*- |
2 | 2 |
* |
3 | 3 |
* This file is a part of LEMON, a generic C++ optimization library |
4 | 4 |
* |
5 | 5 |
* Copyright (C) 2003-2007 |
6 | 6 |
* Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport |
7 | 7 |
* (Egervary Research Group on Combinatorial Optimization, EGRES). |
8 | 8 |
* |
9 | 9 |
* Permission to use, modify and distribute this software is granted |
10 | 10 |
* provided that this copyright notice appears in all copies. For |
11 | 11 |
* precise terms see the accompanying LICENSE file. |
12 | 12 |
* |
13 | 13 |
* This software is provided "AS IS" with no warranty of any kind, |
14 | 14 |
* express or implied, and with no claim as to its suitability for any |
15 | 15 |
* purpose. |
16 | 16 |
* |
17 | 17 |
*/ |
18 | 18 |
|
19 | 19 |
#ifndef LEMON_MAPS_H |
20 | 20 |
#define LEMON_MAPS_H |
21 | 21 |
|
22 | 22 |
#include <iterator> |
23 | 23 |
#include <functional> |
24 | 24 |
#include <vector> |
25 | 25 |
|
26 | 26 |
#include <lemon/bits/utility.h> |
27 | 27 |
// #include <lemon/bits/traits.h> |
28 | 28 |
|
29 | 29 |
///\file |
30 | 30 |
///\ingroup maps |
31 | 31 |
///\brief Miscellaneous property maps |
32 | 32 |
/// |
33 | 33 |
#include <map> |
34 | 34 |
|
35 | 35 |
namespace lemon { |
36 | 36 |
|
37 | 37 |
/// \addtogroup maps |
38 | 38 |
/// @{ |
39 | 39 |
|
40 | 40 |
/// Base class of maps. |
41 | 41 |
|
42 | 42 |
/// Base class of maps. |
43 | 43 |
/// It provides the necessary <tt>typedef</tt>s required by the map concept. |
44 | 44 |
template<typename K, typename T> |
45 | 45 |
class MapBase { |
46 | 46 |
public: |
47 |
/// |
|
47 |
/// The key type of the map. |
|
48 | 48 |
typedef K Key; |
49 |
/// |
|
49 |
/// The value type of the map. (The type of objects associated with the keys). |
|
50 | 50 |
typedef T Value; |
51 | 51 |
}; |
52 | 52 |
|
53 | 53 |
/// Null map. (a.k.a. DoNothingMap) |
54 | 54 |
|
55 | 55 |
/// This map can be used if you have to provide a map only for |
56 | 56 |
/// its type definitions, or if you have to provide a writable map, |
57 | 57 |
/// but data written to it is not required (i.e. it will be sent to |
58 | 58 |
/// <tt>/dev/null</tt>). |
59 | 59 |
template<typename K, typename T> |
60 | 60 |
class NullMap : public MapBase<K, T> { |
61 | 61 |
public: |
62 | 62 |
typedef MapBase<K, T> Parent; |
63 | 63 |
typedef typename Parent::Key Key; |
64 | 64 |
typedef typename Parent::Value Value; |
65 | 65 |
|
66 | 66 |
/// Gives back a default constructed element. |
67 | 67 |
T operator[](const K&) const { return T(); } |
68 | 68 |
/// Absorbs the value. |
69 | 69 |
void set(const K&, const T&) {} |
70 | 70 |
}; |
71 | 71 |
|
72 | 72 |
///Returns a \c NullMap class |
73 | 73 |
|
74 | 74 |
///This function just returns a \c NullMap class. |
75 | 75 |
///\relates NullMap |
76 | 76 |
template <typename K, typename V> |
77 | 77 |
NullMap<K, V> nullMap() { |
78 | 78 |
return NullMap<K, V>(); |
79 | 79 |
} |
80 | 80 |
|
81 | 81 |
|
82 | 82 |
/// Constant map. |
83 | 83 |
|
84 | 84 |
/// This is a readable map which assigns a specified value to each key. |
85 | 85 |
/// In other aspects it is equivalent to the \c NullMap. |
86 | 86 |
template<typename K, typename T> |
87 | 87 |
class ConstMap : public MapBase<K, T> { |
88 | 88 |
private: |
89 | 89 |
T v; |
90 | 90 |
public: |
91 | 91 |
|
92 | 92 |
typedef MapBase<K, T> Parent; |
93 | 93 |
typedef typename Parent::Key Key; |
94 | 94 |
typedef typename Parent::Value Value; |
95 | 95 |
|
96 | 96 |
/// Default constructor |
97 | 97 |
|
98 | 98 |
/// Default constructor. |
99 | 99 |
/// The value of the map will be uninitialized. |
100 | 100 |
/// (More exactly it will be default constructed.) |
101 | 101 |
ConstMap() {} |
102 | 102 |
|
103 | 103 |
/// Constructor with specified initial value |
104 | 104 |
|
105 | 105 |
/// Constructor with specified initial value. |
106 | 106 |
/// \param _v is the initial value of the map. |
107 | 107 |
ConstMap(const T &_v) : v(_v) {} |
108 | 108 |
|
109 | 109 |
///\e |
110 | 110 |
T operator[](const K&) const { return v; } |
111 | 111 |
|
112 | 112 |
///\e |
113 | 113 |
void setAll(const T &t) { |
114 | 114 |
v = t; |
115 | 115 |
} |
116 | 116 |
|
117 | 117 |
template<typename T1> |
118 | 118 |
struct rebind { |
119 | 119 |
typedef ConstMap<K, T1> other; |
120 | 120 |
}; |
121 | 121 |
|
122 | 122 |
template<typename T1> |
123 | 123 |
ConstMap(const ConstMap<K, T1> &, const T &_v) : v(_v) {} |
124 | 124 |
}; |
125 | 125 |
|
126 | 126 |
///Returns a \c ConstMap class |
127 | 127 |
|
128 | 128 |
///This function just returns a \c ConstMap class. |
129 | 129 |
///\relates ConstMap |
130 | 130 |
template<typename K, typename V> |
131 | 131 |
inline ConstMap<K, V> constMap(const V &v) { |
132 | 132 |
return ConstMap<K, V>(v); |
133 | 133 |
} |
134 | 134 |
|
135 | 135 |
|
136 | 136 |
template<typename T, T v> |
137 | 137 |
struct Const { }; |
138 | 138 |
|
139 | 139 |
/// Constant map with inlined constant value. |
140 | 140 |
|
141 | 141 |
/// This is a readable map which assigns a specified value to each key. |
142 | 142 |
/// In other aspects it is equivalent to the \c NullMap. |
143 | 143 |
template<typename K, typename V, V v> |
144 | 144 |
class ConstMap<K, Const<V, v> > : public MapBase<K, V> { |
145 | 145 |
public: |
... | ... |
@@ -156,195 +156,195 @@ |
156 | 156 |
|
157 | 157 |
///Returns a \c ConstMap class |
158 | 158 |
|
159 | 159 |
///This function just returns a \c ConstMap class with inlined value. |
160 | 160 |
///\relates ConstMap |
161 | 161 |
template<typename K, typename V, V v> |
162 | 162 |
inline ConstMap<K, Const<V, v> > constMap() { |
163 | 163 |
return ConstMap<K, Const<V, v> >(); |
164 | 164 |
} |
165 | 165 |
|
166 | 166 |
///Map based on std::map |
167 | 167 |
|
168 | 168 |
///This is essentially a wrapper for \c std::map with addition that |
169 | 169 |
///you can specify a default value different from \c Value(). |
170 | 170 |
template <typename K, typename T, typename Compare = std::less<K> > |
171 | 171 |
class StdMap { |
172 | 172 |
template <typename K1, typename T1, typename C1> |
173 | 173 |
friend class StdMap; |
174 | 174 |
public: |
175 | 175 |
|
176 | 176 |
typedef True ReferenceMapTag; |
177 | 177 |
///\e |
178 | 178 |
typedef K Key; |
179 | 179 |
///\e |
180 | 180 |
typedef T Value; |
181 | 181 |
///\e |
182 | 182 |
typedef T& Reference; |
183 | 183 |
///\e |
184 | 184 |
typedef const T& ConstReference; |
185 | 185 |
|
186 | 186 |
private: |
187 | 187 |
|
188 | 188 |
typedef std::map<K, T, Compare> Map; |
189 | 189 |
Value _value; |
190 | 190 |
Map _map; |
191 | 191 |
|
192 | 192 |
public: |
193 | 193 |
|
194 | 194 |
/// Constructor with specified default value |
195 | 195 |
StdMap(const T& value = T()) : _value(value) {} |
196 | 196 |
/// \brief Constructs the map from an appropriate std::map, and explicitly |
197 | 197 |
/// specifies a default value. |
198 | 198 |
template <typename T1, typename Comp1> |
199 | 199 |
StdMap(const std::map<Key, T1, Comp1> &map, const T& value = T()) |
200 | 200 |
: _map(map.begin(), map.end()), _value(value) {} |
201 | 201 |
|
202 | 202 |
/// \brief Constructs a map from an other StdMap. |
203 | 203 |
template<typename T1, typename Comp1> |
204 | 204 |
StdMap(const StdMap<Key, T1, Comp1> &c) |
205 | 205 |
: _map(c._map.begin(), c._map.end()), _value(c._value) {} |
206 | 206 |
|
207 | 207 |
private: |
208 | 208 |
|
209 | 209 |
StdMap& operator=(const StdMap&); |
210 | 210 |
|
211 | 211 |
public: |
212 | 212 |
|
213 | 213 |
///\e |
214 | 214 |
Reference operator[](const Key &k) { |
215 | 215 |
typename Map::iterator it = _map.lower_bound(k); |
216 | 216 |
if (it != _map.end() && !_map.key_comp()(k, it->first)) |
217 | 217 |
return it->second; |
218 | 218 |
else |
219 | 219 |
return _map.insert(it, std::make_pair(k, _value))->second; |
220 | 220 |
} |
221 | 221 |
|
222 | 222 |
/// \e |
223 | 223 |
ConstReference operator[](const Key &k) const { |
224 | 224 |
typename Map::const_iterator it = _map.find(k); |
225 | 225 |
if (it != _map.end()) |
226 | 226 |
return it->second; |
227 | 227 |
else |
228 | 228 |
return _value; |
229 | 229 |
} |
230 | 230 |
|
231 | 231 |
/// \e |
232 | 232 |
void set(const Key &k, const T &t) { |
233 | 233 |
typename Map::iterator it = _map.lower_bound(k); |
234 | 234 |
if (it != _map.end() && !_map.key_comp()(k, it->first)) |
235 | 235 |
it->second = t; |
236 | 236 |
else |
237 | 237 |
_map.insert(it, std::make_pair(k, t)); |
238 | 238 |
} |
239 | 239 |
|
240 | 240 |
/// \e |
241 | 241 |
void setAll(const T &t) { |
242 | 242 |
_value = t; |
243 | 243 |
_map.clear(); |
244 | 244 |
} |
245 | 245 |
|
246 | 246 |
template <typename T1, typename C1 = std::less<T1> > |
247 | 247 |
struct rebind { |
248 | 248 |
typedef StdMap<Key, T1, C1> other; |
249 | 249 |
}; |
250 | 250 |
}; |
251 | 251 |
|
252 |
/// \brief Map for storing values for the range |
|
252 |
/// \brief Map for storing values for keys from the range <tt>[0..size-1]</tt> |
|
253 | 253 |
/// |
254 |
/// The current map has the |
|
254 |
/// The current map has the <tt>[0..size-1]</tt> keyset and the values |
|
255 | 255 |
/// are stored in a \c std::vector<T> container. It can be used with |
256 | 256 |
/// some data structures, for example \c UnionFind, \c BinHeap, when |
257 | 257 |
/// the used items are small integer numbers. |
258 | 258 |
/// |
259 | 259 |
/// \todo Revise its name |
260 | 260 |
template <typename T> |
261 | 261 |
class IntegerMap { |
262 | 262 |
|
263 | 263 |
template <typename T1> |
264 | 264 |
friend class IntegerMap; |
265 | 265 |
|
266 | 266 |
public: |
267 | 267 |
|
268 | 268 |
typedef True ReferenceMapTag; |
269 | 269 |
///\e |
270 | 270 |
typedef int Key; |
271 | 271 |
///\e |
272 | 272 |
typedef T Value; |
273 | 273 |
///\e |
274 | 274 |
typedef T& Reference; |
275 | 275 |
///\e |
276 | 276 |
typedef const T& ConstReference; |
277 | 277 |
|
278 | 278 |
private: |
279 | 279 |
|
280 | 280 |
typedef std::vector<T> Vector; |
281 | 281 |
Vector _vector; |
282 | 282 |
|
283 | 283 |
public: |
284 | 284 |
|
285 | 285 |
/// Constructor with specified default value |
286 | 286 |
IntegerMap(int size = 0, const T& value = T()) : _vector(size, value) {} |
287 | 287 |
|
288 | 288 |
/// \brief Constructs the map from an appropriate std::vector. |
289 | 289 |
template <typename T1> |
290 | 290 |
IntegerMap(const std::vector<T1>& vector) |
291 | 291 |
: _vector(vector.begin(), vector.end()) {} |
292 | 292 |
|
293 | 293 |
/// \brief Constructs a map from an other IntegerMap. |
294 | 294 |
template <typename T1> |
295 | 295 |
IntegerMap(const IntegerMap<T1> &c) |
296 | 296 |
: _vector(c._vector.begin(), c._vector.end()) {} |
297 | 297 |
|
298 | 298 |
/// \brief Resize the container |
299 | 299 |
void resize(int size, const T& value = T()) { |
300 | 300 |
_vector.resize(size, value); |
301 | 301 |
} |
302 | 302 |
|
303 | 303 |
private: |
304 | 304 |
|
305 | 305 |
IntegerMap& operator=(const IntegerMap&); |
306 | 306 |
|
307 | 307 |
public: |
308 | 308 |
|
309 | 309 |
///\e |
310 | 310 |
Reference operator[](Key k) { |
311 | 311 |
return _vector[k]; |
312 | 312 |
} |
313 | 313 |
|
314 | 314 |
/// \e |
315 | 315 |
ConstReference operator[](Key k) const { |
316 | 316 |
return _vector[k]; |
317 | 317 |
} |
318 | 318 |
|
319 | 319 |
/// \e |
320 | 320 |
void set(const Key &k, const T& t) { |
321 | 321 |
_vector[k] = t; |
322 | 322 |
} |
323 | 323 |
|
324 | 324 |
}; |
325 | 325 |
|
326 | 326 |
/// @} |
327 | 327 |
|
328 | 328 |
/// \addtogroup map_adaptors |
329 | 329 |
/// @{ |
330 | 330 |
|
331 | 331 |
/// \brief Identity map. |
332 | 332 |
/// |
333 | 333 |
/// This map gives back the given key as value without any |
334 | 334 |
/// modification. |
335 | 335 |
template <typename T> |
336 | 336 |
class IdentityMap : public MapBase<T, T> { |
337 | 337 |
public: |
338 | 338 |
typedef MapBase<T, T> Parent; |
339 | 339 |
typedef typename Parent::Key Key; |
340 | 340 |
typedef typename Parent::Value Value; |
341 | 341 |
|
342 | 342 |
/// \e |
343 | 343 |
const T& operator[](const T& t) const { |
344 | 344 |
return t; |
345 | 345 |
} |
346 | 346 |
}; |
347 | 347 |
|
348 | 348 |
///Returns an \c IdentityMap class |
349 | 349 |
|
350 | 350 |
///This function just returns an \c IdentityMap class. |
... | ... |
@@ -736,327 +736,328 @@ |
736 | 736 |
|
737 | 737 |
///Composition of two maps |
738 | 738 |
|
739 | 739 |
///This \c concepts::ReadMap "read only map" returns the composition of |
740 | 740 |
///two given maps. |
741 | 741 |
///That is to say, if \c m1 is of type \c M1 and \c m2 is of \c M2, |
742 | 742 |
///then for |
743 | 743 |
///\code |
744 | 744 |
/// ComposeMap<M1, M2> cm(m1,m2); |
745 | 745 |
///\endcode |
746 | 746 |
/// <tt>cm[x]</tt> will be equal to <tt>m1[m2[x]]</tt>. |
747 | 747 |
/// |
748 | 748 |
///Its \c Key is inherited from \c M2 and its \c Value is from \c M1. |
749 | 749 |
///\c M2::Value must be convertible to \c M1::Key. |
750 | 750 |
/// |
751 | 751 |
///\sa CombineMap |
752 | 752 |
/// |
753 | 753 |
///\todo Check the requirements. |
754 | 754 |
template <typename M1, typename M2> |
755 | 755 |
class ComposeMap : public MapBase<typename M2::Key, typename M1::Value> { |
756 | 756 |
const M1& m1; |
757 | 757 |
const M2& m2; |
758 | 758 |
public: |
759 | 759 |
typedef MapBase<typename M2::Key, typename M1::Value> Parent; |
760 | 760 |
typedef typename Parent::Key Key; |
761 | 761 |
typedef typename Parent::Value Value; |
762 | 762 |
|
763 | 763 |
///Constructor |
764 | 764 |
ComposeMap(const M1 &_m1,const M2 &_m2) : m1(_m1), m2(_m2) {}; |
765 | 765 |
|
766 | 766 |
/// \e |
767 | 767 |
|
768 | 768 |
|
769 | 769 |
/// \todo Use the MapTraits once it is ported. |
770 | 770 |
/// |
771 | 771 |
|
772 | 772 |
//typename MapTraits<M1>::ConstReturnValue |
773 | 773 |
typename M1::Value |
774 | 774 |
operator[](Key k) const {return m1[m2[k]];} |
775 | 775 |
}; |
776 | 776 |
|
777 | 777 |
///Returns a \c ComposeMap class |
778 | 778 |
|
779 | 779 |
///This function just returns a \c ComposeMap class. |
780 | 780 |
///\relates ComposeMap |
781 | 781 |
template <typename M1, typename M2> |
782 | 782 |
inline ComposeMap<M1, M2> composeMap(const M1 &m1,const M2 &m2) { |
783 | 783 |
return ComposeMap<M1, M2>(m1,m2); |
784 | 784 |
} |
785 | 785 |
|
786 | 786 |
///Combine of two maps using an STL (binary) functor. |
787 | 787 |
|
788 | 788 |
///Combine of two maps using an STL (binary) functor. |
789 | 789 |
/// |
790 | 790 |
///This \c concepts::ReadMap "read only map" takes two maps and a |
791 | 791 |
///binary functor and returns the composition of the two |
792 | 792 |
///given maps unsing the functor. |
793 | 793 |
///That is to say, if \c m1 and \c m2 is of type \c M1 and \c M2 |
794 | 794 |
///and \c f is of \c F, then for |
795 | 795 |
///\code |
796 | 796 |
/// CombineMap<M1,M2,F,V> cm(m1,m2,f); |
797 | 797 |
///\endcode |
798 | 798 |
/// <tt>cm[x]</tt> will be equal to <tt>f(m1[x],m2[x])</tt> |
799 | 799 |
/// |
800 | 800 |
///Its \c Key is inherited from \c M1 and its \c Value is \c V. |
801 | 801 |
///\c M2::Value and \c M1::Value must be convertible to the corresponding |
802 | 802 |
///input parameter of \c F and the return type of \c F must be convertible |
803 | 803 |
///to \c V. |
804 | 804 |
/// |
805 | 805 |
///\sa ComposeMap |
806 | 806 |
/// |
807 | 807 |
///\todo Check the requirements. |
808 | 808 |
template<typename M1, typename M2, typename F, |
809 | 809 |
typename V = typename F::result_type> |
810 | 810 |
class CombineMap : public MapBase<typename M1::Key, V> { |
811 | 811 |
const M1& m1; |
812 | 812 |
const M2& m2; |
813 | 813 |
F f; |
814 | 814 |
public: |
815 | 815 |
typedef MapBase<typename M1::Key, V> Parent; |
816 | 816 |
typedef typename Parent::Key Key; |
817 | 817 |
typedef typename Parent::Value Value; |
818 | 818 |
|
819 | 819 |
///Constructor |
820 | 820 |
CombineMap(const M1 &_m1,const M2 &_m2,const F &_f = F()) |
821 | 821 |
: m1(_m1), m2(_m2), f(_f) {}; |
822 | 822 |
/// \e |
823 | 823 |
Value operator[](Key k) const {return f(m1[k],m2[k]);} |
824 | 824 |
}; |
825 | 825 |
|
826 | 826 |
///Returns a \c CombineMap class |
827 | 827 |
|
828 | 828 |
///This function just returns a \c CombineMap class. |
829 | 829 |
/// |
830 | 830 |
///For example if \c m1 and \c m2 are both \c double valued maps, then |
831 | 831 |
///\code |
832 |
///combineMap |
|
832 |
///combineMap(m1,m2,std::plus<double>()) |
|
833 | 833 |
///\endcode |
834 | 834 |
///is equivalent to |
835 | 835 |
///\code |
836 | 836 |
///addMap(m1,m2) |
837 | 837 |
///\endcode |
838 | 838 |
/// |
839 | 839 |
///This function is specialized for adaptable binary function |
840 | 840 |
///classes and C++ functions. |
841 | 841 |
/// |
842 | 842 |
///\relates CombineMap |
843 | 843 |
template<typename M1, typename M2, typename F, typename V> |
844 | 844 |
inline CombineMap<M1, M2, F, V> |
845 | 845 |
combineMap(const M1& m1,const M2& m2, const F& f) { |
846 | 846 |
return CombineMap<M1, M2, F, V>(m1,m2,f); |
847 | 847 |
} |
848 | 848 |
|
849 | 849 |
template<typename M1, typename M2, typename F> |
850 | 850 |
inline CombineMap<M1, M2, F, typename F::result_type> |
851 | 851 |
combineMap(const M1& m1, const M2& m2, const F& f) { |
852 | 852 |
return combineMap<M1, M2, F, typename F::result_type>(m1,m2,f); |
853 | 853 |
} |
854 | 854 |
|
855 | 855 |
template<typename M1, typename M2, typename K1, typename K2, typename V> |
856 | 856 |
inline CombineMap<M1, M2, V (*)(K1, K2), V> |
857 | 857 |
combineMap(const M1 &m1, const M2 &m2, V (*f)(K1, K2)) { |
858 | 858 |
return combineMap<M1, M2, V (*)(K1, K2), V>(m1,m2,f); |
859 | 859 |
} |
860 | 860 |
|
861 | 861 |
///Negative value of a map |
862 | 862 |
|
863 | 863 |
///This \c concepts::ReadMap "read only map" returns the negative |
864 | 864 |
///value of the value returned by the given map. |
865 | 865 |
///Its \c Key and \c Value are inherited from \c M. |
866 | 866 |
///The unary \c - operator must be defined for \c Value, of course. |
867 | 867 |
/// |
868 | 868 |
///\sa NegWriteMap |
869 | 869 |
template<typename M> |
870 | 870 |
class NegMap : public MapBase<typename M::Key, typename M::Value> { |
871 | 871 |
const M& m; |
872 | 872 |
public: |
873 | 873 |
typedef MapBase<typename M::Key, typename M::Value> Parent; |
874 | 874 |
typedef typename Parent::Key Key; |
875 | 875 |
typedef typename Parent::Value Value; |
876 | 876 |
|
877 | 877 |
///Constructor |
878 | 878 |
NegMap(const M &_m) : m(_m) {}; |
879 | 879 |
/// \e |
880 | 880 |
Value operator[](Key k) const {return -m[k];} |
881 | 881 |
}; |
882 | 882 |
|
883 | 883 |
///Negative value of a map (ReadWrite version) |
884 | 884 |
|
885 | 885 |
///This \c concepts::ReadWriteMap "read-write map" returns the negative |
886 | 886 |
///value of the value returned by the given map. |
887 | 887 |
///Its \c Key and \c Value are inherited from \c M. |
888 | 888 |
///The unary \c - operator must be defined for \c Value, of course. |
889 | 889 |
/// |
890 | 890 |
/// \sa NegMap |
891 | 891 |
template<typename M> |
892 | 892 |
class NegWriteMap : public MapBase<typename M::Key, typename M::Value> { |
893 | 893 |
M& m; |
894 | 894 |
public: |
895 | 895 |
typedef MapBase<typename M::Key, typename M::Value> Parent; |
896 | 896 |
typedef typename Parent::Key Key; |
897 | 897 |
typedef typename Parent::Value Value; |
898 | 898 |
|
899 | 899 |
///Constructor |
900 | 900 |
NegWriteMap(M &_m) : m(_m) {}; |
901 | 901 |
/// \e |
902 | 902 |
Value operator[](Key k) const {return -m[k];} |
903 | 903 |
/// \e |
904 | 904 |
void set(Key k, const Value& v) { m.set(k, -v); } |
905 | 905 |
}; |
906 | 906 |
|
907 | 907 |
///Returns a \c NegMap class |
908 | 908 |
|
909 | 909 |
///This function just returns a \c NegMap class. |
910 | 910 |
///\relates NegMap |
911 | 911 |
template <typename M> |
912 | 912 |
inline NegMap<M> negMap(const M &m) { |
913 | 913 |
return NegMap<M>(m); |
914 | 914 |
} |
915 | 915 |
|
916 | 916 |
///Returns a \c NegWriteMap class |
917 | 917 |
|
918 | 918 |
///This function just returns a \c NegWriteMap class. |
919 | 919 |
///\relates NegWriteMap |
920 | 920 |
template <typename M> |
921 | 921 |
inline NegWriteMap<M> negMap(M &m) { |
922 | 922 |
return NegWriteMap<M>(m); |
923 | 923 |
} |
924 | 924 |
|
925 | 925 |
///Absolute value of a map |
926 | 926 |
|
927 | 927 |
///This \c concepts::ReadMap "read only map" returns the absolute value |
928 | 928 |
///of the value returned by the given map. |
929 | 929 |
///Its \c Key and \c Value are inherited from \c M. |
930 | 930 |
///\c Value must be comparable to \c 0 and the unary \c - |
931 | 931 |
///operator must be defined for it, of course. |
932 | 932 |
template<typename M> |
933 | 933 |
class AbsMap : public MapBase<typename M::Key, typename M::Value> { |
934 | 934 |
const M& m; |
935 | 935 |
public: |
936 | 936 |
typedef MapBase<typename M::Key, typename M::Value> Parent; |
937 | 937 |
typedef typename Parent::Key Key; |
938 | 938 |
typedef typename Parent::Value Value; |
939 | 939 |
|
940 | 940 |
///Constructor |
941 | 941 |
AbsMap(const M &_m) : m(_m) {}; |
942 | 942 |
/// \e |
943 | 943 |
Value operator[](Key k) const { |
944 | 944 |
Value tmp = m[k]; |
945 | 945 |
return tmp >= 0 ? tmp : -tmp; |
946 | 946 |
} |
947 | 947 |
|
948 | 948 |
}; |
949 | 949 |
|
950 | 950 |
///Returns an \c AbsMap class |
951 | 951 |
|
952 | 952 |
///This function just returns an \c AbsMap class. |
953 | 953 |
///\relates AbsMap |
954 | 954 |
template<typename M> |
955 | 955 |
inline AbsMap<M> absMap(const M &m) { |
956 | 956 |
return AbsMap<M>(m); |
957 | 957 |
} |
958 | 958 |
|
959 | 959 |
///Converts an STL style functor to a map |
960 | 960 |
|
961 | 961 |
///This \c concepts::ReadMap "read only map" returns the value |
962 | 962 |
///of a given functor. |
963 | 963 |
/// |
964 | 964 |
///Template parameters \c K and \c V will become its |
965 |
///\c Key and \c Value. They must be given explicitly |
|
966 |
///because a functor does not provide such typedefs. |
|
965 |
///\c Key and \c Value. |
|
966 |
///In most cases they have to be given explicitly because a |
|
967 |
///functor typically does not provide such typedefs. |
|
967 | 968 |
/// |
968 | 969 |
///Parameter \c F is the type of the used functor. |
969 | 970 |
/// |
970 | 971 |
///\sa MapFunctor |
971 | 972 |
template<typename F, |
972 | 973 |
typename K = typename F::argument_type, |
973 | 974 |
typename V = typename F::result_type> |
974 | 975 |
class FunctorMap : public MapBase<K, V> { |
975 | 976 |
F f; |
976 | 977 |
public: |
977 | 978 |
typedef MapBase<K, V> Parent; |
978 | 979 |
typedef typename Parent::Key Key; |
979 | 980 |
typedef typename Parent::Value Value; |
980 | 981 |
|
981 | 982 |
///Constructor |
982 | 983 |
FunctorMap(const F &_f = F()) : f(_f) {} |
983 | 984 |
/// \e |
984 | 985 |
Value operator[](Key k) const { return f(k);} |
985 | 986 |
}; |
986 | 987 |
|
987 | 988 |
///Returns a \c FunctorMap class |
988 | 989 |
|
989 | 990 |
///This function just returns a \c FunctorMap class. |
990 | 991 |
/// |
991 | 992 |
///It is specialized for adaptable function classes and |
992 | 993 |
///C++ functions. |
993 | 994 |
///\relates FunctorMap |
994 | 995 |
template<typename K, typename V, typename F> inline |
995 | 996 |
FunctorMap<F, K, V> functorMap(const F &f) { |
996 | 997 |
return FunctorMap<F, K, V>(f); |
997 | 998 |
} |
998 | 999 |
|
999 | 1000 |
template <typename F> inline |
1000 | 1001 |
FunctorMap<F, typename F::argument_type, typename F::result_type> |
1001 | 1002 |
functorMap(const F &f) { |
1002 | 1003 |
return FunctorMap<F, typename F::argument_type, |
1003 | 1004 |
typename F::result_type>(f); |
1004 | 1005 |
} |
1005 | 1006 |
|
1006 | 1007 |
template <typename K, typename V> inline |
1007 | 1008 |
FunctorMap<V (*)(K), K, V> functorMap(V (*f)(K)) { |
1008 | 1009 |
return FunctorMap<V (*)(K), K, V>(f); |
1009 | 1010 |
} |
1010 | 1011 |
|
1011 | 1012 |
|
1012 | 1013 |
///Converts a map to an STL style (unary) functor |
1013 | 1014 |
|
1014 | 1015 |
///This class Converts a map to an STL style (unary) functor. |
1015 | 1016 |
///that is it provides an <tt>operator()</tt> to read its values. |
1016 | 1017 |
/// |
1017 | 1018 |
///For the sake of convenience it also works as |
1018 | 1019 |
///a ususal \c concepts::ReadMap "readable map", |
1019 | 1020 |
///i.e. <tt>operator[]</tt> and the \c Key and \c Value typedefs also exist. |
1020 | 1021 |
/// |
1021 | 1022 |
///\sa FunctorMap |
1022 | 1023 |
template <typename M> |
1023 | 1024 |
class MapFunctor : public MapBase<typename M::Key, typename M::Value> { |
1024 | 1025 |
const M& m; |
1025 | 1026 |
public: |
1026 | 1027 |
typedef MapBase<typename M::Key, typename M::Value> Parent; |
1027 | 1028 |
typedef typename Parent::Key Key; |
1028 | 1029 |
typedef typename Parent::Value Value; |
1029 | 1030 |
|
1030 | 1031 |
typedef typename M::Key argument_type; |
1031 | 1032 |
typedef typename M::Value result_type; |
1032 | 1033 |
|
1033 | 1034 |
///Constructor |
1034 | 1035 |
MapFunctor(const M &_m) : m(_m) {}; |
1035 | 1036 |
///\e |
1036 | 1037 |
Value operator()(Key k) const {return m[k];} |
1037 | 1038 |
///\e |
1038 | 1039 |
Value operator[](Key k) const {return m[k];} |
1039 | 1040 |
}; |
1040 | 1041 |
|
1041 | 1042 |
///Returns a \c MapFunctor class |
1042 | 1043 |
|
1043 | 1044 |
///This function just returns a \c MapFunctor class. |
1044 | 1045 |
///\relates MapFunctor |
1045 | 1046 |
template<typename M> |
1046 | 1047 |
inline MapFunctor<M> mapFunctor(const M &m) { |
1047 | 1048 |
return MapFunctor<M>(m); |
1048 | 1049 |
} |
1049 | 1050 |
|
1050 | 1051 |
///Applies all map setting operations to two maps |
1051 | 1052 |
|
1052 | 1053 |
///This map has two \c concepts::ReadMap "readable map" |
1053 | 1054 |
///parameters and each read request will be passed just to the |
1054 | 1055 |
///first map. This class is the just readable map type of the ForkWriteMap. |
1055 | 1056 |
/// |
1056 | 1057 |
///The \c Key and \c Value are inherited from \c M1. |
1057 | 1058 |
///The \c Key and \c Value of M2 must be convertible from those of \c M1. |
1058 | 1059 |
/// |
1059 | 1060 |
///\sa ForkWriteMap |
1060 | 1061 |
/// |
1061 | 1062 |
/// \todo Why is it needed? |
1062 | 1063 |
template<typename M1, typename M2> |
... | ... |
@@ -1147,293 +1148,295 @@ |
1147 | 1148 |
Value operator[](Key k) const {return !m[k];} |
1148 | 1149 |
}; |
1149 | 1150 |
|
1150 | 1151 |
///Logical 'not' of a map (ReadWrie version) |
1151 | 1152 |
|
1152 | 1153 |
///This bool \c concepts::ReadWriteMap "read-write map" returns the |
1153 | 1154 |
///logical negation of the value returned by the given map. When it is set, |
1154 | 1155 |
///the opposite value is set to the original map. |
1155 | 1156 |
///Its \c Key is inherited from \c M, its Value is \c bool. |
1156 | 1157 |
/// |
1157 | 1158 |
///\sa NotMap |
1158 | 1159 |
template <typename M> |
1159 | 1160 |
class NotWriteMap : public MapBase<typename M::Key, bool> { |
1160 | 1161 |
M& m; |
1161 | 1162 |
public: |
1162 | 1163 |
typedef MapBase<typename M::Key, bool> Parent; |
1163 | 1164 |
typedef typename Parent::Key Key; |
1164 | 1165 |
typedef typename Parent::Value Value; |
1165 | 1166 |
|
1166 | 1167 |
/// Constructor |
1167 | 1168 |
NotWriteMap(M &_m) : m(_m) {}; |
1168 | 1169 |
///\e |
1169 | 1170 |
Value operator[](Key k) const {return !m[k];} |
1170 | 1171 |
///\e |
1171 | 1172 |
void set(Key k, bool v) { m.set(k, !v); } |
1172 | 1173 |
}; |
1173 | 1174 |
|
1174 | 1175 |
///Returns a \c NotMap class |
1175 | 1176 |
|
1176 | 1177 |
///This function just returns a \c NotMap class. |
1177 | 1178 |
///\relates NotMap |
1178 | 1179 |
template <typename M> |
1179 | 1180 |
inline NotMap<M> notMap(const M &m) { |
1180 | 1181 |
return NotMap<M>(m); |
1181 | 1182 |
} |
1182 | 1183 |
|
1183 | 1184 |
///Returns a \c NotWriteMap class |
1184 | 1185 |
|
1185 | 1186 |
///This function just returns a \c NotWriteMap class. |
1186 | 1187 |
///\relates NotWriteMap |
1187 | 1188 |
template <typename M> |
1188 | 1189 |
inline NotWriteMap<M> notMap(M &m) { |
1189 | 1190 |
return NotWriteMap<M>(m); |
1190 | 1191 |
} |
1191 | 1192 |
|
1192 | 1193 |
namespace _maps_bits { |
1193 | 1194 |
|
1194 | 1195 |
template <typename Value> |
1195 | 1196 |
struct Identity { |
1196 | 1197 |
typedef Value argument_type; |
1197 | 1198 |
typedef Value result_type; |
1198 | 1199 |
Value operator()(const Value& val) const { |
1199 | 1200 |
return val; |
1200 | 1201 |
} |
1201 | 1202 |
}; |
1202 | 1203 |
|
1203 | 1204 |
template <typename _Iterator, typename Enable = void> |
1204 | 1205 |
struct IteratorTraits { |
1205 | 1206 |
typedef typename std::iterator_traits<_Iterator>::value_type Value; |
1206 | 1207 |
}; |
1207 | 1208 |
|
1208 | 1209 |
template <typename _Iterator> |
1209 | 1210 |
struct IteratorTraits<_Iterator, |
1210 | 1211 |
typename exists<typename _Iterator::container_type>::type> |
1211 | 1212 |
{ |
1212 | 1213 |
typedef typename _Iterator::container_type::value_type Value; |
1213 | 1214 |
}; |
1214 | 1215 |
|
1215 | 1216 |
} |
1216 | 1217 |
|
1217 | 1218 |
|
1218 | 1219 |
/// \brief Writable bool map for logging each \c true assigned element |
1219 | 1220 |
/// |
1220 | 1221 |
/// Writable bool map for logging each \c true assigned element, i.e it |
1221 | 1222 |
/// copies all the keys set to \c true to the given iterator. |
1222 | 1223 |
/// |
1223 | 1224 |
/// \note The container of the iterator should contain space |
1224 | 1225 |
/// for each element. |
1225 | 1226 |
/// |
1226 | 1227 |
/// The following example shows how you can write the edges found by the Prim |
1227 | 1228 |
/// algorithm directly |
1228 | 1229 |
/// to the standard output. |
1229 | 1230 |
///\code |
1230 | 1231 |
/// typedef IdMap<Graph, Edge> EdgeIdMap; |
1231 | 1232 |
/// EdgeIdMap edgeId(graph); |
1232 | 1233 |
/// |
1233 | 1234 |
/// typedef MapFunctor<EdgeIdMap> EdgeIdFunctor; |
1234 | 1235 |
/// EdgeIdFunctor edgeIdFunctor(edgeId); |
1235 | 1236 |
/// |
1236 | 1237 |
/// StoreBoolMap<ostream_iterator<int>, EdgeIdFunctor> |
1237 | 1238 |
/// writerMap(ostream_iterator<int>(cout, " "), edgeIdFunctor); |
1238 | 1239 |
/// |
1239 | 1240 |
/// prim(graph, cost, writerMap); |
1240 | 1241 |
///\endcode |
1241 | 1242 |
/// |
1242 | 1243 |
///\sa BackInserterBoolMap |
1244 |
///\sa FrontInserterBoolMap |
|
1245 |
///\sa InserterBoolMap |
|
1243 | 1246 |
/// |
1244 | 1247 |
///\todo Revise the name of this class and the related ones. |
1245 | 1248 |
template <typename _Iterator, |
1246 | 1249 |
typename _Functor = |
1247 | 1250 |
_maps_bits::Identity<typename _maps_bits:: |
1248 | 1251 |
IteratorTraits<_Iterator>::Value> > |
1249 | 1252 |
class StoreBoolMap { |
1250 | 1253 |
public: |
1251 | 1254 |
typedef _Iterator Iterator; |
1252 | 1255 |
|
1253 | 1256 |
typedef typename _Functor::argument_type Key; |
1254 | 1257 |
typedef bool Value; |
1255 | 1258 |
|
1256 | 1259 |
typedef _Functor Functor; |
1257 | 1260 |
|
1258 | 1261 |
/// Constructor |
1259 | 1262 |
StoreBoolMap(Iterator it, const Functor& functor = Functor()) |
1260 | 1263 |
: _begin(it), _end(it), _functor(functor) {} |
1261 | 1264 |
|
1262 | 1265 |
/// Gives back the given iterator set for the first key |
1263 | 1266 |
Iterator begin() const { |
1264 | 1267 |
return _begin; |
1265 | 1268 |
} |
1266 | 1269 |
|
1267 | 1270 |
/// Gives back the the 'after the last' iterator |
1268 | 1271 |
Iterator end() const { |
1269 | 1272 |
return _end; |
1270 | 1273 |
} |
1271 | 1274 |
|
1272 | 1275 |
/// The \c set function of the map |
1273 | 1276 |
void set(const Key& key, Value value) const { |
1274 | 1277 |
if (value) { |
1275 | 1278 |
*_end++ = _functor(key); |
1276 | 1279 |
} |
1277 | 1280 |
} |
1278 | 1281 |
|
1279 | 1282 |
private: |
1280 | 1283 |
Iterator _begin; |
1281 | 1284 |
mutable Iterator _end; |
1282 | 1285 |
Functor _functor; |
1283 | 1286 |
}; |
1284 | 1287 |
|
1285 | 1288 |
/// \brief Writable bool map for logging each \c true assigned element in |
1286 | 1289 |
/// a back insertable container. |
1287 | 1290 |
/// |
1288 | 1291 |
/// Writable bool map for logging each \c true assigned element by pushing |
1289 | 1292 |
/// them into a back insertable container. |
1290 | 1293 |
/// It can be used to retrieve the items into a standard |
1291 | 1294 |
/// container. The next example shows how you can store the |
1292 | 1295 |
/// edges found by the Prim algorithm in a vector. |
1293 | 1296 |
/// |
1294 | 1297 |
///\code |
1295 | 1298 |
/// vector<Edge> span_tree_edges; |
1296 | 1299 |
/// BackInserterBoolMap<vector<Edge> > inserter_map(span_tree_edges); |
1297 | 1300 |
/// prim(graph, cost, inserter_map); |
1298 | 1301 |
///\endcode |
1299 | 1302 |
/// |
1300 | 1303 |
///\sa StoreBoolMap |
1301 | 1304 |
///\sa FrontInserterBoolMap |
1302 | 1305 |
///\sa InserterBoolMap |
1303 | 1306 |
template <typename Container, |
1304 | 1307 |
typename Functor = |
1305 | 1308 |
_maps_bits::Identity<typename Container::value_type> > |
1306 | 1309 |
class BackInserterBoolMap { |
1307 | 1310 |
public: |
1308 |
typedef typename |
|
1311 |
typedef typename Functor::argument_type Key; |
|
1309 | 1312 |
typedef bool Value; |
1310 | 1313 |
|
1311 | 1314 |
/// Constructor |
1312 | 1315 |
BackInserterBoolMap(Container& _container, |
1313 | 1316 |
const Functor& _functor = Functor()) |
1314 | 1317 |
: container(_container), functor(_functor) {} |
1315 | 1318 |
|
1316 | 1319 |
/// The \c set function of the map |
1317 | 1320 |
void set(const Key& key, Value value) { |
1318 | 1321 |
if (value) { |
1319 | 1322 |
container.push_back(functor(key)); |
1320 | 1323 |
} |
1321 | 1324 |
} |
1322 | 1325 |
|
1323 | 1326 |
private: |
1324 | 1327 |
Container& container; |
1325 | 1328 |
Functor functor; |
1326 | 1329 |
}; |
1327 | 1330 |
|
1328 | 1331 |
/// \brief Writable bool map for logging each \c true assigned element in |
1329 | 1332 |
/// a front insertable container. |
1330 | 1333 |
/// |
1331 | 1334 |
/// Writable bool map for logging each \c true assigned element by pushing |
1332 | 1335 |
/// them into a front insertable container. |
1333 | 1336 |
/// It can be used to retrieve the items into a standard |
1334 | 1337 |
/// container. For example see \ref BackInserterBoolMap. |
1335 | 1338 |
/// |
1336 | 1339 |
///\sa BackInserterBoolMap |
1337 | 1340 |
///\sa InserterBoolMap |
1338 | 1341 |
template <typename Container, |
1339 | 1342 |
typename Functor = |
1340 | 1343 |
_maps_bits::Identity<typename Container::value_type> > |
1341 | 1344 |
class FrontInserterBoolMap { |
1342 | 1345 |
public: |
1343 |
typedef typename |
|
1346 |
typedef typename Functor::argument_type Key; |
|
1344 | 1347 |
typedef bool Value; |
1345 | 1348 |
|
1346 | 1349 |
/// Constructor |
1347 | 1350 |
FrontInserterBoolMap(Container& _container, |
1348 | 1351 |
const Functor& _functor = Functor()) |
1349 | 1352 |
: container(_container), functor(_functor) {} |
1350 | 1353 |
|
1351 | 1354 |
/// The \c set function of the map |
1352 | 1355 |
void set(const Key& key, Value value) { |
1353 | 1356 |
if (value) { |
1354 | 1357 |
container.push_front(functor(key)); |
1355 | 1358 |
} |
1356 | 1359 |
} |
1357 | 1360 |
|
1358 | 1361 |
private: |
1359 | 1362 |
Container& container; |
1360 | 1363 |
Functor functor; |
1361 | 1364 |
}; |
1362 | 1365 |
|
1363 | 1366 |
/// \brief Writable bool map for storing each \c true assigned element in |
1364 | 1367 |
/// an insertable container. |
1365 | 1368 |
/// |
1366 | 1369 |
/// Writable bool map for storing each \c true assigned element in an |
1367 | 1370 |
/// insertable container. It will insert all the keys set to \c true into |
1368 | 1371 |
/// the container. |
1369 | 1372 |
/// |
1370 | 1373 |
/// For example, if you want to store the cut arcs of the strongly |
1371 | 1374 |
/// connected components in a set you can use the next code: |
1372 | 1375 |
/// |
1373 | 1376 |
///\code |
1374 | 1377 |
/// set<Arc> cut_arcs; |
1375 | 1378 |
/// InserterBoolMap<set<Arc> > inserter_map(cut_arcs); |
1376 | 1379 |
/// stronglyConnectedCutArcs(digraph, cost, inserter_map); |
1377 | 1380 |
///\endcode |
1378 | 1381 |
/// |
1379 | 1382 |
///\sa BackInserterBoolMap |
1380 | 1383 |
///\sa FrontInserterBoolMap |
1381 | 1384 |
template <typename Container, |
1382 | 1385 |
typename Functor = |
1383 | 1386 |
_maps_bits::Identity<typename Container::value_type> > |
1384 | 1387 |
class InserterBoolMap { |
1385 | 1388 |
public: |
1386 | 1389 |
typedef typename Container::value_type Key; |
1387 | 1390 |
typedef bool Value; |
1388 | 1391 |
|
1389 | 1392 |
/// Constructor with specified iterator |
1390 | 1393 |
|
1391 | 1394 |
/// Constructor with specified iterator. |
1392 | 1395 |
/// \param _container The container for storing the elements. |
1393 | 1396 |
/// \param _it The elements will be inserted before this iterator. |
1394 | 1397 |
/// \param _functor The functor that is used when an element is stored. |
1395 | 1398 |
InserterBoolMap(Container& _container, typename Container::iterator _it, |
1396 | 1399 |
const Functor& _functor = Functor()) |
1397 | 1400 |
: container(_container), it(_it), functor(_functor) {} |
1398 | 1401 |
|
1399 | 1402 |
/// Constructor |
1400 | 1403 |
|
1401 | 1404 |
/// Constructor without specified iterator. |
1402 | 1405 |
/// The elements will be inserted before <tt>_container.end()</tt>. |
1403 | 1406 |
/// \param _container The container for storing the elements. |
1404 | 1407 |
/// \param _functor The functor that is used when an element is stored. |
1405 | 1408 |
InserterBoolMap(Container& _container, const Functor& _functor = Functor()) |
1406 | 1409 |
: container(_container), it(_container.end()), functor(_functor) {} |
1407 | 1410 |
|
1408 | 1411 |
/// The \c set function of the map |
1409 | 1412 |
void set(const Key& key, Value value) { |
1410 | 1413 |
if (value) { |
1411 | 1414 |
it = container.insert(it, functor(key)); |
1412 | 1415 |
++it; |
1413 | 1416 |
} |
1414 | 1417 |
} |
1415 | 1418 |
|
1416 | 1419 |
private: |
1417 | 1420 |
Container& container; |
1418 | 1421 |
typename Container::iterator it; |
1419 | 1422 |
Functor functor; |
1420 | 1423 |
}; |
1421 | 1424 |
|
1422 | 1425 |
/// \brief Writable bool map for filling each \c true assigned element with a |
1423 | 1426 |
/// given value. |
1424 | 1427 |
/// |
1425 | 1428 |
/// Writable bool map for filling each \c true assigned element with a |
1426 | 1429 |
/// given value. The value can set the container. |
1427 | 1430 |
/// |
1428 | 1431 |
/// The following code finds the connected components of a graph |
1429 | 1432 |
/// and stores it in the \c comp map: |
1430 | 1433 |
///\code |
1431 | 1434 |
/// typedef Graph::NodeMap<int> ComponentMap; |
1432 | 1435 |
/// ComponentMap comp(graph); |
1433 | 1436 |
/// typedef FillBoolMap<Graph::NodeMap<int> > ComponentFillerMap; |
1434 | 1437 |
/// ComponentFillerMap filler(comp, 0); |
1435 | 1438 |
/// |
1436 | 1439 |
/// Dfs<Graph>::DefProcessedMap<ComponentFillerMap>::Create dfs(graph); |
1437 | 1440 |
/// dfs.processedMap(filler); |
1438 | 1441 |
/// dfs.init(); |
1439 | 1442 |
/// for (NodeIt it(graph); it != INVALID; ++it) { |
0 comments (0 inline)