aboutsummaryrefslogtreecommitdiffstats
path: root/contrib/libs/llvm14/tools/polly/lib/External/isl/isl_space.c
blob: fd67a89c65a6411b82cea1aae392d3210f02f555 (plain) (blame)
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
/*
 * Copyright 2008-2009 Katholieke Universiteit Leuven
 * Copyright 2010      INRIA Saclay
 * Copyright 2013-2014 Ecole Normale Superieure
 * Copyright 2018-2019 Cerebras Systems
 *
 * Use of this software is governed by the MIT license
 *
 * Written by Sven Verdoolaege, K.U.Leuven, Departement
 * Computerwetenschappen, Celestijnenlaan 200A, B-3001 Leuven, Belgium
 * and INRIA Saclay - Ile-de-France, Parc Club Orsay Universite,
 * ZAC des vignes, 4 rue Jacques Monod, 91893 Orsay, France 
 * and Ecole Normale Superieure, 45 rue d’Ulm, 75230 Paris, France
 * and Cerebras Systems, 175 S San Antonio Rd, Los Altos, CA, USA
 */

#include <stdlib.h>
#include <string.h>
#include <isl_space_private.h>
#include <isl_id_private.h>
#include <isl_reordering.h>

isl_ctx *isl_space_get_ctx(__isl_keep isl_space *space)
{
	return space ? space->ctx : NULL;
}

__isl_give isl_space *isl_space_alloc(isl_ctx *ctx,
			unsigned nparam, unsigned n_in, unsigned n_out)
{
	isl_space *space;

	space = isl_alloc_type(ctx, struct isl_space);
	if (!space)
		return NULL;

	space->ctx = ctx;
	isl_ctx_ref(ctx);
	space->ref = 1;
	space->nparam = nparam;
	space->n_in = n_in;
	space->n_out = n_out;

	space->tuple_id[0] = NULL;
	space->tuple_id[1] = NULL;

	space->nested[0] = NULL;
	space->nested[1] = NULL;

	space->n_id = 0;
	space->ids = NULL;

	return space;
}

/* Mark the space as being that of a set, by setting the domain tuple
 * to isl_id_none.
 */
static __isl_give isl_space *mark_as_set(__isl_take isl_space *space)
{
	space = isl_space_cow(space);
	if (!space)
		return NULL;
	space = isl_space_set_tuple_id(space, isl_dim_in, &isl_id_none);
	return space;
}

/* Is the space that of a set?
 */
isl_bool isl_space_is_set(__isl_keep isl_space *space)
{
	if (!space)
		return isl_bool_error;
	if (space->n_in != 0 || space->nested[0])
		return isl_bool_false;
	if (space->tuple_id[0] != &isl_id_none)
		return isl_bool_false;
	return isl_bool_true;
}

/* Check that "space" is a set space.
 */
isl_stat isl_space_check_is_set(__isl_keep isl_space *space)
{
	isl_bool is_set;

	is_set = isl_space_is_set(space);
	if (is_set < 0)
		return isl_stat_error;
	if (!is_set)
		isl_die(isl_space_get_ctx(space), isl_error_invalid,
			"space is not a set", return isl_stat_error);
	return isl_stat_ok;
}

/* Is the given space that of a map?
 */
isl_bool isl_space_is_map(__isl_keep isl_space *space)
{
	int r;

	if (!space)
		return isl_bool_error;
	r = space->tuple_id[0] != &isl_id_none &&
	    space->tuple_id[1] != &isl_id_none;
	return isl_bool_ok(r);
}

/* Check that "space" is the space of a map.
 */
static isl_stat isl_space_check_is_map(__isl_keep isl_space *space)
{
	isl_bool is_space;

	is_space = isl_space_is_map(space);
	if (is_space < 0)
		return isl_stat_error;
	if (!is_space)
		isl_die(isl_space_get_ctx(space), isl_error_invalid,
			"expecting map space", return isl_stat_error);
	return isl_stat_ok;
}

/* Check that "space" is the space of a map
 * where the domain is a wrapped map space.
 */
isl_stat isl_space_check_domain_is_wrapping(__isl_keep isl_space *space)
{
	isl_bool wrapping;

	wrapping = isl_space_domain_is_wrapping(space);
	if (wrapping < 0)
		return isl_stat_error;
	if (!wrapping)
		isl_die(isl_space_get_ctx(space), isl_error_invalid,
			"domain not a product", return isl_stat_error);
	return isl_stat_ok;
}

/* Check that "space" is the space of a map
 * where the range is a wrapped map space.
 */
isl_stat isl_space_check_range_is_wrapping(__isl_keep isl_space *space)
{
	isl_bool wrapping;

	wrapping = isl_space_range_is_wrapping(space);
	if (wrapping < 0)
		return isl_stat_error;
	if (!wrapping)
		isl_die(isl_space_get_ctx(space), isl_error_invalid,
			"range not a product", return isl_stat_error);
	return isl_stat_ok;
}

__isl_give isl_space *isl_space_set_alloc(isl_ctx *ctx,
			unsigned nparam, unsigned dim)
{
	isl_space *space;
	space = isl_space_alloc(ctx, nparam, 0, dim);
	space = mark_as_set(space);
	return space;
}

/* Mark the space as being that of a parameter domain, by setting
 * both tuples to isl_id_none.
 */
static __isl_give isl_space *mark_as_params(isl_space *space)
{
	if (!space)
		return NULL;
	space = isl_space_set_tuple_id(space, isl_dim_in, &isl_id_none);
	space = isl_space_set_tuple_id(space, isl_dim_out, &isl_id_none);
	return space;
}

/* Is the space that of a parameter domain?
 */
isl_bool isl_space_is_params(__isl_keep isl_space *space)
{
	if (!space)
		return isl_bool_error;
	if (space->n_in != 0 || space->nested[0] ||
	    space->n_out != 0 || space->nested[1])
		return isl_bool_false;
	if (space->tuple_id[0] != &isl_id_none)
		return isl_bool_false;
	if (space->tuple_id[1] != &isl_id_none)
		return isl_bool_false;
	return isl_bool_true;
}

/* Create a space for a parameter domain.
 */
__isl_give isl_space *isl_space_params_alloc(isl_ctx *ctx, unsigned nparam)
{
	isl_space *space;
	space = isl_space_alloc(ctx, nparam, 0, 0);
	space = mark_as_params(space);
	return space;
}

/* Create a space for a parameter domain, without any parameters.
 */
__isl_give isl_space *isl_space_unit(isl_ctx *ctx)
{
	return isl_space_params_alloc(ctx, 0);
}

static isl_size global_pos(__isl_keep isl_space *space,
				 enum isl_dim_type type, unsigned pos)
{
	if (isl_space_check_range(space, type, pos, 1) < 0)
		return isl_size_error;

	switch (type) {
	case isl_dim_param:
		return pos;
	case isl_dim_in:
		return pos + space->nparam;
	case isl_dim_out:
		return pos + space->nparam + space->n_in;
	default:
		isl_assert(isl_space_get_ctx(space), 0, return isl_size_error);
	}
	return isl_size_error;
}

/* Extend length of ids array to the total number of dimensions.
 */
static __isl_give isl_space *extend_ids(__isl_take isl_space *space)
{
	isl_id **ids;
	int i;
	isl_size dim;

	dim = isl_space_dim(space, isl_dim_all);
	if (dim < 0)
		return isl_space_free(space);
	if (dim <= space->n_id)
		return space;

	if (!space->ids) {
		space->ids = isl_calloc_array(space->ctx, isl_id *, dim);
		if (!space->ids)
			goto error;
	} else {
		ids = isl_realloc_array(space->ctx, space->ids, isl_id *, dim);
		if (!ids)
			goto error;
		space->ids = ids;
		for (i = space->n_id; i < dim; ++i)
			space->ids[i] = NULL;
	}

	space->n_id = dim;

	return space;
error:
	isl_space_free(space);
	return NULL;
}

static __isl_give isl_space *set_id(__isl_take isl_space *space,
	enum isl_dim_type type, unsigned pos, __isl_take isl_id *id)
{
	isl_size gpos;

	space = isl_space_cow(space);

	gpos = global_pos(space, type, pos);
	if (gpos < 0)
		goto error;

	if (gpos >= space->n_id) {
		if (!id)
			return space;
		space = extend_ids(space);
		if (!space)
			goto error;
	}

	space->ids[gpos] = id;

	return space;
error:
	isl_id_free(id);
	isl_space_free(space);
	return NULL;
}

static __isl_keep isl_id *get_id(__isl_keep isl_space *space,
				 enum isl_dim_type type, unsigned pos)
{
	isl_size gpos;

	gpos = global_pos(space, type, pos);
	if (gpos < 0)
		return NULL;
	if (gpos >= space->n_id)
		return NULL;
	return space->ids[gpos];
}

/* Return the nested space at the given position.
 */
static __isl_keep isl_space *isl_space_peek_nested(__isl_keep isl_space *space,
	int pos)
{
	if (!space)
		return NULL;
	if (!space->nested[pos])
		isl_die(isl_space_get_ctx(space), isl_error_invalid,
			"no nested space", return NULL);
	return space->nested[pos];
}

static unsigned offset(__isl_keep isl_space *space, enum isl_dim_type type)
{
	switch (type) {
	case isl_dim_param:	return 0;
	case isl_dim_in:	return space->nparam;
	case isl_dim_out:	return space->nparam + space->n_in;
	default:		return 0;
	}
}

static unsigned n(__isl_keep isl_space *space, enum isl_dim_type type)
{
	switch (type) {
	case isl_dim_param:	return space->nparam;
	case isl_dim_in:	return space->n_in;
	case isl_dim_out:	return space->n_out;
	case isl_dim_all:
		return space->nparam + space->n_in + space->n_out;
	default:		return 0;
	}
}

isl_size isl_space_dim(__isl_keep isl_space *space, enum isl_dim_type type)
{
	if (!space)
		return isl_size_error;
	return n(space, type);
}

/* Return the dimensionality of tuple "inner" within the wrapped relation
 * inside tuple "outer".
 */
isl_size isl_space_wrapped_dim(__isl_keep isl_space *space,
	enum isl_dim_type outer, enum isl_dim_type inner)
{
	int pos;

	if (!space)
		return isl_size_error;
	if (outer != isl_dim_in && outer != isl_dim_out)
		isl_die(isl_space_get_ctx(space), isl_error_invalid,
			"only input, output and set tuples "
			"can have nested relations", return isl_size_error);
	pos = outer - isl_dim_in;
	return isl_space_dim(isl_space_peek_nested(space, pos), inner);
}

unsigned isl_space_offset(__isl_keep isl_space *space, enum isl_dim_type type)
{
	if (!space)
		return 0;
	return offset(space, type);
}

static __isl_give isl_space *copy_ids(__isl_take isl_space *dst,
	enum isl_dim_type dst_type, unsigned offset, __isl_keep isl_space *src,
	enum isl_dim_type src_type)
{
	int i;
	isl_id *id;

	if (!dst)
		return NULL;

	for (i = 0; i < n(src, src_type); ++i) {
		id = get_id(src, src_type, i);
		if (!id)
			continue;
		dst = set_id(dst, dst_type, offset + i, isl_id_copy(id));
		if (!dst)
			return NULL;
	}
	return dst;
}

__isl_take isl_space *isl_space_dup(__isl_keep isl_space *space)
{
	isl_space *dup;
	if (!space)
		return NULL;
	dup = isl_space_alloc(space->ctx,
				space->nparam, space->n_in, space->n_out);
	if (!dup)
		return NULL;
	if (space->tuple_id[0] &&
	    !(dup->tuple_id[0] = isl_id_copy(space->tuple_id[0])))
		goto error;
	if (space->tuple_id[1] &&
	    !(dup->tuple_id[1] = isl_id_copy(space->tuple_id[1])))
		goto error;
	if (space->nested[0] &&
	    !(dup->nested[0] = isl_space_copy(space->nested[0])))
		goto error;
	if (space->nested[1] &&
	    !(dup->nested[1] = isl_space_copy(space->nested[1])))
		goto error;
	if (!space->ids)
		return dup;
	dup = copy_ids(dup, isl_dim_param, 0, space, isl_dim_param);
	dup = copy_ids(dup, isl_dim_in, 0, space, isl_dim_in);
	dup = copy_ids(dup, isl_dim_out, 0, space, isl_dim_out);
	return dup;
error:
	isl_space_free(dup);
	return NULL;
}

