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
|
// <ranges> -*- C++ -*-
// Copyright (C) 2019-2020 Free Software Foundation, Inc.
//
// This file is part of the GNU ISO C++ Library. This library is free
// software; you can redistribute it and/or modify it under the
// terms of the GNU General Public License as published by the
// Free Software Foundation; either version 3, or (at your option)
// any later version.
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// Under Section 7 of GPL version 3, you are granted additional
// permissions described in the GCC Runtime Library Exception, version
// 3.1, as published by the Free Software Foundation.
// You should have received __a copy of the GNU General Public License and
// __a copy of the GCC Runtime Library Exception along with this program;
// see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
// <http://www.gnu.org/licenses/>.
/** @file include/ranges
* This is a Standard C++ Library header.
* @ingroup concepts
*/
#ifndef _GLIBCXX_RANGES
#define _GLIBCXX_RANGES 1
#if __cplusplus > 201703L
#pragma GCC system_header
#include <concepts>
#if __cpp_lib_concepts
#include <compare>
#include <initializer_list>
#include <iterator>
#include <optional>
#include <tuple>
#include <bits/ranges_util.h>
#include <bits/refwrap.h>
/**
* @defgroup ranges Ranges
*
* Components for dealing with ranges of elements.
*/
namespace std _GLIBCXX_VISIBILITY(default)
{
_GLIBCXX_BEGIN_NAMESPACE_VERSION
namespace ranges
{
// [range.access] customization point objects
// [range.req] range and view concepts
// [range.dangling] dangling iterator handling
// Defined in <bits/ranges_base.h>
// [view.interface] View interface
// [range.subrange] Sub-ranges
// Defined in <bits/ranges_util.h>
// C++20 24.6 [range.factories] Range factories
/// A view that contains no elements.
template<typename _Tp> requires is_object_v<_Tp>
class empty_view
: public view_interface<empty_view<_Tp>>
{
public:
static constexpr _Tp* begin() noexcept { return nullptr; }
static constexpr _Tp* end() noexcept { return nullptr; }
static constexpr _Tp* data() noexcept { return nullptr; }
static constexpr size_t size() noexcept { return 0; }
static constexpr bool empty() noexcept { return true; }
};
template<typename _Tp>
inline constexpr bool enable_borrowed_range<empty_view<_Tp>> = true;
namespace __detail
{
template<copy_constructible _Tp> requires is_object_v<_Tp>
struct __box : std::optional<_Tp>
{
using std::optional<_Tp>::optional;
constexpr
__box()
noexcept(is_nothrow_default_constructible_v<_Tp>)
requires default_initializable<_Tp>
: std::optional<_Tp>{std::in_place}
{ }
__box(const __box&) = default;
__box(__box&&) = default;
using std::optional<_Tp>::operator=;
// _GLIBCXX_RESOLVE_LIB_DEFECTS
// 3477. Simplify constraints for semiregular-box
__box&
operator=(const __box& __that)
noexcept(is_nothrow_copy_constructible_v<_Tp>)
requires (!copyable<_Tp>)
{
if ((bool)__that)
this->emplace(*__that);
else
this->reset();
return *this;
}
__box&
operator=(__box&& __that)
noexcept(is_nothrow_move_constructible_v<_Tp>)
requires (!movable<_Tp>)
{
if ((bool)__that)
this->emplace(std::move(*__that));
else
this->reset();
return *this;
}
};
} // namespace __detail
/// A view that contains exactly one element.
template<copy_constructible _Tp> requires is_object_v<_Tp>
class single_view : public view_interface<single_view<_Tp>>
{
public:
single_view() = default;
constexpr explicit
single_view(const _Tp& __t)
: _M_value(__t)
{ }
constexpr explicit
single_view(_Tp&& __t)
: _M_value(std::move(__t))
{ }
template<typename... _Args>
requires constructible_from<_Tp, _Args...>
constexpr
single_view(in_place_t, _Args&&... __args)
: _M_value{in_place, std::forward<_Args>(__args)...}
{ }
constexpr _Tp*
begin() noexcept
{ return data(); }
constexpr const _Tp*
begin() const noexcept
{ return data(); }
constexpr _Tp*
end() noexcept
{ return data() + 1; }
constexpr const _Tp*
end() const noexcept
{ return data() + 1; }
static constexpr size_t
size() noexcept
{ return 1; }
constexpr _Tp*
data() noexcept
{ return _M_value.operator->(); }
constexpr const _Tp*
data() const noexcept
{ return _M_value.operator->(); }
private:
__detail::__box<_Tp> _M_value;
};
namespace __detail
{
template<typename _Wp>
constexpr auto __to_signed_like(_Wp __w) noexcept
{
if constexpr (!integral<_Wp>)
return iter_difference_t<_Wp>();
else if constexpr (sizeof(iter_difference_t<_Wp>) > sizeof(_Wp))
return iter_difference_t<_Wp>(__w);
else if constexpr (sizeof(ptrdiff_t) > sizeof(_Wp))
return ptrdiff_t(__w);
else if constexpr (sizeof(long long) > sizeof(_Wp))
return (long long)(__w);
#ifdef __SIZEOF_INT128__
else if constexpr (__SIZEOF_INT128__ > sizeof(_Wp))
return __int128(__w);
#endif
else
return __max_diff_type(__w);
}
template<typename _Wp>
using __iota_diff_t = decltype(__to_signed_like(std::declval<_Wp>()));
template<typename _It>
concept __decrementable = incrementable<_It>
&& requires(_It __i)
{
{ --__i } -> same_as<_It&>;
{ __i-- } -> same_as<_It>;
};
template<typename _It>
concept __advanceable = __decrementable<_It> && totally_ordered<_It>
&& requires( _It __i, const _It __j, const __iota_diff_t<_It> __n)
{
{ __i += __n } -> same_as<_It&>;
{ __i -= __n } -> same_as<_It&>;
_It(__j + __n);
_It(__n + __j);
_It(__j - __n);
{ __j - __j } -> convertible_to<__iota_diff_t<_It>>;
};
} // namespace __detail
template<weakly_incrementable _Winc,
semiregular _Bound = unreachable_sentinel_t>
requires std::__detail::__weakly_eq_cmp_with<_Winc, _Bound>
&& semiregular<_Winc>
class iota_view : public view_interface<iota_view<_Winc, _Bound>>
{
private:
struct _Sentinel;
struct _Iterator
{
private:
static auto
_S_iter_cat()
{
using namespace __detail;
if constexpr (__advanceable<_Winc>)
return random_access_iterator_tag{};
else if constexpr (__decrementable<_Winc>)
return bidirectional_iterator_tag{};
else if constexpr (incrementable<_Winc>)
return forward_iterator_tag{};
else
return input_iterator_tag{};
}
public:
using iterator_category = decltype(_S_iter_cat());
using value_type = _Winc;
using difference_type = __detail::__iota_diff_t<_Winc>;
_Iterator() = default;
constexpr explicit
_Iterator(_Winc __value)
: _M_value(__value) { }
constexpr _Winc
operator*() const noexcept(is_nothrow_copy_constructible_v<_Winc>)
{ return _M_value; }
constexpr _Iterator&
operator++()
{
++_M_value;
return *this;
}
constexpr void
operator++(int)
{ ++*this; }
constexpr _Iterator
operator++(int) requires incrementable<_Winc>
{
auto __tmp = *this;
++*this;
return __tmp;
}
constexpr _Iterator&
operator--() requires __detail::__decrementable<_Winc>
{
--_M_value;
return *this;
}
constexpr _Iterator
operator--(int) requires __detail::__decrementable<_Winc>
{
auto __tmp = *this;
--*this;
return __tmp;
}
constexpr _Iterator&
operator+=(difference_type __n) requires __detail::__advanceable<_Winc>
{
using __detail::__is_integer_like;
using __detail::__is_signed_integer_like;
if constexpr (__is_integer_like<_Winc>
&& !__is_signed_integer_like<_Winc>)
{
if (__n >= difference_type(0))
_M_value += static_cast<_Winc>(__n);
else
_M_value -= static_cast<_Winc>(-__n);
}
else
_M_value += __n;
return *this;
}
constexpr _Iterator&
operator-=(difference_type __n) requires __detail::__advanceable<_Winc>
{
using __detail::__is_integer_like;
using __detail::__is_signed_integer_like;
if constexpr (__is_integer_like<_Winc>
&& !__is_signed_integer_like<_Winc>)
{
if (__n >= difference_type(0))
_M_value -= static_cast<_Winc>(__n);
else
_M_value += static_cast<_Winc>(-__n);
}
else
_M_value -= __n;
return *this;
}
constexpr _Winc
operator[](difference_type __n) const
requires __detail::__advanceable<_Winc>
{ return _Winc(_M_value + __n); }
friend constexpr bool
operator==(const _Iterator& __x, const _Iterator& __y)
requires equality_comparable<_Winc>
{ return __x._M_value == __y._M_value; }
friend constexpr bool
operator<(const _Iterator& __x, const _Iterator& __y)
requires totally_ordered<_Winc>
{ return __x._M_value < __y._M_value; }
friend constexpr bool
operator>(const _Iterator& __x, const _Iterator& __y)
requires totally_ordered<_Winc>
{ return __y < __x; }
friend constexpr bool
operator<=(const _Iterator& __x, const _Iterator& __y)
requires totally_ordered<_Winc>
{ return !(__y < __x); }
friend constexpr bool
operator>=(const _Iterator& __x, const _Iterator& __y)
requires totally_ordered<_Winc>
{ return !(__x < __y); }
#ifdef __cpp_lib_three_way_comparison
friend constexpr auto
operator<=>(const _Iterator& __x, const _Iterator& __y)
requires totally_ordered<_Winc> && three_way_comparable<_Winc>
{ return __x._M_value <=> __y._M_value; }
#endif
friend constexpr _Iterator
operator+(_Iterator __i, difference_type __n)
requires __detail::__advanceable<_Winc>
{ return __i += __n; }
friend constexpr _Iterator
operator+(difference_type __n, _Iterator __i)
requires __detail::__advanceable<_Winc>
{ return __i += __n; }
friend constexpr _Iterator
operator-(_Iterator __i, difference_type __n)
requires __detail::__advanceable<_Winc>
{ return __i -= __n; }
friend constexpr difference_type
operator-(const _Iterator& __x, const _Iterator& __y)
requires __detail::__advanceable<_Winc>
{
using __detail::__is_integer_like;
using __detail::__is_signed_integer_like;
using _Dt = difference_type;
if constexpr (__is_integer_like<_Winc>)
{
if constexpr (__is_signed_integer_like<_Winc>)
return _Dt(_Dt(__x._M_value) - _Dt(__y._M_value));
else
return (__y._M_value > __x._M_value)
? _Dt(-_Dt(__y._M_value - __x._M_value))
: _Dt(__x._M_value - __y._M_value);
}
else
return __x._M_value - __y._M_value;
}
private:
_Winc _M_value = _Winc();
friend _Sentinel;
};
struct _Sentinel
{
private:
constexpr bool
_M_equal(const _Iterator& __x) const
{ return __x._M_value == _M_bound; }
_Bound _M_bound = _Bound();
public:
_Sentinel() = default;
constexpr explicit
_Sentinel(_Bound __bound)
: _M_bound(__bound) { }
friend constexpr bool
operator==(const _Iterator& __x, const _Sentinel& __y)
{ return __y._M_equal(__x); }
friend constexpr iter_difference_t<_Winc>
operator-(const _Iterator& __x, const _Sentinel& __y)
requires sized_sentinel_for<_Bound, _Winc>
{ return __x._M_value - __y._M_bound; }
friend constexpr iter_difference_t<_Winc>
operator-(const _Sentinel& __x, const _Iterator& __y)
requires sized_sentinel_for<_Bound, _Winc>
{ return -(__y - __x); }
};
_Winc _M_value = _Winc();
_Bound _M_bound = _Bound();
public:
iota_view() = default;
constexpr explicit
iota_view(_Winc __value)
: _M_value(__value)
{ }
constexpr
iota_view(type_identity_t<_Winc> __value,
type_identity_t<_Bound> __bound)
: _M_value(__value), _M_bound(__bound)
{
if constexpr (totally_ordered_with<_Winc, _Bound>)
__glibcxx_assert( bool(__value <= __bound) );
}
constexpr _Iterator
begin() const { return _Iterator{_M_value}; }
constexpr auto
end() const
{
if constexpr (same_as<_Bound, unreachable_sentinel_t>)
return unreachable_sentinel;
else
return _Sentinel{_M_bound};
}
constexpr _Iterator
end() const requires same_as<_Winc, _Bound>
{ return _Iterator{_M_bound}; }
constexpr auto
size() const
requires (same_as<_Winc, _Bound> && __detail::__advanceable<_Winc>)
|| (integral<_Winc> && integral<_Bound>)
|| sized_sentinel_for<_Bound, _Winc>
{
using __detail::__is_integer_like;
using __detail::__to_unsigned_like;
if constexpr (integral<_Winc> && integral<_Bound>)
{
using _Up = make_unsigned_t<decltype(_M_bound - _M_value)>;
return _Up(_M_bound) - _Up(_M_value);
}
else if constexpr (__is_integer_like<_Winc>)
return __to_unsigned_like(_M_bound) - __to_unsigned_like(_M_value);
else
return __to_unsigned_like(_M_bound - _M_value);
}
};
template<typename _Winc, typename _Bound>
requires (!__detail::__is_integer_like<_Winc>
|| !__detail::__is_integer_like<_Bound>
|| (__detail::__is_signed_integer_like<_Winc>
== __detail::__is_signed_integer_like<_Bound>))
iota_view(_Winc, _Bound) -> iota_view<_Winc, _Bound>;
template<weakly_incrementable _Winc, semiregular _Bound>
inline constexpr bool
enable_borrowed_range<iota_view<_Winc, _Bound>> = true;
namespace views
{
template<typename _Tp>
inline constexpr empty_view<_Tp> empty{};
struct _Single
{
template<typename _Tp>
constexpr auto
operator()(_Tp&& __e) const
{ return single_view{std::forward<_Tp>(__e)}; }
};
inline constexpr _Single single{};
struct _Iota
{
template<typename _Tp>
constexpr auto
operator()(_Tp&& __e) const
{ return iota_view{std::forward<_Tp>(__e)}; }
template<typename _Tp, typename _Up>
constexpr auto
operator()(_Tp&& __e, _Up&& __f) const
{ return iota_view{std::forward<_Tp>(__e), std::forward<_Up>(__f)}; }
};
inline constexpr _Iota iota{};
} // namespace views
namespace __detail
{
template<typename _Val, typename _CharT, typename _Traits>
concept __stream_extractable
= requires(basic_istream<_CharT, _Traits>& is, _Val& t) { is >> t; };
} // namespace __detail
template<movable _Val, typename _CharT, typename _Traits>
requires default_initializable<_Val>
&& __detail::__stream_extractable<_Val, _CharT, _Traits>
class basic_istream_view
: public view_interface<basic_istream_view<_Val, _CharT, _Traits>>
{
public:
basic_istream_view() = default;
constexpr explicit
basic_istream_view(basic_istream<_CharT, _Traits>& __stream)
: _M_stream(std::__addressof(__stream))
{ }
constexpr auto
begin()
{
if (_M_stream != nullptr)
*_M_stream >> _M_object;
return _Iterator{*this};
}
constexpr default_sentinel_t
end() const noexcept
{ return default_sentinel; }
private:
basic_istream<_CharT, _Traits>* _M_stream = nullptr;
_Val _M_object = _Val();
struct _Iterator
{
public:
using iterator_concept = input_iterator_tag;
using difference_type = ptrdiff_t;
using value_type = _Val;
_Iterator() = default;
constexpr explicit
_Iterator(basic_istream_view& __parent) noexcept
: _M_parent(std::__addressof(__parent))
{ }
_Iterator(const _Iterator&) = delete;
_Iterator(_Iterator&&) = default;
_Iterator& operator=(const _Iterator&) = delete;
_Iterator& operator=(_Iterator&&) = default;
_Iterator&
operator++()
{
__glibcxx_assert(_M_parent->_M_stream != nullptr);
*_M_parent->_M_stream >> _M_parent->_M_object;
return *this;
}
void
operator++(int)
{ ++*this; }
_Val&
operator*() const
{
__glibcxx_assert(_M_parent->_M_stream != nullptr);
return _M_parent->_M_object;
}
friend bool
operator==(const _Iterator& __x, default_sentinel_t)
{ return __x._M_at_end(); }
private:
basic_istream_view* _M_parent = nullptr;
bool
_M_at_end() const
{ return _M_parent == nullptr || !*_M_parent->_M_stream; }
};
friend _Iterator;
};
template<typename _Val, typename _CharT, typename _Traits>
basic_istream_view<_Val, _CharT, _Traits>
istream_view(basic_istream<_CharT, _Traits>& __s)
{ return basic_istream_view<_Val, _CharT, _Traits>{__s}; }
// C++20 24.7 [range.adaptors] Range adaptors
namespace __detail
{
struct _Empty { };
// Alias for a type that is conditionally present
// (and is an empty type otherwise).
// Data members using this alias should use [[no_unique_address]] so that
// they take no space when not needed.
template<bool _Present, typename _Tp>
using __maybe_present_t = conditional_t<_Present, _Tp, _Empty>;
// Alias for a type that is conditionally const.
template<bool _Const, typename _Tp>
using __maybe_const_t = conditional_t<_Const, const _Tp, _Tp>;
} // namespace __detail
namespace views
{
namespace __adaptor
{
template<typename _Tp>
inline constexpr auto
__maybe_refwrap(_Tp& __arg)
{ return reference_wrapper<_Tp>{__arg}; }
template<typename _Tp>
inline constexpr auto
__maybe_refwrap(const _Tp& __arg)
{ return reference_wrapper<const _Tp>{__arg}; }
template<typename _Tp>
inline constexpr decltype(auto)
__maybe_refwrap(_Tp&& __arg)
{ return std::forward<_Tp>(__arg); }
template<typename _Callable>
struct _RangeAdaptorClosure;
template<typename _Callable>
struct _RangeAdaptor
{
protected:
[[no_unique_address]]
__detail::__maybe_present_t<!is_default_constructible_v<_Callable>,
_Callable> _M_callable;
public:
constexpr
_RangeAdaptor(const _Callable& = {})
requires is_default_constructible_v<_Callable>
{ }
constexpr
_RangeAdaptor(_Callable __callable)
requires (!is_default_constructible_v<_Callable>)
: _M_callable(std::move(__callable))
{ }
template<typename... _Args>
requires (sizeof...(_Args) >= 1)
constexpr auto
operator()(_Args&&... __args) const
{
// [range.adaptor.object]: If a range adaptor object accepts more
// than one argument, then the following expressions are equivalent:
//
// (1) adaptor(range, args...)
// (2) adaptor(args...)(range)
// (3) range | adaptor(args...)
//
// In this case, adaptor(args...) is a range adaptor closure object.
//
// We handle (1) and (2) here, and (3) is just a special case of a
// more general case already handled by _RangeAdaptorClosure.
if constexpr (is_invocable_v<_Callable, _Args...>)
{
static_assert(sizeof...(_Args) != 1,
"a _RangeAdaptor that accepts only one argument "
"should be defined as a _RangeAdaptorClosure");
// Here we handle adaptor(range, args...) -- just forward all
// arguments to the underlying adaptor routine.
return _Callable{}(std::forward<_Args>(__args)...);
}
else
{
// Here we handle adaptor(args...)(range).
// Given args..., we return a _RangeAdaptorClosure that takes a
// range argument, such that (2) is equivalent to (1).
//
// We need to be careful about how we capture args... in this
// closure. By using __maybe_refwrap, we capture lvalue
// references by reference (through a reference_wrapper) and
// otherwise capture by value.
auto __closure
= [...__args(__maybe_refwrap(std::forward<_Args>(__args)))]
<typename _Range> (_Range&& __r) {
// This static_cast has two purposes: it forwards a
// reference_wrapper<T> capture as a T&, and otherwise
// forwards the captured argument as an rvalue.
return _Callable{}(std::forward<_Range>(__r),
(static_cast<unwrap_reference_t
<remove_const_t<decltype(__args)>>>
(__args))...);
};
using _ClosureType = decltype(__closure);
return _RangeAdaptorClosure<_ClosureType>(std::move(__closure));
}
}
};
template<typename _Callable>
_RangeAdaptor(_Callable) -> _RangeAdaptor<_Callable>;
template<typename _Callable>
struct _RangeAdaptorClosure : public _RangeAdaptor<_Callable>
{
using _RangeAdaptor<_Callable>::_RangeAdaptor;
template<viewable_range _Range>
requires requires { declval<_Callable>()(declval<_Range>()); }
constexpr auto
operator()(_Range&& __r) const
{
if constexpr (is_default_constructible_v<_Callable>)
return _Callable{}(std::forward<_Range>(__r));
else
return this->_M_callable(std::forward<_Range>(__r));
}
template<viewable_range _Range>
requires requires { declval<_Callable>()(declval<_Range>()); }
friend constexpr auto
operator|(_Range&& __r, const _RangeAdaptorClosure& __o)
{ return __o(std::forward<_Range>(__r)); }
template<typename _Tp>
friend constexpr auto
operator|(const _RangeAdaptorClosure<_Tp>& __x,
const _RangeAdaptorClosure& __y)
{
if constexpr (is_default_constructible_v<_Tp>
&& is_default_constructible_v<_Callable>)
{
auto __closure = [] <typename _Up> (_Up&& __e) {
return std::forward<_Up>(__e) | decltype(__x){} | decltype(__y){};
};
return _RangeAdaptorClosure<decltype(__closure)>(__closure);
}
else if constexpr (is_default_constructible_v<_Tp>
&& !is_default_constructible_v<_Callable>)
{
auto __closure = [__y] <typename _Up> (_Up&& __e) {
return std::forward<_Up>(__e) | decltype(__x){} | __y;
};
return _RangeAdaptorClosure<decltype(__closure)>(__closure);
}
else if constexpr (!is_default_constructible_v<_Tp>
&& is_default_constructible_v<_Callable>)
{
auto __closure = [__x] <typename _Up> (_Up&& __e) {
return std::forward<_Up>(__e) | __x | decltype(__y){};
};
return _RangeAdaptorClosure<decltype(__closure)>(__closure);
}
else
{
auto __closure = [__x, __y] <typename _Up> (_Up&& __e) {
return std::forward<_Up>(__e) | __x | __y;
};
return _RangeAdaptorClosure<decltype(__closure)>(__closure);
}
}
};
template<typename _Callable>
_RangeAdaptorClosure(_Callable) -> _RangeAdaptorClosure<_Callable>;
} // namespace __adaptor
} // namespace views
template<range _Range> requires is_object_v<_Range>
class ref_view : public view_interface<ref_view<_Range>>
{
private:
_Range* _M_r = nullptr;
static void _S_fun(_Range&); // not defined
static void _S_fun(_Range&&) = delete;
public:
constexpr
ref_view() noexcept = default;
template<__detail::__not_same_as<ref_view> _Tp>
requires convertible_to<_Tp, _Range&>
&& requires { _S_fun(declval<_Tp>()); }
constexpr
ref_view(_Tp&& __t)
: _M_r(std::__addressof(static_cast<_Range&>(std::forward<_Tp>(__t))))
{ }
constexpr _Range&
base() const
{ return *_M_r; }
constexpr iterator_t<_Range>
begin() const
{ return ranges::begin(*_M_r); }
constexpr sentinel_t<_Range>
end() const
{ return ranges::end(*_M_r); }
constexpr bool
empty() const requires requires { ranges::empty(*_M_r); }
{ return ranges::empty(*_M_r); }
constexpr auto
size() const requires sized_range<_Range>
{ return ranges::size(*_M_r); }
constexpr auto
data() const requires contiguous_range<_Range>
{ return ranges::data(*_M_r); }
};
template<typename _Range>
ref_view(_Range&) -> ref_view<_Range>;
template<typename _Tp>
inline constexpr bool enable_borrowed_range<ref_view<_Tp>> = true;
namespace views
{
inline constexpr __adaptor::_RangeAdaptorClosure all
= [] <viewable_range _Range> (_Range&& __r)
{
if constexpr (view<decay_t<_Range>>)
return std::forward<_Range>(__r);
else if constexpr (requires { ref_view{std::forward<_Range>(__r)}; })
return ref_view{std::forward<_Range>(__r)};
else
return subrange{std::forward<_Range>(__r)};
};
template<viewable_range _Range>
using all_t = decltype(all(std::declval<_Range>()));
} // namespace views
// XXX: the following algos are copied from ranges_algo.h to avoid a circular
// dependency with that header.
namespace __detail
{
template<input_iterator _Iter, sentinel_for<_Iter> _Sent,
typename _Proj = identity,
indirect_unary_predicate<projected<_Iter, _Proj>> _Pred>
constexpr _Iter
find_if(_Iter __first, _Sent __last, _Pred __pred, _Proj __proj = {})
{
while (__first != __last
&& !(bool)std::__invoke(__pred, std::__invoke(__proj, *__first)))
++__first;
return __first;
}
template<input_iterator _Iter, sentinel_for<_Iter> _Sent,
typename _Proj = identity,
indirect_unary_predicate<projected<_Iter, _Proj>> _Pred>
constexpr _Iter
find_if_not(_Iter __first, _Sent __last, _Pred __pred, _Proj __proj = {})
{
while (__first != __last
&& (bool)std::__invoke(__pred, std::__invoke(__proj, *__first)))
++__first;
return __first;
}
template<typename _Tp, typename _Proj = identity,
indirect_strict_weak_order<projected<const _Tp*, _Proj>>
_Comp = ranges::less>
constexpr const _Tp&
min(const _Tp& __a, const _Tp& __b, _Comp __comp = {}, _Proj __proj = {})
{
if (std::__invoke(std::move(__comp),
std::__invoke(__proj, __b),
std::__invoke(__proj, __a)))
return __b;
else
return __a;
}
template<input_iterator _Iter1, sentinel_for<_Iter1> _Sent1,
input_iterator _Iter2, sentinel_for<_Iter2> _Sent2,
typename _Pred = ranges::equal_to,
typename _Proj1 = identity, typename _Proj2 = identity>
requires indirectly_comparable<_Iter1, _Iter2, _Pred, _Proj1, _Proj2>
constexpr pair<_Iter1, _Iter2>
mismatch(_Iter1 __first1, _Sent1 __last1, _Iter2 __first2, _Sent2 __last2,
_Pred __pred = {}, _Proj1 __proj1 = {}, _Proj2 __proj2 = {})
{
while (__first1 != __last1 && __first2 != __last2
&& (bool)std::__invoke(__pred,
std::__invoke(__proj1, *__first1),
std::__invoke(__proj2, *__first2)))
{
++__first1;
++__first2;
}
return { std::move(__first1), std::move(__first2) };
}
} // namespace __detail
namespace __detail
{
template<range _Range>
struct _CachedPosition
{
constexpr bool
_M_has_value() const
{ return false; }
constexpr iterator_t<_Range>
_M_get(const _Range&) const
{
__glibcxx_assert(false);
return {};
}
constexpr void
_M_set(const _Range&, const iterator_t<_Range>&) const
{ }
};
template<forward_range _Range>
struct _CachedPosition<_Range>
{
private:
iterator_t<_Range> _M_iter{};
public:
constexpr bool
_M_has_value() const
{ return _M_iter != iterator_t<_Range>{}; }
constexpr iterator_t<_Range>
_M_get(const _Range&) const
{
__glibcxx_assert(_M_has_value());
return _M_iter;
}
constexpr void
_M_set(const _Range&, const iterator_t<_Range>& __it)
{
__glibcxx_assert(!_M_has_value());
_M_iter = __it;
}
};
template<random_access_range _Range>
requires (sizeof(range_difference_t<_Range>)
<= sizeof(iterator_t<_Range>))
struct _CachedPosition<_Range>
{
private:
range_difference_t<_Range> _M_offset = -1;
public:
constexpr bool
_M_has_value() const
{ return _M_offset >= 0; }
constexpr iterator_t<_Range>
_M_get(_Range& __r) const
{
__glibcxx_assert(_M_has_value());
return ranges::begin(__r) + _M_offset;
}
constexpr void
_M_set(_Range& __r, const iterator_t<_Range>& __it)
{
__glibcxx_assert(!_M_has_value());
_M_offset = __it - ranges::begin(__r);
}
};
} // namespace __detail
template<input_range _Vp,
indirect_unary_predicate<iterator_t<_Vp>> _Pred>
requires view<_Vp> && is_object_v<_Pred>
class filter_view : public view_interface<filter_view<_Vp, _Pred>>
{
private:
struct _Sentinel;
struct _Iterator
{
private:
static constexpr auto
_S_iter_concept()
{
if constexpr (bidirectional_range<_Vp>)
return bidirectional_iterator_tag{};
else if constexpr (forward_range<_Vp>)
return forward_iterator_tag{};
else
return input_iterator_tag{};
}
static constexpr auto
_S_iter_cat()
{
using _Cat = typename iterator_traits<_Vp_iter>::iterator_category;
if constexpr (derived_from<_Cat, bidirectional_iterator_tag>)
return bidirectional_iterator_tag{};
else if constexpr (derived_from<_Cat, forward_iterator_tag>)
return forward_iterator_tag{};
else
return _Cat{};
}
friend filter_view;
using _Vp_iter = iterator_t<_Vp>;
_Vp_iter _M_current = _Vp_iter();
filter_view* _M_parent = nullptr;
public:
using iterator_concept = decltype(_S_iter_concept());
using iterator_category = decltype(_S_iter_cat());
using value_type = range_value_t<_Vp>;
using difference_type = range_difference_t<_Vp>;
_Iterator() = default;
constexpr
_Iterator(filter_view& __parent, _Vp_iter __current)
: _M_current(std::move(__current)),
_M_parent(std::__addressof(__parent))
{ }
constexpr _Vp_iter
base() const &
requires copyable<_Vp_iter>
{ return _M_current; }
constexpr _Vp_iter
base() &&
{ return std::move(_M_current); }
constexpr range_reference_t<_Vp>
operator*() const
{ return *_M_current; }
constexpr _Vp_iter
operator->() const
requires __detail::__has_arrow<_Vp_iter>
&& copyable<_Vp_iter>
{ return _M_current; }
constexpr _Iterator&
operator++()
{
_M_current = __detail::find_if(std::move(++_M_current),
ranges::end(_M_parent->_M_base),
std::ref(*_M_parent->_M_pred));
return *this;
}
constexpr void
operator++(int)
{ ++*this; }
constexpr _Iterator
operator++(int) requires forward_range<_Vp>
{
auto __tmp = *this;
++*this;
return __tmp;
}
constexpr _Iterator&
operator--() requires bidirectional_range<_Vp>
{
do
--_M_current;
while (!std::__invoke(*_M_parent->_M_pred, *_M_current));
return *this;
}
constexpr _Iterator
operator--(int) requires bidirectional_range<_Vp>
{
auto __tmp = *this;
--*this;
return __tmp;
}
friend constexpr bool
operator==(const _Iterator& __x, const _Iterator& __y)
requires equality_comparable<_Vp_iter>
{ return __x._M_current == __y._M_current; }
friend constexpr range_rvalue_reference_t<_Vp>
iter_move(const _Iterator& __i)
noexcept(noexcept(ranges::iter_move(__i._M_current)))
{ return ranges::iter_move(__i._M_current); }
friend constexpr void
iter_swap(const _Iterator& __x, const _Iterator& __y)
noexcept(noexcept(ranges::iter_swap(__x._M_current, __y._M_current)))
requires indirectly_swappable<_Vp_iter>
{ ranges::iter_swap(__x._M_current, __y._M_current); }
};
struct _Sentinel
{
private:
sentinel_t<_Vp> _M_end = sentinel_t<_Vp>();
constexpr bool
__equal(const _Iterator& __i) const
{ return __i._M_current == _M_end; }
public:
_Sentinel() = default;
constexpr explicit
_Sentinel(filter_view& __parent)
: _M_end(ranges::end(__parent._M_base))
{ }
constexpr sentinel_t<_Vp>
base() const
{ return _M_end; }
friend constexpr bool
operator==(const _Iterator& __x, const _Sentinel& __y)
{ return __y.__equal(__x); }
};
_Vp _M_base = _Vp();
__detail::__box<_Pred> _M_pred;
[[no_unique_address]] __detail::_CachedPosition<_Vp> _M_cached_begin;
public:
filter_view() = default;
constexpr
filter_view(_Vp __base, _Pred __pred)
: _M_base(std::move(__base)), _M_pred(std::move(__pred))
{ }
constexpr _Vp
base() const& requires copy_constructible<_Vp>
{ return _M_base; }
constexpr _Vp
base() &&
{ return std::move(_M_base); }
constexpr const _Pred&
pred() const
{ return *_M_pred; }
constexpr _Iterator
begin()
{
if (_M_cached_begin._M_has_value())
return {*this, _M_cached_begin._M_get(_M_base)};
__glibcxx_assert(_M_pred.has_value());
auto __it = __detail::find_if(ranges::begin(_M_base),
ranges::end(_M_base),
std::ref(*_M_pred));
_M_cached_begin._M_set(_M_base, __it);
return {*this, std::move(__it)};
}
constexpr auto
end()
{
if constexpr (common_range<_Vp>)
return _Iterator{*this, ranges::end(_M_base)};
else
return _Sentinel{*this};
}
};
template<typename _Range, typename _Pred>
filter_view(_Range&&, _Pred) -> filter_view<views::all_t<_Range>, _Pred>;
namespace views
{
inline constexpr __adaptor::_RangeAdaptor filter
= [] <viewable_range _Range, typename _Pred> (_Range&& __r, _Pred&& __p)
{
return filter_view{std::forward<_Range>(__r), std::forward<_Pred>(__p)};
};
} // namespace views
template<input_range _Vp, copy_constructible _Fp>
requires view<_Vp> && is_object_v<_Fp>
&& regular_invocable<_Fp&, range_reference_t<_Vp>>
&& std::__detail::__can_reference<invoke_result_t<_Fp&,
range_reference_t<_Vp>>>
class transform_view : public view_interface<transform_view<_Vp, _Fp>>
{
private:
template<bool _Const>
struct _Sentinel;
template<bool _Const>
struct _Iterator
{
private:
using _Parent = __detail::__maybe_const_t<_Const, transform_view>;
using _Base = __detail::__maybe_const_t<_Const, _Vp>;
static constexpr auto
_S_iter_concept()
{
if constexpr (random_access_range<_Vp>)
return random_access_iterator_tag{};
else if constexpr (bidirectional_range<_Vp>)
return bidirectional_iterator_tag{};
else if constexpr (forward_range<_Vp>)
return forward_iterator_tag{};
else
return input_iterator_tag{};
}
static constexpr auto
_S_iter_cat()
{
using _Res = invoke_result_t<_Fp&, range_reference_t<_Base>>;
if constexpr (is_lvalue_reference_v<_Res>)
{
using _Cat
= typename iterator_traits<_Base_iter>::iterator_category;
if constexpr (derived_from<_Cat, contiguous_iterator_tag>)
return random_access_iterator_tag{};
else
return _Cat{};
}
else
return input_iterator_tag{};
}
using _Base_iter = iterator_t<_Base>;
_Base_iter _M_current = _Base_iter();
_Parent* _M_parent = nullptr;
public:
using iterator_concept = decltype(_S_iter_concept());
using iterator_category = decltype(_S_iter_cat());
using value_type
= remove_cvref_t<invoke_result_t<_Fp&, range_reference_t<_Base>>>;
using difference_type = range_difference_t<_Base>;
_Iterator() = default;
constexpr
_Iterator(_Parent& __parent, _Base_iter __current)
: _M_current(std::move(__current)),
_M_parent(std::__addressof(__parent))
{ }
constexpr
_Iterator(_Iterator<!_Const> __i)
requires _Const
&& convertible_to<iterator_t<_Vp>, _Base_iter>
: _M_current(std::move(__i._M_current)), _M_parent(__i._M_parent)
{ }
constexpr _Base_iter
base() const &
requires copyable<_Base_iter>
{ return _M_current; }
constexpr _Base_iter
base() &&
{ return std::move(_M_current); }
constexpr decltype(auto)
operator*() const
noexcept(noexcept(std::__invoke(*_M_parent->_M_fun, *_M_current)))
{ return std::__invoke(*_M_parent->_M_fun, *_M_current); }
constexpr _Iterator&
operator++()
{
++_M_current;
return *this;
}
constexpr void
operator++(int)
{ ++_M_current; }
constexpr _Iterator
operator++(int) requires forward_range<_Base>
{
auto __tmp = *this;
++*this;
return __tmp;
}
constexpr _Iterator&
operator--() requires bidirectional_range<_Base>
{
--_M_current;
return *this;
}
constexpr _Iterator
operator--(int) requires bidirectional_range<_Base>
{
auto __tmp = *this;
--*this;
return __tmp;
}
constexpr _Iterator&
operator+=(difference_type __n) requires random_access_range<_Base>
{
_M_current += __n;
return *this;
}
constexpr _Iterator&
operator-=(difference_type __n) requires random_access_range<_Base>
{
_M_current -= __n;
return *this;
}
constexpr decltype(auto)
operator[](difference_type __n) const
requires random_access_range<_Base>
{ return std::__invoke(*_M_parent->_M_fun, _M_current[__n]); }
friend constexpr bool
operator==(const _Iterator& __x, const _Iterator& __y)
requires equality_comparable<_Base_iter>
{ return __x._M_current == __y._M_current; }
friend constexpr bool
operator<(const _Iterator& __x, const _Iterator& __y)
requires random_access_range<_Base>
{ return __x._M_current < __y._M_current; }
friend constexpr bool
operator>(const _Iterator& __x, const _Iterator& __y)
requires random_access_range<_Base>
{ return __y < __x; }
friend constexpr bool
operator<=(const _Iterator& __x, const _Iterator& __y)
requires random_access_range<_Base>
{ return !(__y < __x); }
friend constexpr bool
operator>=(const _Iterator& __x, const _Iterator& __y)
requires random_access_range<_Base>
{ return !(__x < __y); }
#ifdef __cpp_lib_three_way_comparison
friend constexpr auto
operator<=>(const _Iterator& __x, const _Iterator& __y)
requires random_access_range<_Base>
&& three_way_comparable<_Base_iter>
{ return __x._M_current <=> __y._M_current; }
#endif
friend constexpr _Iterator
operator+(_Iterator __i, difference_type __n)
requires random_access_range<_Base>
{ return {*__i._M_parent, __i._M_current + __n}; }
friend constexpr _Iterator
operator+(difference_type __n, _Iterator __i)
requires random_access_range<_Base>
{ return {*__i._M_parent, __i._M_current + __n}; }
friend constexpr _Iterator
operator-(_Iterator __i, difference_type __n)
requires random_access_range<_Base>
{ return {*__i._M_parent, __i._M_current - __n}; }
// _GLIBCXX_RESOLVE_LIB_DEFECTS
// 3483. transform_view::iterator's difference is overconstrained
friend constexpr difference_type
operator-(const _Iterator& __x, const _Iterator& __y)
requires sized_sentinel_for<iterator_t<_Base>, iterator_t<_Base>>
{ return __x._M_current - __y._M_current; }
friend constexpr decltype(auto)
iter_move(const _Iterator& __i) noexcept(noexcept(*__i))
{
if constexpr (is_lvalue_reference_v<decltype(*__i)>)
return std::move(*__i);
else
return *__i;
}
friend constexpr void
iter_swap(const _Iterator& __x, const _Iterator& __y)
noexcept(noexcept(ranges::iter_swap(__x._M_current, __y._M_current)))
requires indirectly_swappable<_Base_iter>
{ return ranges::iter_swap(__x._M_current, __y._M_current); }
friend _Iterator<!_Const>;
template<bool> friend struct _Sentinel;
};
template<bool _Const>
struct _Sentinel
{
private:
using _Parent = __detail::__maybe_const_t<_Const, transform_view>;
using _Base = __detail::__maybe_const_t<_Const, _Vp>;
template<bool _Const2>
constexpr auto
__distance_from(const _Iterator<_Const2>& __i) const
{ return _M_end - __i._M_current; }
template<bool _Const2>
constexpr bool
__equal(const _Iterator<_Const2>& __i) const
{ return __i._M_current == _M_end; }
sentinel_t<_Base> _M_end = sentinel_t<_Base>();
public:
_Sentinel() = default;
constexpr explicit
_Sentinel(sentinel_t<_Base> __end)
: _M_end(__end)
{ }
constexpr
_Sentinel(_Sentinel<!_Const> __i)
requires _Const
&& convertible_to<sentinel_t<_Vp>, sentinel_t<_Base>>
: _M_end(std::move(__i._M_end))
{ }
constexpr sentinel_t<_Base>
base() const
{ return _M_end; }
template<bool _Const2>
requires sentinel_for<sentinel_t<_Base>,
iterator_t<__detail::__maybe_const_t<_Const2, _Vp>>>
friend constexpr bool
operator==(const _Iterator<_Const2>& __x, const _Sentinel& __y)
{ return __y.__equal(__x); }
template<bool _Const2,
typename _Base2 = __detail::__maybe_const_t<_Const2, _Vp>>
requires sized_sentinel_for<sentinel_t<_Base>, iterator_t<_Base2>>
friend constexpr range_difference_t<_Base2>
operator-(const _Iterator<_Const2>& __x, const _Sentinel& __y)
{ return -__y.__distance_from(__x); }
template<bool _Const2,
typename _Base2 = __detail::__maybe_const_t<_Const2, _Vp>>
requires sized_sentinel_for<sentinel_t<_Base>, iterator_t<_Base2>>
friend constexpr range_difference_t<_Base2>
operator-(const _Sentinel& __y, const _Iterator<_Const2>& __x)
{ return __y.__distance_from(__x); }
friend _Sentinel<!_Const>;
};
_Vp _M_base = _Vp();
__detail::__box<_Fp> _M_fun;
public:
transform_view() = default;
constexpr
transform_view(_Vp __base, _Fp __fun)
: _M_base(std::move(__base)), _M_fun(std::move(__fun))
{ }
constexpr _Vp
base() const& requires copy_constructible<_Vp>
{ return _M_base ; }
constexpr _Vp
base() &&
{ return std::move(_M_base); }
constexpr _Iterator<false>
begin()
{ return _Iterator<false>{*this, ranges::begin(_M_base)}; }
constexpr _Iterator<true>
begin() const
requires range<const _Vp>
&& regular_invocable<const _Fp&, range_reference_t<const _Vp>>
{ return _Iterator<true>{*this, ranges::begin(_M_base)}; }
constexpr _Sentinel<false>
end()
{ return _Sentinel<false>{ranges::end(_M_base)}; }
constexpr _Iterator<false>
end() requires common_range<_Vp>
{ return _Iterator<false>{*this, ranges::end(_M_base)}; }
constexpr _Sentinel<true>
end() const
requires range<const _Vp>
&& regular_invocable<const _Fp&, range_reference_t<const _Vp>>
{ return _Sentinel<true>{ranges::end(_M_base)}; }
constexpr _Iterator<true>
end() const
requires common_range<const _Vp>
&& regular_invocable<const _Fp&, range_reference_t<const _Vp>>
{ return _Iterator<true>{*this, ranges::end(_M_base)}; }
constexpr auto
size() requires sized_range<_Vp>
{ return ranges::size(_M_base); }
constexpr auto
size() const requires sized_range<const _Vp>
{ return ranges::size(_M_base); }
};
template<typename _Range, typename _Fp>
transform_view(_Range&&, _Fp) -> transform_view<views::all_t<_Range>, _Fp>;
namespace views
{
inline constexpr __adaptor::_RangeAdaptor transform
= [] <viewable_range _Range, typename _Fp> (_Range&& __r, _Fp&& __f)
{
return transform_view{std::forward<_Range>(__r), std::forward<_Fp>(__f)};
};
} // namespace views
template<view _Vp>
class take_view : public view_interface<take_view<_Vp>>
{
private:
template<bool _Const>
struct _Sentinel
{
private:
using _Base = __detail::__maybe_const_t<_Const, _Vp>;
using _CI = counted_iterator<iterator_t<_Base>>;
sentinel_t<_Base> _M_end = sentinel_t<_Base>();
public:
_Sentinel() = default;
constexpr explicit
_Sentinel(sentinel_t<_Base> __end)
: _M_end(__end)
{ }
constexpr
_Sentinel(_Sentinel<!_Const> __s)
requires _Const && convertible_to<sentinel_t<_Vp>, sentinel_t<_Base>>
: _M_end(std::move(__s._M_end))
{ }
constexpr sentinel_t<_Base>
base() const
{ return _M_end; }
friend constexpr bool operator==(const _CI& __y, const _Sentinel& __x)
{ return __y.count() == 0 || __y.base() == __x._M_end; }
friend _Sentinel<!_Const>;
};
_Vp _M_base = _Vp();
range_difference_t<_Vp> _M_count = 0;
public:
take_view() = default;
constexpr
take_view(_Vp base, range_difference_t<_Vp> __count)
: _M_base(std::move(base)), _M_count(std::move(__count))
{ }
constexpr _Vp
base() const& requires copy_constructible<_Vp>
{ return _M_base; }
constexpr _Vp
base() &&
{ return std::move(_M_base); }
constexpr auto
begin() requires (!__detail::__simple_view<_Vp>)
{
if constexpr (sized_range<_Vp>)
{
if constexpr (random_access_range<_Vp>)
return ranges::begin(_M_base);
else
{
auto __sz = size();
return counted_iterator{ranges::begin(_M_base), __sz};
}
}
else
return counted_iterator{ranges::begin(_M_base), _M_count};
}
constexpr auto
begin() const requires range<const _Vp>
{
if constexpr (sized_range<const _Vp>)
{
if constexpr (random_access_range<const _Vp>)
return ranges::begin(_M_base);
else
{
auto __sz = size();
return counted_iterator{ranges::begin(_M_base), __sz};
}
}
else
return counted_iterator{ranges::begin(_M_base), _M_count};
}
constexpr auto
end() requires (!__detail::__simple_view<_Vp>)
{
if constexpr (sized_range<_Vp>)
{
if constexpr (random_access_range<_Vp>)
return ranges::begin(_M_base) + size();
else
return default_sentinel;
}
else
return _Sentinel<false>{ranges::end(_M_base)};
}
constexpr auto
end() const requires range<const _Vp>
{
if constexpr (sized_range<const _Vp>)
{
if constexpr (random_access_range<const _Vp>)
return ranges::begin(_M_base) + size();
else
return default_sentinel;
}
else
return _Sentinel<true>{ranges::end(_M_base)};
}
constexpr auto
size() requires sized_range<_Vp>
{
auto __n = ranges::size(_M_base);
return __detail::min(__n, static_cast<decltype(__n)>(_M_count));
}
constexpr auto
size() const requires sized_range<const _Vp>
{
auto __n = ranges::size(_M_base);
return __detail::min(__n, static_cast<decltype(__n)>(_M_count));
}
};
template<range _Range>
take_view(_Range&&, range_difference_t<_Range>)
-> take_view<views::all_t<_Range>>;
namespace views
{
inline constexpr __adaptor::_RangeAdaptor take
= [] <viewable_range _Range, typename _Tp> (_Range&& __r, _Tp&& __n)
{
return take_view{std::forward<_Range>(__r), std::forward<_Tp>(__n)};
};
} // namespace views
template<view _Vp, typename _Pred>
requires input_range<_Vp> && is_object_v<_Pred>
&& indirect_unary_predicate<const _Pred, iterator_t<_Vp>>
class take_while_view : public view_interface<take_while_view<_Vp, _Pred>>
{
template<bool _Const>
struct _Sentinel
{
private:
using _Base = __detail::__maybe_const_t<_Const, _Vp>;
sentinel_t<_Base> _M_end = sentinel_t<_Base>();
const _Pred* _M_pred = nullptr;
public:
_Sentinel() = default;
constexpr explicit
_Sentinel(sentinel_t<_Base> __end, const _Pred* __pred)
: _M_end(__end), _M_pred(__pred)
{ }
constexpr
_Sentinel(_Sentinel<!_Const> __s)
requires _Const && convertible_to<sentinel_t<_Vp>, sentinel_t<_Base>>
: _M_end(__s._M_end), _M_pred(__s._M_pred)
{ }
constexpr sentinel_t<_Base>
base() const { return _M_end; }
friend constexpr bool
operator==(const iterator_t<_Base>& __x, const _Sentinel& __y)
{ return __y._M_end == __x || !std::__invoke(*__y._M_pred, *__x); }
friend _Sentinel<!_Const>;
};
_Vp _M_base = _Vp();
__detail::__box<_Pred> _M_pred;
public:
take_while_view() = default;
constexpr
take_while_view(_Vp base, _Pred __pred)
: _M_base(std::move(base)), _M_pred(std::move(__pred))
{
}
constexpr _Vp
base() const& requires copy_constructible<_Vp>
{ return _M_base; }
constexpr _Vp
base() &&
{ return std::move(_M_base); }
constexpr const _Pred&
pred() const
{ return *_M_pred; }
constexpr auto
begin() requires (!__detail::__simple_view<_Vp>)
{ return ranges::begin(_M_base); }
constexpr auto
begin() const requires range<const _Vp>
{ return ranges::begin(_M_base); }
constexpr auto
end() requires (!__detail::__simple_view<_Vp>)
{ return _Sentinel<false>(ranges::end(_M_base),
std::__addressof(*_M_pred)); }
constexpr auto
end() const requires range<const _Vp>
{ return _Sentinel<true>(ranges::end(_M_base),
std::__addressof(*_M_pred)); }
};
template<typename _Range, typename _Pred>
take_while_view(_Range&&, _Pred)
-> take_while_view<views::all_t<_Range>, _Pred>;
namespace views
{
inline constexpr __adaptor::_RangeAdaptor take_while
= [] <viewable_range _Range, typename _Pred> (_Range&& __r, _Pred&& __p)
{
return take_while_view{std::forward<_Range>(__r), std::forward<_Pred>(__p)};
};
} // namespace views
template<view _Vp>
class drop_view : public view_interface<drop_view<_Vp>>
{
private:
_Vp _M_base = _Vp();
range_difference_t<_Vp> _M_count = 0;
// ranges::next(begin(base), count, end(base)) is O(1) if _Vp satisfies
// both random_access_range and sized_range. Otherwise, cache its result.
static constexpr bool _S_needs_cached_begin
= !(random_access_range<const _Vp> && sized_range<const _Vp>);
[[no_unique_address]]
__detail::__maybe_present_t<_S_needs_cached_begin,
__detail::_CachedPosition<_Vp>>
_M_cached_begin;
public:
drop_view() = default;
constexpr
drop_view(_Vp __base, range_difference_t<_Vp> __count)
: _M_base(std::move(__base)), _M_count(__count)
{ __glibcxx_assert(__count >= 0); }
constexpr _Vp
base() const& requires copy_constructible<_Vp>
{ return _M_base; }
constexpr _Vp
base() &&
{ return std::move(_M_base); }
// This overload is disabled for simple views with constant-time begin().
constexpr auto
begin()
requires (!(__detail::__simple_view<_Vp>
&& random_access_range<const _Vp>
&& sized_range<const _Vp>))
{
if constexpr (_S_needs_cached_begin)
if (_M_cached_begin._M_has_value())
return _M_cached_begin._M_get(_M_base);
auto __it = ranges::next(ranges::begin(_M_base),
_M_count, ranges::end(_M_base));
if constexpr (_S_needs_cached_begin)
_M_cached_begin._M_set(_M_base, __it);
return __it;
}
// _GLIBCXX_RESOLVE_LIB_DEFECTS
// 3482. drop_view's const begin should additionally require sized_range
constexpr auto
begin() const
requires random_access_range<const _Vp> && sized_range<const _Vp>
{
return ranges::next(ranges::begin(_M_base), _M_count,
ranges::end(_M_base));
}
constexpr auto
end() requires (!__detail::__simple_view<_Vp>)
{ return ranges::end(_M_base); }
constexpr auto
end() const requires range<const _Vp>
{ return ranges::end(_M_base); }
constexpr auto
size() requires sized_range<_Vp>
{
const auto __s = ranges::size(_M_base);
const auto __c = static_cast<decltype(__s)>(_M_count);
return __s < __c ? 0 : __s - __c;
}
constexpr auto
size() const requires sized_range<const _Vp>
{
const auto __s = ranges::size(_M_base);
const auto __c = static_cast<decltype(__s)>(_M_count);
return __s < __c ? 0 : __s - __c;
}
};
template<typename _Range>
drop_view(_Range&&, range_difference_t<_Range>)
-> drop_view<views::all_t<_Range>>;
namespace views
{
inline constexpr __adaptor::_RangeAdaptor drop
= [] <viewable_range _Range, typename _Tp> (_Range&& __r, _Tp&& __n)
{
return drop_view{std::forward<_Range>(__r), std::forward<_Tp>(__n)};
};
} // namespace views
template<view _Vp, typename _Pred>
requires input_range<_Vp> && is_object_v<_Pred>
&& indirect_unary_predicate<const _Pred, iterator_t<_Vp>>
class drop_while_view : public view_interface<drop_while_view<_Vp, _Pred>>
{
private:
_Vp _M_base = _Vp();
__detail::__box<_Pred> _M_pred;
[[no_unique_address]] __detail::_CachedPosition<_Vp> _M_cached_begin;
public:
drop_while_view() = default;
constexpr
drop_while_view(_Vp __base, _Pred __pred)
: _M_base(std::move(__base)), _M_pred(std::move(__pred))
{ }
constexpr _Vp
base() const& requires copy_constructible<_Vp>
{ return _M_base; }
constexpr _Vp
base() &&
{ return std::move(_M_base); }
constexpr const _Pred&
pred() const
{ return *_M_pred; }
constexpr auto
begin()
{
if (_M_cached_begin._M_has_value())
return _M_cached_begin._M_get(_M_base);
auto __it = __detail::find_if_not(ranges::begin(_M_base),
ranges::end(_M_base),
std::cref(*_M_pred));
_M_cached_begin._M_set(_M_base, __it);
return __it;
}
constexpr auto
end()
{ return ranges::end(_M_base); }
};
template<typename _Range, typename _Pred>
drop_while_view(_Range&&, _Pred)
-> drop_while_view<views::all_t<_Range>, _Pred>;
namespace views
{
inline constexpr __adaptor::_RangeAdaptor drop_while
= [] <viewable_range _Range, typename _Pred> (_Range&& __r, _Pred&& __p)
{
return drop_while_view{std::forward<_Range>(__r),
std::forward<_Pred>(__p)};
};
} // namespace views
template<input_range _Vp>
requires view<_Vp> && input_range<range_reference_t<_Vp>>
&& (is_reference_v<range_reference_t<_Vp>>
|| view<range_value_t<_Vp>>)
class join_view : public view_interface<join_view<_Vp>>
{
private:
using _InnerRange = range_reference_t<_Vp>;
template<bool _Const>
struct _Sentinel;
template<bool _Const>
struct _Iterator
{
private:
using _Parent = __detail::__maybe_const_t<_Const, join_view>;
using _Base = __detail::__maybe_const_t<_Const, _Vp>;
static constexpr bool _S_ref_is_glvalue
= is_reference_v<range_reference_t<_Base>>;
constexpr void
_M_satisfy()
{
auto __update_inner = [this] (range_reference_t<_Base> __x) -> auto&
{
if constexpr (_S_ref_is_glvalue)
return __x;
else
return (_M_parent->_M_inner = views::all(std::move(__x)));
};
for (; _M_outer != ranges::end(_M_parent->_M_base); ++_M_outer)
{
auto& inner = __update_inner(*_M_outer);
_M_inner = ranges::begin(inner);
if (_M_inner != ranges::end(inner))
return;
}
if constexpr (_S_ref_is_glvalue)
_M_inner = _Inner_iter();
}
static constexpr auto
_S_iter_concept()
{
if constexpr (_S_ref_is_glvalue
&& bidirectional_range<_Base>
&& bidirectional_range<range_reference_t<_Base>>)
return bidirectional_iterator_tag{};
else if constexpr (_S_ref_is_glvalue
&& forward_range<_Base>
&& forward_range<range_reference_t<_Base>>)
return forward_iterator_tag{};
else
return input_iterator_tag{};
}
static constexpr auto
_S_iter_cat()
{
using _OuterCat
= typename iterator_traits<_Outer_iter>::iterator_category;
using _InnerCat
= typename iterator_traits<_Inner_iter>::iterator_category;
if constexpr (_S_ref_is_glvalue
&& derived_from<_OuterCat, bidirectional_iterator_tag>
&& derived_from<_InnerCat, bidirectional_iterator_tag>)
return bidirectional_iterator_tag{};
else if constexpr (_S_ref_is_glvalue
&& derived_from<_OuterCat, forward_iterator_tag>
&& derived_from<_InnerCat, forward_iterator_tag>)
return forward_iterator_tag{};
else if constexpr (derived_from<_OuterCat, input_iterator_tag>
&& derived_from<_InnerCat, input_iterator_tag>)
return input_iterator_tag{};
else
return output_iterator_tag{};
}
using _Outer_iter = iterator_t<_Base>;
using _Inner_iter = iterator_t<range_reference_t<_Base>>;
_Outer_iter _M_outer = _Outer_iter();
_Inner_iter _M_inner = _Inner_iter();
_Parent* _M_parent = nullptr;
public:
using iterator_concept = decltype(_S_iter_concept());
using iterator_category = decltype(_S_iter_cat());
using value_type = range_value_t<range_reference_t<_Base>>;
using difference_type
= common_type_t<range_difference_t<_Base>,
range_difference_t<range_reference_t<_Base>>>;
_Iterator() = default;
constexpr
_Iterator(_Parent& __parent, _Outer_iter __outer)
: _M_outer(std::move(__outer)),
_M_parent(std::__addressof(__parent))
{ _M_satisfy(); }
constexpr
_Iterator(_Iterator<!_Const> __i)
requires _Const
&& convertible_to<iterator_t<_Vp>, _Outer_iter>
&& convertible_to<iterator_t<_InnerRange>, _Inner_iter>
: _M_outer(std::move(__i._M_outer)), _M_inner(__i._M_inner),
_M_parent(__i._M_parent)
{ }
constexpr decltype(auto)
operator*() const
{ return *_M_inner; }
constexpr _Outer_iter
operator->() const
requires __detail::__has_arrow<_Outer_iter>
&& copyable<_Outer_iter>
{ return _M_inner; }
constexpr _Iterator&
operator++()
{
auto&& __inner_range = [this] () -> decltype(auto) {
if constexpr (_S_ref_is_glvalue)
return *_M_outer;
else
return _M_parent->_M_inner;
}();
if (++_M_inner == ranges::end(__inner_range))
{
++_M_outer;
_M_satisfy();
}
return *this;
}
constexpr void
operator++(int)
{ ++*this; }
constexpr _Iterator
operator++(int)
requires _S_ref_is_glvalue && forward_range<_Base>
&& forward_range<range_reference_t<_Base>>
{
auto __tmp = *this;
++*this;
return __tmp;
}
constexpr _Iterator&
operator--()
requires _S_ref_is_glvalue && bidirectional_range<_Base>
&& bidirectional_range<range_reference_t<_Base>>
&& common_range<range_reference_t<_Base>>
{
if (_M_outer == ranges::end(_M_parent->_M_base))
_M_inner = ranges::end(*--_M_outer);
while (_M_inner == ranges::begin(*_M_outer))
_M_inner = ranges::end(*--_M_outer);
--_M_inner;
return *this;
}
constexpr _Iterator
operator--(int)
requires _S_ref_is_glvalue && bidirectional_range<_Base>
&& bidirectional_range<range_reference_t<_Base>>
&& common_range<range_reference_t<_Base>>
{
auto __tmp = *this;
--*this;
return __tmp;
}
friend constexpr bool
operator==(const _Iterator& __x, const _Iterator& __y)
requires _S_ref_is_glvalue
&& equality_comparable<_Outer_iter>
&& equality_comparable<_Inner_iter>
{
return (__x._M_outer == __y._M_outer
&& __x._M_inner == __y._M_inner);
}
friend constexpr decltype(auto)
iter_move(const _Iterator& __i)
noexcept(noexcept(ranges::iter_move(__i._M_inner)))
{ return ranges::iter_move(__i._M_inner); }
friend constexpr void
iter_swap(const _Iterator& __x, const _Iterator& __y)
noexcept(noexcept(ranges::iter_swap(__x._M_inner, __y._M_inner)))
{ return ranges::iter_swap(__x._M_inner, __y._M_inner); }
friend _Iterator<!_Const>;
template<bool> friend struct _Sentinel;
};
template<bool _Const>
struct _Sentinel
{
private:
using _Parent = __detail::__maybe_const_t<_Const, join_view>;
using _Base = __detail::__maybe_const_t<_Const, _Vp>;
template<bool _Const2>
constexpr bool
__equal(const _Iterator<_Const2>& __i) const
{ return __i._M_outer == _M_end; }
sentinel_t<_Base> _M_end = sentinel_t<_Base>();
public:
_Sentinel() = default;
constexpr explicit
_Sentinel(_Parent& __parent)
: _M_end(ranges::end(__parent._M_base))
{ }
constexpr
_Sentinel(_Sentinel<!_Const> __s)
requires _Const && convertible_to<sentinel_t<_Vp>, sentinel_t<_Base>>
: _M_end(std::move(__s._M_end))
{ }
template<bool _Const2>
requires sentinel_for<sentinel_t<_Base>,
iterator_t<__detail::__maybe_const_t<_Const2, _Vp>>>
friend constexpr bool
operator==(const _Iterator<_Const2>& __x, const _Sentinel& __y)
{ return __y.__equal(__x); }
friend _Sentinel<!_Const>;
};
_Vp _M_base = _Vp();
// XXX: _M_inner is "present only when !is_reference_v<_InnerRange>"
[[no_unique_address]]
__detail::__maybe_present_t<!is_reference_v<_InnerRange>,
views::all_t<_InnerRange>> _M_inner;
public:
join_view() = default;
constexpr explicit
join_view(_Vp __base)
: _M_base(std::move(__base))
{ }
constexpr _Vp
base() const& requires copy_constructible<_Vp>
{ return _M_base; }
constexpr _Vp
base() &&
{ return std::move(_M_base); }
constexpr auto
begin()
{
constexpr bool __use_const
= (__detail::__simple_view<_Vp>
&& is_reference_v<range_reference_t<_Vp>>);
return _Iterator<__use_const>{*this, ranges::begin(_M_base)};
}
constexpr auto
begin() const
requires input_range<const _Vp>
&& is_reference_v<range_reference_t<const _Vp>>
{
return _Iterator<true>{*this, ranges::begin(_M_base)};
}
constexpr auto
end()
{
if constexpr (forward_range<_Vp> && is_reference_v<_InnerRange>
&& forward_range<_InnerRange>
&& common_range<_Vp> && common_range<_InnerRange>)
return _Iterator<__detail::__simple_view<_Vp>>{*this,
ranges::end(_M_base)};
else
return _Sentinel<__detail::__simple_view<_Vp>>{*this};
}
constexpr auto
end() const
requires input_range<const _Vp>
&& is_reference_v<range_reference_t<const _Vp>>
{
if constexpr (forward_range<const _Vp>
&& is_reference_v<range_reference_t<const _Vp>>
&& forward_range<range_reference_t<const _Vp>>
&& common_range<const _Vp>
&& common_range<range_reference_t<const _Vp>>)
return _Iterator<true>{*this, ranges::end(_M_base)};
else
return _Sentinel<true>{*this};
}
};
template<typename _Range>
explicit join_view(_Range&&) -> join_view<views::all_t<_Range>>;
// _GLIBCXX_RESOLVE_LIB_DEFECTS
// 3474. Nesting join_views is broken because of CTAD
template<typename _View>
explicit join_view(join_view<_View>) -> join_view<join_view<_View>>;
namespace views
{
inline constexpr __adaptor::_RangeAdaptorClosure join
= [] <viewable_range _Range> (_Range&& __r)
{
return join_view{std::forward<_Range>(__r)};
};
} // namespace views
namespace __detail
{
template<auto>
struct __require_constant;
template<typename _Range>
concept __tiny_range = sized_range<_Range>
&& requires
{ typename __require_constant<remove_reference_t<_Range>::size()>; }
&& (remove_reference_t<_Range>::size() <= 1);
}
template<input_range _Vp, forward_range _Pattern>
requires view<_Vp> && view<_Pattern>
&& indirectly_comparable<iterator_t<_Vp>, iterator_t<_Pattern>,
ranges::equal_to>
&& (forward_range<_Vp> || __detail::__tiny_range<_Pattern>)
class split_view : public view_interface<split_view<_Vp, _Pattern>>
{
private:
template<bool _Const>
struct _InnerIter;
template<bool _Const>
struct _OuterIter
{
private:
using _Parent = __detail::__maybe_const_t<_Const, split_view>;
using _Base = __detail::__maybe_const_t<_Const, _Vp>;
constexpr bool
__at_end() const
{ return __current() == ranges::end(_M_parent->_M_base); }
// [range.split.outer] p1
// Many of the following specifications refer to the notional member
// current of outer-iterator. current is equivalent to current_ if
// V models forward_range, and parent_->current_ otherwise.
constexpr auto&
__current() noexcept
{
if constexpr (forward_range<_Vp>)
return _M_current;
else
return _M_parent->_M_current;
}
constexpr auto&
__current() const noexcept
{
if constexpr (forward_range<_Vp>)
return _M_current;
else
return _M_parent->_M_current;
}
_Parent* _M_parent = nullptr;
// XXX: _M_current is present only if "V models forward_range"
[[no_unique_address]]
__detail::__maybe_present_t<forward_range<_Vp>,
iterator_t<_Base>> _M_current;
public:
using iterator_concept = conditional_t<forward_range<_Base>,
forward_iterator_tag,
input_iterator_tag>;
using iterator_category = input_iterator_tag;
using difference_type = range_difference_t<_Base>;
struct value_type : view_interface<value_type>
{
private:
_OuterIter _M_i = _OuterIter();
public:
value_type() = default;
constexpr explicit
value_type(_OuterIter __i)
: _M_i(std::move(__i))
{ }
constexpr _InnerIter<_Const>
begin() const
requires copyable<_OuterIter>
{ return _InnerIter<_Const>{_M_i}; }
constexpr _InnerIter<_Const>
begin()
requires (!copyable<_OuterIter>)
{ return _InnerIter<_Const>{std::move(_M_i)}; }
constexpr default_sentinel_t
end() const
{ return default_sentinel; }
};
_OuterIter() = default;
constexpr explicit
_OuterIter(_Parent& __parent) requires (!forward_range<_Base>)
: _M_parent(std::__addressof(__parent))
{ }
constexpr
_OuterIter(_Parent& __parent, iterator_t<_Base> __current)
requires forward_range<_Base>
: _M_parent(std::__addressof(__parent)),
_M_current(std::move(__current))
{ }
constexpr
_OuterIter(_OuterIter<!_Const> __i)
requires _Const
&& convertible_to<iterator_t<_Vp>, iterator_t<_Base>>
: _M_parent(__i._M_parent), _M_current(std::move(__i._M_current))
{ }
constexpr value_type
operator*() const
{ return value_type{*this}; }
constexpr _OuterIter&
operator++()
{
const auto __end = ranges::end(_M_parent->_M_base);
if (__current() == __end)
return *this;
const auto [__pbegin, __pend] = subrange{_M_parent->_M_pattern};
if (__pbegin == __pend)
++__current();
else
do
{
auto [__b, __p]
= __detail::mismatch(std::move(__current()), __end,
__pbegin, __pend);
__current() = std::move(__b);
if (__p == __pend)
break;
} while (++__current() != __end);
return *this;
}
constexpr decltype(auto)
operator++(int)
{
if constexpr (forward_range<_Base>)
{
auto __tmp = *this;
++*this;
return __tmp;
}
else
++*this;
}
friend constexpr bool
operator==(const _OuterIter& __x, const _OuterIter& __y)
requires forward_range<_Base>
{ return __x._M_current == __y._M_current; }
friend constexpr bool
operator==(const _OuterIter& __x, default_sentinel_t)
{ return __x.__at_end(); };
friend _OuterIter<!_Const>;
friend _InnerIter<_Const>;
};
template<bool _Const>
struct _InnerIter
{
private:
using _Base = __detail::__maybe_const_t<_Const, _Vp>;
constexpr bool
__at_end() const
{
auto [__pcur, __pend] = subrange{_M_i._M_parent->_M_pattern};
auto __end = ranges::end(_M_i._M_parent->_M_base);
if constexpr (__detail::__tiny_range<_Pattern>)
{
const auto& __cur = _M_i_current();
if (__cur == __end)
return true;
if (__pcur == __pend)
return _M_incremented;
return *__cur == *__pcur;
}
else
{
auto __cur = _M_i_current();
if (__cur == __end)
return true;
if (__pcur == __pend)
return _M_incremented;
do
{
if (*__cur != *__pcur)
return false;
if (++__pcur == __pend)
return true;
} while (++__cur != __end);
return false;
}
}
static constexpr auto
_S_iter_cat()
{
using _Cat
= typename iterator_traits<iterator_t<_Base>>::iterator_category;
if constexpr (derived_from<_Cat, forward_iterator_tag>)
return forward_iterator_tag{};
else
return _Cat{};
}
constexpr auto&
_M_i_current() noexcept
{ return _M_i.__current(); }
constexpr auto&
_M_i_current() const noexcept
{ return _M_i.__current(); }
_OuterIter<_Const> _M_i = _OuterIter<_Const>();
bool _M_incremented = false;
public:
using iterator_concept
= typename _OuterIter<_Const>::iterator_concept;
using iterator_category = decltype(_S_iter_cat());
using value_type = range_value_t<_Base>;
using difference_type = range_difference_t<_Base>;
_InnerIter() = default;
constexpr explicit
_InnerIter(_OuterIter<_Const> __i)
: _M_i(std::move(__i))
{ }
constexpr decltype(auto)
operator*() const
{ return *_M_i_current(); }
constexpr _InnerIter&
operator++()
{
_M_incremented = true;
if constexpr (!forward_range<_Base>)
if constexpr (_Pattern::size() == 0)
return *this;
++_M_i_current();
return *this;
}
constexpr decltype(auto)
operator++(int)
{
if constexpr (forward_range<_Vp>)
{
auto __tmp = *this;
++*this;
return __tmp;
}
else
++*this;
}
friend constexpr bool
operator==(const _InnerIter& __x, const _InnerIter& __y)
requires forward_range<_Base>
{ return __x._M_i == __y._M_i; }
friend constexpr bool
operator==(const _InnerIter& __x, default_sentinel_t)
{ return __x.__at_end(); }
friend constexpr decltype(auto)
iter_move(const _InnerIter& __i)
noexcept(noexcept(ranges::iter_move(__i._M_i_current())))
{ return ranges::iter_move(__i._M_i_current()); }
friend constexpr void
iter_swap(const _InnerIter& __x, const _InnerIter& __y)
noexcept(noexcept(ranges::iter_swap(__x._M_i_current(),
__y._M_i_current())))
requires indirectly_swappable<iterator_t<_Base>>
{ ranges::iter_swap(__x._M_i_current(), __y._M_i_current()); }
};
_Vp _M_base = _Vp();
_Pattern _M_pattern = _Pattern();
// XXX: _M_current is "present only if !forward_range<V>"
[[no_unique_address]]
__detail::__maybe_present_t<!forward_range<_Vp>, iterator_t<_Vp>>
_M_current;
public:
split_view() = default;
constexpr
split_view(_Vp __base, _Pattern __pattern)
: _M_base(std::move(__base)), _M_pattern(std::move(__pattern))
{ }
template<input_range _Range>
requires constructible_from<_Vp, views::all_t<_Range>>
&& constructible_from<_Pattern, single_view<range_value_t<_Range>>>
constexpr
split_view(_Range&& __r, range_value_t<_Range> __e)
: _M_base(views::all(std::forward<_Range>(__r))),
_M_pattern(std::move(__e))
{ }
constexpr _Vp
base() const& requires copy_constructible<_Vp>
{ return _M_base; }
constexpr _Vp
base() &&
{ return std::move(_M_base); }
constexpr auto
begin()
{
if constexpr (forward_range<_Vp>)
return _OuterIter<__detail::__simple_view<_Vp>>{
*this, ranges::begin(_M_base)};
else
{
_M_current = ranges::begin(_M_base);
return _OuterIter<false>{*this};
}
}
constexpr auto
begin() const requires forward_range<_Vp> && forward_range<const _Vp>
{
return _OuterIter<true>{*this, ranges::begin(_M_base)};
}
constexpr auto
end() requires forward_range<_Vp> && common_range<_Vp>
{
return _OuterIter<__detail::__simple_view<_Vp>>{
*this, ranges::end(_M_base)};
}
constexpr auto
end() const
{
if constexpr (forward_range<_Vp>
&& forward_range<const _Vp>
&& common_range<const _Vp>)
return _OuterIter<true>{*this, ranges::end(_M_base)};
else
return default_sentinel;
}
};
template<typename _Range, typename _Pred>
split_view(_Range&&, _Pred&&)
-> split_view<views::all_t<_Range>, views::all_t<_Pred>>;
template<input_range _Range>
split_view(_Range&&, range_value_t<_Range>)
-> split_view<views::all_t<_Range>, single_view<range_value_t<_Range>>>;
namespace views
{
inline constexpr __adaptor::_RangeAdaptor split
= [] <viewable_range _Range, typename _Fp> (_Range&& __r, _Fp&& __f)
{
return split_view{std::forward<_Range>(__r), std::forward<_Fp>(__f)};
};
} // namespace views
namespace views
{
struct _Counted
{
template<input_or_output_iterator _Iter>
constexpr auto
operator()(_Iter __i, iter_difference_t<_Iter> __n) const
{
if constexpr (random_access_iterator<_Iter>)
return subrange{__i, __i + __n};
else
return subrange{counted_iterator{std::move(__i), __n},
default_sentinel};
}
};
inline constexpr _Counted counted{};
} // namespace views
template<view _Vp>
requires (!common_range<_Vp>) && copyable<iterator_t<_Vp>>
class common_view : public view_interface<common_view<_Vp>>
{
private:
_Vp _M_base = _Vp();
public:
common_view() = default;
constexpr explicit
common_view(_Vp __r)
: _M_base(std::move(__r))
{ }
/* XXX: LWG 3280 didn't remove this constructor, but I think it should?
template<viewable_range _Range>
requires (!common_range<_Range>)
&& constructible_from<_Vp, views::all_t<_Range>>
constexpr explicit
common_view(_Range&& __r)
: _M_base(views::all(std::forward<_Range>(__r)))
{ }
*/
constexpr _Vp
base() const& requires copy_constructible<_Vp>
{ return _M_base; }
constexpr _Vp
base() &&
{ return std::move(_M_base); }
constexpr auto
begin()
{
if constexpr (random_access_range<_Vp> && sized_range<_Vp>)
return ranges::begin(_M_base);
else
return common_iterator<iterator_t<_Vp>, sentinel_t<_Vp>>
(ranges::begin(_M_base));
}
constexpr auto
begin() const requires range<const _Vp>
{
if constexpr (random_access_range<const _Vp> && sized_range<const _Vp>)
return ranges::begin(_M_base);
else
return common_iterator<iterator_t<const _Vp>, sentinel_t<const _Vp>>
(ranges::begin(_M_base));
}
constexpr auto
end()
{
if constexpr (random_access_range<_Vp> && sized_range<_Vp>)
return ranges::begin(_M_base) + ranges::size(_M_base);
else
return common_iterator<iterator_t<_Vp>, sentinel_t<_Vp>>
(ranges::end(_M_base));
}
constexpr auto
end() const requires range<const _Vp>
{
if constexpr (random_access_range<const _Vp> && sized_range<const _Vp>)
return ranges::begin(_M_base) + ranges::size(_M_base);
else
return common_iterator<iterator_t<const _Vp>, sentinel_t<const _Vp>>
(ranges::end(_M_base));
}
constexpr auto
size() requires sized_range<_Vp>
{ return ranges::size(_M_base); }
constexpr auto
size() const requires sized_range<const _Vp>
{ return ranges::size(_M_base); }
};
template<typename _Range>
common_view(_Range&&) -> common_view<views::all_t<_Range>>;
namespace views
{
inline constexpr __adaptor::_RangeAdaptorClosure common
= [] <viewable_range _Range> (_Range&& __r)
{
if constexpr (common_range<_Range>
&& requires { views::all(std::forward<_Range>(__r)); })
return views::all(std::forward<_Range>(__r));
else
return common_view{std::forward<_Range>(__r)};
};
} // namespace views
template<view _Vp>
requires bidirectional_range<_Vp>
class reverse_view : public view_interface<reverse_view<_Vp>>
{
private:
_Vp _M_base = _Vp();
static constexpr bool _S_needs_cached_begin
= !common_range<_Vp> && !random_access_range<_Vp>;
[[no_unique_address]]
__detail::__maybe_present_t<_S_needs_cached_begin,
__detail::_CachedPosition<_Vp>>
_M_cached_begin;
public:
reverse_view() = default;
constexpr explicit
reverse_view(_Vp __r)
: _M_base(std::move(__r))
{ }
constexpr _Vp
base() const& requires copy_constructible<_Vp>
{ return _M_base; }
constexpr _Vp
base() &&
{ return std::move(_M_base); }
constexpr reverse_iterator<iterator_t<_Vp>>
begin()
{
if constexpr (_S_needs_cached_begin)
if (_M_cached_begin._M_has_value())
return make_reverse_iterator(_M_cached_begin._M_get(_M_base));
auto __it = ranges::next(ranges::begin(_M_base), ranges::end(_M_base));
if constexpr (_S_needs_cached_begin)
_M_cached_begin._M_set(_M_base, __it);
return make_reverse_iterator(std::move(__it));
}
constexpr auto
begin() requires common_range<_Vp>
{ return make_reverse_iterator(ranges::end(_M_base)); }
constexpr auto
begin() const requires common_range<const _Vp>
{ return make_reverse_iterator(ranges::end(_M_base)); }
constexpr reverse_iterator<iterator_t<_Vp>>
end()
{ return make_reverse_iterator(ranges::begin(_M_base)); }
constexpr auto
end() const requires common_range<const _Vp>
{ return make_reverse_iterator(ranges::begin(_M_base)); }
constexpr auto
size() requires sized_range<_Vp>
{ return ranges::size(_M_base); }
constexpr auto
size() const requires sized_range<const _Vp>
{ return ranges::size(_M_base); }
};
template<typename _Range>
reverse_view(_Range&&) -> reverse_view<views::all_t<_Range>>;
namespace views
{
namespace __detail
{
template<typename>
inline constexpr bool __is_reversible_subrange = false;
template<typename _Iter, subrange_kind _Kind>
inline constexpr bool
__is_reversible_subrange<subrange<reverse_iterator<_Iter>,
reverse_iterator<_Iter>,
_Kind>> = true;
template<typename>
inline constexpr bool __is_reverse_view = false;
template<typename _Vp>
inline constexpr bool __is_reverse_view<reverse_view<_Vp>> = true;
}
inline constexpr __adaptor::_RangeAdaptorClosure reverse
= [] <viewable_range _Range> (_Range&& __r)
{
using _Tp = remove_cvref_t<_Range>;
if constexpr (__detail::__is_reverse_view<_Tp>)
return std::forward<_Range>(__r).base();
else if constexpr (__detail::__is_reversible_subrange<_Tp>)
{
using _Iter = decltype(ranges::begin(__r).base());
if constexpr (sized_range<_Tp>)
return subrange<_Iter, _Iter, subrange_kind::sized>
(__r.end().base(), __r.begin().base(), __r.size());
else
return subrange<_Iter, _Iter, subrange_kind::unsized>
(__r.end().base(), __r.begin().base());
}
else
return reverse_view{std::forward<_Range>(__r)};
};
} // namespace views
namespace __detail
{
template<typename _Tp, size_t _Nm>
concept __has_tuple_element = requires(_Tp __t)
{
typename tuple_size<_Tp>::type;
requires _Nm < tuple_size_v<_Tp>;
typename tuple_element_t<_Nm, _Tp>;
{ std::get<_Nm>(__t) }
-> convertible_to<const tuple_element_t<_Nm, _Tp>&>;
};
}
template<input_range _Vp, size_t _Nm>
requires view<_Vp>
&& __detail::__has_tuple_element<range_value_t<_Vp>, _Nm>
&& __detail::__has_tuple_element<remove_reference_t<range_reference_t<_Vp>>,
_Nm>
class elements_view : public view_interface<elements_view<_Vp, _Nm>>
{
public:
elements_view() = default;
constexpr explicit
elements_view(_Vp base)
: _M_base(std::move(base))
{ }
constexpr _Vp
base() const& requires copy_constructible<_Vp>
{ return _M_base; }
constexpr _Vp
base() &&
{ return std::move(_M_base); }
constexpr auto
begin() requires (!__detail::__simple_view<_Vp>)
{ return _Iterator<false>(ranges::begin(_M_base)); }
constexpr auto
begin() const requires range<const _Vp>
{ return _Iterator<true>(ranges::begin(_M_base)); }
constexpr auto
end() requires (!__detail::__simple_view<_Vp> && !common_range<_Vp>)
{ return _Sentinel<false>{ranges::end(_M_base)}; }
constexpr auto
end() requires (!__detail::__simple_view<_Vp> && common_range<_Vp>)
{ return _Iterator<false>{ranges::end(_M_base)}; }
constexpr auto
end() const requires range<const _Vp>
{ return _Sentinel<true>{ranges::end(_M_base)}; }
constexpr auto
end() const requires common_range<const _Vp>
{ return _Iterator<true>{ranges::end(_M_base)}; }
constexpr auto
size() requires sized_range<_Vp>
{ return ranges::size(_M_base); }
constexpr auto
size() const requires sized_range<const _Vp>
{ return ranges::size(_M_base); }
private:
template<bool _Const>
struct _Sentinel;
template<bool _Const>
struct _Iterator
{
using _Base = __detail::__maybe_const_t<_Const, _Vp>;
iterator_t<_Base> _M_current = iterator_t<_Base>();
friend _Iterator<!_Const>;
public:
using iterator_category
= typename iterator_traits<iterator_t<_Base>>::iterator_category;
using value_type
= remove_cvref_t<tuple_element_t<_Nm, range_value_t<_Base>>>;
using difference_type = range_difference_t<_Base>;
_Iterator() = default;
constexpr explicit
_Iterator(iterator_t<_Base> current)
: _M_current(std::move(current))
{ }
constexpr
_Iterator(_Iterator<!_Const> i)
requires _Const && convertible_to<iterator_t<_Vp>, iterator_t<_Base>>
: _M_current(std::move(i._M_current))
{ }
constexpr iterator_t<_Base>
base() const&
requires copyable<iterator_t<_Base>>
{ return _M_current; }
constexpr iterator_t<_Base>
base() &&
{ return std::move(_M_current); }
constexpr decltype(auto)
operator*() const
{ return std::get<_Nm>(*_M_current); }
constexpr _Iterator&
operator++()
{
++_M_current;
return *this;
}
constexpr void
operator++(int) requires (!forward_range<_Base>)
{ ++_M_current; }
constexpr _Iterator
operator++(int) requires forward_range<_Base>
{
auto __tmp = *this;
++_M_current;
return __tmp;
}
constexpr _Iterator&
operator--() requires bidirectional_range<_Base>
{
--_M_current;
return *this;
}
constexpr _Iterator
operator--(int) requires bidirectional_range<_Base>
{
auto __tmp = *this;
--_M_current;
return __tmp;
}
constexpr _Iterator&
operator+=(difference_type __n)
requires random_access_range<_Base>
{
_M_current += __n;
return *this;
}
constexpr _Iterator&
operator-=(difference_type __n)
requires random_access_range<_Base>
{
_M_current -= __n;
return *this;
}
constexpr decltype(auto)
operator[](difference_type __n) const
requires random_access_range<_Base>
{ return std::get<_Nm>(*(_M_current + __n)); }
friend constexpr bool
operator==(const _Iterator& __x, const _Iterator& __y)
requires equality_comparable<iterator_t<_Base>>
{ return __x._M_current == __y._M_current; }
friend constexpr bool
operator<(const _Iterator& __x, const _Iterator& __y)
requires random_access_range<_Base>
{ return __x._M_current < __y._M_current; }
friend constexpr bool
operator>(const _Iterator& __x, const _Iterator& __y)
requires random_access_range<_Base>
{ return __y._M_current < __x._M_current; }
friend constexpr bool
operator<=(const _Iterator& __x, const _Iterator& __y)
requires random_access_range<_Base>
{ return !(__y._M_current > __x._M_current); }
friend constexpr bool
operator>=(const _Iterator& __x, const _Iterator& __y)
requires random_access_range<_Base>
{ return !(__x._M_current > __y._M_current); }
#ifdef __cpp_lib_three_way_comparison
friend constexpr auto
operator<=>(const _Iterator& __x, const _Iterator& __y)
requires random_access_range<_Base>
&& three_way_comparable<iterator_t<_Base>>
{ return __x._M_current <=> __y._M_current; }
#endif
friend constexpr _Iterator
operator+(const _Iterator& __x, difference_type __y)
requires random_access_range<_Base>
{ return _Iterator{__x} += __y; }
friend constexpr _Iterator
operator+(difference_type __x, const _Iterator& __y)
requires random_access_range<_Base>
{ return __y + __x; }
friend constexpr _Iterator
operator-(const _Iterator& __x, difference_type __y)
requires random_access_range<_Base>
{ return _Iterator{__x} -= __y; }
// _GLIBCXX_RESOLVE_LIB_DEFECTS
// 3483. transform_view::iterator's difference is overconstrained
friend constexpr difference_type
operator-(const _Iterator& __x, const _Iterator& __y)
requires sized_sentinel_for<iterator_t<_Base>, iterator_t<_Base>>
{ return __x._M_current - __y._M_current; }
friend _Sentinel<_Const>;
};
template<bool _Const>
struct _Sentinel
{
private:
constexpr bool
_M_equal(const _Iterator<_Const>& __x) const
{ return __x._M_current == _M_end; }
using _Base = __detail::__maybe_const_t<_Const, _Vp>;
sentinel_t<_Base> _M_end = sentinel_t<_Base>();
public:
_Sentinel() = default;
constexpr explicit
_Sentinel(sentinel_t<_Base> __end)
: _M_end(std::move(__end))
{ }
constexpr
_Sentinel(_Sentinel<!_Const> __other)
requires _Const
&& convertible_to<sentinel_t<_Vp>, sentinel_t<_Base>>
: _M_end(std::move(__other._M_end))
{ }
constexpr sentinel_t<_Base>
base() const
{ return _M_end; }
template<bool _Const2>
requires sentinel_for<sentinel_t<_Base>,
iterator_t<__detail::__maybe_const_t<_Const2, _Vp>>>
friend constexpr bool
operator==(const _Iterator<_Const2>& __x, const _Sentinel& __y)
{ return __y._M_equal(__x); }
template<bool _Const2,
typename _Base2 = __detail::__maybe_const_t<_Const2, _Vp>>
requires sized_sentinel_for<sentinel_t<_Base>, iterator_t<_Base2>>
friend constexpr range_difference_t<_Base2>
operator-(const _Iterator<_Const2>& __x, const _Sentinel& __y)
{ return __x._M_current - __y._M_end; }
template<bool _Const2,
typename _Base2 = __detail::__maybe_const_t<_Const2, _Vp>>
requires sized_sentinel_for<sentinel_t<_Base>, iterator_t<_Base2>>
friend constexpr range_difference_t<_Base>
operator-(const _Sentinel& __x, const _Iterator<_Const2>& __y)
{ return __x._M_end - __y._M_current; }
friend _Sentinel<!_Const>;
};
_Vp _M_base = _Vp();
};
template<typename _Range>
using keys_view = elements_view<views::all_t<_Range>, 0>;
template<typename _Range>
using values_view = elements_view<views::all_t<_Range>, 1>;
namespace views
{
template<size_t _Nm>
inline constexpr __adaptor::_RangeAdaptorClosure elements
= [] <viewable_range _Range> (_Range&& __r)
{
using _El = elements_view<views::all_t<_Range>, _Nm>;
return _El{std::forward<_Range>(__r)};
};
inline constexpr __adaptor::_RangeAdaptorClosure keys = elements<0>;
inline constexpr __adaptor::_RangeAdaptorClosure values = elements<1>;
} // namespace views
} // namespace ranges
namespace views = ranges::views;
template<typename _Iter, typename _Sent, ranges::subrange_kind _Kind>
struct tuple_size<ranges::subrange<_Iter, _Sent, _Kind>>
: integral_constant<size_t, 2>
{ };
template<typename _Iter, typename _Sent, ranges::subrange_kind _Kind>
struct tuple_element<0, ranges::subrange<_Iter, _Sent, _Kind>>
{ using type = _Iter; };
template<typename _Iter, typename _Sent, ranges::subrange_kind _Kind>
struct tuple_element<1, ranges::subrange<_Iter, _Sent, _Kind>>
{ using type = _Sent; };
template<typename _Iter, typename _Sent, ranges::subrange_kind _Kind>
struct tuple_element<0, const ranges::subrange<_Iter, _Sent, _Kind>>
{ using type = _Iter; };
template<typename _Iter, typename _Sent, ranges::subrange_kind _Kind>
struct tuple_element<1, const ranges::subrange<_Iter, _Sent, _Kind>>
{ using type = _Sent; };
_GLIBCXX_END_NAMESPACE_VERSION
} // namespace
#endif // library concepts
#endif // C++2a
#endif /* _GLIBCXX_RANGES */
|