gravatar
kpeter (Peter Kovacs)
kpeter@inf.elte.hu
Small improvements in maps.h - Add a new version of constMap() function. - Fix in FunctorToMap class.
0 2 0
default
2 files changed with 18 insertions and 6 deletions:
↑ Collapse diff ↑
Ignore white space 6 line context
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-2008
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. It provides the necessary type definitions
43 43
  /// required by the map %concepts.
44 44
  template<typename K, typename V>
45 45
  class MapBase {
46 46
  public:
47 47
    /// \biref The key type of the map.
48 48
    typedef K Key;
49 49
    /// \brief The value type of the map.
50 50
    /// (The type of objects associated with the keys).
51 51
    typedef V Value;
52 52
  };
53 53

	
54 54

	
55 55
  /// Null map. (a.k.a. DoNothingMap)
56 56

	
57 57
  /// This map can be used if you have to provide a map only for
58 58
  /// its type definitions, or if you have to provide a writable map,
59 59
  /// but data written to it is not required (i.e. it will be sent to
60 60
  /// <tt>/dev/null</tt>).
61 61
  /// It conforms the \ref concepts::ReadWriteMap "ReadWriteMap" concept.
62 62
  ///
63 63
  /// \sa ConstMap
64 64
  template<typename K, typename V>
65 65
  class NullMap : public MapBase<K, V> {
66 66
  public:
67 67
    typedef MapBase<K, V> Parent;
68 68
    typedef typename Parent::Key Key;
69 69
    typedef typename Parent::Value Value;
70 70

	
71 71
    /// Gives back a default constructed element.
72 72
    Value operator[](const Key&) const { return Value(); }
73 73
    /// Absorbs the value.
74 74
    void set(const Key&, const Value&) {}
75 75
  };
76 76

	
77 77
  /// Returns a \ref NullMap class
78 78

	
79 79
  /// This function just returns a \ref NullMap class.
80 80
  /// \relates NullMap
81 81
  template <typename K, typename V>
82 82
  NullMap<K, V> nullMap() {
83 83
    return NullMap<K, V>();
84 84
  }
85 85

	
86 86

	
87 87
  /// Constant map.
88 88

	
89 89
  /// This \ref concepts::ReadMap "readable map" assigns a specified
90 90
  /// value to each key.
91 91
  ///
92 92
  /// In other aspects it is equivalent to \ref NullMap.
93 93
  /// So it conforms the \ref concepts::ReadWriteMap "ReadWriteMap"
94 94
  /// concept, but it absorbs the data written to it.
95 95
  ///
96 96
  /// The simplest way of using this map is through the constMap()
97 97
  /// function.
98 98
  ///
99 99
  /// \sa NullMap
100 100
  /// \sa IdentityMap
101 101
  template<typename K, typename V>
102 102
  class ConstMap : public MapBase<K, V> {
103 103
  private:
104 104
    V _value;
105 105
  public:
106 106
    typedef MapBase<K, V> Parent;
107 107
    typedef typename Parent::Key Key;
108 108
    typedef typename Parent::Value Value;
109 109

	
110 110
    /// Default constructor
111 111

	
112 112
    /// Default constructor.
113 113
    /// The value of the map will be default constructed.
114 114
    ConstMap() {}
115 115

	
116 116
    /// Constructor with specified initial value
117 117

	
118 118
    /// Constructor with specified initial value.
119
    /// \param v is the initial value of the map.
119
    /// \param v The initial value of the map.
120 120
    ConstMap(const Value &v) : _value(v) {}
121 121

	
122 122
    /// Gives back the specified value.
123 123
    Value operator[](const Key&) const { return _value; }
124 124

	
125 125
    /// Absorbs the value.
126 126
    void set(const Key&, const Value&) {}
127 127

	
128 128
    /// Sets the value that is assigned to each key.
129 129
    void setAll(const Value &v) {
130 130
      _value = v;
131 131
    }
132 132

	
133 133
    template<typename V1>
134 134
    ConstMap(const ConstMap<K, V1> &, const Value &v) : _value(v) {}
135 135
  };
136 136

	
137 137
  /// Returns a \ref ConstMap class
138 138

	
139 139
  /// This function just returns a \ref ConstMap class.
140 140
  /// \relates ConstMap
141 141
  template<typename K, typename V>
142 142
  inline ConstMap<K, V> constMap(const V &v) {
143 143
    return ConstMap<K, V>(v);
144 144
  }
145 145

	
146
  template<typename K, typename V>
147
  inline ConstMap<K, V> constMap() {
148
    return ConstMap<K, V>();
149
  }
150

	
146 151

	
147 152
  template<typename T, T v>
148 153
  struct Const {};
149 154

	
150 155
  /// Constant map with inlined constant value.
151 156

	
152 157
  /// This \ref concepts::ReadMap "readable map" assigns a specified
153 158
  /// value to each key.
154 159
  ///
155 160
  /// In other aspects it is equivalent to \ref NullMap.
156 161
  /// So it conforms the \ref concepts::ReadWriteMap "ReadWriteMap"
157 162
  /// concept, but it absorbs the data written to it.
158 163
  ///
159 164
  /// The simplest way of using this map is through the constMap()
160 165
  /// function.
161 166
  ///
162 167
  /// \sa NullMap
163 168
  /// \sa IdentityMap
164 169
  template<typename K, typename V, V v>
165 170
  class ConstMap<K, Const<V, v> > : public MapBase<K, V> {
166 171
  public:
167 172
    typedef MapBase<K, V> Parent;
168 173
    typedef typename Parent::Key Key;
169 174
    typedef typename Parent::Value Value;
170 175

	
171 176
    /// Constructor.
172 177
    ConstMap() {}
173 178

	
174 179
    /// Gives back the specified value.
175 180
    Value operator[](const Key&) const { return v; }
176 181

	
177 182
    /// Absorbs the value.
178 183
    void set(const Key&, const Value&) {}
179 184
  };
180 185

	
181 186
  /// Returns a \ref ConstMap class with inlined constant value
182 187

	
183 188
  /// This function just returns a \ref ConstMap class with inlined
184 189
  /// constant value.
185 190
  /// \relates ConstMap
186 191
  template<typename K, typename V, V v>
187 192
  inline ConstMap<K, Const<V, v> > constMap() {
188 193
    return ConstMap<K, Const<V, v> >();
189 194
  }
190 195

	
191 196

	
192 197
  /// Identity map.
193 198

	
194 199
  /// This \ref concepts::ReadMap "read-only map" gives back the given
195 200
  /// key as value without any modification.
196 201
  ///
197 202
  /// \sa ConstMap
198 203
  template <typename T>
199 204
  class IdentityMap : public MapBase<T, T> {
200 205
  public:
201 206
    typedef MapBase<T, T> Parent;
202 207
    typedef typename Parent::Key Key;
203 208
    typedef typename Parent::Value Value;
204 209

	
205 210
    /// Gives back the given value without any modification.
206 211
    Value operator[](const Key &k) const {
207 212
      return k;
208 213
    }
209 214
  };
210 215

	
211 216
  /// Returns an \ref IdentityMap class
212 217

	
213 218
  /// This function just returns an \ref IdentityMap class.
214 219
  /// \relates IdentityMap
215 220
  template<typename T>
216 221
  inline IdentityMap<T> identityMap() {
217 222
    return IdentityMap<T>();
218 223
  }
219 224

	
220 225

	
221 226
  /// \brief Map for storing values for integer keys from the range
222 227
  /// <tt>[0..size-1]</tt>.
223 228
  ///
224 229
  /// This map is essentially a wrapper for \c std::vector. It assigns
225 230
  /// values to integer keys from the range <tt>[0..size-1]</tt>.
226 231
  /// It can be used with some data structures, for example
227 232
  /// \ref UnionFind, \ref BinHeap, when the used items are small
228 233
  /// integers. This map conforms the \ref concepts::ReferenceMap
229 234
  /// "ReferenceMap" concept.
230 235
  ///
231 236
  /// The simplest way of using this map is through the rangeMap()
232 237
  /// function.
233 238
  template <typename V>
234 239
  class RangeMap : public MapBase<int, V> {
235 240
    template <typename V1>
236 241
    friend class RangeMap;
237 242
  private:
238 243

	
239 244
    typedef std::vector<V> Vector;
240 245
    Vector _vector;
241 246

	
242 247
  public:
243 248

	
244 249
    typedef MapBase<int, V> Parent;
245 250
    /// Key type
246 251
    typedef typename Parent::Key Key;
247 252
    /// Value type
248 253
    typedef typename Parent::Value Value;
249 254
    /// Reference type
250 255
    typedef typename Vector::reference Reference;
251 256
    /// Const reference type
252 257
    typedef typename Vector::const_reference ConstReference;
253 258

	
254 259
    typedef True ReferenceMapTag;
255 260

	
256 261
  public:
257 262

	
258 263
    /// Constructor with specified default value.
259 264
    RangeMap(int size = 0, const Value &value = Value())
260 265
      : _vector(size, value) {}
261 266

	
262 267
    /// Constructs the map from an appropriate \c std::vector.
263 268
    template <typename V1>
264 269
    RangeMap(const std::vector<V1>& vector)
265 270
      : _vector(vector.begin(), vector.end()) {}
266 271

	
267 272
    /// Constructs the map from another \ref RangeMap.
268 273
    template <typename V1>
269 274
    RangeMap(const RangeMap<V1> &c)
270 275
      : _vector(c._vector.begin(), c._vector.end()) {}
271 276

	
272 277
    /// Returns the size of the map.
273 278
    int size() {
274 279
      return _vector.size();
275 280
    }
276 281

	
277 282
    /// Resizes the map.
278 283

	
279 284
    /// Resizes the underlying \c std::vector container, so changes the
280 285
    /// keyset of the map.
281 286
    /// \param size The new size of the map. The new keyset will be the
282 287
    /// range <tt>[0..size-1]</tt>.
283 288
    /// \param value The default value to assign to the new keys.
284 289
    void resize(int size, const Value &value = Value()) {
285 290
      _vector.resize(size, value);
286 291
    }
287 292

	
288 293
  private:
289 294

	
290 295
    RangeMap& operator=(const RangeMap&);
291 296

	
292 297
  public:
293 298

	
294 299
    ///\e
295 300
    Reference operator[](const Key &k) {
296 301
      return _vector[k];
297 302
    }
298 303

	
299 304
    ///\e
300 305
    ConstReference operator[](const Key &k) const {
301 306
      return _vector[k];
302 307
    }
303 308

	
304 309
    ///\e
305 310
    void set(const Key &k, const Value &v) {
306 311
      _vector[k] = v;
307 312
    }
308 313
  };
309 314

	
310 315
  /// Returns a \ref RangeMap class
311 316

	
312 317
  /// This function just returns a \ref RangeMap class.
313 318
  /// \relates RangeMap
314 319
  template<typename V>
315 320
  inline RangeMap<V> rangeMap(int size = 0, const V &value = V()) {
316 321
    return RangeMap<V>(size, value);
317 322
  }
318 323

	
319 324
  /// \brief Returns a \ref RangeMap class created from an appropriate
320 325
  /// \c std::vector
321 326

	
322 327
  /// This function just returns a \ref RangeMap class created from an
323 328
  /// appropriate \c std::vector.
324 329
  /// \relates RangeMap
325 330
  template<typename V>
326 331
  inline RangeMap<V> rangeMap(const std::vector<V> &vector) {
327 332
    return RangeMap<V>(vector);
328 333
  }
329 334

	
330 335

	
331 336
  /// Map type based on \c std::map
332 337

	
333 338
  /// This map is essentially a wrapper for \c std::map with addition
334 339
  /// that you can specify a default value for the keys that are not
335 340
  /// stored actually. This value can be different from the default
336 341
  /// contructed value (i.e. \c %Value()).