__isl_give isl_space *isl_space_cow(__isl_take isl_space *space)
{
	if (!space)
		return NULL;

	if (space->ref == 1)
		return space;
	space->ref--;
	return isl_space_dup(space);
}

__isl_give isl_space *isl_space_copy(__isl_keep isl_space *space)
{
	if (!space)
		return NULL;

	space->ref++;
	return space;
}

__isl_null isl_space *isl_space_free(__isl_take isl_space *space)
{
	int i;

	if (!space)
		return NULL;

	if (--space->ref > 0)
		return NULL;

	isl_id_free(space->tuple_id[0]);
	isl_id_free(space->tuple_id[1]);

	isl_space_free(space->nested[0]);
	isl_space_free(space->nested[1]);

	for (i = 0; i < space->n_id; ++i)
		isl_id_free(space->ids[i]);
	free(space->ids);
	isl_ctx_deref(space->ctx);
	
	free(space);

	return NULL;
}

/* Check if "s" is a valid dimension or tuple name.
 * We currently only forbid names that look like a number.
 *
 * s is assumed to be non-NULL.
 */
static int name_ok(isl_ctx *ctx, const char *s)
{
	char *p;
	long dummy;

	dummy = strtol(s, &p, 0);
	if (p != s)
		isl_die(ctx, isl_error_invalid, "name looks like a number",
			return 0);

	return 1;
}

/* Return a copy of the nested space at the given position.
 */
static __isl_keep isl_space *isl_space_get_nested(__isl_keep isl_space *space,
	int pos)
{
	return isl_space_copy(isl_space_peek_nested(space, pos));
}

/* Return the nested space at the given position.
 * This may be either a copy or the nested space itself
 * if there is only one reference to "space".
 * This allows the nested space to be modified inplace
 * if both "space" and the nested space have only a single reference.
 * The caller is not allowed to modify "space" between this call and
 * a subsequent call to isl_space_restore_nested.
 * The only exception is that isl_space_free can be called instead.
 */
static __isl_give isl_space *isl_space_take_nested(__isl_keep isl_space *space,
	int pos)
{
	isl_space *nested;

	if (!space)
		return NULL;
	if (space->ref != 1)
		return isl_space_get_nested(space, pos);
	nested = space->nested[pos];
	space->nested[pos] = NULL;
	return nested;
}

/* Replace the nested space at the given position by "nested",
 * where this nested space of "space" may be missing
 * due to a preceding call to isl_space_take_nested.
 * However, in this case, "space" only has a single reference and
 * then the call to isl_space_cow has no effect.
 */
static __isl_give isl_space *isl_space_restore_nested(
	__isl_take isl_space *space, int pos, __isl_take isl_space *nested)
{
	if (!space || !nested)
		goto error;

	if (space->nested[pos] == nested) {
		isl_space_free(nested);
		return space;
	}

	space = isl_space_cow(space);
	if (!space)
		goto error;
	isl_space_free(space->nested[pos]);
	space->nested[pos] = nested;

	return space;
error:
	isl_space_free(space);
	isl_space_free(nested);
	return NULL;
}

/* Is it possible for the given dimension type to have a tuple id?
 */
static int space_can_have_id(__isl_keep isl_space *space,
	enum isl_dim_type type)
{
	if (!space)
		return 0;
	if (isl_space_is_params(space))
		isl_die(space->ctx, isl_error_invalid,
			"parameter spaces don't have tuple ids", return 0);
	if (isl_space_is_set(space) && type != isl_dim_set)
		isl_die(space->ctx, isl_error_invalid,
			"set spaces can only have a set id", return 0);
	if (type != isl_dim_in && type != isl_dim_out)
		isl_die(space->ctx, isl_error_invalid,
			"only input, output and set tuples can have ids",
			return 0);

	return 1;
}

/* Does the tuple have an id?
 */
isl_bool isl_space_has_tuple_id(__isl_keep isl_space *space,
	enum isl_dim_type type)
{
	if (!space_can_have_id(space, type))
		return isl_bool_error;
	return isl_bool_ok(space->tuple_id[type - isl_dim_in] != NULL);
}

/* Does the domain tuple of the map space "space" have an identifier?
 */
isl_bool isl_space_has_domain_tuple_id(__isl_keep isl_space *space)
{
	if (isl_space_check_is_map(space) < 0)
		return isl_bool_error;
	return isl_space_has_tuple_id(space, isl_dim_in);
}

/* Does the range tuple of the map space "space" have an identifier?
 */
isl_bool isl_space_has_range_tuple_id(__isl_keep isl_space *space)
{
	if (isl_space_check_is_map(space) < 0)
		return isl_bool_error;
	return isl_space_has_tuple_id(space, isl_dim_out);
}

__isl_give isl_id *isl_space_get_tuple_id(__isl_keep isl_space *space,
	enum isl_dim_type type)
{
	int has_id;

	if (!space)
		return NULL;
	has_id = isl_space_has_tuple_id(space, type);
	if (has_id < 0)
		return NULL;
	if (!has_id)
		isl_die(space->ctx, isl_error_invalid,
			"tuple has no id", return NULL);
	return isl_id_copy(space->tuple_id[type - isl_dim_in]);
}

/* Return the identifier of the domain tuple of the map space "space",
 * assuming it has one.
 */
__isl_give isl_id *isl_space_get_domain_tuple_id(
	__isl_keep isl_space *space)
{
	if (isl_space_check_is_map(space) < 0)
		return NULL;
	return isl_space_get_tuple_id(space, isl_dim_in);
}

/* Return the identifier of the range tuple of the map space "space",
 * assuming it has one.
 */
__isl_give isl_id *isl_space_get_range_tuple_id(
	__isl_keep isl_space *space)
{
	if (isl_space_check_is_map(space) < 0)
		return NULL;
	return isl_space_get_tuple_id(space, isl_dim_out);
}

__isl_give isl_space *isl_space_set_tuple_id(__isl_take isl_space *space,
	enum isl_dim_type type, __isl_take isl_id *id)
{
	space = isl_space_cow(space);
	if (!space || !id)
		goto error;
	if (type != isl_dim_in && type != isl_dim_out)
		isl_die(space->ctx, isl_error_invalid,
			"only input, output and set tuples can have names",
			goto error);

	isl_id_free(space->tuple_id[type - isl_dim_in]);
	space->tuple_id[type - isl_dim_in] = id;

	return space;
error:
	isl_id_free(id);
	isl_space_free(space);
	return NULL;
}

/* Replace the identifier of the domain tuple of the map space "space"
 * by "id".
 */
__isl_give isl_space *isl_space_set_domain_tuple_id(
	__isl_take isl_space *space, __isl_take isl_id *id)
{
	if (isl_space_check_is_map(space) < 0)
		space = isl_space_free(space);
	return isl_space_set_tuple_id(space, isl_dim_in, id);
}

/* Replace the identifier of the range tuple of the map space "space"
 * by "id".
 */
__isl_give isl_space *isl_space_set_range_tuple_id(
	__isl_take isl_space *space, __isl_take isl_id *id)
{
	if (isl_space_check_is_map(space) < 0)
		space = isl_space_free(space);
	return isl_space_set_tuple_id(space, isl_dim_out, id);
}

__isl_give isl_space *isl_space_reset_tuple_id(__isl_take isl_space *space,
	enum isl_dim_type type)
{
	space = isl_space_cow(space);
	if (!space)
		return NULL;
	if (type != isl_dim_in && type != isl_dim_out)
		isl_die(space->ctx, isl_error_invalid,
			"only input, output and set tuples can have names",
			goto error);

	isl_id_free(space->tuple_id[type - isl_dim_in]);
	space->tuple_id[type - isl_dim_in] = NULL;

	return space;
error:
	isl_space_free(space);
	return NULL;
}

/* Set the id of the given dimension of "space" to "id".
 * If the dimension already has an id, then it is replaced.
 * If the dimension is a parameter, then we need to change it
 * in the nested spaces (if any) as well.
 */
__isl_give isl_space *isl_space_set_dim_id(__isl_take isl_space *space,
	enum isl_dim_type type, unsigned pos, __isl_take isl_id *id)
{
	space = isl_space_cow(space);
	if (!space || !id)
		goto error;

	if (type == isl_dim_param) {
		int i;

		for (i = 0; i < 2; ++i) {
			if (!space->nested[i])
				continue;
			space->nested[i] =
				isl_space_set_dim_id(space->nested[i],
						type, pos, isl_id_copy(id));
			if (!space->nested[i])
				goto error;
		}
	}

	isl_id_free(get_id(space, type, pos));
	return set_id(space, type, pos, id);
error:
	isl_id_free(id);
	isl_space_free(space);
	return NULL;
}

/* Reset the id of the given dimension of "space".
 * If the dimension already has an id, then it is removed.
 * If the dimension is a parameter, then we need to reset it
 * in the nested spaces (if any) as well.
 */
__isl_give isl_space *isl_space_reset_dim_id(__isl_take isl_space *space,
	enum isl_dim_type type, unsigned pos)
{
	space = isl_space_cow(space);
	if (!space)
		goto error;

	if (type == isl_dim_param) {
		int i;

		for (i = 0; i < 2; ++i) {
			if (!space->nested[i])
				continue;
			space->nested[i] =
				isl_space_reset_dim_id(space->nested[i],
							type, pos);
			if (!space->nested[i])
				goto error;
		}
	}

	isl_id_free(get_id(space, type, pos));
	return set_id(space, type, pos, NULL);
error:
	isl_space_free(space);
	return NULL;
}

isl_bool isl_space_has_dim_id(__isl_keep isl_space *space,
	enum isl_dim_type type, unsigned pos)
{
	if (!space)
		return isl_bool_error;
	return isl_bool_ok(get_id(space, type, pos) != NULL);
}

__isl_give isl_id *isl_space_get_dim_id(__isl_keep isl_space *space,
	enum isl_dim_type type, unsigned pos)
{
	if (!space)
		return NULL;
	if (!get_id(space, type, pos))
		isl_die(space->ctx, isl_error_invalid,
			"dim has no id", return NULL);
	return isl_id_copy(get_id(space, type, pos));
}

__isl_give isl_space *isl_space_set_tuple_name(__isl_take isl_space *space,
	enum isl_dim_type type, const char *s)
{
	isl_id *id;

	if (!space)
		return NULL;

	if (!s)
		return isl_space_reset_tuple_id(space, type);

	if (!name_ok(space->ctx, s))
		goto error;

	id = isl_id_alloc(space->ctx, s, NULL);
	return isl_space_set_tuple_id(space, type, id);
error:
	isl_space_free(space);
	return NULL;
}

/* Does the tuple have a name?
 */
isl_bool isl_space_has_tuple_name(__isl_keep isl_space *space,
	enum isl_dim_type type)
{
	isl_id *id;

	if (!space_can_have_id(space, type))
		return isl_bool_error;
	id = space->tuple_id[type - isl_dim_in];
	return isl_bool_ok(id && id->name);
}

__isl_keep const char *isl_space_get_tuple_name(__isl_keep isl_space *space,
	 enum isl_dim_type type)
{
	isl_id *id;
	if (!space)
		return NULL;
	if (type != isl_dim_in && type != isl_dim_out)
		return NULL;
	id = space->tuple_id[type - isl_dim_in];
	return id ? id->name : NULL;
}

__isl_give isl_space *isl_space_set_dim_name(__isl_take isl_space *space,
				 enum isl_dim_type type, unsigned pos,
				 const char *s)
{
	isl_id *id;

	if (!space)
		return NULL;
	if (!s)
		return isl_space_reset_dim_id(space, type, pos);
	if (!name_ok(space->ctx, s))
		goto error;
	id = isl_id_alloc(space->ctx, s, NULL);
	return isl_space_set_dim_id(space, type, pos, id);
error:
	isl_space_free(space);
	return NULL;
}

/* Does the given dimension have a name?
 */
isl_bool isl_space_has_dim_name(__isl_keep isl_space *space,
	enum isl_dim_type type, unsigned pos)
{
	isl_id *id;

	if (!space)
		return isl_bool_error;
	id = get_id(space, type, pos);
	return isl_bool_ok(id && id->name);
}

__isl_keep const char *isl_space_get_dim_name(__isl_keep isl_space *space,
				 enum isl_dim_type type, unsigned pos)
{
	isl_id *id = get_id(space, type, pos);
	return id ? id->name : NULL;
}

int isl_space_find_dim_by_id(__isl_keep isl_space *space,
	enum isl_dim_type type, __isl_keep isl_id *id)
{
	int i;
	int offset;
	isl_size n;

