gravatar
alpar (Alpar Juttner)
alpar@cs.elte.hu
Merge
0 1 0
merge default
1 file changed with 4 insertions and 13 deletions:
↑ Collapse diff ↑
Ignore white space 192 line context
... ...
@@ -22,495 +22,486 @@
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
    struct rebind {
119
      typedef ConstMap<K, T1> other;
120
    };
121

	
122
    template<typename T1>
123 118
    ConstMap(const ConstMap<K, T1> &, const T &_v) : v(_v) {}
124 119
  };
125 120

	
126 121
  ///Returns a \c ConstMap class
127 122

	
128 123
  ///This function just returns a \c ConstMap class.
129 124
  ///\relates ConstMap
130 125
  template<typename K, typename V> 
131 126
  inline ConstMap<K, V> constMap(const V &v) {
132 127
    return ConstMap<K, V>(v);
133 128
  }
134 129

	
135 130

	
136 131
  template<typename T, T v>
137 132
  struct Const { };
138 133

	
139 134
  /// Constant map with inlined constant value.
140 135

	
141 136
  /// This is a readable map which assigns a specified value to each key.
142 137
  /// In other aspects it is equivalent to the \c NullMap.
143 138
  template<typename K, typename V, V v>
144 139
  class ConstMap<K, Const<V, v> > : public MapBase<K, V> {
145 140
  public:
146 141
    typedef MapBase<K, V> Parent;
147 142
    typedef typename Parent::Key Key;
148 143
    typedef typename Parent::Value Value;
149 144

	
150 145
    ConstMap() { }
151 146
    ///\e
152 147
    V operator[](const K&) const { return v; }
153 148
    ///\e
154 149
    void set(const K&, const V&) { }
155 150
  };
156 151

	
157 152
  ///Returns a \c ConstMap class
158 153

	
159 154
  ///This function just returns a \c ConstMap class with inlined value.
160 155
  ///\relates ConstMap
161 156
  template<typename K, typename V, V v> 
162 157
  inline ConstMap<K, Const<V, v> > constMap() {
163 158
    return ConstMap<K, Const<V, v> >();
164 159
  }
165 160

	
166 161
  ///Map based on std::map
167 162

	
168 163
  ///This is essentially a wrapper for \c std::map with addition that
169 164
  ///you can specify a default value different from \c Value().
170 165
  template <typename K, typename T, typename Compare = std::less<K> >
171 166
  class StdMap {
172 167
    template <typename K1, typename T1, typename C1>
173 168
    friend class StdMap;
174 169
  public:
175 170

	
176 171
    typedef True ReferenceMapTag;
177 172
    ///Key type
178 173
    typedef K Key;
179 174
    ///Value type
180 175
    typedef T Value;
181 176
    ///Reference Type
182 177
    typedef T& Reference;
183 178
    ///Const reference type
184 179
    typedef const T& ConstReference;
185 180

	
186 181
  private:
187 182
    
188 183
    typedef std::map<K, T, Compare> Map;
189 184
    Value _value;
190 185
    Map _map;
191 186

	
192 187
  public:
193 188

	
194 189
    /// Constructor with specified default value
195 190
    StdMap(const T& value = T()) : _value(value) {}
196 191
    /// \brief Constructs the map from an appropriate std::map, and explicitly
197 192
    /// specifies a default value.
198 193
    template <typename T1, typename Comp1>
199 194
    StdMap(const std::map<Key, T1, Comp1> &map, const T& value = T()) 
200 195
      : _map(map.begin(), map.end()), _value(value) {}
201 196
    
202 197
    /// \brief Constructs a map from an other StdMap.
203 198
    template<typename T1, typename Comp1>
204 199
    StdMap(const StdMap<Key, T1, Comp1> &c) 
205 200
      : _map(c._map.begin(), c._map.end()), _value(c._value) {}
206 201

	
207 202
  private:
208 203

	
209 204
    StdMap& operator=(const StdMap&);
210 205

	
211 206
  public:
212 207

	
213 208
    ///\e
214 209
    Reference operator[](const Key &k) {
215 210
      typename Map::iterator it = _map.lower_bound(k);
216 211
      if (it != _map.end() && !_map.key_comp()(k, it->first))
217 212
	return it->second;
218 213
      else
219 214
	return _map.insert(it, std::make_pair(k, _value))->second;
220 215
    }
221 216

	
222 217
    /// \e 
223 218
    ConstReference operator[](const Key &k) const {
224 219
      typename Map::const_iterator it = _map.find(k);
225 220
      if (it != _map.end())
226 221
	return it->second;
227 222
      else
228 223
	return _value;
229 224
    }
230 225

	
231 226
    /// \e 
232 227
    void set(const Key &k, const T &t) {
233 228
      typename Map::iterator it = _map.lower_bound(k);
234 229
      if (it != _map.end() && !_map.key_comp()(k, it->first))
235 230
	it->second = t;
236 231
      else
237 232
	_map.insert(it, std::make_pair(k, t));
238 233
    }
239 234

	
240 235
    /// \e
241 236
    void setAll(const T &t) {
242 237
      _value = t;
243 238
      _map.clear();
244 239
    }    
245 240

	
246
    template <typename T1, typename C1 = std::less<T1> >
247
    struct rebind {
248
      typedef StdMap<Key, T1, C1> other;
249
    };
250 241
  };