337 342
  /// This type conforms the \ref concepts::ReferenceMap "ReferenceMap"
338 343
  /// concept.
339 344
  ///
340 345
  /// This map is useful if a default value should be assigned to most of
341 346
  /// the keys and different values should be assigned only to a few
342 347
  /// keys (i.e. the map is "sparse").
343 348
  /// The name of this type also refers to this important usage.
344 349
  ///
345 350
  /// Apart form that this map can be used in many other cases since it
346 351
  /// is based on \c std::map, which is a general associative container.
347 352
  /// However keep in mind that it is usually not as efficient as other
348 353
  /// maps.
349 354
  ///
350 355
  /// The simplest way of using this map is through the sparseMap()
351 356
  /// function.
352 357
  template <typename K, typename V, typename Compare = std::less<K> >
353 358
  class SparseMap : public MapBase<K, V> {
354 359
    template <typename K1, typename V1, typename C1>
355 360
    friend class SparseMap;
356 361
  public:
357 362

	
358 363
    typedef MapBase<K, V> Parent;
359 364
    /// Key type
360 365
    typedef typename Parent::Key Key;
361 366
    /// Value type
362 367
    typedef typename Parent::Value Value;
363 368
    /// Reference type
364 369
    typedef Value& Reference;
365 370
    /// Const reference type
366 371
    typedef const Value& ConstReference;
367 372

	
368 373
    typedef True ReferenceMapTag;
369 374

	
370 375
  private:
371 376

	
372 377
    typedef std::map<K, V, Compare> Map;
373 378
    Map _map;
374 379
    Value _value;
375 380

	
376 381
  public:
377 382

	
378 383
    /// \brief Constructor with specified default value.
379 384
    SparseMap(const Value &value = Value()) : _value(value) {}
380 385
    /// \brief Constructs the map from an appropriate \c std::map, and
381 386
    /// explicitly specifies a default value.
382 387
    template <typename V1, typename Comp1>
383 388
    SparseMap(const std::map<Key, V1, Comp1> &map,
384 389
              const Value &value = Value())
385 390
      : _map(map.begin(), map.end()), _value(value) {}
386 391

	
387 392
    /// \brief Constructs the map from another \ref SparseMap.
388 393
    template<typename V1, typename Comp1>
389 394
    SparseMap(const SparseMap<Key, V1, Comp1> &c)
390 395
      : _map(c._map.begin(), c._map.end()), _value(c._value) {}
391 396

	
392 397
  private:
393 398

	
394 399
    SparseMap& operator=(const SparseMap&);
395 400

	
396 401
  public:
397 402

	
398 403
    ///\e
399 404
    Reference operator[](const Key &k) {
400 405
      typename Map::iterator it = _map.lower_bound(k);
401 406
      if (it != _map.end() && !_map.key_comp()(k, it->first))
402 407
	return it->second;
403 408
      else
404 409
	return _map.insert(it, std::make_pair(k, _value))->second;
405 410
    }
406 411

	
407 412
    ///\e
408 413
    ConstReference operator[](const Key &k) const {
409 414
      typename Map::const_iterator it = _map.find(k);
410 415
      if (it != _map.end())
411 416
	return it->second;
412 417
      else
413 418
	return _value;
414 419
    }
415 420

	
416 421
    ///\e
417 422
    void set(const Key &k, const Value &v) {
418 423
      typename Map::iterator it = _map.lower_bound(k);
419 424
      if (it != _map.end() && !_map.key_comp()(k, it->first))
420 425
	it->second = v;
421 426
      else
422 427
	_map.insert(it, std::make_pair(k, v));
423 428
    }
424 429

	
425 430
    ///\e
426 431
    void setAll(const Value &v) {
427 432
      _value = v;
428 433
      _map.clear();
429 434
    }
430 435
  };
431 436

	
432 437
  /// Returns a \ref SparseMap class
433 438

	
434 439
  /// This function just returns a \ref SparseMap class with specified
435 440
  /// default value.
436 441
  /// \relates SparseMap
437 442
  template<typename K, typename V, typename Compare>
438 443
  inline SparseMap<K, V, Compare> sparseMap(const V& value = V()) {
439 444
    return SparseMap<K, V, Compare>(value);
440 445
  }
441 446

	
442 447
  template<typename K, typename V>
443 448
  inline SparseMap<K, V, std::less<K> > sparseMap(const V& value = V()) {
444 449
    return SparseMap<K, V, std::less<K> >(value);
445 450
  }
446 451

	
447 452
  /// \brief Returns a \ref SparseMap class created from an appropriate
448 453
  /// \c std::map
449 454

	
450 455
  /// This function just returns a \ref SparseMap class created from an
451 456
  /// appropriate \c std::map.
452 457
  /// \relates SparseMap
453 458
  template<typename K, typename V, typename Compare>
454 459
  inline SparseMap<K, V, Compare>
455 460
    sparseMap(const std::map<K, V, Compare> &map, const V& value = V())
456 461
  {
457 462
    return SparseMap<K, V, Compare>(map, value);
458 463
  }
459 464

	
460 465
  /// @}
461 466

	
462 467
  /// \addtogroup map_adaptors
463 468
  /// @{
464 469

	
465 470
  /// Composition of two maps
466 471

	
467 472
  /// This \ref concepts::ReadMap "read-only map" returns the
468 473
  /// composition of two given maps. That is to say, if \c m1 is of
469 474
  /// type \c M1 and \c m2 is of \c M2, then for
470 475
  /// \code
471 476
  ///   ComposeMap<M1, M2> cm(m1,m2);
472 477
  /// \endcode
473 478
  /// <tt>cm[x]</tt> will be equal to <tt>m1[m2[x]]</tt>.
474 479
  ///
475 480
  /// The \c Key type of the map is inherited from \c M2 and the
476 481
  /// \c Value type is from \c M1.
477 482
  /// \c M2::Value must be convertible to \c M1::Key.
478 483
  ///
479 484
  /// The simplest way of using this map is through the composeMap()
480 485
  /// function.
481 486
  ///
482 487
  /// \sa CombineMap
483 488
  ///
484 489
  /// \todo Check the requirements.
485 490
  template <typename M1, typename M2>
486 491
  class ComposeMap : public MapBase<typename M2::Key, typename M1::Value> {
487 492
    const M1 &_m1;
488 493
    const M2 &_m2;
489 494
  public:
490 495
    typedef MapBase<typename M2::Key, typename M1::Value> Parent;
491 496
    typedef typename Parent::Key Key;
492 497
    typedef typename Parent::Value Value;
493 498

	
494 499
    /// Constructor
495 500
    ComposeMap(const M1 &m1, const M2 &m2) : _m1(m1), _m2(m2) {}
496 501

	
497 502
    /// \e
498 503
    typename MapTraits<M1>::ConstReturnValue
499 504
    operator[](const Key &k) const { return _m1[_m2[k]]; }
500 505
  };
501 506

	
502 507
  /// Returns a \ref ComposeMap class
503 508

	
504 509
  /// This function just returns a \ref ComposeMap class.
505 510
  ///
506 511
  /// If \c m1 and \c m2 are maps and the \c Value type of \c m2 is
507 512
  /// convertible to the \c Key of \c m1, then <tt>composeMap(m1,m2)[x]</tt>
508 513
  /// will be equal to <tt>m1[m2[x]]</tt>.
509 514
  ///
510 515
  /// \relates ComposeMap
511 516
  template <typename M1, typename M2>
512 517
  inline ComposeMap<M1, M2> composeMap(const M1 &m1, const M2 &m2) {
513 518
    return ComposeMap<M1, M2>(m1, m2);
514 519
  }
515 520

	
516 521

	
517 522
  /// Combination of two maps using an STL (binary) functor.
518 523

	
519 524
  /// This \ref concepts::ReadMap "read-only map" takes two maps and a
520 525
  /// binary functor and returns the combination of the two given maps
521 526
  /// using the functor.
522 527
  /// That is to say, if \c m1 is of type \c M1 and \c m2 is of \c M2
523 528
  /// and \c f is of \c F, then for
524 529
  /// \code
525 530
  ///   CombineMap<M1,M2,F,V> cm(m1,m2,f);
526 531
  /// \endcode
527 532
  /// <tt>cm[x]</tt> will be equal to <tt>f(m1[x],m2[x])</tt>.
528 533
  ///
529 534
  /// The \c Key type of the map is inherited from \c M1 (\c M1::Key
530 535
  /// must be convertible to \c M2::Key) and the \c Value type is \c V.
531 536
  /// \c M2::Value and \c M1::Value must be convertible to the
532 537
  /// corresponding input parameter of \c F and the return type of \c F
533 538
  /// must be convertible to \c V.
534 539
  ///
535 540
  /// The simplest way of using this map is through the combineMap()
536 541
  /// function.
537 542
  ///
538 543
  /// \sa ComposeMap
539 544
  ///
540 545
  /// \todo Check the requirements.
541 546
  template<typename M1, typename M2, typename F,
542 547
	   typename V = typename F::result_type>
543 548
  class CombineMap : public MapBase<typename M1::Key, V> {
544 549
    const M1 &_m1;
545 550
    const M2 &_m2;
546 551
    F _f;
547 552
  public:
548 553
    typedef MapBase<typename M1::Key, V> Parent;
549 554
    typedef typename Parent::Key Key;
550 555
    typedef typename Parent::Value Value;
551 556

	
552 557
    /// Constructor
553 558
    CombineMap(const M1 &m1, const M2 &m2, const F &f = F())
554 559
      : _m1(m1), _m2(m2), _f(f) {}
555 560
    /// \e
556 561
    Value operator[](const Key &k) const { return _f(_m1[k],_m2[k]); }
557 562
  };
558 563

	
559 564
  /// Returns a \ref CombineMap class
560 565

	
561 566
  /// This function just returns a \ref CombineMap class.
562 567
  ///
563 568
  /// For example, if \c m1 and \c m2 are both maps with \c double
564 569
  /// values, then
565 570
  /// \code
566 571
  ///   combineMap(m1,m2,std::plus<double>())
567 572
  /// \endcode
568 573
  /// is equivalent to
569 574
  /// \code
570 575
  ///   addMap(m1,m2)
571 576
  /// \endcode
572 577
  ///
573 578
  /// This function is specialized for adaptable binary function
574 579
  /// classes and C++ functions.
575 580
  ///
576 581
  /// \relates CombineMap
577 582
  template<typename M1, typename M2, typename F, typename V>
578 583
  inline CombineMap<M1, M2, F, V>
579 584
  combineMap(const M1 &m1, const M2 &m2, const F &f) {
580 585
    return CombineMap<M1, M2, F, V>(m1,m2,f);
581 586
  }
582 587

	
583 588
  template<typename M1, typename M2, typename F>
584 589
  inline CombineMap<M1, M2, F, typename F::result_type>
585 590
  combineMap(const M1 &m1, const M2 &m2, const F &f) {
586 591
    return combineMap<M1, M2, F, typename F::result_type>(m1,m2,f);
587 592
  }
588 593

	
589 594
  template<typename M1, typename M2, typename K1, typename K2, typename V>
590 595
  inline CombineMap<M1, M2, V (*)(K1, K2), V>
591 596
  combineMap(const M1 &m1, const M2 &m2, V (*f)(K1, K2)) {
592 597
    return combineMap<M1, M2, V (*)(K1, K2), V>(m1,m2,f);
593 598
  }
594 599

	
595 600

	
596 601
  /// Converts an STL style (unary) functor to a map
597 602

	
598 603
  /// This \ref concepts::ReadMap "read-only map" returns the value
599 604
  /// of a given functor. Actually, it just wraps the functor and
600 605
  /// provides the \c Key and \c Value typedefs.
601 606
  ///
602 607
  /// Template parameters \c K and \c V will become its \c Key and
603 608
  /// \c Value. In most cases they have to be given explicitly because