	n = isl_space_dim(space, type);
	if (n < 0 || !id)
		return -1;

	offset = isl_space_offset(space, type);
	for (i = 0; i < n && offset + i < space->n_id; ++i)
		if (space->ids[offset + i] == id)
			return i;

	return -1;
}

int isl_space_find_dim_by_name(__isl_keep isl_space *space,
	enum isl_dim_type type, const char *name)
{
	int i;
	int offset;
	isl_size n;

	n = isl_space_dim(space, type);
	if (n < 0 || !name)
		return -1;

	offset = isl_space_offset(space, type);
	for (i = 0; i < n && offset + i < space->n_id; ++i) {
		isl_id *id = get_id(space, type, i);
		if (id && id->name && !strcmp(id->name, name))
			return i;
	}

	return -1;
}

/* Reset the user pointer on all identifiers of parameters and tuples
 * of "space".
 */
__isl_give isl_space *isl_space_reset_user(__isl_take isl_space *space)
{
	int i;
	isl_ctx *ctx;
	isl_id *id;
	const char *name;

	if (!space)
		return NULL;

	ctx = isl_space_get_ctx(space);

	for (i = 0; i < space->nparam && i < space->n_id; ++i) {
		if (!isl_id_get_user(space->ids[i]))
			continue;
		space = isl_space_cow(space);
		if (!space)
			return NULL;
		name = isl_id_get_name(space->ids[i]);
		id = isl_id_alloc(ctx, name, NULL);
		isl_id_free(space->ids[i]);
		space->ids[i] = id;
		if (!id)
			return isl_space_free(space);
	}

	for (i = 0; i < 2; ++i) {
		if (!space->tuple_id[i])
			continue;
		if (!isl_id_get_user(space->tuple_id[i]))
			continue;
		space = isl_space_cow(space);
		if (!space)
			return NULL;
		name = isl_id_get_name(space->tuple_id[i]);
		id = isl_id_alloc(ctx, name, NULL);
		isl_id_free(space->tuple_id[i]);
		space->tuple_id[i] = id;
		if (!id)
			return isl_space_free(space);
	}

	for (i = 0; i < 2; ++i) {
		isl_space *nested;

		if (!space->nested[i])
			continue;
		nested = isl_space_take_nested(space, i);
		nested = isl_space_reset_user(nested);
		space = isl_space_restore_nested(space, i, nested);
		if (!space)
			return NULL;
	}

	return space;
}

static __isl_keep isl_id *tuple_id(__isl_keep isl_space *space,
	enum isl_dim_type type)
{
	if (!space)
		return NULL;
	if (type == isl_dim_in)
		return space->tuple_id[0];
	if (type == isl_dim_out)
		return space->tuple_id[1];
	return NULL;
}

static __isl_keep isl_space *nested(__isl_keep isl_space *space,
	enum isl_dim_type type)
{
	if (!space)
		return NULL;
	if (type == isl_dim_in)
		return space->nested[0];
	if (type == isl_dim_out)
		return space->nested[1];
	return NULL;
}

/* Are the two spaces the same, apart from positions and names of parameters?
 */
isl_bool isl_space_has_equal_tuples(__isl_keep isl_space *space1,
	__isl_keep isl_space *space2)
{
	if (!space1 || !space2)
		return isl_bool_error;
	if (space1 == space2)
		return isl_bool_true;
	return isl_space_tuple_is_equal(space1, isl_dim_in,
					space2, isl_dim_in) &&
	       isl_space_tuple_is_equal(space1, isl_dim_out,
					space2, isl_dim_out);
}

/* Check that a match involving "space" was successful.
 * That is, check that "match" is equal to isl_bool_true.
 */
static isl_stat check_match(__isl_keep isl_space *space, isl_bool match)
{
	if (match < 0)
		return isl_stat_error;
	if (!match)
		isl_die(isl_space_get_ctx(space), isl_error_invalid,
			"incompatible spaces", return isl_stat_error);

	return isl_stat_ok;
}

/* Check that the two spaces are the same,
 * apart from positions and names of parameters.
 */
isl_stat isl_space_check_equal_tuples(__isl_keep isl_space *space1,
	__isl_keep isl_space *space2)
{
	isl_bool is_equal;

	is_equal = isl_space_has_equal_tuples(space1, space2);
	return check_match(space1, is_equal);
}

/* Check if the tuple of type "type1" of "space1" is the same as
 * the tuple of type "type2" of "space2".
 *
 * That is, check if the tuples have the same identifier, the same dimension
 * and the same internal structure.
 * The identifiers of the dimensions inside the tuples do not affect the result.
 *
 * Note that this function only checks the tuples themselves.
 * If nested tuples are involved, then we need to be careful not
 * to have result affected by possibly differing parameters
 * in those nested tuples.
 */
isl_bool isl_space_tuple_is_equal(__isl_keep isl_space *space1,
	enum isl_dim_type type1, __isl_keep isl_space *space2,
	enum isl_dim_type type2)
{
	isl_id *id1, *id2;
	isl_space *nested1, *nested2;

	if (!space1 || !space2)
		return isl_bool_error;

	if (space1 == space2 && type1 == type2)
		return isl_bool_true;

	if (n(space1, type1) != n(space2, type2))
		return isl_bool_false;
	id1 = tuple_id(space1, type1);
	id2 = tuple_id(space2, type2);
	if (!id1 ^ !id2)
		return isl_bool_false;
	if (id1 && id1 != id2)
		return isl_bool_false;
	nested1 = nested(space1, type1);
	nested2 = nested(space2, type2);
	if (!nested1 ^ !nested2)
		return isl_bool_false;
	if (nested1 && !isl_space_has_equal_tuples(nested1, nested2))
		return isl_bool_false;
	return isl_bool_true;
}

/* Is the tuple "inner" within the wrapped relation inside tuple "outer"
 * of "space1" equal to tuple "type2" of "space2"?
 */
isl_bool isl_space_wrapped_tuple_is_equal(__isl_keep isl_space *space1,
	enum isl_dim_type outer, enum isl_dim_type inner,
	__isl_keep isl_space *space2, enum isl_dim_type type2)
{
	int pos;
	isl_space *nested;

	if (!space1)
		return isl_bool_error;
	if (outer != isl_dim_in && outer != isl_dim_out)
		isl_die(isl_space_get_ctx(space1), isl_error_invalid,
			"only input, output and set tuples "
			"can have nested relations", return isl_bool_error);
	pos = outer - isl_dim_in;
	nested = isl_space_peek_nested(space1, pos);
	return isl_space_tuple_is_equal(nested, inner, space2, type2);
}

/* Check that the tuple "inner" within the wrapped relation inside tuple "outer"
 * of "space1" is equal to tuple "type2" of "space2".
 */
isl_stat isl_space_check_wrapped_tuple_is_equal(__isl_keep isl_space *space1,
	enum isl_dim_type outer, enum isl_dim_type inner,
	__isl_keep isl_space *space2, enum isl_dim_type type2)
{
	isl_bool is_equal;

	is_equal = isl_space_wrapped_tuple_is_equal(space1, outer, inner,
							space2, type2);
	return check_match(space1, is_equal);
}

static isl_bool match(__isl_keep isl_space *space1, enum isl_dim_type type1,
	__isl_keep isl_space *space2, enum isl_dim_type type2)
{
	int i;
	isl_bool equal;

	if (!space1 || !space2)
		return isl_bool_error;

	if (space1 == space2 && type1 == type2)
		return isl_bool_true;

	equal = isl_space_tuple_is_equal(space1, type1, space2, type2);
	if (equal < 0 || !equal)
		return equal;

	if (!space1->ids && !space2->ids)
		return isl_bool_true;

	for (i = 0; i < n(space1, type1); ++i) {
		if (get_id(space1, type1, i) != get_id(space2, type2, i))
			return isl_bool_false;
	}
	return isl_bool_true;
}

/* Do "space1" and "space2" have the same parameters?
 */
isl_bool isl_space_has_equal_params(__isl_keep isl_space *space1,
	__isl_keep isl_space *space2)
{
	return match(space1, isl_dim_param, space2, isl_dim_param);
}

/* Do "space1" and "space2" have the same identifiers for all
 * the tuple variables?
 */
isl_bool isl_space_has_equal_ids(__isl_keep isl_space *space1,
	__isl_keep isl_space *space2)
{
	isl_bool equal;

	equal = match(space1, isl_dim_in, space2, isl_dim_in);
	if (equal < 0 || !equal)
		return equal;
	return match(space1, isl_dim_out, space2, isl_dim_out);
}

isl_bool isl_space_match(__isl_keep isl_space *space1, enum isl_dim_type type1,
	__isl_keep isl_space *space2, enum isl_dim_type type2)
{
	return match(space1, type1, space2, type2);
}

static void get_ids(__isl_keep isl_space *space, enum isl_dim_type type,
	unsigned first, unsigned n, __isl_keep isl_id **ids)
{
	int i;

	for (i = 0; i < n ; ++i)
		ids[i] = get_id(space, type, first + i);
}

static __isl_give isl_space *space_extend(__isl_take isl_space *space,
			unsigned nparam, unsigned n_in, unsigned n_out)
{
	isl_id **ids = NULL;

	if (!space)
		return NULL;
	if (space->nparam == nparam &&
	    space->n_in == n_in && space->n_out == n_out)
		return space;

	isl_assert(space->ctx, space->nparam <= nparam, goto error);
	isl_assert(space->ctx, space->n_in <= n_in, goto error);
	isl_assert(space->ctx, space->n_out <= n_out, goto error);

	space = isl_space_cow(space);
	if (!space)
		goto error;

	if (space->ids) {
		unsigned n;
		n = nparam + n_in + n_out;
		if (n < nparam || n < n_in || n < n_out)
			isl_die(isl_space_get_ctx(space), isl_error_invalid,
				"overflow in total number of dimensions",
				goto error);
		ids = isl_calloc_array(space->ctx, isl_id *, n);
		if (!ids)
			goto error;
		get_ids(space, isl_dim_param, 0, space->nparam, ids);
		get_ids(space, isl_dim_in, 0, space->n_in, ids + nparam);
		get_ids(space, isl_dim_out, 0, space->n_out,
			ids + nparam + n_in);
		free(space->ids);
		space->ids = ids;
		space->n_id = nparam + n_in + n_out;
	}
	space->nparam = nparam;
	space->n_in = n_in;
	space->n_out = n_out;

	return space;
error:
	free(ids);
	isl_space_free(space);
	return NULL;
}

__isl_give isl_space *isl_space_extend(__isl_take isl_space *space,
	unsigned nparam, unsigned n_in, unsigned n_out)
{
	return space_extend(space, nparam, n_in, n_out);
}

__isl_give isl_space *isl_space_add_dims(__isl_take isl_space *space,
	enum isl_dim_type type, unsigned n)
{
	space = isl_space_reset(space, type);
	if (!space)
		return NULL;
	switch (type) {
	case isl_dim_param:
		space = space_extend(space,
				space->nparam + n, space->n_in, space->n_out);
		if (space && space->nested[0] &&
		    !(space->nested[0] = isl_space_add_dims(space->nested[0],
						    isl_dim_param, n)))
			goto error;
		if (space && space->nested[1] &&
		    !(space->nested[1] = isl_space_add_dims(space->nested[1],
						    isl_dim_param, n)))
			goto error;
		return space;
	case isl_dim_in:
		return space_extend(space,
				space->nparam, space->n_in + n, space->n_out);
	case isl_dim_out:
		return space_extend(space,
				space->nparam, space->n_in, space->n_out + n);
	default:
		isl_die(space->ctx, isl_error_invalid,
			"cannot add dimensions of specified type", goto error);
	}
error:
	isl_space_free(space);
	return NULL;
}

/* Add a parameter with identifier "id" to "space", provided
 * it does not already appear in "space".
 */
__isl_give isl_space *isl_space_add_param_id(__isl_take isl_space *space,
	__isl_take isl_id *id)
{
	isl_size pos;

	if (!space || !id)
		goto error;

	if (isl_space_find_dim_by_id(space, isl_dim_param, id) >= 0) {
		isl_id_free(id);
		return space;
	}

	pos = isl_space_dim(space, isl_dim_param);
	if (pos < 0)
		goto error;
	space = isl_space_add_dims(space, isl_dim_param, 1);
	space = isl_space_set_dim_id(space, isl_dim_param, pos, id);

	return space;
error:
	isl_space_free(space);
	isl_id_free(id);
	return NULL;
}