251 242

	
252 243
  /// \brief Map for storing values for keys from the range <tt>[0..size-1]</tt>
253 244
  ///
254 245
  /// The current map has the <tt>[0..size-1]</tt> keyset and the values
255 246
  /// are stored in a \c std::vector<T>  container. It can be used with
256 247
  /// some data structures, for example \c UnionFind, \c BinHeap, when 
257 248
  /// the used items are small integer numbers. 
258 249
  ///
259 250
  /// \todo Revise its name
260 251
  template <typename T>
261 252
  class IntegerMap {
262 253

	
263 254
    template <typename T1>
264 255
    friend class IntegerMap;
265 256

	
266 257
  public:
267 258

	
268 259
    typedef True ReferenceMapTag;
269 260
    ///\e
270 261
    typedef int Key;
271 262
    ///\e
272 263
    typedef T Value;
273 264
    ///\e
274 265
    typedef T& Reference;
275 266
    ///\e
276 267
    typedef const T& ConstReference;
277 268

	
278 269
  private:
279 270
    
280 271
    typedef std::vector<T> Vector;
281 272
    Vector _vector;
282 273

	
283 274
  public:
284 275

	
285 276
    /// Constructor with specified default value
286 277
    IntegerMap(int size = 0, const T& value = T()) : _vector(size, value) {}
287 278

	
288 279
    /// \brief Constructs the map from an appropriate std::vector.
289 280
    template <typename T1>
290 281
    IntegerMap(const std::vector<T1>& vector) 
291 282
      : _vector(vector.begin(), vector.end()) {}
292 283
    
293 284
    /// \brief Constructs a map from an other IntegerMap.
294 285
    template <typename T1>
295 286
    IntegerMap(const IntegerMap<T1> &c) 
296 287
      : _vector(c._vector.begin(), c._vector.end()) {}
297 288

	
298 289
    /// \brief Resize the container
299 290
    void resize(int size, const T& value = T()) {
300 291
      _vector.resize(size, value);
301 292
    }
302 293

	
303 294
  private:
304 295

	
305 296
    IntegerMap& operator=(const IntegerMap&);
306 297

	
307 298
  public:
308 299

	
309 300
    ///\e
310 301
    Reference operator[](Key k) {
311 302
      return _vector[k];
312 303
    }
313 304

	
314 305
    /// \e 
315 306
    ConstReference operator[](Key k) const {
316 307
      return _vector[k];
317 308
    }
318 309

	
319 310
    /// \e 
320 311
    void set(const Key &k, const T& t) {
321 312
      _vector[k] = t;
322 313
    }
323 314

	
324 315
  };
325 316

	
326 317
  /// @}
327 318

	
328 319
  /// \addtogroup map_adaptors
329 320
  /// @{
330 321

	
331 322
  /// \brief Identity map.
332 323
  ///
333 324
  /// This map gives back the given key as value without any
334 325
  /// modification. 
335 326
  template <typename T>
336 327
  class IdentityMap : public MapBase<T, T> {
337 328
  public:
338 329
    typedef MapBase<T, T> Parent;
339 330
    typedef typename Parent::Key Key;
340 331
    typedef typename Parent::Value Value;
341 332

	
342 333
    /// \e
343 334
    const T& operator[](const T& t) const {
344 335
      return t;
345 336
    }
346 337
  };