604 609
  /// a functor typically does not provide \c argument_type and
605 610
  /// \c result_type typedefs.
606 611
  /// Parameter \c F is the type of the used functor.
607 612
  ///
608 613
  /// The simplest way of using this map is through the functorToMap()
609 614
  /// function.
610 615
  ///
611 616
  /// \sa MapToFunctor
612 617
  template<typename F,
613 618
	   typename K = typename F::argument_type,
614 619
	   typename V = typename F::result_type>
615 620
  class FunctorToMap : public MapBase<K, V> {
616
    const F &_f;
621
    F _f;
617 622
  public:
618 623
    typedef MapBase<K, V> Parent;
619 624
    typedef typename Parent::Key Key;
620 625
    typedef typename Parent::Value Value;
621 626

	
622 627
    /// Constructor
623 628
    FunctorToMap(const F &f = F()) : _f(f) {}
624 629
    /// \e
625 630
    Value operator[](const Key &k) const { return _f(k); }
626 631
  };
627 632

	
628 633
  /// Returns a \ref FunctorToMap class
629 634

	
630 635
  /// This function just returns a \ref FunctorToMap class.
631 636
  ///
632 637
  /// This function is specialized for adaptable binary function
633 638
  /// classes and C++ functions.
634 639
  ///
635 640
  /// \relates FunctorToMap
636 641
  template<typename K, typename V, typename F>
637 642
  inline FunctorToMap<F, K, V> functorToMap(const F &f) {
638 643
    return FunctorToMap<F, K, V>(f);
639 644
  }
640 645

	
641 646
  template <typename F>
642 647
  inline FunctorToMap<F, typename F::argument_type, typename F::result_type>
643 648
    functorToMap(const F &f)
644 649
  {
645 650
    return FunctorToMap<F, typename F::argument_type,
646 651
      typename F::result_type>(f);
647 652
  }
648 653

	
649 654
  template <typename K, typename V>
650 655
  inline FunctorToMap<V (*)(K), K, V> functorToMap(V (*f)(K)) {
651 656
    return FunctorToMap<V (*)(K), K, V>(f);
652 657
  }
653 658

	
654 659

	
655 660
  /// Converts a map to an STL style (unary) functor
656 661

	
657 662
  /// This class converts a map to an STL style (unary) functor.
658 663
  /// That is it provides an <tt>operator()</tt> to read its values.
659 664
  ///
660 665
  /// For the sake of convenience it also works as a usual
661 666
  /// \ref concepts::ReadMap "readable map", i.e. <tt>operator[]</tt>
662 667
  /// and the \c Key and \c Value typedefs also exist.
663 668
  ///
664 669
  /// The simplest way of using this map is through the mapToFunctor()
665 670
  /// function.
666 671
  ///
667 672
  ///\sa FunctorToMap
668 673
  template <typename M>
669 674
  class MapToFunctor : public MapBase<typename M::Key, typename M::Value> {
670 675
    const M &_m;
671 676
  public:
672 677
    typedef MapBase<typename M::Key, typename M::Value> Parent;
673 678
    typedef typename Parent::Key Key;
674 679
    typedef typename Parent::Value Value;
675 680

	
676 681
    typedef typename Parent::Key argument_type;
677 682
    typedef typename Parent::Value result_type;
678 683

	
679 684
    /// Constructor
680 685
    MapToFunctor(const M &m) : _m(m) {}
681 686
    /// \e
682 687
    Value operator()(const Key &k) const { return _m[k]; }
683 688
    /// \e
684 689
    Value operator[](const Key &k) const { return _m[k]; }
685 690
  };
686 691

	
687 692
  /// Returns a \ref MapToFunctor class
688 693

	
689 694
  /// This function just returns a \ref MapToFunctor class.
690 695
  /// \relates MapToFunctor
691 696
  template<typename M>
692 697
  inline MapToFunctor<M> mapToFunctor(const M &m) {
693 698
    return MapToFunctor<M>(m);
694 699
  }
695 700

	
696 701

	
697 702
  /// \brief Map adaptor to convert the \c Value type of a map to
698 703
  /// another type using the default conversion.
699 704

	
700 705
  /// Map adaptor to convert the \c Value type of a \ref concepts::ReadMap
701 706
  /// "readable map" to another type using the default conversion.
702 707
  /// The \c Key type of it is inherited from \c M and the \c Value
703 708
  /// type is \c V.
704 709
  /// This type conforms the \ref concepts::ReadMap "ReadMap" concept.
705 710
  ///
706 711
  /// The simplest way of using this map is through the convertMap()
707 712
  /// function.
708 713
  template <typename M, typename V>
709 714
  class ConvertMap : public MapBase<typename M::Key, V> {
710 715
    const M &_m;
711 716
  public:
712 717
    typedef MapBase<typename M::Key, V> Parent;
713 718
    typedef typename Parent::Key Key;
714 719
    typedef typename Parent::Value Value;
715 720

	
716 721
    /// Constructor
717 722

	
718 723
    /// Constructor.
719 724
    /// \param m The underlying map.
720 725
    ConvertMap(const M &m) : _m(m) {}
721 726

	
722 727
    /// \e
723 728
    Value operator[](const Key &k) const { return _m[k]; }
724 729
  };
725 730

	
726 731
  /// Returns a \ref ConvertMap class
727 732

	
728 733
  /// This function just returns a \ref ConvertMap class.
729 734
  /// \relates ConvertMap
730 735
  template<typename V, typename M>
731 736
  inline ConvertMap<M, V> convertMap(const M &map) {
732 737
    return ConvertMap<M, V>(map);
733 738
  }
734 739

	
735 740

	
736 741
  /// Applies all map setting operations to two maps
737 742

	
738 743
  /// This map has two \ref concepts::WriteMap "writable map" parameters
739 744
  /// and each write request will be passed to both of them.
740 745
  /// If \c M1 is also \ref concepts::ReadMap "readable", then the read
741 746
  /// operations will return the corresponding values of \c M1.
742 747
  ///
743 748
  /// The \c Key and \c Value types are inherited from \c M1.
744 749
  /// The \c Key and \c Value of \c M2 must be convertible from those
745 750
  /// of \c M1.
746 751
  ///
747 752
  /// The simplest way of using this map is through the forkMap()
748 753
  /// function.
749 754
  template<typename  M1, typename M2>
750 755
  class ForkMap : public MapBase<typename M1::Key, typename M1::Value> {
751 756
    M1 &_m1;
752 757
    M2 &_m2;
753 758
  public:
754 759
    typedef MapBase<typename M1::Key, typename M1::Value> Parent;
755 760
    typedef typename Parent::Key Key;
756 761
    typedef typename Parent::Value Value;
757 762

	
758 763
    /// Constructor
759 764
    ForkMap(M1 &m1, M2 &m2) : _m1(m1), _m2(m2) {}
760 765
    /// Returns the value associated with the given key in the first map.
761 766
    Value operator[](const Key &k) const { return _m1[k]; }
762 767
    /// Sets the value associated with the given key in both maps.
763 768
    void set(const Key &k, const Value &v) { _m1.set(k,v); _m2.set(k,v); }
764 769
  };
765 770

	
766 771
  /// Returns a \ref ForkMap class
767 772

	
768 773
  /// This function just returns a \ref ForkMap class.
769 774
  /// \relates ForkMap
770 775
  template <typename M1, typename M2>
771 776
  inline ForkMap<M1,M2> forkMap(M1 &m1, M2 &m2) {
772 777
    return ForkMap<M1,M2>(m1,m2);
773 778
  }
774 779

	
775 780

	
776 781
  /// Sum of two maps
777 782

	
778 783
  /// This \ref concepts::ReadMap "read-only map" returns the sum
779 784
  /// of the values of the two given maps.
780 785
  /// Its \c Key and \c Value types are inherited from \c M1.
781 786
  /// The \c Key and \c Value of \c M2 must be convertible to those of
782 787
  /// \c M1.
783 788
  ///
784 789
  /// If \c m1 is of type \c M1 and \c m2 is of \c M2, then for
785 790
  /// \code
786 791
  ///   AddMap<M1,M2> am(m1,m2);
787 792
  /// \endcode
788 793
  /// <tt>am[x]</tt> will be equal to <tt>m1[x]+m2[x]</tt>.
789 794
  ///
790 795
  /// The simplest way of using this map is through the addMap()
791 796
  /// function.
792 797
  ///
793 798
  /// \sa SubMap, MulMap, DivMap
794 799
  /// \sa ShiftMap, ShiftWriteMap
795 800
  template<typename M1, typename M2>
796 801
  class AddMap : public MapBase<typename M1::Key, typename M1::Value> {
797 802
    const M1 &_m1;
798 803
    const M2 &_m2;
799 804
  public:
800 805
    typedef MapBase<typename M1::Key, typename M1::Value> Parent;
801 806
    typedef typename Parent::Key Key;
802 807
    typedef typename Parent::Value Value;
803 808

	
804 809
    /// Constructor
805 810
    AddMap(const M1 &m1, const M2 &m2) : _m1(m1), _m2(m2) {}
806 811
    /// \e
807 812
    Value operator[](const Key &k) const { return _m1[k]+_m2[k]; }
808 813
  };
809 814

	
810 815
  /// Returns an \ref AddMap class
811 816

	
812 817
  /// This function just returns an \ref AddMap class.
813 818
  ///
814 819
  /// For example, if \c m1 and \c m2 are both maps with \c double
815 820
  /// values, then <tt>addMap(m1,m2)[x]</tt> will be equal to
816 821
  /// <tt>m1[x]+m2[x]</tt>.
817 822
  ///
818 823
  /// \relates AddMap
819 824
  template<typename M1, typename M2>
820 825
  inline AddMap<M1, M2> addMap(const M1 &m1, const M2 &m2) {
821 826
    return AddMap<M1, M2>(m1,m2);
822 827
  }
823 828

	
824 829

	
825 830
  /// Difference of two maps
826 831

	
827 832
  /// This \ref concepts::ReadMap "read-only map" returns the difference
828 833
  /// of the values of the two given maps.
829 834
  /// Its \c Key and \c Value types are inherited from \c M1.
830 835
  /// The \c Key and \c Value of \c M2 must be convertible to those of
831 836
  /// \c M1.
832 837
  ///
833 838
  /// If \c m1 is of type \c M1 and \c m2 is of \c M2, then for
834 839
  /// \code
835 840
  ///   SubMap<M1,M2> sm(m1,m2);
836 841
  /// \endcode
837 842
  /// <tt>sm[x]</tt> will be equal to <tt>m1[x]-m2[x]</tt>.
838 843
  ///
839 844
  /// The simplest way of using this map is through the subMap()
840 845
  /// function.
841 846
  ///
842 847
  /// \sa AddMap, MulMap, DivMap
843 848
  template<typename M1, typename M2>
844 849
  class SubMap : public MapBase<typename M1::Key, typename M1::Value> {
845 850
    const M1 &_m1;
846 851
    const M2 &_m2;
847 852
  public:
848 853
    typedef MapBase<typename M1::Key, typename M1::Value> Parent;
849 854
    typedef typename Parent::Key Key;
850 855
    typedef typename Parent::Value Value;
851 856

	
852 857
    /// Constructor
853 858
    SubMap(const M1 &m1, const M2 &m2) : _m1(m1), _m2(m2) {}
854 859
    /// \e
855 860
    Value operator[](const Key &k) const { return _m1[k]-_m2[k]; }
856 861
  };
857 862

	
858 863
  /// Returns a \ref SubMap class
859 864

	
860 865
  /// This function just returns a \ref SubMap class.
861 866
  ///
862 867
  /// For example, if \c m1 and \c m2 are both maps with \c double
863 868
  /// values, then <tt>subMap(m1,m2)[x]</tt> will be equal to
864 869
  /// <tt>m1[x]-m2[x]</tt>.