static int valid_dim_type(enum isl_dim_type type)
{
	switch (type) {
	case isl_dim_param:
	case isl_dim_in:
	case isl_dim_out:
		return 1;
	default:
		return 0;
	}
}

#undef TYPE
#define TYPE	isl_space
#include "check_type_range_templ.c"

/* Insert "n" dimensions of type "type" at position "pos".
 * If we are inserting parameters, then they are also inserted in
 * any nested spaces.
 */
__isl_give isl_space *isl_space_insert_dims(__isl_take isl_space *space,
	enum isl_dim_type type, unsigned pos, unsigned n)
{
	isl_ctx *ctx;
	isl_id **ids = NULL;

	if (!space)
		return NULL;
	if (n == 0)
		return isl_space_reset(space, type);

	ctx = isl_space_get_ctx(space);
	if (!valid_dim_type(type))
		isl_die(ctx, isl_error_invalid,
			"cannot insert dimensions of specified type",
			goto error);

	if (isl_space_check_range(space, type, pos, 0) < 0)
		return isl_space_free(space);

	space = isl_space_cow(space);
	if (!space)
		return NULL;

	if (space->ids) {
		enum isl_dim_type t, o = isl_dim_param;
		int off;
		int s[3];
		ids = isl_calloc_array(ctx, isl_id *,
			     space->nparam + space->n_in + space->n_out + n);
		if (!ids)
			goto error;
		off = 0;
		s[isl_dim_param - o] = space->nparam;
		s[isl_dim_in - o] = space->n_in;
		s[isl_dim_out - o] = space->n_out;
		for (t = isl_dim_param; t <= isl_dim_out; ++t) {
			if (t != type) {
				get_ids(space, t, 0, s[t - o], ids + off);
				off += s[t - o];
			} else {
				get_ids(space, t, 0, pos, ids + off);
				off += pos + n;
				get_ids(space, t, pos, s[t - o] - pos,
					ids + off);
				off += s[t - o] - pos;
			}
		}
		free(space->ids);
		space->ids = ids;
		space->n_id = space->nparam + space->n_in + space->n_out + n;
	}
	switch (type) {
	case isl_dim_param:	space->nparam += n; break;
	case isl_dim_in:	space->n_in += n; break;
	case isl_dim_out:	space->n_out += n; break;
	default:		;
	}
	space = isl_space_reset(space, type);

	if (type == isl_dim_param) {
		if (space && space->nested[0] &&
		    !(space->nested[0] = isl_space_insert_dims(space->nested[0],
						    isl_dim_param, pos, n)))
			goto error;
		if (space && space->nested[1] &&
		    !(space->nested[1] = isl_space_insert_dims(space->nested[1],
						    isl_dim_param, pos, n)))
			goto error;
	}

	return space;
error:
	isl_space_free(space);
	return NULL;
}

__isl_give isl_space *isl_space_move_dims(__isl_take isl_space *space,
	enum isl_dim_type dst_type, unsigned dst_pos,
	enum isl_dim_type src_type, unsigned src_pos, unsigned n)
{
	int i;

	space = isl_space_reset(space, src_type);
	space = isl_space_reset(space, dst_type);
	if (!space)
		return NULL;
	if (n == 0)
		return space;

	if (isl_space_check_range(space, src_type, src_pos, n) < 0)
		return isl_space_free(space);

	if (dst_type == src_type && dst_pos == src_pos)
		return space;

	isl_assert(space->ctx, dst_type != src_type, goto error);

	space = isl_space_cow(space);
	if (!space)
		return NULL;

	if (space->ids) {
		isl_id **ids;
		enum isl_dim_type t, o = isl_dim_param;
		int off;
		int s[3];
		ids = isl_calloc_array(space->ctx, isl_id *,
				 space->nparam + space->n_in + space->n_out);
		if (!ids)
			goto error;
		off = 0;
		s[isl_dim_param - o] = space->nparam;
		s[isl_dim_in - o] = space->n_in;
		s[isl_dim_out - o] = space->n_out;
		for (t = isl_dim_param; t <= isl_dim_out; ++t) {
			if (t == dst_type) {
				get_ids(space, t, 0, dst_pos, ids + off);
				off += dst_pos;
				get_ids(space, src_type, src_pos, n, ids + off);
				off += n;
				get_ids(space, t, dst_pos, s[t - o] - dst_pos,
						ids + off);
				off += s[t - o] - dst_pos;
			} else if (t == src_type) {
				get_ids(space, t, 0, src_pos, ids + off);
				off += src_pos;
				get_ids(space, t, src_pos + n,
					    s[t - o] - src_pos - n, ids + off);
				off += s[t - o] - src_pos - n;
			} else {
				get_ids(space, t, 0, s[t - o], ids + off);
				off += s[t - o];
			}
		}
		free(space->ids);
		space->ids = ids;
		space->n_id = space->nparam + space->n_in + space->n_out;
	}

	switch (dst_type) {
	case isl_dim_param:	space->nparam += n; break;
	case isl_dim_in:	space->n_in += n; break;
	case isl_dim_out:	space->n_out += n; break;
	default:		;
	}

	switch (src_type) {
	case isl_dim_param:	space->nparam -= n; break;
	case isl_dim_in:	space->n_in -= n; break;
	case isl_dim_out:	space->n_out -= n; break;
	default:		;
	}

	if (dst_type != isl_dim_param && src_type != isl_dim_param)
		return space;

	for (i = 0; i < 2; ++i) {
		isl_space *nested;

		if (!space->nested[i])
			continue;
		nested = isl_space_take_nested(space, i);
		nested = isl_space_replace_params(nested, space);
		space = isl_space_restore_nested(space, i, nested);
		if (!space)
			return NULL;
	}

	return space;
error:
	isl_space_free(space);
	return NULL;
}

/* Check that "space1" and "space2" have the same parameters,
 * reporting an error if they do not.
 */
isl_stat isl_space_check_equal_params(__isl_keep isl_space *space1,
	__isl_keep isl_space *space2)
{
	isl_bool equal;

	equal = isl_space_has_equal_params(space1, space2);
	if (equal < 0)
		return isl_stat_error;
	if (!equal)
		isl_die(isl_space_get_ctx(space1), isl_error_invalid,
			"parameters need to match", return isl_stat_error);
	return isl_stat_ok;
}

__isl_give isl_space *isl_space_join(__isl_take isl_space *left,
	__isl_take isl_space *right)
{
	isl_space *space;

	if (isl_space_check_equal_params(left, right) < 0)
		goto error;

	isl_assert(left->ctx,
		isl_space_tuple_is_equal(left, isl_dim_out, right, isl_dim_in),
		goto error);

	space = isl_space_alloc(left->ctx,
				left->nparam, left->n_in, right->n_out);
	if (!space)
		goto error;

	space = copy_ids(space, isl_dim_param, 0, left, isl_dim_param);
	space = copy_ids(space, isl_dim_in, 0, left, isl_dim_in);
	space = copy_ids(space, isl_dim_out, 0, right, isl_dim_out);

	if (space && left->tuple_id[0] &&
	    !(space->tuple_id[0] = isl_id_copy(left->tuple_id[0])))
		goto error;
	if (space && right->tuple_id[1] &&
	    !(space->tuple_id[1] = isl_id_copy(right->tuple_id[1])))
		goto error;
	if (space && left->nested[0] &&
	    !(space->nested[0] = isl_space_copy(left->nested[0])))
		goto error;
	if (space && right->nested[1] &&
	    !(space->nested[1] = isl_space_copy(right->nested[1])))
		goto error;

	isl_space_free(left);
	isl_space_free(right);

	return space;
error:
	isl_space_free(left);
	isl_space_free(right);
	return NULL;
}

/* Given two map spaces { A -> C } and { B -> D }, construct the space
 * { [A -> B] -> [C -> D] }.
 * Given two set spaces { A } and { B }, construct the space { [A -> B] }.
 */
__isl_give isl_space *isl_space_product(__isl_take isl_space *left,
	__isl_take isl_space *right)
{
	isl_space *dom1, *dom2, *nest1, *nest2;
	int is_set;

	if (!left || !right)
		goto error;

	is_set = isl_space_is_set(left);
	if (is_set != isl_space_is_set(right))
		isl_die(isl_space_get_ctx(left), isl_error_invalid,
			"expecting either two set spaces or two map spaces",
			goto error);
	if (is_set)
		return isl_space_range_product(left, right);

	if (isl_space_check_equal_params(left, right) < 0)
		goto error;

	dom1 = isl_space_domain(isl_space_copy(left));
	dom2 = isl_space_domain(isl_space_copy(right));
	nest1 = isl_space_wrap(isl_space_join(isl_space_reverse(dom1), dom2));

	dom1 = isl_space_range(left);
	dom2 = isl_space_range(right);
	nest2 = isl_space_wrap(isl_space_join(isl_space_reverse(dom1), dom2));

	return isl_space_join(isl_space_reverse(nest1), nest2);
error:
	isl_space_free(left);
	isl_space_free(right);
	return NULL;
}

/* Given two spaces { A -> C } and { B -> C }, construct the space
 * { [A -> B] -> C }
 */
__isl_give isl_space *isl_space_domain_product(__isl_take isl_space *left,
	__isl_take isl_space *right)
{
	isl_space *ran, *dom1, *dom2, *nest;

	if (isl_space_check_equal_params(left, right) < 0)
		goto error;

	if (!isl_space_tuple_is_equal(left, isl_dim_out, right, isl_dim_out))
		isl_die(left->ctx, isl_error_invalid,
			"ranges need to match", goto error);

	ran = isl_space_range(isl_space_copy(left));

	dom1 = isl_space_domain(left);
	dom2 = isl_space_domain(right);
	nest = isl_space_wrap(isl_space_join(isl_space_reverse(dom1), dom2));

	return isl_space_join(isl_space_reverse(nest), ran);
error:
	isl_space_free(left);
	isl_space_free(right);
	return NULL;
}

__isl_give isl_space *isl_space_range_product(__isl_take isl_space *left,
	__isl_take isl_space *right)
{
	isl_space *dom, *ran1, *ran2, *nest;

	if (isl_space_check_equal_params(left, right) < 0)
		goto error;

	if (!isl_space_tuple_is_equal(left, isl_dim_in, right, isl_dim_in))
		isl_die(left->ctx, isl_error_invalid,
			"domains need to match", goto error);

	dom = isl_space_domain(isl_space_copy(left));

	ran1 = isl_space_range(left);
	ran2 = isl_space_range(right);
	nest = isl_space_wrap(isl_space_join(isl_space_reverse(ran1), ran2));

	return isl_space_join(isl_space_reverse(dom), nest);
error:
	isl_space_free(left);
	isl_space_free(right);
	return NULL;
}

/* Given a space of the form [A -> B] -> C, return the space A -> C.
 */
__isl_give isl_space *isl_space_domain_factor_domain(
	__isl_take isl_space *space)
{
	isl_space *nested;
	isl_space *domain;

	if (isl_space_check_domain_is_wrapping(space) < 0)
		return isl_space_free(space);

	nested = space->nested[0];
	domain = isl_space_copy(space);
	domain = isl_space_drop_dims(domain, isl_dim_in,
					nested->n_in, nested->n_out);
	if (!domain)
		return isl_space_free(space);
	if (nested->tuple_id[0]) {
		domain->tuple_id[0] = isl_id_copy(nested->tuple_id[0]);
		if (!domain->tuple_id[0])
			goto error;
	}
	if (nested->nested[0]) {
		domain->nested[0] = isl_space_copy(nested->nested[0]);
		if (!domain->nested[0])
			goto error;
	}

	isl_space_free(space);
	return domain;
error:
	isl_space_free(space);
	isl_space_free(domain);
	return NULL;
}

/* Given a space of the form [A -> B] -> C, return the space B -> C.
 */
__isl_give isl_space *isl_space_domain_factor_range(
	__isl_take isl_space *space)
{
	isl_space *nested;
	isl_space *range;

	if (isl_space_check_domain_is_wrapping(space) < 0)
		return isl_space_free(space);

	nested = space->nested[0];
	range = isl_space_copy(space);
	range = isl_space_drop_dims(range, isl_dim_in, 0, nested->n_in);
	if (!range)
		return isl_space_free(space);
	if (nested->tuple_id[1]) {
		range->tuple_id[0] = isl_id_copy(nested->tuple_id[1]);
		if (!range->tuple_id[0])
			goto error;
	}
	if (nested->nested[1]) {
		range->nested[0] = isl_space_copy(nested->nested[1]);
		if (!range->nested[0])
			goto error;
	}

	isl_space_free(space);
	return range;
error:
	isl_space_free(space);
	isl_space_free(range);
	return NULL;
}

