aboutsummaryrefslogtreecommitdiffstats
path: root/gcc-4.9/libcilkrts/include/cilk/reducer_min_max.h
blob: 55f068c34a39c1ccceb770ca18ef8b5d8933866a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
/*  reducer_min_max.h                  -*- C++ -*-
 *
 *  @copyright
 *  Copyright (C) 2009-2013, Intel Corporation
 *  All rights reserved.
 *  
 *  @copyright
 *  Redistribution and use in source and binary forms, with or without
 *  modification, are permitted provided that the following conditions
 *  are met:
 *  
 *    * Redistributions of source code must retain the above copyright
 *      notice, this list of conditions and the following disclaimer.
 *    * Redistributions in binary form must reproduce the above copyright
 *      notice, this list of conditions and the following disclaimer in
 *      the documentation and/or other materials provided with the
 *      distribution.
 *    * Neither the name of Intel Corporation nor the names of its
 *      contributors may be used to endorse or promote products derived
 *      from this software without specific prior written permission.
 *  
 *  @copyright
 *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 *  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 *  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 *  A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 *  HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
 *  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
 *  BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
 *  OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
 *  AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 *  LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY
 *  WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 *  POSSIBILITY OF SUCH DAMAGE.
 */

/** @file reducer_min_max.h
 *
 *  @brief Defines classes for doing parallel minimum and maximum reductions.
 *
 *  @ingroup ReducersMinMax
 *
 *  @see ReducersMinMax
 */

#ifndef REDUCER_MIN_MAX_H_INCLUDED
#define REDUCER_MIN_MAX_H_INCLUDED

#include <cilk/reducer.h>

#ifdef __cplusplus

#include <algorithm>
#include <limits>

/** @defgroup ReducersMinMax Minimum and Maximum Reducers
 *
 *  Minimum and maximum reducers allow the computation of the minimum or
 *  maximum of a set of values in parallel.
 *
 *  @ingroup Reducers
 *
 *  You should be familiar with @ref pagereducers "Cilk reducers", described in
 *  file `reducers.md`, and particularly with @ref reducers_using, before trying
 *  to use the information in this file.
 *
 *  @section redminmax_usage Usage Examples
 *
 *      cilk::reducer< cilk::op_max<int> > rm;
 *      cilk_for (int i = 0; i < ARRAY_SIZE; ++i)
 *      {
 *          rm->calc_max(a[i]); // or *rm = cilk::max_of(*max, a[i])
 *      }
 *      std::cout << "maximum value is " << rm.get_value() << std::endl;
 *
 *  and
 *
 *      cilk::reducer< cilk::op_min_index<int, double> > rmi;
 *      cilk_for (int i = 0; i < ARRAY_SIZE; ++i)
 *      {
 *          rmi->calc_min(i, a[i]) // or *rmi = cilk::min_of(*rmi, i, a[i]);
 *      }
 *      std::cout << "minimum value a[" << rmi.get_value().first << "] = "
 *                << rmi.get_value().second << std::endl;
 *
 *  @section redminmax_monoid The Monoid
 *
 *  @subsection redminmax_monoid_values Value Set
 *
 *  The value set of a minimum or maximum reducer is the set of values of
 *  `Type`, possibly augmented with a special identity value which is greater
 *  than (less than) any value of `Type`.
 *
 *  @subsection redminmax_monoid_operator Operator
 *
 *  In the most common case, the operator of a minimum reducer is defined as
 *
 *      x MIN y == (x < y) ? x : y
 *
 *  Thus, `a1 MIN a2 MIN … an` is the first `ai` which is not greater than any
 *  other `ai`.
 *
 *  The operator of a maximum reducer is defined as
 *
 *      x MAX y == (x > y) ? x : y
 *
 *  Thus, `a1 MAX a2 MAX … an` is the first `ai` which is not less than any
 *  other `ai`.
 *
 *  @subsection redminmax_monoid_comparators Comparators
 *
 *  Min/max reducers are not limited to finding the minimum or maximum value
 *  determined by the `<` or `>` operator. In fact, all min/max reducers use a
 *  _comparator_, which is either a function or an object of a function class
 *  that defines a [strict weak ordering] 
 *  (http://en.wikipedia.org/wiki/Strict_weak_ordering#Strict_weak_orderings)
 *  on a set of values. (This is exactly the same as the requirement for the
 *  comparison predicate for STL associative containers and sorting
 *  algorithms.)
 *
 *  Just as with STL algorithms and containers, the comparator type parameter
 *  for min/max reducers is optional. If it is omitted, it defaults to
 *  `std::less`, which gives the behavior described in the previous section.
 *  Using non-default comparators (anything other than `std::less`) with 
 *  min/max reducers is just like using them with STL containers and 
 *  algorithms.
 *
 *  Taking comparator objects into account, the reduction operation `MIN` for a
 *  minimum reducer is defined as
 *
 *      x MIN y == compare(x, y) ? x : y
 *
 *  where `compare()` is the reducer’s comparator. Similarly, the reduction
 *  operation MAX for a maximum reducer is defined as
 *
 *      x MAX y == compare(y, x) ? x : y
 *
 *  (If `compare(x, y) == x < y`, then `compare(y, x) == x > y`.) 
 *
 *  @subsection redminmax_monoid_identity Identity
 *
 *  The identity value of the reducer is the value which is greater than (less
 *  than) any other value in the value set of the reducer. This is the 
 *  [“special identity value”](#redminmax_monoid_values) if the reducer has
 *  one, or the largest (smallest) value in the value set otherwise.
 *
 *  @section redminmax_index Value and Index Reducers
 *
 *  Min/max reducers come in two families. The _value_ reducers, using `op_min`
 *  and `op_max` monoids, simply find the smallest or largest value from a set
 *  of values. The _index_ reducers, using `op_min_index` and `op_max_index` 
 *  monoids, also record an index value associated with the first occurrence of
 *  the smallest or largest value.
 *
 *  In the `%op_min_index` usage example [above](#redminmax_usage), the values
 *  are taken from an array, and the index of a value is the index of the array
 *  element it comes from. More generally, though, an index can be any sort of
 *  key which identifies a particular value in a collection of values. For
 *  example, if the values were taken from the nodes of a tree, then the 
 *  “index” of a value might be a pointer to the node containing that value. 
 *
 *  A min/max index reducer is essentially the same as a min/max value reducer
 *  whose value type is an (index, value) pair, and whose comparator ignores 
 *  the index part of the pair. (index, value) pairs are represented by
 *  `std::pair<Index, Type>` objects. This has the consequence that wherever 
 *  the interface of a min/max value reducer has a `Type`, the interface of the
 *  corresponding min/max index reducer has a `std::pair<Index, Type>`. (There
 *  are convenience variants of the `reducer(Type)` constructor and the
 *  `calc_min()`, `calc_max()`, `%min_of()`, and `%max_of()` functions that
 *  take an index argument and a value argument instead of an index/value 
 *  pair.)
 *
 *  @section redminmax_operations Operations
 *
 *  @subsection redminmax_constructors Constructors
 *
 *  @subsubsection redminmax_constructors_value Min/Max Value Reducers
 *
 *      reducer()                           // identity
 *      reducer(const Compare& compare)     // identity
 *      reducer(const Type& value)
 *      reducer(move_in(Type& variable))
 *      reducer(const Type& value, const Compare& compare)
 *      reducer(move_in(Type& variable), const Compare& compare)
 *
 *  @subsubsection redminmax_constructors_index Min/Max Index Reducers
 *
 *      reducer()                           // identity
 *      reducer(const Compare& compare)     // identity
 *      reducer(const std::pair<Index, Type>& pair)
 *      reducer(const Index& index, const Type& value)
 *      reducer(move_in(std::pair<Index, Type>& variable))
 *      reducer(const std::pair<Index, Type>& pair, const Compare& compare)
 *      reducer(const Index& index, const Type& value, const Compare& compare)
 *      reducer(move_in(std::pair<Index, Type>& variable), const Compare& compare)
 *
 *  @subsection redminmax_get_set Set and Get
 *
 *      r.set_value(const Type& value)
 *      Type = r.get_value() const
 *      r.move_in(Type& variable)
 *      r.move_out(Type& variable)
 *
 *  Note that for an index reducer, the `Type` in these operations is actually a
 *  `std::pair<Index, Type>`. (See @ref redminmax_index.) There is _not_ a
 *  `set_value(value, index)` operation.
 *
 *  @subsection redminmax_initial Initial Values and is_set()
 *
 *  A minimum or maximum reducer without a specified initial value, before any
 *  MIN or MAX operation has been performed on it, represents the [identity
 *  value](#redminmax_monoid_identity) of its monoid. For value reducers with a
 *  numeric type and default comparator (`std::less`), this will be a well
 *  defined value. For example,
 *
 *      reducer< op_max<unsigned> > r1;
 *      // r1.get_value() == 0
 *
 *      reducer< op_min<float> > r2;
 *      // r2.get_value() == std::numeric_limits<float>::infinity
 *
 *  In other cases, though (index reducers, non-numeric types, or non-default
 *  comparators), the actual identity value for the monoid may be unknown, or
 *  it may not even be a value of the reducer’s type. For example, there is no
 *  “largest string” to serve as the initial value for a 
 *  `reducer< op_min<std::string> >`. In these cases, the result of calling
 *  `get_value()` is undefined.
 *
 *  To avoid calling `get_value()` when its result is undefined, you can call
 *  the view’s `is_set()` function, which will return true  if the reducer
 *  has a well-defined value — either because a MIN or MAX operation has been
 *  performed, or because it had a well-defined initial value:
 *
 *      reducer< op_max<unsigned> > r1;
 *      // r1->is_set() == true
 *      // r1.get_value() == 0
 *
 *      reducer< op_min<std::string> > r2;
 *      // r2->is_set() == false
 *      // r2.get_value() is undefined
 *      r2->calc_min("xyzzy");
 *      // r2->is_set() == true
 *      // r2.get_value() == "xyzzy"
 *
 *  >   Note: For an index reducer without a specified initial value, the 
 *  >   initial value of the index is the default value of the `Index` type.
 *
 *  @subsection redminmax_view_ops View Operations
 *
 *  The basic reduction operation is `x = x MIN a` for a minimum reducer, or 
 *  `x = x MAX a` for a maximum reducer. The basic syntax for these operations
 *  uses the `calc_min()` and `calc_max()` member functions of the view class.
 *  An assignment syntax is also provided, using the %cilk::min_of() and
 *  %cilk::max_of() global functions:
 *
 *  Class          | Modifier            | Assignment
 *  ---------------|---------------------|-----------
 *  `op_min`       | `r->calc_min(x)`    | `*r = min_of(*r, x)` or `*r = min_of(x, *r)`
 *  `op_max`       | `r->calc_max(x)`    | `*r = max_of(*r, x)` or `*r = max_of(x, *r)`
 *  `op_min_index` | `r->calc_min(i, x)` | `*r = min_of(*r, i, x)` or `*r = min_of(i, x, *r)`
 *  `op_max_index` | `r->calc_max(i, x)` | `*r = max_of(*r, i, x)` or `*r = max_of(i, x, *r)`
 *
 *  Wherever an “`i`, `x`” argument pair is shown in the table above, a single
 *  pair argument may be passed instead. For example:
 *
 *      Index index;
 *      Type value;
 *      std::pair<Index, Type> ind_val(index, value);
 *      // The following statements are all equivalent.
 *      r->calc_min(index, value);
 *      r->calc_min(ind_val);
 *      *r = min_of(*r, index, value);
 *      *r = min_of(*r, ind_val);
 *
 *  The `calc_min()` and `calc_max()` member functions return a reference to 
 *  the view, so they can be chained:
 *
 *      r->calc_max(x).calc_max(y).calc_max(z);
 *
 *  In a `%min_of()` or `%max_of()` assignment, the view on the left-hand side
 *  of the assignment must be the same as the view argument in the call.
 *  Otherwise, the behavior is undefined (but an assertion error will occur if
 *  the code is compiled with debugging enabled).
 *
 *      *r = max_of(*r, x);     // OK
 *      *r1 = max_of(*r2, y);   // ERROR
 *
 *  `%min_of()` and `%max_of()` calls can be nested:
 *
 *      *r = max_of(max_of(max_of(*r, x), y), z);
 *      *r = min_of(i, a[i], min_of(j, a[j], min_of(k, a[k], *r)));
 *
 *  @section redminmax_compatibility Compatibility Issues
 *
 *  Most Cilk library reducers provide
 *  *   Binary compatibility between `reducer_KIND` reducers compiled with Cilk
 *      library version 0.9 (distributed with Intel® C++ Composer XE version
 *      13.0 and earlier) and the same reducers compiled with Cilk library
 *      version 1.0 and later.
 *  *   Transparent casting between references to `reducer<op_KIND>` and
 *      `reducer_KIND`.
 *
 *  This compatibility is not available in all cases for min/max reducers. 
 *  There are two areas of incompatibility.
 *
 *  @subsection redminmax_compatibility_stateful Non-empty Comparators
 *
 *  There is no way to provide binary compatibility between the 0.9 and 1.0
 *  definitions of min/max reducers that use a non-empty comparator class or a
 *  comparator function. (Empty comparator classes like `std::less` are not a
 *  problem.) 
 *
 *  To avoid run-time surprises, the legacy `reducer_{min|max}[_index]` classes
 *  have been coded in the 1.0 library so that they will not even compile when
 *  instantiated with a non-empty comparator class.
 *
 *  @subsection redminmax_compatibility_optimized Numeric Optimization
 *
 *  Min/max reducers with a numeric value type and the default comparator can 
 *  be implemented slightly more efficiently than other min/max reducers.
 *  However, the optimization is incompatible with the 0.9 library
 *  implementation of min/max reducers.
 *
 *  The default min/max reducers implementation in the 1.0 library uses this
 *  numeric optimization. Code using legacy reducers compiled with the 1.0
 *  library can be safely used in the same program as code compiled with the
 *  0.9 library, but classes compiled with the different Cilk libraries will be
 *  defined in different namespaces.
 *
 *  The simplest solution is just to recompile the code that was compiled with
 *  the older version of Cilk. However, if this is impossible, you can define
 *  the `CILK_LIBRARY_0_9_REDUCER_MINMAX` macro (on the compiler command line,
 *  or in your source code before including `reducer_min_max.h`) when compiling
 *  with the new library. This will cause it to generate numeric reducers that
 *  will be less efficient, but will be fully compatible with previously
 *  compiled code. (Note that this macro has no effect on [the non-empty
 *  comparator incompatibility] (redminmax_compatibility_stateful).)
 *
 *  @section redminmax_types Type Requirements
 *
 *  `Type` and `Index` must be `Copy Constructible`, `Default Constructible`,
 *  and `Assignable`.
 *
 *  `Compare` must be `Copy Constructible` if the reducer is constructed with a 
 *  `compare` argument, and `Default Constructible` otherwise.
 *
 *  The `Compare` function must induce a strict weak ordering on the elements
 *  of `Type`.
 *
 *  @section redminmax_in_c Minimum and Maximum Reducers in C
 *
 *  These macros can be used to do minimum and maximum reductions in C:
 *
 *  Declaration                  | Type                              | Operation
 *  -----------------------------|-----------------------------------|----------
 * @ref CILK_C_REDUCER_MIN       |@ref CILK_C_REDUCER_MIN_TYPE       |@ref CILK_C_REDUCER_MIN_CALC      
 * @ref CILK_C_REDUCER_MAX       |@ref CILK_C_REDUCER_MAX_TYPE       |@ref CILK_C_REDUCER_MAX_CALC      
 * @ref CILK_C_REDUCER_MIN_INDEX |@ref CILK_C_REDUCER_MIN_INDEX_TYPE |@ref CILK_C_REDUCER_MIN_INDEX_CALC
 * @ref CILK_C_REDUCER_MAX_INDEX |@ref CILK_C_REDUCER_MAX_INDEX_TYPE |@ref CILK_C_REDUCER_MAX_INDEX_CALC
 *
 *  For example:
 *
 *      CILK_C_REDUCER_MIN(r, int, INT_MAX);
 *      CILK_C_REGISTER_REDUCER(r);
 *      cilk_for(int i = 0; i != n; ++i) {
 *          CILK_C_REDUCER_MIN_CALC(r, a[i]);
 *      }
 *      CILK_C_UNREGISTER_REDUCER(r);
 *      printf("The smallest value in a is %d\n", REDUCER_VIEW(r));
 *
 *
 *      CILK_C_REDUCER_MAX_INDEX(r, uint, 0);
 *      CILK_C_REGISTER_REDUCER(r);
 *      cilk_for(int i = 0; i != n; ++i) {
 *          CILK_C_REDUCER_MAX_INDEX_CALC(r, i, a[i]);
 *      }
 *      CILK_C_UNREGISTER_REDUCER(r);
 *      printf("The largest value in a is %u at %d\n", 
 *              REDUCER_VIEW (r).value, REDUCER_VIEW(r).index);
 *
 *  See @ref reducers_c_predefined.
 */