865 870
  ///
866 871
  /// \relates SubMap
867 872
  template<typename M1, typename M2>
868 873
  inline SubMap<M1, M2> subMap(const M1 &m1, const M2 &m2) {
869 874
    return SubMap<M1, M2>(m1,m2);
870 875
  }
871 876

	
872 877

	
873 878
  /// Product of two maps
874 879

	
875 880
  /// This \ref concepts::ReadMap "read-only map" returns the product
876 881
  /// of the values of the two given maps.
877 882
  /// Its \c Key and \c Value types are inherited from \c M1.
878 883
  /// The \c Key and \c Value of \c M2 must be convertible to those of
879 884
  /// \c M1.
880 885
  ///
881 886
  /// If \c m1 is of type \c M1 and \c m2 is of \c M2, then for
882 887
  /// \code
883 888
  ///   MulMap<M1,M2> mm(m1,m2);
884 889
  /// \endcode
885 890
  /// <tt>mm[x]</tt> will be equal to <tt>m1[x]*m2[x]</tt>.
886 891
  ///
887 892
  /// The simplest way of using this map is through the mulMap()
888 893
  /// function.
889 894
  ///
890 895
  /// \sa AddMap, SubMap, DivMap
891 896
  /// \sa ScaleMap, ScaleWriteMap
892 897
  template<typename M1, typename M2>
893 898
  class MulMap : public MapBase<typename M1::Key, typename M1::Value> {
894 899
    const M1 &_m1;
895 900
    const M2 &_m2;
896 901
  public:
897 902
    typedef MapBase<typename M1::Key, typename M1::Value> Parent;
898 903
    typedef typename Parent::Key Key;
899 904
    typedef typename Parent::Value Value;
900 905

	
901 906
    /// Constructor
902 907
    MulMap(const M1 &m1,const M2 &m2) : _m1(m1), _m2(m2) {}
903 908
    /// \e
904 909
    Value operator[](const Key &k) const { return _m1[k]*_m2[k]; }
905 910
  };
906 911

	
907 912
  /// Returns a \ref MulMap class
908 913

	
909 914
  /// This function just returns a \ref MulMap class.
910 915
  ///
911 916
  /// For example, if \c m1 and \c m2 are both maps with \c double
912 917
  /// values, then <tt>mulMap(m1,m2)[x]</tt> will be equal to
913 918
  /// <tt>m1[x]*m2[x]</tt>.
914 919
  ///
915 920
  /// \relates MulMap
916 921
  template<typename M1, typename M2>
917 922
  inline MulMap<M1, M2> mulMap(const M1 &m1,const M2 &m2) {
918 923
    return MulMap<M1, M2>(m1,m2);
919 924
  }
920 925

	
921 926

	
922 927
  /// Quotient of two maps
923 928

	
924 929
  /// This \ref concepts::ReadMap "read-only map" returns the quotient
925 930
  /// of the values of the two given maps.
926 931
  /// Its \c Key and \c Value types are inherited from \c M1.
927 932
  /// The \c Key and \c Value of \c M2 must be convertible to those of
928 933
  /// \c M1.
929 934
  ///
930 935
  /// If \c m1 is of type \c M1 and \c m2 is of \c M2, then for
931 936
  /// \code
932 937
  ///   DivMap<M1,M2> dm(m1,m2);
933 938
  /// \endcode
934 939
  /// <tt>dm[x]</tt> will be equal to <tt>m1[x]/m2[x]</tt>.
935 940
  ///
936 941
  /// The simplest way of using this map is through the divMap()
937 942
  /// function.
938 943
  ///
939 944
  /// \sa AddMap, SubMap, MulMap
940 945
  template<typename M1, typename M2>
941 946
  class DivMap : public MapBase<typename M1::Key, typename M1::Value> {
942 947
    const M1 &_m1;
943 948
    const M2 &_m2;
944 949
  public:
945 950
    typedef MapBase<typename M1::Key, typename M1::Value> Parent;
946 951
    typedef typename Parent::Key Key;
947 952
    typedef typename Parent::Value Value;
948 953

	
949 954
    /// Constructor
950 955
    DivMap(const M1 &m1,const M2 &m2) : _m1(m1), _m2(m2) {}
951 956
    /// \e
952 957
    Value operator[](const Key &k) const { return _m1[k]/_m2[k]; }
953 958
  };
954 959

	
955 960
  /// Returns a \ref DivMap class
956 961

	
957 962
  /// This function just returns a \ref DivMap class.
958 963
  ///
959 964
  /// For example, if \c m1 and \c m2 are both maps with \c double
960 965
  /// values, then <tt>divMap(m1,m2)[x]</tt> will be equal to
961 966
  /// <tt>m1[x]/m2[x]</tt>.
962 967
  ///
963 968
  /// \relates DivMap
964 969
  template<typename M1, typename M2>
965 970
  inline DivMap<M1, M2> divMap(const M1 &m1,const M2 &m2) {
966 971
    return DivMap<M1, M2>(m1,m2);
967 972
  }
968 973

	
969 974

	
970 975
  /// Shifts a map with a constant.
971 976

	
972 977
  /// This \ref concepts::ReadMap "read-only map" returns the sum of
973 978
  /// the given map and a constant value (i.e. it shifts the map with
974 979
  /// the constant). Its \c Key and \c Value are inherited from \c M.
975 980
  ///
976 981
  /// Actually,
977 982
  /// \code
978 983
  ///   ShiftMap<M> sh(m,v);
979 984
  /// \endcode
980 985
  /// is equivalent to
981 986
  /// \code
982 987
  ///   ConstMap<M::Key, M::Value> cm(v);
983 988
  ///   AddMap<M, ConstMap<M::Key, M::Value> > sh(m,cm);
984 989
  /// \endcode
985 990
  ///
986 991
  /// The simplest way of using this map is through the shiftMap()
987 992
  /// function.
988 993
  ///
989 994
  /// \sa ShiftWriteMap
990 995
  template<typename M, typename C = typename M::Value>
991 996
  class ShiftMap : public MapBase<typename M::Key, typename M::Value> {
992 997
    const M &_m;
993 998
    C _v;
994 999
  public:
995 1000
    typedef MapBase<typename M::Key, typename M::Value> Parent;
996 1001
    typedef typename Parent::Key Key;
997 1002
    typedef typename Parent::Value Value;
998 1003

	
999 1004
    /// Constructor
1000 1005

	
1001 1006
    /// Constructor.
1002 1007
    /// \param m The undelying map.
1003 1008
    /// \param v The constant value.
1004 1009
    ShiftMap(const M &m, const C &v) : _m(m), _v(v) {}
1005 1010
    /// \e
1006 1011
    Value operator[](const Key &k) const { return _m[k]+_v; }
1007 1012
  };
1008 1013

	
1009 1014
  /// Shifts a map with a constant (read-write version).
1010 1015

	
1011 1016
  /// This \ref concepts::ReadWriteMap "read-write map" returns the sum
1012 1017
  /// of the given map and a constant value (i.e. it shifts the map with
1013 1018
  /// the constant). Its \c Key and \c Value are inherited from \c M.
1014 1019
  /// It makes also possible to write the map.
1015 1020
  ///
1016 1021
  /// The simplest way of using this map is through the shiftWriteMap()
1017 1022
  /// function.
1018 1023
  ///
1019 1024
  /// \sa ShiftMap
1020 1025
  template<typename M, typename C = typename M::Value>
1021 1026
  class ShiftWriteMap : public MapBase<typename M::Key, typename M::Value> {
1022 1027
    M &_m;
1023 1028
    C _v;
1024 1029
  public:
1025 1030
    typedef MapBase<typename M::Key, typename M::Value> Parent;
1026 1031
    typedef typename Parent::Key Key;
1027 1032
    typedef typename Parent::Value Value;
1028 1033

	
1029 1034
    /// Constructor
1030 1035

	
1031 1036
    /// Constructor.
1032 1037
    /// \param m The undelying map.
1033 1038
    /// \param v The constant value.
1034 1039
    ShiftWriteMap(M &m, const C &v) : _m(m), _v(v) {}
1035 1040
    /// \e
1036 1041
    Value operator[](const Key &k) const { return _m[k]+_v; }
1037 1042
    /// \e
1038 1043
    void set(const Key &k, const Value &v) { _m.set(k, v-_v); }
1039 1044
  };
1040 1045

	
1041 1046
  /// Returns a \ref ShiftMap class
1042 1047

	
1043 1048
  /// This function just returns a \ref ShiftMap class.
1044 1049
  ///
1045 1050
  /// For example, if \c m is a map with \c double values and \c v is
1046 1051
  /// \c double, then <tt>shiftMap(m,v)[x]</tt> will be equal to
1047 1052
  /// <tt>m[x]+v</tt>.
1048 1053
  ///
1049 1054
  /// \relates ShiftMap
1050 1055
  template<typename M, typename C>
1051 1056
  inline ShiftMap<M, C> shiftMap(const M &m, const C &v) {
1052 1057
    return ShiftMap<M, C>(m,v);
1053 1058
  }
1054 1059

	
1055 1060
  /// Returns a \ref ShiftWriteMap class
1056 1061

	
1057 1062
  /// This function just returns a \ref ShiftWriteMap class.
1058 1063
  ///
1059 1064
  /// For example, if \c m is a map with \c double values and \c v is
1060 1065
  /// \c double, then <tt>shiftWriteMap(m,v)[x]</tt> will be equal to
1061 1066
  /// <tt>m[x]+v</tt>.
1062 1067
  /// Moreover it makes also possible to write the map.
1063 1068
  ///
1064 1069
  /// \relates ShiftWriteMap
1065 1070
  template<typename M, typename C>
1066 1071
  inline ShiftWriteMap<M, C> shiftWriteMap(M &m, const C &v) {
1067 1072
    return ShiftWriteMap<M, C>(m,v);
1068 1073
  }
1069 1074

	
1070 1075

	
1071 1076
  /// Scales a map with a constant.
1072 1077

	
1073 1078
  /// This \ref concepts::ReadMap "read-only map" returns the value of
1074 1079
  /// the given map multiplied from the left side with a constant value.
1075 1080
  /// Its \c Key and \c Value are inherited from \c M.
1076 1081
  ///
1077 1082
  /// Actually,
1078 1083
  /// \code
1079 1084
  ///   ScaleMap<M> sc(m,v);
1080 1085
  /// \endcode
1081 1086
  /// is equivalent to
1082 1087
  /// \code
1083 1088
  ///   ConstMap<M::Key, M::Value> cm(v);
1084 1089
  ///   MulMap<ConstMap<M::Key, M::Value>, M> sc(cm,m);
1085 1090
  /// \endcode
1086 1091
  ///
1087 1092
  /// The simplest way of using this map is through the scaleMap()
1088 1093
  /// function.
1089 1094
  ///
1090 1095
  /// \sa ScaleWriteMap
1091 1096
  template<typename M, typename C = typename M::Value>
1092 1097
  class ScaleMap : public MapBase<typename M::Key, typename M::Value> {
1093 1098
    const M &_m;
1094 1099
    C _v;
1095 1100
  public:
1096 1101
    typedef MapBase<typename M::Key, typename M::Value> Parent;
1097 1102
    typedef typename Parent::Key Key;
1098 1103
    typedef typename Parent::Value Value;
1099 1104

	
1100 1105
    /// Constructor
1101 1106

	
1102 1107
    /// Constructor.
1103 1108
    /// \param m The undelying map.
1104 1109
    /// \param v The constant value.
1105 1110
    ScaleMap(const M &m, const C &v) : _m(m), _v(v) {}
1106 1111
    /// \e
1107 1112
    Value operator[](const Key &k) const { return _v*_m[k]; }
1108 1113
  };
1109 1114

	
1110 1115
  /// Scales a map with a constant (read-write version).