/* Internal function that selects the domain of the map that is
 * embedded in either a set space or the range of a map space.
 * In particular, given a space of the form [A -> B], return the space A.
 * Given a space of the form A -> [B -> C], return the space A -> B.
 */
static __isl_give isl_space *range_factor_domain(__isl_take isl_space *space)
{
	isl_space *nested;
	isl_space *domain;

	if (!space)
		return NULL;

	nested = space->nested[1];
	domain = isl_space_copy(space);
	domain = isl_space_drop_dims(domain, isl_dim_out,
					nested->n_in, nested->n_out);
	if (!domain)
		return isl_space_free(space);
	if (nested->tuple_id[0]) {
		domain->tuple_id[1] = isl_id_copy(nested->tuple_id[0]);
		if (!domain->tuple_id[1])
			goto error;
	}
	if (nested->nested[0]) {
		domain->nested[1] = isl_space_copy(nested->nested[0]);
		if (!domain->nested[1])
			goto error;
	}

	isl_space_free(space);
	return domain;
error:
	isl_space_free(space);
	isl_space_free(domain);
	return NULL;
}

/* Given a space of the form A -> [B -> C], return the space A -> B.
 */
__isl_give isl_space *isl_space_range_factor_domain(
	__isl_take isl_space *space)
{
	if (isl_space_check_range_is_wrapping(space) < 0)
		return isl_space_free(space);

	return range_factor_domain(space);
}

/* Given a space of the form [A -> B], return the space A.
 */
static __isl_give isl_space *set_factor_domain(__isl_take isl_space *space)
{
	if (!space)
		return NULL;
	if (!isl_space_is_wrapping(space))
		isl_die(isl_space_get_ctx(space), isl_error_invalid,
			"not a product", return isl_space_free(space));

	return range_factor_domain(space);
}

/* Given a space of the form [A -> B] -> [C -> D], return the space A -> C.
 * Given a space of the form [A -> B], return the space A.
 */
__isl_give isl_space *isl_space_factor_domain(__isl_take isl_space *space)
{
	if (!space)
		return NULL;
	if (isl_space_is_set(space))
		return set_factor_domain(space);
	space = isl_space_domain_factor_domain(space);
	space = isl_space_range_factor_domain(space);
	return space;
}

/* Internal function that selects the range of the map that is
 * embedded in either a set space or the range of a map space.
 * In particular, given a space of the form [A -> B], return the space B.
 * Given a space of the form A -> [B -> C], return the space A -> C.
 */
static __isl_give isl_space *range_factor_range(__isl_take isl_space *space)
{
	isl_space *nested;
	isl_space *range;

	if (!space)
		return NULL;

	nested = space->nested[1];
	range = isl_space_copy(space);
	range = isl_space_drop_dims(range, isl_dim_out, 0, nested->n_in);
	if (!range)
		return isl_space_free(space);
	if (nested->tuple_id[1]) {
		range->tuple_id[1] = isl_id_copy(nested->tuple_id[1]);
		if (!range->tuple_id[1])
			goto error;
	}
	if (nested->nested[1]) {
		range->nested[1] = isl_space_copy(nested->nested[1]);
		if (!range->nested[1])
			goto error;
	}

	isl_space_free(space);
	return range;
error:
	isl_space_free(space);
	isl_space_free(range);
	return NULL;
}

/* Given a space of the form A -> [B -> C], return the space A -> C.
 */
__isl_give isl_space *isl_space_range_factor_range(
	__isl_take isl_space *space)
{
	if (isl_space_check_range_is_wrapping(space) < 0)
		return isl_space_free(space);

	return range_factor_range(space);
}

/* Given a space of the form [A -> B], return the space B.
 */
static __isl_give isl_space *set_factor_range(__isl_take isl_space *space)
{
	if (!space)
		return NULL;
	if (!isl_space_is_wrapping(space))
		isl_die(isl_space_get_ctx(space), isl_error_invalid,
			"not a product", return isl_space_free(space));

	return range_factor_range(space);
}

/* Given a space of the form [A -> B] -> [C -> D], return the space B -> D.
 * Given a space of the form [A -> B], return the space B.
 */
__isl_give isl_space *isl_space_factor_range(__isl_take isl_space *space)
{
	if (!space)
		return NULL;
	if (isl_space_is_set(space))
		return set_factor_range(space);
	space = isl_space_domain_factor_range(space);
	space = isl_space_range_factor_range(space);
	return space;
}

__isl_give isl_space *isl_space_map_from_set(__isl_take isl_space *space)
{
	isl_ctx *ctx;
	isl_id **ids = NULL;
	int n_id;

	if (!space)
		return NULL;
	ctx = isl_space_get_ctx(space);
	if (!isl_space_is_set(space))
		isl_die(ctx, isl_error_invalid, "not a set space", goto error);
	space = isl_space_cow(space);
	if (!space)
		return NULL;
	n_id = space->nparam + space->n_out + space->n_out;
	if (n_id > 0 && space->ids) {
		ids = isl_calloc_array(space->ctx, isl_id *, n_id);
		if (!ids)
			goto error;
		get_ids(space, isl_dim_param, 0, space->nparam, ids);
		get_ids(space, isl_dim_out, 0, space->n_out,
			ids + space->nparam);
	}
	space->n_in = space->n_out;
	if (ids) {
		free(space->ids);
		space->ids = ids;
		space->n_id = n_id;
		space = copy_ids(space, isl_dim_out, 0, space, isl_dim_in);
	}
	isl_id_free(space->tuple_id[0]);
	space->tuple_id[0] = isl_id_copy(space->tuple_id[1]);
	isl_space_free(space->nested[0]);
	space->nested[0] = isl_space_copy(space->nested[1]);
	return space;
error:
	isl_space_free(space);
	return NULL;
}

__isl_give isl_space *isl_space_map_from_domain_and_range(
	__isl_take isl_space *domain, __isl_take isl_space *range)
{
	if (!domain || !range)
		goto error;
	if (!isl_space_is_set(domain))
		isl_die(isl_space_get_ctx(domain), isl_error_invalid,
			"domain is not a set space", goto error);
	if (!isl_space_is_set(range))
		isl_die(isl_space_get_ctx(range), isl_error_invalid,
			"range is not a set space", goto error);
	return isl_space_join(isl_space_reverse(domain), range);
error:
	isl_space_free(domain);
	isl_space_free(range);
	return NULL;
}

static __isl_give isl_space *set_ids(__isl_take isl_space *space,
	enum isl_dim_type type,
	unsigned first, unsigned n, __isl_take isl_id **ids)
{
	int i;

	for (i = 0; i < n ; ++i)
		space = set_id(space, type, first + i, ids[i]);

	return space;
}

__isl_give isl_space *isl_space_reverse(__isl_take isl_space *space)
{
	unsigned t;
	isl_bool equal;
	isl_space *nested;
	isl_id **ids = NULL;
	isl_id *id;

	equal = match(space, isl_dim_in, space, isl_dim_out);
	if (equal < 0)
		return isl_space_free(space);
	if (equal)
		return space;

	space = isl_space_cow(space);
	if (!space)
		return NULL;

	id = space->tuple_id[0];
	space->tuple_id[0] = space->tuple_id[1];
	space->tuple_id[1] = id;

	nested = space->nested[0];
	space->nested[0] = space->nested[1];
	space->nested[1] = nested;

	if (space->ids) {
		int n_id = space->n_in + space->n_out;
		ids = isl_alloc_array(space->ctx, isl_id *, n_id);
		if (n_id && !ids)
			goto error;
		get_ids(space, isl_dim_in, 0, space->n_in, ids);
		get_ids(space, isl_dim_out, 0, space->n_out, ids + space->n_in);
	}

	t = space->n_in;
	space->n_in = space->n_out;
	space->n_out = t;

	if (space->ids) {
		space = set_ids(space, isl_dim_out, 0, space->n_out, ids);
		space = set_ids(space, isl_dim_in, 0, space->n_in,
				ids + space->n_out);
		free(ids);
	}

	return space;
error:
	free(ids);
	isl_space_free(space);
	return NULL;
}

/* Given a space A -> (B -> C), return the corresponding space
 * A -> (C -> B).
 *
 * If the range tuple is named, then the name is only preserved
 * if B and C are equal tuples, in which case the output
 * of this function is identical to the input.
 */
__isl_give isl_space *isl_space_range_reverse(__isl_take isl_space *space)
{
	isl_space *nested;
	isl_bool equal;

	if (isl_space_check_range_is_wrapping(space) < 0)
		return isl_space_free(space);

	nested = isl_space_peek_nested(space, 1);
	equal = isl_space_tuple_is_equal(nested, isl_dim_in,
					nested, isl_dim_out);
	if (equal < 0)
		return isl_space_free(space);

	nested = isl_space_take_nested(space, 1);
	nested = isl_space_reverse(nested);
	space = isl_space_restore_nested(space, 1, nested);
	if (!equal)
		space = isl_space_reset_tuple_id(space, isl_dim_out);

	return space;
}

__isl_give isl_space *isl_space_drop_dims(__isl_take isl_space *space,
	enum isl_dim_type type, unsigned first, unsigned num)
{
	int i;

	if (!space)
		return NULL;

	if (num == 0)
		return isl_space_reset(space, type);

	if (!valid_dim_type(type))
		isl_die(space->ctx, isl_error_invalid,
			"cannot drop dimensions of specified type", goto error);

	if (isl_space_check_range(space, type, first, num) < 0)
		return isl_space_free(space);
	space = isl_space_cow(space);
	if (!space)
		goto error;
	if (space->ids) {
		space = extend_ids(space);
		if (!space)
			goto error;
		for (i = 0; i < num; ++i)
			isl_id_free(get_id(space, type, first + i));
		for (i = first+num; i < n(space, type); ++i)
			set_id(space, type, i - num, get_id(space, type, i));
		switch (type) {
		case isl_dim_param:
			get_ids(space, isl_dim_in, 0, space->n_in,
				space->ids + offset(space, isl_dim_in) - num);
		case isl_dim_in:
			get_ids(space, isl_dim_out, 0, space->n_out,
				space->ids + offset(space, isl_dim_out) - num);
		default:
			;
		}
		space->n_id -= num;
	}
	switch (type) {
	case isl_dim_param:	space->nparam -= num; break;
	case isl_dim_in:	space->n_in -= num; break;
	case isl_dim_out:	space->n_out -= num; break;
	default:		;
	}
	space = isl_space_reset(space, type);
	if (type == isl_dim_param) {
		if (space && space->nested[0] &&
		    !(space->nested[0] = isl_space_drop_dims(space->nested[0],
						    isl_dim_param, first, num)))
			goto error;
		if (space && space->nested[1] &&
		    !(space->nested[1] = isl_space_drop_dims(space->nested[1],
						    isl_dim_param, first, num)))
			goto error;
	}
	return space;
error:
	isl_space_free(space);
	return NULL;
}

__isl_give isl_space *isl_space_drop_inputs(__isl_take isl_space *space,
		unsigned first, unsigned n)
{
	if (!space)
		return NULL;
	return isl_space_drop_dims(space, isl_dim_in, first, n);
}

__isl_give isl_space *isl_space_drop_outputs(__isl_take isl_space *space,
		unsigned first, unsigned n)
{
	if (!space)
		return NULL;
	return isl_space_drop_dims(space, isl_dim_out, first, n);
}

/* Remove all parameters from "space".
 */
__isl_give isl_space *isl_space_drop_all_params(__isl_take isl_space *space)
{
	isl_size nparam;

	nparam = isl_space_dim(space, isl_dim_param);
	if (nparam < 0)
		return isl_space_free(space);
	return isl_space_drop_dims(space, isl_dim_param, 0, nparam);
}

__isl_give isl_space *isl_space_domain(__isl_take isl_space *space)
{
	if (!space)
		return NULL;
	space = isl_space_drop_dims(space, isl_dim_out, 0, space->n_out);
	space = isl_space_reverse(space);
	space = mark_as_set(space);
	return space;
}