namespace cilk {

/** @defgroup ReducersMinMaxBinComp Binary compatibility
 *
 *  If the macro CILK_LIBRARY_0_9_REDUCER_MINMAX is defined, then we generate
 *  reducer code and data structures which are binary-compatible with code that
 *  was compiled with the old min/max wrapper definitions, so we want the
 *  mangled names of the legacy min/max reducer wrapper classes to be the
 *  same as the names produced by the old definitions.
 *
 *  Conversely, if the macro is not defined, then we generate binary- 
 *  incompatible code, so we want different mangled names, to make sure that 
 *  the linker does not allow new and old compiled legacy wrappers to be passed
 *  to one another. (Global variables are a different, and probably insoluble,
 *  problem.)
 *
 *  Similarly, min/max classes compiled with and without 
 *  CILK_LIBRARY_0_9_REDUCER_MINMAX are binary-incompatible, and must get 
 *  different mangled names.
 *
 *  The trick is, when compiling in normal (non-compatibility) mode, wrap
 *  everything in an extra namespace, and then `use` it into the top-level cilk
 *  namespace. Then 
 *
 *  *   Classes and functions compiled in normal mode will be in
 *      different namespaces from the same classes and functions compiled in
 *      compatibility mode.
 *  *   The legacy wrapper classes and functions will be in the same namespace 
 *      as the same classes and functions compiled with the0.9 library if and
 *      only if the are compiled in compatibility mode.
 *
 *  @ingroup ReducersMinMax
 */
 
#ifndef CILK_LIBRARY_0_9_REDUCER_MINMAX
/** Namespace to wrap min/max reducer definitions when not compiling in “binary
 *  compatibility” mode.
 *
 *  By default, all of the min/max reducer definitions are defined in this
 *  namespace and then imported into namespace ::cilk, so that they do not
 *  clash with the legacy definitions with the same names. However, if the
 *  macro `CILK_LIBRARY_0_9_REDUCER_MINMAX` is defined, then the min/max
 *  definitions go directly into namespace ::cilk, so that, for example,
 *  cilk::reducer_max defined with the 1.0 library is equivalent (to the
 *  linker) to cilk::reducer_max defined with the 0.9 library.
 *
 *  @ingroup ReducersMinMaxBinComp
 *  @ingroup ReducersMinMax
 */
namespace cilk_lib_1_0 {
#endif

/** Namespace containing internal implementation classes and functions for
 *  min/max reducers.
 *
 *  @ingroup ReducersMinMax
 */
namespace min_max_internal {

using ::cilk::internal::binary_functor;
using ::cilk::internal::typed_indirect_binary_function;
using ::cilk::internal::class_is_empty;

/** @defgroup ReducersMinMaxIsSet The “is_set optimization”
 *
 *  The obvious definition of the identity value for a max or min reducer is as
 *  the smallest (or largest) value of the value type. However, for an 
 *  arbitrary comparator and/or an arbitrary value type, the largest / smallest
 *  value may not be known. It may not even be defined — what is the largest
 *  string?
 *
 *  Therefore, min/max reducers represent their value internally as a pair
 *  `(value, is_set)`. When `is_set` is true, the pair represents the known
 *  value `value`; when `is_set` is false, the pair represents the identity
 *  value.
 *
 *  This is an effective solution, but the most common use of min/max reducers
 *  is probably with numeric types and the default definition of minimum or
 *  maximum (using `std::less`), in which case there are well-defined, knowable
 *  smallest and largest values. Testing `is_set` for every comparison is then
 *  unnecessary and wasteful.
 *
 *  The “is_set optimization” just means generating code that doesn’t use
 *  `is_set` when it isn’t needed. It is implemented using two metaprogramming
 *  classes:
 *
 *  -   do_is_set_optimization tests whether the optimization is applicable.
 *  -   identity_value gets the appropriate identity value for a type.
 *
 *  The is_set optimization is the reason that min/max reducers compiled with
 *  Cilk library 1.0 are binary-incompatible with the same reducers compiled
 *  with library 0.9, and therefore the optimization is suppressed when
 *  compiling in 
 *  ReducersMinMaxBinComp "binary compatibility mode". 
 *  
 *  @ingroup ReducersMinMax
 */

/** Test whether the ReducersMinMaxIsSet "is_set optimization" is
 *  applicable.
 *
 *  The @ref do_is_set_optimization class is used to test whether the is_set
 *  optimization should be applied for a particular reducer. It is instantiated
 *  with a value type and a comparator, and defines a boolean constant, 
 *  `value`. Then `%do_is_set_optimization<Type, Comp>::%value` can be used as
 *  a boolean template parameter to control the specialization of another
 *  class.
 *
 *  In ReducersMinMaxBinComp "binary compatibility mode", when the
 *  `CILK_LIBRARY_0_9_REDUCER_MINMAX` macro is defined, `value` will always
 *  be false.
 *
 *  @tparam Type   The value type for the reducer.
 *  @tparam Compare The comparator type for the reducer.
 *
 *  @result The `value` data member will be `true` if @a Type is a numeric 
 *          type, @a Compare is `std::less<Type>`, and 
 *          `CILK_LIBRARY_0_9_REDUCER_MINMAX` is not defined.
 *
 *  @see ReducersMinMaxIsSet
 *  @see @ref view_content
 *
 *  @ingroup ReducersMinMaxIsSet
 */
template <  typename Type, 
            typename Compare >
struct do_is_set_optimization 
{ 
    /// `True` if the is_set optimization should be applied to min/max reducers
    /// with this value type and comparator; `false` otherwise.
    static const bool value = false;
};

#ifndef CILK_LIBRARY_0_9_REDUCER_MINMAX
/// @cond
template <typename Type>
struct do_is_set_optimization<Type, std::less<Type> > 
{ 
    /// True in the special case where optimization is possible.
    static const bool value = std::numeric_limits<Type>::is_specialized;
};
/// @endcond
#endif


/** Get the identity value when using the ReducersMinMaxIsSet 
 *  "is_set optimization".
 *
 *  This class defines a function which assigns the appropriate identity value
 *  to a variable when the is_set optimization is applicable.
 *
 *  @tparam Type    The value type for the reducer.
 *  @tparam Compare The comparator type for the reducer.
 *  @tparam ForMax  `true` to get the identity value for a max reducer (i.e., 
 *                  the smallest value of @a Type), `false` to get the identity
 *                  value for a min reducer (i.e., the largest value of
 *                  @a Type).
 *
 *  @result If @a Type and @a Compare qualify for the is_set optimization, the
 *          `set_identity()' function will set its argument variable to the
 *          smallest or largest value of @a Type, depending on @a ForMax.
 *          Otherwise, `set_identity()` will be a no-op.
 *
 *  @see ReducersMinMaxIsSet
 *
 *  @ingroup ReducersMinMaxIsSet
 *  @see @ref view_content
 */
template <  typename    Type, 
            typename    Compare, 
            bool        ForMax,
            bool        = std::numeric_limits<Type>::is_specialized,
            bool        = std::numeric_limits<Type>::has_infinity >
struct identity_value {
    /// Assign the identity value to the reference parameter.
    static void set_identity(Type&) {}
};

/// @cond
template <typename Type>
struct identity_value<Type, std::less<Type>, true, true, true> {
    /// Floating max identity is negative infinity.
    static void set_identity(Type& id) 
    { id = -std::numeric_limits<Type>::infinity(); }
};

template <typename Type>
struct identity_value<Type, std::less<Type>, true, true, false> {
    /// Integer max identity is minimum value of type.
    static void set_identity(Type& id)
    { id = std::numeric_limits<Type>::min(); }
};

template <typename Type>
struct identity_value<Type, std::less<Type>, false, true, true> {
    /// Floating min identity is positive infinity.
    static void set_identity(Type& id)
    { id = std::numeric_limits<Type>::infinity(); }
};

template <typename Type>
struct identity_value<Type, std::less<Type>, false, true, false> {
    /// Integer min identity is maximum value of type.
    static void set_identity(Type& id)
    { id = std::numeric_limits<Type>::max(); }
};

/// @endcond


/** Adapter class to reverse the arguments of a predicate.
 *
 *  Observe that:
 *
 *      (x < y) == (y > x)
 *      max(x, y) == (x < y) ? y : x
 *      min(x, y) == (y < x) ? y : x == (x > y) ? y : x
 *
 *  More generally, if `c` is a predicate defining a `Strict Weak Ordering`, 
 *  and  `c*(x, y) == c(y, x)`, then
 *
 *      max(x, y, c) == c(x, y) ? y : x
 *      min(x, y, c) == c(y, x) ? y : x == c*(x, y) ? y : x == max(x, y, c*)
 *
 *  For any predicate `C` with argument type `T`, the template class 
 *  `%reverse_predicate<C, T>` defines a predicate which is identical to `C`,
 *  except that its arguments are reversed. Thus, for example, we could
 *  implement `%op_min_view<Type, Compare>` as
 *  `%op_max_view<Type, %reverse_predicate<Compare, Type> >`. 
 *  (Actually, op_min_view and op_max_view are both implemented as subclasses 
 *  of a common base class, view_base.)
 *
 *  @note   If `C` is an empty functor class, then `reverse_predicate(C)` will
 *          also be an empty functor class.
 *
 *  @tparam Predicate   The predicate whose arguments are to be reversed.
 *  @tparam Argument    @a Predicate’s argument type.
 *
 *  @ingroup ReducersMinMax
 */
template <typename Predicate,
          typename Argument = typename Predicate::first_argument_type>
class reverse_predicate : private binary_functor<Predicate>::type {
    typedef typename binary_functor<Predicate>::type base;
public:
    /// Default constructor
    reverse_predicate() : base() {}
    /// Constructor with predicate object
    reverse_predicate(const Predicate& p) : base(p) {} 
    /// The reversed predicate operation
    bool operator()(const Argument& x, const Argument& y) const
        { return base::operator()(y, x); }
};


/** Class to represent the comparator for a min/max view class.
 *
 *  This class is intended to accomplish two objectives in the implementation 
 *  of min/max views.
 *
 *  1.  To minimize data bloat, when we have a reducer with a non-stateless
 *      comparator, we want to keep a single instance of the comparator object
 *      in the monoid, and just call it from the views.
 *  2.  In ReducersMinMaxBinComp "binary compatibility mode", views for
 *      reducers with a stateless comparator must have the same content as in
 *      Cilk library 0.9 — that is, they must contain only `value` and
 *      `is_set` data members.
 *
 *  To achieve the first objective, we use the 
 *  @ref internal::typed_indirect_binary_function class defined in
 *  metaprogramming.h to wrap a pointer to the actual comparator. If no
 *  pointer is needed because the actual comparator is stateless, the 
 *  `typed_indirect_binary_function` class will be empty, too.
 *
 *  To achieve the second objective, we make the
 *  `typed_indirect_binary_function` class a base class of the view rather than
 *  a data member, so the “empty base class” rule will ensure no that no
 *  additional space is allocated in the view unless it is needed.
 *
 *  We could simply use typed_indirect_binary_function as the base class of the
 *  view, but this would mean writing comparisons as `(*this)(x, y)`, which is
 *  just weird. So, instead, we comparator_base as a subclass of
 *  typed_indirect_binary_function which provides function `compare()` 
 *  as a synonym for `operator()`.
 *
 *  @tparam Type    The value type of the comparator class.
 *  @tparam Compare A predicate class.
 *
 *  @see internal::typed_indirect_binary_function
 *
 *  @ingroup ReducersMinMax
 */
template <typename Type, typename Compare>
class comparator_base : private typed_indirect_binary_function<Compare, Type, Type, bool>
{
    typedef typed_indirect_binary_function<Compare, Type, Type, bool> base;
protected:
    comparator_base(const Compare* f) : base(f) {}  ///< Constructor.
    
    /// Comparison function.
    bool compare(const Type& a, const Type& b) const
    {
        return base::operator()(a, b); 
    }
    
    /// Get the comparator pointer.
    const Compare* compare_pointer() const { return base::pointer(); }
};


/** @defgroup ReducersMinMaxViewContent Content classes for min/max views
 *
 *  @ingroup ReducersMinMax
 *
 *  Minimum and maximum reducer view classes inherit from a “view content”
 *  class. The content class defines the actual data members for the view,
 *  and provides typedefs and member functions for accessing the data members
 *  as needed to support the view functionality.
 *
 *  There are two content classes, which encapsulate the differences between
 *  simple min/max reducers and min/max with index reducers:
 *
 *  -   view_content
 *  -   index_view_content
 *
 *  @note   An obvious, and arguably simpler, encapsulation strategy would be
 *          to just let the `Type` of a min/max view be an (index, value) pair
 *          structure for min_index and max_index reducers. Then all views 
 *          would just have a `Type` data member and an `is_set` data member,
 *          and the comparator for min_index and max_index views could be
 *          customized to consider only the value component of the (index,
 *          value) `Type` pair. Unfortunately, this would break binary
 *          compatibility with reducer_max_index and reducer_min_index in
 *          Cilk library 0.9, because the memory layout of an (index, value)
 *          pair followed by a `bool` is different from the memory layout of an
 *          index data member followed by a value data member followed by a
 *          `bool` data member. The content class is designed to exactly
 *          replicate the layout of the views in library 0.9 reducers.
 *
 *  A content class `C`, and its objects `c`, must define the following:
 *
 *  Definition                          | Meaning
 *  ------------------------------------|--------
 *  `C::value_type`                     | A typedef for `Type` of the view. (A `std::pair<Index, Type>` for min_index and max_index views).
 *  `C::comp_value_type`                | A typedef for the type of value compared by the view’s `compare()` function.
 *  `C()`                               | Constructs the content with the identity value.
 *  `C(const value_type&)`              | Constructs the content with a specified value.
 *  `c.is_set()`                        | Returns true if the content has a known value.
 *  `c.value()`                         | Returns the content’s value.
 *  `c.set_value(const value_type&)`    | Sets the content’s value. (The value becomes known.)
 *  `c.comp_value()`                    | Returns a const reference to the value or component of the value that is to be compared by the view’s comparator.
 *  `C::comp_value(const value_type&)`  | Returns a const reference to a value or component of a value that is to be compared by the view’s comparator.
 *
 *  @see view_base
 */

/** Content class for op_min_view and op_max_view.
 *
 *  @tparam Type    The value type of the op_min_view or op_max_view.
 *  @tparam Compare The comparator class specified for the op_min_view or 
 *                  op_max_view. (_Not_ the derived comparator class actually
 *                  used by the view_base. For example, the view_content of an
 *                  `op_min_view<int>` will have `Compare = std::less<int>`, 
 *                  but its comparator_base will have 
 *                  `Compare = reverse_predicate< std::less<int> >`.)
 *  @tparam ForMax  `true` if this is the content class for an op_max_view,
 *                  `false` if it is for an op_min_view.
 *
 *  @note   The general implementation of view_content uses an `is_set` data
 *          member. There is also a specialization which implements the 
 *          ReducersMinMaxIsSet "is_set optimization". View classes that
 *          inherit from view_content do not need to know anything about the
 *          difference, though; the details are abstracted away in the 
 *          view_content interface.
 *
 *  @see ReducersMinMaxViewContent
 *
 *  @ingroup ReducersMinMaxViewContent
 *  @ingroup ReducersMinMax
 */
template < typename Type
         , typename Compare
         , bool     ForMax
         , bool     = do_is_set_optimization<Type, Compare>::value
         >
class view_content {
    Type    m_value;
    bool    m_is_set;
public:
    /// The value type of the view.
    typedef Type value_type;
    
    /// The type compared by the view’s `compare()` function (which is the same
    /// as the value type for view_content).
    typedef Type comp_value_type;
    
    /// Construct with the identity value.
    view_content() : m_value(), m_is_set(false) {}
    
    /// Construct with a defined value.
    view_content(const value_type& value) : m_value(value), m_is_set(true) {}
    
    /// Get the value.
    value_type value() const { return m_value; }
    
    /// Set the value.
    void set_value(const value_type& value) 
    { 
        m_value = value;
        m_is_set = true;
    }
    
    /// Get the comparison value (which is the same as the value for
    /// view_content).
    const comp_value_type& comp_value() const { return m_value; }

    /// Given an arbitrary value, get the corresponding comparison value (which
    /// is the same as the value for view_content).
    static const comp_value_type& comp_value(const value_type& value) 
    {
        return value; 
    }
    
    /// Get a const reference to value part of the value (which is the same as
    /// the value for view_content).
    const Type& get_reference() const { return m_value; }
    
    /// Get a const reference to the index part of the value (which is 
    /// meaningless for non-index reducers, but required for view_base.
    const Type& get_index_reference() const { return m_value; }
    
    /// Test if the value is defined.
    bool is_set() const { return m_is_set; }
};

/// @cond

/*  This is the specialization of the view_content class for cases where
 *  `AssumeIsSet` is true (i.e., where the is_set optimization is applicable).
 */
template < typename Type
         , typename Compare
         , bool ForMax
         >
class view_content<Type, Compare, ForMax, true> {
    typedef identity_value<Type, Compare, ForMax> Identity;
    Type    m_value;
public:
    typedef Type value_type;
    typedef Type comp_value_type;
    
    /// Construct with identity value.
    view_content() { Identity::set_identity(m_value); }
    
    view_content(const value_type& value) : m_value(value) {}
    
    value_type value() const { return m_value; }
    
    void set_value(const value_type& value) 
    { 
        m_value = value;
    }
    
    const comp_value_type& comp_value() const { return m_value; }

    static const comp_value_type& comp_value(const value_type& value) 
    {
        return value; 
    }
    
    const Type& get_reference() const { return m_value; }
    
    const Type& get_index_reference() const { return m_value; }
    
    /// Test if the value is defined.
    bool is_set() const { return true; }
};

/// @endcond


/** Content class for op_min_index_view and op_max_index_view.
 *
 *  @tparam Index   The index type of the op_min_index_view or
                    op_max_index_view.
 *  @tparam Type    The value type of the op_min_view or op_max_view. (_Not_ 
 *                  the value type of the view, which will be
 *                  `std::pair<Index, Type>`.)
 *  @tparam Compare The comparator class specified for the op_min_index_view or 
 *                  op_max_index_view. (_Not_ the derived comparator class
 *                  actually used by the view_base. For example, the
 *                  index_view_content of an `op_min_index_view<int>` will have
 *                  `Compare = std::less<int>`, but its comparator_base will
 *                  have `Compare = reverse_predicate< std::less<int> >`.)
 *  @tparam ForMax  `true` if this is the content class for an
 *                  op_max_index_view, `false` if it is for an
 *                  op_min_index_view.
 *
 *  @see ReducersMinMaxViewContent
 *
 *  @ingroup ReducersMinMaxViewContent
 *  @ingroup ReducersMinMax
 */
template < typename Index
         , typename Type
         , typename Compare
         , bool ForMax
         >
class index_view_content {
    typedef identity_value<Type, Compare, ForMax> Identity;