1111 1116

	
1112 1117
  /// This \ref concepts::ReadWriteMap "read-write map" returns the value of
1113 1118
  /// the given map multiplied from the left side with a constant value.
1114 1119
  /// Its \c Key and \c Value are inherited from \c M.
1115 1120
  /// It can also be used as write map if the \c / operator is defined
1116 1121
  /// between \c Value and \c C and the given multiplier is not zero.
1117 1122
  ///
1118 1123
  /// The simplest way of using this map is through the scaleWriteMap()
1119 1124
  /// function.
1120 1125
  ///
1121 1126
  /// \sa ScaleMap
1122 1127
  template<typename M, typename C = typename M::Value>
1123 1128
  class ScaleWriteMap : public MapBase<typename M::Key, typename M::Value> {
1124 1129
    M &_m;
1125 1130
    C _v;
1126 1131
  public:
1127 1132
    typedef MapBase<typename M::Key, typename M::Value> Parent;
1128 1133
    typedef typename Parent::Key Key;
1129 1134
    typedef typename Parent::Value Value;
1130 1135

	
1131 1136
    /// Constructor
1132 1137

	
1133 1138
    /// Constructor.
1134 1139
    /// \param m The undelying map.
1135 1140
    /// \param v The constant value.
1136 1141
    ScaleWriteMap(M &m, const C &v) : _m(m), _v(v) {}
1137 1142
    /// \e
1138 1143
    Value operator[](const Key &k) const { return _v*_m[k]; }
1139 1144
    /// \e
1140 1145
    void set(const Key &k, const Value &v) { _m.set(k, v/_v); }
1141 1146
  };
1142 1147

	
1143 1148
  /// Returns a \ref ScaleMap class
1144 1149

	
1145 1150
  /// This function just returns a \ref ScaleMap class.
1146 1151
  ///
1147 1152
  /// For example, if \c m is a map with \c double values and \c v is
1148 1153
  /// \c double, then <tt>scaleMap(m,v)[x]</tt> will be equal to
1149 1154
  /// <tt>v*m[x]</tt>.
1150 1155
  ///
1151 1156
  /// \relates ScaleMap
1152 1157
  template<typename M, typename C>
1153 1158
  inline ScaleMap<M, C> scaleMap(const M &m, const C &v) {
1154 1159
    return ScaleMap<M, C>(m,v);
1155 1160
  }
1156 1161

	
1157 1162
  /// Returns a \ref ScaleWriteMap class
1158 1163

	
1159 1164
  /// This function just returns a \ref ScaleWriteMap class.
1160 1165
  ///
1161 1166
  /// For example, if \c m is a map with \c double values and \c v is
1162 1167
  /// \c double, then <tt>scaleWriteMap(m,v)[x]</tt> will be equal to
1163 1168
  /// <tt>v*m[x]</tt>.
1164 1169
  /// Moreover it makes also possible to write the map.
1165 1170
  ///
1166 1171
  /// \relates ScaleWriteMap
1167 1172
  template<typename M, typename C>
1168 1173
  inline ScaleWriteMap<M, C> scaleWriteMap(M &m, const C &v) {
1169 1174
    return ScaleWriteMap<M, C>(m,v);
1170 1175
  }
1171 1176

	
1172 1177

	
1173 1178
  /// Negative of a map
1174 1179

	
1175 1180
  /// This \ref concepts::ReadMap "read-only map" returns the negative
1176 1181
  /// of the values of the given map (using the unary \c - operator).
1177 1182
  /// Its \c Key and \c Value are inherited from \c M.
1178 1183
  ///
1179 1184
  /// If M::Value is \c int, \c double etc., then
1180 1185
  /// \code
1181 1186
  ///   NegMap<M> neg(m);
1182 1187
  /// \endcode
1183 1188
  /// is equivalent to
1184 1189
  /// \code
1185 1190
  ///   ScaleMap<M> neg(m,-1);
1186 1191
  /// \endcode
1187 1192
  ///
1188 1193
  /// The simplest way of using this map is through the negMap()
1189 1194
  /// function.
1190 1195
  ///
1191 1196
  /// \sa NegWriteMap
1192 1197
  template<typename M>
1193 1198
  class NegMap : public MapBase<typename M::Key, typename M::Value> {
1194 1199
    const M& _m;
1195 1200
  public:
1196 1201
    typedef MapBase<typename M::Key, typename M::Value> Parent;
1197 1202
    typedef typename Parent::Key Key;
1198 1203
    typedef typename Parent::Value Value;
1199 1204

	
1200 1205
    /// Constructor
1201 1206
    NegMap(const M &m) : _m(m) {}
1202 1207
    /// \e
1203 1208
    Value operator[](const Key &k) const { return -_m[k]; }
1204 1209
  };
1205 1210

	
1206 1211
  /// Negative of a map (read-write version)
1207 1212

	
1208 1213
  /// This \ref concepts::ReadWriteMap "read-write map" returns the
1209 1214
  /// negative of the values of the given map (using the unary \c -
1210 1215
  /// operator).
1211 1216
  /// Its \c Key and \c Value are inherited from \c M.
1212 1217
  /// It makes also possible to write the map.
1213 1218
  ///
1214 1219
  /// If M::Value is \c int, \c double etc., then
1215 1220
  /// \code
1216 1221
  ///   NegWriteMap<M> neg(m);
1217 1222
  /// \endcode
1218 1223
  /// is equivalent to
1219 1224
  /// \code
1220 1225
  ///   ScaleWriteMap<M> neg(m,-1);
1221 1226
  /// \endcode
1222 1227
  ///
1223 1228
  /// The simplest way of using this map is through the negWriteMap()
1224 1229
  /// function.
1225 1230
  ///
1226 1231
  /// \sa NegMap
1227 1232
  template<typename M>
1228 1233
  class NegWriteMap : public MapBase<typename M::Key, typename M::Value> {
1229 1234
    M &_m;
1230 1235
  public:
1231 1236
    typedef MapBase<typename M::Key, typename M::Value> Parent;
1232 1237
    typedef typename Parent::Key Key;
1233 1238
    typedef typename Parent::Value Value;
1234 1239

	
1235 1240
    /// Constructor
1236 1241
    NegWriteMap(M &m) : _m(m) {}
1237 1242
    /// \e
1238 1243
    Value operator[](const Key &k) const { return -_m[k]; }
1239 1244
    /// \e
1240 1245
    void set(const Key &k, const Value &v) { _m.set(k, -v); }
1241 1246
  };
1242 1247

	
1243 1248
  /// Returns a \ref NegMap class
1244 1249

	
1245 1250
  /// This function just returns a \ref NegMap class.
1246 1251
  ///
1247 1252
  /// For example, if \c m is a map with \c double values, then
1248 1253
  /// <tt>negMap(m)[x]</tt> will be equal to <tt>-m[x]</tt>.
1249 1254
  ///
1250 1255
  /// \relates NegMap
1251 1256
  template <typename M>
1252 1257
  inline NegMap<M> negMap(const M &m) {
1253 1258
    return NegMap<M>(m);
1254 1259
  }
1255 1260

	
1256 1261
  /// Returns a \ref NegWriteMap class
1257 1262

	
1258 1263
  /// This function just returns a \ref NegWriteMap class.
1259 1264
  ///
1260 1265
  /// For example, if \c m is a map with \c double values, then
1261 1266
  /// <tt>negWriteMap(m)[x]</tt> will be equal to <tt>-m[x]</tt>.
1262 1267
  /// Moreover it makes also possible to write the map.
1263 1268
  ///
1264 1269
  /// \relates NegWriteMap
1265 1270
  template <typename M>
1266 1271
  inline NegWriteMap<M> negWriteMap(M &m) {
1267 1272
    return NegWriteMap<M>(m);
1268 1273
  }
1269 1274

	
1270 1275

	
1271 1276
  /// Absolute value of a map
1272 1277

	
1273 1278
  /// This \ref concepts::ReadMap "read-only map" returns the absolute
1274 1279
  /// value of the values of the given map.
1275 1280
  /// Its \c Key and \c Value are inherited from \c M.
1276 1281
  /// \c Value must be comparable to \c 0 and the unary \c -
1277 1282
  /// operator must be defined for it, of course.
1278 1283
  ///
1279 1284
  /// The simplest way of using this map is through the absMap()
1280 1285
  /// function.
1281 1286
  template<typename M>
1282 1287
  class AbsMap : public MapBase<typename M::Key, typename M::Value> {
1283 1288
    const M &_m;
1284 1289
  public:
1285 1290
    typedef MapBase<typename M::Key, typename M::Value> Parent;
1286 1291
    typedef typename Parent::Key Key;
1287 1292
    typedef typename Parent::Value Value;
1288 1293

	
1289 1294
    /// Constructor
1290 1295
    AbsMap(const M &m) : _m(m) {}
1291 1296
    /// \e
1292 1297
    Value operator[](const Key &k) const {
1293 1298
      Value tmp = _m[k];
1294 1299
      return tmp >= 0 ? tmp : -tmp;
1295 1300
    }
1296 1301

	
1297 1302
  };
1298 1303

	
1299 1304
  /// Returns an \ref AbsMap class
1300 1305

	
1301 1306
  /// This function just returns an \ref AbsMap class.
1302 1307
  ///
1303 1308
  /// For example, if \c m is a map with \c double values, then
1304 1309
  /// <tt>absMap(m)[x]</tt> will be equal to <tt>m[x]</tt> if
1305 1310
  /// it is positive or zero and <tt>-m[x]</tt> if <tt>m[x]</tt> is
1306 1311
  /// negative.
1307 1312
  ///
1308 1313
  /// \relates AbsMap
1309 1314
  template<typename M>
1310 1315
  inline AbsMap<M> absMap(const M &m) {
1311 1316
    return AbsMap<M>(m);
1312 1317
  }
1313 1318

	
1314 1319
  /// @}
1315 1320
  
1316 1321
  // Logical maps and map adaptors:
1317 1322

	
1318 1323
  /// \addtogroup maps
1319 1324
  /// @{
1320 1325

	
1321 1326
  /// Constant \c true map.
1322 1327

	
1323 1328
  /// This \ref concepts::ReadMap "read-only map" assigns \c true to
1324 1329
  /// each key.
1325 1330
  ///
1326 1331
  /// Note that
1327 1332
  /// \code
1328 1333
  ///   TrueMap<K> tm;
1329 1334
  /// \endcode
1330 1335
  /// is equivalent to
1331 1336
  /// \code
1332 1337
  ///   ConstMap<K,bool> tm(true);
1333 1338
  /// \endcode
1334 1339
  ///
1335 1340
  /// \sa FalseMap
1336 1341
  /// \sa ConstMap
1337 1342
  template <typename K>
1338 1343
  class TrueMap : public MapBase<K, bool> {
1339 1344
  public:
1340 1345
    typedef MapBase<K, bool> Parent;
1341 1346
    typedef typename Parent::Key Key;
1342 1347
    typedef typename Parent::Value Value;
1343 1348

	
1344 1349
    /// Gives back \c true.
1345 1350
    Value operator[](const Key&) const { return true; }
1346 1351
  };
1347 1352

	
1348 1353
  /// Returns a \ref TrueMap class
1349 1354

	
1350 1355
  /// This function just returns a \ref TrueMap class.
1351 1356
  /// \relates TrueMap
1352 1357
  template<typename K>
1353 1358
  inline TrueMap<K> trueMap() {
1354 1359
    return TrueMap<K>();
1355 1360
  }
1356 1361

	
1357 1362

	
1358 1363
  /// Constant \c false map.
1359 1364

	
1360 1365
  /// This \ref concepts::ReadMap "read-only map" assigns \c false to
1361 1366
  /// each key.
1362 1367
  ///
1363 1368
  /// Note that
1364 1369
  /// \code
1365 1370
  ///   FalseMap<K> fm;
1366 1371
  /// \endcode
