1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
4130
4131
4132
4133
4134
4135
4136
4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159
4160
4161
4162
4163
4164
4165
4166
4167
4168
4169
4170
4171
4172
4173
4174
4175
4176
4177
4178
4179
4180
4181
4182
4183
4184
4185
4186
4187
4188
4189
4190
4191
4192
4193
4194
4195
4196
4197
4198
4199
4200
4201
4202
4203
4204
4205
4206
4207
4208
4209
4210
4211
4212
4213
4214
4215
4216
4217
4218
4219
4220
4221
4222
4223
4224
| /*
* Copyright 2010-2011 INRIA Saclay
* Copyright 2013-2014 Ecole Normale Superieure
* Copyright 2014 INRIA Rocquencourt
* Copyright 2016-2017 Sven Verdoolaege
*
* Use of this software is governed by the MIT license
*
* Written by Sven Verdoolaege, INRIA Saclay - Ile-de-France,
* Parc Club Orsay Universite, ZAC des vignes, 4 rue Jacques Monod,
* 91893 Orsay, France
* and Inria Paris - Rocquencourt, Domaine de Voluceau - Rocquencourt,
* B.P. 105 - 78153 Le Chesnay, France
*/
#include <isl_map_private.h>
#include <isl_union_map_private.h>
#include <isl/ctx.h>
#include <isl/hash.h>
#include <isl_aff_private.h>
#include <isl/map.h>
#include <isl/set.h>
#include <isl_space_private.h>
#include <isl/union_set.h>
#include <isl_maybe_map.h>
#include <bset_from_bmap.c>
#include <set_to_map.c>
#include <set_from_map.c>
#include <uset_to_umap.c>
#include <uset_from_umap.c>
#include <set_list_from_map_list_inl.c>
/* Return the number of parameters of "umap", where "type"
* is required to be set to isl_dim_param.
*/
unsigned isl_union_map_dim(__isl_keep isl_union_map *umap,
enum isl_dim_type type)
{
if (!umap)
return 0;
if (type != isl_dim_param)
isl_die(isl_union_map_get_ctx(umap), isl_error_invalid,
"can only reference parameters", return 0);
return isl_space_dim(umap->dim, type);
}
/* Return the number of parameters of "uset", where "type"
* is required to be set to isl_dim_param.
*/
unsigned isl_union_set_dim(__isl_keep isl_union_set *uset,
enum isl_dim_type type)
{
return isl_union_map_dim(uset, type);
}
/* Return the id of the specified dimension.
*/
__isl_give isl_id *isl_union_map_get_dim_id(__isl_keep isl_union_map *umap,
enum isl_dim_type type, unsigned pos)
{
if (!umap)
return NULL;
if (type != isl_dim_param)
isl_die(isl_union_map_get_ctx(umap), isl_error_invalid,
"can only reference parameters", return NULL);
return isl_space_get_dim_id(umap->dim, type, pos);
}
/* Is this union set a parameter domain?
*/
isl_bool isl_union_set_is_params(__isl_keep isl_union_set *uset)
{
isl_set *set;
isl_bool params;
if (!uset)
return isl_bool_error;
if (uset->table.n != 1)
return isl_bool_false;
set = isl_set_from_union_set(isl_union_set_copy(uset));
params = isl_set_is_params(set);
isl_set_free(set);
return params;
}
/* Is this union map actually a parameter domain?
* Users should never call this function. Outside of isl,
* a union map can never be a parameter domain.
*/
isl_bool isl_union_map_is_params(__isl_keep isl_union_map *umap)
{
return isl_union_set_is_params(uset_from_umap(umap));
}
static __isl_give isl_union_map *isl_union_map_alloc(
__isl_take isl_space *space, int size)
{
isl_union_map *umap;
space = isl_space_params(space);
if (!space)
return NULL;
umap = isl_calloc_type(space->ctx, isl_union_map);
if (!umap) {
isl_space_free(space);
return NULL;
}
umap->ref = 1;
umap->dim = space;
if (isl_hash_table_init(space->ctx, &umap->table, size) < 0)
return isl_union_map_free(umap);
return umap;
}
__isl_give isl_union_map *isl_union_map_empty(__isl_take isl_space *space)
{
return isl_union_map_alloc(space, 16);
}
__isl_give isl_union_set *isl_union_set_empty(__isl_take isl_space *space)
{
return isl_union_map_empty(space);
}
isl_ctx *isl_union_map_get_ctx(__isl_keep isl_union_map *umap)
{
return umap ? umap->dim->ctx : NULL;
}
isl_ctx *isl_union_set_get_ctx(__isl_keep isl_union_set *uset)
{
return uset ? uset->dim->ctx : NULL;
}
/* Return the space of "umap".
*/
__isl_keep isl_space *isl_union_map_peek_space(__isl_keep isl_union_map *umap)
{
return umap ? umap->dim : NULL;
}
__isl_give isl_space *isl_union_map_get_space(__isl_keep isl_union_map *umap)
{
return isl_space_copy(isl_union_map_peek_space(umap));
}
/* Return the position of the parameter with the given name
* in "umap".
* Return -1 if no such dimension can be found.
*/
int isl_union_map_find_dim_by_name(__isl_keep isl_union_map *umap,
enum isl_dim_type type, const char *name)
{
if (!umap)
return -1;
return isl_space_find_dim_by_name(umap->dim, type, name);
}
__isl_give isl_space *isl_union_set_get_space(__isl_keep isl_union_set *uset)
{
return isl_union_map_get_space(uset);
}
static isl_stat free_umap_entry(void **entry, void *user)
{
isl_map *map = *entry;
isl_map_free(map);
return isl_stat_ok;
}
static isl_stat add_map(__isl_take isl_map *map, void *user)
{
isl_union_map **umap = (isl_union_map **)user;
*umap = isl_union_map_add_map(*umap, map);
return isl_stat_ok;
}
__isl_give isl_union_map *isl_union_map_dup(__isl_keep isl_union_map *umap)
{
isl_union_map *dup;
if (!umap)
return NULL;
dup = isl_union_map_empty(isl_space_copy(umap->dim));
if (isl_union_map_foreach_map(umap, &add_map, &dup) < 0)
goto error;
return dup;
error:
isl_union_map_free(dup);
return NULL;
}
__isl_give isl_union_map *isl_union_map_cow(__isl_take isl_union_map *umap)
{
if (!umap)
return NULL;
if (umap->ref == 1)
return umap;
umap->ref--;
return isl_union_map_dup(umap);
}
struct isl_union_align {
isl_reordering *exp;
isl_union_map *res;
};
static isl_stat align_entry(void **entry, void *user)
{
isl_map *map = *entry;
isl_reordering *exp;
struct isl_union_align *data = user;
exp = isl_reordering_extend_space(isl_reordering_copy(data->exp),
isl_map_get_space(map));
data->res = isl_union_map_add_map(data->res,
isl_map_realign(isl_map_copy(map), exp));
return isl_stat_ok;
}
/* Align the parameters of umap along those of model.
* The result has the parameters of model first, in the same order
* as they appear in model, followed by any remaining parameters of
* umap that do not appear in model.
*/
__isl_give isl_union_map *isl_union_map_align_params(
__isl_take isl_union_map *umap, __isl_take isl_space *model)
{
struct isl_union_align data = { NULL, NULL };
isl_bool equal_params;
if (!umap || !model)
goto error;
equal_params = isl_space_has_equal_params(umap->dim, model);
if (equal_params < 0)
goto error;
if (equal_params) {
isl_space_free(model);
return umap;
}
data.exp = isl_parameter_alignment_reordering(umap->dim, model);
if (!data.exp)
goto error;
data.res = isl_union_map_alloc(isl_reordering_get_space(data.exp),
umap->table.n);
if (isl_hash_table_foreach(umap->dim->ctx, &umap->table,
&align_entry, &data) < 0)
goto error;
isl_reordering_free(data.exp);
isl_union_map_free(umap);
isl_space_free(model);
return data.res;
error:
isl_reordering_free(data.exp);
isl_union_map_free(umap);
isl_union_map_free(data.res);
isl_space_free(model);
return NULL;
}
__isl_give isl_union_set *isl_union_set_align_params(
__isl_take isl_union_set *uset, __isl_take isl_space *model)
{
return isl_union_map_align_params(uset, model);
}
__isl_give isl_union_map *isl_union_map_union(__isl_take isl_union_map *umap1,
__isl_take isl_union_map *umap2)
{
umap1 = isl_union_map_align_params(umap1, isl_union_map_get_space(umap2));
umap2 = isl_union_map_align_params(umap2, isl_union_map_get_space(umap1));
umap1 = isl_union_map_cow(umap1);
if (!umap1 || !umap2)
goto error;
if (isl_union_map_foreach_map(umap2, &add_map, &umap1) < 0)
goto error;
isl_union_map_free(umap2);
return umap1;
error:
isl_union_map_free(umap1);
isl_union_map_free(umap2);
return NULL;
}
__isl_give isl_union_set *isl_union_set_union(__isl_take isl_union_set *uset1,
__isl_take isl_union_set *uset2)
{
return isl_union_map_union(uset1, uset2);
}
__isl_give isl_union_map *isl_union_map_copy(__isl_keep isl_union_map *umap)
{
if (!umap)
return NULL;
umap->ref++;
return umap;
}
__isl_give isl_union_set *isl_union_set_copy(__isl_keep isl_union_set *uset)
{
return isl_union_map_copy(uset);
}
__isl_null isl_union_map *isl_union_map_free(__isl_take isl_union_map *umap)
{
if (!umap)
return NULL;
if (--umap->ref > 0)
return NULL;
isl_hash_table_foreach(umap->dim->ctx, &umap->table,
&free_umap_entry, NULL);
isl_hash_table_clear(&umap->table);
isl_space_free(umap->dim);
free(umap);
return NULL;
}
__isl_null isl_union_set *isl_union_set_free(__isl_take isl_union_set *uset)
{
return isl_union_map_free(uset);
}
/* Do "umap" and "space" have the same parameters?
*/
isl_bool isl_union_map_space_has_equal_params(__isl_keep isl_union_map *umap,
__isl_keep isl_space *space)
{
isl_space *umap_space;
umap_space = isl_union_map_peek_space(umap);
return isl_space_has_equal_params(umap_space, space);
}
/* Do "uset" and "space" have the same parameters?
*/
isl_bool isl_union_set_space_has_equal_params(__isl_keep isl_union_set *uset,
__isl_keep isl_space *space)
{
return isl_union_map_space_has_equal_params(uset_to_umap(uset), space);
}
static int has_space(const void *entry, const void *val)
{
isl_map *map = (isl_map *)entry;
isl_space *space = (isl_space *) val;
return isl_space_is_equal(map->dim, space);
}
__isl_give isl_union_map *isl_union_map_add_map(__isl_take isl_union_map *umap,
__isl_take isl_map *map)
{
uint32_t hash;
struct isl_hash_table_entry *entry;
isl_bool aligned;
if (!map || !umap)
goto error;
if (isl_map_plain_is_empty(map)) {
isl_map_free(map);
return umap;
}
aligned = isl_map_space_has_equal_params(map, umap->dim);
if (aligned < 0)
goto error;
if (!aligned) {
umap = isl_union_map_align_params(umap, isl_map_get_space(map));
map = isl_map_align_params(map, isl_union_map_get_space(umap));
}
umap = isl_union_map_cow(umap);
if (!map || !umap)
goto error;
hash = isl_space_get_hash(map->dim);
entry = isl_hash_table_find(umap->dim->ctx, &umap->table, hash,
&has_space, map->dim, 1);
if (!entry)
goto error;
if (!entry->data)
entry->data = map;
else {
entry->data = isl_map_union(entry->data, isl_map_copy(map));
if (!entry->data)
goto error;
isl_map_free(map);
}
return umap;
error:
isl_map_free(map);
isl_union_map_free(umap);
return NULL;
}
__isl_give isl_union_set *isl_union_set_add_set(__isl_take isl_union_set *uset,
__isl_take isl_set *set)
{
return isl_union_map_add_map(uset, set_to_map(set));
}
__isl_give isl_union_map *isl_union_map_from_map(__isl_take isl_map *map)
{
isl_space *dim;
isl_union_map *umap;
if (!map)
return NULL;
dim = isl_map_get_space(map);
dim = isl_space_params(dim);
umap = isl_union_map_empty(dim);
umap = isl_union_map_add_map(umap, map);
return umap;
}
__isl_give isl_union_set *isl_union_set_from_set(__isl_take isl_set *set)
{
return isl_union_map_from_map(set_to_map(set));
}
__isl_give isl_union_map *isl_union_map_from_basic_map(
__isl_take isl_basic_map *bmap)
{
return isl_union_map_from_map(isl_map_from_basic_map(bmap));
}
__isl_give isl_union_set *isl_union_set_from_basic_set(
__isl_take isl_basic_set *bset)
{
return isl_union_map_from_basic_map(bset);
}
struct isl_union_map_foreach_data
{
isl_stat (*fn)(__isl_take isl_map *map, void *user);
void *user;
};
static isl_stat call_on_copy(void **entry, void *user)
{
isl_map *map = *entry;
struct isl_union_map_foreach_data *data;
data = (struct isl_union_map_foreach_data *)user;
return data->fn(isl_map_copy(map), data->user);
}
int isl_union_map_n_map(__isl_keep isl_union_map *umap)
{
return umap ? umap->table.n : 0;
}
int isl_union_set_n_set(__isl_keep isl_union_set *uset)
{
return uset ? uset->table.n : 0;
}
isl_stat isl_union_map_foreach_map(__isl_keep isl_union_map *umap,
isl_stat (*fn)(__isl_take isl_map *map, void *user), void *user)
{
struct isl_union_map_foreach_data data = { fn, user };
if (!umap)
return isl_stat_error;
return isl_hash_table_foreach(umap->dim->ctx, &umap->table,
&call_on_copy, &data);
}
/* Internal data structure for isl_union_map_every_map.
*
* "test" is the user-specified callback function.
* "user" is the user-specified callback function argument.
*
* "failed" is initialized to 0 and set to 1 if "test" fails
* on any map.
*/
struct isl_union_map_every_data {
isl_bool (*test)(__isl_keep isl_map *map, void *user);
void *user;
int failed;
};
/* Call data->test on "map".
* If this fails, then set data->failed and abort.
*/
static isl_stat call_every(void **entry, void *user)
{
isl_map *map = *entry;
struct isl_union_map_every_data *data = user;
isl_bool r;
r = data->test(map, data->user);
if (r < 0)
return isl_stat_error;
if (r)
return isl_stat_ok;
data->failed = 1;
return isl_stat_error;
}
/* Does "test" succeed on every map in "umap"?
*/
isl_bool isl_union_map_every_map(__isl_keep isl_union_map *umap,
isl_bool (*test)(__isl_keep isl_map *map, void *user), void *user)
{
struct isl_union_map_every_data data = { test, user, 0 };
isl_stat r;
if (!umap)
return isl_bool_error;
r = isl_hash_table_foreach(isl_union_map_get_ctx(umap), &umap->table,
&call_every, &data);
if (r >= 0)
return isl_bool_true;
if (data.failed)
return isl_bool_false;
return isl_bool_error;
}
/* Add "map" to "list".
*/
static isl_stat add_list_map(__isl_take isl_map *map, void *user)
{
isl_map_list **list = user;
*list = isl_map_list_add(*list, map);
if (!*list)
return isl_stat_error;
return isl_stat_ok;
}
/* Return the maps in "umap" as a list.
*
* First construct a list of the appropriate size and then add all the
* elements.
*/
__isl_give isl_map_list *isl_union_map_get_map_list(
__isl_keep isl_union_map *umap)
{
int n_maps;
isl_ctx *ctx;
isl_map_list *list;
if (!umap)
return NULL;
ctx = isl_union_map_get_ctx(umap);
n_maps = isl_union_map_n_map(umap);
list = isl_map_list_alloc(ctx, n_maps);
if (isl_union_map_foreach_map(umap, &add_list_map, &list) < 0)
list = isl_map_list_free(list);
return list;
}
/* Return the sets in "uset" as a list.
*/
__isl_give isl_set_list *isl_union_set_get_set_list(
__isl_keep isl_union_set *uset)
{
return set_list_from_map_list(
isl_union_map_get_map_list(uset_to_umap(uset)));
}
static isl_stat copy_map(void **entry, void *user)
{
isl_map *map = *entry;
isl_map **map_p = user;
*map_p = isl_map_copy(map);
return isl_stat_error;
}
__isl_give isl_map *isl_map_from_union_map(__isl_take isl_union_map *umap)
{
isl_ctx *ctx;
isl_map *map = NULL;
if (!umap)
return NULL;
ctx = isl_union_map_get_ctx(umap);
if (umap->table.n != 1)
isl_die(ctx, isl_error_invalid,
"union map needs to contain elements in exactly "
"one space", goto error);
isl_hash_table_foreach(ctx, &umap->table, ©_map, &map);
isl_union_map_free(umap);
return map;
error:
isl_union_map_free(umap);
return NULL;
}
__isl_give isl_set *isl_set_from_union_set(__isl_take isl_union_set *uset)
{
return isl_map_from_union_map(uset);
}
/* Extract the map in "umap" that lives in the given space (ignoring
* parameters).
*/
__isl_give isl_map *isl_union_map_extract_map(__isl_keep isl_union_map *umap,
__isl_take isl_space *space)
{
uint32_t hash;
struct isl_hash_table_entry *entry;
space = isl_space_drop_dims(space, isl_dim_param,
0, isl_space_dim(space, isl_dim_param));
space = isl_space_align_params(space, isl_union_map_get_space(umap));
if (!umap || !space)
goto error;
hash = isl_space_get_hash(space);
entry = isl_hash_table_find(umap->dim->ctx, &umap->table, hash,
&has_space, space, 0);
if (!entry)
return isl_map_empty(space);
isl_space_free(space);
return isl_map_copy(entry->data);
error:
isl_space_free(space);
return NULL;
}
__isl_give isl_set *isl_union_set_extract_set(__isl_keep isl_union_set *uset,
__isl_take isl_space *dim)
{
return set_from_map(isl_union_map_extract_map(uset, dim));
}
/* Check if umap contains a map in the given space.
*/
isl_bool isl_union_map_contains(__isl_keep isl_union_map *umap,
__isl_keep isl_space *space)
{
uint32_t hash;
struct isl_hash_table_entry *entry;
if (!umap || !space)
return isl_bool_error;
hash = isl_space_get_hash(space);
entry = isl_hash_table_find(umap->dim->ctx, &umap->table, hash,
&has_space, space, 0);
return !!entry;
}
isl_bool isl_union_set_contains(__isl_keep isl_union_set *uset,
__isl_keep isl_space *space)
{
return isl_union_map_contains(uset, space);
}
isl_stat isl_union_set_foreach_set(__isl_keep isl_union_set *uset,
isl_stat (*fn)(__isl_take isl_set *set, void *user), void *user)
{
return isl_union_map_foreach_map(uset,
(isl_stat(*)(__isl_take isl_map *, void*))fn, user);
}
struct isl_union_set_foreach_point_data {
isl_stat (*fn)(__isl_take isl_point *pnt, void *user);
void *user;
};
static isl_stat foreach_point(__isl_take isl_set *set, void *user)
{
struct isl_union_set_foreach_point_data *data = user;
isl_stat r;
r = isl_set_foreach_point(set, data->fn, data->user);
isl_set_free(set);
return r;
}
isl_stat isl_union_set_foreach_point(__isl_keep isl_union_set *uset,
isl_stat (*fn)(__isl_take isl_point *pnt, void *user), void *user)
{
struct isl_union_set_foreach_point_data data = { fn, user };
return isl_union_set_foreach_set(uset, &foreach_point, &data);
}
/* Data structure that specifies how gen_bin_op should
* construct results from the inputs.
*
* If "subtract" is set, then a map in the first input is copied to the result
* if there is no corresponding map in the second input.
* Otherwise, a map in the first input with no corresponding map
* in the second input is ignored.
* If "filter" is not NULL, then it specifies which maps in the first
* input may have a matching map in the second input.
* In particular, it makes sure that "match_space" can be called
* on the space of the map.
* "match_space" specifies how to transform the space of a map
* in the first input to the space of the corresponding map
* in the second input.
* "fn_map" specifies how the matching maps, one from each input,
* should be combined to form a map in the result.
*/
struct isl_bin_op_control {
int subtract;
isl_bool (*filter)(__isl_keep isl_map *map);
__isl_give isl_space *(*match_space)(__isl_take isl_space *space);
__isl_give isl_map *(*fn_map)(__isl_take isl_map *map1,
__isl_take isl_map *map2);
};
/* Internal data structure for gen_bin_op.
* "control" specifies how the maps in the result should be constructed.
* "umap2" is a pointer to the second argument.
* "res" collects the results.
*/
struct isl_union_map_gen_bin_data {
struct isl_bin_op_control *control;
isl_union_map *umap2;
isl_union_map *res;
};
/* Add a copy of "map" to "res" and return the result.
*/
static __isl_give isl_union_map *bin_add_map(__isl_take isl_union_map *res,
__isl_keep isl_map *map)
{
return isl_union_map_add_map(res, isl_map_copy(map));
}
/* Combine "map1" and "map2", add the result to "res" and return the result.
* Check whether the result is empty before adding it to "res".
*/
static __isl_give isl_union_map *bin_add_pair(__isl_take isl_union_map *res,
__isl_keep isl_map *map1, __isl_keep isl_map *map2,
struct isl_union_map_gen_bin_data *data)
{
isl_bool empty;
isl_map *map;
map = data->control->fn_map(isl_map_copy(map1), isl_map_copy(map2));
empty = isl_map_is_empty(map);
if (empty < 0 || empty) {
isl_map_free(map);
if (empty < 0)
return isl_union_map_free(res);
return res;
}
return isl_union_map_add_map(res, map);
}
/* Dummy match_space function that simply returns the input space.
*/
static __isl_give isl_space *identity(__isl_take isl_space *space)
{
return space;
}
/* Look for the map in data->umap2 that corresponds to "map", if any.
* Return (isl_bool_true, matching map) if there is one,
* (isl_bool_false, NULL) if there is no matching map and
* (isl_bool_error, NULL) on error.
*
* If not NULL, then data->control->filter specifies whether "map"
* can have any matching map. If so,
* data->control->match_space specifies which map in data->umap2
* corresponds to "map".
*/
static __isl_keep isl_maybe_isl_map bin_try_get_match(
struct isl_union_map_gen_bin_data *data, __isl_keep isl_map *map)
{
uint32_t hash;
struct isl_hash_table_entry *entry2;
isl_space *space;
isl_maybe_isl_map res = { isl_bool_error, NULL };
if (data->control->filter) {
res.valid = data->control->filter(map);
if (res.valid < 0 || !res.valid)
return res;
res.valid = isl_bool_error;
}
space = isl_map_get_space(map);
if (data->control->match_space != &identity)
space = data->control->match_space(space);
if (!space)
return res;
hash = isl_space_get_hash(space);
entry2 = isl_hash_table_find(isl_union_map_get_ctx(data->umap2),
&data->umap2->table, hash,
&has_space, space, 0);
isl_space_free(space);
res.valid = entry2 != NULL;
if (entry2)
res.value = entry2->data;
return res;
}
/* isl_hash_table_foreach callback for gen_bin_op.
* Look for the map in data->umap2 that corresponds
* to the map that "entry" points to, apply the binary operation and
* add the result to data->res.
*
* If no corresponding map can be found, then the effect depends
* on data->control->subtract. If it is set, then the current map
* is added directly to the result. Otherwise, it is ignored.
*/
static isl_stat gen_bin_entry(void **entry, void *user)
{
struct isl_union_map_gen_bin_data *data = user;
isl_map *map = *entry;
isl_maybe_isl_map m;
m = bin_try_get_match(data, map);
if (m.valid < 0)
return isl_stat_error;
if (!m.valid && !data->control->subtract)
return isl_stat_ok;
if (!m.valid)
data->res = bin_add_map(data->res, map);
else
data->res = bin_add_pair(data->res, map, m.value, data);
if (!data->res)
return isl_stat_error;
return isl_stat_ok;
}
/* Apply a binary operation to "umap1" and "umap2" based on "control".
* Run over all maps in "umap1" and look for the corresponding map in "umap2"
* in gen_bin_entry.
*/
static __isl_give isl_union_map *gen_bin_op(__isl_take isl_union_map *umap1,
__isl_take isl_union_map *umap2, struct isl_bin_op_control *control)
{
struct isl_union_map_gen_bin_data data = { control, NULL, NULL };
umap1 = isl_union_map_align_params(umap1, isl_union_map_get_space(umap2));
umap2 = isl_union_map_align_params(umap2, isl_union_map_get_space(umap1));
if (!umap1 || !umap2)
goto error;
data.umap2 = umap2;
data.res = isl_union_map_alloc(isl_space_copy(umap1->dim),
umap1->table.n);
if (isl_hash_table_foreach(umap1->dim->ctx, &umap1->table,
&gen_bin_entry, &data) < 0)
goto error;
isl_union_map_free(umap1);
isl_union_map_free(umap2);
return data.res;
error:
isl_union_map_free(umap1);
isl_union_map_free(umap2);
isl_union_map_free(data.res);
return NULL;
}
__isl_give isl_union_map *isl_union_map_subtract(
__isl_take isl_union_map *umap1, __isl_take isl_union_map *umap2)
{
struct isl_bin_op_control control = {
.subtract = 1,
.match_space = &identity,
.fn_map = &isl_map_subtract,
};
return gen_bin_op(umap1, umap2, &control);
}
__isl_give isl_union_set *isl_union_set_subtract(
__isl_take isl_union_set *uset1, __isl_take isl_union_set *uset2)
{
return isl_union_map_subtract(uset1, uset2);
}
struct isl_union_map_gen_bin_set_data {
isl_set *set;
isl_union_map *res;
};
static isl_stat intersect_params_entry(void **entry, void *user)
{
struct isl_union_map_gen_bin_set_data *data = user;
isl_map *map = *entry;
int empty;
map = isl_map_copy(map);
map = isl_map_intersect_params(map, isl_set_copy(data->set));
empty = isl_map_is_empty(map);
if (empty < 0) {
isl_map_free(map);
return isl_stat_error;
}
data->res = isl_union_map_add_map(data->res, map);
return isl_stat_ok;
}
static __isl_give isl_union_map *gen_bin_set_op(__isl_take isl_union_map *umap,
__isl_take isl_set *set, isl_stat (*fn)(void **, void *))
{
struct isl_union_map_gen_bin_set_data data = { NULL, NULL };
umap = isl_union_map_align_params(umap, isl_set_get_space(set));
set = isl_set_align_params(set, isl_union_map_get_space(umap));
if (!umap || !set)
goto error;
data.set = set;
data.res = isl_union_map_alloc(isl_space_copy(umap->dim),
umap->table.n);
if (isl_hash_table_foreach(umap->dim->ctx, &umap->table,
fn, &data) < 0)
goto error;
isl_union_map_free(umap);
isl_set_free(set);
return data.res;
error:
isl_union_map_free(umap);
isl_set_free(set);
isl_union_map_free(data.res);
return NULL;
}
/* Intersect "umap" with the parameter domain "set".
*
* If "set" does not have any constraints, then we can return immediately.
*/
__isl_give isl_union_map *isl_union_map_intersect_params(
__isl_take isl_union_map *umap, __isl_take isl_set *set)
{
int is_universe;
is_universe = isl_set_plain_is_universe(set);
if (is_universe < 0)
goto error;
if (is_universe) {
isl_set_free(set);
return umap;
}
return gen_bin_set_op(umap, set, &intersect_params_entry);
error:
isl_union_map_free(umap);
isl_set_free(set);
return NULL;
}
__isl_give isl_union_set *isl_union_set_intersect_params(
__isl_take isl_union_set *uset, __isl_take isl_set *set)
{
return isl_union_map_intersect_params(uset, set);
}
static __isl_give isl_union_map *union_map_intersect_params(
__isl_take isl_union_map *umap, __isl_take isl_union_set *uset)
{
return isl_union_map_intersect_params(umap,
isl_set_from_union_set(uset));
}
static __isl_give isl_union_map *union_map_gist_params(
__isl_take isl_union_map *umap, __isl_take isl_union_set *uset)
{
return isl_union_map_gist_params(umap, isl_set_from_union_set(uset));
}
struct isl_union_map_match_bin_data {
isl_union_map *umap2;
isl_union_map *res;
__isl_give isl_map *(*fn)(__isl_take isl_map*, __isl_take isl_map*);
};
static isl_stat match_bin_entry(void **entry, void *user)
{
struct isl_union_map_match_bin_data *data = user;
uint32_t hash;
struct isl_hash_table_entry *entry2;
isl_map *map = *entry;
int empty;
hash = isl_space_get_hash(map->dim);
entry2 = isl_hash_table_find(data->umap2->dim->ctx, &data->umap2->table,
hash, &has_space, map->dim, 0);
if (!entry2)
return isl_stat_ok;
map = isl_map_copy(map);
map = data->fn(map, isl_map_copy(entry2->data));
empty = isl_map_is_empty(map);
if (empty < 0) {
isl_map_free(map);
return isl_stat_error;
}
if (empty) {
isl_map_free(map);
return isl_stat_ok;
}
data->res = isl_union_map_add_map(data->res, map);
return isl_stat_ok;
}
static __isl_give isl_union_map *match_bin_op(__isl_take isl_union_map *umap1,
__isl_take isl_union_map *umap2,
__isl_give isl_map *(*fn)(__isl_take isl_map*, __isl_take isl_map*))
{
struct isl_union_map_match_bin_data data = { NULL, NULL, fn };
umap1 = isl_union_map_align_params(umap1, isl_union_map_get_space(umap2));
umap2 = isl_union_map_align_params(umap2, isl_union_map_get_space(umap1));
if (!umap1 || !umap2)
goto error;
data.umap2 = umap2;
data.res = isl_union_map_alloc(isl_space_copy(umap1->dim),
umap1->table.n);
if (isl_hash_table_foreach(umap1->dim->ctx, &umap1->table,
&match_bin_entry, &data) < 0)
goto error;
isl_union_map_free(umap1);
isl_union_map_free(umap2);
return data.res;
error:
isl_union_map_free(umap1);
isl_union_map_free(umap2);
isl_union_map_free(data.res);
return NULL;
}
__isl_give isl_union_map *isl_union_map_intersect(
__isl_take isl_union_map *umap1, __isl_take isl_union_map *umap2)
{
return match_bin_op(umap1, umap2, &isl_map_intersect);
}
/* Compute the intersection of the two union_sets.
* As a special case, if exactly one of the two union_sets
* is a parameter domain, then intersect the parameter domain
* of the other one with this set.
*/
__isl_give isl_union_set *isl_union_set_intersect(
__isl_take isl_union_set *uset1, __isl_take isl_union_set *uset2)
{
int p1, p2;
p1 = isl_union_set_is_params(uset1);
p2 = isl_union_set_is_params(uset2);
if (p1 < 0 || p2 < 0)
goto error;
if (!p1 && p2)
return union_map_intersect_params(uset1, uset2);
if (p1 && !p2)
return union_map_intersect_params(uset2, uset1);
return isl_union_map_intersect(uset1, uset2);
error:
isl_union_set_free(uset1);
isl_union_set_free(uset2);
return NULL;
}
static isl_stat gist_params_entry(void **entry, void *user)
{
struct isl_union_map_gen_bin_set_data *data = user;
isl_map *map = *entry;
int empty;
map = isl_map_copy(map);
map = isl_map_gist_params(map, isl_set_copy(data->set));
empty = isl_map_is_empty(map);
if (empty < 0) {
isl_map_free(map);
return isl_stat_error;
}
data->res = isl_union_map_add_map(data->res, map);
return isl_stat_ok;
}
__isl_give isl_union_map *isl_union_map_gist_params(
__isl_take isl_union_map *umap, __isl_take isl_set *set)
{
return gen_bin_set_op(umap, set, &gist_params_entry);
}
__isl_give isl_union_set *isl_union_set_gist_params(
__isl_take isl_union_set *uset, __isl_take isl_set *set)
{
return isl_union_map_gist_params(uset, set);
}
__isl_give isl_union_map *isl_union_map_gist(__isl_take isl_union_map *umap,
__isl_take isl_union_map *context)
{
return match_bin_op(umap, context, &isl_map_gist);
}
__isl_give isl_union_set *isl_union_set_gist(__isl_take isl_union_set *uset,
__isl_take isl_union_set *context)
{
if (isl_union_set_is_params(context))
return union_map_gist_params(uset, context);
return isl_union_map_gist(uset, context);
}
/* For each map in "umap", remove the constraints in the corresponding map
* of "context".
* Each map in "context" is assumed to consist of a single disjunct and
* to have explicit representations for all local variables.
*/
__isl_give isl_union_map *isl_union_map_plain_gist(
__isl_take isl_union_map *umap, __isl_take isl_union_map *context)
{
return match_bin_op(umap, context, &isl_map_plain_gist);
}
/* For each set in "uset", remove the constraints in the corresponding set
* of "context".
* Each set in "context" is assumed to consist of a single disjunct and
* to have explicit representations for all local variables.
*/
__isl_give isl_union_set *isl_union_set_plain_gist(
__isl_take isl_union_set *uset, __isl_take isl_union_set *context)
{
return isl_union_map_plain_gist(uset, context);
}
static __isl_give isl_map *lex_le_set(__isl_take isl_map *set1,
__isl_take isl_map *set2)
{
return isl_set_lex_le_set(set_from_map(set1), set_from_map(set2));
}
static __isl_give isl_map *lex_lt_set(__isl_take isl_map *set1,
__isl_take isl_map *set2)
{
return isl_set_lex_lt_set(set_from_map(set1), set_from_map(set2));
}
__isl_give isl_union_map *isl_union_set_lex_lt_union_set(
__isl_take isl_union_set *uset1, __isl_take isl_union_set *uset2)
{
return match_bin_op(uset1, uset2, &lex_lt_set);
}
__isl_give isl_union_map *isl_union_set_lex_le_union_set(
__isl_take isl_union_set *uset1, __isl_take isl_union_set *uset2)
{
return match_bin_op(uset1, uset2, &lex_le_set);
}
__isl_give isl_union_map *isl_union_set_lex_gt_union_set(
__isl_take isl_union_set *uset1, __isl_take isl_union_set *uset2)
{
return isl_union_map_reverse(isl_union_set_lex_lt_union_set(uset2, uset1));
}
__isl_give isl_union_map *isl_union_set_lex_ge_union_set(
__isl_take isl_union_set *uset1, __isl_take isl_union_set *uset2)
{
return isl_union_map_reverse(isl_union_set_lex_le_union_set(uset2, uset1));
}
__isl_give isl_union_map *isl_union_map_lex_gt_union_map(
__isl_take isl_union_map *umap1, __isl_take isl_union_map *umap2)
{
return isl_union_map_reverse(isl_union_map_lex_lt_union_map(umap2, umap1));
}
__isl_give isl_union_map *isl_union_map_lex_ge_union_map(
__isl_take isl_union_map *umap1, __isl_take isl_union_map *umap2)
{
return isl_union_map_reverse(isl_union_map_lex_le_union_map(umap2, umap1));
}
/* Intersect the domain of "umap" with "uset".
*/
static __isl_give isl_union_map *union_map_intersect_domain(
__isl_take isl_union_map *umap, __isl_take isl_union_set *uset)
{
struct isl_bin_op_control control = {
.match_space = &isl_space_domain,
.fn_map = &isl_map_intersect_domain,
};
return gen_bin_op(umap, uset, &control);
}
/* Intersect the domain of "umap" with "uset".
* If "uset" is a parameters domain, then intersect the parameter
* domain of "umap" with this set.
*/
__isl_give isl_union_map *isl_union_map_intersect_domain(
__isl_take isl_union_map *umap, __isl_take isl_union_set *uset)
{
if (isl_union_set_is_params(uset))
return union_map_intersect_params(umap, uset);
else
return union_map_intersect_domain(umap, uset);
}
/* Remove the elements of "uset" from the domain of "umap".
*/
__isl_give isl_union_map *isl_union_map_subtract_domain(
__isl_take isl_union_map *umap, __isl_take isl_union_set *dom)
{
struct isl_bin_op_control control = {
.subtract = 1,
.match_space = &isl_space_domain,
.fn_map = &isl_map_subtract_domain,
};
return gen_bin_op(umap, dom, &control);
}
/* Remove the elements of "uset" from the range of "umap".
*/
__isl_give isl_union_map *isl_union_map_subtract_range(
__isl_take isl_union_map *umap, __isl_take isl_union_set *dom)
{
struct isl_bin_op_control control = {
.subtract = 1,
.match_space = &isl_space_range,
.fn_map = &isl_map_subtract_range,
};
return gen_bin_op(umap, dom, &control);
}
/* Compute the gist of "umap" with respect to the domain "uset".
*/
static __isl_give isl_union_map *union_map_gist_domain(
__isl_take isl_union_map *umap, __isl_take isl_union_set *uset)
{
struct isl_bin_op_control control = {
.match_space = &isl_space_domain,
.fn_map = &isl_map_gist_domain,
};
return gen_bin_op(umap, uset, &control);
}
/* Compute the gist of "umap" with respect to the domain "uset".
* If "uset" is a parameters domain, then compute the gist
* with respect to this parameter domain.
*/
__isl_give isl_union_map *isl_union_map_gist_domain(
__isl_take isl_union_map *umap, __isl_take isl_union_set *uset)
{
if (isl_union_set_is_params(uset))
return union_map_gist_params(umap, uset);
else
return union_map_gist_domain(umap, uset);
}
/* Compute the gist of "umap" with respect to the range "uset".
*/
__isl_give isl_union_map *isl_union_map_gist_range(
__isl_take isl_union_map *umap, __isl_take isl_union_set *uset)
{
struct isl_bin_op_control control = {
.match_space = &isl_space_range,
.fn_map = &isl_map_gist_range,
};
return gen_bin_op(umap, uset, &control);
}
__isl_give isl_union_map *isl_union_map_intersect_range(
__isl_take isl_union_map *umap, __isl_take isl_union_set *uset)
{
struct isl_bin_op_control control = {
.match_space = &isl_space_range,
.fn_map = &isl_map_intersect_range,
};
return gen_bin_op(umap, uset, &control);
}
/* Intersect each map in "umap" in a space A -> [B -> C]
* with the corresponding map in "factor" in the space A -> C and
* collect the results.
*/
__isl_give isl_union_map *isl_union_map_intersect_range_factor_range(
__isl_take isl_union_map *umap, __isl_take isl_union_map *factor)
{
struct isl_bin_op_control control = {
.filter = &isl_map_range_is_wrapping,
.match_space = &isl_space_range_factor_range,
.fn_map = &isl_map_intersect_range_factor_range,
};
return gen_bin_op(umap, factor, &control);
}
struct isl_union_map_bin_data {
isl_union_map *umap2;
isl_union_map *res;
isl_map *map;
isl_stat (*fn)(void **entry, void *user);
};
static isl_stat apply_range_entry(void **entry, void *user)
{
struct isl_union_map_bin_data *data = user;
isl_map *map2 = *entry;
isl_bool empty;
if (!isl_space_tuple_is_equal(data->map->dim, isl_dim_out,
map2->dim, isl_dim_in))
return isl_stat_ok;
map2 = isl_map_apply_range(isl_map_copy(data->map), isl_map_copy(map2));
empty = isl_map_is_empty(map2);
if (empty < 0) {
isl_map_free(map2);
return isl_stat_error;
}
if (empty) {
isl_map_free(map2);
return isl_stat_ok;
}
data->res = isl_union_map_add_map(data->res, map2);
return isl_stat_ok;
}
static isl_stat bin_entry(void **entry, void *user)
{
struct isl_union_map_bin_data *data = user;
isl_map *map = *entry;
data->map = map;
if (isl_hash_table_foreach(data->umap2->dim->ctx, &data->umap2->table,
data->fn, data) < 0)
return isl_stat_error;
return isl_stat_ok;
}
static __isl_give isl_union_map *bin_op(__isl_take isl_union_map *umap1,
__isl_take isl_union_map *umap2,
isl_stat (*fn)(void **entry, void *user))
{
struct isl_union_map_bin_data data = { NULL, NULL, NULL, fn };
umap1 = isl_union_map_align_params(umap1, isl_union_map_get_space(umap2));
umap2 = isl_union_map_align_params(umap2, isl_union_map_get_space(umap1));
if (!umap1 || !umap2)
goto error;
data.umap2 = umap2;
data.res = isl_union_map_alloc(isl_space_copy(umap1->dim),
umap1->table.n);
if (isl_hash_table_foreach(umap1->dim->ctx, &umap1->table,
&bin_entry, &data) < 0)
goto error;
isl_union_map_free(umap1);
isl_union_map_free(umap2);
return data.res;
error:
isl_union_map_free(umap1);
isl_union_map_free(umap2);
isl_union_map_free(data.res);
return NULL;
}
__isl_give isl_union_map *isl_union_map_apply_range(
__isl_take isl_union_map *umap1, __isl_take isl_union_map *umap2)
{
return bin_op(umap1, umap2, &apply_range_entry);
}
__isl_give isl_union_map *isl_union_map_apply_domain(
__isl_take isl_union_map *umap1, __isl_take isl_union_map *umap2)
{
umap1 = isl_union_map_reverse(umap1);
umap1 = isl_union_map_apply_range(umap1, umap2);
return isl_union_map_reverse(umap1);
}
__isl_give isl_union_set *isl_union_set_apply(
__isl_take isl_union_set *uset, __isl_take isl_union_map *umap)
{
return isl_union_map_apply_range(uset, umap);
}
static isl_stat map_lex_lt_entry(void **entry, void *user)
{
struct isl_union_map_bin_data *data = user;
isl_map *map2 = *entry;
if (!isl_space_tuple_is_equal(data->map->dim, isl_dim_out,
map2->dim, isl_dim_out))
return isl_stat_ok;
map2 = isl_map_lex_lt_map(isl_map_copy(data->map), isl_map_copy(map2));
data->res = isl_union_map_add_map(data->res, map2);
return isl_stat_ok;
}
__isl_give isl_union_map *isl_union_map_lex_lt_union_map(
__isl_take isl_union_map *umap1, __isl_take isl_union_map *umap2)
{
return bin_op(umap1, umap2, &map_lex_lt_entry);
}
static isl_stat map_lex_le_entry(void **entry, void *user)
{
struct isl_union_map_bin_data *data = user;
isl_map *map2 = *entry;
if (!isl_space_tuple_is_equal(data->map->dim, isl_dim_out,
map2->dim, isl_dim_out))
return isl_stat_ok;
map2 = isl_map_lex_le_map(isl_map_copy(data->map), isl_map_copy(map2));
data->res = isl_union_map_add_map(data->res, map2);
return isl_stat_ok;
}
__isl_give isl_union_map *isl_union_map_lex_le_union_map(
__isl_take isl_union_map *umap1, __isl_take isl_union_map *umap2)
{
return bin_op(umap1, umap2, &map_lex_le_entry);
}
static isl_stat product_entry(void **entry, void *user)
{
struct isl_union_map_bin_data *data = user;
isl_map *map2 = *entry;
map2 = isl_map_product(isl_map_copy(data->map), isl_map_copy(map2));
data->res = isl_union_map_add_map(data->res, map2);
return isl_stat_ok;
}
__isl_give isl_union_map *isl_union_map_product(__isl_take isl_union_map *umap1,
__isl_take isl_union_map *umap2)
{
return bin_op(umap1, umap2, &product_entry);
}
static isl_stat set_product_entry(void **entry, void *user)
{
struct isl_union_map_bin_data *data = user;
isl_set *set2 = *entry;
set2 = isl_set_product(isl_set_copy(data->map), isl_set_copy(set2));
data->res = isl_union_set_add_set(data->res, set2);
return isl_stat_ok;
}
__isl_give isl_union_set *isl_union_set_product(__isl_take isl_union_set *uset1,
__isl_take isl_union_set *uset2)
{
return bin_op(uset1, uset2, &set_product_entry);
}
static isl_stat domain_product_entry(void **entry, void *user)
{
struct isl_union_map_bin_data *data = user;
isl_map *map2 = *entry;
if (!isl_space_tuple_is_equal(data->map->dim, isl_dim_out,
map2->dim, isl_dim_out))
return isl_stat_ok;
map2 = isl_map_domain_product(isl_map_copy(data->map),
isl_map_copy(map2));
data->res = isl_union_map_add_map(data->res, map2);
return isl_stat_ok;
}
/* Given two maps A -> B and C -> D, construct a map [A -> C] -> (B * D)
*/
__isl_give isl_union_map *isl_union_map_domain_product(
__isl_take isl_union_map *umap1, __isl_take isl_union_map *umap2)
{
return bin_op(umap1, umap2, &domain_product_entry);
}
static isl_stat range_product_entry(void **entry, void *user)
{
struct isl_union_map_bin_data *data = user;
isl_map *map2 = *entry;
if (!isl_space_tuple_is_equal(data->map->dim, isl_dim_in,
map2->dim, isl_dim_in))
return isl_stat_ok;
map2 = isl_map_range_product(isl_map_copy(data->map),
isl_map_copy(map2));
data->res = isl_union_map_add_map(data->res, map2);
return isl_stat_ok;
}
__isl_give isl_union_map *isl_union_map_range_product(
__isl_take isl_union_map *umap1, __isl_take isl_union_map *umap2)
{
return bin_op(umap1, umap2, &range_product_entry);
}
/* If data->map A -> B and "map2" C -> D have the same range space,
* then add (A, C) -> (B * D) to data->res.
*/
static isl_stat flat_domain_product_entry(void **entry, void *user)
{
struct isl_union_map_bin_data *data = user;
isl_map *map2 = *entry;
if (!isl_space_tuple_is_equal(data->map->dim, isl_dim_out,
map2->dim, isl_dim_out))
return isl_stat_ok;
map2 = isl_map_flat_domain_product(isl_map_copy(data->map),
isl_map_copy(map2));
data->res = isl_union_map_add_map(data->res, map2);
return isl_stat_ok;
}
/* Given two maps A -> B and C -> D, construct a map (A, C) -> (B * D).
*/
__isl_give isl_union_map *isl_union_map_flat_domain_product(
__isl_take isl_union_map *umap1, __isl_take isl_union_map *umap2)
{
return bin_op(umap1, umap2, &flat_domain_product_entry);
}
static isl_stat flat_range_product_entry(void **entry, void *user)
{
struct isl_union_map_bin_data *data = user;
isl_map *map2 = *entry;
if (!isl_space_tuple_is_equal(data->map->dim, isl_dim_in,
map2->dim, isl_dim_in))
return isl_stat_ok;
map2 = isl_map_flat_range_product(isl_map_copy(data->map),
isl_map_copy(map2));
data->res = isl_union_map_add_map(data->res, map2);
return isl_stat_ok;
}
__isl_give isl_union_map *isl_union_map_flat_range_product(
__isl_take isl_union_map *umap1, __isl_take isl_union_map *umap2)
{
return bin_op(umap1, umap2, &flat_range_product_entry);
}
/* Data structure that specifies how un_op should modify
* the maps in the union map.
*
* If "inplace" is set, then the maps in the input union map
* are modified in place. This means that "fn_map" should not
* change the meaning of the map or that the union map only
* has a single reference.
* If "total" is set, then all maps need to be modified and
* the results need to live in the same space.
* Otherwise, a new union map is constructed to store the results.
* If "filter" is not NULL, then only the input maps that satisfy "filter"
* are taken into account. "filter_user" is passed as the second argument
* to "filter". No filter can be set if "inplace" or
* "total" is set.
* "fn_map" specifies how the maps (selected by "filter")
* should be transformed.
*/
struct isl_un_op_control {
int inplace;
int total;
isl_bool (*filter)(__isl_keep isl_map *map, void *user);
void *filter_user;
__isl_give isl_map *(*fn_map)(__isl_take isl_map *map);
};
/* Data structure for wrapping the data for un_op_filter_drop_user.
* "filter" is the function that is being wrapped.
*/
struct isl_un_op_drop_user_data {
isl_bool (*filter)(__isl_keep isl_map *map);
};
/* Wrapper for isl_un_op_control filters that do not require
* a second argument.
* Simply call data->filter without the second argument.
*/
static isl_bool un_op_filter_drop_user(__isl_keep isl_map *map, void *user)
{
struct isl_un_op_drop_user_data *data = user;
return data->filter(map);
}
/* Internal data structure for "un_op".
* "control" specifies how the maps in the union map should be modified.
* "res" collects the results.
*/
struct isl_union_map_un_data {
struct isl_un_op_control *control;
isl_union_map *res;
};
/* isl_hash_table_foreach callback for un_op.
* Handle the map that "entry" points to.
*
* If control->filter is set, then check if this map satisfies the filter.
* If so (or if control->filter is not set), modify the map
* by calling control->fn_map and either add the result to data->res or
* replace the original entry by the result (if control->inplace is set).
*/
static isl_stat un_entry(void **entry, void *user)
{
struct isl_union_map_un_data *data = user;
isl_map *map = *entry;
if (data->control->filter) {
isl_bool ok;
ok = data->control->filter(map, data->control->filter_user);
if (ok < 0)
return isl_stat_error;
if (!ok)
return isl_stat_ok;
}
map = data->control->fn_map(isl_map_copy(map));
if (!map)
return isl_stat_error;
if (data->control->inplace) {
isl_map_free(*entry);
*entry = map;
} else {
data->res = isl_union_map_add_map(data->res, map);
if (!data->res)
return isl_stat_error;
}
return isl_stat_ok;
}
/* Modify the maps in "umap" based on "control".
* If control->inplace is set, then modify the maps in "umap" in-place.
* Otherwise, create a new union map to hold the results.
* If control->total is set, then perform an inplace computation
* if "umap" is only referenced once. Otherwise, create a new union map
* to store the results.
*/
static __isl_give isl_union_map *un_op(__isl_take isl_union_map *umap,
struct isl_un_op_control *control)
{
struct isl_union_map_un_data data = { control };
if (!umap)
return NULL;
if ((control->inplace || control->total) && control->filter)
isl_die(isl_union_map_get_ctx(umap), isl_error_invalid,
"inplace/total modification cannot be filtered",
return isl_union_map_free(umap));
if (control->total && umap->ref == 1)
control->inplace = 1;
if (control->inplace) {
data.res = umap;
} else {
isl_space *space;
space = isl_union_map_get_space(umap);
data.res = isl_union_map_alloc(space, umap->table.n);
}
if (isl_hash_table_foreach(isl_union_map_get_ctx(umap),
&umap->table, &un_entry, &data) < 0)
data.res = isl_union_map_free(data.res);
if (control->inplace)
return data.res;
isl_union_map_free(umap);
return data.res;
}
__isl_give isl_union_map *isl_union_map_from_range(
__isl_take isl_union_set *uset)
{
struct isl_un_op_control control = {
.fn_map = &isl_map_from_range,
};
return un_op(uset, &control);
}
__isl_give isl_union_map *isl_union_map_from_domain(
__isl_take isl_union_set *uset)
{
return isl_union_map_reverse(isl_union_map_from_range(uset));
}
__isl_give isl_union_map *isl_union_map_from_domain_and_range(
__isl_take isl_union_set *domain, __isl_take isl_union_set *range)
{
return isl_union_map_apply_range(isl_union_map_from_domain(domain),
isl_union_map_from_range(range));
}
/* Modify the maps in "umap" by applying "fn" on them.
* "fn" should apply to all maps in "umap" and should not modify the space.
*/
static __isl_give isl_union_map *total(__isl_take isl_union_map *umap,
__isl_give isl_map *(*fn)(__isl_take isl_map *))
{
struct isl_un_op_control control = {
.total = 1,
.fn_map = fn,
};
return un_op(umap, &control);
}
/* Compute the affine hull of "map" and return the result as an isl_map.
*/
static __isl_give isl_map *isl_map_affine_hull_map(__isl_take isl_map *map)
{
return isl_map_from_basic_map(isl_map_affine_hull(map));
}
__isl_give isl_union_map *isl_union_map_affine_hull(
__isl_take isl_union_map *umap)
{
return total(umap, &isl_map_affine_hull_map);
}
__isl_give isl_union_set *isl_union_set_affine_hull(
__isl_take isl_union_set *uset)
{
return isl_union_map_affine_hull(uset);
}
/* Wrapper around isl_set_combined_lineality_space
* that returns the combined lineality space in the form of an isl_set
* instead of an isl_basic_set.
*/
static __isl_give isl_set *combined_lineality_space(__isl_take isl_set *set)
{
return isl_set_from_basic_set(isl_set_combined_lineality_space(set));
}
/* For each set in "uset", compute the (linear) hull
* of the lineality spaces of its basic sets and
* collect and return the results.
*/
__isl_give isl_union_set *isl_union_set_combined_lineality_space(
__isl_take isl_union_set *uset)
{
struct isl_un_op_control control = {
.fn_map = &combined_lineality_space,
};
return un_op(uset, &control);
}
/* Compute the polyhedral hull of "map" and return the result as an isl_map.
*/
static __isl_give isl_map *isl_map_polyhedral_hull_map(__isl_take isl_map *map)
{
return isl_map_from_basic_map(isl_map_polyhedral_hull(map));
}
__isl_give isl_union_map *isl_union_map_polyhedral_hull(
__isl_take isl_union_map *umap)
{
return total(umap, &isl_map_polyhedral_hull_map);
}
__isl_give isl_union_set *isl_union_set_polyhedral_hull(
__isl_take isl_union_set *uset)
{
return isl_union_map_polyhedral_hull(uset);
}
/* Compute a superset of the convex hull of "map" that is described
* by only translates of the constraints in the constituents of "map" and
* return the result as an isl_map.
*/
static __isl_give isl_map *isl_map_simple_hull_map(__isl_take isl_map *map)
{
return isl_map_from_basic_map(isl_map_simple_hull(map));
}
__isl_give isl_union_map *isl_union_map_simple_hull(
__isl_take isl_union_map *umap)
{
return total(umap, &isl_map_simple_hull_map);
}
__isl_give isl_union_set *isl_union_set_simple_hull(
__isl_take isl_union_set *uset)
{
return isl_union_map_simple_hull(uset);
}
static __isl_give isl_union_map *inplace(__isl_take isl_union_map *umap,
__isl_give isl_map *(*fn)(__isl_take isl_map *))
{
struct isl_un_op_control control = {
.inplace = 1,
.fn_map = fn,
};
return un_op(umap, &control);
}
/* Remove redundant constraints in each of the basic maps of "umap".
* Since removing redundant constraints does not change the meaning
* or the space, the operation can be performed in-place.
*/
__isl_give isl_union_map *isl_union_map_remove_redundancies(
__isl_take isl_union_map *umap)
{
return inplace(umap, &isl_map_remove_redundancies);
}
/* Remove redundant constraints in each of the basic sets of "uset".
*/
__isl_give isl_union_set *isl_union_set_remove_redundancies(
__isl_take isl_union_set *uset)
{
return isl_union_map_remove_redundancies(uset);
}
__isl_give isl_union_map *isl_union_map_coalesce(
__isl_take isl_union_map *umap)
{
return inplace(umap, &isl_map_coalesce);
}
__isl_give isl_union_set *isl_union_set_coalesce(
__isl_take isl_union_set *uset)
{
return isl_union_map_coalesce(uset);
}
__isl_give isl_union_map *isl_union_map_detect_equalities(
__isl_take isl_union_map *umap)
{
return inplace(umap, &isl_map_detect_equalities);
}
__isl_give isl_union_set *isl_union_set_detect_equalities(
__isl_take isl_union_set *uset)
{
return isl_union_map_detect_equalities(uset);
}
__isl_give isl_union_map *isl_union_map_compute_divs(
__isl_take isl_union_map *umap)
{
return inplace(umap, &isl_map_compute_divs);
}
__isl_give isl_union_set *isl_union_set_compute_divs(
__isl_take isl_union_set *uset)
{
return isl_union_map_compute_divs(uset);
}
__isl_give isl_union_map *isl_union_map_lexmin(
__isl_take isl_union_map *umap)
{
return total(umap, &isl_map_lexmin);
}
__isl_give isl_union_set *isl_union_set_lexmin(
__isl_take isl_union_set *uset)
{
return isl_union_map_lexmin(uset);
}
__isl_give isl_union_map *isl_union_map_lexmax(
__isl_take isl_union_map *umap)
{
return total(umap, &isl_map_lexmax);
}
__isl_give isl_union_set *isl_union_set_lexmax(
__isl_take isl_union_set *uset)
{
return isl_union_map_lexmax(uset);
}
/* Return the universe in the space of "map".
*/
static __isl_give isl_map *universe(__isl_take isl_map *map)
{
isl_space *space;
space = isl_map_get_space(map);
isl_map_free(map);
return isl_map_universe(space);
}
__isl_give isl_union_map *isl_union_map_universe(__isl_take isl_union_map *umap)
{
struct isl_un_op_control control = {
.fn_map = &universe,
};
return un_op(umap, &control);
}
__isl_give isl_union_set *isl_union_set_universe(__isl_take isl_union_set *uset)
{
return isl_union_map_universe(uset);
}
__isl_give isl_union_map *isl_union_map_reverse(__isl_take isl_union_map *umap)
{
struct isl_un_op_control control = {
.fn_map = &isl_map_reverse,
};
return un_op(umap, &control);
}
/* Compute the parameter domain of the given union map.
*/
__isl_give isl_set *isl_union_map_params(__isl_take isl_union_map *umap)
{
struct isl_un_op_control control = {
.fn_map = &isl_map_params,
};
int empty;
empty = isl_union_map_is_empty(umap);
if (empty < 0)
goto error;
if (empty) {
isl_space *space;
space = isl_union_map_get_space(umap);
isl_union_map_free(umap);
return isl_set_empty(space);
}
return isl_set_from_union_set(un_op(umap, &control));
error:
isl_union_map_free(umap);
return NULL;
}
/* Compute the parameter domain of the given union set.
*/
__isl_give isl_set *isl_union_set_params(__isl_take isl_union_set *uset)
{
return isl_union_map_params(uset);
}
__isl_give isl_union_set *isl_union_map_domain(__isl_take isl_union_map *umap)
{
struct isl_un_op_control control = {
.fn_map = &isl_map_domain,
};
return un_op(umap, &control);
}
__isl_give isl_union_set *isl_union_map_range(__isl_take isl_union_map *umap)
{
struct isl_un_op_control control = {
.fn_map = &isl_map_range,
};
return un_op(umap, &control);
}
__isl_give isl_union_map *isl_union_map_domain_map(
__isl_take isl_union_map *umap)
{
struct isl_un_op_control control = {
.fn_map = &isl_map_domain_map,
};
return un_op(umap, &control);
}
/* Construct an isl_pw_multi_aff that maps "map" to its domain and
* add the result to "res".
*/
static isl_stat domain_map_upma(__isl_take isl_map *map, void *user)
{
isl_union_pw_multi_aff **res = user;
isl_multi_aff *ma;
isl_pw_multi_aff *pma;
ma = isl_multi_aff_domain_map(isl_map_get_space(map));
pma = isl_pw_multi_aff_alloc(isl_map_wrap(map), ma);
*res = isl_union_pw_multi_aff_add_pw_multi_aff(*res, pma);
return *res ? isl_stat_ok : isl_stat_error;
}
/* Return an isl_union_pw_multi_aff that maps a wrapped copy of "umap"
* to its domain.
*/
__isl_give isl_union_pw_multi_aff *isl_union_map_domain_map_union_pw_multi_aff(
__isl_take isl_union_map *umap)
{
isl_union_pw_multi_aff *res;
res = isl_union_pw_multi_aff_empty(isl_union_map_get_space(umap));
if (isl_union_map_foreach_map(umap, &domain_map_upma, &res) < 0)
res = isl_union_pw_multi_aff_free(res);
isl_union_map_free(umap);
return res;
}
__isl_give isl_union_map *isl_union_map_range_map(
__isl_take isl_union_map *umap)
{
struct isl_un_op_control control = {
.fn_map = &isl_map_range_map,
};
return un_op(umap, &control);
}
/* Given a collection of wrapped maps of the form A[B -> C],
* return the collection of maps A[B -> C] -> B.
*/
__isl_give isl_union_map *isl_union_set_wrapped_domain_map(
__isl_take isl_union_set *uset)
{
struct isl_un_op_drop_user_data data = { &isl_set_is_wrapping };
struct isl_un_op_control control = {
.filter = &un_op_filter_drop_user,
.filter_user = &data,
.fn_map = &isl_set_wrapped_domain_map,
};
return un_op(uset, &control);
}
/* Does "map" relate elements from the same space?
*/
static isl_bool equal_tuples(__isl_keep isl_map *map, void *user)
{
return isl_space_tuple_is_equal(map->dim, isl_dim_in,
map->dim, isl_dim_out);
}
__isl_give isl_union_set *isl_union_map_deltas(__isl_take isl_union_map *umap)
{
struct isl_un_op_control control = {
.filter = &equal_tuples,
.fn_map = &isl_map_deltas,
};
return un_op(umap, &control);
}
__isl_give isl_union_map *isl_union_map_deltas_map(
__isl_take isl_union_map *umap)
{
struct isl_un_op_control control = {
.filter = &equal_tuples,
.fn_map = &isl_map_deltas_map,
};
return un_op(umap, &control);
}
__isl_give isl_union_map *isl_union_set_identity(__isl_take isl_union_set *uset)
{
struct isl_un_op_control control = {
.fn_map = &isl_set_identity,
};
return un_op(uset, &control);
}
/* Construct an identity isl_pw_multi_aff on "set" and add it to *res.
*/
static isl_stat identity_upma(__isl_take isl_set *set, void *user)
{
isl_union_pw_multi_aff **res = user;
isl_space *space;
isl_pw_multi_aff *pma;
space = isl_space_map_from_set(isl_set_get_space(set));
pma = isl_pw_multi_aff_identity(space);
pma = isl_pw_multi_aff_intersect_domain(pma, set);
*res = isl_union_pw_multi_aff_add_pw_multi_aff(*res, pma);
return *res ? isl_stat_ok : isl_stat_error;
}
/* Return an identity function on "uset" in the form
* of an isl_union_pw_multi_aff.
*/
__isl_give isl_union_pw_multi_aff *isl_union_set_identity_union_pw_multi_aff(
__isl_take isl_union_set *uset)
{
isl_union_pw_multi_aff *res;
res = isl_union_pw_multi_aff_empty(isl_union_set_get_space(uset));
if (isl_union_set_foreach_set(uset, &identity_upma, &res) < 0)
res = isl_union_pw_multi_aff_free(res);
isl_union_set_free(uset);
return res;
}
/* For each map in "umap" of the form [A -> B] -> C,
* construct the map A -> C and collect the results.
*/
__isl_give isl_union_map *isl_union_map_domain_factor_domain(
__isl_take isl_union_map *umap)
{
struct isl_un_op_drop_user_data data = { &isl_map_domain_is_wrapping };
struct isl_un_op_control control = {
.filter = &un_op_filter_drop_user,
.filter_user = &data,
.fn_map = &isl_map_domain_factor_domain,
};
return un_op(umap, &control);
}
/* For each map in "umap" of the form [A -> B] -> C,
* construct the map B -> C and collect the results.
*/
__isl_give isl_union_map *isl_union_map_domain_factor_range(
__isl_take isl_union_map *umap)
{
struct isl_un_op_drop_user_data data = { &isl_map_domain_is_wrapping };
struct isl_un_op_control control = {
.filter = &un_op_filter_drop_user,
.filter_user = &data,
.fn_map = &isl_map_domain_factor_range,
};
return un_op(umap, &control);
}
/* For each map in "umap" of the form A -> [B -> C],
* construct the map A -> B and collect the results.
*/
__isl_give isl_union_map *isl_union_map_range_factor_domain(
__isl_take isl_union_map *umap)
{
struct isl_un_op_drop_user_data data = { &isl_map_range_is_wrapping };
struct isl_un_op_control control = {
.filter = &un_op_filter_drop_user,
.filter_user = &data,
.fn_map = &isl_map_range_factor_domain,
};
return un_op(umap, &control);
}
/* For each map in "umap" of the form A -> [B -> C],
* construct the map A -> C and collect the results.
*/
__isl_give isl_union_map *isl_union_map_range_factor_range(
__isl_take isl_union_map *umap)
{
struct isl_un_op_drop_user_data data = { &isl_map_range_is_wrapping };
struct isl_un_op_control control = {
.filter = &un_op_filter_drop_user,
.filter_user = &data,
.fn_map = &isl_map_range_factor_range,
};
return un_op(umap, &control);
}
/* For each map in "umap" of the form [A -> B] -> [C -> D],
* construct the map A -> C and collect the results.
*/
__isl_give isl_union_map *isl_union_map_factor_domain(
__isl_take isl_union_map *umap)
{
struct isl_un_op_drop_user_data data = { &isl_map_is_product };
struct isl_un_op_control control = {
.filter = &un_op_filter_drop_user,
.filter_user = &data,
.fn_map = &isl_map_factor_domain,
};
return un_op(umap, &control);
}
/* For each map in "umap" of the form [A -> B] -> [C -> D],
* construct the map B -> D and collect the results.
*/
__isl_give isl_union_map *isl_union_map_factor_range(
__isl_take isl_union_map *umap)
{
struct isl_un_op_drop_user_data data = { &isl_map_is_product };
struct isl_un_op_control control = {
.filter = &un_op_filter_drop_user,
.filter_user = &data,
.fn_map = &isl_map_factor_range,
};
return un_op(umap, &control);
}
__isl_give isl_union_map *isl_union_set_unwrap(__isl_take isl_union_set *uset)
{
struct isl_un_op_drop_user_data data = { &isl_set_is_wrapping };
struct isl_un_op_control control = {
.filter = &un_op_filter_drop_user,
.filter_user = &data,
.fn_map = &isl_set_unwrap,
};
return un_op(uset, &control);
}
__isl_give isl_union_set *isl_union_map_wrap(__isl_take isl_union_map *umap)
{
struct isl_un_op_control control = {
.fn_map = &isl_map_wrap,
};
return un_op(umap, &control);
}
struct isl_union_map_is_subset_data {
isl_union_map *umap2;
isl_bool is_subset;
};
static isl_stat is_subset_entry(void **entry, void *user)
{
struct isl_union_map_is_subset_data *data = user;
uint32_t hash;
struct isl_hash_table_entry *entry2;
isl_map *map = *entry;
hash = isl_space_get_hash(map->dim);
entry2 = isl_hash_table_find(data->umap2->dim->ctx, &data->umap2->table,
hash, &has_space, map->dim, 0);
if (!entry2) {
int empty = isl_map_is_empty(map);
if (empty < 0)
return isl_stat_error;
if (empty)
return isl_stat_ok;
data->is_subset = 0;
return isl_stat_error;
}
data->is_subset = isl_map_is_subset(map, entry2->data);
if (data->is_subset < 0 || !data->is_subset)
return isl_stat_error;
return isl_stat_ok;
}
isl_bool isl_union_map_is_subset(__isl_keep isl_union_map *umap1,
__isl_keep isl_union_map *umap2)
{
struct isl_union_map_is_subset_data data = { NULL, isl_bool_true };
umap1 = isl_union_map_copy(umap1);
umap2 = isl_union_map_copy(umap2);
umap1 = isl_union_map_align_params(umap1, isl_union_map_get_space(umap2));
umap2 = isl_union_map_align_params(umap2, isl_union_map_get_space(umap1));
if (!umap1 || !umap2)
goto error;
data.umap2 = umap2;
if (isl_hash_table_foreach(umap1->dim->ctx, &umap1->table,
&is_subset_entry, &data) < 0 &&
data.is_subset)
goto error;
isl_union_map_free(umap1);
isl_union_map_free(umap2);
return data.is_subset;
error:
isl_union_map_free(umap1);
isl_union_map_free(umap2);
return isl_bool_error;
}
isl_bool isl_union_set_is_subset(__isl_keep isl_union_set *uset1,
__isl_keep isl_union_set *uset2)
{
return isl_union_map_is_subset(uset1, uset2);
}
isl_bool isl_union_map_is_equal(__isl_keep isl_union_map *umap1,
__isl_keep isl_union_map *umap2)
{
isl_bool is_subset;
if (!umap1 || !umap2)
return isl_bool_error;
is_subset = isl_union_map_is_subset(umap1, umap2);
if (is_subset != isl_bool_true)
return is_subset;
is_subset = isl_union_map_is_subset(umap2, umap1);
return is_subset;
}
isl_bool isl_union_set_is_equal(__isl_keep isl_union_set *uset1,
__isl_keep isl_union_set *uset2)
{
return isl_union_map_is_equal(uset1, uset2);
}
isl_bool isl_union_map_is_strict_subset(__isl_keep isl_union_map *umap1,
__isl_keep isl_union_map *umap2)
{
isl_bool is_subset;
if (!umap1 || !umap2)
return isl_bool_error;
is_subset = isl_union_map_is_subset(umap1, umap2);
if (is_subset != isl_bool_true)
return is_subset;
is_subset = isl_union_map_is_subset(umap2, umap1);
if (is_subset == isl_bool_error)
return is_subset;
return !is_subset;
}
isl_bool isl_union_set_is_strict_subset(__isl_keep isl_union_set *uset1,
__isl_keep isl_union_set *uset2)
{
return isl_union_map_is_strict_subset(uset1, uset2);
}
/* Internal data structure for isl_union_map_is_disjoint.
* umap2 is the union map with which we are comparing.
* is_disjoint is initialized to 1 and is set to 0 as soon
* as the union maps turn out not to be disjoint.
*/
struct isl_union_map_is_disjoint_data {
isl_union_map *umap2;
isl_bool is_disjoint;
};
/* Check if "map" is disjoint from data->umap2 and abort
* the search if it is not.
*/
static isl_stat is_disjoint_entry(void **entry, void *user)
{
struct isl_union_map_is_disjoint_data *data = user;
uint32_t hash;
struct isl_hash_table_entry *entry2;
isl_map *map = *entry;
hash = isl_space_get_hash(map->dim);
entry2 = isl_hash_table_find(data->umap2->dim->ctx, &data->umap2->table,
hash, &has_space, map->dim, 0);
if (!entry2)
return isl_stat_ok;
data->is_disjoint = isl_map_is_disjoint(map, entry2->data);
if (data->is_disjoint < 0 || !data->is_disjoint)
return isl_stat_error;
return isl_stat_ok;
}
/* Are "umap1" and "umap2" disjoint?
*/
isl_bool isl_union_map_is_disjoint(__isl_keep isl_union_map *umap1,
__isl_keep isl_union_map *umap2)
{
struct isl_union_map_is_disjoint_data data = { NULL, isl_bool_true };
umap1 = isl_union_map_copy(umap1);
umap2 = isl_union_map_copy(umap2);
umap1 = isl_union_map_align_params(umap1,
isl_union_map_get_space(umap2));
umap2 = isl_union_map_align_params(umap2,
isl_union_map_get_space(umap1));
if (!umap1 || !umap2)
goto error;
data.umap2 = umap2;
if (isl_hash_table_foreach(umap1->dim->ctx, &umap1->table,
&is_disjoint_entry, &data) < 0 &&
data.is_disjoint)
goto error;
isl_union_map_free(umap1);
isl_union_map_free(umap2);
return data.is_disjoint;
error:
isl_union_map_free(umap1);
isl_union_map_free(umap2);
return isl_bool_error;
}
/* Are "uset1" and "uset2" disjoint?
*/
isl_bool isl_union_set_is_disjoint(__isl_keep isl_union_set *uset1,
__isl_keep isl_union_set *uset2)
{
return isl_union_map_is_disjoint(uset1, uset2);
}
static isl_stat sample_entry(void **entry, void *user)
{
isl_basic_map **sample = (isl_basic_map **)user;
isl_map *map = *entry;
*sample = isl_map_sample(isl_map_copy(map));
if (!*sample)
return isl_stat_error;
if (!isl_basic_map_plain_is_empty(*sample))
return isl_stat_error;
return isl_stat_ok;
}
__isl_give isl_basic_map *isl_union_map_sample(__isl_take isl_union_map *umap)
{
isl_basic_map *sample = NULL;
if (!umap)
return NULL;
if (isl_hash_table_foreach(umap->dim->ctx, &umap->table,
&sample_entry, &sample) < 0 &&
!sample)
goto error;
if (!sample)
sample = isl_basic_map_empty(isl_union_map_get_space(umap));
isl_union_map_free(umap);
return sample;
error:
isl_union_map_free(umap);
return NULL;
}
__isl_give isl_basic_set *isl_union_set_sample(__isl_take isl_union_set *uset)
{
return bset_from_bmap(isl_union_map_sample(uset));
}
/* Return an element in "uset" in the form of an isl_point.
* Return a void isl_point if "uset" is empty.
*/
__isl_give isl_point *isl_union_set_sample_point(__isl_take isl_union_set *uset)
{
return isl_basic_set_sample_point(isl_union_set_sample(uset));
}
struct isl_forall_data {
isl_bool res;
isl_bool (*fn)(__isl_keep isl_map *map);
};
static isl_stat forall_entry(void **entry, void *user)
{
struct isl_forall_data *data = user;
isl_map *map = *entry;
data->res = data->fn(map);
if (data->res < 0)
return isl_stat_error;
if (!data->res)
return isl_stat_error;
return isl_stat_ok;
}
static isl_bool union_map_forall(__isl_keep isl_union_map *umap,
isl_bool (*fn)(__isl_keep isl_map *map))
{
struct isl_forall_data data = { isl_bool_true, fn };
if (!umap)
return isl_bool_error;
if (isl_hash_table_foreach(umap->dim->ctx, &umap->table,
&forall_entry, &data) < 0 && data.res)
return isl_bool_error;
return data.res;
}
struct isl_forall_user_data {
isl_bool res;
isl_bool (*fn)(__isl_keep isl_map *map, void *user);
void *user;
};
static isl_stat forall_user_entry(void **entry, void *user)
{
struct isl_forall_user_data *data = user;
isl_map *map = *entry;
data->res = data->fn(map, data->user);
if (data->res < 0)
return isl_stat_error;
if (!data->res)
return isl_stat_error;
return isl_stat_ok;
}
/* Check if fn(map, user) returns true for all maps "map" in umap.
*/
static isl_bool union_map_forall_user(__isl_keep isl_union_map *umap,
isl_bool (*fn)(__isl_keep isl_map *map, void *user), void *user)
{
struct isl_forall_user_data data = { isl_bool_true, fn, user };
if (!umap)
return isl_bool_error;
if (isl_hash_table_foreach(umap->dim->ctx, &umap->table,
&forall_user_entry, &data) < 0 && data.res)
return isl_bool_error;
return data.res;
}
/* Is "umap" obviously empty?
*/
isl_bool isl_union_map_plain_is_empty(__isl_keep isl_union_map *umap)
{
if (!umap)
return isl_bool_error;
return isl_union_map_n_map(umap) == 0;
}
isl_bool isl_union_map_is_empty(__isl_keep isl_union_map *umap)
{
return union_map_forall(umap, &isl_map_is_empty);
}
isl_bool isl_union_set_is_empty(__isl_keep isl_union_set *uset)
{
return isl_union_map_is_empty(uset);
}
static isl_bool is_subset_of_identity(__isl_keep isl_map *map)
{
isl_bool is_subset;
isl_space *dim;
isl_map *id;
if (!map)
return isl_bool_error;
if (!isl_space_tuple_is_equal(map->dim, isl_dim_in,
map->dim, isl_dim_out))
return isl_bool_false;
dim = isl_map_get_space(map);
id = isl_map_identity(dim);
is_subset = isl_map_is_subset(map, id);
isl_map_free(id);
return is_subset;
}
/* Given an isl_union_map that consists of a single map, check
* if it is single-valued.
*/
static isl_bool single_map_is_single_valued(__isl_keep isl_union_map *umap)
{
isl_map *map;
isl_bool sv;
umap = isl_union_map_copy(umap);
map = isl_map_from_union_map(umap);
sv = isl_map_is_single_valued(map);
isl_map_free(map);
return sv;
}
/* Internal data structure for single_valued_on_domain.
*
* "umap" is the union map to be tested.
* "sv" is set to 1 as long as "umap" may still be single-valued.
*/
struct isl_union_map_is_sv_data {
isl_union_map *umap;
isl_bool sv;
};
/* Check if the data->umap is single-valued on "set".
*
* If data->umap consists of a single map on "set", then test it
* as an isl_map.
*
* Otherwise, compute
*
* M \circ M^-1
*
* check if the result is a subset of the identity mapping and
* store the result in data->sv.
*
* Terminate as soon as data->umap has been determined not to
* be single-valued.
*/
static isl_stat single_valued_on_domain(__isl_take isl_set *set, void *user)
{
struct isl_union_map_is_sv_data *data = user;
isl_union_map *umap, *test;
umap = isl_union_map_copy(data->umap);
umap = isl_union_map_intersect_domain(umap,
isl_union_set_from_set(set));
if (isl_union_map_n_map(umap) == 1) {
data->sv = single_map_is_single_valued(umap);
isl_union_map_free(umap);
} else {
test = isl_union_map_reverse(isl_union_map_copy(umap));
test = isl_union_map_apply_range(test, umap);
data->sv = union_map_forall(test, &is_subset_of_identity);
isl_union_map_free(test);
}
if (data->sv < 0 || !data->sv)
return isl_stat_error;
return isl_stat_ok;
}
/* Check if the given map is single-valued.
*
* If the union map consists of a single map, then test it as an isl_map.
* Otherwise, check if the union map is single-valued on each of its
* domain spaces.
*/
isl_bool isl_union_map_is_single_valued(__isl_keep isl_union_map *umap)
{
isl_union_map *universe;
isl_union_set *domain;
struct isl_union_map_is_sv_data data;
if (isl_union_map_n_map(umap) == 1)
return single_map_is_single_valued(umap);
universe = isl_union_map_universe(isl_union_map_copy(umap));
domain = isl_union_map_domain(universe);
data.sv = isl_bool_true;
data.umap = umap;
if (isl_union_set_foreach_set(domain,
&single_valued_on_domain, &data) < 0 && data.sv)
data.sv = isl_bool_error;
isl_union_set_free(domain);
return data.sv;
}
isl_bool isl_union_map_is_injective(__isl_keep isl_union_map *umap)
{
isl_bool in;
umap = isl_union_map_copy(umap);
umap = isl_union_map_reverse(umap);
in = isl_union_map_is_single_valued(umap);
isl_union_map_free(umap);
return in;
}
/* Is "map" obviously not an identity relation because
* it maps elements from one space to another space?
* Update *non_identity accordingly.
*
* In particular, if the domain and range spaces are the same,
* then the map is not considered to obviously not be an identity relation.
* Otherwise, the map is considered to obviously not be an identity relation
* if it is is non-empty.
*
* If "map" is determined to obviously not be an identity relation,
* then the search is aborted.
*/
static isl_stat map_plain_is_not_identity(__isl_take isl_map *map, void *user)
{
isl_bool *non_identity = user;
isl_bool equal;
isl_space *space;
space = isl_map_get_space(map);
equal = isl_space_tuple_is_equal(space, isl_dim_in, space, isl_dim_out);
if (equal >= 0 && !equal)
*non_identity = isl_bool_not(isl_map_is_empty(map));
else
*non_identity = isl_bool_not(equal);
isl_space_free(space);
isl_map_free(map);
if (*non_identity < 0 || *non_identity)
return isl_stat_error;
return isl_stat_ok;
}
/* Is "umap" obviously not an identity relation because
* it maps elements from one space to another space?
*
* As soon as a map has been found that maps elements to a different space,
* non_identity is changed and the search is aborted.
*/
static isl_bool isl_union_map_plain_is_not_identity(
__isl_keep isl_union_map *umap)
{
isl_bool non_identity;
non_identity = isl_bool_false;
if (isl_union_map_foreach_map(umap, &map_plain_is_not_identity,
&non_identity) < 0 &&
non_identity == isl_bool_false)
return isl_bool_error;
return non_identity;
}
/* Does "map" only map elements to themselves?
* Update *identity accordingly.
*
* If "map" is determined not to be an identity relation,
* then the search is aborted.
*/
static isl_stat map_is_identity(__isl_take isl_map *map, void *user)
{
isl_bool *identity = user;
*identity = isl_map_is_identity(map);
isl_map_free(map);
if (*identity < 0 || !*identity)
return isl_stat_error;
return isl_stat_ok;
}
/* Does "umap" only map elements to themselves?
*
* First check if there are any maps that map elements to different spaces.
* If not, then check that all the maps (between identical spaces)
* are identity relations.
*/
isl_bool isl_union_map_is_identity(__isl_keep isl_union_map *umap)
{
isl_bool non_identity;
isl_bool identity;
non_identity = isl_union_map_plain_is_not_identity(umap);
if (non_identity < 0 || non_identity)
return isl_bool_not(non_identity);
identity = isl_bool_true;
if (isl_union_map_foreach_map(umap, &map_is_identity, &identity) < 0 &&
identity == isl_bool_true)
return isl_bool_error;
return identity;
}
/* Represents a map that has a fixed value (v) for one of its
* range dimensions.
* The map in this structure is not reference counted, so it
* is only valid while the isl_union_map from which it was
* obtained is still alive.
*/
struct isl_fixed_map {
isl_int v;
isl_map *map;
};
static struct isl_fixed_map *alloc_isl_fixed_map_array(isl_ctx *ctx,
int n)
{
int i;
struct isl_fixed_map *v;
v = isl_calloc_array(ctx, struct isl_fixed_map, n);
if (!v)
return NULL;
for (i = 0; i < n; ++i)
isl_int_init(v[i].v);
return v;
}
static void free_isl_fixed_map_array(struct isl_fixed_map *v, int n)
{
int i;
if (!v)
return;
for (i = 0; i < n; ++i)
isl_int_clear(v[i].v);
free(v);
}
/* Compare the "v" field of two isl_fixed_map structs.
*/
static int qsort_fixed_map_cmp(const void *p1, const void *p2)
{
const struct isl_fixed_map *e1 = (const struct isl_fixed_map *) p1;
const struct isl_fixed_map *e2 = (const struct isl_fixed_map *) p2;
return isl_int_cmp(e1->v, e2->v);
}
/* Internal data structure used while checking whether all maps
* in a union_map have a fixed value for a given output dimension.
* v is the list of maps, with the fixed value for the dimension
* n is the number of maps considered so far
* pos is the output dimension under investigation
*/
struct isl_fixed_dim_data {
struct isl_fixed_map *v;
int n;
int pos;
};
static isl_bool fixed_at_pos(__isl_keep isl_map *map, void *user)
{
struct isl_fixed_dim_data *data = user;
data->v[data->n].map = map;
return isl_map_plain_is_fixed(map, isl_dim_out, data->pos,
&data->v[data->n++].v);
}
static isl_bool plain_injective_on_range(__isl_take isl_union_map *umap,
int first, int n_range);
/* Given a list of the maps, with their fixed values at output dimension "pos",
* check whether the ranges of the maps form an obvious partition.
*
* We first sort the maps according to their fixed values.
* If all maps have a different value, then we know the ranges form
* a partition.
* Otherwise, we collect the maps with the same fixed value and
* check whether each such collection is obviously injective
* based on later dimensions.
*/
static int separates(struct isl_fixed_map *v, int n,
__isl_take isl_space *dim, int pos, int n_range)
{
int i;
if (!v)
goto error;
qsort(v, n, sizeof(*v), &qsort_fixed_map_cmp);
for (i = 0; i + 1 < n; ++i) {
int j, k;
isl_union_map *part;
int injective;
for (j = i + 1; j < n; ++j)
if (isl_int_ne(v[i].v, v[j].v))
break;
if (j == i + 1)
continue;
part = isl_union_map_alloc(isl_space_copy(dim), j - i);
for (k = i; k < j; ++k)
part = isl_union_map_add_map(part,
isl_map_copy(v[k].map));
injective = plain_injective_on_range(part, pos + 1, n_range);
if (injective < 0)
goto error;
if (!injective)
break;
i = j - 1;
}
isl_space_free(dim);
free_isl_fixed_map_array(v, n);
return i + 1 >= n;
error:
isl_space_free(dim);
free_isl_fixed_map_array(v, n);
return -1;
}
/* Check whether the maps in umap have obviously distinct ranges.
* In particular, check for an output dimension in the range
* [first,n_range) for which all maps have a fixed value
* and then check if these values, possibly along with fixed values
* at later dimensions, entail distinct ranges.
*/
static isl_bool plain_injective_on_range(__isl_take isl_union_map *umap,
int first, int n_range)
{
isl_ctx *ctx;
int n;
struct isl_fixed_dim_data data = { NULL };
ctx = isl_union_map_get_ctx(umap);
n = isl_union_map_n_map(umap);
if (!umap)
goto error;
if (n <= 1) {
isl_union_map_free(umap);
return isl_bool_true;
}
if (first >= n_range) {
isl_union_map_free(umap);
return isl_bool_false;
}
data.v = alloc_isl_fixed_map_array(ctx, n);
if (!data.v)
goto error;
for (data.pos = first; data.pos < n_range; ++data.pos) {
isl_bool fixed;
int injective;
isl_space *dim;
data.n = 0;
fixed = union_map_forall_user(umap, &fixed_at_pos, &data);
if (fixed < 0)
goto error;
if (!fixed)
continue;
dim = isl_union_map_get_space(umap);
injective = separates(data.v, n, dim, data.pos, n_range);
isl_union_map_free(umap);
return injective;
}
free_isl_fixed_map_array(data.v, n);
isl_union_map_free(umap);
return isl_bool_false;
error:
free_isl_fixed_map_array(data.v, n);
isl_union_map_free(umap);
return isl_bool_error;
}
/* Check whether the maps in umap that map to subsets of "ran"
* have obviously distinct ranges.
*/
static isl_bool plain_injective_on_range_wrap(__isl_keep isl_set *ran,
void *user)
{
isl_union_map *umap = user;
umap = isl_union_map_copy(umap);
umap = isl_union_map_intersect_range(umap,
isl_union_set_from_set(isl_set_copy(ran)));
return plain_injective_on_range(umap, 0, isl_set_dim(ran, isl_dim_set));
}
/* Check if the given union_map is obviously injective.
*
* In particular, we first check if all individual maps are obviously
* injective and then check if all the ranges of these maps are
* obviously disjoint.
*/
isl_bool isl_union_map_plain_is_injective(__isl_keep isl_union_map *umap)
{
isl_bool in;
isl_union_map *univ;
isl_union_set *ran;
in = union_map_forall(umap, &isl_map_plain_is_injective);
if (in < 0)
return isl_bool_error;
if (!in)
return isl_bool_false;
univ = isl_union_map_universe(isl_union_map_copy(umap));
ran = isl_union_map_range(univ);
in = union_map_forall_user(ran, &plain_injective_on_range_wrap, umap);
isl_union_set_free(ran);
return in;
}
isl_bool isl_union_map_is_bijective(__isl_keep isl_union_map *umap)
{
isl_bool sv;
sv = isl_union_map_is_single_valued(umap);
if (sv < 0 || !sv)
return sv;
return isl_union_map_is_injective(umap);
}
__isl_give isl_union_map *isl_union_map_zip(__isl_take isl_union_map *umap)
{
struct isl_un_op_drop_user_data data = { &isl_map_can_zip };
struct isl_un_op_control control = {
.filter = &un_op_filter_drop_user,
.filter_user = &data,
.fn_map = &isl_map_zip,
};
return un_op(umap, &control);
}
/* Given a union map, take the maps of the form A -> (B -> C) and
* return the union of the corresponding maps (A -> B) -> C.
*/
__isl_give isl_union_map *isl_union_map_uncurry(__isl_take isl_union_map *umap)
{
struct isl_un_op_drop_user_data data = { &isl_map_can_uncurry };
struct isl_un_op_control control = {
.filter = &un_op_filter_drop_user,
.filter_user = &data,
.fn_map = &isl_map_uncurry,
};
return un_op(umap, &control);
}
/* Given a union map, take the maps of the form (A -> B) -> C and
* return the union of the corresponding maps A -> (B -> C).
*/
__isl_give isl_union_map *isl_union_map_curry(__isl_take isl_union_map *umap)
{
struct isl_un_op_drop_user_data data = { &isl_map_can_curry };
struct isl_un_op_control control = {
.filter = &un_op_filter_drop_user,
.filter_user = &data,
.fn_map = &isl_map_curry,
};
return un_op(umap, &control);
}
/* Given a union map, take the maps of the form A -> ((B -> C) -> D) and
* return the union of the corresponding maps A -> (B -> (C -> D)).
*/
__isl_give isl_union_map *isl_union_map_range_curry(
__isl_take isl_union_map *umap)
{
struct isl_un_op_drop_user_data data = { &isl_map_can_range_curry };
struct isl_un_op_control control = {
.filter = &un_op_filter_drop_user,
.filter_user = &data,
.fn_map = &isl_map_range_curry,
};
return un_op(umap, &control);
}
__isl_give isl_union_set *isl_union_set_lift(__isl_take isl_union_set *uset)
{
struct isl_un_op_control control = {
.fn_map = &isl_set_lift,
};
return un_op(uset, &control);
}
static isl_stat coefficients_entry(void **entry, void *user)
{
isl_set *set = *entry;
isl_union_set **res = user;
set = isl_set_copy(set);
set = isl_set_from_basic_set(isl_set_coefficients(set));
*res = isl_union_set_add_set(*res, set);
return isl_stat_ok;
}
__isl_give isl_union_set *isl_union_set_coefficients(
__isl_take isl_union_set *uset)
{
isl_ctx *ctx;
isl_space *dim;
isl_union_set *res;
if (!uset)
return NULL;
ctx = isl_union_set_get_ctx(uset);
dim = isl_space_set_alloc(ctx, 0, 0);
res = isl_union_map_alloc(dim, uset->table.n);
if (isl_hash_table_foreach(uset->dim->ctx, &uset->table,
&coefficients_entry, &res) < 0)
goto error;
isl_union_set_free(uset);
return res;
error:
isl_union_set_free(uset);
isl_union_set_free(res);
return NULL;
}
static isl_stat solutions_entry(void **entry, void *user)
{
isl_set *set = *entry;
isl_union_set **res = user;
set = isl_set_copy(set);
set = isl_set_from_basic_set(isl_set_solutions(set));
if (!*res)
*res = isl_union_set_from_set(set);
else
*res = isl_union_set_add_set(*res, set);
if (!*res)
return isl_stat_error;
return isl_stat_ok;
}
__isl_give isl_union_set *isl_union_set_solutions(
__isl_take isl_union_set *uset)
{
isl_union_set *res = NULL;
if (!uset)
return NULL;
if (uset->table.n == 0) {
res = isl_union_set_empty(isl_union_set_get_space(uset));
isl_union_set_free(uset);
return res;
}
if (isl_hash_table_foreach(uset->dim->ctx, &uset->table,
&solutions_entry, &res) < 0)
goto error;
isl_union_set_free(uset);
return res;
error:
isl_union_set_free(uset);
isl_union_set_free(res);
return NULL;
}
/* Is the domain space of "map" equal to "space"?
*/
static int domain_match(__isl_keep isl_map *map, __isl_keep isl_space *space)
{
return isl_space_tuple_is_equal(map->dim, isl_dim_in,
space, isl_dim_out);
}
/* Is the range space of "map" equal to "space"?
*/
static int range_match(__isl_keep isl_map *map, __isl_keep isl_space *space)
{
return isl_space_tuple_is_equal(map->dim, isl_dim_out,
space, isl_dim_out);
}
/* Is the set space of "map" equal to "space"?
*/
static int set_match(__isl_keep isl_map *map, __isl_keep isl_space *space)
{
return isl_space_tuple_is_equal(map->dim, isl_dim_set,
space, isl_dim_out);
}
/* Internal data structure for preimage_pw_multi_aff.
*
* "pma" is the function under which the preimage should be taken.
* "space" is the space of "pma".
* "res" collects the results.
* "fn" computes the preimage for a given map.
* "match" returns true if "fn" can be called.
*/
struct isl_union_map_preimage_data {
isl_space *space;
isl_pw_multi_aff *pma;
isl_union_map *res;
int (*match)(__isl_keep isl_map *map, __isl_keep isl_space *space);
__isl_give isl_map *(*fn)(__isl_take isl_map *map,
__isl_take isl_pw_multi_aff *pma);
};
/* Call data->fn to compute the preimage of the domain or range of *entry
* under the function represented by data->pma, provided the domain/range
* space of *entry matches the target space of data->pma
* (as given by data->match), and add the result to data->res.
*/
static isl_stat preimage_entry(void **entry, void *user)
{
int m;
isl_map *map = *entry;
struct isl_union_map_preimage_data *data = user;
isl_bool empty;
m = data->match(map, data->space);
if (m < 0)
return isl_stat_error;
if (!m)
return isl_stat_ok;
map = isl_map_copy(map);
map = data->fn(map, isl_pw_multi_aff_copy(data->pma));
empty = isl_map_is_empty(map);
if (empty < 0 || empty) {
isl_map_free(map);
return empty < 0 ? isl_stat_error : isl_stat_ok;
}
data->res = isl_union_map_add_map(data->res, map);
return isl_stat_ok;
}
/* Compute the preimage of the domain or range of "umap" under the function
* represented by "pma".
* In other words, plug in "pma" in the domain or range of "umap".
* The function "fn" performs the actual preimage computation on a map,
* while "match" determines to which maps the function should be applied.
*/
static __isl_give isl_union_map *preimage_pw_multi_aff(
__isl_take isl_union_map *umap, __isl_take isl_pw_multi_aff *pma,
int (*match)(__isl_keep isl_map *map, __isl_keep isl_space *space),
__isl_give isl_map *(*fn)(__isl_take isl_map *map,
__isl_take isl_pw_multi_aff *pma))
{
isl_ctx *ctx;
isl_space *space;
struct isl_union_map_preimage_data data;
umap = isl_union_map_align_params(umap,
isl_pw_multi_aff_get_space(pma));
pma = isl_pw_multi_aff_align_params(pma, isl_union_map_get_space(umap));
if (!umap || !pma)
goto error;
ctx = isl_union_map_get_ctx(umap);
space = isl_union_map_get_space(umap);
data.space = isl_pw_multi_aff_get_space(pma);
data.pma = pma;
data.res = isl_union_map_alloc(space, umap->table.n);
data.match = match;
data.fn = fn;
if (isl_hash_table_foreach(ctx, &umap->table, &preimage_entry,
&data) < 0)
data.res = isl_union_map_free(data.res);
isl_space_free(data.space);
isl_union_map_free(umap);
isl_pw_multi_aff_free(pma);
return data.res;
error:
isl_union_map_free(umap);
isl_pw_multi_aff_free(pma);
return NULL;
}
/* Compute the preimage of the domain of "umap" under the function
* represented by "pma".
* In other words, plug in "pma" in the domain of "umap".
* The result contains maps that live in the same spaces as the maps of "umap"
* with domain space equal to the target space of "pma",
* except that the domain has been replaced by the domain space of "pma".
*/
__isl_give isl_union_map *isl_union_map_preimage_domain_pw_multi_aff(
__isl_take isl_union_map *umap, __isl_take isl_pw_multi_aff *pma)
{
return preimage_pw_multi_aff(umap, pma, &domain_match,
&isl_map_preimage_domain_pw_multi_aff);
}
/* Compute the preimage of the range of "umap" under the function
* represented by "pma".
* In other words, plug in "pma" in the range of "umap".
* The result contains maps that live in the same spaces as the maps of "umap"
* with range space equal to the target space of "pma",
* except that the range has been replaced by the domain space of "pma".
*/
__isl_give isl_union_map *isl_union_map_preimage_range_pw_multi_aff(
__isl_take isl_union_map *umap, __isl_take isl_pw_multi_aff *pma)
{
return preimage_pw_multi_aff(umap, pma, &range_match,
&isl_map_preimage_range_pw_multi_aff);
}
/* Compute the preimage of "uset" under the function represented by "pma".
* In other words, plug in "pma" in "uset".
* The result contains sets that live in the same spaces as the sets of "uset"
* with space equal to the target space of "pma",
* except that the space has been replaced by the domain space of "pma".
*/
__isl_give isl_union_set *isl_union_set_preimage_pw_multi_aff(
__isl_take isl_union_set *uset, __isl_take isl_pw_multi_aff *pma)
{
return preimage_pw_multi_aff(uset, pma, &set_match,
&isl_set_preimage_pw_multi_aff);
}
/* Compute the preimage of the domain of "umap" under the function
* represented by "ma".
* In other words, plug in "ma" in the domain of "umap".
* The result contains maps that live in the same spaces as the maps of "umap"
* with domain space equal to the target space of "ma",
* except that the domain has been replaced by the domain space of "ma".
*/
__isl_give isl_union_map *isl_union_map_preimage_domain_multi_aff(
__isl_take isl_union_map *umap, __isl_take isl_multi_aff *ma)
{
return isl_union_map_preimage_domain_pw_multi_aff(umap,
isl_pw_multi_aff_from_multi_aff(ma));
}
/* Compute the preimage of the range of "umap" under the function
* represented by "ma".
* In other words, plug in "ma" in the range of "umap".
* The result contains maps that live in the same spaces as the maps of "umap"
* with range space equal to the target space of "ma",
* except that the range has been replaced by the domain space of "ma".
*/
__isl_give isl_union_map *isl_union_map_preimage_range_multi_aff(
__isl_take isl_union_map *umap, __isl_take isl_multi_aff *ma)
{
return isl_union_map_preimage_range_pw_multi_aff(umap,
isl_pw_multi_aff_from_multi_aff(ma));
}
/* Compute the preimage of "uset" under the function represented by "ma".
* In other words, plug in "ma" in "uset".
* The result contains sets that live in the same spaces as the sets of "uset"
* with space equal to the target space of "ma",
* except that the space has been replaced by the domain space of "ma".
*/
__isl_give isl_union_map *isl_union_set_preimage_multi_aff(
__isl_take isl_union_set *uset, __isl_take isl_multi_aff *ma)
{
return isl_union_set_preimage_pw_multi_aff(uset,
isl_pw_multi_aff_from_multi_aff(ma));
}
/* Internal data structure for preimage_multi_pw_aff.
*
* "mpa" is the function under which the preimage should be taken.
* "space" is the space of "mpa".
* "res" collects the results.
* "fn" computes the preimage for a given map.
* "match" returns true if "fn" can be called.
*/
struct isl_union_map_preimage_mpa_data {
isl_space *space;
isl_multi_pw_aff *mpa;
isl_union_map *res;
int (*match)(__isl_keep isl_map *map, __isl_keep isl_space *space);
__isl_give isl_map *(*fn)(__isl_take isl_map *map,
__isl_take isl_multi_pw_aff *mpa);
};
/* Call data->fn to compute the preimage of the domain or range of *entry
* under the function represented by data->mpa, provided the domain/range
* space of *entry matches the target space of data->mpa
* (as given by data->match), and add the result to data->res.
*/
static isl_stat preimage_mpa_entry(void **entry, void *user)
{
int m;
isl_map *map = *entry;
struct isl_union_map_preimage_mpa_data *data = user;
isl_bool empty;
m = data->match(map, data->space);
if (m < 0)
return isl_stat_error;
if (!m)
return isl_stat_ok;
map = isl_map_copy(map);
map = data->fn(map, isl_multi_pw_aff_copy(data->mpa));
empty = isl_map_is_empty(map);
if (empty < 0 || empty) {
isl_map_free(map);
return empty < 0 ? isl_stat_error : isl_stat_ok;
}
data->res = isl_union_map_add_map(data->res, map);
return isl_stat_ok;
}
/* Compute the preimage of the domain or range of "umap" under the function
* represented by "mpa".
* In other words, plug in "mpa" in the domain or range of "umap".
* The function "fn" performs the actual preimage computation on a map,
* while "match" determines to which maps the function should be applied.
*/
static __isl_give isl_union_map *preimage_multi_pw_aff(
__isl_take isl_union_map *umap, __isl_take isl_multi_pw_aff *mpa,
int (*match)(__isl_keep isl_map *map, __isl_keep isl_space *space),
__isl_give isl_map *(*fn)(__isl_take isl_map *map,
__isl_take isl_multi_pw_aff *mpa))
{
isl_ctx *ctx;
isl_space *space;
struct isl_union_map_preimage_mpa_data data;
umap = isl_union_map_align_params(umap,
isl_multi_pw_aff_get_space(mpa));
mpa = isl_multi_pw_aff_align_params(mpa, isl_union_map_get_space(umap));
if (!umap || !mpa)
goto error;
ctx = isl_union_map_get_ctx(umap);
space = isl_union_map_get_space(umap);
data.space = isl_multi_pw_aff_get_space(mpa);
data.mpa = mpa;
data.res = isl_union_map_alloc(space, umap->table.n);
data.match = match;
data.fn = fn;
if (isl_hash_table_foreach(ctx, &umap->table, &preimage_mpa_entry,
&data) < 0)
data.res = isl_union_map_free(data.res);
isl_space_free(data.space);
isl_union_map_free(umap);
isl_multi_pw_aff_free(mpa);
return data.res;
error:
isl_union_map_free(umap);
isl_multi_pw_aff_free(mpa);
return NULL;
}
/* Compute the preimage of the domain of "umap" under the function
* represented by "mpa".
* In other words, plug in "mpa" in the domain of "umap".
* The result contains maps that live in the same spaces as the maps of "umap"
* with domain space equal to the target space of "mpa",
* except that the domain has been replaced by the domain space of "mpa".
*/
__isl_give isl_union_map *isl_union_map_preimage_domain_multi_pw_aff(
__isl_take isl_union_map *umap, __isl_take isl_multi_pw_aff *mpa)
{
return preimage_multi_pw_aff(umap, mpa, &domain_match,
&isl_map_preimage_domain_multi_pw_aff);
}
/* Internal data structure for preimage_upma.
*
* "umap" is the map of which the preimage should be computed.
* "res" collects the results.
* "fn" computes the preimage for a given piecewise multi-affine function.
*/
struct isl_union_map_preimage_upma_data {
isl_union_map *umap;
isl_union_map *res;
__isl_give isl_union_map *(*fn)(__isl_take isl_union_map *umap,
__isl_take isl_pw_multi_aff *pma);
};
/* Call data->fn to compute the preimage of the domain or range of data->umap
* under the function represented by pma and add the result to data->res.
*/
static isl_stat preimage_upma(__isl_take isl_pw_multi_aff *pma, void *user)
{
struct isl_union_map_preimage_upma_data *data = user;
isl_union_map *umap;
umap = isl_union_map_copy(data->umap);
umap = data->fn(umap, pma);
data->res = isl_union_map_union(data->res, umap);
return data->res ? isl_stat_ok : isl_stat_error;
}
/* Compute the preimage of the domain or range of "umap" under the function
* represented by "upma".
* In other words, plug in "upma" in the domain or range of "umap".
* The function "fn" performs the actual preimage computation
* on a piecewise multi-affine function.
*/
static __isl_give isl_union_map *preimage_union_pw_multi_aff(
__isl_take isl_union_map *umap,
__isl_take isl_union_pw_multi_aff *upma,
__isl_give isl_union_map *(*fn)(__isl_take isl_union_map *umap,
__isl_take isl_pw_multi_aff *pma))
{
struct isl_union_map_preimage_upma_data data;
data.umap = umap;
data.res = isl_union_map_empty(isl_union_map_get_space(umap));
data.fn = fn;
if (isl_union_pw_multi_aff_foreach_pw_multi_aff(upma,
&preimage_upma, &data) < 0)
data.res = isl_union_map_free(data.res);
isl_union_map_free(umap);
isl_union_pw_multi_aff_free(upma);
return data.res;
}
/* Compute the preimage of the domain of "umap" under the function
* represented by "upma".
* In other words, plug in "upma" in the domain of "umap".
* The result contains maps that live in the same spaces as the maps of "umap"
* with domain space equal to one of the target spaces of "upma",
* except that the domain has been replaced by one of the domain spaces that
* correspond to that target space of "upma".
*/
__isl_give isl_union_map *isl_union_map_preimage_domain_union_pw_multi_aff(
__isl_take isl_union_map *umap,
__isl_take isl_union_pw_multi_aff *upma)
{
return preimage_union_pw_multi_aff(umap, upma,
&isl_union_map_preimage_domain_pw_multi_aff);
}
/* Compute the preimage of the range of "umap" under the function
* represented by "upma".
* In other words, plug in "upma" in the range of "umap".
* The result contains maps that live in the same spaces as the maps of "umap"
* with range space equal to one of the target spaces of "upma",
* except that the range has been replaced by one of the domain spaces that
* correspond to that target space of "upma".
*/
__isl_give isl_union_map *isl_union_map_preimage_range_union_pw_multi_aff(
__isl_take isl_union_map *umap,
__isl_take isl_union_pw_multi_aff *upma)
{
return preimage_union_pw_multi_aff(umap, upma,
&isl_union_map_preimage_range_pw_multi_aff);
}
/* Compute the preimage of "uset" under the function represented by "upma".
* In other words, plug in "upma" in the range of "uset".
* The result contains sets that live in the same spaces as the sets of "uset"
* with space equal to one of the target spaces of "upma",
* except that the space has been replaced by one of the domain spaces that
* correspond to that target space of "upma".
*/
__isl_give isl_union_set *isl_union_set_preimage_union_pw_multi_aff(
__isl_take isl_union_set *uset,
__isl_take isl_union_pw_multi_aff *upma)
{
return preimage_union_pw_multi_aff(uset, upma,
&isl_union_set_preimage_pw_multi_aff);
}
/* Reset the user pointer on all identifiers of parameters and tuples
* of the spaces of "umap".
*/
__isl_give isl_union_map *isl_union_map_reset_user(
__isl_take isl_union_map *umap)
{
umap = isl_union_map_cow(umap);
if (!umap)
return NULL;
umap->dim = isl_space_reset_user(umap->dim);
if (!umap->dim)
return isl_union_map_free(umap);
return total(umap, &isl_map_reset_user);
}
/* Reset the user pointer on all identifiers of parameters and tuples
* of the spaces of "uset".
*/
__isl_give isl_union_set *isl_union_set_reset_user(
__isl_take isl_union_set *uset)
{
return isl_union_map_reset_user(uset);
}
/* Remove all existentially quantified variables and integer divisions
* from "umap" using Fourier-Motzkin elimination.
*/
__isl_give isl_union_map *isl_union_map_remove_divs(
__isl_take isl_union_map *umap)
{
return total(umap, &isl_map_remove_divs);
}
/* Remove all existentially quantified variables and integer divisions
* from "uset" using Fourier-Motzkin elimination.
*/
__isl_give isl_union_set *isl_union_set_remove_divs(
__isl_take isl_union_set *uset)
{
return isl_union_map_remove_divs(uset);
}
/* Internal data structure for isl_union_map_project_out.
* "type", "first" and "n" are the arguments for the isl_map_project_out
* call.
* "res" collects the results.
*/
struct isl_union_map_project_out_data {
enum isl_dim_type type;
unsigned first;
unsigned n;
isl_union_map *res;
};
/* Turn the data->n dimensions of type data->type, starting at data->first
* into existentially quantified variables and add the result to data->res.
*/
static isl_stat project_out(__isl_take isl_map *map, void *user)
{
struct isl_union_map_project_out_data *data = user;
map = isl_map_project_out(map, data->type, data->first, data->n);
data->res = isl_union_map_add_map(data->res, map);
return isl_stat_ok;
}
/* Turn the "n" dimensions of type "type", starting at "first"
* into existentially quantified variables.
* Since the space of an isl_union_map only contains parameters,
* type is required to be equal to isl_dim_param.
*/
__isl_give isl_union_map *isl_union_map_project_out(
__isl_take isl_union_map *umap,
enum isl_dim_type type, unsigned first, unsigned n)
{
isl_space *space;
struct isl_union_map_project_out_data data = { type, first, n };
if (!umap)
return NULL;
if (type != isl_dim_param)
isl_die(isl_union_map_get_ctx(umap), isl_error_invalid,
"can only project out parameters",
return isl_union_map_free(umap));
space = isl_union_map_get_space(umap);
space = isl_space_drop_dims(space, type, first, n);
data.res = isl_union_map_empty(space);
if (isl_union_map_foreach_map(umap, &project_out, &data) < 0)
data.res = isl_union_map_free(data.res);
isl_union_map_free(umap);
return data.res;
}
/* Project out all parameters from "umap" by existentially quantifying
* over them.
*/
__isl_give isl_union_map *isl_union_map_project_out_all_params(
__isl_take isl_union_map *umap)
{
unsigned n;
if (!umap)
return NULL;
n = isl_union_map_dim(umap, isl_dim_param);
return isl_union_map_project_out(umap, isl_dim_param, 0, n);
}
/* Turn the "n" dimensions of type "type", starting at "first"
* into existentially quantified variables.
* Since the space of an isl_union_set only contains parameters,
* "type" is required to be equal to isl_dim_param.
*/
__isl_give isl_union_set *isl_union_set_project_out(
__isl_take isl_union_set *uset,
enum isl_dim_type type, unsigned first, unsigned n)
{
return isl_union_map_project_out(uset, type, first, n);
}
/* Internal data structure for isl_union_map_involves_dims.
* "first" and "n" are the arguments for the isl_map_involves_dims calls.
*/
struct isl_union_map_involves_dims_data {
unsigned first;
unsigned n;
};
/* Does "map" _not_ involve the data->n parameters starting at data->first?
*/
static isl_bool map_excludes(__isl_keep isl_map *map, void *user)
{
struct isl_union_map_involves_dims_data *data = user;
isl_bool involves;
involves = isl_map_involves_dims(map,
isl_dim_param, data->first, data->n);
if (involves < 0)
return isl_bool_error;
return !involves;
}
/* Does "umap" involve any of the n parameters starting at first?
* "type" is required to be set to isl_dim_param.
*
* "umap" involves any of those parameters if any of its maps
* involve the parameters. In other words, "umap" does not
* involve any of the parameters if all its maps to not
* involve the parameters.
*/
isl_bool isl_union_map_involves_dims(__isl_keep isl_union_map *umap,
enum isl_dim_type type, unsigned first, unsigned n)
{
struct isl_union_map_involves_dims_data data = { first, n };
isl_bool excludes;
if (type != isl_dim_param)
isl_die(isl_union_map_get_ctx(umap), isl_error_invalid,
"can only reference parameters", return isl_bool_error);
excludes = union_map_forall_user(umap, &map_excludes, &data);
if (excludes < 0)
return isl_bool_error;
return !excludes;
}
/* Internal data structure for isl_union_map_reset_range_space.
* "range" is the space from which to set the range space.
* "res" collects the results.
*/
struct isl_union_map_reset_range_space_data {
isl_space *range;
isl_union_map *res;
};
/* Replace the range space of "map" by the range space of data->range and
* add the result to data->res.
*/
static isl_stat reset_range_space(__isl_take isl_map *map, void *user)
{
struct isl_union_map_reset_range_space_data *data = user;
isl_space *space;
space = isl_map_get_space(map);
space = isl_space_domain(space);
space = isl_space_extend_domain_with_range(space,
isl_space_copy(data->range));
map = isl_map_reset_space(map, space);
data->res = isl_union_map_add_map(data->res, map);
return data->res ? isl_stat_ok : isl_stat_error;
}
/* Replace the range space of all the maps in "umap" by
* the range space of "space".
*
* This assumes that all maps have the same output dimension.
* This function should therefore not be made publicly available.
*
* Since the spaces of the maps change, so do their hash value.
* We therefore need to create a new isl_union_map.
*/
__isl_give isl_union_map *isl_union_map_reset_range_space(
__isl_take isl_union_map *umap, __isl_take isl_space *space)
{
struct isl_union_map_reset_range_space_data data = { space };
data.res = isl_union_map_empty(isl_union_map_get_space(umap));
if (isl_union_map_foreach_map(umap, &reset_range_space, &data) < 0)
data.res = isl_union_map_free(data.res);
isl_space_free(space);
isl_union_map_free(umap);
return data.res;
}
/* Check that "umap" and "space" have the same number of parameters.
*/
static isl_stat check_union_map_space_equal_dim(__isl_keep isl_union_map *umap,
__isl_keep isl_space *space)
{
unsigned dim1, dim2;
if (!umap || !space)
return isl_stat_error;
dim1 = isl_union_map_dim(umap, isl_dim_param);
dim2 = isl_space_dim(space, isl_dim_param);
if (dim1 == dim2)
return isl_stat_ok;
isl_die(isl_union_map_get_ctx(umap), isl_error_invalid,
"number of parameters does not match", return isl_stat_error);
}
/* Internal data structure for isl_union_map_reset_equal_dim_space.
* "space" is the target space.
* "res" collects the results.
*/
struct isl_union_map_reset_params_data {
isl_space *space;
isl_union_map *res;
};
/* Replace the parameters of "map" by those of data->space and
* add the result to data->res.
*/
static isl_stat reset_params(__isl_take isl_map *map, void *user)
{
struct isl_union_map_reset_params_data *data = user;
isl_space *space;
space = isl_map_get_space(map);
space = isl_space_replace_params(space, data->space);
map = isl_map_reset_equal_dim_space(map, space);
data->res = isl_union_map_add_map(data->res, map);
return data->res ? isl_stat_ok : isl_stat_error;
}
/* Replace the space of "umap" by "space", without modifying
* the dimension of "umap", i.e., the number of parameters of "umap".
*
* Since the hash values of the maps in the union map depend
* on the parameters, a new union map needs to be constructed.
*/
__isl_give isl_union_map *isl_union_map_reset_equal_dim_space(
__isl_take isl_union_map *umap, __isl_take isl_space *space)
{
struct isl_union_map_reset_params_data data = { space };
isl_bool equal;
isl_space *umap_space;
umap_space = isl_union_map_peek_space(umap);
equal = isl_space_is_equal(umap_space, space);
if (equal < 0)
goto error;
if (equal) {
isl_space_free(space);
return umap;
}
if (check_union_map_space_equal_dim(umap, space) < 0)
goto error;
data.res = isl_union_map_empty(isl_space_copy(space));
if (isl_union_map_foreach_map(umap, &reset_params, &data) < 0)
data.res = isl_union_map_free(data.res);
isl_space_free(space);
isl_union_map_free(umap);
return data.res;
error:
isl_union_map_free(umap);
isl_space_free(space);
return NULL;
}
/* Internal data structure for isl_union_map_order_at_multi_union_pw_aff.
* "mupa" is the function from which the isl_multi_pw_affs are extracted.
* "order" is applied to the extracted isl_multi_pw_affs that correspond
* to the domain and the range of each map.
* "res" collects the results.
*/
struct isl_union_order_at_data {
isl_multi_union_pw_aff *mupa;
__isl_give isl_map *(*order)(__isl_take isl_multi_pw_aff *mpa1,
__isl_take isl_multi_pw_aff *mpa2);
isl_union_map *res;
};
/* Intersect "map" with the result of applying data->order to
* the functions in data->mupa that apply to the domain and the range
* of "map" and add the result to data->res.
*/
static isl_stat order_at(__isl_take isl_map *map, void *user)
{
struct isl_union_order_at_data *data = user;
isl_space *space;
isl_multi_pw_aff *mpa1, *mpa2;
isl_map *order;
space = isl_space_domain(isl_map_get_space(map));
mpa1 = isl_multi_union_pw_aff_extract_multi_pw_aff(data->mupa, space);
space = isl_space_range(isl_map_get_space(map));
mpa2 = isl_multi_union_pw_aff_extract_multi_pw_aff(data->mupa, space);
order = data->order(mpa1, mpa2);
map = isl_map_intersect(map, order);
data->res = isl_union_map_add_map(data->res, map);
return data->res ? isl_stat_ok : isl_stat_error;
}
/* If "mupa" has a non-trivial explicit domain, then intersect
* domain and range of "umap" with this explicit domain.
* If the explicit domain only describes constraints on the parameters,
* then the intersection only needs to be performed once.
*/
static __isl_give isl_union_map *intersect_explicit_domain(
__isl_take isl_union_map *umap, __isl_keep isl_multi_union_pw_aff *mupa)
{
isl_bool non_trivial, is_params;
isl_union_set *dom;
non_trivial = isl_multi_union_pw_aff_has_non_trivial_domain(mupa);
if (non_trivial < 0)
return isl_union_map_free(umap);
if (!non_trivial)
return umap;
mupa = isl_multi_union_pw_aff_copy(mupa);
dom = isl_multi_union_pw_aff_domain(mupa);
is_params = isl_union_set_is_params(dom);
if (is_params < 0) {
isl_union_set_free(dom);
return isl_union_map_free(umap);
}
if (is_params) {
isl_set *set;
set = isl_union_set_params(dom);
umap = isl_union_map_intersect_params(umap, set);
return umap;
}
umap = isl_union_map_intersect_domain(umap, isl_union_set_copy(dom));
umap = isl_union_map_intersect_range(umap, dom);
return umap;
}
/* Intersect each map in "umap" with the result of calling "order"
* on the functions is "mupa" that apply to the domain and the range
* of the map.
*/
static __isl_give isl_union_map *isl_union_map_order_at_multi_union_pw_aff(
__isl_take isl_union_map *umap, __isl_take isl_multi_union_pw_aff *mupa,
__isl_give isl_map *(*order)(__isl_take isl_multi_pw_aff *mpa1,
__isl_take isl_multi_pw_aff *mpa2))
{
struct isl_union_order_at_data data;
umap = isl_union_map_align_params(umap,
isl_multi_union_pw_aff_get_space(mupa));
mupa = isl_multi_union_pw_aff_align_params(mupa,
isl_union_map_get_space(umap));
umap = intersect_explicit_domain(umap, mupa);
data.mupa = mupa;
data.order = order;
data.res = isl_union_map_empty(isl_union_map_get_space(umap));
if (isl_union_map_foreach_map(umap, &order_at, &data) < 0)
data.res = isl_union_map_free(data.res);
isl_multi_union_pw_aff_free(mupa);
isl_union_map_free(umap);
return data.res;
}
/* Return the subset of "umap" where the domain and the range
* have equal "mupa" values.
*/
__isl_give isl_union_map *isl_union_map_eq_at_multi_union_pw_aff(
__isl_take isl_union_map *umap,
__isl_take isl_multi_union_pw_aff *mupa)
{
return isl_union_map_order_at_multi_union_pw_aff(umap, mupa,
&isl_multi_pw_aff_eq_map);
}
/* Return the subset of "umap" where the domain has a lexicographically
* smaller "mupa" value than the range.
*/
__isl_give isl_union_map *isl_union_map_lex_lt_at_multi_union_pw_aff(
__isl_take isl_union_map *umap,
__isl_take isl_multi_union_pw_aff *mupa)
{
return isl_union_map_order_at_multi_union_pw_aff(umap, mupa,
&isl_multi_pw_aff_lex_lt_map);
}
/* Return the subset of "umap" where the domain has a lexicographically
* greater "mupa" value than the range.
*/
__isl_give isl_union_map *isl_union_map_lex_gt_at_multi_union_pw_aff(
__isl_take isl_union_map *umap,
__isl_take isl_multi_union_pw_aff *mupa)
{
return isl_union_map_order_at_multi_union_pw_aff(umap, mupa,
&isl_multi_pw_aff_lex_gt_map);
}
/* Return the union of the elements in the list "list".
*/
__isl_give isl_union_set *isl_union_set_list_union(
__isl_take isl_union_set_list *list)
{
int i, n;
isl_ctx *ctx;
isl_space *space;
isl_union_set *res;
if (!list)
return NULL;
ctx = isl_union_set_list_get_ctx(list);
space = isl_space_params_alloc(ctx, 0);
res = isl_union_set_empty(space);
n = isl_union_set_list_n_union_set(list);
for (i = 0; i < n; ++i) {
isl_union_set *uset_i;
uset_i = isl_union_set_list_get_union_set(list, i);
res = isl_union_set_union(res, uset_i);
}
isl_union_set_list_free(list);
return res;
}
/* Update *hash with the hash value of "map".
*/
static isl_stat add_hash(__isl_take isl_map *map, void *user)
{
uint32_t *hash = user;
uint32_t map_hash;
map_hash = isl_map_get_hash(map);
isl_hash_hash(*hash, map_hash);
isl_map_free(map);
return isl_stat_ok;
}
/* Return a hash value that digests "umap".
*/
uint32_t isl_union_map_get_hash(__isl_keep isl_union_map *umap)
{
uint32_t hash;
if (!umap)
return 0;
hash = isl_hash_init();
if (isl_union_map_foreach_map(umap, &add_hash, &hash) < 0)
return 0;
return hash;
}
/* Return a hash value that digests "uset".
*/
uint32_t isl_union_set_get_hash(__isl_keep isl_union_set *uset)
{
return isl_union_map_get_hash(uset);
}
/* Add the number of basic sets in "set" to "n".
*/
static isl_stat add_n(__isl_take isl_set *set, void *user)
{
int *n = user;
*n += isl_set_n_basic_set(set);
isl_set_free(set);
return isl_stat_ok;
}
/* Return the total number of basic sets in "uset".
*/
int isl_union_set_n_basic_set(__isl_keep isl_union_set *uset)
{
int n = 0;
if (isl_union_set_foreach_set(uset, &add_n, &n) < 0)
return -1;
return n;
}
/* Add the basic sets in "set" to "list".
*/
static isl_stat add_list(__isl_take isl_set *set, void *user)
{
isl_basic_set_list **list = user;
isl_basic_set_list *list_i;
list_i = isl_set_get_basic_set_list(set);
*list = isl_basic_set_list_concat(*list, list_i);
isl_set_free(set);
if (!*list)
return isl_stat_error;
return isl_stat_ok;
}
/* Return a list containing all the basic sets in "uset".
*
* First construct a list of the appropriate size and
* then add all the elements.
*/
__isl_give isl_basic_set_list *isl_union_set_get_basic_set_list(
__isl_keep isl_union_set *uset)
{
int n;
isl_ctx *ctx;
isl_basic_set_list *list;
if (!uset)
return NULL;
ctx = isl_union_set_get_ctx(uset);
n = isl_union_set_n_basic_set(uset);
if (n < 0)
return NULL;
list = isl_basic_set_list_alloc(ctx, n);
if (isl_union_set_foreach_set(uset, &add_list, &list) < 0)
list = isl_basic_set_list_free(list);
return list;
}
/* Internal data structure for isl_union_map_remove_map_if.
* "fn" and "user" are the arguments to isl_union_map_remove_map_if.
*/
struct isl_union_map_remove_map_if_data {
isl_bool (*fn)(__isl_keep isl_map *map, void *user);
void *user;
};
/* isl_un_op_control filter that negates the result of data->fn
* called on "map".
*/
static isl_bool not(__isl_keep isl_map *map, void *user)
{
struct isl_union_map_remove_map_if_data *data = user;
return isl_bool_not(data->fn(map, data->user));
}
/* Dummy isl_un_op_control transformation callback that
* simply returns the input.
*/
static __isl_give isl_map *map_id(__isl_take isl_map *map)
{
return map;
}
/* Call "fn" on every map in "umap" and remove those maps
* for which the callback returns true.
*
* Use un_op to keep only those maps that are not filtered out,
* applying an identity transformation on them.
*/
__isl_give isl_union_map *isl_union_map_remove_map_if(
__isl_take isl_union_map *umap,
isl_bool (*fn)(__isl_keep isl_map *map, void *user), void *user)
{
struct isl_union_map_remove_map_if_data data = { fn, user };
struct isl_un_op_control control = {
.filter = ¬,
.filter_user = &data,
.fn_map = &map_id,
};
return un_op(umap, &control);
}
|