    Index   m_index;
    Type    m_value;
    bool    m_is_set;
public:
    /// The value type of the view (which is an <index, value> pair for 
    /// index_view_content).
    typedef std::pair<Index, Type> value_type;
    
    /// The type compared by the view’s `compare()` function (which is the data 
    /// value type for index_view_content).
    typedef Type comp_value_type;
    
    /// Construct with the identity value.
    index_view_content() : m_index(), m_value(), m_is_set(false) {}
    
    /// Construct with an index/value pair.
    index_view_content(const value_type& value) : 
        m_index(value.first), m_value(value.second), m_is_set(true) {}
    
    /// Construct with an index and a value.
    index_view_content(const Index& index, const Type& value) : 
        m_index(index), m_value(value), m_is_set(true) {}
    
    /// Construct with just an index.
    index_view_content(const Index& index) : 
        m_index(index), m_value(), m_is_set(false) {}
    
    /// Get the value.
    value_type value() const { return value_type(m_index, m_value); }
    
    /// Set value.
    void set_value(const value_type& value) 
    { 
        m_index = value.first; 
        m_value = value.second;
        m_is_set = true;
    }
    
    /// Get the comparison value (which is the value component of the 
    /// index/value pair for index_view_content).
    const comp_value_type& comp_value() const { return m_value; }
    
    /// Given an arbitrary value (i.e., index/value pair), get the
    /// corresponding comparison value (which is the value component of the
    /// index/value pair for index_view_content).
    static const comp_value_type& comp_value(const value_type& value) 
        { return value.second; }
    
    /// Get a const reference to value part of the value.
    const Type& get_reference() const { return m_value; }
    
    /// Get a const reference to the index part of the value.
    const Index& get_index_reference() const { return m_index; }
    
    /// Test if the value is defined.
    bool is_set() const { return m_is_set; }
};


template <typename View> class rhs_proxy;

/** Create an rhs_proxy.
 */
template <typename View>
inline rhs_proxy<View> 
make_proxy(const typename View::value_type& value, const View& view);

template <typename Content, typename Less, typename Compare> class view_base;


/** Class to represent the right-hand side of 
 *  `*reducer = {min|max}_of(*reducer, value)`.
 *
 *  The only assignment operator for a min/max view class takes a rhs_proxy as
 *  its operand. This results in the syntactic restriction that the only
 *  expressions that can be assigned to a min/max view are ones which generate
 *  an rhs_proxy — that is, expressions of the form `max_of(view, value)` and
 *  `min_of(view, value)`.
 *
 *  @warning
 *  The lhs and rhs views in such an assignment must be the same; otherwise, 
 *  the behavior will be undefined. (I.e., `*r1 = min_of(*r1, x)` is legal; 
 *  `*r1 = min_of(*r2, x)` is illegal.)  This condition will be checked with a
 *  runtime assertion when compiled in debug mode.
 *
 *  @tparam View    The view class (op_{min|max}[_index]_view) that this proxy
 *                  was created from.
 *
 *  @see view_base
 *
 *  @ingroup ReducersMinMax
 */
template <typename View>
class rhs_proxy {
    typedef typename View::less_type                less_type;
    typedef typename View::compare_type             compare_type;
    typedef typename View::value_type               value_type;
    typedef typename View::content_type             content_type;
    typedef typename content_type::comp_value_type  comp_value_type;
    
    friend class view_base<content_type, less_type, compare_type>;
    friend rhs_proxy make_proxy<View>(
        const typename View::value_type& value, 
        const View& view);
    
    typed_indirect_binary_function<
        compare_type, comp_value_type, comp_value_type, bool>
                                        m_comp;
    const View*                         m_view;
    value_type                          m_value;

    rhs_proxy& operator=(const rhs_proxy&); // Disable assignment operator
    rhs_proxy();                            // Disable default constructor
    
    // Constructor (called from view_base::make_proxy).
    rhs_proxy(const View* view, 
              const value_type& value,
              const compare_type* compare) : 
        m_view(view), m_value(value), m_comp(compare) {}
        
    // Check matching view, then return value (called from view_base::assign).
    value_type value(const typename View::base* view) const
    { 
        __CILKRTS_ASSERT(view == m_view); 
        return m_value; 
    }

public:

    /** Support max_of(max_of(view, value), value) and the like.
     */
    rhs_proxy calc(const value_type& x) const
    {
        return rhs_proxy(
            m_view, 
            m_comp( content_type::comp_value(m_value),     
                    content_type::comp_value(x)
                  ) ? x : m_value,
            m_comp.pointer());
    }
};
    
    
template <typename View>
inline rhs_proxy<View> 
make_proxy(const typename View::value_type& value, const View& view)
{
    return rhs_proxy<View>(&view, value, view.compare_pointer());
}

//@}

/** Base class for min and max view classes.
 *
 *  This class accumulates the minimum or maximum of a set of values which have
 *  occurred as arguments to the `calc()` function, as determined by a
 *  comparator. The accumulated value will be the first `calc()` argument value
 *  `x` such that `compare(x, y)` is false for every `calc()` argument value
 *  `y`.
 *
 *  If the comparator is `std::less`, then the accumulated value is the first
 *  argument value which is not less than any other argument value, i.e., the
 *  maximum. Similarly, if the comparator is `reverse_predicate<std::less>`,
 *  which is equivalent to `std::greater`, then the accumulated value is the
 *  first argument value which is not greater than any other argument value,
 *  i.e., the minimum.
 *  
 *  @note   This class provides the definitions that are required for a class
 *          that will be used as the parameter of a 
 *          min_max_internal::monoid_base specialization. 
 *
 *  @tparam Content     A content class that provides the value types and data
 *                      members for the view.
 *  @tparam Less        A “less than” binary predicate that defines the min or
 *                      max function.
 *  @tparam Compare     A binary predicate to be used to compare the values.
 *                      (The same as @a Less for max reducers; its reversal for
 *                      min reducers.)
 *
 *  @see ReducersMinMaxViewContent
 *  @see op_max_view
 *  @see op_min_view
 *  @see op_max_index_view
 *  @see op_min_index_view
 *  @see monoid_base
 *
 *  @ingroup ReducersMinMax
 */
template <typename Content, typename Less, typename Compare>
class view_base : 
    // comparator_base comes first to ensure that it will get empty base class
    // treatment
    private comparator_base<typename Content::comp_value_type, Compare>, 
    private Content
{
    typedef comparator_base<typename Content::comp_value_type, Compare> base;
    using base::compare;
    using Content::value;
    using Content::set_value;
    using Content::comp_value;
    typedef Content content_type;
    
    template <typename View> friend class rhs_proxy;
    template <typename View>
    friend rhs_proxy<View> make_proxy(const typename View::value_type& value, const View& view);
    
public:
    
    /** @name Monoid support.
     */
    //@{
    
    /** Value type. Required by @ref monoid_with_view.
     */
    typedef typename Content::value_type    value_type;
    
    /** The type of the comparator specified by the user, that defines the 
     *  ordering on @a Type. Required by min_max::monoid_base.
     */
    typedef Less                            less_type;
    
    /** The type of the comparator actually used by the view. Required by 
     *  min_max::monoid_base. (This is the same as the @ref less_type for a 
     *  max reducer, or `reverse_predicate<less_type>` for a min reducer.)
     */
    typedef Compare                         compare_type;

    /** Reduce operation. Required by @ref monoid_with_view.
     */
    void reduce(view_base* other)
    {
        if (    other->is_set() &&
                (   !this->is_set() || 
                    compare(this->comp_value(), other->comp_value()) ) )
        {
            this->set_value(other->value());
        }
    }
    
    //@}
    
    /** Default constructor. Initializes to identity value.
     */
    explicit view_base(const compare_type* compare) : 
        base(compare), Content() {}
    
    /** Value constructor.
     */
    template <typename T1>
    view_base(const T1& x1, const compare_type* compare) : 
        base(compare), Content(x1) {}

    /** Value constructor.
     */
    template <typename T1, typename T2>
    view_base(const T1& x1, const T2& x2, const compare_type* compare) : 
        base(compare), Content(x1, x2) {}


    /** Move-in constructor.
     */
    explicit view_base(move_in_wrapper<value_type> w, const compare_type* compare) :
        base(compare), Content(w.value()) {}
    
    /** @name Reducer support.
     */
    //@{
    
    void                view_move_in(value_type& v)         { set_value(v); }
    void                view_move_out(value_type& v)        { v = value(); }
    void                view_set_value(const value_type& v) { set_value(v); }
    value_type          view_get_value() const              { return value(); }
    //                  view_get_reference()                NOT SUPPORTED
    
    //@}
    
    /** Is the value defined?
     */
    using Content::is_set;
    
    /** Reference to contained value data member.
     *  @deprecated For legacy reducers only.
     */
    using Content::get_reference;
    
    /** Reference to contained index data member.
     *  (Meaningless for non-index reducers.)
     *  @deprecated For legacy reducers only.
     */
    using Content::get_index_reference;
    
protected:

    /** Update the min/max value.
     */
    void calc(const value_type& x)
    {
        if (!is_set() || compare(comp_value(), comp_value(x))) set_value(x);
    }
    
    /** Assign the result of a `{min|max}_of(view, value)` expression to the 
     *  view.
     *
     *  @see rhs_proxy
     */
    template <typename View>
    void assign(const rhs_proxy<View>& rhs)
    {
        calc(rhs.value(this));
    }
    
};


/** Base class for min and max monoid classes.
 *
 *  The unique characteristic of minimum and maximum reducers is that they 
 *  incorporate a comparator functor that defines what “minimum” or “maximum”
 *  means. The monoid for a reducer contains the comparator that will be used
 *  for the reduction. If the comparator is a function or a class with state,
 *  then each view will have a pointer to the comparator.
 *
 *  This means that the `construct()` functions first construct the monoid
 *  (possibly with an explicit comparator argument), and then construct the 
 *  view with a pointer to the monoid’s comparator.
 *
 *  @tparam View    The view class.
 *  @tparam Align   If true, reducers instantiated on this monoid will be
 *                  aligned. By default, library reducers (unlike legacy
 *                  library reducer _wrappers_) are unaligned.
 *
 *  @see view_base
 *
 *  @ingroup ReducersMinMax
 */
template <typename View, bool Align = false>
class monoid_base : public monoid_with_view<View, Align>
{
    typedef typename View::compare_type compare_type;
    typedef typename View::less_type    less_type;
    const compare_type                  m_compare;

    const compare_type* compare_pointer() const { return &m_compare; }
    
    using cilk::monoid_base<typename View::value_type, View>::provisional;
    
public:

    /** Default constructor uses default comparator.
     */
    monoid_base() : m_compare() {}

    /** Constructor.
     *
     *  @param  compare The comparator to use.
     */
    monoid_base(const compare_type& compare) : m_compare(compare) {}

    /** Create an identity view.
     *
     *  List view identity constructors take the list allocator as an argument.
     *
     *  @param v    The address of the uninitialized memory in which the view 
     *  will be constructed.
     */
    void identity(View *v) const { ::new((void*) v) View(compare_pointer()); }
    
    /** @name construct functions
     *
     *  Min/max monoid `construct()` functions optionally take one or two value
     *  arguments, a @ref move_in argument, and/or a comparator argument.
     */
    //@{
    
    template <typename Monoid>
    static void construct(Monoid* monoid, View* view)
        { provisional( new ((void*)monoid) Monoid() ).confirm_if( 
            new ((void*)view) View(monoid->compare_pointer()) ); }

    template <typename Monoid, typename T1>
    static void construct(Monoid* monoid, View* view, const T1& x1)
        { provisional( new ((void*)monoid) Monoid() ).confirm_if( 
            new ((void*)view) View(x1, monoid->compare_pointer()) ); }

    template <typename Monoid, typename T1, typename T2>
    static void construct(Monoid* monoid, View* view, const T1& x1, const T2& x2)
        { provisional( new ((void*)monoid) Monoid() ).confirm_if( 
            new ((void*)view) View(x1, x2, monoid->compare_pointer()) ); }

    template <typename Monoid>
    static void construct(Monoid* monoid, View* view, const less_type& compare)
        { provisional( new ((void*)monoid) Monoid(compare) ).confirm_if( 
            new ((void*)view) View(monoid->compare_pointer()) ); }

    template <typename Monoid, typename T1>
    static void construct(Monoid* monoid, View* view, const T1& x1, const less_type& compare)
        { provisional( new ((void*)monoid) Monoid(compare) ).confirm_if( 
            new ((void*)view) View(x1, monoid->compare_pointer()) ); }

    template <typename Monoid, typename T1, typename T2>
    static void construct(Monoid* monoid, View* view, const T1& x1, const T2& x2, const less_type& compare)
        { provisional( new ((void*)monoid) Monoid(compare) ).confirm_if( 
            new ((void*)view) View(x1, x2, monoid->compare_pointer()) ); }