1367 1372
  /// is equivalent to
1368 1373
  /// \code
1369 1374
  ///   ConstMap<K,bool> fm(false);
1370 1375
  /// \endcode
1371 1376
  ///
1372 1377
  /// \sa TrueMap
1373 1378
  /// \sa ConstMap
1374 1379
  template <typename K>
1375 1380
  class FalseMap : public MapBase<K, bool> {
1376 1381
  public:
1377 1382
    typedef MapBase<K, bool> Parent;
1378 1383
    typedef typename Parent::Key Key;
1379 1384
    typedef typename Parent::Value Value;
1380 1385

	
1381 1386
    /// Gives back \c false.
1382 1387
    Value operator[](const Key&) const { return false; }
1383 1388
  };
1384 1389

	
1385 1390
  /// Returns a \ref FalseMap class
1386 1391

	
1387 1392
  /// This function just returns a \ref FalseMap class.
1388 1393
  /// \relates FalseMap
1389 1394
  template<typename K>
1390 1395
  inline FalseMap<K> falseMap() {
1391 1396
    return FalseMap<K>();
1392 1397
  }
1393 1398

	
1394 1399
  /// @}
1395 1400

	
1396 1401
  /// \addtogroup map_adaptors
1397 1402
  /// @{
1398 1403

	
1399 1404
  /// Logical 'and' of two maps
1400 1405

	
1401 1406
  /// This \ref concepts::ReadMap "read-only map" returns the logical
1402 1407
  /// 'and' of the values of the two given maps.
1403 1408
  /// Its \c Key type is inherited from \c M1 and its \c Value type is
1404 1409
  /// \c bool. \c M2::Key must be convertible to \c M1::Key.
1405 1410
  ///
1406 1411
  /// If \c m1 is of type \c M1 and \c m2 is of \c M2, then for
1407 1412
  /// \code
1408 1413
  ///   AndMap<M1,M2> am(m1,m2);
1409 1414
  /// \endcode
1410 1415
  /// <tt>am[x]</tt> will be equal to <tt>m1[x]&&m2[x]</tt>.
1411 1416
  ///
1412 1417
  /// The simplest way of using this map is through the andMap()
1413 1418
  /// function.
1414 1419
  ///
1415 1420
  /// \sa OrMap
1416 1421
  /// \sa NotMap, NotWriteMap
1417 1422
  template<typename M1, typename M2>
1418 1423
  class AndMap : public MapBase<typename M1::Key, bool> {
1419 1424
    const M1 &_m1;
1420 1425
    const M2 &_m2;
1421 1426
  public:
1422 1427
    typedef MapBase<typename M1::Key, bool> Parent;
1423 1428
    typedef typename Parent::Key Key;
1424 1429
    typedef typename Parent::Value Value;
1425 1430

	
1426 1431
    /// Constructor
1427 1432
    AndMap(const M1 &m1, const M2 &m2) : _m1(m1), _m2(m2) {}
1428 1433
    /// \e
1429 1434
    Value operator[](const Key &k) const { return _m1[k]&&_m2[k]; }
1430 1435
  };
1431 1436

	
1432 1437
  /// Returns an \ref AndMap class
1433 1438

	
1434 1439
  /// This function just returns an \ref AndMap class.
1435 1440
  ///
1436 1441
  /// For example, if \c m1 and \c m2 are both maps with \c bool values,
1437 1442
  /// then <tt>andMap(m1,m2)[x]</tt> will be equal to
1438 1443
  /// <tt>m1[x]&&m2[x]</tt>.
1439 1444
  ///
1440 1445
  /// \relates AndMap
1441 1446
  template<typename M1, typename M2>
1442 1447
  inline AndMap<M1, M2> andMap(const M1 &m1, const M2 &m2) {
1443 1448
    return AndMap<M1, M2>(m1,m2);
1444 1449
  }
1445 1450

	
1446 1451

	
1447 1452
  /// Logical 'or' of two maps
1448 1453

	
1449 1454
  /// This \ref concepts::ReadMap "read-only map" returns the logical
1450 1455
  /// 'or' of the values of the two given maps.
1451 1456
  /// Its \c Key type is inherited from \c M1 and its \c Value type is
1452 1457
  /// \c bool. \c M2::Key must be convertible to \c M1::Key.
1453 1458
  ///
1454 1459
  /// If \c m1 is of type \c M1 and \c m2 is of \c M2, then for
1455 1460
  /// \code
1456 1461
  ///   OrMap<M1,M2> om(m1,m2);
1457 1462
  /// \endcode
1458 1463
  /// <tt>om[x]</tt> will be equal to <tt>m1[x]||m2[x]</tt>.
1459 1464
  ///
1460 1465
  /// The simplest way of using this map is through the orMap()
1461 1466
  /// function.
1462 1467
  ///
1463 1468
  /// \sa AndMap
1464 1469
  /// \sa NotMap, NotWriteMap
1465 1470
  template<typename M1, typename M2>
1466 1471
  class OrMap : public MapBase<typename M1::Key, bool> {
1467 1472
    const M1 &_m1;
1468 1473
    const M2 &_m2;
1469 1474
  public:
1470 1475
    typedef MapBase<typename M1::Key, bool> Parent;
1471 1476
    typedef typename Parent::Key Key;
1472 1477
    typedef typename Parent::Value Value;
1473 1478

	
1474 1479
    /// Constructor
1475 1480
    OrMap(const M1 &m1, const M2 &m2) : _m1(m1), _m2(m2) {}
1476 1481
    /// \e
1477 1482
    Value operator[](const Key &k) const { return _m1[k]||_m2[k]; }
1478 1483
  };
1479 1484

	
1480 1485
  /// Returns an \ref OrMap class
1481 1486

	
1482 1487
  /// This function just returns an \ref OrMap class.
1483 1488
  ///
1484 1489
  /// For example, if \c m1 and \c m2 are both maps with \c bool values,
1485 1490
  /// then <tt>orMap(m1,m2)[x]</tt> will be equal to
1486 1491
  /// <tt>m1[x]||m2[x]</tt>.
1487 1492
  ///
1488 1493
  /// \relates OrMap
1489 1494
  template<typename M1, typename M2>
1490 1495
  inline OrMap<M1, M2> orMap(const M1 &m1, const M2 &m2) {
1491 1496
    return OrMap<M1, M2>(m1,m2);
1492 1497
  }
1493 1498

	
1494 1499

	
1495 1500
  /// Logical 'not' of a map
1496 1501

	
1497 1502
  /// This \ref concepts::ReadMap "read-only map" returns the logical
1498 1503
  /// negation of the values of the given map.
1499 1504
  /// Its \c Key is inherited from \c M and its \c Value is \c bool.
1500 1505
  ///
1501 1506
  /// The simplest way of using this map is through the notMap()
1502 1507
  /// function.
1503 1508
  ///
1504 1509
  /// \sa NotWriteMap
1505 1510
  template <typename M>
1506 1511
  class NotMap : public MapBase<typename M::Key, bool> {
1507 1512
    const M &_m;
1508 1513
  public:
1509 1514
    typedef MapBase<typename M::Key, bool> Parent;
1510 1515
    typedef typename Parent::Key Key;
1511 1516
    typedef typename Parent::Value Value;
1512 1517

	
1513 1518
    /// Constructor
1514 1519
    NotMap(const M &m) : _m(m) {}
1515 1520
    /// \e
1516 1521
    Value operator[](const Key &k) const { return !_m[k]; }
1517 1522
  };
1518 1523

	
1519 1524
  /// Logical 'not' of a map (read-write version)
1520 1525

	
1521 1526
  /// This \ref concepts::ReadWriteMap "read-write map" returns the
1522 1527
  /// logical negation of the values of the given map.
1523 1528
  /// Its \c Key is inherited from \c M and its \c Value is \c bool.
1524 1529
  /// It makes also possible to write the map. When a value is set,
1525 1530
  /// the opposite value is set to the original map.
1526 1531
  ///
1527 1532
  /// The simplest way of using this map is through the notWriteMap()
1528 1533
  /// function.
1529 1534
  ///
1530 1535
  /// \sa NotMap
1531 1536
  template <typename M>
1532 1537
  class NotWriteMap : public MapBase<typename M::Key, bool> {
1533 1538
    M &_m;
1534 1539
  public:
1535 1540
    typedef MapBase<typename M::Key, bool> Parent;
1536 1541
    typedef typename Parent::Key Key;
1537 1542
    typedef typename Parent::Value Value;
1538 1543

	
1539 1544
    /// Constructor
1540 1545
    NotWriteMap(M &m) : _m(m) {}
1541 1546
    /// \e
1542 1547
    Value operator[](const Key &k) const { return !_m[k]; }
1543 1548
    /// \e
1544 1549
    void set(const Key &k, bool v) { _m.set(k, !v); }
1545 1550
  };
1546 1551

	
1547 1552
  /// Returns a \ref NotMap class
1548 1553

	
1549 1554
  /// This function just returns a \ref NotMap class.
1550 1555
  ///
1551 1556
  /// For example, if \c m is a map with \c bool values, then
1552 1557
  /// <tt>notMap(m)[x]</tt> will be equal to <tt>!m[x]</tt>.
1553 1558
  ///
1554 1559
  /// \relates NotMap
1555 1560
  template <typename M>
1556 1561
  inline NotMap<M> notMap(const M &m) {
1557 1562
    return NotMap<M>(m);
1558 1563
  }
1559 1564

	
1560 1565
  /// Returns a \ref NotWriteMap class
1561 1566

	
1562 1567
  /// This function just returns a \ref NotWriteMap class.
1563 1568
  ///
1564 1569
  /// For example, if \c m is a map with \c bool values, then
1565 1570
  /// <tt>notWriteMap(m)[x]</tt> will be equal to <tt>!m[x]</tt>.
1566 1571
  /// Moreover it makes also possible to write the map.
1567 1572
  ///
1568 1573
  /// \relates NotWriteMap
1569 1574
  template <typename M>
1570 1575
  inline NotWriteMap<M> notWriteMap(M &m) {
1571 1576
    return NotWriteMap<M>(m);
1572 1577
  }
1573 1578

	
1574 1579

	
1575 1580
  /// Combination of two maps using the \c == operator
1576 1581

	
1577 1582
  /// This \ref concepts::ReadMap "read-only map" assigns \c true to
1578 1583
  /// the keys for which the corresponding values of the two maps are
1579 1584
  /// equal.
1580 1585
  /// Its \c Key type is inherited from \c M1 and its \c Value type is
1581 1586
  /// \c bool. \c M2::Key must be convertible to \c M1::Key.
1582 1587
  ///
1583 1588
  /// If \c m1 is of type \c M1 and \c m2 is of \c M2, then for
1584 1589
  /// \code
1585 1590
  ///   EqualMap<M1,M2> em(m1,m2);
1586 1591
  /// \endcode
1587 1592
  /// <tt>em[x]</tt> will be equal to <tt>m1[x]==m2[x]</tt>.
1588 1593
  ///
1589 1594
  /// The simplest way of using this map is through the equalMap()
1590 1595
  /// function.
1591 1596
  ///
1592 1597
  /// \sa LessMap
1593 1598
  template<typename M1, typename M2>
1594 1599
  class EqualMap : public MapBase<typename M1::Key, bool> {
1595 1600
    const M1 &_m1;
1596 1601
    const M2 &_m2;
1597 1602
  public:
1598 1603
    typedef MapBase<typename M1::Key, bool> Parent;
1599 1604
    typedef typename Parent::Key Key;
1600 1605
    typedef typename Parent::Value Value;
1601 1606

	
1602 1607
    /// Constructor
1603 1608
    EqualMap(const M1 &m1, const M2 &m2) : _m1(m1), _m2(m2) {}
1604 1609
    /// \e
1605 1610
    Value operator[](const Key &k) const { return _m1[k]==_m2[k]; }
1606 1611
  };