__isl_give isl_space *isl_space_from_domain(__isl_take isl_space *space)
{
	if (!space)
		return NULL;
	if (!isl_space_is_set(space))
		isl_die(isl_space_get_ctx(space), isl_error_invalid,
			"not a set space", goto error);
	space = isl_space_reverse(space);
	space = isl_space_reset(space, isl_dim_out);
	return space;
error:
	isl_space_free(space);
	return NULL;
}

__isl_give isl_space *isl_space_range(__isl_take isl_space *space)
{
	if (!space)
		return NULL;
	space = isl_space_drop_dims(space, isl_dim_in, 0, space->n_in);
	space = mark_as_set(space);
	return space;
}

__isl_give isl_space *isl_space_from_range(__isl_take isl_space *space)
{
	if (!space)
		return NULL;
	if (!isl_space_is_set(space))
		isl_die(isl_space_get_ctx(space), isl_error_invalid,
			"not a set space", goto error);
	return isl_space_reset(space, isl_dim_in);
error:
	isl_space_free(space);
	return NULL;
}

/* Given a map space A -> B, return the map space [A -> B] -> A.
 */
__isl_give isl_space *isl_space_domain_map(__isl_take isl_space *space)
{
	isl_space *domain;

	domain = isl_space_from_range(isl_space_domain(isl_space_copy(space)));
	space = isl_space_from_domain(isl_space_wrap(space));
	space = isl_space_join(space, domain);

	return space;
}

/* Given a map space A -> B, return the map space [A -> B] -> B.
 */
__isl_give isl_space *isl_space_range_map(__isl_take isl_space *space)
{
	isl_space *range;

	range = isl_space_from_range(isl_space_range(isl_space_copy(space)));
	space = isl_space_from_domain(isl_space_wrap(space));
	space = isl_space_join(space, range);

	return space;
}

__isl_give isl_space *isl_space_params(__isl_take isl_space *space)
{
	isl_size n_in, n_out;

	if (isl_space_is_params(space))
		return space;
	n_in = isl_space_dim(space, isl_dim_in);
	n_out = isl_space_dim(space, isl_dim_out);
	if (n_in < 0 || n_out < 0)
		return isl_space_free(space);
	space = isl_space_drop_dims(space, isl_dim_in, 0, n_in);
	space = isl_space_drop_dims(space, isl_dim_out, 0, n_out);
	space = mark_as_params(space);
	return space;
}

__isl_give isl_space *isl_space_set_from_params(__isl_take isl_space *space)
{
	if (!space)
		return NULL;
	if (!isl_space_is_params(space))
		isl_die(isl_space_get_ctx(space), isl_error_invalid,
			"not a parameter space", goto error);
	return isl_space_reset(space, isl_dim_set);
error:
	isl_space_free(space);
	return NULL;
}

/* Add an unnamed tuple of dimension "dim" to "space".
 * This requires "space" to be a parameter or set space.
 *
 * In particular, if "space" is a parameter space, then return
 * a set space with the given dimension.
 * If "space" is a set space, then return a map space
 * with "space" as domain and a range of the given dimension.
 */
__isl_give isl_space *isl_space_add_unnamed_tuple_ui(
	__isl_take isl_space *space, unsigned dim)
{
	isl_bool is_params, is_set;

	is_params = isl_space_is_params(space);
	is_set = isl_space_is_set(space);
	if (is_params < 0 || is_set < 0)
		return isl_space_free(space);
	if (!is_params && !is_set)
		isl_die(isl_space_get_ctx(space), isl_error_invalid,
			"cannot add tuple to map space",
			return isl_space_free(space));
	if (is_params)
		space = isl_space_set_from_params(space);
	else
		space = isl_space_from_domain(space);
	space = isl_space_add_dims(space, isl_dim_out, dim);
	return space;
}

/* Add a tuple of dimension "dim" and with tuple identifier "tuple_id"
 * to "space".
 * This requires "space" to be a parameter or set space.
 */
__isl_give isl_space *isl_space_add_named_tuple_id_ui(
	__isl_take isl_space *space, __isl_take isl_id *tuple_id, unsigned dim)
{
	space = isl_space_add_unnamed_tuple_ui(space, dim);
	space = isl_space_set_tuple_id(space, isl_dim_out, tuple_id);
	return space;
}

/* Check that the identifiers in "tuple" do not appear as parameters
 * in "space".
 */
static isl_stat check_fresh_params(__isl_keep isl_space *space,
	__isl_keep isl_multi_id *tuple)
{
	int i;
	isl_size n;

	n = isl_multi_id_size(tuple);
	if (n < 0)
		return isl_stat_error;
	for (i = 0; i < n; ++i) {
		isl_id *id;
		int pos;

		id = isl_multi_id_get_at(tuple, i);
		if (!id)
			return isl_stat_error;
		pos = isl_space_find_dim_by_id(space, isl_dim_param, id);
		isl_id_free(id);
		if (pos >= 0)
			isl_die(isl_space_get_ctx(space), isl_error_invalid,
				"parameters not unique", return isl_stat_error);
	}

	return isl_stat_ok;
}

/* Add the identifiers in "tuple" as parameters of "space"
 * that are known to be fresh.
 */
static __isl_give isl_space *add_bind_params(__isl_take isl_space *space,
	__isl_keep isl_multi_id *tuple)
{
	int i;
	isl_size first, n;

	first = isl_space_dim(space, isl_dim_param);
	n = isl_multi_id_size(tuple);
	if (first < 0 || n < 0)
		return isl_space_free(space);
	space = isl_space_add_dims(space, isl_dim_param, n);
	for (i = 0; i < n; ++i) {
		isl_id *id;

		id = isl_multi_id_get_at(tuple, i);
		space = isl_space_set_dim_id(space,
						isl_dim_param, first + i, id);
	}

	return space;
}

/* Internal function that removes the set tuple of "space",
 * which is assumed to correspond to the range space of "tuple", and
 * adds the identifiers in "tuple" as fresh parameters.
 * In other words, the set dimensions of "space" are reinterpreted
 * as parameters, but stay in the same global positions.
 */
__isl_give isl_space *isl_space_bind_set(__isl_take isl_space *space,
	__isl_keep isl_multi_id *tuple)
{
	isl_space *tuple_space;

	if (isl_space_check_is_set(space) < 0)
		return isl_space_free(space);
	tuple_space = isl_multi_id_peek_space(tuple);
	if (isl_space_check_equal_tuples(tuple_space, space) < 0)
		return isl_space_free(space);
	if (check_fresh_params(space, tuple) < 0)
		return isl_space_free(space);
	space = isl_space_params(space);
	space = add_bind_params(space, tuple);
	return space;
}

/* Internal function that removes the domain tuple of the map space "space",
 * which is assumed to correspond to the range space of "tuple", and
 * adds the identifiers in "tuple" as fresh parameters.
 * In other words, the domain dimensions of "space" are reinterpreted
 * as parameters, but stay in the same global positions.
 */
__isl_give isl_space *isl_space_bind_map_domain(__isl_take isl_space *space,
	__isl_keep isl_multi_id *tuple)
{
	isl_space *tuple_space;

	if (isl_space_check_is_map(space) < 0)
		return isl_space_free(space);
	tuple_space = isl_multi_id_peek_space(tuple);
	if (isl_space_check_domain_tuples(tuple_space, space) < 0)
		return isl_space_free(space);
	if (check_fresh_params(space, tuple) < 0)
		return isl_space_free(space);
	space = isl_space_range(space);
	space = add_bind_params(space, tuple);
	return space;
}

/* Internal function that, given a space of the form [A -> B] -> C and
 * a tuple of identifiers in A, returns a space B -> C with
 * the identifiers in "tuple" added as fresh parameters.
 * In other words, the domain dimensions of the wrapped relation
 * in the domain of "space" are reinterpreted
 * as parameters, but stay in the same global positions.
 */
__isl_give isl_space *isl_space_bind_domain_wrapped_domain(
	__isl_take isl_space *space, __isl_keep isl_multi_id *tuple)
{
	isl_space *tuple_space;

	if (isl_space_check_is_map(space) < 0)
		return isl_space_free(space);
	tuple_space = isl_multi_id_peek_space(tuple);
	if (isl_space_check_domain_wrapped_domain_tuples(tuple_space,
							space) < 0)
		  return isl_space_free(space);
	if (check_fresh_params(space, tuple) < 0)
		return isl_space_free(space);
	space = isl_space_domain_factor_range(space);
	space = add_bind_params(space, tuple);
	return space;
}

/* Insert a domain tuple in "space" corresponding to the set space "domain".
 * In particular, if "space" is a parameter space, then the result
 * is the set space "domain" combined with the parameters of "space".
 * If "space" is a set space, then the result
 * is a map space with "domain" as domain and the original space as range.
 */
static __isl_give isl_space *isl_space_insert_domain(
	__isl_take isl_space *space, __isl_take isl_space *domain)
{
	isl_bool is_params;

	domain = isl_space_replace_params(domain, space);

	is_params = isl_space_is_params(space);
	if (is_params < 0) {
		isl_space_free(domain);
		space = isl_space_free(space);
	} else if (is_params) {
		isl_space_free(space);
		space = domain;
	} else {
		space = isl_space_map_from_domain_and_range(domain, space);
	}
	return space;
}

/* Internal function that introduces a domain in "space"
 * corresponding to the range space of "tuple".
 * In particular, if "space" is a parameter space, then the result
 * is a set space.  If "space" is a set space, then the result
 * is a map space with the original space as range.
 * Parameters that correspond to the identifiers in "tuple" are removed.
 *
 * The parameters are removed in reverse order (under the assumption
 * that they appear in the same order in "multi") because
 * it is slightly more efficient to remove parameters at the end.
 *
 * For pretty-printing purposes, the identifiers of the set dimensions
 * of the introduced domain are set to the identifiers in "tuple".
 */
__isl_give isl_space *isl_space_unbind_params_insert_domain(
	__isl_take isl_space *space, __isl_keep isl_multi_id *tuple)
{
	int i;
	isl_size n;
	isl_space *tuple_space;

	n = isl_multi_id_size(tuple);
	if (!space || n < 0)
		return isl_space_free(space);
	for (i = n - 1; i >= 0; --i) {
		isl_id *id;
		int pos;

		id = isl_multi_id_get_id(tuple, i);
		if (!id)
			return isl_space_free(space);
		pos = isl_space_find_dim_by_id(space, isl_dim_param, id);
		isl_id_free(id);
		if (pos < 0)
			continue;
		space = isl_space_drop_dims(space, isl_dim_param, pos, 1);
	}
	tuple_space = isl_multi_id_get_space(tuple);
	for (i = 0; i < n; ++i) {
		isl_id *id;

		id = isl_multi_id_get_id(tuple, i);
		tuple_space = isl_space_set_dim_id(tuple_space,
						    isl_dim_set, i, id);
	}
	return isl_space_insert_domain(space, tuple_space);
}

__isl_give isl_space *isl_space_underlying(__isl_take isl_space *space,
	unsigned n_div)
{
	int i;
	isl_bool is_set;

	is_set = isl_space_is_set(space);
	if (is_set < 0)
		return isl_space_free(space);
	if (n_div == 0 && is_set &&
	    space->nparam == 0 && space->n_in == 0 && space->n_id == 0)
		return isl_space_reset(space, isl_dim_out);
	space = isl_space_cow(space);
	if (!space)
		return NULL;
	space->n_out += space->nparam + space->n_in + n_div;
	space->nparam = 0;
	space->n_in = 0;

	for (i = 0; i < space->n_id; ++i)
		isl_id_free(get_id(space, isl_dim_out, i));
	space->n_id = 0;
	space = isl_space_reset(space, isl_dim_in);
	space = isl_space_reset(space, isl_dim_out);
	space = mark_as_set(space);

	return space;
}

/* Are the two spaces the same, including positions and names of parameters?
 */
isl_bool isl_space_is_equal(__isl_keep isl_space *space1,
	__isl_keep isl_space *space2)
{
	isl_bool equal;

	if (!space1 || !space2)
		return isl_bool_error;
	if (space1 == space2)
		return isl_bool_true;
	equal = isl_space_has_equal_params(space1, space2);
	if (equal < 0 || !equal)
		return equal;
	return isl_space_has_equal_tuples(space1, space2);
}

/* Do the tuples of "space1" correspond to those of the domain of "space2"?
 * That is, is "space1" equal to the domain of "space2", ignoring parameters.
 *
 * "space2" is allowed to be a set space, in which case "space1"
 * should be a parameter space.
 */