    //@}
};

} //namespace min_max_internal


/** @defgroup ReducersMinMaxMaxValue Maximum reducers (value only)
 *
 *  These reducers will find the largest value from a set of values.
 *
 *  @ingroup ReducersMinMax
 */
//@{

/** The maximum reducer view class.
 *
 *  This is the view class for reducers created with 
 *  `cilk::reducer< cilk::op_max<Type, Compare> >`. It accumulates the maximum,
 *  as determined by a comparator, of a set of values which have occurred as
 *  arguments to the `calc_max()` function. The accumulated value will be the
 *  first argument `x` such that `compare(x, y)` is false for every argument
 *  `y`.
 *
 *  If the comparator is `std::less`, then the accumulated value is the first
 *  argument value which is not less than any other argument value, i.e., the
 *  maximum.
 *
 *  @note   The reducer “dereference” operation (`reducer::operator *()`) 
 *          yields a reference to the view. Thus, for example, the view class’s
 *          `calc_max()` function would be used in an expression like
 *          `r->calc_max(a)` where `r` is an op_max reducer variable.
 *
 *  @tparam Type    The type of the values compared by the reducer. This will
 *                  be the value type of a monoid_with_view that is 
 *                  instantiated with this view.
 *  @tparam Compare A `Strict Weak Ordering` whose argument type is @a Type. It
 *                  defines the “less than” relation used to compute the
 *                  maximum.
 *
 *  @see ReducersMinMax
 *  @see op_max
 */
template <typename Type, typename Compare>
class op_max_view : public min_max_internal::view_base<
    min_max_internal::view_content<Type, Compare, true>, 
    Compare,
    Compare>
{
    typedef min_max_internal::view_base<
        min_max_internal::view_content<Type, Compare, true>, 
        Compare,
        Compare> base;
    using base::calc;
    using base::assign;
    friend class min_max_internal::rhs_proxy<op_max_view>;
    
public:

    /** @name Constructors.
     *
     *  All op_max_view constructors simply pass their arguments on to the 
     *  @ref view_base base class.
     */
    //@{
    
    op_max_view() : base() {}
    
    template <typename T1>
    op_max_view(const T1& x1) : base(x1) {}
    
    template <typename T1, typename T2>
    op_max_view(const T1& x1, const T2& x2) : base(x1, x2) {}
    
    //@}    

    /** @name View modifier operations.
     */
    //@{
    
    /** Maximize with a value.
     *
     *  If @a x is greater than the current value of the view (as defined by 
     *  the reducer’s comparator), or if the view was created without an 
     *  initial value and its value has never been updated (with `calc_max()` 
     *  or `= max_of()`), then the value of the view is set to @a x.
     *
     *  @param  x   The value to maximize the view’s value with.
     *
     *  @return     A reference to the view. (Allows chaining
     *              `view.comp_max(a).comp_max(b)…`.)
     */
    op_max_view& calc_max(const Type& x) { calc(x); return *this; }

    /** Assign the result of a `max_of(view, value)` expression to the view.
     *
     *  @param  rhs An rhs_proxy value created by a `max_of(view, value)`
     *              expression.
     *
     *  @return     A reference to the view.
     *
     *  @see min_max_internal::view_base::rhs_proxy
     */
    op_max_view& operator=(const min_max_internal::rhs_proxy<op_max_view>& rhs) 
        { assign(rhs); return *this; }
    
    //@}
};


/** Compute the maximum of the value in an op_max_view and another value.
 *
 *  The result of this computation can only be assigned back to the original 
 *  view or used in another max_of() call. For example,
 *
 *      *reducer = max_of(*reducer, x);
 *      *reducer = max_of(x, *reducer);
 *
 *  @see min_max_internal::rhs_proxy
 */
template <typename Type, typename Compare>
inline min_max_internal::rhs_proxy< op_max_view<Type, Compare> >
max_of(const op_max_view<Type, Compare>& view, const Type& value)
{
    return min_max_internal::make_proxy(value, view);
}

/// @copydoc max_of(const op_max_view<Type, Compare>&, const Type&)
template <typename Type, typename Compare>
inline min_max_internal::rhs_proxy< op_max_view<Type, Compare> >
max_of(const Type& value, const op_max_view<Type, Compare>& view)
{
    return min_max_internal::make_proxy(value, view);
}

/** Nested maximum computation.
 *
 *  Compute the maximum of the result of a max_of() call and another value.
 *
 *  The result of this computation can only be assigned back to the original
 *  view or wrapper, or used in another max_of() call. For example,
 *
 *      *reducer = max_of(x, max_of(y, *reducer));
 *      wrapper = max_of(max_of(wrapper, x), y);
 *
 *  @see min_max_internal::rhs_proxy
 */
template <typename Type, typename Compare>
inline min_max_internal::rhs_proxy< op_max_view<Type, Compare> >
max_of(const min_max_internal::rhs_proxy< op_max_view<Type, Compare> >& proxy, 
       const Type& value)
{
    return proxy.calc(value);
}

/// @copydoc max_of(const min_max_internal::rhs_proxy< op_max_view<Type, Compare> >&, const Type&)
template <typename Type, typename Compare>
inline min_max_internal::rhs_proxy< op_max_view<Type, Compare> >
max_of(const Type& value, 
       const min_max_internal::rhs_proxy< op_max_view<Type, Compare> >& proxy)
{
    return proxy.calc(value);
}


/** Monoid class for maximum reductions. Instantiate the cilk::reducer template
 *  class with an op_max monoid to create a maximum reducer class. For example,
 *  to compute the maximum of a set of `int` values:
 *
 *      cilk::reducer< cilk::op_max<int> > r;
 *
 *  @see ReducersMinMax
 *  @see op_max_view
 */
template <typename Type, typename Compare=std::less<Type>, bool Align = false>
class op_max : 
    public min_max_internal::monoid_base<op_max_view<Type, Compare>, Align> 
{
    typedef min_max_internal::monoid_base<op_max_view<Type, Compare>, Align>
            base;
public:
    /// Construct with default comparator.
    op_max() {}
    /// Construct with specified comparator.
    op_max(const Compare& compare) : base(compare) {}
};

//@}


/** @defgroup ReducersMinMaxMinValue Minimum reducers (value only)
 *
 *  These reducers will find the smallest value from a set of values.
 *
 *  @ingroup ReducersMinMax
 */
//@{

/** The minimum reducer view class.
 *
 *  This is the view class for reducers created with 
 *  `cilk::reducer< cilk::op_min<Type, Compare> >`. It accumulates the minimum,
 *  as determined by a comparator, of a set of values which have occurred as
 *  arguments to the `calc_min()` function. The accumulated value will be the
 *  first argument `x` such that `compare(y, x)` is false for every argument
 *  `y`.
 *
 *  If the comparator is `std::less`, then the accumulated value is the first
 *  argument value which no other argument value is less than, i.e., the
 *  minimum.
 *
 *  @note   The reducer “dereference” operation (`reducer::operator *()`) 
 *          yields a reference to the view. Thus, for example, the view class’s
 *          `calc_min()` function would be used in an expression like
 *          `r->calc_min(a)` where `r` is an op_min reducer variable.
 *
 *  @tparam Type    The type of the values compared by the reducer. This will 
 *                  be the value type of a monoid_with_view that is 
 *                  instantiated with this view.
 *  @tparam Compare A `Strict Weak Ordering` whose argument type is @a Type. It 
 *                  defines the “less than” relation used to compute the
 *                  minimum.
 *
 *  @see ReducersMinMax
 *  @see op_min
 */
template <typename Type, typename Compare>
class op_min_view : public min_max_internal::view_base<
    min_max_internal::view_content<Type, Compare, false>, 
    Compare,
    min_max_internal::reverse_predicate<Compare, Type> >
{
    typedef min_max_internal::view_base<
        min_max_internal::view_content<Type, Compare, false>, 
        Compare,
        min_max_internal::reverse_predicate<Compare, Type> > base;
    using base::calc;
    using base::assign;
    friend class min_max_internal::rhs_proxy<op_min_view>;

public:
    /** @name Constructors.
     *
     *  All op_min_view constructors simply pass their arguments on to the 
     *  @ref view_base base class.
     */
    //@{
    
    op_min_view() : base() {}
    
    template <typename T1>
    op_min_view(const T1& x1) : base(x1) {}
    
    template <typename T1, typename T2>
    op_min_view(const T1& x1, const T2& x2) : base(x1, x2) {}
    
    //@}    

    /** @name View modifier operations.
     */
    //@{
    
    /** Minimize with a value.
     *
     *  If @a x is less than the current value of the view (as defined by the
     *  reducer’s comparator), or if the view was created without an initial
     *  value and its value has never been updated (with `calc_min()` or
     *  `= min_of()`), then the value of the view is set to @a x.
     *
     *  @param  x   The value to minimize the view’s value with.
     *
     *  @return     A reference to the view. (Allows chaining
     *              `view.comp_min(a).comp_min(b)…`.)
     */
    op_min_view& calc_min(const Type& x) { calc(x); return *this; }

    /** Assign the result of a `min_of(view, value)` expression to the view.
     *
     *  @param  rhs An rhs_proxy value created by a `min_of(view, value)`
     *              expression.
     *
     *  @return     A reference to the view.
     *
     *  @see min_max_internal::view_base::rhs_proxy
     */
    op_min_view& operator=(const min_max_internal::rhs_proxy<op_min_view>& rhs) 
        { assign(rhs); return *this; }
};


/** Compute the minimum of the value in a view and another value.
 *
 *  The result of this computation can only be assigned back to the original
 *  view or used in another min_of() call. For example,
 *
 *      *reducer = min_of(*reducer, x);
 *      *reducer = min_of(x, *reducer);
 *
 *  @see min_max_internal::view_base::rhs_proxy
 */
template <typename Type, typename Compare>
inline min_max_internal::rhs_proxy< op_min_view<Type, Compare> >
min_of(const op_min_view<Type, Compare>& view, const Type& value)
{
    return min_max_internal::make_proxy(value, view);
}

/// @copydoc min_of(const op_min_view<Type, Compare>&, const Type&)
template <typename Type, typename Compare>
inline min_max_internal::rhs_proxy< op_min_view<Type, Compare> >
min_of(const Type& value, const op_min_view<Type, Compare>& view)
{
    return min_max_internal::make_proxy(value, view);
}

/** Nested minimum computation.
 *
 *  Compute the minimum of the result of a min_of() call and another value.
 *
 *  The result of this computation can only be assigned back to the original
 *  view or wrapper, or used in another min_of() call. For example,
 *
 *      *reducer = min_of(x, min_of(y, *reducer));
 *      wrapper = min_of(min_of(wrapper, x), y);
 *
 *  @see min_max_internal::rhs_proxy
 */
template <typename Type, typename Compare>
inline min_max_internal::rhs_proxy< op_min_view<Type, Compare> >
min_of(const min_max_internal::rhs_proxy< op_min_view<Type, Compare> >& proxy, 
       const Type& value)
{
    return proxy.calc(value);
}

/// @copydoc min_of(const min_max_internal::rhs_proxy< op_min_view<Type, Compare> >&, const Type&)
template <typename Type, typename Compare>
inline min_max_internal::rhs_proxy< op_min_view<Type, Compare> >
min_of(const Type& value, 
       const min_max_internal::rhs_proxy< op_min_view<Type, Compare> >& proxy)
{
    return proxy.calc(value);
}


/** Monoid class for minimum reductions. Instantiate the cilk::reducer template
 *  class with an op_min monoid to create a minimum reducer class. For example,
 *  to compute the minimum of a set of `int` values:
 *
 *      cilk::reducer< cilk::op_min<int> > r;
 *
 *  @see ReducersMinMax
 *  @see op_min_view
 */
template <typename Type, typename Compare=std::less<Type>, bool Align = false>
class op_min : public min_max_internal::monoid_base<op_min_view<Type, Compare>, Align> {
    typedef min_max_internal::monoid_base<op_min_view<Type, Compare>, Align> base;
public:
    /// Construct with default comparator.
    op_min() {}
    /// Construct with specified comparator.
    op_min(const Compare& compare) : base(compare) {}
};

//@}


/** @defgroup ReducersMinMaxMaxIndex Maximum reducers (value and index)
 *
 *  These reducers will find the largest value from a set of values, and its
 *  index in the set.
 *
 *  @ingroup ReducersMinMax
 */
//@{

/** The maximum index reducer view class.
 *
 *  This is the view class for reducers created with 
 *  `cilk::reducer< cilk::op_max_index<Index, Type, Compare> >`. It accumulates
 *  the maximum, as determined by a comparator, of a set of values which have
 *  occurred as arguments to the `calc_max()` function, and records the index
 *  of the maximum value. The accumulated value will be the first argument `x`
 *  such that `compare(x, y)` is false for every argument `y`.
 *
 *  If the comparator is `std::less`, then the accumulated value is the first
 *  argument value which is not less than any other argument value, i.e., the
 *  maximum.
 *
 *  @note   The reducer “dereference” operation (`reducer::operator *()`) 
 *          yields a reference to the view. Thus, for example, the view class’s
 *          `calc_max()` function would be used in an expression like
 *          `r->calc_max(i, a)`where `r` is an op_max_index reducer
 *          variable.
 *
 *  @note   The word “index” suggests an integer index into an array, but there
 *          is no restriction on the index type or how it should be used. In
 *          general, it may be convenient to use it for any kind of key that 
 *          can be used to locate the maximum value in the collection that it
 *          came from — for example:
 *              -   An index into an array.
 *              -   A key into an STL map.
 *              -   An iterator into any STL container.
 *
 *  @note   A max_index reducer is essentially a max reducer whose value type 
 *          is a `std::pair<Index, Type>`. This fact is camouflaged in the view
 *          `calc_max` function, the global `max_of` functions, and the reducer
 *          value constructor, which can all take an index argument and a value
 *          argument as an alternative to a single `std::pair` argument.
 *          However, the reducer `set_value()`, `get_value()`, `move_in()`, and
 *          `move_out()` functions work only with pairs, not with individual 
 *          value and/or index arguments.
 *
 *  @tparam Index   The type of the indices associated with the values.
 *  @tparam Type    The type of the values compared by the reducer. This will 
 *                  be the value type of a monoid_with_view that is 
 *                  instantiated with this view.
 *  @tparam Compare Used to compare the values. It must be a binary predicate.
 *                  If it is omitted, then the view computes the conventional
 *                  arithmetic maximum.
 *
 *  @see ReducersMinMax
 *  @see op_max_index
 */
template <typename Index, typename Type, typename Compare>
class op_max_index_view : public min_max_internal::view_base<
    min_max_internal::index_view_content<Index, Type, Compare, true>,
    Compare,
    Compare>
{
    typedef min_max_internal::view_base<
        min_max_internal::index_view_content<Index, Type, Compare, true>,
        Compare,
        Compare> base;
    using base::calc;
    using base::assign;
    typedef std::pair<Index, Type> pair_type;
    friend class min_max_internal::rhs_proxy<op_max_index_view>;

public:
    /** @name Constructors.
     *
     *  All op_max_index_view constructors simply pass their arguments on to the 
     *  @ref view_base base class, except for the `(index, value [, compare])`
     *  constructors, which create a `std::pair` containing the index and value.
     */
    //@{
    
    op_max_index_view() : base() {}
    
    template <typename T1>
    op_max_index_view(const T1& x1) : base(x1) {}
    
    template <typename T1, typename T2>
    op_max_index_view(const T1& x1, const T2& x2) : base(x1, x2) {}
    
    template <typename T1, typename T2, typename T3>
    op_max_index_view(const T1& x1, const T2& x2, const T3& x3) : base(x1, x2, x3) {}
    
    op_max_index_view(const Index& i, const Type& v) : base(pair_type(i, v)) {}
    
    op_max_index_view(const Index& i, const Type& v, const typename base::compare_type* c) : 
        base(pair_type(i, v), c) {}
    
    //@}    

    /** Maximize with a value and index.
     *
     *  If @a x is greater than the current value of the view (as defined by 
     *  the reducer’s comparator), or if the view was created without an 
     *  initial value and its value has never been updated (with `calc_max()` 
     *  or `= max_of()`), then the value of the view is set to @a x, and the
     *  index is set to @a i..
     *
     *  @param  i   The index of the value @a x.
     *  @param  x   The value to maximize the view’s value with.
     *
     *  @return     A reference to the view. (Allows 
     *              `view.comp_max(i, a).comp_max(j, b)…`.)
     */
    op_max_index_view& calc_max(const Index& i, const Type& x) 
        { calc(pair_type(i, x)); return *this; }

    /** Maximize with an index/value pair.
     *
     *  If @a pair.second is greater than the current value of the view (as
     *  defined by the reducer’s comparator), or if the view was created 
     *  without an initial value and its value has never been updated (with
     *  `calc_max()` or `= max_of()`), then the value of the view is set to
     *  @a pair.second, and the index is set to @a pair.first.
     *
     *  @param  pair    A pair containing a value to maximize the view’s value
     *                  with and its associated index.
     *
     *  @return         A reference to the view. (Allows
     *                  `view.comp_max(p1).comp_max(p2)…`.)
     */
    op_max_index_view& calc_max(const pair_type& pair) 
        { calc(pair); return *this; }

    /** Assign the result of a `max_of(view, index, value)` expression to the 
     *  view.
     *
     *  @param  rhs An rhs_proxy value created by a `max_of(view, index, value)`
     *              expression.
     *
     *  @return     A reference to the view.
     *
     *  @see min_max_internal::view_base::rhs_proxy
     */
    op_max_index_view& operator=(const min_max_internal::rhs_proxy<op_max_index_view>& rhs) 
        { assign(rhs); return *this; }
};


/** Compute the maximum of the value in a view and another value.
 *
 *  The result of this computation can only be assigned back to the original
 *  view or used in another max_of() call. For example,
 *
 *      *reducer = max_of(*reducer, i, x);
 *      *reducer = max_of(i, x, *reducer);
 *
 *  @see min_max_internal::rhs_proxy
 */
template <typename Index, typename Type, typename Compare>
inline min_max_internal::rhs_proxy< op_max_index_view<Index, Type, Compare> >
max_of(const op_max_index_view<Index, Type, Compare>& view,
       const Index& index, const Type& value)
{
    return min_max_internal::make_proxy(std::pair<Index, Type>(index, value), view);
}

/// @copydoc max_of(const op_max_index_view<Index, Type, Compare>&, const Index&, const Type&)
template <typename Index, typename Type, typename Compare>
inline min_max_internal::rhs_proxy< op_max_index_view<Index, Type, Compare> >
max_of(const Index& index, const Type& value,
       const op_max_index_view<Index, Type, Compare>& view)
{
    return min_max_internal::make_proxy(std::pair<Index, Type>(index, value), view);
}

/// @copydoc max_of(const op_max_index_view<Index, Type, Compare>&, const Index&, const Type&)
template <typename Index, typename Type, typename Compare>
inline min_max_internal::rhs_proxy< op_max_index_view<Index, Type, Compare> >
max_of(const op_max_index_view<Index, Type, Compare>& view,
       const std::pair<Index, Type>& pair)
{
    return min_max_internal::make_proxy(pair, view);
}

/// @copydoc max_of(const op_max_index_view<Index, Type, Compare>&, const Index&, const Type&)
template <typename Index, typename Type, typename Compare>
inline min_max_internal::rhs_proxy< op_max_index_view<Index, Type, Compare> >
max_of(const std::pair<Index, Type>& pair,
       const op_max_index_view<Index, Type, Compare>& view)
{
    return min_max_internal::make_proxy(pair, view);
}

/** Nested computation of the maximum of the value in a view and other values.
 *
 *  Compute the maximum of the result of a max_of() call and another value.
 *
 *  The result of this computation can only be assigned back to the original
 *  view or used in another max_of() call. For example,
 *
 *      *reducer = max_of(x, max_of(y, *reducer));
 *      *reducer = max_of(max_of(*reducer, x), y);
 *
 *  @see min_max_internal::rhs_proxy
 */
template <typename Index, typename Type, typename Compare>
inline min_max_internal::rhs_proxy< op_max_index_view<Index, Type, Compare> >
max_of(const min_max_internal::rhs_proxy< op_max_index_view<Index, Type, Compare> >& proxy,
       const Index& index, const Type& value)
{
    return proxy.calc(std::pair<Index, Type>(index, value));
}

/// @copydoc max_of(const min_max_internal::rhs_proxy< op_max_index_view<Index, Type, Compare> >&, const Index&, const Type&)
template <typename Index, typename Type, typename Compare>
inline min_max_internal::rhs_proxy< op_max_index_view<Index, Type, Compare> >
max_of(const Index& index, const Type& value,
       const min_max_internal::rhs_proxy< op_max_index_view<Index, Type, Compare> >& proxy)
{
    return proxy.calc(std::pair<Index, Type>(index, value));
}

/// @copydoc max_of(const min_max_internal::rhs_proxy< op_max_index_view<Index, Type, Compare> >&, const Index&, const Type&)
template <typename Index, typename Type, typename Compare>
inline min_max_internal::rhs_proxy< op_max_index_view<Index, Type, Compare> >
max_of(const min_max_internal::rhs_proxy< op_max_index_view<Index, Type, Compare> >& proxy,
       const std::pair<Index, Type>& pair)
{
    return proxy.calc(pair);
}

/// @copydoc max_of(const min_max_internal::rhs_proxy< op_max_index_view<Index, Type, Compare> >&, const Index&, const Type&)
template <typename Index, typename Type, typename Compare>
inline min_max_internal::rhs_proxy< op_max_index_view<Index, Type, Compare> >
max_of(const std::pair<Index, Type>& pair,
       const min_max_internal::rhs_proxy< op_max_index_view<Index, Type, Compare> >& proxy)
{
    return proxy.calc(pair);
}


/** Monoid class for maximum reductions with index. Instantiate the
 *  cilk::reducer template class with an op_max_index monoid to create a
 *  max_index reducer class. For example, to compute the maximum of an array of
 *  `double` values and the array index of the max value:
 *
 *      cilk::reducer< cilk::op_max_index<unsigned, double> > r;
 *
 *  @see ReducersMinMax
 *  @see op_max_index_view
 */
template < typename Index
         , typename Type
         , typename Compare=std::less<Type>
         , bool     Align = false
         >
class op_max_index : public min_max_internal::monoid_base<op_max_index_view<Index, Type, Compare>, Align> 
{
    typedef min_max_internal::monoid_base<
        op_max_index_view<Index, Type, Compare>, Align> base;
public:
    /// Construct with default comparator.
    op_max_index() {}
    /// Construct with specified comparator.
    op_max_index(const Compare& compare) : base(compare) {}
};

//@}



/** @defgroup ReducersMinMaxMinIndex Minimum reducers (value and index)
 *
 *  These reducers will find the smallest value from a set of values, and its
 *  index in the set.
 *
 *  @ingroup ReducersMinMax
 */
//@{

/** The minimum index reducer view class.
 *
 *  This is the view class for reducers created with 
 *  `cilk::reducer<cilk::op_min_index<Index, Type, Compare> >`. It accumulates
 *  the minimum, as determined by a comparator, of a set of values which have
 *  occurred as arguments to the `calc_min()` function, and records the index
 *  of the minimum value. The accumulated value will be the first argument `x`
 *  such that `compare(y, x)` is false for every argument `y`.
 *
 *  If the comparator is `std::less`, then the accumulated value is the first
 *  argument value which no other argument value is less than, i.e., the
 *  minimum.
 *
 *  @note   The reducer “dereference” operation (`reducer::operator *()`) 
 *          yields a reference to the view. Thus, for example, the view class’s
 *          `calc_min()` function would be
 *          used in an expression like `r->calc_min(i, a)`where `r` is an
 *          op_min_index reducer variable.
 *
 *  @note   The word “index” suggests an integer index into an array, but there
 *          is no restriction on the index type or how it should be used. In
 *          general, it may be convenient to use it for any kind of key that 
 *          can be used to locate the minimum value in the collection that it
 *          came from — for example:
 *              -   An index into an array.
 *              -   A key into an STL map.
 *              -   An iterator into any STL container.
 *
 *  @note   A min_index reducer is essentially a min reducer whose value type 
 *          is a `std::pair<Index, Type>`. This fact is camouflaged in the view
 *          `calc_min` function, the global `min_of` functions, and the reducer
 *          value constructor, which can all take an index argument and a value
 *          argument as an alternative to a single `std::pair` argument.
 *          However, the reducer `set_value()`, `get_value()`, `move_in()`, and
 *          `move_out()` functions work only with pairs, not with individual
 *          value and/or index arguments.
 *
 *  @tparam Index   The type of the indices associated with the values.
 *  @tparam Type    The type of the values compared by the reducer. This will 
 *                  be the value type of a monoid_with_view that is 
 *                  instantiated with this view.
 *  @tparam Compare Used to compare the values. It must be a binary predicate.
 *                  If it is omitted, then the view computes the conventional
 *                  arithmetic minimum.
 *
 *  @see ReducersMinMax
 *  @see op_min_index
 */
template <typename Index, typename Type, typename Compare>
class op_min_index_view : public min_max_internal::view_base<
    min_max_internal::index_view_content<Index, Type, Compare, false>,
    Compare,
    min_max_internal::reverse_predicate<Compare, Type> >
{
    typedef min_max_internal::view_base<
        min_max_internal::index_view_content<Index, Type, Compare, false>,
        Compare,
        min_max_internal::reverse_predicate<Compare, Type> > base;
    using base::calc;
    using base::assign;
    typedef std::pair<Index, Type> pair_type;
    friend class min_max_internal::rhs_proxy<op_min_index_view>;

public:
    /** @name Constructors.
     *
     *  All op_min_index_view constructors simply pass their arguments on to the 
     *  @ref view_base base class, except for the `(index, value [, compare])`
     *  constructors, which create a `std::pair` containing the index and value.
     */
    //@{
    
    op_min_index_view() : base() {}
    
    template <typename T1>
    op_min_index_view(const T1& x1) : base(x1) {}
    
    template <typename T1, typename T2>
    op_min_index_view(const T1& x1, const T2& x2) : base(x1, x2) {}
    
    template <typename T1, typename T2, typename T3>
    op_min_index_view(const T1& x1, const T2& x2, const T3& x3) : base(x1, x2, x3) {}
    
    op_min_index_view(const Index& i, const Type& v) : base(pair_type(i, v)) {}
    
    op_min_index_view(const Index& i, const Type& v, const typename base::compare_type* c) : 
        base(pair_type(i, v), c) {}
    
    //@}    

    /** Minimize with a value and index.
     *
     *  If @a x is greater than the current value of the view (as defined by 
     *  the reducer’s comparator), or if the view was created without an 
     *  initial value and its value has never been updated (with `calc_min()` 
     *  or `= min_of()`), then the value of the view is set to @a x, and the
     *  index is set to @a i..
     *
     *  @param  i   The index of the value @a x.
     *  @param  x   The value to minimize the view’s value with.
     *
     *  @return     A reference to the view. (Allows 
     *              `view.comp_min(i, a).comp_min(j, b)…`.)
     */
    op_min_index_view& calc_min(const Index& i, const Type& x) 
        { calc(pair_type(i, x)); return *this; }

    /** Maximize with an index/value pair.
     *
     *  If @a pair.second is less than the current value of the view (as
     *  defined by the reducer’s comparator), or if the view was created 
     *  without an initial value and its value has never been updated (with
     *  `calc_min()` or `= min_of()`), then the value of the view is set to
     *  @a pair.second, and the index is set to @a pair.first.
     *
     *  @param  pair    A pair containing a value to minimize the view’s value
     *                  with and its associated index.
     *
     *  @return         A reference to the view. (Allows
     *                  `view.comp_min(p1).comp_min(p2)…`.)
     */
    op_min_index_view& calc_min(const pair_type& pair) 
        { calc(pair); return *this; }

    /** Assign the result of a `min_of(view, index, value)` expression to the
     *  view.
     *
     *  @param  rhs An rhs_proxy value created by a `min_of(view, index, value)`
     *              expression.
     *
     *  @return     A reference to the view.
     *
     *  @see min_max_internal::view_base::rhs_proxy
     */
    op_min_index_view& operator=(const min_max_internal::rhs_proxy<op_min_index_view>& rhs) 
        { assign(rhs); return *this; }
};


/** Compute the minimum of the value in a view and another value.
 *
 *  The result of this computation can only be assigned back to the original
 *  view or used in another min_of() call. For example,
 *
 *      *reducer = min_of(*reducer, i, x);
 *      *reducer = min_of(i, x, *reducer);
 *
 *  @see min_max_internal::min_min_view_base::rhs_proxy
 */
template <typename Index, typename Type, typename Compare>
inline min_max_internal::rhs_proxy< op_min_index_view<Index, Type, Compare> >
min_of(const op_min_index_view<Index, Type, Compare>& view,
       const Index& index, const Type& value)
{
    return min_max_internal::make_proxy(std::pair<Index, Type>(index, value), view);
}

/// @copydoc min_of(const op_min_index_view<Index, Type, Compare>&, const Index&, const Type&)
template <typename Index, typename Type, typename Compare>
inline min_max_internal::rhs_proxy< op_min_index_view<Index, Type, Compare> >
min_of(const Index& index, const Type& value,
       const op_min_index_view<Index, Type, Compare>& view)
{
    return min_max_internal::make_proxy(std::pair<Index, Type>(index, value), view);
}

/// @copydoc min_of(const op_min_index_view<Index, Type, Compare>&, const Index&, const Type&)
template <typename Index, typename Type, typename Compare>
inline min_max_internal::rhs_proxy< op_min_index_view<Index, Type, Compare> >
min_of(const op_min_index_view<Index, Type, Compare>& view,
       const std::pair<Index, Type>& pair)
{
    return min_max_internal::make_proxy(pair, view);
}

/// @copydoc min_of(const op_min_index_view<Index, Type, Compare>&, const Index&, const Type&)
template <typename Index, typename Type, typename Compare>
inline min_max_internal::rhs_proxy< op_min_index_view<Index, Type, Compare> >
min_of(const std::pair<Index, Type>& pair,
       const op_min_index_view<Index, Type, Compare>& view)
{
    return min_max_internal::make_proxy(pair, view);
}

/** Nested computation of the minimum of the value in a view and other values.
 *
 *  Compute the minimum of the result of a min_of() call and another value.
 *
 *  The result of this computation can only be assigned back to the original
 *  view or used in another min_of() call. For example,
 *
 *      *reducer = min_of(x, min_of(y, *reducer));
 *      *reducer = min_of(min_of(*reducer, x), y);
 *
 *  @see min_max_internal::min_min_view_base::rhs_proxy
 */
template <typename Index, typename Type, typename Compare>
inline min_max_internal::rhs_proxy< op_min_index_view<Index, Type, Compare> >
min_of(const min_max_internal::rhs_proxy< op_min_index_view<Index, Type, Compare> >& proxy,
       const Index& index, const Type& value)
{
    return proxy.calc(std::pair<Index, Type>(index, value));
}

/// @copydoc min_of(const min_max_internal::rhs_proxy< op_min_index_view<Index, Type, Compare> >&, const Index&, const Type&)
template <typename Index, typename Type, typename Compare>
inline min_max_internal::rhs_proxy< op_min_index_view<Index, Type, Compare> >
min_of(const Index& index, const Type& value,
       const min_max_internal::rhs_proxy< op_min_index_view<Index, Type, Compare> >& proxy)
{
    return proxy.calc(std::pair<Index, Type>(index, value));
}

/// @copydoc min_of(const min_max_internal::rhs_proxy< op_min_index_view<Index, Type, Compare> >&, const Index&, const Type&)
template <typename Index, typename Type, typename Compare>
inline min_max_internal::rhs_proxy< op_min_index_view<Index, Type, Compare> >
min_of(const min_max_internal::rhs_proxy< op_min_index_view<Index, Type, Compare> >& proxy,
       const std::pair<Index, Type>& pair)
{
    return proxy.calc(pair);
}

/// @copydoc min_of(const min_max_internal::rhs_proxy< op_min_index_view<Index, Type, Compare> >&, const Index&, const Type&)
template <typename Index, typename Type, typename Compare>
inline min_max_internal::rhs_proxy< op_min_index_view<Index, Type, Compare> >
min_of(const std::pair<Index, Type>& pair,
       const min_max_internal::rhs_proxy< op_min_index_view<Index, Type, Compare> >& proxy)
{
    return proxy.calc(pair);
}


/** Monoid class for minimum reductions with index. Instantiate the
 *  cilk::reducer template class with an op_min_index monoid to create a
 *  min_index reducer class. For example, to compute the minimum of an array of 
 *  `double` values and the array index of the min value:
 *
 *      cilk::reducer< cilk::op_min_index<unsigned, double> > r;
 *
 *  @see ReducersMinMax
 *  @see op_min_index_view
 */
template < typename Index
         , typename Type
         , typename Compare=std::less<Type>
         , bool     Align = false
         >
class op_min_index : public min_max_internal::monoid_base<op_min_index_view<Index, Type, Compare>, Align> 
{
    typedef min_max_internal::monoid_base<
        op_min_index_view<Index, Type, Compare>, Align> base;
public:
    /// Construct with default comparator.
    op_min_index() {}
    /// Construct with specified comparator.
    op_min_index(const Compare& compare) : base(compare) {}
};

//@}


/** Deprecated maximum reducer wrapper class.
 *
 *  reducer_max is the same as @ref reducer<@ref op_max>, except that
 *  reducer_max is a proxy for the contained view, so that accumulator
 *  variable update operations can be applied directly to the reducer. For
 *  example, a value is maximized with  a `reducer<%op_max>` with
 *  `r->calc_max(a)`, but a value can be maximized with a `%reducer_max` with 
 *  `r.calc_max(a)`.
 *
 *
 *  @deprecated Users are strongly encouraged to use `reducer<monoid>`
 *              reducers rather than the old wrappers like reducer_max. 
 *              The `reducer<monoid>` reducers show the reducer/monoid/view
 *              architecture more clearly, are more consistent in their
 *              implementation, and present a simpler model for new
 *              user-implemented reducers.
 *
 *  @note   Implicit conversions are provided between `%reducer_max` 
 *          and `reducer<%op_max>`. This allows incremental code
 *          conversion: old code that used `%reducer_max` can pass a
 *          `%reducer_max` to a converted function that now expects a
 *          pointer or reference to a `reducer<%op_max>`, and vice
 *          versa. **But see  @ref redminmax_compatibility.**
 *
 *  @tparam Type    The value type of the reducer.
 *  @tparam Compare The “less than” comparator type for the reducer.
 *
 *  @see op_max
 *  @see op_max_view
 *  @see reducer
 *  @see ReducersMinMax
 *  @ingroup ReducersMinMaxMaxValue
 */
template <typename Type, typename Compare=std::less<Type> >
class reducer_max : public reducer< op_max<Type, Compare, true> >
{
    __CILKRTS_STATIC_ASSERT(
        ::cilk::internal::class_is_empty< 
            typename ::cilk::internal::binary_functor<Compare>::type >::value, 
        "cilk::reducer_max<Type, Compare> only works with "
        "an empty Compare class");
    typedef reducer< op_max<Type, Compare, true> > base;
public:
    