347 338

	
348 339
  ///Returns an \c IdentityMap class
349 340

	
350 341
  ///This function just returns an \c IdentityMap class.
351 342
  ///\relates IdentityMap
352 343
  template<typename T>
353 344
  inline IdentityMap<T> identityMap() {
354 345
    return IdentityMap<T>();
355 346
  }
356 347
  
357 348

	
358 349
  ///\brief Convert the \c Value of a map to another type using
359 350
  ///the default conversion.
360 351
  ///
361 352
  ///This \c concepts::ReadMap "read only map"
362 353
  ///converts the \c Value of a map to type \c T.
363 354
  ///Its \c Key is inherited from \c M.
364 355
  template <typename M, typename T> 
365 356
  class ConvertMap : public MapBase<typename M::Key, T> {
366 357
    const M& m;
367 358
  public:
368 359
    typedef MapBase<typename M::Key, T> Parent;
369 360
    typedef typename Parent::Key Key;
370 361
    typedef typename Parent::Value Value;
371 362

	
372 363
    ///Constructor
373 364

	
374 365
    ///Constructor.
375 366
    ///\param _m is the underlying map.
376 367
    ConvertMap(const M &_m) : m(_m) {};
377 368

	
378 369
    /// \brief The subscript operator.
379 370
    ///
380 371
    /// The subscript operator.
381 372
    Value operator[](const Key& k) const {return m[k];}
382 373
  };
383 374
  
384 375
  ///Returns a \c ConvertMap class
385 376

	
386 377
  ///This function just returns a \c ConvertMap class.
387 378
  ///\relates ConvertMap
388 379
  template<typename T, typename M>
389 380
  inline ConvertMap<M, T> convertMap(const M &m) {
390 381
    return ConvertMap<M, T>(m);
391 382
  }
392 383

	
393 384
  ///Simple wrapping of a map
394 385

	
395
  ///This \c concepts::ReadMap "read only map" returns the simple
386
  ///This \ref concepts::ReadMap "read only map" returns the simple
396 387
  ///wrapping of the given map. Sometimes the reference maps cannot be
397 388
  ///combined with simple read maps. This map adaptor wraps the given
398 389
  ///map to simple read map.
399 390
  ///
400 391
  ///\sa SimpleWriteMap
401 392
  ///
402 393
  /// \todo Revise the misleading name 
403 394
  template<typename M> 
404 395
  class SimpleMap : public MapBase<typename M::Key, typename M::Value> {
405 396
    const M& m;
406 397

	
407 398
  public:
408 399
    typedef MapBase<typename M::Key, typename M::Value> Parent;
409 400
    typedef typename Parent::Key Key;
410 401
    typedef typename Parent::Value Value;
411 402

	
412 403
    ///Constructor
413 404
    SimpleMap(const M &_m) : m(_m) {};
414 405
    ///\e
415 406
    Value operator[](Key k) const {return m[k];}
416 407
  };
417 408

	
418
  ///Simple writable wrapping of the map
409
  ///Simple writable wrapping of a map
419 410

	
420
  ///This \c concepts::WriteMap "write map" returns the simple
411
  ///This \ref concepts::WriteMap "write map" returns the simple
421 412
  ///wrapping of the given map. Sometimes the reference maps cannot be
422 413
  ///combined with simple read-write maps. This map adaptor wraps the
423 414
  ///given map to simple read-write map.
424 415
  ///
425 416
  ///\sa SimpleMap
426 417
  ///
427 418
  /// \todo Revise the misleading name
428 419
  template<typename M> 
429 420
  class SimpleWriteMap : public MapBase<typename M::Key, typename M::Value> {
430 421
    M& m;
431 422

	
432 423
  public:
433 424
    typedef MapBase<typename M::Key, typename M::Value> Parent;
434 425
    typedef typename Parent::Key Key;
435 426
    typedef typename Parent::Value Value;
436 427

	
437 428
    ///Constructor
438 429
    SimpleWriteMap(M &_m) : m(_m) {};
439 430
    ///\e
440 431
    Value operator[](Key k) const {return m[k];}
441 432
    ///\e
442 433
    void set(Key k, const Value& c) { m.set(k, c); }
443 434
  };
