Location: LEMON/LEMON-official/lemon/maps.h

Load file history
gravatar
kpeter (Peter Kovacs)
Added missing inheritances and map-creator functions.
   1
   2
   3
   4
   5
   6
   7
   8
   9
  10
  11
  12
  13
  14
  15
  16
  17
  18
  19
  20
  21
  22
  23
  24
  25
  26
  27
  28
  29
  30
  31
  32
  33
  34
  35
  36
  37
  38
  39
  40
  41
  42
  43
  44
  45
  46
  47
  48
  49
  50
  51
  52
  53
  54
  55
  56
  57
  58
  59
  60
  61
  62
  63
  64
  65
  66
  67
  68
  69
  70
  71
  72
  73
  74
  75
  76
  77
  78
  79
  80
  81
  82
  83
  84
  85
  86
  87
  88
  89
  90
  91
  92
  93
  94
  95
  96
  97
  98
  99
 100
 101
 102
 103
 104
 105
 106
 107
 108
 109
 110
 111
 112
 113
 114
 115
 116
 117
 118
 119
 120
 121
 122
 123
 124
 125
 126
 127
 128
 129
 130
 131
 132
 133
 134
 135
 136
 137
 138
 139
 140
 141
 142
 143
 144
 145
 146
 147
 148
 149
 150
 151
 152
 153
 154
 155
 156
 157
 158
 159
 160
 161
 162
 163
 164
 165
 166
 167
 168
 169
 170
 171
 172
 173
 174
 175
 176
 177
 178
 179
 180
 181
 182
 183
 184
 185
 186
 187
 188
 189
 190
 191
 192
 193
 194
 195
 196
 197
 198
 199
 200
 201
 202
 203
 204
 205
 206
 207
 208
 209
 210
 211
 212
 213
 214
 215
 216
 217
 218
 219
 220
 221
 222
 223
 224
 225
 226
 227
 228
 229
 230
 231
 232
 233
 234
 235
 236
 237
 238
 239
 240
 241
 242
 243
 244
 245
 246
 247
 248
 249
 250
 251
 252
 253
 254
 255
 256
 257
 258
 259
 260
 261
 262
 263
 264
 265
 266
 267
 268
 269
 270
 271
 272
 273
 274
 275
 276
 277
 278
 279
 280
 281
 282
 283
 284
 285
 286
 287
 288
 289
 290
 291
 292
 293
 294
 295
 296
 297
 298
 299
 300
 301
 302
 303
 304
 305
 306
 307
 308
 309
 310
 311
 312
 313
 314
 315
 316
 317
 318
 319
 320
 321
 322
 323
 324
 325
 326
 327
 328
 329
 330
 331
 332
 333
 334
 335
 336
 337
 338
 339
 340
 341
 342
 343
 344
 345
 346
 347
 348
 349
 350
 351
 352
 353
 354
 355
 356
 357
 358
 359
 360
 361
 362
 363
 364
 365
 366
 367
 368
 369
 370
 371
 372
 373
 374
 375
 376
 377
 378
 379
 380
 381
 382
 383
 384
 385
 386
 387
 388
 389
 390
 391
 392
 393
 394
 395
 396
 397
 398
 399
 400
 401
 402
 403
 404
 405
 406
 407
 408
 409
 410
 411
 412
 413
 414
 415
 416
 417
 418
 419
 420
 421
 422
 423
 424
 425
 426
 427
 428
 429
 430
 431
 432
 433
 434
 435
 436
 437
 438
 439
 440
 441
 442
 443
 444
 445
 446
 447
 448
 449
 450
 451
 452
 453
 454
 455
 456
 457
 458
 459
 460
 461
 462
 463
 464
 465
 466
 467
 468
 469
 470
 471
 472
 473
 474
 475
 476
 477
 478
 479
 480
 481
 482
 483
 484
 485
 486
 487
 488
 489
 490
 491
 492
 493
 494
 495
 496
 497
 498
 499
 500
 501
 502
 503
 504
 505
 506
 507
 508
 509
 510
 511
 512
 513
 514
 515
 516
 517
 518
 519
 520
 521
 522
 523
 524
 525
 526
 527
 528
 529
 530
 531
 532
 533
 534
 535
 536
 537
 538
 539
 540
 541
 542
 543
 544
 545
 546
 547
 548
 549
 550
 551
 552
 553
 554
 555
 556
 557
 558
 559
 560
 561
 562
 563
 564
 565
 566
 567
 568
 569
 570
 571
 572
 573
 574
 575
 576
 577
 578
 579
 580
 581
 582
 583
 584
 585
 586
 587
 588
 589
 590
 591
 592
 593
 594
 595
 596
 597
 598
 599
 600
 601
 602
 603
 604
 605
 606
 607
 608
 609
 610
 611
 612
 613
 614
 615
 616
 617
 618
 619
 620
 621
 622
 623
 624
 625
 626
 627
 628
 629
 630
 631
 632
 633
 634
 635
 636
 637
 638
 639
 640
 641
 642
 643
 644
 645
 646
 647
 648
 649
 650
 651
 652
 653
 654
 655
 656
 657
 658
 659
 660
 661
 662
 663
 664
 665
 666
 667
 668
 669
 670
 671
 672
 673
 674
 675
 676
 677
 678
 679
 680
 681
 682
 683
 684
 685
 686
 687
 688
 689
 690
 691
 692
 693
 694
 695
 696
 697
 698
 699
 700
 701
 702
 703
 704
 705
 706
 707
 708
 709
 710
 711
 712
 713
 714
 715
 716
 717
 718
 719
 720
 721
 722
 723
 724
 725
 726
 727
 728
 729
 730
 731
 732
 733
 734
 735
 736
 737
 738
 739
 740
 741
 742
 743
 744
 745
 746
 747
 748
 749
 750
 751
 752
 753
 754
 755
 756
 757
 758
 759
 760
 761
 762
 763
 764
 765
 766
 767
 768
 769
 770
 771
 772
 773
 774
 775
 776
 777
 778
 779
 780
 781
 782
 783
 784
 785
 786
 787
 788
 789
 790
 791
 792
 793
 794
 795
 796
 797
 798
 799
 800
 801
 802
 803
 804
 805
 806
 807
 808
 809
 810
 811
 812
 813
 814
 815
 816
 817
 818
 819
 820
 821
 822
 823
 824
 825
 826
 827
 828
 829
 830
 831
 832
 833
 834
 835
 836
 837
 838
 839
 840
 841
 842
 843
 844
 845
 846
 847
 848
 849
 850
 851
 852
 853
 854
 855
 856
 857
 858
 859
 860
 861
 862
 863
 864
 865
 866
 867
 868
 869
 870
 871
 872
 873
 874
 875
 876
 877
 878
 879
 880
 881
 882
 883
 884
 885
 886
 887
 888
 889
 890
 891
 892
 893
 894
 895
 896
 897
 898
 899
 900
 901
 902
 903
 904
 905
 906
 907
 908
 909
 910
 911
 912
 913
 914
 915
 916
 917
 918
 919
 920
 921
 922
 923
 924
 925
 926
 927
 928
 929
 930
 931
 932
 933
 934
 935
 936
 937
 938
 939
 940
 941
 942
 943
 944
 945
 946
 947
 948
 949
 950
 951
 952
 953
 954
 955
 956
 957
 958
 959
 960
 961
 962
 963
 964
 965
 966
 967
 968
 969
 970
 971
 972
 973
 974
 975
 976
 977
 978
 979
 980
 981
 982
 983
 984
 985
 986
 987
 988
 989
 990
 991
 992
 993
 994
 995
 996
 997
 998
 999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