    /// Type of data in a reducer_max.
    typedef Type                            basic_value_type;
    
    /// The view type for the reducer.
    typedef typename base::view_type        view_type;
    
    /// The view type for the reducer.
    typedef typename base::view_type        View;
    
    /// The monoid type for the reducer.
    typedef typename base::monoid_type      monoid_type;
    
    /// The monoid type for the reducer.
    typedef typename base::monoid_type      Monoid;

    /// The view’s rhs proxy type.          
    typedef min_max_internal::rhs_proxy<View> rhs_proxy;
    
    using base::view;

    /** @name Constructors
     */
    //@{
    
    /// Construct the wrapper in its identity state (either `!is_set()`, or
    /// `value() == identity value`).
    reducer_max() : base() {}

    /// Construct the wrapper with a specified initial value.
    explicit reducer_max(const Type& initial_value) : base(initial_value) {}

    /// Construct the wrapper in its identity state with a specified 
    /// comparator.
    explicit reducer_max(const Compare& comp) : base(comp) {}

    /// Construct the wrapper with a specified initial value and a specified 
    /// comparator.
    reducer_max(const Type& initial_value, const Compare& comp)
    :   base(initial_value, comp) {}
    
    //@}

    /** @name Forwarded functions
     *  @details Functions that update the contained accumulator variable are
     *  simply forwarded to the contained @ref op_max_view. */
    //@{

    /// @copydoc cilk_lib_1_0::min_max_internal::view_content::is_set() const
    bool is_set() const { return view().is_set(); }

    /// @copydoc op_max_view::calc_max(const Type&)
    reducer_max& calc_max(const Type& x) 
        { view().calc_max(x); return *this; }

    /// @copydoc op_max_view::operator=(const min_max_internal::rhs_proxy<op_max_view>&) 
    reducer_max& operator=(const rhs_proxy& rhs)
        { view() = rhs; return *this; }
        
    //@}

    /** Allow read-only access to the value within the current view.
     * 
     *  @returns    A const reference to the value within the current view.
     */
    const Type& get_reference() const { return view().get_reference(); }
    
    /// @name Dereference
    /** Dereferencing a wrapper is a no-op. It simply returns the wrapper.
     *  Combined with the rule that a wrapper forwards view operations to the
     *  view, this means that view operations can be written the same way on
     *  reducers and wrappers, which is convenient for incrementally
     *  converting code using wrappers to code using reducers. That is:
     *
     *      reducer< op_max<int> > r;
     *      r->calc_max(a);      // *r returns the view
     *                           // calc_max is a view member function
     *
     *      reducer_max<int> w;
     *      w->calc_max(a);      // *w returns the wrapper
     *                           // calc_max is a wrapper member function that
     *                           // calls the corresponding view function
     */
    //@{
    reducer_max&       operator*()       { return *this; }
    reducer_max const& operator*() const { return *this; }

    reducer_max*       operator->()       { return this; }
    reducer_max const* operator->() const { return this; }
    //@}
    
    /** @name Upcast
     *  @details In Cilk library 0.9, reducers were always cache-aligned. In
     *  library  1.0, reducer cache alignment is optional. By default, reducers
     *  are unaligned (i.e., just naturally aligned), but legacy wrappers
     *  inherit from cache-aligned reducers for binary compatibility.
     *
     *  This means that a wrapper will automatically be upcast to its aligned
     *  reducer base class. The following conversion operators provide
     *  pseudo-upcasts to the corresponding unaligned reducer class.
     */
    //@{
    operator reducer< op_max<Type, Compare, false> >& ()
    {
        return *reinterpret_cast< reducer< op_max<Type, Compare, false> >* >(this);
    }
    