444 435

	
445 436
  ///Sum of two maps
446 437

	
447 438
  ///This \c concepts::ReadMap "read only map" returns the sum of the two
448 439
  ///given maps.
449 440
  ///Its \c Key and \c Value are inherited from \c M1.
450 441
  ///The \c Key and \c Value of M2 must be convertible to those of \c M1.
451 442
  template<typename M1, typename M2> 
452 443
  class AddMap : public MapBase<typename M1::Key, typename M1::Value> {
453 444
    const M1& m1;
454 445
    const M2& m2;
455 446

	
456 447
  public:
457 448
    typedef MapBase<typename M1::Key, typename M1::Value> Parent;
458 449
    typedef typename Parent::Key Key;
459 450
    typedef typename Parent::Value Value;
460 451

	
461 452
    ///Constructor
462 453
    AddMap(const M1 &_m1,const M2 &_m2) : m1(_m1), m2(_m2) {};
463 454
    ///\e
464 455
    Value operator[](Key k) const {return m1[k]+m2[k];}
465 456
  };
466 457
  
467 458
  ///Returns an \c AddMap class
468 459

	
469 460
  ///This function just returns an \c AddMap class.
470 461
  ///\todo How to call these type of functions?
471 462
  ///
472 463
  ///\relates AddMap
473 464
  template<typename M1, typename M2> 
474 465
  inline AddMap<M1, M2> addMap(const M1 &m1,const M2 &m2) {
475 466
    return AddMap<M1, M2>(m1,m2);
476 467
  }
477 468

	
478 469
  ///Shift a map with a constant.
479 470

	
480 471
  ///This \c concepts::ReadMap "read only map" returns the sum of the
481 472
  ///given map and a constant value.
482 473
  ///Its \c Key and \c Value are inherited from \c M.
483 474
  ///
484 475
  ///Actually,
485 476
  ///\code
486 477
  ///  ShiftMap<X> sh(x,v);
487 478
  ///\endcode
488 479
  ///is equivalent to
489 480
  ///\code
490 481
  ///  ConstMap<X::Key, X::Value> c_tmp(v);
491 482
  ///  AddMap<X, ConstMap<X::Key, X::Value> > sh(x,v);
492 483
  ///\endcode
493 484
  ///
494 485
  ///\sa ShiftWriteMap
495 486
  template<typename M, typename C = typename M::Value> 
496 487
  class ShiftMap : public MapBase<typename M::Key, typename M::Value> {
497 488
    const M& m;
498 489
    C v;
499 490
  public:
500 491
    typedef MapBase<typename M::Key, typename M::Value> Parent;
501 492
    typedef typename Parent::Key Key;
502 493
    typedef typename Parent::Value Value;
503 494

	
504 495
    ///Constructor
505 496

	
506 497
    ///Constructor.
507 498
    ///\param _m is the undelying map.
508 499
    ///\param _v is the shift value.
509 500
    ShiftMap(const M &_m, const C &_v ) : m(_m), v(_v) {};
510 501
    ///\e
511 502
    Value operator[](Key k) const {return m[k] + v;}
512 503
  };
513 504

	
514 505
  ///Shift a map with a constant (ReadWrite version).
515 506

	
516 507
  ///This \c concepts::ReadWriteMap "read-write map" returns the sum of the
... ...
@@ -1460,112 +1451,112 @@
1460 1451
    /// Constructor
1461 1452
    FillBoolMap(Map& _map) 
1462 1453
      : map(_map), fill() {}
1463 1454

	
1464 1455
    /// Gives back the current fill value
1465 1456
    const typename Map::Value& fillValue() const {
1466 1457
      return fill;
1467 1458
    } 
1468 1459

	
1469 1460
    /// Gives back the current fill value
1470 1461
    typename Map::Value& fillValue() {
1471 1462
      return fill;
1472 1463
    } 
1473 1464

	
1474 1465
    /// Sets the current fill value
1475 1466
    void fillValue(const typename Map::Value& _fill) {
1476 1467
      fill = _fill;
1477 1468
    } 
1478 1469

	
1479 1470
    /// The \c set function of the map