1607 1612

	
1608 1613
  /// Returns an \ref EqualMap class
1609 1614

	
1610 1615
  /// This function just returns an \ref EqualMap class.
1611 1616
  ///
1612 1617
  /// For example, if \c m1 and \c m2 are maps with keys and values of
1613 1618
  /// the same type, then <tt>equalMap(m1,m2)[x]</tt> will be equal to
1614 1619
  /// <tt>m1[x]==m2[x]</tt>.
1615 1620
  ///
1616 1621
  /// \relates EqualMap
1617 1622
  template<typename M1, typename M2>
1618 1623
  inline EqualMap<M1, M2> equalMap(const M1 &m1, const M2 &m2) {
1619 1624
    return EqualMap<M1, M2>(m1,m2);
1620 1625
  }
1621 1626

	
1622 1627

	
1623 1628
  /// Combination of two maps using the \c < operator
1624 1629

	
1625 1630
  /// This \ref concepts::ReadMap "read-only map" assigns \c true to
1626 1631
  /// the keys for which the corresponding value of the first map is
1627 1632
  /// less then the value of the second map.
1628 1633
  /// Its \c Key type is inherited from \c M1 and its \c Value type is
1629 1634
  /// \c bool. \c M2::Key must be convertible to \c M1::Key.
1630 1635
  ///
1631 1636
  /// If \c m1 is of type \c M1 and \c m2 is of \c M2, then for
1632 1637
  /// \code
1633 1638
  ///   LessMap<M1,M2> lm(m1,m2);
1634 1639
  /// \endcode
1635 1640
  /// <tt>lm[x]</tt> will be equal to <tt>m1[x]<m2[x]</tt>.
1636 1641
  ///
1637 1642
  /// The simplest way of using this map is through the lessMap()
1638 1643
  /// function.
1639 1644
  ///
1640 1645
  /// \sa EqualMap
1641 1646
  template<typename M1, typename M2>
1642 1647
  class LessMap : public MapBase<typename M1::Key, bool> {
1643 1648
    const M1 &_m1;
1644 1649
    const M2 &_m2;
1645 1650
  public:
1646 1651
    typedef MapBase<typename M1::Key, bool> Parent;
1647 1652
    typedef typename Parent::Key Key;
1648 1653
    typedef typename Parent::Value Value;
1649 1654

	
1650 1655
    /// Constructor
1651 1656
    LessMap(const M1 &m1, const M2 &m2) : _m1(m1), _m2(m2) {}
1652 1657
    /// \e
1653 1658
    Value operator[](const Key &k) const { return _m1[k]<_m2[k]; }
1654 1659
  };
1655 1660

	
1656 1661
  /// Returns an \ref LessMap class
1657 1662

	
1658 1663
  /// This function just returns an \ref LessMap class.
1659 1664
  ///
1660 1665
  /// For example, if \c m1 and \c m2 are maps with keys and values of
1661 1666
  /// the same type, then <tt>lessMap(m1,m2)[x]</tt> will be equal to
1662 1667
  /// <tt>m1[x]<m2[x]</tt>.
1663 1668
  ///
1664 1669
  /// \relates LessMap
1665 1670
  template<typename M1, typename M2>
1666 1671
  inline LessMap<M1, M2> lessMap(const M1 &m1, const M2 &m2) {
1667 1672
    return LessMap<M1, M2>(m1,m2);
1668 1673
  }
1669 1674

	
1670 1675
  namespace _maps_bits {
1671 1676

	
1672 1677
    template <typename Value>
1673 1678
    struct Identity {
1674 1679
      typedef Value argument_type;
1675 1680
      typedef Value result_type;
1676 1681
      Value operator()(const Value& val) const {
1677 1682
	return val;
1678 1683
      }
1679 1684
    };
1680 1685

	
1681 1686
    template <typename _Iterator, typename Enable = void>
1682 1687
    struct IteratorTraits {
1683 1688
      typedef typename std::iterator_traits<_Iterator>::value_type Value;
1684 1689
    };
1685 1690

	
1686 1691
    template <typename _Iterator>
1687 1692
    struct IteratorTraits<_Iterator,
1688 1693
      typename exists<typename _Iterator::container_type>::type>
1689 1694
    {
1690 1695
      typedef typename _Iterator::container_type::value_type Value;
1691 1696
    };
1692 1697

	
1693 1698
  }
1694 1699

	
1695 1700
  /// \brief Writable bool map for logging each \c true assigned element
1696 1701
  ///
1697 1702
  /// A \ref concepts::ReadWriteMap "read-write" bool map for logging
1698 1703
  /// each \c true assigned element, i.e it copies subsequently each
1699 1704
  /// keys set to \c true to the given iterator.
1700 1705
  ///
1701 1706
  /// \tparam It the type of the Iterator.
1702 1707
  /// \tparam Ke the type of the map's Key. The default value should
1703 1708
  /// work in most cases.
1704 1709
  ///
1705 1710
  /// \note The container of the iterator must contain enough space
1706 1711
  /// for the elements. (Or it should be an inserter iterator).
1707 1712
  ///
1708 1713
  /// \todo Revise the name of this class and give an example code.
1709 1714
  template <typename It,
1710 1715
	    typename Ke=typename _maps_bits::IteratorTraits<It>::Value>
1711 1716
  class StoreBoolMap {
1712 1717
  public:
1713 1718
    typedef It Iterator;
1714 1719

	
1715 1720
    typedef Ke Key;
1716 1721
    typedef bool Value;
1717 1722

	
1718 1723
    /// Constructor
1719 1724
    StoreBoolMap(Iterator it)
1720 1725
      : _begin(it), _end(it) {}
1721 1726

	
1722 1727
    /// Gives back the given iterator set for the first key
1723 1728
    Iterator begin() const {
1724 1729
      return _begin;
1725 1730
    }
1726 1731

	
1727 1732
    /// Gives back the the 'after the last' iterator
1728 1733
    Iterator end() const {
1729 1734
      return _end;
1730 1735
    }
1731 1736

	
1732 1737
    /// The set function of the map
1733 1738
    void set(const Key& key, Value value) const {
1734 1739
      if (value) {
1735 1740
	*_end++ = key;
1736 1741
      }
1737 1742
    }
1738 1743

	
1739 1744
  private:
1740 1745
    Iterator _begin;
1741 1746
    mutable Iterator _end;
1742 1747
  };
1743 1748

	
1744 1749
  /// @}