    operator const reducer< op_max<Type, Compare, false> >& () const
    {
        return *reinterpret_cast< const reducer< op_max<Type, Compare, false> >* >(this);
    }
    //@}
};


/// @cond internal
// The legacy definition of max_of(reducer_max, value) has different
// behavior and a different return type than this definition. We add an 
// unused third argument to this version of the function to give it a different
// signature, so that they won’t end up sharing a single object file entry.
struct max_of_1_0_t {};
const max_of_1_0_t max_of_1_0 = {};
/// @endcond

/** Compute the maximum of the value in a reducer_max and another value.
 *
 *  @deprecated Because reducer_max is deprecated.
 *
 *  The result of this computation can only be assigned back to the original 
 *  reducer or used in another max_of() call. For example,
 *
 *      reducer = max_of(reducer, x);
 *      reducer = max_of(x, reducer);
 *
 *  @see min_max_internal::rhs_proxy
 *
 *  @ingroup ReducersMinMaxMaxValue
 */
template <typename Type, typename Compare>
inline min_max_internal::rhs_proxy< op_max_view<Type, Compare> >
max_of(const reducer_max<Type, Compare>& r, const Type& value,
        const max_of_1_0_t& = max_of_1_0)
{
    return min_max_internal::make_proxy(value, r.view());
}

/// @copydoc max_of(const reducer_max<Type, Compare>&, const Type&, const max_of_1_0_t&)
/// @ingroup ReducersMinMaxMaxValue
template <typename Type, typename Compare>
inline min_max_internal::rhs_proxy< op_max_view<Type, Compare> >
max_of(const Type& value, const reducer_max<Type, Compare>& r,
        const max_of_1_0_t& = max_of_1_0)
{
    return min_max_internal::make_proxy(value, r.view());
}


/** Deprecated minimum reducer wrapper class.
 *
 *  reducer_min is the same as @ref reducer<@ref op_min>, except that
 *  reducer_min is a proxy for the contained view, so that accumulator
 *  variable update operations can be applied directly to the reducer. For
 *  example, a value is minimized with  a `reducer<%op_min>` with
 *  `r->calc_min(a)`, but a value can be minimized with a `%reducer_min` with 
 *  `r.calc_min(a)`.
 *
 *
 *  @deprecated Users are strongly encouraged to use `reducer<monoid>`
 *              reducers rather than the old wrappers like reducer_min. 
 *              The `reducer<monoid>` reducers show the reducer/monoid/view
 *              architecture more clearly, are more consistent in their
 *              implementation, and present a simpler model for new
 *              user-implemented reducers.
 *
 *  @note   Implicit conversions are provided between `%reducer_min` 
 *          and `reducer<%op_min>`. This allows incremental code
 *          conversion: old code that used `%reducer_min` can pass a
 *          `%reducer_min` to a converted function that now expects a
 *          pointer or reference to a `reducer<%op_min>`, and vice
 *          versa. **But see  @ref redminmax_compatibility.**
 *
 *  @tparam Type    The value type of the reducer.
 *  @tparam Compare The “less than” comparator type for the reducer.
 *
 *  @see op_min
 *  @see op_min_view
 *  @see reducer
 *  @see ReducersMinMax
 *  @ingroup ReducersMinMaxMinValue
 */
template <typename Type, typename Compare=std::less<Type> >
class reducer_min : public reducer< op_min<Type, Compare, true> >
{
    __CILKRTS_STATIC_ASSERT(
        ::cilk::internal::class_is_empty<
            typename ::cilk::internal::binary_functor<Compare>::type >::value, 
        "cilk::reducer_min<Type, Compare> only works with "
        "an empty Compare class");
    typedef reducer< op_min<Type, Compare, true> > base;
public:
    
    /// Type of data in a reducer_min.
    typedef Type                            basic_value_type;
    
    /// The view type for the reducer.
    typedef typename base::view_type        view_type;
    
    /// The view type for the reducer.
    typedef typename base::view_type        View;
    
    /// The monoid type for the reducer.
    typedef typename base::monoid_type      monoid_type;
    
    /// The monoid type for the reducer.
    typedef typename base::monoid_type      Monoid;

    /// The view’s rhs proxy type.          
    typedef min_max_internal::rhs_proxy<View> rhs_proxy;
    
    using base::view;

    /** @name Constructors
     */
    //@{
    
    /// Construct the wrapper in its identity state (either `!is_set()`, or
    /// `value() == identity value`).
    reducer_min() : base() {}

    /// Construct the wrapper with a specified initial value.
    explicit reducer_min(const Type& initial_value) : base(initial_value) {}

    /// Construct the wrapper in its identity state with a specified 
    /// comparator.
    explicit reducer_min(const Compare& comp) : base(comp) {}

    /// Construct the wrapper with a specified initial value and a specified 
    /// comparator.
    reducer_min(const Type& initial_value, const Compare& comp)
    :   base(initial_value, comp) {}
    
    //@}

    /** @name Forwarded functions
     *  @details Functions that update the contained accumulator variable are
     *  simply forwarded to the contained @ref op_min_view. */
    //@{

    /// @copydoc cilk_lib_1_0::min_max_internal::view_content::is_set() const
    bool is_set() const { return view().is_set(); }

    /// @copydoc op_min_view::calc_min(const Type&)
    reducer_min& calc_min(const Type& x) 
        { view().calc_min(x); return *this; }

    /// @copydoc op_min_view::operator=(const min_max_internal::rhs_proxy<op_min_view>&) 
    reducer_min& operator=(const rhs_proxy& rhs)
        { view() = rhs; return *this; }
        
    //@}

    /** Allow read-only access to the value within the current view.
     * 
     *  @returns    A const reference to the value within the current view.
     */
    const Type& get_reference() const { return view().get_reference(); }
    
    /// @name Dereference
    /** Dereferencing a wrapper is a no-op. It simply returns the wrapper.
     *  Combined with the rule that a wrapper forwards view operations to the
     *  view, this means that view operations can be written the same way on
     *  reducers and wrappers, which is convenient for incrementally
     *  converting code using wrappers to code using reducers. That is:
     *
     *      reducer< op_min<int> > r;
     *      r->calc_min(a);      // *r returns the view
     *                           // calc_min is a view member function
     *
     *      reducer_min<int> w;
     *      w->calc_min(a);      // *w returns the wrapper
     *                           // calc_min is a wrapper member function that
     *                           // calls the corresponding view function
     */
    //@{
    reducer_min&       operator*()       { return *this; }
    reducer_min const& operator*() const { return *this; }

    reducer_min*       operator->()       { return this; }
    reducer_min const* operator->() const { return this; }
    //@}
    
    /** @name Upcast
     *  @details In Cilk library 0.9, reducers were always cache-aligned. In
     *  library  1.0, reducer cache alignment is optional. By default, reducers
     *  are unaligned (i.e., just naturally aligned), but legacy wrappers
     *  inherit from cache-aligned reducers for binary compatibility.
     *
     *  This means that a wrapper will automatically be upcast to its aligned
     *  reducer base class. The following conversion operators provide
     *  pseudo-upcasts to the corresponding unaligned reducer class.
     */
    //@{
    operator reducer< op_min<Type, Compare, false> >& ()
    {
        return *reinterpret_cast< reducer< op_min<Type, Compare, false> >* >(this);
    }
    
    operator const reducer< op_min<Type, Compare, false> >& () const
    {
        return *reinterpret_cast< const reducer< op_min<Type, Compare, false> >* >(this);
    }
    //@}
};


/** Compute the minimum of a reducer and a value.
 *
 *  @deprecated Because reducer_min is deprecated.
 */
//@{
// The legacy definition of min_of(reducer_min, value) has different
// behavior and a different return type than this definition. We add an 
// unused third argument to this version of the function to give it a different
// signature, so that they won’t end up sharing a single object file entry.
struct min_of_1_0_t {};
const min_of_1_0_t min_of_1_0 = {};

template <typename Type, typename Compare>
inline min_max_internal::rhs_proxy< op_min_view<Type, Compare> >
min_of(const reducer_min<Type, Compare>& r, const Type& value,
        const min_of_1_0_t& = min_of_1_0)
{
    return min_max_internal::make_proxy(value, r.view());
}

template <typename Type, typename Compare>
inline min_max_internal::rhs_proxy< op_min_view<Type, Compare> >
min_of(const Type& value, const reducer_min<Type, Compare>& r,
        const min_of_1_0_t& = min_of_1_0)
{
    return min_max_internal::make_proxy(value, r.view());
}
//@}


/** Deprecated maximum with index reducer wrapper class.
 *
 *  reducer_max_index is the same as @ref reducer<@ref op_max_index>, except
 *  that reducer_max_index is a proxy for the contained view, so that
 *  accumulator variable update operations can be applied directly to the
 *  reducer. For example, a value is maximized with  a `reducer<%op_max_index>`
 *  with `r->calc_max(i, a)`, but a value can be maximized with a 
 *  `%reducer_max` with `r.calc_max(i, aa)`.
 *
 *
 *  @deprecated Users are strongly encouraged to use `reducer<monoid>`
 *              reducers rather than the old wrappers like reducer_max. 
 *              The `reducer<monoid>` reducers show the reducer/monoid/view
 *              architecture more clearly, are more consistent in their
 *              implementation, and present a simpler model for new
 *              user-implemented reducers.
 *
 *  @note   Implicit conversions are provided between `%reducer_max_index` 
 *          and `reducer<%op_max_index>`. This allows incremental code
 *          conversion: old code that used `%reducer_max_index` can pass a
 *          `%reducer_max_index` to a converted function that now expects a
 *          pointer or reference to a `reducer<%op_max_index>`, and vice
 *          versa. **But see  @ref redminmax_compatibility.**
 *
 *  @tparam Index   The index type of the reducer.
 *  @tparam Type    The value type of the reducer.
 *  @tparam Compare The “less than” comparator type for the reducer.
 *
 *  @see op_max_index
 *  @see op_max_index_view
 *  @see reducer
 *  @see ReducersMinMax
 *  @ingroup ReducersMinMaxMaxIndex
 */
template < typename Index
         , typename Type
         , typename Compare = std::less<Type>
         >
class reducer_max_index : 
    public reducer< op_max_index<Index, Type, Compare, true> >
{
    __CILKRTS_STATIC_ASSERT(
        ::cilk::internal::class_is_empty< 
            typename ::cilk::internal::binary_functor<Compare>::type >::value, 
        "cilk::reducer_max_index<Type, Compare> only works with "
        "an empty Compare class");
    typedef reducer< op_max_index<Index, Type, Compare, true> > base;
public:
    
    /// Type of data in a reducer_max_index.
    typedef Type                            basic_value_type;
    
    /// The view type for the reducer.
    typedef typename base::view_type        view_type;
    
    /// The view type for the reducer.
    typedef typename base::view_type        View;
    
    /// The monoid type for the reducer.
    typedef typename base::monoid_type      monoid_type;
    
    /// The monoid type for the reducer.
    typedef typename base::monoid_type      Monoid;

    /// The view’s rhs proxy type.          
    typedef min_max_internal::rhs_proxy<View> rhs_proxy;
    
    using base::view;

    /** @name Constructors
     */
    //@{
    
    /// Construct the wrapper in its identity state (`!is_set()`).
    reducer_max_index() : base() {}

    /// Construct with a specified initial index and value.
    reducer_max_index(const Index& initial_index,
                      const Type& initial_value)
    : base(initial_index, initial_value) {}

    /// Construct the wrapper with a specified comparator.
    explicit reducer_max_index(const Compare& comp) : base(comp) {}

    /// Construct the wrapper with a specified initial index, value, 
    /// and comparator.
    reducer_max_index(const Index& initial_index,
                      const Type& initial_value,
                      const Compare& comp)
    : base(initial_index, initial_value, comp) {}

    //@}
    
    /** @name Set / Get
     */
    //@{
    
    /// Set the index and value of this object.
    void set_value(const Index& index, const Type& value)
        { base::set_value(std::make_pair(index, value)); }

    /// Return the maximum value.
    const Type& get_value() const 
        { return view().get_reference(); }

    /// Return the maximum index.
    const Index& get_index() const 
        { return view().get_index_reference(); }

    /// Return a const reference to value data member in the view.
    const Type& get_reference() const
        { return view().get_reference(); }
    
    /// Return a const reference to index data member in the view.
    const Index& get_index_reference() const 
        { return view().get_index_reference(); }
    
    //@}
    
    /** @name Forwarded functions
     *  @details Functions that update the contained accumulator variable are
     *  simply forwarded to the contained @ref op_max_view. */
    //@{

    /// @copydoc cilk_lib_1_0::min_max_internal::view_content::is_set() const
    bool is_set() const { return view().is_set(); }

    /// @copydoc op_max_index_view::calc_max(const Index&, const Type&)
    reducer_max_index& calc_max(const Index& i, const Type& x) 
        { view().calc_max(i, x); return *this; }

    /// @copydoc op_max_view::operator=(const min_max_internal::rhs_proxy<op_max_view>&) 
    reducer_max_index& operator=(const rhs_proxy& rhs)
        { view() = rhs; return *this; }
        
    //@}

    /// @name Dereference
    /** Dereferencing a wrapper is a no-op. It simply returns the wrapper.
     *  Combined with the rule that a wrapper forwards view operations to the
     *  view, this means that view operations can be written the same way on
     *  reducers and wrappers, which is convenient for incrementally
     *  converting code using wrappers to code using reducers. That is:
     *
     *      reducer< op_max_index<int, int> > r;
     *      r->calc_max(i, a);   // *r returns the view
     *                           // calc_max is a view member function
     *
     *      reducer_max_index<int, int> w;
     *      w->calc_max(i, a);   // *w returns the wrapper
     *                           // calc_max is a wrapper member function that
     *                           // calls the corresponding view function
     */
    //@{
    reducer_max_index&       operator*()       { return *this; }
    reducer_max_index const& operator*() const { return *this; }

    reducer_max_index*       operator->()       { return this; }
    reducer_max_index const* operator->() const { return this; }
    //@}
    
    /** @name Upcast
     *  @details In Cilk library 0.9, reducers were always cache-aligned. In
     *  library  1.0, reducer cache alignment is optional. By default, reducers
     *  are unaligned (i.e., just naturally aligned), but legacy wrappers
     *  inherit from cache-aligned reducers for binary compatibility.
     *
     *  This means that a wrapper will automatically be upcast to its aligned
     *  reducer base class. The following conversion operators provide
     *  pseudo-upcasts to the corresponding unaligned reducer class.
     */
    //@{
    operator reducer< op_max_index<Index, Type, Compare, false> >& ()
    {
        return *reinterpret_cast< reducer< op_max_index<Index, Type, Compare, false> >* >(this);
    }
    
    operator const reducer< op_max_index<Index, Type, Compare, false> >& () const
    {
        return *reinterpret_cast< const reducer< op_max_index<Index, Type, Compare, false> >* >(this);
    }
    //@}
    
};


/** Deprecated minimum with index reducer wrapper class.
 *
 *  reducer_min_index is the same as @ref reducer<@ref op_min_index>, except
 *  that reducer_min_index is a proxy for the contained view, so that
 *  accumulator variable update operations can be applied directly to the
 *  reducer. For example, a value is minimized with  a `reducer<%op_min_index>`
 *  with `r->calc_min(i, a)`, but a value can be minimized with a 
 *  `%reducer_min` with `r.calc_min(i, aa)`.
 *
 *
 *  @deprecated Users are strongly encouraged to use `reducer<monoid>`
 *              reducers rather than the old wrappers like reducer_min. 
 *              The `reducer<monoid>` reducers show the reducer/monoid/view
 *              architecture more clearly, are more consistent in their
 *              implementation, and present a simpler model for new
 *              user-implemented reducers.
 *
 *  @note   Implicit conversions are provided between `%reducer_min_index` 
 *          and `reducer<%op_min_index>`. This allows incremental code
 *          conversion: old code that used `%reducer_min_index` can pass a
 *          `%reducer_min_index` to a converted function that now expects a
 *          pointer or reference to a `reducer<%op_min_index>`, and vice
 *          versa. **But see  @ref redminmax_compatibility.**
 *
 *  @tparam Index   The index type of the reducer.
 *  @tparam Type    The value type of the reducer.
 *  @tparam Compare The “less than” comparator type for the reducer.
 *
 *  @see op_min_index
 *  @see op_min_index_view
 *  @see reducer
 *  @see ReducersMinMax
 *  @ingroup ReducersMinMaxMinIndex
 */
template < typename Index
         , typename Type
         , typename Compare = std::less<Type>
         >
class reducer_min_index : 
    public reducer< op_min_index<Index, Type, Compare, true> >
{
    __CILKRTS_STATIC_ASSERT(
        ::cilk::internal::class_is_empty< 
            typename ::cilk::internal::binary_functor<Compare>::type >::value, 
        "cilk::reducer_min_index<Type, Compare> only works with "
        "an empty Compare class");
    typedef reducer< op_min_index<Index, Type, Compare, true> > base;
public:
    
    /// Type of data in a reducer_min_index.
    typedef Type                            basic_value_type;
    
    /// The view type for the reducer.
    typedef typename base::view_type        view_type;
    
    /// The view type for the reducer.
    typedef typename base::view_type        View;
    
    /// The monoid type for the reducer.
    typedef typename base::monoid_type      monoid_type;
    
    /// The monoid type for the reducer.
    typedef typename base::monoid_type      Monoid;

    /// The view’s rhs proxy type.          
    typedef min_max_internal::rhs_proxy<View> rhs_proxy;
    
    using base::view;

    /** @name Constructors
     */
    //@{
    
    /// Construct the wrapper in its identity state (`!is_set()`).
    reducer_min_index() : base() {}

    /// Construct with a specified initial index and value.
    reducer_min_index(const Index& initial_index,
                      const Type& initial_value)
    : base(initial_index, initial_value) {}

    /// Construct the wrapper with a specified comparator.
    explicit reducer_min_index(const Compare& comp) : base(comp) {}

    /// Construct the wrapper with a specified initial index, value, 
    /// and comparator.
    reducer_min_index(const Index& initial_index,
                      const Type& initial_value,
                      const Compare& comp)
    : base(initial_index, initial_value, comp) {}

    //@}
    
    /** @name Set / Get
     */
    //@{
    
    /// Set the index and value of this object.
    void set_value(const Index& index, const Type& value)
        { base::set_value(std::make_pair(index, value)); }

    /// Return the minimum value.
    const Type& get_value() const 
        { return view().get_reference(); }

    /// Return the minimum index.
    const Index& get_index() const 
        { return view().get_index_reference(); }

    /// Return a const reference to value data member in the view.
    const Type& get_reference() const
        { return view().get_reference(); }
    
    /// Return a const reference to index data member in the view.
    const Index& get_index_reference() const 
        { return view().get_index_reference(); }
    
    //@}
    
    /** @name Forwarded functions
     *  @details Functions that update the contained accumulator variable are
     *  simply forwarded to the contained @ref op_min_view. */
    //@{

    /// @copydoc cilk_lib_1_0::min_max_internal::view_content::is_set() const
    bool is_set() const { return view().is_set(); }

    /// @copydoc op_min_index_view::calc_min(const Index&, const Type&)
    reducer_min_index& calc_min(const Index& i, const Type& x) 
        { view().calc_min(i, x); return *this; }

    /// @copydoc op_min_view::operator=(const min_max_internal::rhs_proxy<op_min_view>&) 
    reducer_min_index& operator=(const rhs_proxy& rhs)
        { view() = rhs; return *this; }
        
    //@}

    /// @name Dereference
    /** Dereferencing a wrapper is a no-op. It simply returns the wrapper.
     *  Combined with the rule that a wrapper forwards view operations to the
     *  view, this means that view operations can be written the same way on
     *  reducers and wrappers, which is convenient for incrementally
     *  converting code using wrappers to code using reducers. That is:
     *
     *      reducer< op_min_index<int, int> > r;
     *      r->calc_min(i, a);   // *r returns the view
     *                           // calc_min is a view member function
     *
     *      reducer_min_index<int, int> w;
     *      w->calc_min(i, a);   // *w returns the wrapper
     *                           // calc_min is a wrapper member function that
     *                           // calls the corresponding view function
     */
    //@{
    reducer_min_index&       operator*()       { return *this; }
    reducer_min_index const& operator*() const { return *this; }

    reducer_min_index*       operator->()       { return this; }
    reducer_min_index const* operator->() const { return this; }
    //@}
    
    /** @name Upcast
     *  @details In Cilk library 0.9, reducers were always cache-aligned. In
     *  library  1.0, reducer cache alignment is optional. By default, reducers
     *  are unaligned (i.e., just naturally aligned), but legacy wrappers
     *  inherit from cache-aligned reducers for binary compatibility.
     *
     *  This means that a wrapper will automatically be upcast to its aligned
     *  reducer base class. The following conversion operators provide
     *  pseudo-upcasts to the corresponding unaligned reducer class.
     */
    //@{
    operator reducer< op_min_index<Index, Type, Compare, false> >& ()
    {
        return *reinterpret_cast< reducer< op_min_index<Index, Type, Compare, false> >* >(this);
    }
    
    operator const reducer< op_min_index<Index, Type, Compare, false> >& () const
    {
        return *reinterpret_cast< const reducer< op_min_index<Index, Type, Compare, false> >* >(this);
    }
    //@}
    
};


#ifndef CILK_LIBRARY_0_9_REDUCER_MINMAX
} // namespace cilk_lib_1_0
using namespace cilk_lib_1_0;
#endif


/// @cond internal
/** Metafunction specialization for reducer conversion.
 *
 *  These specializations of the @ref legacy_reducer_downcast template class
 *  defined in reducer.h causes each `reducer< op_xxxx<Type> >` classes to have
 *  an `operator reducer_xxxx<Type>& ()` conversion operator that statically
 *  downcasts the `reducer<op_xxxx>` to the corresponding `reducer_xxxx` type.
 *  (The reverse conversion, from `reducer_xxxx` to `reducer<op_xxxx>`, is just
 *  an upcast, which is provided for free by the language.)
 */
template <typename Type, typename Compare, bool Align>
struct legacy_reducer_downcast< reducer< op_max<Type, Compare, Align> > >
{
    typedef reducer_max<Type> type;
};

template <typename Type, typename Compare, bool Align>
struct legacy_reducer_downcast< reducer< op_min<Type, Compare, Align> > >
{
    typedef reducer_min<Type> type;
};

template <typename Index, typename Type, typename Compare, bool Align>
struct legacy_reducer_downcast< reducer< op_max_index<Index, Type, Compare, Align> > >
{
    typedef reducer_max_index<Index, Type> type;
};

template <typename Index, typename Type, typename Compare, bool Align>
struct legacy_reducer_downcast< reducer< op_min_index<Index, Type, Compare, Align> > >
{
    typedef reducer_min_index<Index, Type> type;
};
/// @endcond

} // namespace cilk