1480 1471
    void set(const Key& key, Value value) {
1481 1472
      if (value) {
1482 1473
	map.set(key, fill);
1483 1474
      }
1484 1475
    }
1485 1476
    
1486 1477
  private:
1487 1478
    Map& map;
1488 1479
    typename Map::Value fill;
1489 1480
  };
1490 1481

	
1491 1482

	
1492 1483
  /// \brief Writable bool map for storing the sequence number of 
1493 1484
  /// \c true assignments.  
1494 1485
  /// 
1495 1486
  /// Writable bool map that stores for each \c true assigned elements  
1496 1487
  /// the sequence number of this setting.
1497 1488
  /// It makes it easy to calculate the leaving
1498 1489
  /// order of the nodes in the \c Dfs algorithm.
1499 1490
  ///
1500 1491
  ///\code
1501 1492
  /// typedef Digraph::NodeMap<int> OrderMap;
1502 1493
  /// OrderMap order(digraph);
1503 1494
  /// typedef SettingOrderBoolMap<OrderMap> OrderSetterMap;
1504 1495
  /// OrderSetterMap setter(order);
1505 1496
  /// Dfs<Digraph>::DefProcessedMap<OrderSetterMap>::Create dfs(digraph);
1506 1497
  /// dfs.processedMap(setter);
1507 1498
  /// dfs.init();
1508 1499
  /// for (NodeIt it(digraph); it != INVALID; ++it) {
1509 1500
  ///   if (!dfs.reached(it)) {
1510 1501
  ///     dfs.addSource(it);
1511 1502
  ///     dfs.start();
1512 1503
  ///   }
1513 1504
  /// }
1514 1505
  ///\endcode
1515 1506
  ///
1516 1507
  /// The storing of the discovering order is more difficult because the
1517 1508
  /// ReachedMap should be readable in the dfs algorithm but the setting
1518 1509
  /// order map is not readable. Thus we must use the fork map:
1519 1510
  ///
1520 1511
  ///\code
1521 1512
  /// typedef Digraph::NodeMap<int> OrderMap;
1522 1513
  /// OrderMap order(digraph);
1523 1514
  /// typedef SettingOrderBoolMap<OrderMap> OrderSetterMap;
1524 1515
  /// OrderSetterMap setter(order);
1525 1516
  /// typedef Digraph::NodeMap<bool> StoreMap;
1526 1517
  /// StoreMap store(digraph);
1527 1518
  ///
1528 1519
  /// typedef ForkWriteMap<StoreMap, OrderSetterMap> ReachedMap;
1529 1520
  /// ReachedMap reached(store, setter);
1530 1521
  ///
1531 1522
  /// Dfs<Digraph>::DefReachedMap<ReachedMap>::Create dfs(digraph);
1532 1523
  /// dfs.reachedMap(reached);
1533 1524
  /// dfs.init();
1534 1525
  /// for (NodeIt it(digraph); it != INVALID; ++it) {
1535 1526
  ///   if (!dfs.reached(it)) {
1536 1527
  ///     dfs.addSource(it);
1537 1528
  ///     dfs.start();
1538 1529
  ///   }
1539 1530
  /// }
1540 1531
  ///\endcode
1541 1532
  template <typename Map>
1542 1533
  class SettingOrderBoolMap {
1543 1534
  public:
1544 1535
    typedef typename Map::Key Key;
1545 1536
    typedef bool Value;
1546 1537

	
1547 1538
    /// Constructor
1548 1539
    SettingOrderBoolMap(Map& _map) 
1549 1540
      : map(_map), counter(0) {}
1550 1541

	
1551 1542
    /// Number of set operations.
1552 1543
    int num() const {
1553 1544
      return counter;
1554 1545
    }
1555 1546

	
1556
    /// Setter function of the map
1547
    /// The \c set function of the map
1557 1548
    void set(const Key& key, Value value) {
1558 1549
      if (value) {
1559 1550
	map.set(key, counter++);
1560 1551
      }
1561 1552
    }
1562 1553
    
1563 1554
  private:
1564 1555
    Map& map;
1565 1556
    int counter;
1566 1557
  };
1567 1558

	
1568 1559
  /// @}
1569 1560
}
1570 1561

	
1571 1562
#endif // LEMON_MAPS_H
0 comments (0 inline)