/* -*- C++ -*-
*
* This file is a part of LEMON, a generic C++ optimization library
*
* Copyright (C) 2003-2007
* Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
* (Egervary Research Group on Combinatorial Optimization, EGRES).
*
* Permission to use, modify and distribute this software is granted
* provided that this copyright notice appears in all copies. For
* precise terms see the accompanying LICENSE file.
*
* This software is provided "AS IS" with no warranty of any kind,
* express or implied, and with no claim as to its suitability for any
* purpose.
*
*/
#ifndef LEMON_MAPS_H
#define LEMON_MAPS_H
#include <iterator>
#include <functional>
#include <vector>
#include <lemon/bits/utility.h>
// #include <lemon/bits/traits.h>
///\file
///\ingroup maps
///\brief Miscellaneous property maps
///
#include <map>
namespace lemon {
/// \addtogroup maps
/// @{
/// Base class of maps.
/// Base class of maps.
/// It provides the necessary <tt>typedef</tt>s required by the map concept.
template<typename K, typename T>
class MapBase {
public:
/// The key type of the map.
typedef K Key;
/// The value type of the map. (The type of objects associated with the keys).
typedef T Value;
};
/// Null map. (a.k.a. DoNothingMap)
/// This map can be used if you have to provide a map only for
/// its type definitions, or if you have to provide a writable map,
/// but data written to it is not required (i.e. it will be sent to
/// <tt>/dev/null</tt>).
template<typename K, typename T>
class NullMap : public MapBase<K, T> {
public:
typedef MapBase<K, T> Parent;
typedef typename Parent::Key Key;
typedef typename Parent::Value Value;
/// Gives back a default constructed element.
T operator[](const K&) const { return T(); }
/// Absorbs the value.
void set(const K&, const T&) {}
};
///Returns a \c NullMap class
///This function just returns a \c NullMap class.
///\relates NullMap
template <typename K, typename V>
NullMap<K, V> nullMap() {
return NullMap<K, V>();
}
/// Constant map.
/// This is a readable map which assigns a specified value to each key.
/// In other aspects it is equivalent to the \c NullMap.
template<typename K, typename T>
class ConstMap : public MapBase<K, T> {
private:
T v;
public:
typedef MapBase<K, T> Parent;
typedef typename Parent::Key Key;
typedef typename Parent::Value Value;
/// Default constructor
/// Default constructor.
/// The value of the map will be uninitialized.
/// (More exactly it will be default constructed.)
ConstMap() {}
/// Constructor with specified initial value
/// Constructor with specified initial value.
/// \param _v is the initial value of the map.
ConstMap(const T &_v) : v(_v) {}
///\e
T operator[](const K&) const { return v; }
///\e
void setAll(const T &t) {
v = t;
}
template<typename T1>
ConstMap(const ConstMap<K, T1> &, const T &_v) : v(_v) {}
};
///Returns a \c ConstMap class
///This function just returns a \c ConstMap class.
///\relates ConstMap
template<typename K, typename V>
inline ConstMap<K, V> constMap(const V &v) {
return ConstMap<K, V>(v);
}
template<typename T, T v>
struct Const { };
/// Constant map with inlined constant value.
/// This is a readable map which assigns a specified value to each key.
/// In other aspects it is equivalent to the \c NullMap.
template<typename K, typename V, V v>
class ConstMap<K, Const<V, v> > : public MapBase<K, V> {
public:
typedef MapBase<K, V> Parent;
typedef typename Parent::Key Key;
typedef typename Parent::Value Value;
ConstMap() { }
///\e
V operator[](const K&) const { return v; }
///\e
void set(const K&, const V&) { }
};
///Returns a \c ConstMap class
///This function just returns a \c ConstMap class with inlined value.
///\relates ConstMap
template<typename K, typename V, V v>
inline ConstMap<K, Const<V, v> > constMap() {
return ConstMap<K, Const<V, v> >();
}
///Map based on std::map
///This is essentially a wrapper for \c std::map with addition that
///you can specify a default value different from \c Value().
template <typename K, typename T, typename Compare = std::less<K> >
class StdMap : public MapBase<K, T> {
template <typename K1, typename T1, typename C1>
friend class StdMap;
public:
typedef MapBase<K, T> Parent;
///\e
typedef typename Parent::Key Key;
///\e
typedef typename Parent::Value Value;
///\e
typedef T& Reference;
///\e
typedef const T& ConstReference;
typedef True ReferenceMapTag;
private:
typedef std::map<K, T, Compare> Map;
Value _value;
Map _map;
public:
/// Constructor with specified default value
StdMap(const T& value = T()) : _value(value) {}
/// \brief Constructs the map from an appropriate std::map, and explicitly
/// specifies a default value.
template <typename T1, typename Comp1>
StdMap(const std::map<Key, T1, Comp1> &map, const T& value = T())
: _map(map.begin(), map.end()), _value(value) {}
/// \brief Constructs a map from an other StdMap.
template<typename T1, typename Comp1>
StdMap(const StdMap<Key, T1, Comp1> &c)
: _map(c._map.begin(), c._map.end()), _value(c._value) {}
private:
StdMap& operator=(const StdMap&);
public:
///\e
Reference operator[](const Key &k) {
typename Map::iterator it = _map.lower_bound(k);
if (it != _map.end() && !_map.key_comp()(k, it->first))
return it->second;
else
return _map.insert(it, std::make_pair(k, _value))->second;
}
/// \e
ConstReference operator[](const Key &k) const {
typename Map::const_iterator it = _map.find(k);
if (it != _map.end())
return it->second;
else
return _value;
}
/// \e
void set(const Key &k, const T &t) {
typename Map::iterator it = _map.lower_bound(k);
if (it != _map.end() && !_map.key_comp()(k, it->first))
it->second = t;
else
_map.insert(it, std::make_pair(k, t));
}
/// \e
void setAll(const T &t) {
_value = t;
_map.clear();
}
};
///Returns a \ref StdMap class
///This function just returns a \ref StdMap class with specified
///default value.
///\relates StdMap
template<typename K, typename V, typename Compare = std::less<K> >
inline StdMap<K, V, Compare> stdMap(const V& value = V()) {
return StdMap<K, V, Compare>(value);
}
///Returns a \ref StdMap class created from an appropriate std::map
///This function just returns a \ref StdMap class created from an
///appropriate std::map.
///\relates StdMap
template<typename K, typename V, typename Compare = std::less<K> >
inline StdMap<K, V, Compare> stdMap( const std::map<K, V, Compare> &map,
const V& value = V() ) {
return StdMap<K, V, Compare>(map, value);
}
/// \brief Map for storing values for keys from the range <tt>[0..size-1]</tt>
///
/// The current map has the <tt>[0..size-1]</tt> keyset and the values
/// are stored in a \c std::vector<T> container. It can be used with
/// some data structures, for example \c UnionFind, \c BinHeap, when
/// the used items are small integer numbers.
///
/// \todo Revise its name
template <typename T>
class IntegerMap : public MapBase<int, T> {
template <typename T1>
friend class IntegerMap;
public:
typedef MapBase<int, T> Parent;
///\e
typedef typename Parent::Key Key;
///\e
typedef typename Parent::Value Value;
///\e
typedef T& Reference;
///\e
typedef const T& ConstReference;
typedef True ReferenceMapTag;
private:
typedef std::vector<T> Vector;
Vector _vector;
public:
/// Constructor with specified default value
IntegerMap(int size = 0, const T& value = T()) : _vector(size, value) {}
/// \brief Constructs the map from an appropriate std::vector.
template <typename T1>
IntegerMap(const std::vector<T1>& vector)
: _vector(vector.begin(), vector.end()) {}
/// \brief Constructs a map from an other IntegerMap.
template <typename T1>
IntegerMap(const IntegerMap<T1> &c)
: _vector(c._vector.begin(), c._vector.end()) {}
/// \brief Resize the container
void resize(int size, const T& value = T()) {
_vector.resize(size, value);
}
private:
IntegerMap& operator=(const IntegerMap&);
public:
///\e
Reference operator[](Key k) {
return _vector[k];
}
/// \e
ConstReference operator[](Key k) const {
return _vector[k];
}
/// \e
void set(const Key &k, const T& t) {
_vector[k] = t;
}
};
///Returns an \ref IntegerMap class
///This function just returns an \ref IntegerMap class.
///\relates IntegerMap
template<typename T>
inline IntegerMap<T> integerMap(int size = 0, const T& value = T()) {
return IntegerMap<T>(size, value);
}
/// @}
/// \addtogroup map_adaptors
/// @{
/// \brief Identity map.
///
/// This map gives back the given key as value without any
/// modification.
template <typename T>
class IdentityMap : public MapBase<T, T> {
public:
typedef MapBase<T, T> Parent;
typedef typename Parent::Key Key;
typedef typename Parent::Value Value;
/// \e
const T& operator[](const T& t) const {
return t;
}
};
///Returns an \c IdentityMap class
///This function just returns an \c IdentityMap class.
///\relates IdentityMap
template<typename T>
inline IdentityMap<T> identityMap() {
return IdentityMap<T>();
}
///\brief Convert the \c Value of a map to another type using
///the default conversion.
///
///This \c concepts::ReadMap "read only map"
///converts the \c Value of a map to type \c T.
///Its \c Key is inherited from \c M.
template <typename M, typename T>
class ConvertMap : public MapBase<typename M::Key, T> {
const M& m;
public:
typedef MapBase<typename M::Key, T> Parent;
typedef typename Parent::Key Key;
typedef typename Parent::Value Value;
///Constructor
///Constructor.
///\param _m is the underlying map.
ConvertMap(const M &_m) : m(_m) {};
/// \brief The subscript operator.
///
/// The subscript operator.
Value operator[](const Key& k) const {return m[k];}
};
///Returns a \c ConvertMap class
///This function just returns a \c ConvertMap class.
///\relates ConvertMap
template<typename T, typename M>
inline ConvertMap<M, T> convertMap(const M &m) {
return ConvertMap<M, T>(m);
}
///Simple wrapping of a map
///This \ref concepts::ReadMap "read only map" returns the simple
///wrapping of the given map. Sometimes the reference maps cannot be
///combined with simple read maps. This map adaptor wraps the given
///map to simple read map.
///
///\sa SimpleWriteMap
///
/// \todo Revise the misleading name
template<typename M>
class SimpleMap : public MapBase<typename M::Key, typename M::Value> {
const M& m;
public:
typedef MapBase<typename M::Key, typename M::Value> Parent;
typedef typename Parent::Key Key;
typedef typename Parent::Value Value;
///Constructor
SimpleMap(const M &_m) : m(_m) {};
///\e
Value operator[](Key k) const {return m[k];}
};
///Returns a \ref SimpleMap class
///This function just returns a \ref SimpleMap class.
///\relates SimpleMap
template<typename M>
inline SimpleMap<M> simpleMap(const M &m) {
return SimpleMap<M>(m);
}
///Simple writable wrapping of a map
///This \ref concepts::WriteMap "write map" returns the simple
///wrapping of the given map. Sometimes the reference maps cannot be
///combined with simple read-write maps. This map adaptor wraps the
///given map to simple read-write map.
///
///\sa SimpleMap
///
/// \todo Revise the misleading name
template<typename M>
class SimpleWriteMap : public MapBase<typename M::Key, typename M::Value> {
M& m;
public:
typedef MapBase<typename M::Key, typename M::Value> Parent;
typedef typename Parent::Key Key;
typedef typename Parent::Value Value;
///Constructor
SimpleWriteMap(M &_m) : m(_m) {};
///\e
Value operator[](Key k) const {return m[k];}
///\e
void set(Key k, const Value& c) { m.set(k, c); }
};
///Returns a \ref SimpleWriteMap class
///This function just returns a \ref SimpleWriteMap class.
///\relates SimpleWriteMap
template<typename M>
inline SimpleWriteMap<M> simpleWriteMap(M &m) {
return SimpleWriteMap<M>(m);
}
///Sum of two maps
///This \c concepts::ReadMap "read only map" returns the sum of the two
///given maps.
///Its \c Key and \c Value are inherited from \c M1.
///The \c Key and \c Value of M2 must be convertible to those of \c M1.
template<typename M1, typename M2>
class AddMap : public MapBase<typename M1::Key, typename M1::Value> {
const M1& m1;
const M2& m2;
public:
typedef MapBase<typename M1::Key, typename M1::Value> Parent;
typedef typename Parent::Key Key;
typedef typename Parent::Value Value;
///Constructor
AddMap(const M1 &_m1,const M2 &_m2) : m1(_m1), m2(_m2) {};
///\e
Value operator[](Key k) const {return m1[k]+m2[k];}
};
///Returns an \c AddMap class
///This function just returns an \c AddMap class.
///\todo How to call these type of functions?
///
///\relates AddMap
template<typename M1, typename M2>
inline AddMap<M1, M2> addMap(const M1 &m1,const M2 &m2) {
return AddMap<M1, M2>(m1,m2);
}
///Shift a map with a constant.
///This \c concepts::ReadMap "read only map" returns the sum of the
///given map and a constant value.
///Its \c Key and \c Value are inherited from \c M.
///
///Actually,
///\code
/// ShiftMap<X> sh(x,v);
///\endcode
///is equivalent to
///\code
/// ConstMap<X::Key, X::Value> c_tmp(v);
/// AddMap<X, ConstMap<X::Key, X::Value> > sh(x,v);
///\endcode
///
///\sa ShiftWriteMap
template<typename M, typename C = typename M::Value>
class ShiftMap : public MapBase<typename M::Key, typename M::Value> {
const M& m;
C v;
public:
typedef MapBase<typename M::Key, typename M::Value> Parent;
typedef typename Parent::Key Key;
typedef typename Parent::Value Value;
///Constructor
///Constructor.
///\param _m is the undelying map.
///\param _v is the shift value.
ShiftMap(const M &_m, const C &_v ) : m(_m), v(_v) {};
///\e
Value operator[](Key k) const {return m[k] + v;}
};
///Shift a map with a constant (ReadWrite version).
///This \c concepts::ReadWriteMap "read-write map" returns the sum of the
///given map and a constant value. It makes also possible to write the map.
///Its \c Key and \c Value are inherited from \c M.
///
///\sa ShiftMap
template<typename M, typename C = typename M::Value>
class ShiftWriteMap : public MapBase<typename M::Key, typename M::Value> {
M& m;
C v;
public:
typedef MapBase<typename M::Key, typename M::Value> Parent;
typedef typename Parent::Key Key;
typedef typename Parent::Value Value;
///Constructor
///Constructor.
///\param _m is the undelying map.
///\param _v is the shift value.
ShiftWriteMap(M &_m, const C &_v ) : m(_m), v(_v) {};
/// \e
Value operator[](Key k) const {return m[k] + v;}
/// \e
void set(Key k, const Value& c) { m.set(k, c - v); }
};
///Returns a \c ShiftMap class
///This function just returns a \c ShiftMap class.
///\relates ShiftMap
template<typename M, typename C>
inline ShiftMap<M, C> shiftMap(const M &m,const C &v) {
return ShiftMap<M, C>(m,v);
}
///Returns a \c ShiftWriteMap class
///This function just returns a \c ShiftWriteMap class.
///\relates ShiftWriteMap
template<typename M, typename C>
inline ShiftWriteMap<M, C> shiftMap(M &m,const C &v) {
return ShiftWriteMap<M, C>(m,v);
}
///Difference of two maps
///This \c concepts::ReadMap "read only map" returns the difference
///of the values of the two given maps.
///Its \c Key and \c Value are inherited from \c M1.
///The \c Key and \c Value of \c M2 must be convertible to those of \c M1.
///
/// \todo Revise the misleading name
template<typename M1, typename M2>
class SubMap : public MapBase<typename M1::Key, typename M1::Value> {
const M1& m1;
const M2& m2;
public:
typedef MapBase<typename M1::Key, typename M1::Value> Parent;
typedef typename Parent::Key Key;
typedef typename Parent::Value Value;
///Constructor
SubMap(const M1 &_m1,const M2 &_m2) : m1(_m1), m2(_m2) {};
/// \e
Value operator[](Key k) const {return m1[k]-m2[k];}
};
///Returns a \c SubMap class
///This function just returns a \c SubMap class.
///
///\relates SubMap
template<typename M1, typename M2>
inline SubMap<M1, M2> subMap(const M1 &m1, const M2 &m2) {
return SubMap<M1, M2>(m1, m2);
}
///Product of two maps
///This \c concepts::ReadMap "read only map" returns the product of the
///values of the two given maps.
///Its \c Key and \c Value are inherited from \c M1.
///The \c Key and \c Value of \c M2 must be convertible to those of \c M1.
template<typename M1, typename M2>
class MulMap : public MapBase<typename M1::Key, typename M1::Value> {
const M1& m1;
const M2& m2;
public:
typedef MapBase<typename M1::Key, typename M1::Value> Parent;
typedef typename Parent::Key Key;
typedef typename Parent::Value Value;
///Constructor
MulMap(const M1 &_m1,const M2 &_m2) : m1(_m1), m2(_m2) {};
/// \e
Value operator[](Key k) const {return m1[k]*m2[k];}
};
///Returns a \c MulMap class
///This function just returns a \c MulMap class.
///\relates MulMap
template<typename M1, typename M2>
inline MulMap<M1, M2> mulMap(const M1 &m1,const M2 &m2) {
return MulMap<M1, M2>(m1,m2);
}
///Scales a map with a constant.
///This \c concepts::ReadMap "read only map" returns the value of the
///given map multiplied from the left side with a constant value.
///Its \c Key and \c Value are inherited from \c M.
///
///Actually,
///\code
/// ScaleMap<X> sc(x,v);
///\endcode
///is equivalent to
///\code
/// ConstMap<X::Key, X::Value> c_tmp(v);
/// MulMap<X, ConstMap<X::Key, X::Value> > sc(x,v);
///\endcode
///
///\sa ScaleWriteMap
template<typename M, typename C = typename M::Value>
class ScaleMap : public MapBase<typename M::Key, typename M::Value> {
const M& m;
C v;
public:
typedef MapBase<typename M::Key, typename M::Value> Parent;
typedef typename Parent::Key Key;
typedef typename Parent::Value Value;
///Constructor
///Constructor.
///\param _m is the undelying map.
///\param _v is the scaling value.
ScaleMap(const M &_m, const C &_v ) : m(_m), v(_v) {};
/// \e
Value operator[](Key k) const {return v * m[k];}
};
///Scales a map with a constant (ReadWrite version).
///This \c concepts::ReadWriteMap "read-write map" returns the value of the
///given map multiplied from the left side with a constant value. It can
///also be used as write map if the \c / operator is defined between
///\c Value and \c C and the given multiplier is not zero.
///Its \c Key and \c Value are inherited from \c M.
///
///\sa ScaleMap
template<typename M, typename C = typename M::Value>
class ScaleWriteMap : public MapBase<typename M::Key, typename M::Value> {
M& m;
C v;
public:
typedef MapBase<typename M::Key, typename M::Value> Parent;
typedef typename Parent::Key Key;
typedef typename Parent::Value Value;
///Constructor
///Constructor.
///\param _m is the undelying map.
///\param _v is the scaling value.
ScaleWriteMap(M &_m, const C &_v ) : m(_m), v(_v) {};
/// \e
Value operator[](Key k) const {return v * m[k];}
/// \e
void set(Key k, const Value& c) { m.set(k, c / v);}
};
///Returns a \c ScaleMap class
///This function just returns a \c ScaleMap class.
///\relates ScaleMap
template<typename M, typename C>
inline ScaleMap<M, C> scaleMap(const M &m,const C &v) {
return ScaleMap<M, C>(m,v);
}
///Returns a \c ScaleWriteMap class
///This function just returns a \c ScaleWriteMap class.
///\relates ScaleWriteMap
template<typename M, typename C>
inline ScaleWriteMap<M, C> scaleMap(M &m,const C &v) {
return ScaleWriteMap<M, C>(m,v);
}
///Quotient of two maps
///This \c concepts::ReadMap "read only map" returns the quotient of the
///values of the two given maps.
///Its \c Key and \c Value are inherited from \c M1.
///The \c Key and \c Value of \c M2 must be convertible to those of \c M1.
template<typename M1, typename M2>
class DivMap : public MapBase<typename M1::Key, typename M1::Value> {
const M1& m1;
const M2& m2;
public:
typedef MapBase<typename M1::Key, typename M1::Value> Parent;
typedef typename Parent::Key Key;
typedef typename Parent::Value Value;
///Constructor
DivMap(const M1 &_m1,const M2 &_m2) : m1(_m1), m2(_m2) {};
/// \e
Value operator[](Key k) const {return m1[k]/m2[k];}
};
///Returns a \c DivMap class
///This function just returns a \c DivMap class.
///\relates DivMap
template<typename M1, typename M2>
inline DivMap<M1, M2> divMap(const M1 &m1,const M2 &m2) {
return DivMap<M1, M2>(m1,m2);
}
///Composition of two maps
///This \c concepts::ReadMap "read only map" returns the composition of
///two given maps.
///That is to say, if \c m1 is of type \c M1 and \c m2 is of \c M2,
///then for
///\code
/// ComposeMap<M1, M2> cm(m1,m2);
///\endcode
/// <tt>cm[x]</tt> will be equal to <tt>m1[m2[x]]</tt>.
///
///Its \c Key is inherited from \c M2 and its \c Value is from \c M1.
///\c M2::Value must be convertible to \c M1::Key.
///
///\sa CombineMap
///
///\todo Check the requirements.
template <typename M1, typename M2>
class ComposeMap : public MapBase<typename M2::Key, typename M1::Value> {
const M1& m1;
const M2& m2;
public:
typedef MapBase<typename M2::Key, typename M1::Value> Parent;
typedef typename Parent::Key Key;
typedef typename Parent::Value Value;
///Constructor
ComposeMap(const M1 &_m1,const M2 &_m2) : m1(_m1), m2(_m2) {};
/// \e
/// \todo Use the MapTraits once it is ported.
///
//typename MapTraits<M1>::ConstReturnValue
typename M1::Value
operator[](Key k) const {return m1[m2[k]];}
};
///Returns a \c ComposeMap class
///This function just returns a \c ComposeMap class.
///\relates ComposeMap
template <typename M1, typename M2>
inline ComposeMap<M1, M2> composeMap(const M1 &m1,const M2 &m2) {
return ComposeMap<M1, M2>(m1,m2);
}
///Combine of two maps using an STL (binary) functor.
///Combine of two maps using an STL (binary) functor.
///
///This \c concepts::ReadMap "read only map" takes two maps and a
///binary functor and returns the composition of the two
///given maps unsing the functor.
///That is to say, if \c m1 and \c m2 is of type \c M1 and \c M2
///and \c f is of \c F, then for
///\code
/// CombineMap<M1,M2,F,V> cm(m1,m2,f);
///\endcode
/// <tt>cm[x]</tt> will be equal to <tt>f(m1[x],m2[x])</tt>
///
///Its \c Key is inherited from \c M1 and its \c Value is \c V.
///\c M2::Value and \c M1::Value must be convertible to the corresponding
///input parameter of \c F and the return type of \c F must be convertible
///to \c V.
///
///\sa ComposeMap
///
///\todo Check the requirements.
template<typename M1, typename M2, typename F,
typename V = typename F::result_type>
class CombineMap : public MapBase<typename M1::Key, V> {
const M1& m1;
const M2& m2;
F f;
public:
typedef MapBase<typename M1::Key, V> Parent;
typedef typename Parent::Key Key;
typedef typename Parent::Value Value;
///Constructor
CombineMap(const M1 &_m1,const M2 &_m2,const F &_f = F())
: m1(_m1), m2(_m2), f(_f) {};
/// \e
Value operator[](Key k) const {return f(m1[k],m2[k]);}
};
///Returns a \c CombineMap class
///This function just returns a \c CombineMap class.
///
///For example if \c m1 and \c m2 are both \c double valued maps, then
///\code
///combineMap(m1,m2,std::plus<double>())
///\endcode
///is equivalent to
///\code
///addMap(m1,m2)
///\endcode
///
///This function is specialized for adaptable binary function
///classes and C++ functions.
///
///\relates CombineMap
template<typename M1, typename M2, typename F, typename V>
inline CombineMap<M1, M2, F, V>
combineMap(const M1& m1,const M2& m2, const F& f) {
return CombineMap<M1, M2, F, V>(m1,m2,f);
}
template<typename M1, typename M2, typename F>
inline CombineMap<M1, M2, F, typename F::result_type>
combineMap(const M1& m1, const M2& m2, const F& f) {
return combineMap<M1, M2, F, typename F::result_type>(m1,m2,f);
}
template<typename M1, typename M2, typename K1, typename K2, typename V>
inline CombineMap<M1, M2, V (*)(K1, K2), V>
combineMap(const M1 &m1, const M2 &m2, V (*f)(K1, K2)) {
return combineMap<M1, M2, V (*)(K1, K2), V>(m1,m2,f);
}
///Negative value of a map
///This \c concepts::ReadMap "read only map" returns the negative
///value of the value returned by the given map.
///Its \c Key and \c Value are inherited from \c M.
///The unary \c - operator must be defined for \c Value, of course.
///
///\sa NegWriteMap
template<typename M>
class NegMap : public MapBase<typename M::Key, typename M::Value> {
const M& m;
public:
typedef MapBase<typename M::Key, typename M::Value> Parent;
typedef typename Parent::Key Key;
typedef typename Parent::Value Value;
///Constructor
NegMap(const M &_m) : m(_m) {};
/// \e
Value operator[](Key k) const {return -m[k];}
};
///Negative value of a map (ReadWrite version)
///This \c concepts::ReadWriteMap "read-write map" returns the negative
///value of the value returned by the given map.
///Its \c Key and \c Value are inherited from \c M.
///The unary \c - operator must be defined for \c Value, of course.
///
/// \sa NegMap
template<typename M>
class NegWriteMap : public MapBase<typename M::Key, typename M::Value> {
M& m;
public:
typedef MapBase<typename M::Key, typename M::Value> Parent;
typedef typename Parent::Key Key;
typedef typename Parent::Value Value;
///Constructor
NegWriteMap(M &_m) : m(_m) {};
/// \e
Value operator[](Key k) const {return -m[k];}
/// \e
void set(Key k, const Value& v) { m.set(k, -v); }
};
///Returns a \c NegMap class
///This function just returns a \c NegMap class.
///\relates NegMap
template <typename M>
inline NegMap<M> negMap(const M &m) {
return NegMap<M>(m);
}
///Returns a \c NegWriteMap class
///This function just returns a \c NegWriteMap class.
///\relates NegWriteMap
template <typename M>
inline NegWriteMap<M> negMap(M &m) {
return NegWriteMap<M>(m);
}
///Absolute value of a map
///This \c concepts::ReadMap "read only map" returns the absolute value
///of the value returned by the given map.
///Its \c Key and \c Value are inherited from \c M.
///\c Value must be comparable to \c 0 and the unary \c -
///operator must be defined for it, of course.
template<typename M>
class AbsMap : public MapBase<typename M::Key, typename M::Value> {
const M& m;
public:
typedef MapBase<typename M::Key, typename M::Value> Parent;
typedef typename Parent::Key Key;
typedef typename Parent::Value Value;
///Constructor
AbsMap(const M &_m) : m(_m) {};
/// \e
Value operator[](Key k) const {
Value tmp = m[k];
return tmp >= 0 ? tmp : -tmp;
}
};
///Returns an \c AbsMap class
///This function just returns an \c AbsMap class.
///\relates AbsMap
template<typename M>
inline AbsMap<M> absMap(const M &m) {
return AbsMap<M>(m);
}
///Converts an STL style functor to a map
///This \c concepts::ReadMap "read only map" returns the value
///of a given functor.
///
///Template parameters \c K and \c V will become its
///\c Key and \c Value.
///In most cases they have to be given explicitly because a
///functor typically does not provide such typedefs.
///
///Parameter \c F is the type of the used functor.
///
///\sa MapFunctor
template<typename F,
typename K = typename F::argument_type,
typename V = typename F::result_type>
class FunctorMap : public MapBase<K, V> {
F f;
public:
typedef MapBase<K, V> Parent;
typedef typename Parent::Key Key;
typedef typename Parent::Value Value;
///Constructor
FunctorMap(const F &_f = F()) : f(_f) {}
/// \e
Value operator[](Key k) const { return f(k);}
};
///Returns a \c FunctorMap class
///This function just returns a \c FunctorMap class.
///
///It is specialized for adaptable function classes and
///C++ functions.
///\relates FunctorMap
template<typename K, typename V, typename F> inline
FunctorMap<F, K, V> functorMap(const F &f) {
return FunctorMap<F, K, V>(f);
}
template <typename F> inline
FunctorMap<F, typename F::argument_type, typename F::result_type>
functorMap(const F &f) {
return FunctorMap<F, typename F::argument_type,
typename F::result_type>(f);
}
template <typename K, typename V> inline
FunctorMap<V (*)(K), K, V> functorMap(V (*f)(K)) {
return FunctorMap<V (*)(K), K, V>(f);
}
///Converts a map to an STL style (unary) functor
///This class Converts a map to an STL style (unary) functor.
///that is it provides an <tt>operator()</tt> to read its values.
///
///For the sake of convenience it also works as
///a ususal \c concepts::ReadMap "readable map",
///i.e. <tt>operator[]</tt> and the \c Key and \c Value typedefs also exist.
///
///\sa FunctorMap
template <typename M>
class MapFunctor : public MapBase<typename M::Key, typename M::Value> {
const M& m;
public:
typedef MapBase<typename M::Key, typename M::Value> Parent;
typedef typename Parent::Key Key;
typedef typename Parent::Value Value;
typedef typename M::Key argument_type;
typedef typename M::Value result_type;
///Constructor
MapFunctor(const M &_m) : m(_m) {};
///\e
Value operator()(Key k) const {return m[k];}
///\e
Value operator[](Key k) const {return m[k];}
};
///Returns a \c MapFunctor class
///This function just returns a \c MapFunctor class.
///\relates MapFunctor
template<typename M>
inline MapFunctor<M> mapFunctor(const M &m) {
return MapFunctor<M>(m);
}
///Applies all map setting operations to two maps
///This map has two \c concepts::ReadMap "readable map"
///parameters and each read request will be passed just to the
///first map. This class is the just readable map type of the ForkWriteMap.
///
///The \c Key and \c Value are inherited from \c M1.
///The \c Key and \c Value of M2 must be convertible from those of \c M1.
///
///\sa ForkWriteMap
///
/// \todo Why is it needed?
template<typename M1, typename M2>
class ForkMap : public MapBase<typename M1::Key, typename M1::Value> {
const M1& m1;
const M2& m2;
public:
typedef MapBase<typename M1::Key, typename M1::Value> Parent;
typedef typename Parent::Key Key;
typedef typename Parent::Value Value;
///Constructor
ForkMap(const M1 &_m1, const M2 &_m2) : m1(_m1), m2(_m2) {};
/// \e
Value operator[](Key k) const {return m1[k];}
};
///Applies all map setting operations to two maps
///This map has two \c concepts::WriteMap "writable map"
///parameters and each write request will be passed to both of them.
///If \c M1 is also \c concepts::ReadMap "readable",
///then the read operations will return the
///corresponding values of \c M1.
///
///The \c Key and \c Value are inherited from \c M1.
///The \c Key and \c Value of M2 must be convertible from those of \c M1.
///
///\sa ForkMap
template<typename M1, typename M2>
class ForkWriteMap : public MapBase<typename M1::Key, typename M1::Value> {
M1& m1;
M2& m2;
public:
typedef MapBase<typename M1::Key, typename M1::Value> Parent;
typedef typename Parent::Key Key;
typedef typename Parent::Value Value;
///Constructor
ForkWriteMap(M1 &_m1, M2 &_m2) : m1(_m1), m2(_m2) {};
///\e
Value operator[](Key k) const {return m1[k];}
///\e
void set(Key k, const Value &v) {m1.set(k,v); m2.set(k,v);}
};
///Returns a \c ForkMap class
///This function just returns a \c ForkMap class.
///\relates ForkMap
template <typename M1, typename M2>
inline ForkMap<M1, M2> forkMap(const M1 &m1, const M2 &m2) {
return ForkMap<M1, M2>(m1,m2);
}
///Returns a \c ForkWriteMap class
///This function just returns a \c ForkWriteMap class.
///\relates ForkWriteMap
template <typename M1, typename M2>
inline ForkWriteMap<M1, M2> forkMap(M1 &m1, M2 &m2) {
return ForkWriteMap<M1, M2>(m1,m2);
}
/* ************* BOOL MAPS ******************* */
///Logical 'not' of a map
///This bool \c concepts::ReadMap "read only map" returns the
///logical negation of the value returned by the given map.
///Its \c Key is inherited from \c M, its Value is \c bool.
///
///\sa NotWriteMap
template <typename M>
class NotMap : public MapBase<typename M::Key, bool> {
const M& m;
public:
typedef MapBase<typename M::Key, bool> Parent;
typedef typename Parent::Key Key;
typedef typename Parent::Value Value;
/// Constructor
NotMap(const M &_m) : m(_m) {};
///\e
Value operator[](Key k) const {return !m[k];}
};
///Logical 'not' of a map (ReadWrie version)
///This bool \c concepts::ReadWriteMap "read-write map" returns the
///logical negation of the value returned by the given map. When it is set,
///the opposite value is set to the original map.
///Its \c Key is inherited from \c M, its Value is \c bool.
///
///\sa NotMap
template <typename M>
class NotWriteMap : public MapBase<typename M::Key, bool> {
M& m;
public:
typedef MapBase<typename M::Key, bool> Parent;
typedef typename Parent::Key Key;
typedef typename Parent::Value Value;
/// Constructor
NotWriteMap(M &_m) : m(_m) {};
///\e
Value operator[](Key k) const {return !m[k];}
///\e
void set(Key k, bool v) { m.set(k, !v); }
};
///Returns a \c NotMap class
///This function just returns a \c NotMap class.
///\relates NotMap
template <typename M>
inline NotMap<M> notMap(const M &m) {
return NotMap<M>(m);
}
///Returns a \c NotWriteMap class
///This function just returns a \c NotWriteMap class.
///\relates NotWriteMap
template <typename M>
inline NotWriteMap<M> notMap(M &m) {
return NotWriteMap<M>(m);
}
namespace _maps_bits {
template <typename Value>
struct Identity {
typedef Value argument_type;
typedef Value result_type;
Value operator()(const Value& val) const {
return val;
}
};
template <typename _Iterator, typename Enable = void>
struct IteratorTraits {
typedef typename std::iterator_traits<_Iterator>::value_type Value;
};
template <typename _Iterator>
struct IteratorTraits<_Iterator,
typename exists<typename _Iterator::container_type>::type>
{
typedef typename _Iterator::container_type::value_type Value;
};
}
/// \brief Writable bool map for logging each \c true assigned element
///
/// Writable bool map for logging each \c true assigned element, i.e it
/// copies all the keys set to \c true to the given iterator.
///
/// \note The container of the iterator should contain space
/// for each element.
///
/// The following example shows how you can write the edges found by the Prim
/// algorithm directly
/// to the standard output.
///\code
/// typedef IdMap<Graph, Edge> EdgeIdMap;
/// EdgeIdMap edgeId(graph);
///
/// typedef MapFunctor<EdgeIdMap> EdgeIdFunctor;
/// EdgeIdFunctor edgeIdFunctor(edgeId);
///
/// StoreBoolMap<ostream_iterator<int>, EdgeIdFunctor>
/// writerMap(ostream_iterator<int>(cout, " "), edgeIdFunctor);
///
/// prim(graph, cost, writerMap);
///\endcode
///
///\sa BackInserterBoolMap
///\sa FrontInserterBoolMap
///\sa InserterBoolMap
///
///\todo Revise the name of this class and the related ones.
template <typename _Iterator,
typename _Functor =
_maps_bits::Identity<typename _maps_bits::
IteratorTraits<_Iterator>::Value> >
class StoreBoolMap {
public:
typedef _Iterator Iterator;
typedef typename _Functor::argument_type Key;
typedef bool Value;
typedef _Functor Functor;
/// Constructor
StoreBoolMap(Iterator it, const Functor& functor = Functor())
: _begin(it), _end(it), _functor(functor) {}
/// Gives back the given iterator set for the first key
Iterator begin() const {
return _begin;
}
/// Gives back the the 'after the last' iterator
Iterator end() const {
return _end;
}
/// The \c set function of the map
void set(const Key& key, Value value) const {
if (value) {
*_end++ = _functor(key);
}
}
private:
Iterator _begin;
mutable Iterator _end;
Functor _functor;
};
/// \brief Writable bool map for logging each \c true assigned element in
/// a back insertable container.
///
/// Writable bool map for logging each \c true assigned element by pushing
/// them into a back insertable container.
/// It can be used to retrieve the items into a standard
/// container. The next example shows how you can store the
/// edges found by the Prim algorithm in a vector.
///
///\code
/// vector<Edge> span_tree_edges;
/// BackInserterBoolMap<vector<Edge> > inserter_map(span_tree_edges);
/// prim(graph, cost, inserter_map);
///\endcode
///
///\sa StoreBoolMap
///\sa FrontInserterBoolMap
///\sa InserterBoolMap
template <typename Container,
typename Functor =
_maps_bits::Identity<typename Container::value_type> >
class BackInserterBoolMap {
public:
typedef typename Functor::argument_type Key;
typedef bool Value;
/// Constructor
BackInserterBoolMap(Container& _container,
const Functor& _functor = Functor())
: container(_container), functor(_functor) {}
/// The \c set function of the map
void set(const Key& key, Value value) {
if (value) {
container.push_back(functor(key));
}
}
private:
Container& container;
Functor functor;
};
/// \brief Writable bool map for logging each \c true assigned element in
/// a front insertable container.
///
/// Writable bool map for logging each \c true assigned element by pushing
/// them into a front insertable container.
/// It can be used to retrieve the items into a standard
/// container. For example see \ref BackInserterBoolMap.
///
///\sa BackInserterBoolMap
///\sa InserterBoolMap
template <typename Container,
typename Functor =
_maps_bits::Identity<typename Container::value_type> >
class FrontInserterBoolMap {
public:
typedef typename Functor::argument_type Key;
typedef bool Value;
/// Constructor
FrontInserterBoolMap(Container& _container,
const Functor& _functor = Functor())
: container(_container), functor(_functor) {}
/// The \c set function of the map
void set(const Key& key, Value value) {
if (value) {
container.push_front(functor(key));
}
}
private:
Container& container;
Functor functor;
};
/// \brief Writable bool map for storing each \c true assigned element in
/// an insertable container.
///
/// Writable bool map for storing each \c true assigned element in an
/// insertable container. It will insert all the keys set to \c true into
/// the container.
///
/// For example, if you want to store the cut arcs of the strongly
/// connected components in a set you can use the next code:
///
///\code
/// set<Arc> cut_arcs;
/// InserterBoolMap<set<Arc> > inserter_map(cut_arcs);
/// stronglyConnectedCutArcs(digraph, cost, inserter_map);
///\endcode
///
///\sa BackInserterBoolMap
///\sa FrontInserterBoolMap
template <typename Container,
typename Functor =
_maps_bits::Identity<typename Container::value_type> >
class InserterBoolMap {
public:
typedef typename Container::value_type Key;
typedef bool Value;
/// Constructor with specified iterator
/// Constructor with specified iterator.
/// \param _container The container for storing the elements.
/// \param _it The elements will be inserted before this iterator.
/// \param _functor The functor that is used when an element is stored.
InserterBoolMap(Container& _container, typename Container::iterator _it,
const Functor& _functor = Functor())
: container(_container), it(_it), functor(_functor) {}
/// Constructor
/// Constructor without specified iterator.
/// The elements will be inserted before <tt>_container.end()</tt>.
/// \param _container The container for storing the elements.
/// \param _functor The functor that is used when an element is stored.
InserterBoolMap(Container& _container, const Functor& _functor = Functor())
: container(_container), it(_container.end()), functor(_functor) {}
/// The \c set function of the map
void set(const Key& key, Value value) {
if (value) {
it = container.insert(it, functor(key));
++it;
}
}
private:
Container& container;
typename Container::iterator it;
Functor functor;
};
/// \brief Writable bool map for filling each \c true assigned element with a
/// given value.
///
/// Writable bool map for filling each \c true assigned element with a
/// given value. The value can set the container.
///
/// The following code finds the connected components of a graph
/// and stores it in the \c comp map:
///\code
/// typedef Graph::NodeMap<int> ComponentMap;
/// ComponentMap comp(graph);
/// typedef FillBoolMap<Graph::NodeMap<int> > ComponentFillerMap;
/// ComponentFillerMap filler(comp, 0);
///
/// Dfs<Graph>::DefProcessedMap<ComponentFillerMap>::Create dfs(graph);
/// dfs.processedMap(filler);
/// dfs.init();
/// for (NodeIt it(graph); it != INVALID; ++it) {
/// if (!dfs.reached(it)) {
/// dfs.addSource(it);
/// dfs.start();
/// ++filler.fillValue();
/// }
/// }
///\endcode
template <typename Map>
class FillBoolMap {
public:
typedef typename Map::Key Key;
typedef bool Value;
/// Constructor
FillBoolMap(Map& _map, const typename Map::Value& _fill)
: map(_map), fill(_fill) {}
/// Constructor
FillBoolMap(Map& _map)
: map(_map), fill() {}
/// Gives back the current fill value
const typename Map::Value& fillValue() const {
return fill;
}
/// Gives back the current fill value
typename Map::Value& fillValue() {
return fill;
}
/// Sets the current fill value
void fillValue(const typename Map::Value& _fill) {
fill = _fill;
}
/// The \c set function of the map
void set(const Key& key, Value value) {
if (value) {
map.set(key, fill);
}
}
private:
Map& map;
typename Map::Value fill;
};
/// \brief Writable bool map for storing the sequence number of
/// \c true assignments.
///
/// Writable bool map that stores for each \c true assigned elements
/// the sequence number of this setting.
/// It makes it easy to calculate the leaving
/// order of the nodes in the \c Dfs algorithm.
///
///\code
/// typedef Digraph::NodeMap<int> OrderMap;
/// OrderMap order(digraph);
/// typedef SettingOrderBoolMap<OrderMap> OrderSetterMap;
/// OrderSetterMap setter(order);
/// Dfs<Digraph>::DefProcessedMap<OrderSetterMap>::Create dfs(digraph);
/// dfs.processedMap(setter);
/// dfs.init();
/// for (NodeIt it(digraph); it != INVALID; ++it) {
/// if (!dfs.reached(it)) {
/// dfs.addSource(it);
/// dfs.start();
/// }
/// }
///\endcode
///
/// The storing of the discovering order is more difficult because the
/// ReachedMap should be readable in the dfs algorithm but the setting
/// order map is not readable. Thus we must use the fork map:
///
///\code
/// typedef Digraph::NodeMap<int> OrderMap;
/// OrderMap order(digraph);
/// typedef SettingOrderBoolMap<OrderMap> OrderSetterMap;
/// OrderSetterMap setter(order);
/// typedef Digraph::NodeMap<bool> StoreMap;
/// StoreMap store(digraph);
///
/// typedef ForkWriteMap<StoreMap, OrderSetterMap> ReachedMap;
/// ReachedMap reached(store, setter);
///
/// Dfs<Digraph>::DefReachedMap<ReachedMap>::Create dfs(digraph);
/// dfs.reachedMap(reached);
/// dfs.init();
/// for (NodeIt it(digraph); it != INVALID; ++it) {
/// if (!dfs.reached(it)) {
/// dfs.addSource(it);
/// dfs.start();
/// }
/// }
///\endcode
template <typename Map>
class SettingOrderBoolMap {
public:
typedef typename Map::Key Key;
typedef bool Value;
/// Constructor
SettingOrderBoolMap(Map& _map)
: map(_map), counter(0) {}
/// Number of set operations.
int num() const {
return counter;
}
/// The \c set function of the map
void set(const Key& key, Value value) {
if (value) {
map.set(key, counter++);
}
}
private:
Map& map;
int counter;
};
/// @}
}
#endif // LEMON_MAPS_H