#endif // __cplusplus


/** @name C language reducer macros
 *
 *  These macros are used to declare and work with numeric minimum and maximum reducers in C
 *  code.
 *
 *  @see @ref page_reducers_in_c
 */
 //@{
 

#ifdef CILK_C_DEFINE_REDUCERS

/* Integer min/max constants */
#include <limits.h>

/* Wchar_t min/max constants */
#if defined(_MSC_VER) || defined(ANDROID)
#   include <wchar.h>
#else
#   include <stdint.h>
#endif

/* Floating-point min/max constants */
#include <math.h>
#ifndef HUGE_VALF
    static const unsigned int __huge_valf[] = {0x7f800000};
#   define HUGE_VALF (*((const float *)__huge_valf))
#endif

#ifndef HUGE_VALL
    static const unsigned int __huge_vall[] = {0, 0, 0x00007f80, 0};
#   define HUGE_VALL (*((const long double *)__huge_vall))
#endif

#endif

/** Max reducer type name.
 *
 *  This macro expands into the identifier which is the name of the max reducer
 *  type for a specified numeric type.
 *
 *  @param  tn  The @ref reducers_c_type_names "numeric type name" specifying the type of the
 *              reducer.
 *
 *  @see @ref reducers_c_predefined
 */
#define CILK_C_REDUCER_MAX_TYPE(tn)                                         \
    __CILKRTS_MKIDENT(cilk_c_reducer_max_,tn)

/** Declare a max reducer object.
 *
 *  This macro expands into a declaration of a max reducer object for a specified numeric
 *  type. For example:
 *
 *      CILK_C_REDUCER_MAX(my_reducer, double, -DBL_MAX);
 *
 *  @param  obj The variable name to be used for the declared reducer object.
 *  @param  tn  The @ref reducers_c_type_names "numeric type name" specifying the type of the
 *              reducer.
 *  @param  v   The initial value for the reducer. (A value which can be assigned to the 
 *              numeric type represented by @a tn.)
 *
 *  @see @ref reducers_c_predefined
 */
#define CILK_C_REDUCER_MAX(obj,tn,v)                                        \
    CILK_C_REDUCER_MAX_TYPE(tn) obj =                                       \
        CILK_C_INIT_REDUCER(_Typeof(obj.value),                             \
                        __CILKRTS_MKIDENT(cilk_c_reducer_max_reduce_,tn),   \
                        __CILKRTS_MKIDENT(cilk_c_reducer_max_identity_,tn), \
                        __cilkrts_hyperobject_noop_destroy, v)

/** Maximize with a value.
 *
 *  `CILK_C_REDUCER_MAX_CALC(reducer, v)` sets the current view of the
 *  reducer to the max of its previous value and a specified new value.
 *  This is equivalent to
 *
 *      REDUCER_VIEW(reducer) = max(REDUCER_VIEW(reducer), v)
 *
 *  @param reducer  The reducer whose contained value is to be updated.
 *  @param v        The value that it is to be maximized with.
 */
#define CILK_C_REDUCER_MAX_CALC(reducer, v) do {                            \
    _Typeof((reducer).value)* view = &(REDUCER_VIEW(reducer));              \
    _Typeof(v) __value = (v);                                               \
    if (*view < __value) {                                                  \
        *view = __value;                                                    \
    } } while (0)

/// @cond internal

/** Declare the max reducer functions for a numeric type.
 *
 *  This macro expands into external function declarations for functions which implement
 *  the reducer functionality for the max reducer type for a specified numeric type.
 *
 *  @param  t   The value type of the reducer.
 *  @param  tn  The value “type name” identifier, used to construct the reducer type name,
 *              function names, etc.
 */
#define CILK_C_REDUCER_MAX_DECLARATION(t,tn,id)                             \
    typedef CILK_C_DECLARE_REDUCER(t) CILK_C_REDUCER_MAX_TYPE(tn);       \
    __CILKRTS_DECLARE_REDUCER_REDUCE(cilk_c_reducer_max,tn,l,r);         \
    __CILKRTS_DECLARE_REDUCER_IDENTITY(cilk_c_reducer_max,tn);
 
/** Define the max reducer functions for a numeric type.
 *
 *  This macro expands into function definitions for functions which implement the
 *  reducer functionality for the max reducer type for a specified numeric type.
 *
 *  @param  t   The value type of the reducer.
 *  @param  tn  The value “type name” identifier, used to construct the reducer type name,
 *              function names, etc.
 */
#define CILK_C_REDUCER_MAX_DEFINITION(t,tn,id)                           \
    typedef CILK_C_DECLARE_REDUCER(t) CILK_C_REDUCER_MAX_TYPE(tn);       \
    __CILKRTS_DECLARE_REDUCER_REDUCE(cilk_c_reducer_max,tn,l,r)          \
        { if (*(t*)l < *(t*)r) *(t*)l = *(t*)r; }                        \
    __CILKRTS_DECLARE_REDUCER_IDENTITY(cilk_c_reducer_max,tn)            \
        { *(t*)v = id; }
 
//@{
/** @def CILK_C_REDUCER_MAX_INSTANCE 
 *  @brief Declare or define implementation functions for a reducer type.
 *
 *  In the runtime source file c_reducers.c, the macro `CILK_C_DEFINE_REDUCERS` will be defined, and
 *  this macro will generate reducer implementation functions. Everywhere else, `CILK_C_DEFINE_REDUCERS`
 *  will be undefined, and this macro will expand into external declarations for the functions.
 */
#ifdef CILK_C_DEFINE_REDUCERS
#   define CILK_C_REDUCER_MAX_INSTANCE(t,tn,id)  \
        CILK_C_REDUCER_MAX_DEFINITION(t,tn,id)
#else
#   define CILK_C_REDUCER_MAX_INSTANCE(t,tn,id)  \
        CILK_C_REDUCER_MAX_DECLARATION(t,tn,id)
#endif
//@}

/*  Declare or define an instance of the reducer type and its functions for each 
 *  numeric type.
 */
__CILKRTS_BEGIN_EXTERN_C
CILK_C_REDUCER_MAX_INSTANCE(char,               char,       CHAR_MIN)
CILK_C_REDUCER_MAX_INSTANCE(unsigned char,      uchar,      0)
CILK_C_REDUCER_MAX_INSTANCE(signed char,        schar,      SCHAR_MIN)
CILK_C_REDUCER_MAX_INSTANCE(wchar_t,            wchar_t,    WCHAR_MIN)
CILK_C_REDUCER_MAX_INSTANCE(short,              short,      SHRT_MIN)
CILK_C_REDUCER_MAX_INSTANCE(unsigned short,     ushort,     0)
CILK_C_REDUCER_MAX_INSTANCE(int,                int,        INT_MIN)
CILK_C_REDUCER_MAX_INSTANCE(unsigned int,       uint,       0)
CILK_C_REDUCER_MAX_INSTANCE(unsigned int,       unsigned,   0) // alternate name
CILK_C_REDUCER_MAX_INSTANCE(long,               long,       LONG_MIN)
CILK_C_REDUCER_MAX_INSTANCE(unsigned long,      ulong,      0)
CILK_C_REDUCER_MAX_INSTANCE(long long,          longlong,   LLONG_MIN)
CILK_C_REDUCER_MAX_INSTANCE(unsigned long long, ulonglong,  0)
CILK_C_REDUCER_MAX_INSTANCE(float,              float,      -HUGE_VALF)
CILK_C_REDUCER_MAX_INSTANCE(double,             double,     -HUGE_VAL)
CILK_C_REDUCER_MAX_INSTANCE(long double,        longdouble, -HUGE_VALL)
__CILKRTS_END_EXTERN_C

/// @endcond

/** Max_index reducer type name.
 *
 *  This macro expands into the identifier which is the name of the max_index reducer
 *  type for a specified numeric type.
 *
 *  @param  tn  The @ref reducers_c_type_names "numeric type name" specifying the type of the
 *              reducer.
 *
 *  @see @ref reducers_c_predefined
 */
#define CILK_C_REDUCER_MAX_INDEX_TYPE(tn)                                         \
    __CILKRTS_MKIDENT(cilk_c_reducer_max_index_,tn)

/** Declare an op_max_index reducer object.
 *
 *  This macro expands into a declaration of a max_index reducer object for a specified
 *  numeric type. For example:
 *
 *      CILK_C_REDUCER_MAX_INDEX(my_reducer, double, -DBL_MAX_INDEX);
 *
 *  @param  obj The variable name to be used for the declared reducer object.
 *  @param  tn  The @ref reducers_c_type_names "numeric type name" specifying the type of the
 *              reducer.
 *  @param  v   The initial value for the reducer. (A value which can be assigned to the 
 *              numeric type represented by @a tn.)
 *
 *  @see @ref reducers_c_predefined
 */
#define CILK_C_REDUCER_MAX_INDEX(obj,tn,v)                                        \
    CILK_C_REDUCER_MAX_INDEX_TYPE(tn) obj =                                       \
        CILK_C_INIT_REDUCER(_Typeof(obj.value),                             \
                        __CILKRTS_MKIDENT(cilk_c_reducer_max_index_reduce_,tn),   \
                        __CILKRTS_MKIDENT(cilk_c_reducer_max_index_identity_,tn), \
                        __cilkrts_hyperobject_noop_destroy, {0, v})

/** Maximize with a value.
 *
 *  `CILK_C_REDUCER_MAX_INDEX_CALC(reducer, i, v)` sets the current view of the
 *  reducer to the max of its previous value and a specified new value.
 *  This is equivalent to
 *
 *      REDUCER_VIEW(reducer) = max_index(REDUCER_VIEW(reducer), v)
 *
 *  If the value of the reducer is changed to @a v, then the index of the reducer is
 *  changed to @a i. 
 *
 *  @param reducer  The reducer whose contained value and index are to be updated.
 *  @param i        The index associated with the new value.
 *  @param v        The value that it is to be maximized with.
 */
#define CILK_C_REDUCER_MAX_INDEX_CALC(reducer, i, v) do {                   \
    _Typeof((reducer).value)* view = &(REDUCER_VIEW(reducer));              \
    _Typeof(v) __value = (v);                                               \
    if (view->value < __value) {                                            \
        view->index = (i);                                                  \
        view->value = __value;                                              \
    } } while (0)

/// @cond internal

/** Declare the max_index view type.
 *
 *  The view of a max_index reducer is a structure containing both the
 *  maximum value for the reducer and the index that was associated with
 *  that value in the sequence of input values.
 */
#define CILK_C_REDUCER_MAX_INDEX_VIEW(t,tn)                                  \
    typedef struct {                                                         \
        __STDNS ptrdiff_t index;                                             \
        t                 value;                                             \
    } __CILKRTS_MKIDENT(cilk_c_reducer_max_index_view_,tn)

/** Declare the max_index reducer functions for a numeric type.
 *
 *  This macro expands into external function declarations for functions which implement
 *  the reducer functionality for the max_index reducer type for a specified numeric type.
 *
 *  @param  t   The value type of the reducer.
 *  @param  tn  The value “type name” identifier, used to construct the reducer type name,
 *              function names, etc.
 */
#define CILK_C_REDUCER_MAX_INDEX_DECLARATION(t,tn,id)                       \
    CILK_C_REDUCER_MAX_INDEX_VIEW(t,tn);                                    \
    typedef CILK_C_DECLARE_REDUCER(                                         \
        __CILKRTS_MKIDENT(cilk_c_reducer_max_index_view_,tn))               \
            CILK_C_REDUCER_MAX_INDEX_TYPE(tn);                              \
    __CILKRTS_DECLARE_REDUCER_REDUCE(cilk_c_reducer_max_index,tn,l,r);      \
    __CILKRTS_DECLARE_REDUCER_IDENTITY(cilk_c_reducer_max_index,tn);
 
/** Define the max_index reducer functions for a numeric type.
 *
 *  This macro expands into function definitions for functions which implement the
 *  reducer functionality for the max_index reducer type for a specified numeric type.
 *
 *  @param  t   The value type of the reducer.
 *  @param  tn  The value “type name” identifier, used to construct the reducer type name,
 *              function names, etc.
 */
#define CILK_C_REDUCER_MAX_INDEX_DEFINITION(t,tn,id)                           \
    CILK_C_REDUCER_MAX_INDEX_VIEW(t,tn);                                    \
    typedef CILK_C_DECLARE_REDUCER(                                         \
        __CILKRTS_MKIDENT(cilk_c_reducer_max_index_view_,tn))               \
            CILK_C_REDUCER_MAX_INDEX_TYPE(tn);                              \
    __CILKRTS_DECLARE_REDUCER_REDUCE(cilk_c_reducer_max_index,tn,l,r)          \
        { typedef __CILKRTS_MKIDENT(cilk_c_reducer_max_index_view_,tn) view_t; \
          if (((view_t*)l)->value < ((view_t*)r)->value)                       \
              *(view_t*)l = *(view_t*)r; }                                     \
    __CILKRTS_DECLARE_REDUCER_IDENTITY(cilk_c_reducer_max_index,tn)            \
        { typedef __CILKRTS_MKIDENT(cilk_c_reducer_max_index_view_,tn) view_t; \
          ((view_t*)v)->index = 0; ((view_t*)v)->value = id; }
 
//@{
/** @def CILK_C_REDUCER_MAX_INDEX_INSTANCE 
 *  @brief Declare or define implementation functions for a reducer type.
 *
 *  In the runtime source file c_reducers.c, the macro `CILK_C_DEFINE_REDUCERS` will be defined, and
 *  this macro will generate reducer implementation functions. Everywhere else, `CILK_C_DEFINE_REDUCERS`
 *  will be undefined, and this macro will expand into external declarations for the functions.
 */