isl_bool isl_space_has_domain_tuples(__isl_keep isl_space *space1,
	__isl_keep isl_space *space2)
{
	isl_bool is_set;

	is_set = isl_space_is_set(space1);
	if (is_set < 0 || !is_set)
		return is_set;
	return isl_space_tuple_is_equal(space1, isl_dim_set,
					space2, isl_dim_in);
}

/* Do the tuples of "space1" correspond to those of the range of "space2"?
 * That is, is "space1" equal to the range of "space2", ignoring parameters.
 *
 * "space2" is allowed to be the space of a set,
 * in which case it should be equal to "space1", ignoring parameters.
 */
isl_bool isl_space_has_range_tuples(__isl_keep isl_space *space1,
	__isl_keep isl_space *space2)
{
	isl_bool is_set;

	is_set = isl_space_is_set(space1);
	if (is_set < 0 || !is_set)
		return is_set;
	return isl_space_tuple_is_equal(space1, isl_dim_set,
					space2, isl_dim_out);
}

/* Check that the tuples of "space1" correspond to those
 * of the domain of "space2".
 * That is, check that "space1" is equal to the domain of "space2",
 * ignoring parameters.
 */
isl_stat isl_space_check_domain_tuples(__isl_keep isl_space *space1,
	__isl_keep isl_space *space2)
{
	isl_bool is_equal;

	is_equal = isl_space_has_domain_tuples(space1, space2);
	return check_match(space1, is_equal);
}

/* Check that the tuples of "space1" correspond to those
 * of the domain of the wrapped relation in the domain of "space2".
 * That is, check that "space1" is equal to this domain,
 * ignoring parameters.
 */
isl_stat isl_space_check_domain_wrapped_domain_tuples(
	__isl_keep isl_space *space1, __isl_keep isl_space *space2)
{
	isl_space *domain;
	isl_stat r;

	domain = isl_space_unwrap(isl_space_domain(isl_space_copy(space2)));
	r = isl_space_check_domain_tuples(space1, domain);
	isl_space_free(domain);

	return r;
}

/* Is space1 equal to the domain of space2?
 *
 * In the internal version we also allow space2 to be the space of a set,
 * provided space1 is a parameter space.
 */
isl_bool isl_space_is_domain_internal(__isl_keep isl_space *space1,
	__isl_keep isl_space *space2)
{
	isl_bool equal_params;

	if (!space1 || !space2)
		return isl_bool_error;
	equal_params = isl_space_has_equal_params(space1, space2);
	if (equal_params < 0 || !equal_params)
		return equal_params;
	return isl_space_has_domain_tuples(space1, space2);
}

/* Is space1 equal to the domain of space2?
 */
isl_bool isl_space_is_domain(__isl_keep isl_space *space1,
	__isl_keep isl_space *space2)
{
	if (!space2)
		return isl_bool_error;
	if (!isl_space_is_map(space2))
		return isl_bool_false;
	return isl_space_is_domain_internal(space1, space2);
}

/* Is space1 equal to the range of space2?
 *
 * In the internal version, space2 is allowed to be the space of a set,
 * in which case it should be equal to space1.
 */
isl_bool isl_space_is_range_internal(__isl_keep isl_space *space1,
	__isl_keep isl_space *space2)
{
	isl_bool equal_params;

	if (!space1 || !space2)
		return isl_bool_error;
	equal_params = isl_space_has_equal_params(space1, space2);
	if (equal_params < 0 || !equal_params)
		return equal_params;
	return isl_space_has_range_tuples(space1, space2);
}

/* Is space1 equal to the range of space2?
 */
isl_bool isl_space_is_range(__isl_keep isl_space *space1,
	__isl_keep isl_space *space2)
{
	if (!space2)
		return isl_bool_error;
	if (!isl_space_is_map(space2))
		return isl_bool_false;
	return isl_space_is_range_internal(space1, space2);
}

/* Update "hash" by hashing in the parameters of "space".
 */
static uint32_t isl_hash_params(uint32_t hash, __isl_keep isl_space *space)
{
	int i;
	isl_id *id;

	if (!space)
		return hash;

	isl_hash_byte(hash, space->nparam % 256);

	for (i = 0; i < space->nparam; ++i) {
		id = get_id(space, isl_dim_param, i);
		hash = isl_hash_id(hash, id);
	}

	return hash;
}

/* Update "hash" by hashing in the tuples of "space".
 * Changes in this function should be reflected in isl_hash_tuples_domain.
 */
static uint32_t isl_hash_tuples(uint32_t hash, __isl_keep isl_space *space)
{
	isl_id *id;

	if (!space)
		return hash;

	isl_hash_byte(hash, space->n_in % 256);
	isl_hash_byte(hash, space->n_out % 256);

	id = tuple_id(space, isl_dim_in);
	hash = isl_hash_id(hash, id);
	id = tuple_id(space, isl_dim_out);
	hash = isl_hash_id(hash, id);

	hash = isl_hash_tuples(hash, space->nested[0]);
	hash = isl_hash_tuples(hash, space->nested[1]);

	return hash;
}

/* Update "hash" by hashing in the domain tuple of "space".
 * The result of this function is equal to the result of applying
 * isl_hash_tuples to the domain of "space".
 */
static uint32_t isl_hash_tuples_domain(uint32_t hash,
	__isl_keep isl_space *space)
{
	isl_id *id;

	if (!space)
		return hash;

	isl_hash_byte(hash, 0);
	isl_hash_byte(hash, space->n_in % 256);

	hash = isl_hash_id(hash, &isl_id_none);
	id = tuple_id(space, isl_dim_in);
	hash = isl_hash_id(hash, id);

	hash = isl_hash_tuples(hash, space->nested[0]);

	return hash;
}

/* Return a hash value that digests the tuples of "space",
 * i.e., that ignores the parameters.
 * Changes in this function should be reflected
 * in isl_space_get_tuple_domain_hash.
 */
uint32_t isl_space_get_tuple_hash(__isl_keep isl_space *space)
{
	uint32_t hash;

	if (!space)
		return 0;

	hash = isl_hash_init();
	hash = isl_hash_tuples(hash, space);

	return hash;
}

/* Return the hash value of "space".
 */
uint32_t isl_space_get_full_hash(__isl_keep isl_space *space)
{
	uint32_t hash;

	if (!space)
		return 0;

	hash = isl_hash_init();
	hash = isl_hash_params(hash, space);
	hash = isl_hash_tuples(hash, space);

	return hash;
}

/* Return the hash value of the domain tuple of "space".
 * That is, isl_space_get_tuple_domain_hash(space) is equal to
 * isl_space_get_tuple_hash(isl_space_domain(space)).
 */
uint32_t isl_space_get_tuple_domain_hash(__isl_keep isl_space *space)
{
	uint32_t hash;

	if (!space)
		return 0;

	hash = isl_hash_init();
	hash = isl_hash_tuples_domain(hash, space);

	return hash;
}

/* Is "space" the space of a set wrapping a map space?
 */
isl_bool isl_space_is_wrapping(__isl_keep isl_space *space)
{
	if (!space)
		return isl_bool_error;

	if (!isl_space_is_set(space))
		return isl_bool_false;

	return isl_bool_ok(space->nested[1] != NULL);
}

/* Is "space" the space of a map where the domain is a wrapped map space?
 */
isl_bool isl_space_domain_is_wrapping(__isl_keep isl_space *space)
{
	if (!space)
		return isl_bool_error;

	if (isl_space_is_set(space))
		return isl_bool_false;

	return isl_bool_ok(space->nested[0] != NULL);
}

/* Is "space" the space of a map where the range is a wrapped map space?
 */
isl_bool isl_space_range_is_wrapping(__isl_keep isl_space *space)
{
	if (!space)
		return isl_bool_error;

	if (isl_space_is_set(space))
		return isl_bool_false;

	return isl_bool_ok(space->nested[1] != NULL);
}

/* Is "space" a product of two spaces?
 * That is, is it a wrapping set space or a map space
 * with wrapping domain and range?
 */
isl_bool isl_space_is_product(__isl_keep isl_space *space)
{
	isl_bool is_set;
	isl_bool is_product;

	is_set = isl_space_is_set(space);
	if (is_set < 0)
		return isl_bool_error;
	if (is_set)
		return isl_space_is_wrapping(space);
	is_product = isl_space_domain_is_wrapping(space);
	if (is_product < 0 || !is_product)
		return is_product;
	return isl_space_range_is_wrapping(space);
}

__isl_give isl_space *isl_space_wrap(__isl_take isl_space *space)
{
	isl_space *wrap;

	if (!space)
		return NULL;

	wrap = isl_space_set_alloc(space->ctx,
				    space->nparam, space->n_in + space->n_out);

	wrap = copy_ids(wrap, isl_dim_param, 0, space, isl_dim_param);
	wrap = copy_ids(wrap, isl_dim_set, 0, space, isl_dim_in);
	wrap = copy_ids(wrap, isl_dim_set, space->n_in, space, isl_dim_out);

	if (!wrap)
		goto error;

	wrap->nested[1] = space;

	return wrap;
error:
	isl_space_free(space);
	return NULL;
}

__isl_give isl_space *isl_space_unwrap(__isl_take isl_space *space)
{
	isl_space *unwrap;

	if (!space)
		return NULL;

	if (!isl_space_is_wrapping(space))
		isl_die(space->ctx, isl_error_invalid, "not a wrapping space",
			goto error);

	unwrap = isl_space_copy(space->nested[1]);
	isl_space_free(space);

	return unwrap;
error:
	isl_space_free(space);
	return NULL;
}

isl_bool isl_space_is_named_or_nested(__isl_keep isl_space *space,
	enum isl_dim_type type)
{
	if (type != isl_dim_in && type != isl_dim_out)
		return isl_bool_false;
	if (!space)
		return isl_bool_error;
	if (space->tuple_id[type - isl_dim_in])
		return isl_bool_true;
	if (space->nested[type - isl_dim_in])
		return isl_bool_true;
	return isl_bool_false;
}

isl_bool isl_space_may_be_set(__isl_keep isl_space *space)
{
	isl_bool nested;
	isl_size n_in;

	if (!space)
		return isl_bool_error;
	if (isl_space_is_set(space))
		return isl_bool_true;
	n_in = isl_space_dim(space, isl_dim_in);
	if (n_in < 0)
		return isl_bool_error;
	if (n_in != 0)
		return isl_bool_false;
	nested = isl_space_is_named_or_nested(space, isl_dim_in);
	if (nested < 0 || nested)
		return isl_bool_not(nested);
	return isl_bool_true;
}

__isl_give isl_space *isl_space_reset(__isl_take isl_space *space,
	enum isl_dim_type type)
{
	if (!isl_space_is_named_or_nested(space, type))
		return space;

	space = isl_space_cow(space);
	if (!space)
		return NULL;

	isl_id_free(space->tuple_id[type - isl_dim_in]);
	space->tuple_id[type - isl_dim_in] = NULL;
	isl_space_free(space->nested[type - isl_dim_in]);
	space->nested[type - isl_dim_in] = NULL;

	return space;
}

__isl_give isl_space *isl_space_flatten(__isl_take isl_space *space)
{
	if (!space)
		return NULL;
	if (!space->nested[0] && !space->nested[1])
		return space;

	if (space->nested[0])
		space = isl_space_reset(space, isl_dim_in);
	if (space && space->nested[1])
		space = isl_space_reset(space, isl_dim_out);

	return space;
}

__isl_give isl_space *isl_space_flatten_domain(__isl_take isl_space *space)
{
	if (!space)
		return NULL;
	if (!space->nested[0])
		return space;

	return isl_space_reset(space, isl_dim_in);
}

__isl_give isl_space *isl_space_flatten_range(__isl_take isl_space *space)
{
	if (!space)
		return NULL;
	if (!space->nested[1])
		return space;

	return isl_space_reset(space, isl_dim_out);
}

/* Replace the parameters of dst by those of src.
 */
__isl_give isl_space *isl_space_replace_params(__isl_take isl_space *dst,
	__isl_keep isl_space *src)
{
	isl_size dst_dim, src_dim;
	isl_bool equal_params;
	enum isl_dim_type type = isl_dim_param;

	equal_params = isl_space_has_equal_params(dst, src);
	if (equal_params < 0)
		return isl_space_free(dst);
	if (equal_params)
		return dst;

	dst = isl_space_cow(dst);

	dst_dim = isl_space_dim(dst, type);
	src_dim = isl_space_dim(src, type);
	if (dst_dim < 0 || src_dim < 0)
		goto error;

	dst = isl_space_drop_dims(dst, type, 0, dst_dim);
	dst = isl_space_add_dims(dst, type, src_dim);
	dst = copy_ids(dst, type, 0, src, type);

	if (dst) {
		int i;
		for (i = 0; i <= 1; ++i) {
			isl_space *nested;

			if (!dst->nested[i])
				continue;
			nested = isl_space_take_nested(dst, i);
			nested = isl_space_replace_params(nested, src);
			dst = isl_space_restore_nested(dst, i, nested);
			if (!dst)
				return NULL;
		}
	}

	return dst;
error:
	isl_space_free(dst);
	return NULL;
}