1745 1750
}
1746 1751

	
1747 1752
#endif // LEMON_MAPS_H
Ignore white space 3072 line context
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-2008
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
#include <deque>
20 20
#include <set>
21 21

	
22 22
#include <lemon/concept_check.h>
23 23
#include <lemon/concepts/maps.h>
24 24
#include <lemon/maps.h>
25 25

	
26 26
#include "test_tools.h"
27 27

	
28 28
using namespace lemon;
29 29
using namespace lemon::concepts;
30 30

	
31 31
struct A {};
32 32
inline bool operator<(A, A) { return true; }
33 33
struct B {};
34 34

	
35 35
class C {
36 36
  int x;
37 37
public:
38 38
  C(int _x) : x(_x) {}
39 39
};
40 40

	
41 41
class F {
42 42
public:
43 43
  typedef A argument_type;
44 44
  typedef B result_type;
45 45

	
46 46
  B operator()(const A&) const { return B(); }
47 47
private:
48 48
  F& operator=(const F&);
49 49
};
50 50

	
51 51
int func(A) { return 3; }
52 52

	
53 53
int binc(int a, B) { return a+1; }
54 54

	
55 55
typedef ReadMap<A, double> DoubleMap;
56 56
typedef ReadWriteMap<A, double> DoubleWriteMap;
57 57
typedef ReferenceMap<A, double, double&, const double&> DoubleRefMap;
58 58

	
59 59
typedef ReadMap<A, bool> BoolMap;
60 60
typedef ReadWriteMap<A, bool> BoolWriteMap;
61 61
typedef ReferenceMap<A, bool, bool&, const bool&> BoolRefMap;
62 62

	
63 63
int main()
64 64
{
65 65
  // Map concepts
66 66
  checkConcept<ReadMap<A,B>, ReadMap<A,B> >();
67 67
  checkConcept<ReadMap<A,C>, ReadMap<A,C> >();
68 68
  checkConcept<WriteMap<A,B>, WriteMap<A,B> >();
69 69
  checkConcept<WriteMap<A,C>, WriteMap<A,C> >();
70 70
  checkConcept<ReadWriteMap<A,B>, ReadWriteMap<A,B> >();
71 71
  checkConcept<ReadWriteMap<A,C>, ReadWriteMap<A,C> >();
72 72
  checkConcept<ReferenceMap<A,B,B&,const B&>, ReferenceMap<A,B,B&,const B&> >();
73 73
  checkConcept<ReferenceMap<A,C,C&,const C&>, ReferenceMap<A,C,C&,const C&> >();
74 74

	
75 75
  // NullMap
76 76
  {
77 77
    checkConcept<ReadWriteMap<A,B>, NullMap<A,B> >();
78 78
    NullMap<A,B> map1;
79 79
    NullMap<A,B> map2 = map1;
80 80
    map1 = nullMap<A,B>();
81 81
  }
82 82

	
83 83
  // ConstMap
84 84
  {
85 85
    checkConcept<ReadWriteMap<A,B>, ConstMap<A,B> >();
86
    checkConcept<ReadWriteMap<A,C>, ConstMap<A,C> >();
86 87
    ConstMap<A,B> map1;
87 88
    ConstMap<A,B> map2(B());
88 89
    ConstMap<A,B> map3 = map1;
89 90
    map1 = constMap<A>(B());
91
    map1 = constMap<A,B>();
90 92
    map1.setAll(B());
93
    ConstMap<A,C> map4(C(1));
94
    ConstMap<A,C> map5 = map4;
95
    map4 = constMap<A>(C(2));
96
    map4.setAll(C(3));
91 97

	
92 98
    checkConcept<ReadWriteMap<A,int>, ConstMap<A,int> >();
93 99
    check(constMap<A>(10)[A()] == 10, "Something is wrong with ConstMap");
94 100

	
95 101
    checkConcept<ReadWriteMap<A,int>, ConstMap<A,Const<int,10> > >();
96
    ConstMap<A,Const<int,10> > map4;
97
    ConstMap<A,Const<int,10> > map5 = map4;
98
    map4 = map5;
99
    check(map4[A()] == 10 && map5[A()] == 10, "Something is wrong with ConstMap");
102
    ConstMap<A,Const<int,10> > map6;
103
    ConstMap<A,Const<int,10> > map7 = map6;
104
    map6 = constMap<A,int,10>();
105
    map7 = constMap<A,Const<int,10> >();
106
    check(map6[A()] == 10 && map7[A()] == 10, "Something is wrong with ConstMap");
100 107
  }
101 108

	
102 109
  // IdentityMap
103 110
  {
104 111
    checkConcept<ReadMap<A,A>, IdentityMap<A> >();
105 112
    IdentityMap<A> map1;
106 113
    IdentityMap<A> map2 = map1;
107 114
    map1 = identityMap<A>();
108 115

	
109 116
    checkConcept<ReadMap<double,double>, IdentityMap<double> >();
110 117
    check(identityMap<double>()[1.0] == 1.0 && identityMap<double>()[3.14] == 3.14,
111 118
          "Something is wrong with IdentityMap");
112 119
  }
113 120

	
114 121
  // RangeMap
115 122
  {
116 123
    checkConcept<ReferenceMap<int,B,B&,const B&>, RangeMap<B> >();
117 124
    RangeMap<B> map1;
118 125
    RangeMap<B> map2(10);
119 126
    RangeMap<B> map3(10,B());
120 127
    RangeMap<B> map4 = map1;
121 128
    RangeMap<B> map5 = rangeMap<B>();
122 129
    RangeMap<B> map6 = rangeMap<B>(10);
123 130
    RangeMap<B> map7 = rangeMap(10,B());
124 131

	
125 132
    checkConcept< ReferenceMap<int, double, double&, const double&>,
126 133
                  RangeMap<double> >();
127 134
    std::vector<double> v(10, 0);
128 135
    v[5] = 100;
129 136
    RangeMap<double> map8(v);
130 137
    RangeMap<double> map9 = rangeMap(v);
131 138
    check(map9.size() == 10 && map9[2] == 0 && map9[5] == 100,
132 139
          "Something is wrong with RangeMap");
133 140
  }
134 141

	
135 142
  // SparseMap
136 143
  {
137 144
    checkConcept<ReferenceMap<A,B,B&,const B&>, SparseMap<A,B> >();
138 145
    SparseMap<A,B> map1;
139 146
    SparseMap<A,B> map2(B());
140 147
    SparseMap<A,B> map3 = sparseMap<A,B>();
141 148
    SparseMap<A,B> map4 = sparseMap<A>(B());
142 149

	
143 150
    checkConcept< ReferenceMap<double, int, int&, const int&>,
144 151
                  SparseMap<double, int> >();
145 152
    std::map<double, int> m;
146 153
    SparseMap<double, int> map5(m);
147 154
    SparseMap<double, int> map6(m,10);
148 155
    SparseMap<double, int> map7 = sparseMap(m);
149 156
    SparseMap<double, int> map8 = sparseMap(m,10);
150 157

	
151 158
    check(map5[1.0] == 0 && map5[3.14] == 0 && map6[1.0] == 10 && map6[3.14] == 10,
152 159
          "Something is wrong with SparseMap");
153 160
    map5[1.0] = map6[3.14] = 100;
154 161
    check(map5[1.0] == 100 && map5[3.14] == 0 && map6[1.0] == 10 && map6[3.14] == 100,
155 162
          "Something is wrong with SparseMap");
156 163
  }
157 164

	
158 165
  // ComposeMap
159 166
  {
160 167
    typedef ComposeMap<DoubleMap, ReadMap<B,A> > CompMap;
161 168
    checkConcept<ReadMap<B,double>, CompMap>();
162 169
    CompMap map1(DoubleMap(),ReadMap<B,A>());
163 170
    CompMap map2 = composeMap(DoubleMap(), ReadMap<B,A>());
164 171

	
165 172
    SparseMap<double, bool> m1(false); m1[3.14] = true;
166 173
    RangeMap<double> m2(2); m2[0] = 3.0; m2[1] = 3.14;
167 174
    check(!composeMap(m1,m2)[0] && composeMap(m1,m2)[1], "Something is wrong with ComposeMap")
168 175
  }
169 176

	
170 177
  // CombineMap
171 178
  {
172 179
    typedef CombineMap<DoubleMap, DoubleMap, std::plus<double> > CombMap;
173 180
    checkConcept<ReadMap<A,double>, CombMap>();
174 181
    CombMap map1(DoubleMap(), DoubleMap());
175 182
    CombMap map2 = combineMap(DoubleMap(), DoubleMap(), std::plus<double>());
176 183

	
177 184
    check(combineMap(constMap<B,int,2>(), identityMap<B>(), &binc)[B()] == 3,
178 185
          "Something is wrong with CombineMap");
179 186
  }
180 187

	
181 188
  // FunctorToMap, MapToFunctor
182 189
  {
183 190
    checkConcept<ReadMap<A,B>, FunctorToMap<F,A,B> >();
184 191
    checkConcept<ReadMap<A,B>, FunctorToMap<F> >();
185 192
    FunctorToMap<F> map1;
186 193
    FunctorToMap<F> map2(F());
187 194
    B b = functorToMap(F())[A()];
188 195

	
189 196
    checkConcept<ReadMap<A,B>, MapToFunctor<ReadMap<A,B> > >();
190 197
    MapToFunctor<ReadMap<A,B> > map(ReadMap<A,B>());
191 198

	
192 199
    check(functorToMap(&func)[A()] == 3, "Something is wrong with FunctorToMap");
193 200
    check(mapToFunctor(constMap<A,int>(2))(A()) == 2, "Something is wrong with MapToFunctor");
194 201
    check(mapToFunctor(functorToMap(&func))(A()) == 3 && mapToFunctor(functorToMap(&func))[A()] == 3,
195 202
          "Something is wrong with FunctorToMap or MapToFunctor");
196 203
    check(functorToMap(mapToFunctor(constMap<A,int>(2)))[A()] == 2,
197 204
          "Something is wrong with FunctorToMap or MapToFunctor");
198 205
  }
199 206

	
200 207
  // ConvertMap
201 208
  {
202 209
    checkConcept<ReadMap<double,double>, ConvertMap<ReadMap<double, int>, double> >();
203 210
    ConvertMap<RangeMap<bool>, int> map1(rangeMap(1, true));
204 211
    ConvertMap<RangeMap<bool>, int> map2 = convertMap<int>(rangeMap(2, false));
205 212
  }
206 213

	
207 214
  // ForkMap
208 215
  {
209 216
    checkConcept<DoubleWriteMap, ForkMap<DoubleWriteMap, DoubleWriteMap> >();
210 217

	
211 218
    typedef RangeMap<double> RM;
212 219
    typedef SparseMap<int, double> SM;
213 220
    RM m1(10, -1);
214 221
    SM m2(-1);
215 222
    checkConcept<ReadWriteMap<int, double>, ForkMap<RM, SM> >();
216 223
    checkConcept<ReadWriteMap<int, double>, ForkMap<SM, RM> >();
217 224
    ForkMap<RM, SM> map1(m1,m2);
218 225
    ForkMap<SM, RM> map2 = forkMap(m2,m1);
219 226
    map2.set(5, 10);
220 227
    check(m1[1] == -1 && m1[5] == 10 && m2[1] == -1 && m2[5] == 10 && map2[1] == -1 && map2[5] == 10,
221 228
          "Something is wrong with ForkMap");
222 229
  }
223 230

	
224 231
  // Arithmetic maps:
225 232
  // - AddMap, SubMap, MulMap, DivMap
226 233
  // - ShiftMap, ShiftWriteMap, ScaleMap, ScaleWriteMap
227 234
  // - NegMap, NegWriteMap, AbsMap
228 235
  {
229 236
    checkConcept<DoubleMap, AddMap<DoubleMap,DoubleMap> >();
230 237
    checkConcept<DoubleMap, SubMap<DoubleMap,DoubleMap> >();
231 238
    checkConcept<DoubleMap, MulMap<DoubleMap,DoubleMap> >();
232 239
    checkConcept<DoubleMap, DivMap<DoubleMap,DoubleMap> >();
233 240

	
234 241
    ConstMap<int, double> c1(1.0), c2(3.14);
235 242
    IdentityMap<int> im;
236 243
    ConvertMap<IdentityMap<int>, double> id(im);
237 244
    check(addMap(c1,id)[0] == 1.0  && addMap(c1,id)[10] == 11.0, "Something is wrong with AddMap");
238 245
    check(subMap(id,c1)[0] == -1.0 && subMap(id,c1)[10] == 9.0,  "Something is wrong with SubMap");
239 246
    check(mulMap(id,c2)[0] == 0    && mulMap(id,c2)[2]  == 6.28, "Something is wrong with MulMap");
240 247
    check(divMap(c2,id)[1] == 3.14 && divMap(c2,id)[2]  == 1.57, "Something is wrong with DivMap");
241 248

	
242 249
    checkConcept<DoubleMap, ShiftMap<DoubleMap> >();
243 250
    checkConcept<DoubleWriteMap, ShiftWriteMap<DoubleWriteMap> >();
244 251
    checkConcept<DoubleMap, ScaleMap<DoubleMap> >();
245 252
    checkConcept<DoubleWriteMap, ScaleWriteMap<DoubleWriteMap> >();
246 253
    checkConcept<DoubleMap, NegMap<DoubleMap> >();
247 254
    checkConcept<DoubleWriteMap, NegWriteMap<DoubleWriteMap> >();
248 255
    checkConcept<DoubleMap, AbsMap<DoubleMap> >();
249 256

	
250 257
    check(shiftMap(id, 2.0)[1] == 3.0 && shiftMap(id, 2.0)[10] == 12.0,
251 258
          "Something is wrong with ShiftMap");
252 259
    check(shiftWriteMap(id, 2.0)[1] == 3.0 && shiftWriteMap(id, 2.0)[10] == 12.0,
253 260
          "Something is wrong with ShiftWriteMap");
254 261
    check(scaleMap(id, 2.0)[1] == 2.0 && scaleMap(id, 2.0)[10] == 20.0,
255 262
          "Something is wrong with ScaleMap");
256 263
    check(scaleWriteMap(id, 2.0)[1] == 2.0 && scaleWriteMap(id, 2.0)[10] == 20.0,
257 264
          "Something is wrong with ScaleWriteMap");
258 265
    check(negMap(id)[1] == -1.0 && negMap(id)[-10] == 10.0,
259 266
          "Something is wrong with NegMap");
260 267
    check(negWriteMap(id)[1] == -1.0 && negWriteMap(id)[-10] == 10.0,
261 268
          "Something is wrong with NegWriteMap");
262 269
    check(absMap(id)[1] == 1.0 && absMap(id)[-10] == 10.0,
263 270
          "Something is wrong with AbsMap");
264 271
  }
265 272

	
266 273
  // Logical maps:
267 274
  // - TrueMap, FalseMap
268 275
  // - AndMap, OrMap
269 276
  // - NotMap, NotWriteMap
270 277
  // - EqualMap, LessMap
271 278
  {
272 279
    checkConcept<BoolMap, TrueMap<A> >();
273 280
    checkConcept<BoolMap, FalseMap<A> >();
274 281
    checkConcept<BoolMap, AndMap<BoolMap,BoolMap> >();
275 282
    checkConcept<BoolMap, OrMap<BoolMap,BoolMap> >();
276 283
    checkConcept<BoolMap, NotMap<BoolMap> >();
277 284
    checkConcept<BoolWriteMap, NotWriteMap<BoolWriteMap> >();
278 285
    checkConcept<BoolMap, EqualMap<DoubleMap,DoubleMap> >();
279 286
    checkConcept<BoolMap, LessMap<DoubleMap,DoubleMap> >();
280 287

	
281 288
    TrueMap<int> tm;
282 289
    FalseMap<int> fm;
283 290
    RangeMap<bool> rm(2);
284 291
    rm[0] = true; rm[1] = false;
285 292
    check(andMap(tm,rm)[0] && !andMap(tm,rm)[1] && !andMap(fm,rm)[0] && !andMap(fm,rm)[1],
286 293
          "Something is wrong with AndMap");
287 294
    check(orMap(tm,rm)[0] && orMap(tm,rm)[1] && orMap(fm,rm)[0] && !orMap(fm,rm)[1],
288 295
          "Something is wrong with OrMap");
289 296
    check(!notMap(rm)[0] && notMap(rm)[1], "Something is wrong with NotMap");
290 297
    check(!notWriteMap(rm)[0] && notWriteMap(rm)[1], "Something is wrong with NotWriteMap");
291 298

	
292 299
    ConstMap<int, double> cm(2.0);
293 300
    IdentityMap<int> im;
294 301
    ConvertMap<IdentityMap<int>, double> id(im);
295 302
    check(lessMap(id,cm)[1] && !lessMap(id,cm)[2] && !lessMap(id,cm)[3],
296 303
          "Something is wrong with LessMap");
297 304
    check(!equalMap(id,cm)[1] && equalMap(id,cm)[2] && !equalMap(id,cm)[3],
298 305
          "Something is wrong with EqualMap");
299 306
  }
300 307

	
301 308
  return 0;
302 309
}
0 comments (0 inline)