#ifdef CILK_C_DEFINE_REDUCERS
#   define CILK_C_REDUCER_MAX_INDEX_INSTANCE(t,tn,id)  \
        CILK_C_REDUCER_MAX_INDEX_DEFINITION(t,tn,id)
#else
#   define CILK_C_REDUCER_MAX_INDEX_INSTANCE(t,tn,id)  \
        CILK_C_REDUCER_MAX_INDEX_DECLARATION(t,tn,id)
#endif
//@}

/*  Declare or define an instance of the reducer type and its functions for each 
 *  numeric type.
 */
__CILKRTS_BEGIN_EXTERN_C
CILK_C_REDUCER_MAX_INDEX_INSTANCE(char,               char,       CHAR_MIN)
CILK_C_REDUCER_MAX_INDEX_INSTANCE(unsigned char,      uchar,      0)
CILK_C_REDUCER_MAX_INDEX_INSTANCE(signed char,        schar,      SCHAR_MIN)
CILK_C_REDUCER_MAX_INDEX_INSTANCE(wchar_t,            wchar_t,    WCHAR_MIN)
CILK_C_REDUCER_MAX_INDEX_INSTANCE(short,              short,      SHRT_MIN)
CILK_C_REDUCER_MAX_INDEX_INSTANCE(unsigned short,     ushort,     0)
CILK_C_REDUCER_MAX_INDEX_INSTANCE(int,                int,        INT_MIN)
CILK_C_REDUCER_MAX_INDEX_INSTANCE(unsigned int,       uint,       0)
CILK_C_REDUCER_MAX_INDEX_INSTANCE(unsigned int,       unsigned,   0) // alternate name
CILK_C_REDUCER_MAX_INDEX_INSTANCE(long,               long,       LONG_MIN)
CILK_C_REDUCER_MAX_INDEX_INSTANCE(unsigned long,      ulong,      0)
CILK_C_REDUCER_MAX_INDEX_INSTANCE(long long,          longlong,   LLONG_MIN)
CILK_C_REDUCER_MAX_INDEX_INSTANCE(unsigned long long, ulonglong,  0)
CILK_C_REDUCER_MAX_INDEX_INSTANCE(float,              float,      -HUGE_VALF)
CILK_C_REDUCER_MAX_INDEX_INSTANCE(double,             double,     -HUGE_VAL)
CILK_C_REDUCER_MAX_INDEX_INSTANCE(long double,        longdouble, -HUGE_VALL)
__CILKRTS_END_EXTERN_C

/// @endcond

/** Min reducer type name.
 *
 *  This macro expands into the identifier which is the name of the min reducer
 *  type for a specified numeric type.
 *
 *  @param  tn  The @ref reducers_c_type_names "numeric type name" specifying the type of the
 *              reducer.
 *
 *  @see @ref reducers_c_predefined
 */
#define CILK_C_REDUCER_MIN_TYPE(tn)                                         \
    __CILKRTS_MKIDENT(cilk_c_reducer_min_,tn)

/** Declare a min reducer object.
 *
 *  This macro expands into a declaration of a min reducer object for a specified numeric
 *  type. For example:
 *
 *      CILK_C_REDUCER_MIN(my_reducer, double, DBL_MAX);
 *
 *  @param  obj The variable name to be used for the declared reducer object.
 *  @param  tn  The @ref reducers_c_type_names "numeric type name" specifying the type of the
 *              reducer.
 *  @param  v   The initial value for the reducer. (A value which can be assigned to the 
 *              numeric type represented by @a tn.)
 *
 *  @see @ref reducers_c_predefined
 */
#define CILK_C_REDUCER_MIN(obj,tn,v)                                        \
    CILK_C_REDUCER_MIN_TYPE(tn) obj =                                       \
        CILK_C_INIT_REDUCER(_Typeof(obj.value),                             \
                        __CILKRTS_MKIDENT(cilk_c_reducer_min_reduce_,tn),   \
                        __CILKRTS_MKIDENT(cilk_c_reducer_min_identity_,tn), \
                        __cilkrts_hyperobject_noop_destroy, v)

/** Minimize with a value.
 *
 *  `CILK_C_REDUCER_MIN_CALC(reducer, v)` sets the current view of the
 *  reducer to the min of its previous value and a specified new value.
 *  This is equivalent to
 *
 *      REDUCER_VIEW(reducer) = min(REDUCER_VIEW(reducer), v)
 *
 *  @param reducer  The reducer whose contained value is to be updated.
 *  @param v        The value that it is to be minimized with.
 */
#define CILK_C_REDUCER_MIN_CALC(reducer, v) do {                            \
    _Typeof((reducer).value)* view = &(REDUCER_VIEW(reducer));              \
    _Typeof(v) __value = (v);                                               \
    if (*view > __value) {                                                  \
        *view = __value;                                                    \
    } } while (0)

/// @cond internal

/** Declare the min reducer functions for a numeric type.
 *
 *  This macro expands into external function declarations for functions which implement
 *  the reducer functionality for the min reducer type for a specified numeric type.
 *
 *  @param  t   The value type of the reducer.
 *  @param  tn  The value “type name” identifier, used to construct the reducer type name,
 *              function names, etc.
 */
#define CILK_C_REDUCER_MIN_DECLARATION(t,tn,id)                             \
    typedef CILK_C_DECLARE_REDUCER(t) CILK_C_REDUCER_MIN_TYPE(tn);       \
    __CILKRTS_DECLARE_REDUCER_REDUCE(cilk_c_reducer_min,tn,l,r);         \
    __CILKRTS_DECLARE_REDUCER_IDENTITY(cilk_c_reducer_min,tn);
 
/** Define the min reducer functions for a numeric type.
 *
 *  This macro expands into function definitions for functions which implement the
 *  reducer functionality for the min reducer type for a specified numeric type.
 *
 *  @param  t   The value type of the reducer.
 *  @param  tn  The value “type name” identifier, used to construct the reducer type name,
 *              function names, etc.
 */
#define CILK_C_REDUCER_MIN_DEFINITION(t,tn,id)                           \
    typedef CILK_C_DECLARE_REDUCER(t) CILK_C_REDUCER_MIN_TYPE(tn);       \
    __CILKRTS_DECLARE_REDUCER_REDUCE(cilk_c_reducer_min,tn,l,r)          \
        { if (*(t*)l > *(t*)r) *(t*)l = *(t*)r; }                        \
    __CILKRTS_DECLARE_REDUCER_IDENTITY(cilk_c_reducer_min,tn)            \
        { *(t*)v = id; }
 
//@{
/** @def CILK_C_REDUCER_MIN_INSTANCE 
 *  @brief Declare or define implementation functions for a reducer type.
 *
 *  In the runtime source file c_reducers.c, the macro `CILK_C_DEFINE_REDUCERS` will be defined, and
 *  this macro will generate reducer implementation functions. Everywhere else, `CILK_C_DEFINE_REDUCERS`
 *  will be undefined, and this macro will expand into external declarations for the functions.
 */
#ifdef CILK_C_DEFINE_REDUCERS
#   define CILK_C_REDUCER_MIN_INSTANCE(t,tn,id)  \
        CILK_C_REDUCER_MIN_DEFINITION(t,tn,id)
#else
#   define CILK_C_REDUCER_MIN_INSTANCE(t,tn,id)  \
        CILK_C_REDUCER_MIN_DECLARATION(t,tn,id)
#endif
//@}

/*  Declare or define an instance of the reducer type and its functions for each 
 *  numeric type.
 */
__CILKRTS_BEGIN_EXTERN_C
CILK_C_REDUCER_MIN_INSTANCE(char,               char,       CHAR_MAX)
CILK_C_REDUCER_MIN_INSTANCE(unsigned char,      uchar,      CHAR_MAX)
CILK_C_REDUCER_MIN_INSTANCE(signed char,        schar,      SCHAR_MAX)
CILK_C_REDUCER_MIN_INSTANCE(wchar_t,            wchar_t,    WCHAR_MAX)
CILK_C_REDUCER_MIN_INSTANCE(short,              short,      SHRT_MAX)
CILK_C_REDUCER_MIN_INSTANCE(unsigned short,     ushort,     USHRT_MAX)
CILK_C_REDUCER_MIN_INSTANCE(int,                int,        INT_MAX)
CILK_C_REDUCER_MIN_INSTANCE(unsigned int,       uint,       UINT_MAX)
CILK_C_REDUCER_MIN_INSTANCE(unsigned int,       unsigned,   UINT_MAX) // alternate name
CILK_C_REDUCER_MIN_INSTANCE(long,               long,       LONG_MAX)
CILK_C_REDUCER_MIN_INSTANCE(unsigned long,      ulong,      ULONG_MAX)
CILK_C_REDUCER_MIN_INSTANCE(long long,          longlong,   LLONG_MAX)
CILK_C_REDUCER_MIN_INSTANCE(unsigned long long, ulonglong,  ULLONG_MAX)
CILK_C_REDUCER_MIN_INSTANCE(float,              float,      HUGE_VALF)
CILK_C_REDUCER_MIN_INSTANCE(double,             double,     HUGE_VAL)
CILK_C_REDUCER_MIN_INSTANCE(long double,        longdouble, HUGE_VALL)
__CILKRTS_END_EXTERN_C

/// @endcond

/** Min_index reducer type name.
 *
 *  This macro expands into the identifier which is the name of the min_index reducer
 *  type for a specified numeric type.
 *
 *  @param  tn  The @ref reducers_c_type_names "numeric type name" specifying the type of the
 *              reducer.
 *
 *  @see @ref reducers_c_predefined
 */
#define CILK_C_REDUCER_MIN_INDEX_TYPE(tn)                                         \
    __CILKRTS_MKIDENT(cilk_c_reducer_min_index_,tn)

/** Declare an op_min_index reducer object.
 *
 *  This macro expands into a declaration of a min_index reducer object for a specified
 *  numeric type. For example:
 *
 *      CILK_C_REDUCER_MIN_INDEX(my_reducer, double, -DBL_MIN_INDEX);
 *
 *  @param  obj The variable name to be used for the declared reducer object.
 *  @param  tn  The @ref reducers_c_type_names "numeric type name" specifying the type of the
 *              reducer.
 *  @param  v   The initial value for the reducer. (A value which can be assigned to the 
 *              numeric type represented by @a tn.)
 *
 *  @see @ref reducers_c_predefined
 */
#define CILK_C_REDUCER_MIN_INDEX(obj,tn,v)                                        \
    CILK_C_REDUCER_MIN_INDEX_TYPE(tn) obj =                                       \
        CILK_C_INIT_REDUCER(_Typeof(obj.value),                             \
                        __CILKRTS_MKIDENT(cilk_c_reducer_min_index_reduce_,tn),   \
                        __CILKRTS_MKIDENT(cilk_c_reducer_min_index_identity_,tn), \
                        __cilkrts_hyperobject_noop_destroy, {0, v})

/** Minimize with a value.
 *
 *  `CILK_C_REDUCER_MIN_INDEX_CALC(reducer, i, v)` sets the current view of the
 *  reducer to the min of its previous value and a specified new value.
 *  This is equivalent to
 *
 *      REDUCER_VIEW(reducer) = min_index(REDUCER_VIEW(reducer), v)
 *
 *  If the value of the reducer is changed to @a v, then the index of the reducer is
 *  changed to @a i. 
 *
 *  @param reducer  The reducer whose contained value and index are to be updated.
 *  @param i        The index associated with the new value.
 *  @param v        The value that it is to be minimized with.
 */
#define CILK_C_REDUCER_MIN_INDEX_CALC(reducer, i, v) do {                   \
    _Typeof((reducer).value)* view = &(REDUCER_VIEW(reducer));              \
    _Typeof(v) __value = (v);                                               \
    if (view->value > __value) {                                            \
        view->index = (i);                                                  \
        view->value = __value;                                              \
    } } while (0)

/// @cond internal

/** Declare the min_index view type.
 *
 *  The view of a min_index reducer is a structure containing both the
 *  minimum value for the reducer and the index that was associated with
 *  that value in the sequence of input values.
 */
#define CILK_C_REDUCER_MIN_INDEX_VIEW(t,tn)                                  \
    typedef struct {                                                         \
        __STDNS ptrdiff_t index;                                             \
        t                 value;                                             \
    } __CILKRTS_MKIDENT(cilk_c_reducer_min_index_view_,tn)

/** Declare the min_index reducer functions for a numeric type.
 *
 *  This macro expands into external function declarations for functions which implement
 *  the reducer functionality for the min_index reducer type for a specified numeric type.
 *
 *  @param  t   The value type of the reducer.
 *  @param  tn  The value “type name” identifier, used to construct the reducer type name,
 *              function names, etc.
 */
#define CILK_C_REDUCER_MIN_INDEX_DECLARATION(t,tn,id)                       \
    CILK_C_REDUCER_MIN_INDEX_VIEW(t,tn);                                    \
    typedef CILK_C_DECLARE_REDUCER(                                         \
        __CILKRTS_MKIDENT(cilk_c_reducer_min_index_view_,tn))               \
            CILK_C_REDUCER_MIN_INDEX_TYPE(tn);                              \
    __CILKRTS_DECLARE_REDUCER_REDUCE(cilk_c_reducer_min_index,tn,l,r);      \
    __CILKRTS_DECLARE_REDUCER_IDENTITY(cilk_c_reducer_min_index,tn);
 
/** Define the min_index reducer functions for a numeric type.
 *
 *  This macro expands into function definitions for functions which implement the
 *  reducer functionality for the min_index reducer type for a specified numeric type.
 *
 *  @param  t   The value type of the reducer.
 *  @param  tn  The value “type name” identifier, used to construct the reducer type name,
 *              function names, etc.
 */
#define CILK_C_REDUCER_MIN_INDEX_DEFINITION(t,tn,id)                           \
    CILK_C_REDUCER_MIN_INDEX_VIEW(t,tn);                                    \
    typedef CILK_C_DECLARE_REDUCER(                                         \
        __CILKRTS_MKIDENT(cilk_c_reducer_min_index_view_,tn))               \
            CILK_C_REDUCER_MIN_INDEX_TYPE(tn);                              \
    __CILKRTS_DECLARE_REDUCER_REDUCE(cilk_c_reducer_min_index,tn,l,r)          \
        { typedef __CILKRTS_MKIDENT(cilk_c_reducer_min_index_view_,tn) view_t; \
          if (((view_t*)l)->value > ((view_t*)r)->value)                       \
              *(view_t*)l = *(view_t*)r; }                                     \
    __CILKRTS_DECLARE_REDUCER_IDENTITY(cilk_c_reducer_min_index,tn)            \
        { typedef __CILKRTS_MKIDENT(cilk_c_reducer_min_index_view_,tn) view_t; \
          ((view_t*)v)->index = 0; ((view_t*)v)->value = id; }
 
//@{
/** @def CILK_C_REDUCER_MIN_INDEX_INSTANCE 
 *  @brief Declare or define implementation functions for a reducer type.
 *
 *  In the runtime source file c_reducers.c, the macro `CILK_C_DEFINE_REDUCERS` will be defined, and
 *  this macro will generate reducer implementation functions. Everywhere else, `CILK_C_DEFINE_REDUCERS`
 *  will be undefined, and this macro will expand into external declarations for the functions.
 */
#ifdef CILK_C_DEFINE_REDUCERS
#   define CILK_C_REDUCER_MIN_INDEX_INSTANCE(t,tn,id)  \
        CILK_C_REDUCER_MIN_INDEX_DEFINITION(t,tn,id)
#else
#   define CILK_C_REDUCER_MIN_INDEX_INSTANCE(t,tn,id)  \
        CILK_C_REDUCER_MIN_INDEX_DECLARATION(t,tn,id)
#endif
//@}

/*  Declare or define an instance of the reducer type and its functions for each 
 *  numeric type.
 */
__CILKRTS_BEGIN_EXTERN_C
CILK_C_REDUCER_MIN_INDEX_INSTANCE(char,               char,       CHAR_MAX)
CILK_C_REDUCER_MIN_INDEX_INSTANCE(unsigned char,      uchar,      CHAR_MAX)
CILK_C_REDUCER_MIN_INDEX_INSTANCE(signed char,        schar,      SCHAR_MAX)
CILK_C_REDUCER_MIN_INDEX_INSTANCE(wchar_t,            wchar_t,    WCHAR_MAX)
CILK_C_REDUCER_MIN_INDEX_INSTANCE(short,              short,      SHRT_MAX)
CILK_C_REDUCER_MIN_INDEX_INSTANCE(unsigned short,     ushort,     USHRT_MAX)
CILK_C_REDUCER_MIN_INDEX_INSTANCE(int,                int,        INT_MAX)
CILK_C_REDUCER_MIN_INDEX_INSTANCE(unsigned int,       uint,       UINT_MAX)
CILK_C_REDUCER_MIN_INDEX_INSTANCE(unsigned int,       unsigned,   UINT_MAX) // alternate name
CILK_C_REDUCER_MIN_INDEX_INSTANCE(long,               long,       LONG_MAX)
CILK_C_REDUCER_MIN_INDEX_INSTANCE(unsigned long,      ulong,      ULONG_MAX)
CILK_C_REDUCER_MIN_INDEX_INSTANCE(long long,          longlong,   LLONG_MAX)
CILK_C_REDUCER_MIN_INDEX_INSTANCE(unsigned long long, ulonglong,  ULLONG_MAX)
CILK_C_REDUCER_MIN_INDEX_INSTANCE(float,              float,      HUGE_VALF)
CILK_C_REDUCER_MIN_INDEX_INSTANCE(double,             double,     HUGE_VAL)
CILK_C_REDUCER_MIN_INDEX_INSTANCE(long double,        longdouble, HUGE_VALL)
__CILKRTS_END_EXTERN_C

/// @endcond

//@}

#endif // defined REDUCER_MAX_H_INCLUDED