/* Given two tuples ("dst_type" in "dst" and "src_type" in "src")
 * of the same size, check if any of the dimensions in the "dst" tuple
 * have no identifier, while the corresponding dimensions in "src"
 * does have an identifier,
 * If so, copy the identifier over to "dst".
 */
__isl_give isl_space *isl_space_copy_ids_if_unset(__isl_take isl_space *dst,
	enum isl_dim_type dst_type, __isl_keep isl_space *src,
	enum isl_dim_type src_type)
{
	int i;
	isl_size n;

	n = isl_space_dim(dst, dst_type);
	if (n < 0)
		return isl_space_free(dst);
	for (i = 0; i < n; ++i) {
		isl_bool set;
		isl_id *id;

		set = isl_space_has_dim_id(dst, dst_type, i);
		if (set < 0)
			return isl_space_free(dst);
		if (set)
			continue;

		set = isl_space_has_dim_id(src, src_type, i);
		if (set < 0)
			return isl_space_free(dst);
		if (!set)
			continue;

		id = isl_space_get_dim_id(src, src_type, i);
		dst = isl_space_set_dim_id(dst, dst_type, i, id);
	}

	return dst;
}

/* Given a space "space" of a set, create a space
 * for the lift of the set.  In particular, the result
 * is of the form lifted[space -> local[..]], with n_local variables in the
 * range of the wrapped map.
 */
__isl_give isl_space *isl_space_lift(__isl_take isl_space *space,
	unsigned n_local)
{
	isl_space *local_space;

	if (!space)
		return NULL;

	local_space = isl_space_dup(space);
	local_space = isl_space_drop_dims(local_space, isl_dim_set, 0,
					space->n_out);
	local_space = isl_space_add_dims(local_space, isl_dim_set, n_local);
	local_space = isl_space_set_tuple_name(local_space,
						isl_dim_set, "local");
	space = isl_space_join(isl_space_from_domain(space),
			    isl_space_from_range(local_space));
	space = isl_space_wrap(space);
	space = isl_space_set_tuple_name(space, isl_dim_set, "lifted");

	return space;
}

isl_bool isl_space_can_zip(__isl_keep isl_space *space)
{
	isl_bool is_set;

	is_set = isl_space_is_set(space);
	if (is_set < 0)
		return isl_bool_error;
	if (is_set)
		return isl_bool_false;
	return isl_space_is_product(space);
}

__isl_give isl_space *isl_space_zip(__isl_take isl_space *space)
{
	isl_space *dom, *ran;
	isl_space *dom_dom, *dom_ran, *ran_dom, *ran_ran;

	if (!isl_space_can_zip(space))
		isl_die(space->ctx, isl_error_invalid, "space cannot be zipped",
			goto error);

	if (!space)
		return NULL;
	dom = isl_space_unwrap(isl_space_domain(isl_space_copy(space)));
	ran = isl_space_unwrap(isl_space_range(space));
	dom_dom = isl_space_domain(isl_space_copy(dom));
	dom_ran = isl_space_range(dom);
	ran_dom = isl_space_domain(isl_space_copy(ran));
	ran_ran = isl_space_range(ran);
	dom = isl_space_join(isl_space_from_domain(dom_dom),
			   isl_space_from_range(ran_dom));
	ran = isl_space_join(isl_space_from_domain(dom_ran),
			   isl_space_from_range(ran_ran));
	return isl_space_join(isl_space_from_domain(isl_space_wrap(dom)),
			    isl_space_from_range(isl_space_wrap(ran)));
error:
	isl_space_free(space);
	return NULL;
}

/* Can we apply isl_space_curry to "space"?
 * That is, does is it have a map space with a nested relation in its domain?
 */
isl_bool isl_space_can_curry(__isl_keep isl_space *space)
{
	return isl_space_domain_is_wrapping(space);
}

/* Given a space (A -> B) -> C, return the corresponding space
 * A -> (B -> C).
 */
__isl_give isl_space *isl_space_curry(__isl_take isl_space *space)
{
	isl_space *dom, *ran;
	isl_space *dom_dom, *dom_ran;

	if (!space)
		return NULL;

	if (!isl_space_can_curry(space))
		isl_die(space->ctx, isl_error_invalid,
			"space cannot be curried", goto error);

	dom = isl_space_unwrap(isl_space_domain(isl_space_copy(space)));
	ran = isl_space_range(space);
	dom_dom = isl_space_domain(isl_space_copy(dom));
	dom_ran = isl_space_range(dom);
	ran = isl_space_join(isl_space_from_domain(dom_ran),
			   isl_space_from_range(ran));
	return isl_space_join(isl_space_from_domain(dom_dom),
			    isl_space_from_range(isl_space_wrap(ran)));
error:
	isl_space_free(space);
	return NULL;
}

/* Can isl_space_range_curry be applied to "space"?
 * That is, does it have a nested relation in its range,
 * the domain of which is itself a nested relation?
 */
isl_bool isl_space_can_range_curry(__isl_keep isl_space *space)
{
	isl_bool can;

	if (!space)
		return isl_bool_error;
	can = isl_space_range_is_wrapping(space);
	if (can < 0 || !can)
		return can;
	return isl_space_can_curry(space->nested[1]);
}

/* Given a space A -> ((B -> C) -> D), return the corresponding space
 * A -> (B -> (C -> D)).
 */
__isl_give isl_space *isl_space_range_curry(__isl_take isl_space *space)
{
	isl_space *nested;

	if (!space)
		return NULL;

	if (!isl_space_can_range_curry(space))
		isl_die(isl_space_get_ctx(space), isl_error_invalid,
			"space range cannot be curried",
			return isl_space_free(space));

	nested = isl_space_take_nested(space, 1);
	nested = isl_space_curry(nested);
	space = isl_space_restore_nested(space, 1, nested);

	return space;
}

/* Can we apply isl_space_uncurry to "space"?
 * That is, does it have a map space with a nested relation in its range?
 */
isl_bool isl_space_can_uncurry(__isl_keep isl_space *space)
{
	return isl_space_range_is_wrapping(space);
}

/* Given a space A -> (B -> C), return the corresponding space
 * (A -> B) -> C.
 */
__isl_give isl_space *isl_space_uncurry(__isl_take isl_space *space)
{
	isl_space *dom, *ran;
	isl_space *ran_dom, *ran_ran;

	if (!space)
		return NULL;

	if (!isl_space_can_uncurry(space))
		isl_die(space->ctx, isl_error_invalid,
			"space cannot be uncurried",
			return isl_space_free(space));

	dom = isl_space_domain(isl_space_copy(space));
	ran = isl_space_unwrap(isl_space_range(space));
	ran_dom = isl_space_domain(isl_space_copy(ran));
	ran_ran = isl_space_range(ran);
	dom = isl_space_join(isl_space_from_domain(dom),
			   isl_space_from_range(ran_dom));
	return isl_space_join(isl_space_from_domain(isl_space_wrap(dom)),
			    isl_space_from_range(ran_ran));
}

isl_bool isl_space_has_named_params(__isl_keep isl_space *space)
{
	int i;
	unsigned off;

	if (!space)
		return isl_bool_error;
	if (space->nparam == 0)
		return isl_bool_true;
	off = isl_space_offset(space, isl_dim_param);
	if (off + space->nparam > space->n_id)
		return isl_bool_false;
	for (i = 0; i < space->nparam; ++i)
		if (!space->ids[off + i])
			return isl_bool_false;
	return isl_bool_true;
}

/* Check that "space" has only named parameters, reporting an error
 * if it does not.
 */
isl_stat isl_space_check_named_params(__isl_keep isl_space *space)
{
	isl_bool named;

	named = isl_space_has_named_params(space);
	if (named < 0)
		return isl_stat_error;
	if (!named)
		isl_die(isl_space_get_ctx(space), isl_error_invalid,
			"unexpected unnamed parameters", return isl_stat_error);

	return isl_stat_ok;
}

/* Align the initial parameters of space1 to match the order in space2.
 */
__isl_give isl_space *isl_space_align_params(__isl_take isl_space *space1,
	__isl_take isl_space *space2)
{
	isl_reordering *exp;

	if (isl_space_check_named_params(space1) < 0 ||
	    isl_space_check_named_params(space2) < 0)
		goto error;

	exp = isl_parameter_alignment_reordering(space1, space2);
	exp = isl_reordering_extend_space(exp, space1);
	isl_space_free(space2);
	space1 = isl_reordering_get_space(exp);
	isl_reordering_free(exp);
	return space1;
error:
	isl_space_free(space1);
	isl_space_free(space2);
	return NULL;
}

/* Given the space of set (domain), construct a space for a map
 * with as domain the given space and as range the range of "model".
 */
__isl_give isl_space *isl_space_extend_domain_with_range(
	__isl_take isl_space *space, __isl_take isl_space *model)
{
	isl_size n_out;

	if (!model)
		goto error;

	space = isl_space_from_domain(space);
	n_out = isl_space_dim(model, isl_dim_out);
	if (n_out < 0)
		goto error;
	space = isl_space_add_dims(space, isl_dim_out, n_out);
	if (isl_space_has_tuple_id(model, isl_dim_out))
		space = isl_space_set_tuple_id(space, isl_dim_out,
				isl_space_get_tuple_id(model, isl_dim_out));
	if (!space)
		goto error;
	if (model->nested[1]) {
		isl_space *nested = isl_space_copy(model->nested[1]);
		isl_size n_nested, n_space;
		nested = isl_space_align_params(nested, isl_space_copy(space));
		n_nested = isl_space_dim(nested, isl_dim_param);
		n_space = isl_space_dim(space, isl_dim_param);
		if (n_nested < 0 || n_space < 0)
			goto error;
		if (n_nested > n_space)
			nested = isl_space_drop_dims(nested, isl_dim_param,
						n_space, n_nested - n_space);
		if (!nested)
			goto error;
		space->nested[1] = nested;
	}
	isl_space_free(model);
	return space;
error:
	isl_space_free(model);
	isl_space_free(space);
	return NULL;
}

/* Compare the "type" dimensions of two isl_spaces.
 *
 * The order is fairly arbitrary.
 */
static int isl_space_cmp_type(__isl_keep isl_space *space1,
	__isl_keep isl_space *space2, enum isl_dim_type type)
{
	int cmp;
	isl_size dim1, dim2;
	isl_space *nested1, *nested2;

	dim1 = isl_space_dim(space1, type);
	dim2 = isl_space_dim(space2, type);
	if (dim1 < 0 || dim2 < 0)
		return 0;
	if (dim1 != dim2)
		return dim1 - dim2;

	cmp = isl_id_cmp(tuple_id(space1, type), tuple_id(space2, type));
	if (cmp != 0)
		return cmp;

	nested1 = nested(space1, type);
	nested2 = nested(space2, type);
	if (!nested1 != !nested2)
		return !nested1 - !nested2;

	if (nested1)
		return isl_space_cmp(nested1, nested2);

	return 0;
}

/* Compare two isl_spaces.
 *
 * The order is fairly arbitrary.
 */
int isl_space_cmp(__isl_keep isl_space *space1, __isl_keep isl_space *space2)
{
	int i;
	int cmp;

	if (space1 == space2)
		return 0;
	if (!space1)
		return -1;
	if (!space2)
		return 1;

	cmp = isl_space_cmp_type(space1, space2, isl_dim_param);
	if (cmp != 0)
		return cmp;
	cmp = isl_space_cmp_type(space1, space2, isl_dim_in);
	if (cmp != 0)
		return cmp;
	cmp = isl_space_cmp_type(space1, space2, isl_dim_out);
	if (cmp != 0)
		return cmp;

	if (!space1->ids && !space2->ids)
		return 0;

	for (i = 0; i < n(space1, isl_dim_param); ++i) {
		cmp = isl_id_cmp(get_id(space1, isl_dim_param, i),
				 get_id(space2, isl_dim_param, i));
		if (cmp != 0)
			return cmp;
	}

	return 0;
}