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

Load file history
gravatar
kpeter (Peter Kovacs)
Minor improvements in CostScaling (#417)
   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
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
/* -*- mode: C++; indent-tabs-mode: nil; -*-
*
* This file is a part of LEMON, a generic C++ optimization library.
*
* Copyright (C) 2003-2010
* 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 <map>
#include <lemon/core.h>
///\file
///\ingroup maps
///\brief Miscellaneous property maps
namespace lemon {
/// \addtogroup maps
/// @{
/// Base class of maps.
/// Base class of maps. It provides the necessary type definitions
/// required by the map %concepts.
template<typename K, typename V>
class MapBase {
public:
/// \brief The key type of the map.
typedef K Key;
/// \brief The value type of the map.
/// (The type of objects associated with the keys).
typedef V 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>).
/// It conforms to the \ref concepts::ReadWriteMap "ReadWriteMap" concept.
///
/// \sa ConstMap
template<typename K, typename V>
class NullMap : public MapBase<K, V> {
public:
///\e
typedef K Key;
///\e
typedef V Value;
/// Gives back a default constructed element.
Value operator[](const Key&) const { return Value(); }
/// Absorbs the value.
void set(const Key&, const Value&) {}
};
/// 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 \ref concepts::ReadMap "readable map" assigns a specified
/// value to each key.
///
/// In other aspects it is equivalent to \c NullMap.
/// So it conforms to the \ref concepts::ReadWriteMap "ReadWriteMap"
/// concept, but it absorbs the data written to it.
///
/// The simplest way of using this map is through the constMap()
/// function.
///
/// \sa NullMap
/// \sa IdentityMap
template<typename K, typename V>
class ConstMap : public MapBase<K, V> {
private:
V _value;
public:
///\e
typedef K Key;
///\e
typedef V Value;
/// Default constructor
/// Default constructor.
/// The value of the map will be default constructed.
ConstMap() {}
/// Constructor with specified initial value
/// Constructor with specified initial value.
/// \param v The initial value of the map.
ConstMap(const Value &v) : _value(v) {}
/// Gives back the specified value.
Value operator[](const Key&) const { return _value; }
/// Absorbs the value.
void set(const Key&, const Value&) {}
/// Sets the value that is assigned to each key.
void setAll(const Value &v) {
_value = v;
}
template<typename V1>
ConstMap(const ConstMap<K, V1> &, const Value &v) : _value(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 K, typename V>
inline ConstMap<K, V> constMap() {
return ConstMap<K, V>();
}
template<typename T, T v>
struct Const {};
/// Constant map with inlined constant value.
/// This \ref concepts::ReadMap "readable map" assigns a specified
/// value to each key.
///
/// In other aspects it is equivalent to \c NullMap.
/// So it conforms to the \ref concepts::ReadWriteMap "ReadWriteMap"
/// concept, but it absorbs the data written to it.
///
/// The simplest way of using this map is through the constMap()
/// function.
///
/// \sa NullMap
/// \sa IdentityMap
template<typename K, typename V, V v>
class ConstMap<K, Const<V, v> > : public MapBase<K, V> {
public:
///\e
typedef K Key;
///\e
typedef V Value;
/// Constructor.
ConstMap() {}
/// Gives back the specified value.
Value operator[](const Key&) const { return v; }
/// Absorbs the value.
void set(const Key&, const Value&) {}
};
/// Returns a \c ConstMap class with inlined constant value
/// This function just returns a \c ConstMap class with inlined
/// constant value.
/// \relates ConstMap
template<typename K, typename V, V v>
inline ConstMap<K, Const<V, v> > constMap() {
return ConstMap<K, Const<V, v> >();
}
/// Identity map.
/// This \ref concepts::ReadMap "read-only map" gives back the given
/// key as value without any modification.
///
/// \sa ConstMap
template <typename T>
class IdentityMap : public MapBase<T, T> {
public:
///\e
typedef T Key;
///\e
typedef T Value;
/// Gives back the given value without any modification.
Value operator[](const Key &k) const {
return k;
}
};
/// 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 Map for storing values for integer keys from the range
/// <tt>[0..size-1]</tt>.
///
/// This map is essentially a wrapper for \c std::vector. It assigns
/// values to integer keys from the range <tt>[0..size-1]</tt>.
/// It can be used together with some data structures, e.g.
/// heap types and \c UnionFind, when the used items are small
/// integers. This map conforms to the \ref concepts::ReferenceMap
/// "ReferenceMap" concept.
///
/// The simplest way of using this map is through the rangeMap()
/// function.
template <typename V>
class RangeMap : public MapBase<int, V> {
template <typename V1>
friend class RangeMap;
private:
typedef std::vector<V> Vector;
Vector _vector;
public:
/// Key type
typedef int Key;
/// Value type
typedef V Value;
/// Reference type
typedef typename Vector::reference Reference;
/// Const reference type
typedef typename Vector::const_reference ConstReference;
typedef True ReferenceMapTag;
public:
/// Constructor with specified default value.
RangeMap(int size = 0, const Value &value = Value())
: _vector(size, value) {}
/// Constructs the map from an appropriate \c std::vector.
template <typename V1>
RangeMap(const std::vector<V1>& vector)
: _vector(vector.begin(), vector.end()) {}
/// Constructs the map from another \c RangeMap.
template <typename V1>
RangeMap(const RangeMap<V1> &c)
: _vector(c._vector.begin(), c._vector.end()) {}
/// Returns the size of the map.
int size() {
return _vector.size();
}
/// Resizes the map.
/// Resizes the underlying \c std::vector container, so changes the
/// keyset of the map.
/// \param size The new size of the map. The new keyset will be the
/// range <tt>[0..size-1]</tt>.
/// \param value The default value to assign to the new keys.
void resize(int size, const Value &value = Value()) {
_vector.resize(size, value);
}
private:
RangeMap& operator=(const RangeMap&);
public:
///\e
Reference operator[](const Key &k) {
return _vector[k];
}
///\e
ConstReference operator[](const Key &k) const {
return _vector[k];
}
///\e
void set(const Key &k, const Value &v) {
_vector[k] = v;
}
};
/// Returns a \c RangeMap class
/// This function just returns a \c RangeMap class.
/// \relates RangeMap
template<typename V>
inline RangeMap<V> rangeMap(int size = 0, const V &value = V()) {
return RangeMap<V>(size, value);
}
/// \brief Returns a \c RangeMap class created from an appropriate
/// \c std::vector
/// This function just returns a \c RangeMap class created from an
/// appropriate \c std::vector.
/// \relates RangeMap
template<typename V>
inline RangeMap<V> rangeMap(const std::vector<V> &vector) {
return RangeMap<V>(vector);
}
/// Map type based on \c std::map
/// This map is essentially a wrapper for \c std::map with addition
/// that you can specify a default value for the keys that are not
/// stored actually. This value can be different from the default
/// contructed value (i.e. \c %Value()).
/// This type conforms to the \ref concepts::ReferenceMap "ReferenceMap"
/// concept.
///
/// This map is useful if a default value should be assigned to most of
/// the keys and different values should be assigned only to a few
/// keys (i.e. the map is "sparse").
/// The name of this type also refers to this important usage.
///
/// Apart form that, this map can be used in many other cases since it
/// is based on \c std::map, which is a general associative container.
/// However, keep in mind that it is usually not as efficient as other
/// maps.
///
/// The simplest way of using this map is through the sparseMap()
/// function.
template <typename K, typename V, typename Comp = std::less<K> >
class SparseMap : public MapBase<K, V> {
template <typename K1, typename V1, typename C1>
friend class SparseMap;
public:
/// Key type
typedef K Key;
/// Value type
typedef V Value;
/// Reference type
typedef Value& Reference;
/// Const reference type
typedef const Value& ConstReference;
typedef True ReferenceMapTag;
private:
typedef std::map<K, V, Comp> Map;
Map _map;
Value _value;
public:
/// \brief Constructor with specified default value.
SparseMap(const Value &value = Value()) : _value(value) {}
/// \brief Constructs the map from an appropriate \c std::map, and
/// explicitly specifies a default value.
template <typename V1, typename Comp1>
SparseMap(const std::map<Key, V1, Comp1> &map,
const Value &value = Value())
: _map(map.begin(), map.end()), _value(value) {}
/// \brief Constructs the map from another \c SparseMap.
template<typename V1, typename Comp1>
SparseMap(const SparseMap<Key, V1, Comp1> &c)
: _map(c._map.begin(), c._map.end()), _value(c._value) {}
private:
SparseMap& operator=(const SparseMap&);
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 Value &v) {
typename Map::iterator it = _map.lower_bound(k);
if (it != _map.end() && !_map.key_comp()(k, it->first))
it->second = v;
else
_map.insert(it, std::make_pair(k, v));
}
///\e
void setAll(const Value &v) {
_value = v;
_map.clear();
}
};
/// Returns a \c SparseMap class
/// This function just returns a \c SparseMap class with specified
/// default value.
/// \relates SparseMap
template<typename K, typename V, typename Compare>
inline SparseMap<K, V, Compare> sparseMap(const V& value = V()) {
return SparseMap<K, V, Compare>(value);
}
template<typename K, typename V>
inline SparseMap<K, V, std::less<K> > sparseMap(const V& value = V()) {
return SparseMap<K, V, std::less<K> >(value);
}
/// \brief Returns a \c SparseMap class created from an appropriate
/// \c std::map
/// This function just returns a \c SparseMap class created from an
/// appropriate \c std::map.
/// \relates SparseMap
template<typename K, typename V, typename Compare>
inline SparseMap<K, V, Compare>
sparseMap(const std::map<K, V, Compare> &map, const V& value = V())
{
return SparseMap<K, V, Compare>(map, value);
}
/// @}
/// \addtogroup map_adaptors
/// @{
/// Composition of two maps
/// This \ref 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>.
///
/// The \c Key type of the map is inherited from \c M2 and the
/// \c Value type is from \c M1.
/// \c M2::Value must be convertible to \c M1::Key.
///
/// The simplest way of using this map is through the composeMap()
/// function.
///
/// \sa CombineMap
template <typename M1, typename M2>
class ComposeMap : public MapBase<typename M2::Key, typename M1::Value> {
const M1 &_m1;
const M2 &_m2;
public:
///\e
typedef typename M2::Key Key;
///\e
typedef typename M1::Value Value;
/// Constructor
ComposeMap(const M1 &m1, const M2 &m2) : _m1(m1), _m2(m2) {}
///\e
typename MapTraits<M1>::ConstReturnValue
operator[](const Key &k) const { return _m1[_m2[k]]; }
};
/// Returns a \c ComposeMap class
/// This function just returns a \c ComposeMap class.
///
/// If \c m1 and \c m2 are maps and the \c Value type of \c m2 is
/// convertible to the \c Key of \c m1, then <tt>composeMap(m1,m2)[x]</tt>
/// will be equal to <tt>m1[m2[x]]</tt>.
///
/// \relates ComposeMap
template <typename M1, typename M2>
inline ComposeMap<M1, M2> composeMap(const M1 &m1, const M2 &m2) {
return ComposeMap<M1, M2>(m1, m2);
}
/// Combination of two maps using an STL (binary) functor.
/// This \ref concepts::ReadMap "read-only map" takes two maps and a
/// binary functor and returns the combination of the two given maps
/// using the functor.
/// That is to say, if \c m1 is of type \c M1 and \c m2 is of \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>.
///
/// The \c Key type of the map is inherited from \c M1 (\c M1::Key
/// must be convertible to \c M2::Key) and the \c Value type 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.
///
/// The simplest way of using this map is through the combineMap()
/// function.
///
/// \sa ComposeMap
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:
///\e
typedef typename M1::Key Key;
///\e
typedef V Value;
/// Constructor
CombineMap(const M1 &m1, const M2 &m2, const F &f = F())
: _m1(m1), _m2(m2), _f(f) {}
///\e
Value operator[](const 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 maps with \c double
/// values, 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);
}
/// Converts an STL style (unary) functor to a map
/// This \ref concepts::ReadMap "read-only map" returns the value
/// of a given functor. Actually, it just wraps the functor and
/// provides the \c Key and \c Value typedefs.
///
/// 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 \c argument_type and
/// \c result_type typedefs.
/// Parameter \c F is the type of the used functor.
///
/// The simplest way of using this map is through the functorToMap()
/// function.
///
/// \sa MapToFunctor
template<typename F,
typename K = typename F::argument_type,
typename V = typename F::result_type>
class FunctorToMap : public MapBase<K, V> {
F _f;
public:
///\e
typedef K Key;
///\e
typedef V Value;
/// Constructor
FunctorToMap(const F &f = F()) : _f(f) {}
///\e
Value operator[](const Key &k) const { return _f(k); }
};
/// Returns a \c FunctorToMap class
/// This function just returns a \c FunctorToMap class.
///
/// This function is specialized for adaptable binary function
/// classes and C++ functions.
///
/// \relates FunctorToMap
template<typename K, typename V, typename F>
inline FunctorToMap<F, K, V> functorToMap(const F &f) {
return FunctorToMap<F, K, V>(f);
}
template <typename F>
inline FunctorToMap<F, typename F::argument_type, typename F::result_type>
functorToMap(const F &f)
{
return FunctorToMap<F, typename F::argument_type,
typename F::result_type>(f);
}
template <typename K, typename V>
inline FunctorToMap<V (*)(K), K, V> functorToMap(V (*f)(K)) {
return FunctorToMap<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 usual
/// \ref concepts::ReadMap "readable map", i.e. <tt>operator[]</tt>
/// and the \c Key and \c Value typedefs also exist.
///
/// The simplest way of using this map is through the mapToFunctor()
/// function.
///
///\sa FunctorToMap
template <typename M>
class MapToFunctor : public MapBase<typename M::Key, typename M::Value> {
const M &_m;
public:
///\e
typedef typename M::Key Key;
///\e
typedef typename M::Value Value;
typedef typename M::Key argument_type;
typedef typename M::Value result_type;
/// Constructor
MapToFunctor(const M &m) : _m(m) {}
///\e
Value operator()(const Key &k) const { return _m[k]; }
///\e
Value operator[](const Key &k) const { return _m[k]; }
};
/// Returns a \c MapToFunctor class
/// This function just returns a \c MapToFunctor class.
/// \relates MapToFunctor
template<typename M>
inline MapToFunctor<M> mapToFunctor(const M &m) {
return MapToFunctor<M>(m);
}
/// \brief Map adaptor to convert the \c Value type of a map to
/// another type using the default conversion.
/// Map adaptor to convert the \c Value type of a \ref concepts::ReadMap
/// "readable map" to another type using the default conversion.
/// The \c Key type of it is inherited from \c M and the \c Value
/// type is \c V.
/// This type conforms to the \ref concepts::ReadMap "ReadMap" concept.
///
/// The simplest way of using this map is through the convertMap()
/// function.
template <typename M, typename V>
class ConvertMap : public MapBase<typename M::Key, V> {
const M &_m;
public:
///\e
typedef typename M::Key Key;
///\e
typedef V Value;
/// Constructor
/// Constructor.
/// \param m The underlying map.
ConvertMap(const M &m) : _m(m) {}
///\e
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 V, typename M>
inline ConvertMap<M, V> convertMap(const M &map) {
return ConvertMap<M, V>(map);
}
/// Applies all map setting operations to two maps
/// This map has two \ref concepts::WriteMap "writable map" parameters
/// and each write request will be passed to both of them.
/// If \c M1 is also \ref concepts::ReadMap "readable", then the read
/// operations will return the corresponding values of \c M1.
///
/// The \c Key and \c Value types are inherited from \c M1.
/// The \c Key and \c Value of \c M2 must be convertible from those
/// of \c M1.
///
/// The simplest way of using this map is through the forkMap()
/// function.
template<typename M1, typename M2>
class ForkMap : public MapBase<typename M1::Key, typename M1::Value> {
M1 &_m1;
M2 &_m2;
public:
///\e
typedef typename M1::Key Key;
///\e
typedef typename M1::Value Value;
/// Constructor
ForkMap(M1 &m1, M2 &m2) : _m1(m1), _m2(m2) {}
/// Returns the value associated with the given key in the first map.
Value operator[](const Key &k) const { return _m1[k]; }
/// Sets the value associated with the given key in both maps.
void set(const 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(M1 &m1, M2 &m2) {
return ForkMap<M1,M2>(m1,m2);
}
/// Sum of two maps
/// This \ref concepts::ReadMap "read-only map" returns the sum
/// of the values of the two given maps.
/// Its \c Key and \c Value types are inherited from \c M1.
/// The \c Key and \c Value of \c M2 must be convertible to those of
/// \c M1.
///
/// If \c m1 is of type \c M1 and \c m2 is of \c M2, then for
/// \code
/// AddMap<M1,M2> am(m1,m2);
/// \endcode
/// <tt>am[x]</tt> will be equal to <tt>m1[x]+m2[x]</tt>.
///
/// The simplest way of using this map is through the addMap()
/// function.
///
/// \sa SubMap, MulMap, DivMap
/// \sa ShiftMap, ShiftWriteMap
template<typename M1, typename M2>
class AddMap : public MapBase<typename M1::Key, typename M1::Value> {
const M1 &_m1;
const M2 &_m2;
public:
///\e
typedef typename M1::Key Key;
///\e
typedef typename M1::Value Value;
/// Constructor
AddMap(const M1 &m1, const M2 &m2) : _m1(m1), _m2(m2) {}
///\e
Value operator[](const Key &k) const { return _m1[k]+_m2[k]; }
};
/// Returns an \c AddMap class
/// This function just returns an \c AddMap class.
///
/// For example, if \c m1 and \c m2 are both maps with \c double
/// values, then <tt>addMap(m1,m2)[x]</tt> will be equal to
/// <tt>m1[x]+m2[x]</tt>.
///
/// \relates AddMap
template<typename M1, typename M2>
inline AddMap<M1, M2> addMap(const M1 &m1, const M2 &m2) {
return AddMap<M1, M2>(m1,m2);
}
/// Difference of two maps
/// This \ref concepts::ReadMap "read-only map" returns the difference
/// of the values of the two given maps.
/// Its \c Key and \c Value types are inherited from \c M1.
/// The \c Key and \c Value of \c M2 must be convertible to those of
/// \c M1.
///
/// If \c m1 is of type \c M1 and \c m2 is of \c M2, then for
/// \code
/// SubMap<M1,M2> sm(m1,m2);
/// \endcode
/// <tt>sm[x]</tt> will be equal to <tt>m1[x]-m2[x]</tt>.
///
/// The simplest way of using this map is through the subMap()
/// function.
///
/// \sa AddMap, MulMap, DivMap
template<typename M1, typename M2>
class SubMap : public MapBase<typename M1::Key, typename M1::Value> {
const M1 &_m1;
const M2 &_m2;
public:
///\e
typedef typename M1::Key Key;
///\e
typedef typename M1::Value Value;
/// Constructor
SubMap(const M1 &m1, const M2 &m2) : _m1(m1), _m2(m2) {}
///\e
Value operator[](const Key &k) const { return _m1[k]-_m2[k]; }
};
/// Returns a \c SubMap class
/// This function just returns a \c SubMap class.
///
/// For example, if \c m1 and \c m2 are both maps with \c double
/// values, then <tt>subMap(m1,m2)[x]</tt> will be equal to
/// <tt>m1[x]-m2[x]</tt>.
///
/// \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 \ref concepts::ReadMap "read-only map" returns the product
/// of the values of the two given maps.
/// Its \c Key and \c Value types are inherited from \c M1.
/// The \c Key and \c Value of \c M2 must be convertible to those of
/// \c M1.
///
/// If \c m1 is of type \c M1 and \c m2 is of \c M2, then for
/// \code
/// MulMap<M1,M2> mm(m1,m2);
/// \endcode
/// <tt>mm[x]</tt> will be equal to <tt>m1[x]*m2[x]</tt>.
///
/// The simplest way of using this map is through the mulMap()
/// function.
///
/// \sa AddMap, SubMap, DivMap
/// \sa ScaleMap, ScaleWriteMap
template<typename M1, typename M2>
class MulMap : public MapBase<typename M1::Key, typename M1::Value> {
const M1 &_m1;
const M2 &_m2;
public:
///\e
typedef typename M1::Key Key;
///\e
typedef typename M1::Value Value;
/// Constructor
MulMap(const M1 &m1,const M2 &m2) : _m1(m1), _m2(m2) {}
///\e
Value operator[](const Key &k) const { return _m1[k]*_m2[k]; }
};
/// Returns a \c MulMap class
/// This function just returns a \c MulMap class.
///
/// For example, if \c m1 and \c m2 are both maps with \c double
/// values, then <tt>mulMap(m1,m2)[x]</tt> will be equal to
/// <tt>m1[x]*m2[x]</tt>.
///
/// \relates MulMap
template<typename M1, typename M2>
inline MulMap<M1, M2> mulMap(const M1 &m1,const M2 &m2) {
return MulMap<M1, M2>(m1,m2);
}
/// Quotient of two maps
/// This \ref concepts::ReadMap "read-only map" returns the quotient
/// of the values of the two given maps.
/// Its \c Key and \c Value types are inherited from \c M1.
/// The \c Key and \c Value of \c M2 must be convertible to those of
/// \c M1.
///
/// If \c m1 is of type \c M1 and \c m2 is of \c M2, then for
/// \code
/// DivMap<M1,M2> dm(m1,m2);
/// \endcode
/// <tt>dm[x]</tt> will be equal to <tt>m1[x]/m2[x]</tt>.
///
/// The simplest way of using this map is through the divMap()
/// function.
///
/// \sa AddMap, SubMap, MulMap
template<typename M1, typename M2>
class DivMap : public MapBase<typename M1::Key, typename M1::Value> {
const M1 &_m1;
const M2 &_m2;
public:
///\e
typedef typename M1::Key Key;
///\e
typedef typename M1::Value Value;
/// Constructor
DivMap(const M1 &m1,const M2 &m2) : _m1(m1), _m2(m2) {}
///\e
Value operator[](const Key &k) const { return _m1[k]/_m2[k]; }
};
/// Returns a \c DivMap class
/// This function just returns a \c DivMap class.
///
/// For example, if \c m1 and \c m2 are both maps with \c double
/// values, then <tt>divMap(m1,m2)[x]</tt> will be equal to
/// <tt>m1[x]/m2[x]</tt>.
///
/// \relates DivMap
template<typename M1, typename M2>
inline DivMap<M1, M2> divMap(const M1 &m1,const M2 &m2) {
return DivMap<M1, M2>(m1,m2);
}
/// Shifts a map with a constant.
/// This \ref concepts::ReadMap "read-only map" returns the sum of
/// the given map and a constant value (i.e. it shifts the map with
/// the constant). Its \c Key and \c Value are inherited from \c M.
///
/// Actually,
/// \code
/// ShiftMap<M> sh(m,v);
/// \endcode
/// is equivalent to
/// \code
/// ConstMap<M::Key, M::Value> cm(v);
/// AddMap<M, ConstMap<M::Key, M::Value> > sh(m,cm);
/// \endcode
///
/// The simplest way of using this map is through the shiftMap()
/// function.
///
/// \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:
///\e
typedef typename M::Key Key;
///\e
typedef typename M::Value Value;
/// Constructor
/// Constructor.
/// \param m The undelying map.
/// \param v The constant value.
ShiftMap(const M &m, const C &v) : _m(m), _v(v) {}
///\e
Value operator[](const Key &k) const { return _m[k]+_v; }
};
/// Shifts a map with a constant (read-write version).
/// This \ref concepts::ReadWriteMap "read-write map" returns the sum
/// of the given map and a constant value (i.e. it shifts the map with
/// the constant). Its \c Key and \c Value are inherited from \c M.
/// It makes also possible to write the map.
///
/// The simplest way of using this map is through the shiftWriteMap()
/// function.
///
/// \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:
///\e
typedef typename M::Key Key;
///\e
typedef typename M::Value Value;
/// Constructor
/// Constructor.
/// \param m The undelying map.
/// \param v The constant value.
ShiftWriteMap(M &m, const C &v) : _m(m), _v(v) {}
///\e
Value operator[](const Key &k) const { return _m[k]+_v; }
///\e
void set(const Key &k, const Value &v) { _m.set(k, v-_v); }
};
/// Returns a \c ShiftMap class
/// This function just returns a \c ShiftMap class.
///
/// For example, if \c m is a map with \c double values and \c v is
/// \c double, then <tt>shiftMap(m,v)[x]</tt> will be equal to
/// <tt>m[x]+v</tt>.
///
/// \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.
///
/// For example, if \c m is a map with \c double values and \c v is
/// \c double, then <tt>shiftWriteMap(m,v)[x]</tt> will be equal to
/// <tt>m[x]+v</tt>.
/// Moreover it makes also possible to write the map.
///
/// \relates ShiftWriteMap
template<typename M, typename C>
inline ShiftWriteMap<M, C> shiftWriteMap(M &m, const C &v) {
return ShiftWriteMap<M, C>(m,v);
}
/// Scales a map with a constant.
/// This \ref 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<M> sc(m,v);
/// \endcode
/// is equivalent to
/// \code
/// ConstMap<M::Key, M::Value> cm(v);
/// MulMap<ConstMap<M::Key, M::Value>, M> sc(cm,m);
/// \endcode
///
/// The simplest way of using this map is through the scaleMap()
/// function.
///
/// \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:
///\e
typedef typename M::Key Key;
///\e
typedef typename M::Value Value;
/// Constructor
/// Constructor.
/// \param m The undelying map.
/// \param v The constant value.
ScaleMap(const M &m, const C &v) : _m(m), _v(v) {}
///\e
Value operator[](const Key &k) const { return _v*_m[k]; }
};
/// Scales a map with a constant (read-write version).
/// This \ref concepts::ReadWriteMap "read-write 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.
/// 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.
///
/// The simplest way of using this map is through the scaleWriteMap()
/// function.
///
/// \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:
///\e
typedef typename M::Key Key;
///\e
typedef typename M::Value Value;
/// Constructor
/// Constructor.
/// \param m The undelying map.
/// \param v The constant value.
ScaleWriteMap(M &m, const C &v) : _m(m), _v(v) {}
///\e
Value operator[](const Key &k) const { return _v*_m[k]; }
///\e
void set(const Key &k, const Value &v) { _m.set(k, v/_v); }
};
/// Returns a \c ScaleMap class
/// This function just returns a \c ScaleMap class.
///
/// For example, if \c m is a map with \c double values and \c v is
/// \c double, then <tt>scaleMap(m,v)[x]</tt> will be equal to
/// <tt>v*m[x]</tt>.
///
/// \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.
///
/// For example, if \c m is a map with \c double values and \c v is
/// \c double, then <tt>scaleWriteMap(m,v)[x]</tt> will be equal to
/// <tt>v*m[x]</tt>.
/// Moreover it makes also possible to write the map.
///
/// \relates ScaleWriteMap
template<typename M, typename C>
inline ScaleWriteMap<M, C> scaleWriteMap(M &m, const C &v) {
return ScaleWriteMap<M, C>(m,v);
}
/// Negative of a map
/// This \ref concepts::ReadMap "read-only map" returns the negative
/// of the values of the given map (using the unary \c - operator).
/// Its \c Key and \c Value are inherited from \c M.
///
/// If M::Value is \c int, \c double etc., then
/// \code
/// NegMap<M> neg(m);
/// \endcode
/// is equivalent to
/// \code
/// ScaleMap<M> neg(m,-1);
/// \endcode
///
/// The simplest way of using this map is through the negMap()
/// function.
///
/// \sa NegWriteMap
template<typename M>
class NegMap : public MapBase<typename M::Key, typename M::Value> {
const M& _m;
public:
///\e
typedef typename M::Key Key;
///\e
typedef typename M::Value Value;
/// Constructor
NegMap(const M &m) : _m(m) {}
///\e
Value operator[](const Key &k) const { return -_m[k]; }
};
/// Negative of a map (read-write version)
/// This \ref concepts::ReadWriteMap "read-write map" returns the
/// negative of the values of the given map (using the unary \c -
/// operator).
/// Its \c Key and \c Value are inherited from \c M.
/// It makes also possible to write the map.
///
/// If M::Value is \c int, \c double etc., then
/// \code
/// NegWriteMap<M> neg(m);
/// \endcode
/// is equivalent to
/// \code
/// ScaleWriteMap<M> neg(m,-1);
/// \endcode
///
/// The simplest way of using this map is through the negWriteMap()
/// function.
///
/// \sa NegMap
template<typename M>
class NegWriteMap : public MapBase<typename M::Key, typename M::Value> {
M &_m;
public:
///\e
typedef typename M::Key Key;
///\e
typedef typename M::Value Value;
/// Constructor
NegWriteMap(M &m) : _m(m) {}
///\e
Value operator[](const Key &k) const { return -_m[k]; }
///\e
void set(const Key &k, const Value &v) { _m.set(k, -v); }
};
/// Returns a \c NegMap class
/// This function just returns a \c NegMap class.
///
/// For example, if \c m is a map with \c double values, then
/// <tt>negMap(m)[x]</tt> will be equal to <tt>-m[x]</tt>.
///
/// \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.
///
/// For example, if \c m is a map with \c double values, then
/// <tt>negWriteMap(m)[x]</tt> will be equal to <tt>-m[x]</tt>.
/// Moreover it makes also possible to write the map.
///
/// \relates NegWriteMap
template <typename M>
inline NegWriteMap<M> negWriteMap(M &m) {
return NegWriteMap<M>(m);
}
/// Absolute value of a map
/// This \ref concepts::ReadMap "read-only map" returns the absolute
/// value of the values of 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.
///
/// The simplest way of using this map is through the absMap()
/// function.
template<typename M>
class AbsMap : public MapBase<typename M::Key, typename M::Value> {
const M &_m;
public:
///\e
typedef typename M::Key Key;
///\e
typedef typename M::Value Value;
/// Constructor
AbsMap(const M &m) : _m(m) {}
///\e
Value operator[](const 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.
///
/// For example, if \c m is a map with \c double values, then
/// <tt>absMap(m)[x]</tt> will be equal to <tt>m[x]</tt> if
/// it is positive or zero and <tt>-m[x]</tt> if <tt>m[x]</tt> is
/// negative.
///
/// \relates AbsMap
template<typename M>
inline AbsMap<M> absMap(const M &m) {
return AbsMap<M>(m);
}
/// @}
// Logical maps and map adaptors:
/// \addtogroup maps
/// @{
/// Constant \c true map.
/// This \ref concepts::ReadMap "read-only map" assigns \c true to
/// each key.
///
/// Note that
/// \code
/// TrueMap<K> tm;
/// \endcode
/// is equivalent to
/// \code
/// ConstMap<K,bool> tm(true);
/// \endcode
///
/// \sa FalseMap
/// \sa ConstMap
template <typename K>
class TrueMap : public MapBase<K, bool> {
public:
///\e
typedef K Key;
///\e
typedef bool Value;
/// Gives back \c true.
Value operator[](const Key&) const { return true; }
};
/// Returns a \c TrueMap class
/// This function just returns a \c TrueMap class.
/// \relates TrueMap
template<typename K>
inline TrueMap<K> trueMap() {
return TrueMap<K>();
}
/// Constant \c false map.
/// This \ref concepts::ReadMap "read-only map" assigns \c false to
/// each key.
///
/// Note that
/// \code
/// FalseMap<K> fm;
/// \endcode
/// is equivalent to
/// \code
/// ConstMap<K,bool> fm(false);
/// \endcode
///
/// \sa TrueMap
/// \sa ConstMap
template <typename K>
class FalseMap : public MapBase<K, bool> {
public:
///\e
typedef K Key;
///\e
typedef bool Value;
/// Gives back \c false.
Value operator[](const Key&) const { return false; }
};
/// Returns a \c FalseMap class
/// This function just returns a \c FalseMap class.
/// \relates FalseMap
template<typename K>
inline FalseMap<K> falseMap() {
return FalseMap<K>();
}
/// @}
/// \addtogroup map_adaptors
/// @{
/// Logical 'and' of two maps
/// This \ref concepts::ReadMap "read-only map" returns the logical
/// 'and' of the values of the two given maps.
/// Its \c Key type is inherited from \c M1 and its \c Value type is
/// \c bool. \c M2::Key must be convertible to \c M1::Key.
///
/// If \c m1 is of type \c M1 and \c m2 is of \c M2, then for
/// \code
/// AndMap<M1,M2> am(m1,m2);
/// \endcode
/// <tt>am[x]</tt> will be equal to <tt>m1[x]&&m2[x]</tt>.
///
/// The simplest way of using this map is through the andMap()
/// function.
///
/// \sa OrMap
/// \sa NotMap, NotWriteMap
template<typename M1, typename M2>
class AndMap : public MapBase<typename M1::Key, bool> {
const M1 &_m1;
const M2 &_m2;
public:
///\e
typedef typename M1::Key Key;
///\e
typedef bool Value;
/// Constructor
AndMap(const M1 &m1, const M2 &m2) : _m1(m1), _m2(m2) {}
///\e
Value operator[](const Key &k) const { return _m1[k]&&_m2[k]; }
};
/// Returns an \c AndMap class
/// This function just returns an \c AndMap class.
///
/// For example, if \c m1 and \c m2 are both maps with \c bool values,
/// then <tt>andMap(m1,m2)[x]</tt> will be equal to
/// <tt>m1[x]&&m2[x]</tt>.
///
/// \relates AndMap
template<typename M1, typename M2>
inline AndMap<M1, M2> andMap(const M1 &m1, const M2 &m2) {
return AndMap<M1, M2>(m1,m2);
}
/// Logical 'or' of two maps
/// This \ref concepts::ReadMap "read-only map" returns the logical
/// 'or' of the values of the two given maps.
/// Its \c Key type is inherited from \c M1 and its \c Value type is
/// \c bool. \c M2::Key must be convertible to \c M1::Key.
///
/// If \c m1 is of type \c M1 and \c m2 is of \c M2, then for
/// \code
/// OrMap<M1,M2> om(m1,m2);
/// \endcode
/// <tt>om[x]</tt> will be equal to <tt>m1[x]||m2[x]</tt>.
///
/// The simplest way of using this map is through the orMap()
/// function.
///
/// \sa AndMap
/// \sa NotMap, NotWriteMap
template<typename M1, typename M2>
class OrMap : public MapBase<typename M1::Key, bool> {
const M1 &_m1;
const M2 &_m2;
public:
///\e
typedef typename M1::Key Key;
///\e
typedef bool Value;
/// Constructor
OrMap(const M1 &m1, const M2 &m2) : _m1(m1), _m2(m2) {}
///\e
Value operator[](const Key &k) const { return _m1[k]||_m2[k]; }
};
/// Returns an \c OrMap class
/// This function just returns an \c OrMap class.
///
/// For example, if \c m1 and \c m2 are both maps with \c bool values,
/// then <tt>orMap(m1,m2)[x]</tt> will be equal to
/// <tt>m1[x]||m2[x]</tt>.
///
/// \relates OrMap
template<typename M1, typename M2>
inline OrMap<M1, M2> orMap(const M1 &m1, const M2 &m2) {
return OrMap<M1, M2>(m1,m2);
}
/// Logical 'not' of a map
/// This \ref concepts::ReadMap "read-only map" returns the logical
/// negation of the values of the given map.
/// Its \c Key is inherited from \c M and its \c Value is \c bool.
///
/// The simplest way of using this map is through the notMap()
/// function.
///
/// \sa NotWriteMap
template <typename M>
class NotMap : public MapBase<typename M::Key, bool> {
const M &_m;
public:
///\e
typedef typename M::Key Key;
///\e
typedef bool Value;
/// Constructor
NotMap(const M &m) : _m(m) {}
///\e
Value operator[](const Key &k) const { return !_m[k]; }
};
/// Logical 'not' of a map (read-write version)
/// This \ref concepts::ReadWriteMap "read-write map" returns the
/// logical negation of the values of the given map.
/// Its \c Key is inherited from \c M and its \c Value is \c bool.
/// It makes also possible to write the map. When a value is set,
/// the opposite value is set to the original map.
///
/// The simplest way of using this map is through the notWriteMap()
/// function.
///
/// \sa NotMap
template <typename M>
class NotWriteMap : public MapBase<typename M::Key, bool> {
M &_m;
public:
///\e
typedef typename M::Key Key;
///\e
typedef bool Value;
/// Constructor
NotWriteMap(M &m) : _m(m) {}
///\e
Value operator[](const Key &k) const { return !_m[k]; }
///\e
void set(const Key &k, bool v) { _m.set(k, !v); }
};
/// Returns a \c NotMap class
/// This function just returns a \c NotMap class.
///
/// For example, if \c m is a map with \c bool values, then
/// <tt>notMap(m)[x]</tt> will be equal to <tt>!m[x]</tt>.
///
/// \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.
///
/// For example, if \c m is a map with \c bool values, then
/// <tt>notWriteMap(m)[x]</tt> will be equal to <tt>!m[x]</tt>.
/// Moreover it makes also possible to write the map.
///
/// \relates NotWriteMap
template <typename M>
inline NotWriteMap<M> notWriteMap(M &m) {
return NotWriteMap<M>(m);
}
/// Combination of two maps using the \c == operator
/// This \ref concepts::ReadMap "read-only map" assigns \c true to
/// the keys for which the corresponding values of the two maps are
/// equal.
/// Its \c Key type is inherited from \c M1 and its \c Value type is
/// \c bool. \c M2::Key must be convertible to \c M1::Key.
///
/// If \c m1 is of type \c M1 and \c m2 is of \c M2, then for
/// \code
/// EqualMap<M1,M2> em(m1,m2);
/// \endcode
/// <tt>em[x]</tt> will be equal to <tt>m1[x]==m2[x]</tt>.
///
/// The simplest way of using this map is through the equalMap()
/// function.
///
/// \sa LessMap
template<typename M1, typename M2>
class EqualMap : public MapBase<typename M1::Key, bool> {
const M1 &_m1;
const M2 &_m2;
public:
///\e
typedef typename M1::Key Key;
///\e
typedef bool Value;
/// Constructor
EqualMap(const M1 &m1, const M2 &m2) : _m1(m1), _m2(m2) {}
///\e
Value operator[](const Key &k) const { return _m1[k]==_m2[k]; }
};
/// Returns an \c EqualMap class
/// This function just returns an \c EqualMap class.
///
/// For example, if \c m1 and \c m2 are maps with keys and values of
/// the same type, then <tt>equalMap(m1,m2)[x]</tt> will be equal to
/// <tt>m1[x]==m2[x]</tt>.
///
/// \relates EqualMap
template<typename M1, typename M2>
inline EqualMap<M1, M2> equalMap(const M1 &m1, const M2 &m2) {
return EqualMap<M1, M2>(m1,m2);
}
/// Combination of two maps using the \c < operator
/// This \ref concepts::ReadMap "read-only map" assigns \c true to
/// the keys for which the corresponding value of the first map is
/// less then the value of the second map.
/// Its \c Key type is inherited from \c M1 and its \c Value type is
/// \c bool. \c M2::Key must be convertible to \c M1::Key.
///
/// If \c m1 is of type \c M1 and \c m2 is of \c M2, then for
/// \code
/// LessMap<M1,M2> lm(m1,m2);
/// \endcode
/// <tt>lm[x]</tt> will be equal to <tt>m1[x]<m2[x]</tt>.
///
/// The simplest way of using this map is through the lessMap()
/// function.
///
/// \sa EqualMap
template<typename M1, typename M2>
class LessMap : public MapBase<typename M1::Key, bool> {
const M1 &_m1;
const M2 &_m2;
public:
///\e
typedef typename M1::Key Key;
///\e
typedef bool Value;
/// Constructor
LessMap(const M1 &m1, const M2 &m2) : _m1(m1), _m2(m2) {}
///\e
Value operator[](const Key &k) const { return _m1[k]<_m2[k]; }
};
/// Returns an \c LessMap class
/// This function just returns an \c LessMap class.
///
/// For example, if \c m1 and \c m2 are maps with keys and values of
/// the same type, then <tt>lessMap(m1,m2)[x]</tt> will be equal to
/// <tt>m1[x]<m2[x]</tt>.
///
/// \relates LessMap
template<typename M1, typename M2>
inline LessMap<M1, M2> lessMap(const M1 &m1, const M2 &m2) {
return LessMap<M1, M2>(m1,m2);
}
namespace _maps_bits {
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;
};
}
/// @}
/// \addtogroup maps
/// @{
/// \brief Writable bool map for logging each \c true assigned element
///
/// A \ref concepts::WriteMap "writable" bool map for logging
/// each \c true assigned element, i.e it copies subsequently each
/// keys set to \c true to the given iterator.
/// The most important usage of it is storing certain nodes or arcs
/// that were marked \c true by an algorithm.
///
/// There are several algorithms that provide solutions through bool
/// maps and most of them assign \c true at most once for each key.
/// In these cases it is a natural request to store each \c true
/// assigned elements (in order of the assignment), which can be
/// easily done with LoggerBoolMap.
///
/// The simplest way of using this map is through the loggerBoolMap()
/// function.
///
/// \tparam IT The type of the iterator.
/// \tparam KEY The key type of the map. The default value set
/// according to the iterator type should work in most cases.
///
/// \note The container of the iterator must contain enough space
/// for the elements or the iterator should be an inserter iterator.
#ifdef DOXYGEN
template <typename IT, typename KEY>
#else
template <typename IT,
typename KEY = typename _maps_bits::IteratorTraits<IT>::Value>
#endif
class LoggerBoolMap : public MapBase<KEY, bool> {
public:
///\e
typedef KEY Key;
///\e
typedef bool Value;
///\e
typedef IT Iterator;
/// Constructor
LoggerBoolMap(Iterator it)
: _begin(it), _end(it) {}
/// 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 set function of the map
void set(const Key& key, Value value) {
if (value) {
*_end++ = key;
}
}
private:
Iterator _begin;
Iterator _end;
};
/// Returns a \c LoggerBoolMap class
/// This function just returns a \c LoggerBoolMap class.
///
/// The most important usage of it is storing certain nodes or arcs
/// that were marked \c true by an algorithm.
/// For example, it makes easier to store the nodes in the processing
/// order of Dfs algorithm, as the following examples show.
/// \code
/// std::vector<Node> v;
/// dfs(g).processedMap(loggerBoolMap(std::back_inserter(v))).run(s);
/// \endcode
/// \code
/// std::vector<Node> v(countNodes(g));
/// dfs(g).processedMap(loggerBoolMap(v.begin())).run(s);
/// \endcode
///
/// \note The container of the iterator must contain enough space
/// for the elements or the iterator should be an inserter iterator.
///
/// \note LoggerBoolMap is just \ref concepts::WriteMap "writable", so
/// it cannot be used when a readable map is needed, for example, as
/// \c ReachedMap for \c Bfs, \c Dfs and \c Dijkstra algorithms.
///
/// \relates LoggerBoolMap
template<typename Iterator>
inline LoggerBoolMap<Iterator> loggerBoolMap(Iterator it) {
return LoggerBoolMap<Iterator>(it);
}
/// @}
/// \addtogroup graph_maps
/// @{
/// \brief Provides an immutable and unique id for each item in a graph.
///
/// IdMap provides a unique and immutable id for each item of the
/// same type (\c Node, \c Arc or \c Edge) in a graph. This id is
/// - \b unique: different items get different ids,
/// - \b immutable: the id of an item does not change (even if you
/// delete other nodes).
///
/// Using this map you get access (i.e. can read) the inner id values of
/// the items stored in the graph, which is returned by the \c id()
/// function of the graph. This map can be inverted with its member
/// class \c InverseMap or with the \c operator()() member.
///
/// \tparam GR The graph type.
/// \tparam K The key type of the map (\c GR::Node, \c GR::Arc or
/// \c GR::Edge).
///
/// \see RangeIdMap
template <typename GR, typename K>
class IdMap : public MapBase<K, int> {
public:
/// The graph type of IdMap.
typedef GR Graph;
typedef GR Digraph;
/// The key type of IdMap (\c Node, \c Arc or \c Edge).
typedef K Item;
/// The key type of IdMap (\c Node, \c Arc or \c Edge).
typedef K Key;
/// The value type of IdMap.
typedef int Value;
/// \brief Constructor.
///
/// Constructor of the map.
explicit IdMap(const Graph& graph) : _graph(&graph) {}
/// \brief Gives back the \e id of the item.
///
/// Gives back the immutable and unique \e id of the item.
int operator[](const Item& item) const { return _graph->id(item);}
/// \brief Gives back the \e item by its id.
///
/// Gives back the \e item by its id.
Item operator()(int id) { return _graph->fromId(id, Item()); }
private:
const Graph* _graph;
public:
/// \brief The inverse map type of IdMap.
///
/// The inverse map type of IdMap. The subscript operator gives back
/// an item by its id.
/// This type conforms to the \ref concepts::ReadMap "ReadMap" concept.
/// \see inverse()
class InverseMap {
public:
/// \brief Constructor.
///
/// Constructor for creating an id-to-item map.
explicit InverseMap(const Graph& graph) : _graph(&graph) {}
/// \brief Constructor.
///
/// Constructor for creating an id-to-item map.
explicit InverseMap(const IdMap& map) : _graph(map._graph) {}
/// \brief Gives back an item by its id.
///
/// Gives back an item by its id.
Item operator[](int id) const { return _graph->fromId(id, Item());}
private:
const Graph* _graph;
};
/// \brief Gives back the inverse of the map.
///
/// Gives back the inverse of the IdMap.
InverseMap inverse() const { return InverseMap(*_graph);}
};
/// \brief Returns an \c IdMap class.
///
/// This function just returns an \c IdMap class.
/// \relates IdMap
template <typename K, typename GR>
inline IdMap<GR, K> idMap(const GR& graph) {
return IdMap<GR, K>(graph);
}
/// \brief General cross reference graph map type.
/// This class provides simple invertable graph maps.
/// It wraps a standard graph map (\c NodeMap, \c ArcMap or \c EdgeMap)
/// and if a key is set to a new value, then stores it in the inverse map.
/// The graph items can be accessed by their values either using
/// \c InverseMap or \c operator()(), and the values of the map can be
/// accessed with an STL compatible forward iterator (\c ValueIt).
///
/// This map is intended to be used when all associated values are
/// different (the map is actually invertable) or there are only a few
/// items with the same value.
/// Otherwise consider to use \c IterableValueMap, which is more
/// suitable and more efficient for such cases. It provides iterators
/// to traverse the items with the same associated value, but
/// it does not have \c InverseMap.
///
/// This type is not reference map, so it cannot be modified with
/// the subscript operator.
///
/// \tparam GR The graph type.
/// \tparam K The key type of the map (\c GR::Node, \c GR::Arc or
/// \c GR::Edge).
/// \tparam V The value type of the map.
///
/// \see IterableValueMap
template <typename GR, typename K, typename V>
class CrossRefMap
: protected ItemSetTraits<GR, K>::template Map<V>::Type {
private:
typedef typename ItemSetTraits<GR, K>::
template Map<V>::Type Map;
typedef std::multimap<V, K> Container;
Container _inv_map;
public:
/// The graph type of CrossRefMap.
typedef GR Graph;
typedef GR Digraph;
/// The key type of CrossRefMap (\c Node, \c Arc or \c Edge).
typedef K Item;
/// The key type of CrossRefMap (\c Node, \c Arc or \c Edge).
typedef K Key;
/// The value type of CrossRefMap.
typedef V Value;
/// \brief Constructor.
///
/// Construct a new CrossRefMap for the given graph.
explicit CrossRefMap(const Graph& graph) : Map(graph) {}
/// \brief Forward iterator for values.
///
/// This iterator is an STL compatible forward
/// iterator on the values of the map. The values can
/// be accessed in the <tt>[beginValue, endValue)</tt> range.
/// They are considered with multiplicity, so each value is
/// traversed for each item it is assigned to.
class ValueIt
: public std::iterator<std::forward_iterator_tag, Value> {
friend class CrossRefMap;
private:
ValueIt(typename Container::const_iterator _it)
: it(_it) {}
public:
/// Constructor
ValueIt() {}
/// \e
ValueIt& operator++() { ++it; return *this; }
/// \e
ValueIt operator++(int) {
ValueIt tmp(*this);
operator++();
return tmp;
}
/// \e
const Value& operator*() const { return it->first; }
/// \e
const Value* operator->() const { return &(it->first); }
/// \e
bool operator==(ValueIt jt) const { return it == jt.it; }
/// \e
bool operator!=(ValueIt jt) const { return it != jt.it; }
private:
typename Container::const_iterator it;
};
/// Alias for \c ValueIt
typedef ValueIt ValueIterator;
/// \brief Returns an iterator to the first value.
///
/// Returns an STL compatible iterator to the
/// first value of the map. The values of the
/// map can be accessed in the <tt>[beginValue, endValue)</tt>
/// range.
ValueIt beginValue() const {
return ValueIt(_inv_map.begin());
}
/// \brief Returns an iterator after the last value.
///
/// Returns an STL compatible iterator after the
/// last value of the map. The values of the
/// map can be accessed in the <tt>[beginValue, endValue)</tt>
/// range.
ValueIt endValue() const {
return ValueIt(_inv_map.end());
}
/// \brief Sets the value associated with the given key.
///
/// Sets the value associated with the given key.
void set(const Key& key, const Value& val) {
Value oldval = Map::operator[](key);
typename Container::iterator it;
for (it = _inv_map.equal_range(oldval).first;
it != _inv_map.equal_range(oldval).second; ++it) {
if (it->second == key) {
_inv_map.erase(it);
break;
}
}
_inv_map.insert(std::make_pair(val, key));
Map::set(key, val);
}
/// \brief Returns the value associated with the given key.
///
/// Returns the value associated with the given key.
typename MapTraits<Map>::ConstReturnValue
operator[](const Key& key) const {
return Map::operator[](key);
}
/// \brief Gives back an item by its value.
///
/// This function gives back an item that is assigned to
/// the given value or \c INVALID if no such item exists.
/// If there are more items with the same associated value,
/// only one of them is returned.
Key operator()(const Value& val) const {
typename Container::const_iterator it = _inv_map.find(val);
return it != _inv_map.end() ? it->second : INVALID;
}
/// \brief Returns the number of items with the given value.
///
/// This function returns the number of items with the given value
/// associated with it.
int count(const Value &val) const {
return _inv_map.count(val);
}
protected:
/// \brief Erase the key from the map and the inverse map.
///
/// Erase the key from the map and the inverse map. It is called by the
/// \c AlterationNotifier.
virtual void erase(const Key& key) {
Value val = Map::operator[](key);
typename Container::iterator it;
for (it = _inv_map.equal_range(val).first;
it != _inv_map.equal_range(val).second; ++it) {
if (it->second == key) {
_inv_map.erase(it);
break;
}
}
Map::erase(key);
}
/// \brief Erase more keys from the map and the inverse map.
///
/// Erase more keys from the map and the inverse map. It is called by the
/// \c AlterationNotifier.
virtual void erase(const std::vector<Key>& keys) {
for (int i = 0; i < int(keys.size()); ++i) {
Value val = Map::operator[](keys[i]);
typename Container::iterator it;
for (it = _inv_map.equal_range(val).first;
it != _inv_map.equal_range(val).second; ++it) {
if (it->second == keys[i]) {
_inv_map.erase(it);
break;
}
}
}
Map::erase(keys);
}
/// \brief Clear the keys from the map and the inverse map.
///
/// Clear the keys from the map and the inverse map. It is called by the
/// \c AlterationNotifier.
virtual void clear() {
_inv_map.clear();
Map::clear();
}
public:
/// \brief The inverse map type of CrossRefMap.
///
/// The inverse map type of CrossRefMap. The subscript operator gives
/// back an item by its value.
/// This type conforms to the \ref concepts::ReadMap "ReadMap" concept.
/// \see inverse()
class InverseMap {
public:
/// \brief Constructor
///
/// Constructor of the InverseMap.
explicit InverseMap(const CrossRefMap& inverted)
: _inverted(inverted) {}
/// The value type of the InverseMap.
typedef typename CrossRefMap::Key Value;
/// The key type of the InverseMap.
typedef typename CrossRefMap::Value Key;
/// \brief Subscript operator.
///
/// Subscript operator. It gives back an item
/// that is assigned to the given value or \c INVALID
/// if no such item exists.
Value operator[](const Key& key) const {
return _inverted(key);
}
private:
const CrossRefMap& _inverted;
};
/// \brief Gives back the inverse of the map.
///
/// Gives back the inverse of the CrossRefMap.
InverseMap inverse() const {
return InverseMap(*this);
}
};
/// \brief Provides continuous and unique id for the
/// items of a graph.
///
/// RangeIdMap provides a unique and continuous
/// id for each item of a given type (\c Node, \c Arc or
/// \c Edge) in a graph. This id is
/// - \b unique: different items get different ids,
/// - \b continuous: the range of the ids is the set of integers
/// between 0 and \c n-1, where \c n is the number of the items of
/// this type (\c Node, \c Arc or \c Edge).
/// - So, the ids can change when deleting an item of the same type.
///
/// Thus this id is not (necessarily) the same as what can get using
/// the \c id() function of the graph or \ref IdMap.
/// This map can be inverted with its member class \c InverseMap,
/// or with the \c operator()() member.
///
/// \tparam GR The graph type.
/// \tparam K The key type of the map (\c GR::Node, \c GR::Arc or
/// \c GR::Edge).
///
/// \see IdMap
template <typename GR, typename K>
class RangeIdMap
: protected ItemSetTraits<GR, K>::template Map<int>::Type {
typedef typename ItemSetTraits<GR, K>::template Map<int>::Type Map;
public:
/// The graph type of RangeIdMap.
typedef GR Graph;
typedef GR Digraph;
/// The key type of RangeIdMap (\c Node, \c Arc or \c Edge).
typedef K Item;
/// The key type of RangeIdMap (\c Node, \c Arc or \c Edge).
typedef K Key;
/// The value type of RangeIdMap.
typedef int Value;
/// \brief Constructor.
///
/// Constructor.
explicit RangeIdMap(const Graph& gr) : Map(gr) {
Item it;
const typename Map::Notifier* nf = Map::notifier();
for (nf->first(it); it != INVALID; nf->next(it)) {
Map::set(it, _inv_map.size());
_inv_map.push_back(it);
}
}
protected:
/// \brief Adds a new key to the map.
///
/// Add a new key to the map. It is called by the
/// \c AlterationNotifier.
virtual void add(const Item& item) {
Map::add(item);
Map::set(item, _inv_map.size());
_inv_map.push_back(item);
}
/// \brief Add more new keys to the map.
///
/// Add more new keys to the map. It is called by the
/// \c AlterationNotifier.
virtual void add(const std::vector<Item>& items) {
Map::add(items);
for (int i = 0; i < int(items.size()); ++i) {
Map::set(items[i], _inv_map.size());
_inv_map.push_back(items[i]);
}
}
/// \brief Erase the key from the map.
///
/// Erase the key from the map. It is called by the
/// \c AlterationNotifier.
virtual void erase(const Item& item) {
Map::set(_inv_map.back(), Map::operator[](item));
_inv_map[Map::operator[](item)] = _inv_map.back();
_inv_map.pop_back();
Map::erase(item);
}
/// \brief Erase more keys from the map.
///
/// Erase more keys from the map. It is called by the
/// \c AlterationNotifier.
virtual void erase(const std::vector<Item>& items) {
for (int i = 0; i < int(items.size()); ++i) {
Map::set(_inv_map.back(), Map::operator[](items[i]));
_inv_map[Map::operator[](items[i])] = _inv_map.back();
_inv_map.pop_back();
}
Map::erase(items);
}
/// \brief Build the unique map.
///
/// Build the unique map. It is called by the
/// \c AlterationNotifier.
virtual void build() {
Map::build();
Item it;
const typename Map::Notifier* nf = Map::notifier();
for (nf->first(it); it != INVALID; nf->next(it)) {
Map::set(it, _inv_map.size());
_inv_map.push_back(it);
}
}
/// \brief Clear the keys from the map.
///
/// Clear the keys from the map. It is called by the
/// \c AlterationNotifier.
virtual void clear() {
_inv_map.clear();
Map::clear();
}
public:
/// \brief Returns the maximal value plus one.
///
/// Returns the maximal value plus one in the map.
unsigned int size() const {
return _inv_map.size();
}
/// \brief Swaps the position of the two items in the map.
///
/// Swaps the position of the two items in the map.
void swap(const Item& p, const Item& q) {
int pi = Map::operator[](p);
int qi = Map::operator[](q);
Map::set(p, qi);
_inv_map[qi] = p;
Map::set(q, pi);
_inv_map[pi] = q;
}
/// \brief Gives back the \e range \e id of the item
///
/// Gives back the \e range \e id of the item.
int operator[](const Item& item) const {
return Map::operator[](item);
}
/// \brief Gives back the item belonging to a \e range \e id
///
/// Gives back the item belonging to the given \e range \e id.
Item operator()(int id) const {
return _inv_map[id];
}
private:
typedef std::vector<Item> Container;
Container _inv_map;
public:
/// \brief The inverse map type of RangeIdMap.
///
/// The inverse map type of RangeIdMap. The subscript operator gives
/// back an item by its \e range \e id.
/// This type conforms to the \ref concepts::ReadMap "ReadMap" concept.
class InverseMap {
public:
/// \brief Constructor
///
/// Constructor of the InverseMap.
explicit InverseMap(const RangeIdMap& inverted)
: _inverted(inverted) {}
/// The value type of the InverseMap.
typedef typename RangeIdMap::Key Value;
/// The key type of the InverseMap.
typedef typename RangeIdMap::Value Key;
/// \brief Subscript operator.
///
/// Subscript operator. It gives back the item
/// that the given \e range \e id currently belongs to.
Value operator[](const Key& key) const {
return _inverted(key);
}
/// \brief Size of the map.
///
/// Returns the size of the map.
unsigned int size() const {
return _inverted.size();
}
private:
const RangeIdMap& _inverted;
};
/// \brief Gives back the inverse of the map.
///
/// Gives back the inverse of the RangeIdMap.
const InverseMap inverse() const {
return InverseMap(*this);
}
};
/// \brief Returns a \c RangeIdMap class.
///
/// This function just returns an \c RangeIdMap class.
/// \relates RangeIdMap
template <typename K, typename GR>
inline RangeIdMap<GR, K> rangeIdMap(const GR& graph) {
return RangeIdMap<GR, K>(graph);
}
/// \brief Dynamic iterable \c bool map.
///
/// This class provides a special graph map type which can store a
/// \c bool value for graph items (\c Node, \c Arc or \c Edge).
/// For both \c true and \c false values it is possible to iterate on
/// the keys mapped to the value.
///
/// This type is a reference map, so it can be modified with the
/// subscript operator.
///
/// \tparam GR The graph type.
/// \tparam K The key type of the map (\c GR::Node, \c GR::Arc or
/// \c GR::Edge).
///
/// \see IterableIntMap, IterableValueMap
/// \see CrossRefMap
template <typename GR, typename K>
class IterableBoolMap
: protected ItemSetTraits<GR, K>::template Map<int>::Type {
private:
typedef GR Graph;
typedef typename ItemSetTraits<GR, K>::ItemIt KeyIt;
typedef typename ItemSetTraits<GR, K>::template Map<int>::Type Parent;
std::vector<K> _array;
int _sep;
public:
/// Indicates that the map is reference map.
typedef True ReferenceMapTag;
/// The key type
typedef K Key;
/// The value type
typedef bool Value;
/// The const reference type.
typedef const Value& ConstReference;
private:
int position(const Key& key) const {
return Parent::operator[](key);
}
public:
/// \brief Reference to the value of the map.
///
/// This class is similar to the \c bool type. It can be converted to
/// \c bool and it provides the same operators.
class Reference {
friend class IterableBoolMap;
private:
Reference(IterableBoolMap& map, const Key& key)
: _key(key), _map(map) {}
public:
Reference& operator=(const Reference& value) {
_map.set(_key, static_cast<bool>(value));
return *this;
}
operator bool() const {
return static_cast<const IterableBoolMap&>(_map)[_key];
}
Reference& operator=(bool value) {
_map.set(_key, value);
return *this;
}
Reference& operator&=(bool value) {
_map.set(_key, _map[_key] & value);
return *this;
}
Reference& operator|=(bool value) {
_map.set(_key, _map[_key] | value);
return *this;
}
Reference& operator^=(bool value) {
_map.set(_key, _map[_key] ^ value);
return *this;
}
private:
Key _key;
IterableBoolMap& _map;
};
/// \brief Constructor of the map with a default value.
///
/// Constructor of the map with a default value.
explicit IterableBoolMap(const Graph& graph, bool def = false)
: Parent(graph) {
typename Parent::Notifier* nf = Parent::notifier();
Key it;
for (nf->first(it); it != INVALID; nf->next(it)) {
Parent::set(it, _array.size());
_array.push_back(it);
}
_sep = (def ? _array.size() : 0);
}
/// \brief Const subscript operator of the map.
///
/// Const subscript operator of the map.
bool operator[](const Key& key) const {
return position(key) < _sep;
}
/// \brief Subscript operator of the map.
///
/// Subscript operator of the map.
Reference operator[](const Key& key) {
return Reference(*this, key);
}
/// \brief Set operation of the map.
///
/// Set operation of the map.
void set(const Key& key, bool value) {
int pos = position(key);
if (value) {
if (pos < _sep) return;
Key tmp = _array[_sep];
_array[_sep] = key;
Parent::set(key, _sep);
_array[pos] = tmp;
Parent::set(tmp, pos);
++_sep;
} else {
if (pos >= _sep) return;
--_sep;
Key tmp = _array[_sep];
_array[_sep] = key;
Parent::set(key, _sep);
_array[pos] = tmp;
Parent::set(tmp, pos);
}
}
/// \brief Set all items.
///
/// Set all items in the map.
/// \note Constant time operation.
void setAll(bool value) {
_sep = (value ? _array.size() : 0);
}
/// \brief Returns the number of the keys mapped to \c true.
///
/// Returns the number of the keys mapped to \c true.
int trueNum() const {
return _sep;
}
/// \brief Returns the number of the keys mapped to \c false.
///
/// Returns the number of the keys mapped to \c false.
int falseNum() const {
return _array.size() - _sep;
}
/// \brief Iterator for the keys mapped to \c true.
///
/// Iterator for the keys mapped to \c true. It works
/// like a graph item iterator, it can be converted to
/// the key type of the map, incremented with \c ++ operator, and
/// if the iterator leaves the last valid key, it will be equal to
/// \c INVALID.
class TrueIt : public Key {
public:
typedef Key Parent;
/// \brief Creates an iterator.
///
/// Creates an iterator. It iterates on the
/// keys mapped to \c true.
/// \param map The IterableBoolMap.
explicit TrueIt(const IterableBoolMap& map)
: Parent(map._sep > 0 ? map._array[map._sep - 1] : INVALID),
_map(&map) {}
/// \brief Invalid constructor \& conversion.
///
/// This constructor initializes the iterator to be invalid.
/// \sa Invalid for more details.
TrueIt(Invalid) : Parent(INVALID), _map(0) {}
/// \brief Increment operator.
///
/// Increment operator.
TrueIt& operator++() {
int pos = _map->position(*this);
Parent::operator=(pos > 0 ? _map->_array[pos - 1] : INVALID);
return *this;
}
private:
const IterableBoolMap* _map;
};
/// \brief Iterator for the keys mapped to \c false.
///
/// Iterator for the keys mapped to \c false. It works
/// like a graph item iterator, it can be converted to
/// the key type of the map, incremented with \c ++ operator, and
/// if the iterator leaves the last valid key, it will be equal to
/// \c INVALID.
class FalseIt : public Key {
public:
typedef Key Parent;
/// \brief Creates an iterator.
///
/// Creates an iterator. It iterates on the
/// keys mapped to \c false.
/// \param map The IterableBoolMap.
explicit FalseIt(const IterableBoolMap& map)
: Parent(map._sep < int(map._array.size()) ?
map._array.back() : INVALID), _map(&map) {}
/// \brief Invalid constructor \& conversion.
///
/// This constructor initializes the iterator to be invalid.
/// \sa Invalid for more details.
FalseIt(Invalid) : Parent(INVALID), _map(0) {}
/// \brief Increment operator.
///
/// Increment operator.
FalseIt& operator++() {
int pos = _map->position(*this);
Parent::operator=(pos > _map->_sep ? _map->_array[pos - 1] : INVALID);
return *this;
}
private:
const IterableBoolMap* _map;
};
/// \brief Iterator for the keys mapped to a given value.
///
/// Iterator for the keys mapped to a given value. It works
/// like a graph item iterator, it can be converted to
/// the key type of the map, incremented with \c ++ operator, and
/// if the iterator leaves the last valid key, it will be equal to
/// \c INVALID.
class ItemIt : public Key {
public:
typedef Key Parent;
/// \brief Creates an iterator with a value.
///
/// Creates an iterator with a value. It iterates on the
/// keys mapped to the given value.
/// \param map The IterableBoolMap.
/// \param value The value.
ItemIt(const IterableBoolMap& map, bool value)
: Parent(value ?
(map._sep > 0 ?
map._array[map._sep - 1] : INVALID) :
(map._sep < int(map._array.size()) ?
map._array.back() : INVALID)), _map(&map) {}
/// \brief Invalid constructor \& conversion.
///
/// This constructor initializes the iterator to be invalid.
/// \sa Invalid for more details.
ItemIt(Invalid) : Parent(INVALID), _map(0) {}
/// \brief Increment operator.
///
/// Increment operator.
ItemIt& operator++() {
int pos = _map->position(*this);
int _sep = pos >= _map->_sep ? _map->_sep : 0;
Parent::operator=(pos > _sep ? _map->_array[pos - 1] : INVALID);
return *this;
}
private:
const IterableBoolMap* _map;
};
protected:
virtual void add(const Key& key) {
Parent::add(key);
Parent::set(key, _array.size());
_array.push_back(key);
}
virtual void add(const std::vector<Key>& keys) {
Parent::add(keys);
for (int i = 0; i < int(keys.size()); ++i) {
Parent::set(keys[i], _array.size());
_array.push_back(keys[i]);
}
}
virtual void erase(const Key& key) {
int pos = position(key);
if (pos < _sep) {
--_sep;
Parent::set(_array[_sep], pos);
_array[pos] = _array[_sep];
Parent::set(_array.back(), _sep);
_array[_sep] = _array.back();
_array.pop_back();
} else {
Parent::set(_array.back(), pos);
_array[pos] = _array.back();
_array.pop_back();
}
Parent::erase(key);
}
virtual void erase(const std::vector<Key>& keys) {
for (int i = 0; i < int(keys.size()); ++i) {
int pos = position(keys[i]);
if (pos < _sep) {
--_sep;
Parent::set(_array[_sep], pos);
_array[pos] = _array[_sep];
Parent::set(_array.back(), _sep);
_array[_sep] = _array.back();
_array.pop_back();
} else {
Parent::set(_array.back(), pos);
_array[pos] = _array.back();
_array.pop_back();
}
}
Parent::erase(keys);
}
virtual void build() {
Parent::build();
typename Parent::Notifier* nf = Parent::notifier();
Key it;
for (nf->first(it); it != INVALID; nf->next(it)) {
Parent::set(it, _array.size());
_array.push_back(it);
}
_sep = 0;
}
virtual void clear() {
_array.clear();
_sep = 0;
Parent::clear();
}
};
namespace _maps_bits {
template <typename Item>
struct IterableIntMapNode {
IterableIntMapNode() : value(-1) {}
IterableIntMapNode(int _value) : value(_value) {}
Item prev, next;
int value;
};
}
/// \brief Dynamic iterable integer map.
///
/// This class provides a special graph map type which can store an
/// integer value for graph items (\c Node, \c Arc or \c Edge).
/// For each non-negative value it is possible to iterate on the keys
/// mapped to the value.
///
/// This map is intended to be used with small integer values, for which
/// it is efficient, and supports iteration only for non-negative values.
/// If you need large values and/or iteration for negative integers,
/// consider to use \ref IterableValueMap instead.
///
/// This type is a reference map, so it can be modified with the
/// subscript operator.
///
/// \note The size of the data structure depends on the largest
/// value in the map.
///
/// \tparam GR The graph type.
/// \tparam K The key type of the map (\c GR::Node, \c GR::Arc or
/// \c GR::Edge).
///
/// \see IterableBoolMap, IterableValueMap
/// \see CrossRefMap
template <typename GR, typename K>
class IterableIntMap
: protected ItemSetTraits<GR, K>::
template Map<_maps_bits::IterableIntMapNode<K> >::Type {
public:
typedef typename ItemSetTraits<GR, K>::
template Map<_maps_bits::IterableIntMapNode<K> >::Type Parent;
/// The key type
typedef K Key;
/// The value type
typedef int Value;
/// The graph type
typedef GR Graph;
/// \brief Constructor of the map.
///
/// Constructor of the map. It sets all values to -1.
explicit IterableIntMap(const Graph& graph)
: Parent(graph) {}
/// \brief Constructor of the map with a given value.
///
/// Constructor of the map with a given value.
explicit IterableIntMap(const Graph& graph, int value)
: Parent(graph, _maps_bits::IterableIntMapNode<K>(value)) {
if (value >= 0) {
for (typename Parent::ItemIt it(*this); it != INVALID; ++it) {
lace(it);
}
}
}
private:
void unlace(const Key& key) {
typename Parent::Value& node = Parent::operator[](key);
if (node.value < 0) return;
if (node.prev != INVALID) {
Parent::operator[](node.prev).next = node.next;
} else {
_first[node.value] = node.next;
}
if (node.next != INVALID) {
Parent::operator[](node.next).prev = node.prev;
}
while (!_first.empty() && _first.back() == INVALID) {
_first.pop_back();
}
}
void lace(const Key& key) {
typename Parent::Value& node = Parent::operator[](key);
if (node.value < 0) return;
if (node.value >= int(_first.size())) {
_first.resize(node.value + 1, INVALID);
}
node.prev = INVALID;
node.next = _first[node.value];
if (node.next != INVALID) {
Parent::operator[](node.next).prev = key;
}
_first[node.value] = key;
}
public:
/// Indicates that the map is reference map.
typedef True ReferenceMapTag;
/// \brief Reference to the value of the map.
///
/// This class is similar to the \c int type. It can
/// be converted to \c int and it has the same operators.
class Reference {
friend class IterableIntMap;
private:
Reference(IterableIntMap& map, const Key& key)
: _key(key), _map(map) {}
public:
Reference& operator=(const Reference& value) {
_map.set(_key, static_cast<const int&>(value));
return *this;
}
operator const int&() const {
return static_cast<const IterableIntMap&>(_map)[_key];
}
Reference& operator=(int value) {
_map.set(_key, value);
return *this;
}
Reference& operator++() {
_map.set(_key, _map[_key] + 1);
return *this;
}
int operator++(int) {
int value = _map[_key];
_map.set(_key, value + 1);
return value;
}
Reference& operator--() {
_map.set(_key, _map[_key] - 1);
return *this;
}
int operator--(int) {
int value = _map[_key];
_map.set(_key, value - 1);
return value;
}
Reference& operator+=(int value) {
_map.set(_key, _map[_key] + value);
return *this;
}
Reference& operator-=(int value) {
_map.set(_key, _map[_key] - value);
return *this;
}
Reference& operator*=(int value) {
_map.set(_key, _map[_key] * value);
return *this;
}
Reference& operator/=(int value) {
_map.set(_key, _map[_key] / value);
return *this;
}
Reference& operator%=(int value) {
_map.set(_key, _map[_key] % value);
return *this;
}
Reference& operator&=(int value) {
_map.set(_key, _map[_key] & value);
return *this;
}
Reference& operator|=(int value) {
_map.set(_key, _map[_key] | value);
return *this;
}
Reference& operator^=(int value) {
_map.set(_key, _map[_key] ^ value);
return *this;
}
Reference& operator<<=(int value) {
_map.set(_key, _map[_key] << value);
return *this;
}
Reference& operator>>=(int value) {
_map.set(_key, _map[_key] >> value);
return *this;
}
private:
Key _key;
IterableIntMap& _map;
};
/// The const reference type.
typedef const Value& ConstReference;
/// \brief Gives back the maximal value plus one.
///
/// Gives back the maximal value plus one.
int size() const {
return _first.size();
}
/// \brief Set operation of the map.
///
/// Set operation of the map.
void set(const Key& key, const Value& value) {
unlace(key);
Parent::operator[](key).value = value;
lace(key);
}
/// \brief Const subscript operator of the map.
///
/// Const subscript operator of the map.
const Value& operator[](const Key& key) const {
return Parent::operator[](key).value;
}
/// \brief Subscript operator of the map.
///
/// Subscript operator of the map.
Reference operator[](const Key& key) {
return Reference(*this, key);
}
/// \brief Iterator for the keys with the same value.
///
/// Iterator for the keys with the same value. It works
/// like a graph item iterator, it can be converted to
/// the item type of the map, incremented with \c ++ operator, and
/// if the iterator leaves the last valid item, it will be equal to
/// \c INVALID.
class ItemIt : public Key {
public:
typedef Key Parent;
/// \brief Invalid constructor \& conversion.
///
/// This constructor initializes the iterator to be invalid.
/// \sa Invalid for more details.
ItemIt(Invalid) : Parent(INVALID), _map(0) {}
/// \brief Creates an iterator with a value.
///
/// Creates an iterator with a value. It iterates on the
/// keys mapped to the given value.
/// \param map The IterableIntMap.
/// \param value The value.
ItemIt(const IterableIntMap& map, int value) : _map(&map) {
if (value < 0 || value >= int(_map->_first.size())) {
Parent::operator=(INVALID);
} else {
Parent::operator=(_map->_first[value]);
}
}
/// \brief Increment operator.
///
/// Increment operator.
ItemIt& operator++() {
Parent::operator=(_map->IterableIntMap::Parent::
operator[](static_cast<Parent&>(*this)).next);
return *this;
}
private:
const IterableIntMap* _map;
};
protected:
virtual void erase(const Key& key) {
unlace(key);
Parent::erase(key);
}
virtual void erase(const std::vector<Key>& keys) {
for (int i = 0; i < int(keys.size()); ++i) {
unlace(keys[i]);
}
Parent::erase(keys);
}
virtual void clear() {
_first.clear();
Parent::clear();
}
private:
std::vector<Key> _first;
};
namespace _maps_bits {
template <typename Item, typename Value>
struct IterableValueMapNode {
IterableValueMapNode(Value _value = Value()) : value(_value) {}
Item prev, next;
Value value;
};
}
/// \brief Dynamic iterable map for comparable values.
///
/// This class provides a special graph map type which can store a
/// comparable value for graph items (\c Node, \c Arc or \c Edge).
/// For each value it is possible to iterate on the keys mapped to
/// the value (\c ItemIt), and the values of the map can be accessed
/// with an STL compatible forward iterator (\c ValueIt).
/// The map stores a linked list for each value, which contains
/// the items mapped to the value, and the used values are stored
/// in balanced binary tree (\c std::map).
///
/// \ref IterableBoolMap and \ref IterableIntMap are similar classes
/// specialized for \c bool and \c int values, respectively.
///
/// This type is not reference map, so it cannot be modified with
/// the subscript operator.
///
/// \tparam GR The graph type.
/// \tparam K The key type of the map (\c GR::Node, \c GR::Arc or
/// \c GR::Edge).
/// \tparam V The value type of the map. It can be any comparable
/// value type.
///
/// \see IterableBoolMap, IterableIntMap
/// \see CrossRefMap
template <typename GR, typename K, typename V>
class IterableValueMap
: protected ItemSetTraits<GR, K>::
template Map<_maps_bits::IterableValueMapNode<K, V> >::Type {
public:
typedef typename ItemSetTraits<GR, K>::
template Map<_maps_bits::IterableValueMapNode<K, V> >::Type Parent;
/// The key type
typedef K Key;
/// The value type
typedef V Value;
/// The graph type
typedef GR Graph;
public:
/// \brief Constructor of the map with a given value.
///
/// Constructor of the map with a given value.
explicit IterableValueMap(const Graph& graph,
const Value& value = Value())
: Parent(graph, _maps_bits::IterableValueMapNode<K, V>(value)) {
for (typename Parent::ItemIt it(*this); it != INVALID; ++it) {
lace(it);
}
}
protected:
void unlace(const Key& key) {
typename Parent::Value& node = Parent::operator[](key);
if (node.prev != INVALID) {
Parent::operator[](node.prev).next = node.next;
} else {
if (node.next != INVALID) {
_first[node.value] = node.next;
} else {
_first.erase(node.value);
}
}
if (node.next != INVALID) {
Parent::operator[](node.next).prev = node.prev;
}
}
void lace(const Key& key) {
typename Parent::Value& node = Parent::operator[](key);
typename std::map<Value, Key>::iterator it = _first.find(node.value);
if (it == _first.end()) {
node.prev = node.next = INVALID;
_first.insert(std::make_pair(node.value, key));
} else {
node.prev = INVALID;
node.next = it->second;
if (node.next != INVALID) {
Parent::operator[](node.next).prev = key;
}
it->second = key;
}
}
public:
/// \brief Forward iterator for values.
///
/// This iterator is an STL compatible forward
/// iterator on the values of the map. The values can
/// be accessed in the <tt>[beginValue, endValue)</tt> range.
class ValueIt
: public std::iterator<std::forward_iterator_tag, Value> {
friend class IterableValueMap;
private:
ValueIt(typename std::map<Value, Key>::const_iterator _it)
: it(_it) {}
public:
/// Constructor
ValueIt() {}
/// \e
ValueIt& operator++() { ++it; return *this; }
/// \e
ValueIt operator++(int) {
ValueIt tmp(*this);
operator++();
return tmp;
}
/// \e
const Value& operator*() const { return it->first; }
/// \e
const Value* operator->() const { return &(it->first); }
/// \e
bool operator==(ValueIt jt) const { return it == jt.it; }
/// \e
bool operator!=(ValueIt jt) const { return it != jt.it; }
private:
typename std::map<Value, Key>::const_iterator it;
};
/// \brief Returns an iterator to the first value.
///
/// Returns an STL compatible iterator to the
/// first value of the map. The values of the
/// map can be accessed in the <tt>[beginValue, endValue)</tt>
/// range.
ValueIt beginValue() const {
return ValueIt(_first.begin());
}
/// \brief Returns an iterator after the last value.
///
/// Returns an STL compatible iterator after the
/// last value of the map. The values of the
/// map can be accessed in the <tt>[beginValue, endValue)</tt>
/// range.
ValueIt endValue() const {
return ValueIt(_first.end());
}
/// \brief Set operation of the map.
///
/// Set operation of the map.
void set(const Key& key, const Value& value) {
unlace(key);
Parent::operator[](key).value = value;
lace(key);
}
/// \brief Const subscript operator of the map.
///
/// Const subscript operator of the map.
const Value& operator[](const Key& key) const {
return Parent::operator[](key).value;
}
/// \brief Iterator for the keys with the same value.
///
/// Iterator for the keys with the same value. It works
/// like a graph item iterator, it can be converted to
/// the item type of the map, incremented with \c ++ operator, and
/// if the iterator leaves the last valid item, it will be equal to
/// \c INVALID.
class ItemIt : public Key {
public:
typedef Key Parent;
/// \brief Invalid constructor \& conversion.
///
/// This constructor initializes the iterator to be invalid.
/// \sa Invalid for more details.
ItemIt(Invalid) : Parent(INVALID), _map(0) {}
/// \brief Creates an iterator with a value.
///
/// Creates an iterator with a value. It iterates on the
/// keys which have the given value.
/// \param map The IterableValueMap
/// \param value The value
ItemIt(const IterableValueMap& map, const Value& value) : _map(&map) {
typename std::map<Value, Key>::const_iterator it =
map._first.find(value);
if (it == map._first.end()) {
Parent::operator=(INVALID);
} else {
Parent::operator=(it->second);
}
}
/// \brief Increment operator.
///
/// Increment Operator.
ItemIt& operator++() {
Parent::operator=(_map->IterableValueMap::Parent::
operator[](static_cast<Parent&>(*this)).next);
return *this;
}
private:
const IterableValueMap* _map;
};
protected:
virtual void add(const Key& key) {
Parent::add(key);
unlace(key);
}
virtual void add(const std::vector<Key>& keys) {
Parent::add(keys);
for (int i = 0; i < int(keys.size()); ++i) {
lace(keys[i]);
}
}
virtual void erase(const Key& key) {
unlace(key);
Parent::erase(key);
}
virtual void erase(const std::vector<Key>& keys) {
for (int i = 0; i < int(keys.size()); ++i) {
unlace(keys[i]);
}
Parent::erase(keys);
}
virtual void build() {
Parent::build();
for (typename Parent::ItemIt it(*this); it != INVALID; ++it) {
lace(it);
}
}
virtual void clear() {
_first.clear();
Parent::clear();
}
private:
std::map<Value, Key> _first;
};
/// \brief Map of the source nodes of arcs in a digraph.
///
/// SourceMap provides access for the source node of each arc in a digraph,
/// which is returned by the \c source() function of the digraph.
/// \tparam GR The digraph type.
/// \see TargetMap
template <typename GR>
class SourceMap {
public:
/// The key type (the \c Arc type of the digraph).
typedef typename GR::Arc Key;
/// The value type (the \c Node type of the digraph).
typedef typename GR::Node Value;
/// \brief Constructor
///
/// Constructor.
/// \param digraph The digraph that the map belongs to.
explicit SourceMap(const GR& digraph) : _graph(digraph) {}
/// \brief Returns the source node of the given arc.
///
/// Returns the source node of the given arc.
Value operator[](const Key& arc) const {
return _graph.source(arc);
}
private:
const GR& _graph;
};
/// \brief Returns a \c SourceMap class.
///
/// This function just returns an \c SourceMap class.
/// \relates SourceMap
template <typename GR>
inline SourceMap<GR> sourceMap(const GR& graph) {
return SourceMap<GR>(graph);
}
/// \brief Map of the target nodes of arcs in a digraph.
///
/// TargetMap provides access for the target node of each arc in a digraph,
/// which is returned by the \c target() function of the digraph.
/// \tparam GR The digraph type.
/// \see SourceMap
template <typename GR>
class TargetMap {
public:
/// The key type (the \c Arc type of the digraph).
typedef typename GR::Arc Key;
/// The value type (the \c Node type of the digraph).
typedef typename GR::Node Value;
/// \brief Constructor
///
/// Constructor.
/// \param digraph The digraph that the map belongs to.
explicit TargetMap(const GR& digraph) : _graph(digraph) {}
/// \brief Returns the target node of the given arc.
///
/// Returns the target node of the given arc.
Value operator[](const Key& e) const {
return _graph.target(e);
}
private:
const GR& _graph;
};
/// \brief Returns a \c TargetMap class.
///
/// This function just returns a \c TargetMap class.
/// \relates TargetMap
template <typename GR>
inline TargetMap<GR> targetMap(const GR& graph) {
return TargetMap<GR>(graph);
}
/// \brief Map of the "forward" directed arc view of edges in a graph.
///
/// ForwardMap provides access for the "forward" directed arc view of
/// each edge in a graph, which is returned by the \c direct() function
/// of the graph with \c true parameter.
/// \tparam GR The graph type.
/// \see BackwardMap
template <typename GR>
class ForwardMap {
public:
/// The key type (the \c Edge type of the digraph).
typedef typename GR::Edge Key;
/// The value type (the \c Arc type of the digraph).
typedef typename GR::Arc Value;
/// \brief Constructor
///
/// Constructor.
/// \param graph The graph that the map belongs to.
explicit ForwardMap(const GR& graph) : _graph(graph) {}
/// \brief Returns the "forward" directed arc view of the given edge.
///
/// Returns the "forward" directed arc view of the given edge.
Value operator[](const Key& key) const {
return _graph.direct(key, true);
}
private:
const GR& _graph;
};
/// \brief Returns a \c ForwardMap class.
///
/// This function just returns an \c ForwardMap class.
/// \relates ForwardMap
template <typename GR>
inline ForwardMap<GR> forwardMap(const GR& graph) {
return ForwardMap<GR>(graph);
}
/// \brief Map of the "backward" directed arc view of edges in a graph.
///
/// BackwardMap provides access for the "backward" directed arc view of
/// each edge in a graph, which is returned by the \c direct() function
/// of the graph with \c false parameter.
/// \tparam GR The graph type.
/// \see ForwardMap
template <typename GR>
class BackwardMap {
public:
/// The key type (the \c Edge type of the digraph).
typedef typename GR::Edge Key;
/// The value type (the \c Arc type of the digraph).
typedef typename GR::Arc Value;
/// \brief Constructor
///
/// Constructor.
/// \param graph The graph that the map belongs to.
explicit BackwardMap(const GR& graph) : _graph(graph) {}
/// \brief Returns the "backward" directed arc view of the given edge.
///
/// Returns the "backward" directed arc view of the given edge.
Value operator[](const Key& key) const {
return _graph.direct(key, false);
}
private:
const GR& _graph;
};
/// \brief Returns a \c BackwardMap class
/// This function just returns a \c BackwardMap class.
/// \relates BackwardMap
template <typename GR>
inline BackwardMap<GR> backwardMap(const GR& graph) {
return BackwardMap<GR>(graph);
}
/// \brief Map of the in-degrees of nodes in a digraph.
///
/// This map returns the in-degree of a node. Once it is constructed,
/// the degrees are stored in a standard \c NodeMap, so each query is done
/// in constant time. On the other hand, the values are updated automatically
/// whenever the digraph changes.
///
/// \warning Besides \c addNode() and \c addArc(), a digraph structure
/// may provide alternative ways to modify the digraph.
/// The correct behavior of InDegMap is not guarantied if these additional
/// features are used. For example, the functions
/// \ref ListDigraph::changeSource() "changeSource()",
/// \ref ListDigraph::changeTarget() "changeTarget()" and
/// \ref ListDigraph::reverseArc() "reverseArc()"
/// of \ref ListDigraph will \e not update the degree values correctly.
///
/// \sa OutDegMap
template <typename GR>
class InDegMap
: protected ItemSetTraits<GR, typename GR::Arc>
::ItemNotifier::ObserverBase {
public:
/// The graph type of InDegMap
typedef GR Graph;
typedef GR Digraph;
/// The key type
typedef typename Digraph::Node Key;
/// The value type
typedef int Value;
typedef typename ItemSetTraits<Digraph, typename Digraph::Arc>
::ItemNotifier::ObserverBase Parent;
private:
class AutoNodeMap
: public ItemSetTraits<Digraph, Key>::template Map<int>::Type {
public:
typedef typename ItemSetTraits<Digraph, Key>::
template Map<int>::Type Parent;
AutoNodeMap(const Digraph& digraph) : Parent(digraph, 0) {}
virtual void add(const Key& key) {
Parent::add(key);
Parent::set(key, 0);
}
virtual void add(const std::vector<Key>& keys) {
Parent::add(keys);
for (int i = 0; i < int(keys.size()); ++i) {
Parent::set(keys[i], 0);
}
}
virtual void build() {
Parent::build();
Key it;
typename Parent::Notifier* nf = Parent::notifier();
for (nf->first(it); it != INVALID; nf->next(it)) {
Parent::set(it, 0);
}
}
};
public:
/// \brief Constructor.
///
/// Constructor for creating an in-degree map.
explicit InDegMap(const Digraph& graph)
: _digraph(graph), _deg(graph) {
Parent::attach(_digraph.notifier(typename Digraph::Arc()));
for(typename Digraph::NodeIt it(_digraph); it != INVALID; ++it) {
_deg[it] = countInArcs(_digraph, it);
}
}
/// \brief Gives back the in-degree of a Node.
///
/// Gives back the in-degree of a Node.
int operator[](const Key& key) const {
return _deg[key];
}
protected:
typedef typename Digraph::Arc Arc;
virtual void add(const Arc& arc) {
++_deg[_digraph.target(arc)];
}
virtual void add(const std::vector<Arc>& arcs) {
for (int i = 0; i < int(arcs.size()); ++i) {
++_deg[_digraph.target(arcs[i])];
}
}
virtual void erase(const Arc& arc) {
--_deg[_digraph.target(arc)];
}
virtual void erase(const std::vector<Arc>& arcs) {
for (int i = 0; i < int(arcs.size()); ++i) {
--_deg[_digraph.target(arcs[i])];
}
}
virtual void build() {
for(typename Digraph::NodeIt it(_digraph); it != INVALID; ++it) {
_deg[it] = countInArcs(_digraph, it);
}
}
virtual void clear() {
for(typename Digraph::NodeIt it(_digraph); it != INVALID; ++it) {
_deg[it] = 0;
}
}
private:
const Digraph& _digraph;
AutoNodeMap _deg;
};
/// \brief Map of the out-degrees of nodes in a digraph.
///
/// This map returns the out-degree of a node. Once it is constructed,
/// the degrees are stored in a standard \c NodeMap, so each query is done
/// in constant time. On the other hand, the values are updated automatically
/// whenever the digraph changes.
///
/// \warning Besides \c addNode() and \c addArc(), a digraph structure
/// may provide alternative ways to modify the digraph.
/// The correct behavior of OutDegMap is not guarantied if these additional
/// features are used. For example, the functions
/// \ref ListDigraph::changeSource() "changeSource()",
/// \ref ListDigraph::changeTarget() "changeTarget()" and
/// \ref ListDigraph::reverseArc() "reverseArc()"
/// of \ref ListDigraph will \e not update the degree values correctly.
///
/// \sa InDegMap
template <typename GR>
class OutDegMap
: protected ItemSetTraits<GR, typename GR::Arc>
::ItemNotifier::ObserverBase {
public:
/// The graph type of OutDegMap
typedef GR Graph;
typedef GR Digraph;
/// The key type
typedef typename Digraph::Node Key;
/// The value type
typedef int Value;
typedef typename ItemSetTraits<Digraph, typename Digraph::Arc>
::ItemNotifier::ObserverBase Parent;
private:
class AutoNodeMap
: public ItemSetTraits<Digraph, Key>::template Map<int>::Type {
public:
typedef typename ItemSetTraits<Digraph, Key>::
template Map<int>::Type Parent;
AutoNodeMap(const Digraph& digraph) : Parent(digraph, 0) {}
virtual void add(const Key& key) {
Parent::add(key);
Parent::set(key, 0);
}
virtual void add(const std::vector<Key>& keys) {
Parent::add(keys);
for (int i = 0; i < int(keys.size()); ++i) {
Parent::set(keys[i], 0);
}
}
virtual void build() {
Parent::build();
Key it;
typename Parent::Notifier* nf = Parent::notifier();
for (nf->first(it); it != INVALID; nf->next(it)) {
Parent::set(it, 0);
}
}
};
public:
/// \brief Constructor.
///
/// Constructor for creating an out-degree map.
explicit OutDegMap(const Digraph& graph)
: _digraph(graph), _deg(graph) {
Parent::attach(_digraph.notifier(typename Digraph::Arc()));
for(typename Digraph::NodeIt it(_digraph); it != INVALID; ++it) {
_deg[it] = countOutArcs(_digraph, it);
}
}
/// \brief Gives back the out-degree of a Node.
///
/// Gives back the out-degree of a Node.
int operator[](const Key& key) const {
return _deg[key];
}
protected:
typedef typename Digraph::Arc Arc;
virtual void add(const Arc& arc) {
++_deg[_digraph.source(arc)];
}
virtual void add(const std::vector<Arc>& arcs) {
for (int i = 0; i < int(arcs.size()); ++i) {
++_deg[_digraph.source(arcs[i])];
}
}
virtual void erase(const Arc& arc) {
--_deg[_digraph.source(arc)];
}
virtual void erase(const std::vector<Arc>& arcs) {
for (int i = 0; i < int(arcs.size()); ++i) {
--_deg[_digraph.source(arcs[i])];
}
}
virtual void build() {
for(typename Digraph::NodeIt it(_digraph); it != INVALID; ++it) {
_deg[it] = countOutArcs(_digraph, it);
}
}
virtual void clear() {
for(typename Digraph::NodeIt it(_digraph); it != INVALID; ++it) {
_deg[it] = 0;
}
}
private:
const Digraph& _digraph;
AutoNodeMap _deg;
};
/// \brief Potential difference map
///
/// PotentialDifferenceMap returns the difference between the potentials of
/// the source and target nodes of each arc in a digraph, i.e. it returns
/// \code
/// potential[gr.target(arc)] - potential[gr.source(arc)].
/// \endcode
/// \tparam GR The digraph type.
/// \tparam POT A node map storing the potentials.
template <typename GR, typename POT>
class PotentialDifferenceMap {
public:
/// Key type
typedef typename GR::Arc Key;
/// Value type
typedef typename POT::Value Value;
/// \brief Constructor
///
/// Contructor of the map.
explicit PotentialDifferenceMap(const GR& gr,
const POT& potential)
: _digraph(gr), _potential(potential) {}
/// \brief Returns the potential difference for the given arc.
///
/// Returns the potential difference for the given arc, i.e.
/// \code
/// potential[gr.target(arc)] - potential[gr.source(arc)].
/// \endcode
Value operator[](const Key& arc) const {
return _potential[_digraph.target(arc)] -
_potential[_digraph.source(arc)];
}
private:
const GR& _digraph;
const POT& _potential;
};
/// \brief Returns a PotentialDifferenceMap.
///
/// This function just returns a PotentialDifferenceMap.
/// \relates PotentialDifferenceMap
template <typename GR, typename POT>
PotentialDifferenceMap<GR, POT>
potentialDifferenceMap(const GR& gr, const POT& potential) {
return PotentialDifferenceMap<GR, POT>(gr, potential);
}
/// \brief Copy the values of a graph map to another map.
///
/// This function copies the values of a graph map to another graph map.
/// \c To::Key must be equal or convertible to \c From::Key and
/// \c From::Value must be equal or convertible to \c To::Value.
///
/// For example, an edge map of \c int value type can be copied to
/// an arc map of \c double value type in an undirected graph, but
/// an arc map cannot be copied to an edge map.
/// Note that even a \ref ConstMap can be copied to a standard graph map,
/// but \ref mapFill() can also be used for this purpose.
///
/// \param gr The graph for which the maps are defined.
/// \param from The map from which the values have to be copied.
/// It must conform to the \ref concepts::ReadMap "ReadMap" concept.
/// \param to The map to which the values have to be copied.
/// It must conform to the \ref concepts::WriteMap "WriteMap" concept.
template <typename GR, typename From, typename To>
void mapCopy(const GR& gr, const From& from, To& to) {
typedef typename To::Key Item;
typedef typename ItemSetTraits<GR, Item>::ItemIt ItemIt;
for (ItemIt it(gr); it != INVALID; ++it) {
to.set(it, from[it]);
}
}
/// \brief Compare two graph maps.
///
/// This function compares the values of two graph maps. It returns
/// \c true if the maps assign the same value for all items in the graph.
/// The \c Key type of the maps (\c Node, \c Arc or \c Edge) must be equal
/// and their \c Value types must be comparable using \c %operator==().
///
/// \param gr The graph for which the maps are defined.
/// \param map1 The first map.
/// \param map2 The second map.
template <typename GR, typename Map1, typename Map2>
bool mapCompare(const GR& gr, const Map1& map1, const Map2& map2) {
typedef typename Map2::Key Item;
typedef typename ItemSetTraits<GR, Item>::ItemIt ItemIt;
for (ItemIt it(gr); it != INVALID; ++it) {
if (!(map1[it] == map2[it])) return false;
}
return true;
}
/// \brief Return an item having minimum value of a graph map.
///
/// This function returns an item (\c Node, \c Arc or \c Edge) having
/// minimum value of the given graph map.
/// If the item set is empty, it returns \c INVALID.
///
/// \param gr The graph for which the map is defined.
/// \param map The graph map.
template <typename GR, typename Map>
typename Map::Key mapMin(const GR& gr, const Map& map) {
return mapMin(gr, map, std::less<typename Map::Value>());
}
/// \brief Return an item having minimum value of a graph map.
///
/// This function returns an item (\c Node, \c Arc or \c Edge) having
/// minimum value of the given graph map.
/// If the item set is empty, it returns \c INVALID.
///
/// \param gr The graph for which the map is defined.
/// \param map The graph map.
/// \param comp Comparison function object.
template <typename GR, typename Map, typename Comp>
typename Map::Key mapMin(const GR& gr, const Map& map, const Comp& comp) {
typedef typename Map::Key Item;
typedef typename Map::Value Value;
typedef typename ItemSetTraits<GR, Item>::ItemIt ItemIt;
ItemIt min_item(gr);
if (min_item == INVALID) return INVALID;
Value min = map[min_item];
for (ItemIt it(gr); it != INVALID; ++it) {
if (comp(map[it], min)) {
min = map[it];
min_item = it;
}
}
return min_item;
}
/// \brief Return an item having maximum value of a graph map.
///
/// This function returns an item (\c Node, \c Arc or \c Edge) having
/// maximum value of the given graph map.
/// If the item set is empty, it returns \c INVALID.
///
/// \param gr The graph for which the map is defined.
/// \param map The graph map.
template <typename GR, typename Map>
typename Map::Key mapMax(const GR& gr, const Map& map) {
return mapMax(gr, map, std::less<typename Map::Value>());
}
/// \brief Return an item having maximum value of a graph map.
///
/// This function returns an item (\c Node, \c Arc or \c Edge) having
/// maximum value of the given graph map.
/// If the item set is empty, it returns \c INVALID.
///
/// \param gr The graph for which the map is defined.
/// \param map The graph map.
/// \param comp Comparison function object.
template <typename GR, typename Map, typename Comp>
typename Map::Key mapMax(const GR& gr, const Map& map, const Comp& comp) {
typedef typename Map::Key Item;
typedef typename Map::Value Value;
typedef typename ItemSetTraits<GR, Item>::ItemIt ItemIt;
ItemIt max_item(gr);
if (max_item == INVALID) return INVALID;
Value max = map[max_item];
for (ItemIt it(gr); it != INVALID; ++it) {
if (comp(max, map[it])) {
max = map[it];
max_item = it;
}
}
return max_item;
}
/// \brief Return the minimum value of a graph map.
///
/// This function returns the minimum value of the given graph map.
/// The corresponding item set of the graph must not be empty.
///
/// \param gr The graph for which the map is defined.
/// \param map The graph map.
template <typename GR, typename Map>
typename Map::Value mapMinValue(const GR& gr, const Map& map) {
return map[mapMin(gr, map, std::less<typename Map::Value>())];
}
/// \brief Return the minimum value of a graph map.
///
/// This function returns the minimum value of the given graph map.
/// The corresponding item set of the graph must not be empty.
///
/// \param gr The graph for which the map is defined.
/// \param map The graph map.
/// \param comp Comparison function object.
template <typename GR, typename Map, typename Comp>
typename Map::Value
mapMinValue(const GR& gr, const Map& map, const Comp& comp) {
return map[mapMin(gr, map, comp)];
}
/// \brief Return the maximum value of a graph map.
///
/// This function returns the maximum value of the given graph map.
/// The corresponding item set of the graph must not be empty.
///
/// \param gr The graph for which the map is defined.
/// \param map The graph map.
template <typename GR, typename Map>
typename Map::Value mapMaxValue(const GR& gr, const Map& map) {
return map[mapMax(gr, map, std::less<typename Map::Value>())];
}
/// \brief Return the maximum value of a graph map.
///
/// This function returns the maximum value of the given graph map.
/// The corresponding item set of the graph must not be empty.
///
/// \param gr The graph for which the map is defined.
/// \param map The graph map.
/// \param comp Comparison function object.
template <typename GR, typename Map, typename Comp>
typename Map::Value
mapMaxValue(const GR& gr, const Map& map, const Comp& comp) {
return map[mapMax(gr, map, comp)];
}
/// \brief Return an item having a specified value in a graph map.
///
/// This function returns an item (\c Node, \c Arc or \c Edge) having
/// the specified assigned value in the given graph map.
/// If no such item exists, it returns \c INVALID.
///
/// \param gr The graph for which the map is defined.
/// \param map The graph map.
/// \param val The value that have to be found.
template <typename GR, typename Map>
typename Map::Key
mapFind(const GR& gr, const Map& map, const typename Map::Value& val) {
typedef typename Map::Key Item;
typedef typename ItemSetTraits<GR, Item>::ItemIt ItemIt;
for (ItemIt it(gr); it != INVALID; ++it) {
if (map[it] == val) return it;
}
return INVALID;
}
/// \brief Return an item having value for which a certain predicate is
/// true in a graph map.
///
/// This function returns an item (\c Node, \c Arc or \c Edge) having
/// such assigned value for which the specified predicate is true
/// in the given graph map.
/// If no such item exists, it returns \c INVALID.
///
/// \param gr The graph for which the map is defined.
/// \param map The graph map.
/// \param pred The predicate function object.
template <typename GR, typename Map, typename Pred>
typename Map::Key
mapFindIf(const GR& gr, const Map& map, const Pred& pred) {
typedef typename Map::Key Item;
typedef typename ItemSetTraits<GR, Item>::ItemIt ItemIt;
for (ItemIt it(gr); it != INVALID; ++it) {
if (pred(map[it])) return it;
}
return INVALID;
}
/// \brief Return the number of items having a specified value in a
/// graph map.
///
/// This function returns the number of items (\c Node, \c Arc or \c Edge)
/// having the specified assigned value in the given graph map.
///
/// \param gr The graph for which the map is defined.
/// \param map The graph map.
/// \param val The value that have to be counted.
template <typename GR, typename Map>
int mapCount(const GR& gr, const Map& map, const typename Map::Value& val) {
typedef typename Map::Key Item;
typedef typename ItemSetTraits<GR, Item>::ItemIt ItemIt;
int cnt = 0;
for (ItemIt it(gr); it != INVALID; ++it) {
if (map[it] == val) ++cnt;
}
return cnt;
}
/// \brief Return the number of items having values for which a certain
/// predicate is true in a graph map.
///
/// This function returns the number of items (\c Node, \c Arc or \c Edge)
/// having such assigned values for which the specified predicate is true
/// in the given graph map.
///
/// \param gr The graph for which the map is defined.
/// \param map The graph map.
/// \param pred The predicate function object.
template <typename GR, typename Map, typename Pred>
int mapCountIf(const GR& gr, const Map& map, const Pred& pred) {
typedef typename Map::Key Item;
typedef typename ItemSetTraits<GR, Item>::ItemIt ItemIt;
int cnt = 0;
for (ItemIt it(gr); it != INVALID; ++it) {
if (pred(map[it])) ++cnt;
}
return cnt;
}
/// \brief Fill a graph map with a certain value.
///
/// This function sets the specified value for all items (\c Node,
/// \c Arc or \c Edge) in the given graph map.
///
/// \param gr The graph for which the map is defined.
/// \param map The graph map. It must conform to the
/// \ref concepts::WriteMap "WriteMap" concept.
/// \param val The value.
template <typename GR, typename Map>
void mapFill(const GR& gr, Map& map, const typename Map::Value& val) {
typedef typename Map::Key Item;
typedef typename ItemSetTraits<GR, Item>::ItemIt ItemIt;
for (ItemIt it(gr); it != INVALID; ++it) {
map.set(it, val);
}
}
/// @}
}
#endif // LEMON_MAPS_H