isl_vertices.c 37.3 KB
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
/*
 * Copyright 2010      INRIA Saclay
 *
 * Use of this software is governed by the MIT license
 *
 * Written by Sven Verdoolaege, INRIA Saclay - Ile-de-France,
 * Parc Club Orsay Universite, ZAC des vignes, 4 rue Jacques Monod,
 * 91893 Orsay, France 
 */

#include <isl_map_private.h>
#include <isl_aff_private.h>
#include <isl/set.h>
#include <isl_seq.h>
#include <isl_tab.h>
#include <isl_space_private.h>
#include <isl_morph.h>
#include <isl_vertices_private.h>
#include <isl_mat_private.h>
#include <isl_vec_private.h>

#define SELECTED	1
#define DESELECTED	-1
#define UNSELECTED	0

static __isl_give isl_vertices *compute_chambers(__isl_take isl_basic_set *bset,
	__isl_take isl_vertices *vertices);

__isl_give isl_vertices *isl_vertices_copy(__isl_keep isl_vertices *vertices)
{
	if (!vertices)
		return NULL;

	vertices->ref++;
	return vertices;
}

__isl_null isl_vertices *isl_vertices_free(__isl_take isl_vertices *vertices)
{
	int i;

	if (!vertices)
		return NULL;

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

	for (i = 0; i < vertices->n_vertices; ++i) {
		isl_basic_set_free(vertices->v[i].vertex);
		isl_basic_set_free(vertices->v[i].dom);
	}
	free(vertices->v);

	for (i = 0; i < vertices->n_chambers; ++i) {
		free(vertices->c[i].vertices);
		isl_basic_set_free(vertices->c[i].dom);
	}
	free(vertices->c);

	isl_basic_set_free(vertices->bset);
	free(vertices);

	return NULL;
}

struct isl_vertex_list {
	struct isl_vertex v;
	struct isl_vertex_list *next;
};

static struct isl_vertex_list *free_vertex_list(struct isl_vertex_list *list)
{
	struct isl_vertex_list *next;

	for (; list; list = next) {
		next = list->next;
		isl_basic_set_free(list->v.vertex);
		isl_basic_set_free(list->v.dom);
		free(list);
	}

	return NULL;
}

static __isl_give isl_vertices *vertices_from_list(__isl_keep isl_basic_set *bset,
	int n_vertices, struct isl_vertex_list *list)
{
	int i;
	struct isl_vertex_list *next;
	isl_vertices *vertices;

	vertices = isl_calloc_type(bset->ctx, isl_vertices);
	if (!vertices)
		goto error;
	vertices->ref = 1;
	vertices->bset = isl_basic_set_copy(bset);
	vertices->v = isl_alloc_array(bset->ctx, struct isl_vertex, n_vertices);
	if (n_vertices && !vertices->v)
		goto error;
	vertices->n_vertices = n_vertices;

	for (i = 0; list; list = next, i++) {
		next = list->next;
		vertices->v[i] = list->v;
		free(list);
	}

	return vertices;
error:
	isl_vertices_free(vertices);
	free_vertex_list(list);
	return NULL;
}

/* Prepend a vertex to the linked list "list" based on the equalities in "tab".
 * Return isl_bool_true if the vertex was actually added and
 * isl_bool_false otherwise.
 * In particular, vertices with a lower-dimensional activity domain are
 * not added to the list because they would not be included in any chamber.
 * Return isl_bool_error on error.
 */
static isl_bool add_vertex(struct isl_vertex_list **list,
	__isl_keep isl_basic_set *bset, struct isl_tab *tab)
{
	isl_size nvar;
	struct isl_vertex_list *v = NULL;

	if (isl_tab_detect_implicit_equalities(tab) < 0)
		return isl_bool_error;

	nvar = isl_basic_set_dim(bset, isl_dim_set);
	if (nvar < 0)
		return isl_bool_error;

	v = isl_calloc_type(tab->mat->ctx, struct isl_vertex_list);
	if (!v)
		goto error;

	v->v.vertex = isl_basic_set_copy(bset);
	v->v.vertex = isl_basic_set_cow(v->v.vertex);
	v->v.vertex = isl_basic_set_update_from_tab(v->v.vertex, tab);
	v->v.vertex = isl_basic_set_simplify(v->v.vertex);
	v->v.vertex = isl_basic_set_finalize(v->v.vertex);
	if (!v->v.vertex)
		goto error;
	isl_assert(bset->ctx, v->v.vertex->n_eq >= nvar, goto error);
	v->v.dom = isl_basic_set_copy(v->v.vertex);
	v->v.dom = isl_basic_set_params(v->v.dom);
	if (!v->v.dom)
		goto error;

	if (v->v.dom->n_eq > 0) {
		free_vertex_list(v);
		return isl_bool_false;
	}

	v->next = *list;
	*list = v;

	return isl_bool_true;
error:
	free_vertex_list(v);
	return isl_bool_error;
}

/* Compute the parametric vertices and the chamber decomposition
 * of an empty parametric polytope.
 */
static __isl_give isl_vertices *vertices_empty(__isl_keep isl_basic_set *bset)
{
	isl_vertices *vertices;

	if (!bset)
		return NULL;

	vertices = isl_calloc_type(bset->ctx, isl_vertices);
	if (!vertices)
		return NULL;
	vertices->bset = isl_basic_set_copy(bset);
	vertices->ref = 1;

	vertices->n_vertices = 0;
	vertices->n_chambers = 0;

	return vertices;
}

/* Compute the parametric vertices and the chamber decomposition
 * of the parametric polytope defined using the same constraints
 * as "bset" in the 0D case.
 * There is exactly one 0D vertex and a single chamber containing
 * the vertex.
 */
static __isl_give isl_vertices *vertices_0D(__isl_keep isl_basic_set *bset)
{
	isl_vertices *vertices;

	if (!bset)
		return NULL;

	vertices = isl_calloc_type(bset->ctx, isl_vertices);
	if (!vertices)
		return NULL;
	vertices->ref = 1;
	vertices->bset = isl_basic_set_copy(bset);

	vertices->v = isl_calloc_array(bset->ctx, struct isl_vertex, 1);
	if (!vertices->v)
		goto error;
	vertices->n_vertices = 1;
	vertices->v[0].vertex = isl_basic_set_copy(bset);
	vertices->v[0].dom = isl_basic_set_params(isl_basic_set_copy(bset));
	if (!vertices->v[0].vertex || !vertices->v[0].dom)
		goto error;

	vertices->c = isl_calloc_array(bset->ctx, struct isl_chamber, 1);
	if (!vertices->c)
		goto error;
	vertices->n_chambers = 1;
	vertices->c[0].n_vertices = 1;
	vertices->c[0].vertices = isl_calloc_array(bset->ctx, int, 1);
	if (!vertices->c[0].vertices)
		goto error;
	vertices->c[0].dom = isl_basic_set_copy(vertices->v[0].dom);
	if (!vertices->c[0].dom)
		goto error;

	return vertices;
error:
	isl_vertices_free(vertices);
	return NULL;
}

/* Is the row pointed to by "f" linearly independent of the "n" first
 * rows in "facets"?
 */
static isl_bool is_independent(__isl_keep isl_mat *facets, int n, isl_int *f)
{
	isl_size rank;

	if (isl_seq_first_non_zero(f, facets->n_col) < 0)
		return isl_bool_false;

	isl_seq_cpy(facets->row[n], f, facets->n_col);
	facets->n_row = n + 1;
	rank = isl_mat_rank(facets);
	if (rank < 0)
		return isl_bool_error;

	return isl_bool_ok(rank == n + 1);
}

/* Check whether we can select constraint "level", given the current selection
 * reflected by facets in "tab", the rows of "facets" and the earlier
 * "selected" elements of "selection".
 *
 * If the constraint is (strictly) redundant in the tableau, selecting it would
 * result in an empty tableau, so it can't be selected.
 * If the set variable part of the constraint is not linearly independent
 * of the set variable parts of the already selected constraints,
 * the constraint cannot be selected.
 * If selecting the constraint results in an empty tableau, the constraint
 * cannot be selected.
 * Finally, if selecting the constraint results in some explicitly
 * deselected constraints turning into equalities, then the corresponding
 * vertices have already been generated, so the constraint cannot be selected.
 */
static isl_bool can_select(__isl_keep isl_basic_set *bset, int level,
	struct isl_tab *tab, __isl_keep isl_mat *facets, int selected,
	int *selection)
{
	int i;
	isl_bool indep;
	unsigned ovar;
	struct isl_tab_undo *snap;

	if (isl_tab_is_redundant(tab, level))
		return isl_bool_false;

	ovar = isl_space_offset(bset->dim, isl_dim_set);

	indep = is_independent(facets, selected, bset->ineq[level] + 1 + ovar);
	if (indep < 0 || !indep)
		return indep;

	snap = isl_tab_snap(tab);
	if (isl_tab_select_facet(tab, level) < 0)
		return isl_bool_error;

	if (tab->empty) {
		if (isl_tab_rollback(tab, snap) < 0)
			return isl_bool_error;
		return isl_bool_false;
	}

	for (i = 0; i < level; ++i) {
		int sgn;

		if (selection[i] != DESELECTED)
			continue;

		if (isl_tab_is_equality(tab, i))
			sgn = 0;
		else if (isl_tab_is_redundant(tab, i))
			sgn = 1;
		else
			sgn = isl_tab_sign_of_max(tab, i);
		if (sgn < -1)
			return isl_bool_error;
		if (sgn <= 0) {
			if (isl_tab_rollback(tab, snap) < 0)
				return isl_bool_error;
			return isl_bool_false;
		}
	}

	return isl_bool_true;
}

/* Compute the parametric vertices and the chamber decomposition
 * of a parametric polytope that is not full-dimensional.
 *
 * Simply map the parametric polytope to a lower dimensional space
 * and map the resulting vertices back.
 */
static __isl_give isl_vertices *lower_dim_vertices(
	__isl_keep isl_basic_set *bset)
{
	isl_morph *morph;
	isl_vertices *vertices;

	bset = isl_basic_set_copy(bset);
	morph = isl_basic_set_full_compression(bset);
	bset = isl_morph_basic_set(isl_morph_copy(morph), bset);

	vertices = isl_basic_set_compute_vertices(bset);
	isl_basic_set_free(bset);

	morph = isl_morph_inverse(morph);

	vertices = isl_morph_vertices(morph, vertices);

	return vertices;
}

/* Compute the parametric vertices and the chamber decomposition
 * of the parametric polytope defined using the same constraints
 * as "bset".  "bset" is assumed to have no existentially quantified
 * variables.
 *
 * The vertices themselves are computed in a fairly simplistic way.
 * We simply run through all combinations of d constraints,
 * with d the number of set variables, and check if those d constraints
 * define a vertex.  To avoid the generation of duplicate vertices,
 * which we may happen if a vertex is defined by more that d constraints,
 * we make sure we only generate the vertex for the d constraints with
 * smallest index.
 *
 * We set up a tableau and keep track of which facets have been
 * selected.  The tableau is marked strict_redundant so that we can be
 * sure that any constraint that is marked redundant (and that is not
 * also marked zero) is not an equality.
 * If a constraint is marked DESELECTED, it means the constraint was
 * SELECTED before (in combination with the same selection of earlier
 * constraints).  If such a deselected constraint turns out to be an
 * equality, then any vertex that may still be found with the current
 * selection has already been generated when the constraint was selected.
 * A constraint is marked UNSELECTED when there is no way selecting
 * the constraint could lead to a vertex (in combination with the current
 * selection of earlier constraints).
 *
 * The set variable coefficients of the selected constraints are stored
 * in the facets matrix.
 */
__isl_give isl_vertices *isl_basic_set_compute_vertices(
	__isl_keep isl_basic_set *bset)
{
	struct isl_tab *tab;
	int level;
	int init;
	isl_size nvar;
	int *selection = NULL;
	int selected;
	struct isl_tab_undo **snap = NULL;
	isl_mat *facets = NULL;
	struct isl_vertex_list *list = NULL;
	int n_vertices = 0;
	isl_vertices *vertices;

	if (!bset)
		return NULL;

	if (isl_basic_set_plain_is_empty(bset))
		return vertices_empty(bset);

	if (bset->n_eq != 0)
		return lower_dim_vertices(bset);

	if (isl_basic_set_check_no_locals(bset) < 0)
		return NULL;

	nvar = isl_basic_set_dim(bset, isl_dim_set);
	if (nvar < 0)
		return NULL;
	if (nvar == 0)
		return vertices_0D(bset);

	bset = isl_basic_set_copy(bset);
	bset = isl_basic_set_set_rational(bset);
	if (!bset)
		return NULL;

	tab = isl_tab_from_basic_set(bset, 0);
	if (!tab)
		goto error;
	tab->strict_redundant = 1;

	if (tab->empty)	{
		vertices = vertices_empty(bset);
		isl_basic_set_free(bset);
		isl_tab_free(tab);
		return vertices;
	}

	selection = isl_alloc_array(bset->ctx, int, bset->n_ineq);
	snap = isl_alloc_array(bset->ctx, struct isl_tab_undo *, bset->n_ineq);
	facets = isl_mat_alloc(bset->ctx, nvar, nvar);
	if ((bset->n_ineq && (!selection || !snap)) || !facets)
		goto error;

	level = 0;
	init = 1;
	selected = 0;

	while (level >= 0) {
		if (level >= bset->n_ineq ||
		    (!init && selection[level] != SELECTED)) {
			--level;
			init = 0;
			continue;
		}
		if (init) {
			isl_bool ok;
			snap[level] = isl_tab_snap(tab);
			ok = can_select(bset, level, tab, facets, selected,
					selection);
			if (ok < 0)
				goto error;
			if (ok) {
				selection[level] = SELECTED;
				selected++;
			} else
				selection[level] = UNSELECTED;
		} else {
			selection[level] = DESELECTED;
			selected--;
			if (isl_tab_rollback(tab, snap[level]) < 0)
				goto error;
		}
		if (selected == nvar) {
			if (tab->n_dead == nvar) {
				isl_bool added = add_vertex(&list, bset, tab);
				if (added < 0)
					goto error;
				if (added)
					n_vertices++;
			}
			init = 0;
			continue;
		}
		++level;
		init = 1;
	}

	isl_mat_free(facets);
	free(selection);
	free(snap);

	isl_tab_free(tab);

	vertices = vertices_from_list(bset, n_vertices, list);

	vertices = compute_chambers(bset, vertices);

	return vertices;
error:
	free_vertex_list(list);
	isl_mat_free(facets);
	free(selection);
	free(snap);
	isl_tab_free(tab);
	isl_basic_set_free(bset);
	return NULL;
}

struct isl_chamber_list {
	struct isl_chamber c;
	struct isl_chamber_list *next;
};

static void free_chamber_list(struct isl_chamber_list *list)
{
	struct isl_chamber_list *next;

	for (; list; list = next) {
		next = list->next;
		isl_basic_set_free(list->c.dom);
		free(list->c.vertices);
		free(list);
	}
}

/* Check whether the basic set "bset" is a superset of the basic set described
 * by "tab", i.e., check whether all constraints of "bset" are redundant.
 */
static isl_bool bset_covers_tab(__isl_keep isl_basic_set *bset,
	struct isl_tab *tab)
{
	int i;

	if (!bset || !tab)
		return isl_bool_error;

	for (i = 0; i < bset->n_ineq; ++i) {
		enum isl_ineq_type type = isl_tab_ineq_type(tab, bset->ineq[i]);
		switch (type) {
		case isl_ineq_error:		return isl_bool_error;
		case isl_ineq_redundant:	continue;
		default:			return isl_bool_false;
		}
	}

	return isl_bool_true;
}

static __isl_give isl_vertices *vertices_add_chambers(
	__isl_take isl_vertices *vertices, int n_chambers,
	struct isl_chamber_list *list)
{
	int i;
	isl_ctx *ctx;
	struct isl_chamber_list *next;

	ctx = isl_vertices_get_ctx(vertices);
	vertices->c = isl_alloc_array(ctx, struct isl_chamber, n_chambers);
	if (!vertices->c)
		goto error;
	vertices->n_chambers = n_chambers;

	for (i = 0; list; list = next, i++) {
		next = list->next;
		vertices->c[i] = list->c;
		free(list);
	}

	return vertices;
error:
	isl_vertices_free(vertices);
	free_chamber_list(list);
	return NULL;
}

/* Can "tab" be intersected with "bset" without resulting in
 * a lower-dimensional set.
 * "bset" itself is assumed to be full-dimensional.
 */
static isl_bool can_intersect(struct isl_tab *tab,
	__isl_keep isl_basic_set *bset)
{
	int i;
	struct isl_tab_undo *snap;

	if (bset->n_eq > 0)
		isl_die(isl_basic_set_get_ctx(bset), isl_error_internal,
			"expecting full-dimensional input",
			return isl_bool_error);

	if (isl_tab_extend_cons(tab, bset->n_ineq) < 0)
		return isl_bool_error;

	snap = isl_tab_snap(tab);

	for (i = 0; i < bset->n_ineq; ++i) {
		enum isl_ineq_type type;

		type = isl_tab_ineq_type(tab, bset->ineq[i]);
		if (type < 0)
			return isl_bool_error;
		if (type == isl_ineq_redundant)
			continue;
		if (isl_tab_add_ineq(tab, bset->ineq[i]) < 0)
			return isl_bool_error;
	}

	if (isl_tab_detect_implicit_equalities(tab) < 0)
		return isl_bool_error;
	if (tab->n_dead) {
		if (isl_tab_rollback(tab, snap) < 0)
			return isl_bool_error;
		return isl_bool_false;
	}

	return isl_bool_true;
}

static int add_chamber(struct isl_chamber_list **list,
	__isl_keep isl_vertices *vertices, struct isl_tab *tab, int *selection)
{
	int n_frozen;
	int i, j;
	int n_vertices = 0;
	struct isl_tab_undo *snap;
	struct isl_chamber_list *c = NULL;

	for (i = 0; i < vertices->n_vertices; ++i)
		if (selection[i])
			n_vertices++;

	snap = isl_tab_snap(tab);

	for (i = 0; i < tab->n_con && tab->con[i].frozen; ++i)
		tab->con[i].frozen = 0;
	n_frozen = i;

	if (isl_tab_detect_redundant(tab) < 0)
		return -1;

	c = isl_calloc_type(tab->mat->ctx, struct isl_chamber_list);
	if (!c)
		goto error;
	c->c.vertices = isl_alloc_array(tab->mat->ctx, int, n_vertices);
	if (n_vertices && !c->c.vertices)
		goto error;
	c->c.dom = isl_basic_set_copy(isl_tab_peek_bset(tab));
	c->c.dom = isl_basic_set_set_rational(c->c.dom);
	c->c.dom = isl_basic_set_cow(c->c.dom);
	c->c.dom = isl_basic_set_update_from_tab(c->c.dom, tab);
	c->c.dom = isl_basic_set_simplify(c->c.dom);
	c->c.dom = isl_basic_set_finalize(c->c.dom);
	if (!c->c.dom)
		goto error;

	c->c.n_vertices = n_vertices;

	for (i = 0, j = 0; i < vertices->n_vertices; ++i)
		if (selection[i]) {
			c->c.vertices[j] = i;
			j++;
		}

	c->next = *list;
	*list = c;

	for (i = 0; i < n_frozen; ++i)
		tab->con[i].frozen = 1;

	if (isl_tab_rollback(tab, snap) < 0)
		return -1;

	return 0;
error:
	free_chamber_list(c);
	return -1;
}

struct isl_facet_todo {
	struct isl_tab *tab;	/* A tableau representation of the facet */
	isl_basic_set *bset;    /* A normalized basic set representation */
	isl_vec *constraint;	/* Constraint pointing to the other side */
	struct isl_facet_todo *next;
};

static void free_todo(struct isl_facet_todo *todo)
{
	while (todo) {
		struct isl_facet_todo *next = todo->next;

		isl_tab_free(todo->tab);
		isl_basic_set_free(todo->bset);
		isl_vec_free(todo->constraint);
		free(todo);

		todo = next;
	}
}

static struct isl_facet_todo *create_todo(struct isl_tab *tab, int con)
{
	int i;
	int n_frozen;
	struct isl_tab_undo *snap;
	struct isl_facet_todo *todo;

	snap = isl_tab_snap(tab);

	for (i = 0; i < tab->n_con && tab->con[i].frozen; ++i)
		tab->con[i].frozen = 0;
	n_frozen = i;

	if (isl_tab_detect_redundant(tab) < 0)
		return NULL;

	todo = isl_calloc_type(tab->mat->ctx, struct isl_facet_todo);
	if (!todo)
		return NULL;

	todo->constraint = isl_vec_alloc(tab->mat->ctx, 1 + tab->n_var);
	if (!todo->constraint)
		goto error;
	isl_seq_neg(todo->constraint->el, tab->bmap->ineq[con], 1 + tab->n_var);
	todo->bset = isl_basic_set_copy(isl_tab_peek_bset(tab));
	todo->bset = isl_basic_set_set_rational(todo->bset);
	todo->bset = isl_basic_set_cow(todo->bset);
	todo->bset = isl_basic_set_update_from_tab(todo->bset, tab);
	todo->bset = isl_basic_set_simplify(todo->bset);
	todo->bset = isl_basic_set_sort_constraints(todo->bset);
	if (!todo->bset)
		goto error;
	ISL_F_SET(todo->bset, ISL_BASIC_SET_NO_REDUNDANT);
	todo->tab = isl_tab_dup(tab);
	if (!todo->tab)
		goto error;

	for (i = 0; i < n_frozen; ++i)
		tab->con[i].frozen = 1;

	if (isl_tab_rollback(tab, snap) < 0)
		goto error;

	return todo;
error:
	free_todo(todo);
	return NULL;
}

/* Create todo items for all interior facets of the chamber represented
 * by "tab" and collect them in "next".
 */
static int init_todo(struct isl_facet_todo **next, struct isl_tab *tab)
{
	int i;
	struct isl_tab_undo *snap;
	struct isl_facet_todo *todo;

	snap = isl_tab_snap(tab);

	for (i = 0; i < tab->n_con; ++i) {
		if (tab->con[i].frozen)
			continue;
		if (tab->con[i].is_redundant)
			continue;

		if (isl_tab_select_facet(tab, i) < 0)
			return -1;

		todo = create_todo(tab, i);
		if (!todo)
			return -1;

		todo->next = *next;
		*next = todo;

		if (isl_tab_rollback(tab, snap) < 0)
			return -1;
	}

	return 0;
}

/* Does the linked list contain a todo item that is the opposite of "todo".
 * If so, return 1 and remove the opposite todo item.
 */
static int has_opposite(struct isl_facet_todo *todo,
	struct isl_facet_todo **list)
{
	for (; *list; list = &(*list)->next) {
		int eq;
		eq = isl_basic_set_plain_is_equal(todo->bset, (*list)->bset);
		if (eq < 0)
			return -1;
		if (!eq)
			continue;
		todo = *list;
		*list = todo->next;
		todo->next = NULL;
		free_todo(todo);
		return 1;
	}

	return 0;
}

/* Create todo items for all interior facets of the chamber represented
 * by "tab" and collect them in first->next, taking care to cancel
 * opposite todo items.
 */
static int update_todo(struct isl_facet_todo *first, struct isl_tab *tab)
{
	int i;
	struct isl_tab_undo *snap;
	struct isl_facet_todo *todo;

	snap = isl_tab_snap(tab);

	for (i = 0; i < tab->n_con; ++i) {
		int drop;

		if (tab->con[i].frozen)
			continue;
		if (tab->con[i].is_redundant)
			continue;

		if (isl_tab_select_facet(tab, i) < 0)
			return -1;

		todo = create_todo(tab, i);
		if (!todo)
			return -1;

		drop = has_opposite(todo, &first->next);
		if (drop < 0)
			return -1;

		if (drop)
			free_todo(todo);
		else {
			todo->next = first->next;
			first->next = todo;
		}

		if (isl_tab_rollback(tab, snap) < 0)
			return -1;
	}

	return 0;
}

/* Compute the chamber decomposition of the parametric polytope respresented
 * by "bset" given the parametric vertices and their activity domains.
 *
 * We are only interested in full-dimensional chambers.
 * Each of these chambers is the intersection of the activity domains of
 * one or more vertices and the union of all chambers is equal to the
 * projection of the entire parametric polytope onto the parameter space.
 *
 * We first create an initial chamber by intersecting as many activity
 * domains as possible without ending up with an empty or lower-dimensional
 * set.  As a minor optimization, we only consider those activity domains
 * that contain some arbitrary point.
 *
 * For each of the interior facets of the chamber, we construct a todo item,
 * containing the facet and a constraint containing the other side of the facet,
 * for constructing the chamber on the other side.
 * While their are any todo items left, we pick a todo item and
 * create the required chamber by intersecting all activity domains
 * that contain the facet and have a full-dimensional intersection with
 * the other side of the facet.  For each of the interior facets, we
 * again create todo items, taking care to cancel opposite todo items.
 */
static __isl_give isl_vertices *compute_chambers(__isl_take isl_basic_set *bset,
	__isl_take isl_vertices *vertices)
{
	int i;
	isl_ctx *ctx;
	isl_vec *sample = NULL;
	struct isl_tab *tab = NULL;
	struct isl_tab_undo *snap;
	int *selection = NULL;
	int n_chambers = 0;
	struct isl_chamber_list *list = NULL;
	struct isl_facet_todo *todo = NULL;

	if (!bset || !vertices)
		goto error;

	ctx = isl_vertices_get_ctx(vertices);
	selection = isl_alloc_array(ctx, int, vertices->n_vertices);
	if (vertices->n_vertices && !selection)
		goto error;

	bset = isl_basic_set_params(bset);

	tab = isl_tab_from_basic_set(bset, 1);
	if (!tab)
		goto error;
	for (i = 0; i < bset->n_ineq; ++i)
		if (isl_tab_freeze_constraint(tab, i) < 0)
			goto error;
	isl_basic_set_free(bset);

	snap = isl_tab_snap(tab);

	sample = isl_tab_get_sample_value(tab);

	for (i = 0; i < vertices->n_vertices; ++i) {
		selection[i] = isl_basic_set_contains(vertices->v[i].dom, sample);
		if (selection[i] < 0)
			goto error;
		if (!selection[i])
			continue;
		selection[i] = can_intersect(tab, vertices->v[i].dom);
		if (selection[i] < 0)
			goto error;
	}

	if (isl_tab_detect_redundant(tab) < 0)
		goto error;

	if (add_chamber(&list, vertices, tab, selection) < 0)
		goto error;
	n_chambers++;

	if (init_todo(&todo, tab) < 0)
		goto error;

	while (todo) {
		struct isl_facet_todo *next;

		if (isl_tab_rollback(tab, snap) < 0)
			goto error;

		if (isl_tab_add_ineq(tab, todo->constraint->el) < 0)
			goto error;
		if (isl_tab_freeze_constraint(tab, tab->n_con - 1) < 0)
			goto error;

		for (i = 0; i < vertices->n_vertices; ++i) {
			selection[i] = bset_covers_tab(vertices->v[i].dom,
							todo->tab);
			if (selection[i] < 0)
				goto error;
			if (!selection[i])
				continue;
			selection[i] = can_intersect(tab, vertices->v[i].dom);
			if (selection[i] < 0)
				goto error;
		}

		if (isl_tab_detect_redundant(tab) < 0)
			goto error;

		if (add_chamber(&list, vertices, tab, selection) < 0)
			goto error;
		n_chambers++;

		if (update_todo(todo, tab) < 0)
			goto error;

		next = todo->next;
		todo->next = NULL;
		free_todo(todo);
		todo = next;
	}

	isl_vec_free(sample);

	isl_tab_free(tab);
	free(selection);

	vertices = vertices_add_chambers(vertices, n_chambers, list);

	for (i = 0; vertices && i < vertices->n_vertices; ++i) {
		isl_basic_set_free(vertices->v[i].dom);
		vertices->v[i].dom = NULL;
	}

	return vertices;
error:
	free_chamber_list(list);
	free_todo(todo);
	isl_vec_free(sample);
	isl_tab_free(tab);
	free(selection);
	if (!tab)
		isl_basic_set_free(bset);
	isl_vertices_free(vertices);
	return NULL;
}

isl_ctx *isl_vertex_get_ctx(__isl_keep isl_vertex *vertex)
{
	return vertex ? isl_vertices_get_ctx(vertex->vertices) : NULL;
}

isl_size isl_vertex_get_id(__isl_keep isl_vertex *vertex)
{
	return vertex ? vertex->id : isl_size_error;
}

/* Return the activity domain of the vertex "vertex".
 */
__isl_give isl_basic_set *isl_vertex_get_domain(__isl_keep isl_vertex *vertex)
{
	struct isl_vertex *v;

	if (!vertex)
		return NULL;

	v = &vertex->vertices->v[vertex->id];
	if (!v->dom) {
		v->dom = isl_basic_set_copy(v->vertex);
		v->dom = isl_basic_set_params(v->dom);
		v->dom = isl_basic_set_set_integral(v->dom);
	}

	return isl_basic_set_copy(v->dom);
}

/* Return a multiple quasi-affine expression describing the vertex "vertex"
 * in terms of the parameters,
 */
__isl_give isl_multi_aff *isl_vertex_get_expr(__isl_keep isl_vertex *vertex)
{
	struct isl_vertex *v;
	isl_basic_set *bset;

	if (!vertex)
		return NULL;

	v = &vertex->vertices->v[vertex->id];

	bset = isl_basic_set_copy(v->vertex);
	return isl_multi_aff_from_basic_set_equalities(bset);
}

static __isl_give isl_vertex *isl_vertex_alloc(__isl_take isl_vertices *vertices,
	int id)
{
	isl_ctx *ctx;
	isl_vertex *vertex;

	if (!vertices)
		return NULL;

	ctx = isl_vertices_get_ctx(vertices);
	vertex = isl_alloc_type(ctx, isl_vertex);
	if (!vertex)
		goto error;

	vertex->vertices = vertices;
	vertex->id = id;

	return vertex;
error:
	isl_vertices_free(vertices);
	return NULL;
}

__isl_null isl_vertex *isl_vertex_free(__isl_take isl_vertex *vertex)
{
	if (!vertex)
		return NULL;
	isl_vertices_free(vertex->vertices);
	free(vertex);

	return NULL;
}

isl_ctx *isl_cell_get_ctx(__isl_keep isl_cell *cell)
{
	return cell ? cell->dom->ctx : NULL;
}

__isl_give isl_basic_set *isl_cell_get_domain(__isl_keep isl_cell *cell)
{
	return cell ? isl_basic_set_copy(cell->dom) : NULL;
}

static __isl_give isl_cell *isl_cell_alloc(__isl_take isl_vertices *vertices,
	__isl_take isl_basic_set *dom, int id)
{
	int i;
	isl_cell *cell = NULL;

	if (!vertices || !dom)
		goto error;

	cell = isl_calloc_type(dom->ctx, isl_cell);
	if (!cell)
		goto error;

	cell->n_vertices = vertices->c[id].n_vertices;
	cell->ids = isl_alloc_array(dom->ctx, int, cell->n_vertices);
	if (cell->n_vertices && !cell->ids)
		goto error;
	for (i = 0; i < cell->n_vertices; ++i)
		cell->ids[i] = vertices->c[id].vertices[i];
	cell->vertices = vertices;
	cell->dom = dom;

	return cell;
error:
	isl_cell_free(cell);
	isl_vertices_free(vertices);
	isl_basic_set_free(dom);
	return NULL;
}

__isl_null isl_cell *isl_cell_free(__isl_take isl_cell *cell)
{
	if (!cell)
		return NULL;

	isl_vertices_free(cell->vertices);
	free(cell->ids);
	isl_basic_set_free(cell->dom);
	free(cell);

	return NULL;
}

/* Create a tableau of the cone obtained by first homogenizing the given
 * polytope and then making all inequalities strict by setting the
 * constant term to -1.
 */
static struct isl_tab *tab_for_shifted_cone(__isl_keep isl_basic_set *bset)
{
	int i;
	isl_vec *c = NULL;
	struct isl_tab *tab;
	isl_size total;

	total = isl_basic_set_dim(bset, isl_dim_all);
	if (total < 0)
		return NULL;
	tab = isl_tab_alloc(bset->ctx, bset->n_eq + bset->n_ineq + 1,
			    1 + total, 0);
	if (!tab)
		return NULL;
	tab->rational = ISL_F_ISSET(bset, ISL_BASIC_SET_RATIONAL);
	if (ISL_F_ISSET(bset, ISL_BASIC_MAP_EMPTY)) {
		if (isl_tab_mark_empty(tab) < 0)
			goto error;
		return tab;
	}

	c = isl_vec_alloc(bset->ctx, 1 + 1 + total);
	if (!c)
		goto error;

	isl_int_set_si(c->el[0], 0);
	for (i = 0; i < bset->n_eq; ++i) {
		isl_seq_cpy(c->el + 1, bset->eq[i], c->size - 1);
		if (isl_tab_add_eq(tab, c->el) < 0)
			goto error;
	}

	isl_int_set_si(c->el[0], -1);
	for (i = 0; i < bset->n_ineq; ++i) {
		isl_seq_cpy(c->el + 1, bset->ineq[i], c->size - 1);
		if (isl_tab_add_ineq(tab, c->el) < 0)
			goto error;
		if (tab->empty) {
			isl_vec_free(c);
			return tab;
		}
	}

	isl_seq_clr(c->el + 1, c->size - 1);
	isl_int_set_si(c->el[1], 1);
	if (isl_tab_add_ineq(tab, c->el) < 0)
		goto error;

	isl_vec_free(c);
	return tab;
error:
	isl_vec_free(c);
	isl_tab_free(tab);
	return NULL;
}

/* Compute an interior point of "bset" by selecting an interior
 * point in homogeneous space and projecting the point back down.
 */
static __isl_give isl_vec *isl_basic_set_interior_point(
	__isl_keep isl_basic_set *bset)
{
	isl_vec *vec;
	struct isl_tab *tab;

	tab = tab_for_shifted_cone(bset);
	vec = isl_tab_get_sample_value(tab);
	isl_tab_free(tab);
	if (!vec)
		return NULL;

	isl_seq_cpy(vec->el, vec->el + 1, vec->size - 1);
	vec->size--;

	return vec;
}

/* Call "fn" on all chambers of the parametric polytope with the shared
 * facets of neighboring chambers only appearing in one of the chambers.
 *
 * We pick an interior point from one of the chambers and then make
 * all constraints that do not satisfy this point strict.
 * For constraints that saturate the interior point, the sign
 * of the first non-zero coefficient is used to determine which
 * of the two (internal) constraints should be tightened.
 */
isl_stat isl_vertices_foreach_disjoint_cell(__isl_keep isl_vertices *vertices,
	isl_stat (*fn)(__isl_take isl_cell *cell, void *user), void *user)
{
	int i;
	isl_vec *vec;
	isl_cell *cell;

	if (!vertices)
		return isl_stat_error;

	if (vertices->n_chambers == 0)
		return isl_stat_ok;

	if (vertices->n_chambers == 1) {
		isl_basic_set *dom = isl_basic_set_copy(vertices->c[0].dom);
		dom = isl_basic_set_set_integral(dom);
		cell = isl_cell_alloc(isl_vertices_copy(vertices), dom, 0);
		if (!cell)
			return isl_stat_error;
		return fn(cell, user);
	}

	vec = isl_basic_set_interior_point(vertices->c[0].dom);
	if (!vec)
		return isl_stat_error;

	for (i = 0; i < vertices->n_chambers; ++i) {
		int r;
		isl_basic_set *dom = isl_basic_set_copy(vertices->c[i].dom);
		if (i)
			dom = isl_basic_set_tighten_outward(dom, vec);
		dom = isl_basic_set_set_integral(dom);
		cell = isl_cell_alloc(isl_vertices_copy(vertices), dom, i);
		if (!cell)
			goto error;
		r = fn(cell, user);
		if (r < 0)
			goto error;
	}

	isl_vec_free(vec);

	return isl_stat_ok;
error:
	isl_vec_free(vec);
	return isl_stat_error;
}

isl_stat isl_vertices_foreach_cell(__isl_keep isl_vertices *vertices,
	isl_stat (*fn)(__isl_take isl_cell *cell, void *user), void *user)
{
	int i;
	isl_cell *cell;

	if (!vertices)
		return isl_stat_error;

	if (vertices->n_chambers == 0)
		return isl_stat_ok;

	for (i = 0; i < vertices->n_chambers; ++i) {
		isl_stat r;
		isl_basic_set *dom = isl_basic_set_copy(vertices->c[i].dom);

		cell = isl_cell_alloc(isl_vertices_copy(vertices), dom, i);
		if (!cell)
			return isl_stat_error;

		r = fn(cell, user);
		if (r < 0)
			return isl_stat_error;
	}

	return isl_stat_ok;
}

isl_stat isl_vertices_foreach_vertex(__isl_keep isl_vertices *vertices,
	isl_stat (*fn)(__isl_take isl_vertex *vertex, void *user), void *user)
{
	int i;
	isl_vertex *vertex;

	if (!vertices)
		return isl_stat_error;

	if (vertices->n_vertices == 0)
		return isl_stat_ok;

	for (i = 0; i < vertices->n_vertices; ++i) {
		isl_stat r;

		vertex = isl_vertex_alloc(isl_vertices_copy(vertices), i);
		if (!vertex)
			return isl_stat_error;

		r = fn(vertex, user);
		if (r < 0)
			return isl_stat_error;
	}

	return isl_stat_ok;
}

isl_stat isl_cell_foreach_vertex(__isl_keep isl_cell *cell,
	isl_stat (*fn)(__isl_take isl_vertex *vertex, void *user), void *user)
{
	int i;
	isl_vertex *vertex;

	if (!cell)
		return isl_stat_error;

	if (cell->n_vertices == 0)
		return isl_stat_ok;

	for (i = 0; i < cell->n_vertices; ++i) {
		isl_stat r;

		vertex = isl_vertex_alloc(isl_vertices_copy(cell->vertices),
					  cell->ids[i]);
		if (!vertex)
			return isl_stat_error;

		r = fn(vertex, user);
		if (r < 0)
			return isl_stat_error;
	}

	return isl_stat_ok;
}

isl_ctx *isl_vertices_get_ctx(__isl_keep isl_vertices *vertices)
{
	return vertices ? vertices->bset->ctx : NULL;
}

isl_size isl_vertices_get_n_vertices(__isl_keep isl_vertices *vertices)
{
	return vertices ? vertices->n_vertices : isl_size_error;
}

__isl_give isl_vertices *isl_morph_vertices(__isl_take isl_morph *morph,
	__isl_take isl_vertices *vertices)
{
	int i;
	isl_morph *param_morph = NULL;

	if (!morph || !vertices)
		goto error;

	isl_assert(vertices->bset->ctx, vertices->ref == 1, goto error);

	param_morph = isl_morph_copy(morph);
	param_morph = isl_morph_dom_params(param_morph);
	param_morph = isl_morph_ran_params(param_morph);

	for (i = 0; i < vertices->n_vertices; ++i) {
		vertices->v[i].dom = isl_morph_basic_set(
			isl_morph_copy(param_morph), vertices->v[i].dom);
		vertices->v[i].vertex = isl_morph_basic_set(
			isl_morph_copy(morph), vertices->v[i].vertex);
		if (!vertices->v[i].vertex)
			goto error;
	}

	for (i = 0; i < vertices->n_chambers; ++i) {
		vertices->c[i].dom = isl_morph_basic_set(
			isl_morph_copy(param_morph), vertices->c[i].dom);
		if (!vertices->c[i].dom)
			goto error;
	}

	isl_morph_free(param_morph);
	isl_morph_free(morph);
	return vertices;
error:
	isl_morph_free(param_morph);
	isl_morph_free(morph);
	isl_vertices_free(vertices);
	return NULL;
}

/* Construct a simplex isl_cell spanned by the vertices with indices in
 * "simplex_ids" and "other_ids" and call "fn" on this isl_cell.
 */
static isl_stat call_on_simplex(__isl_keep isl_cell *cell,
	int *simplex_ids, int n_simplex, int *other_ids, int n_other,
	isl_stat (*fn)(__isl_take isl_cell *simplex, void *user), void *user)
{
	int i;
	isl_ctx *ctx;
	struct isl_cell *simplex;

	ctx = isl_cell_get_ctx(cell);

	simplex = isl_calloc_type(ctx, struct isl_cell);
	if (!simplex)
		return isl_stat_error;
	simplex->vertices = isl_vertices_copy(cell->vertices);
	if (!simplex->vertices)
		goto error;
	simplex->dom = isl_basic_set_copy(cell->dom);
	if (!simplex->dom)
		goto error;
	simplex->n_vertices = n_simplex + n_other;
	simplex->ids = isl_alloc_array(ctx, int, simplex->n_vertices);
	if (!simplex->ids)
		goto error;

	for (i = 0; i < n_simplex; ++i)
		simplex->ids[i] = simplex_ids[i];
	for (i = 0; i < n_other; ++i)
		simplex->ids[n_simplex + i] = other_ids[i];

	return fn(simplex, user);
error:
	isl_cell_free(simplex);
	return isl_stat_error;
}

/* Check whether the parametric vertex described by "vertex"
 * lies on the facet corresponding to constraint "facet" of "bset".
 * The isl_vec "v" is a temporary vector than can be used by this function.
 *
 * We eliminate the variables from the facet constraint using the
 * equalities defining the vertex and check if the result is identical
 * to zero.
 *
 * It would probably be better to keep track of the constraints defining
 * a vertex during the vertex construction so that we could simply look
 * it up here.
 */
static int vertex_on_facet(__isl_keep isl_basic_set *vertex,
	__isl_keep isl_basic_set *bset, int facet, __isl_keep isl_vec *v)
{
	int i;
	isl_int m;

	isl_seq_cpy(v->el, bset->ineq[facet], v->size);

	isl_int_init(m);
	for (i = 0; i < vertex->n_eq; ++i) {
		int k = isl_seq_last_non_zero(vertex->eq[i], v->size);
		isl_seq_elim(v->el, vertex->eq[i], k, v->size, &m);
	}
	isl_int_clear(m);

	return isl_seq_first_non_zero(v->el, v->size) == -1;
}

/* Triangulate the polytope spanned by the vertices with ids
 * in "simplex_ids" and "other_ids" and call "fn" on each of
 * the resulting simplices.
 * If the input polytope is already a simplex, we simply call "fn".
 * Otherwise, we pick a point from "other_ids" and add it to "simplex_ids".
 * Then we consider each facet of "bset" that does not contain the point
 * we just picked, but does contain some of the other points in "other_ids"
 * and call ourselves recursively on the polytope spanned by the new
 * "simplex_ids" and those points in "other_ids" that lie on the facet.
 */
static isl_stat triangulate(__isl_keep isl_cell *cell, __isl_keep isl_vec *v,
	int *simplex_ids, int n_simplex, int *other_ids, int n_other,
	isl_stat (*fn)(__isl_take isl_cell *simplex, void *user), void *user)
{
	int i, j, k;
	isl_size d, nparam;
	int *ids;
	isl_ctx *ctx;
	isl_basic_set *vertex;
	isl_basic_set *bset;

	ctx = isl_cell_get_ctx(cell);
	d = isl_basic_set_dim(cell->vertices->bset, isl_dim_set);
	nparam = isl_basic_set_dim(cell->vertices->bset, isl_dim_param);
	if (d < 0 || nparam < 0)
		return isl_stat_error;

	if (n_simplex + n_other == d + 1)
		return call_on_simplex(cell, simplex_ids, n_simplex,
				       other_ids, n_other, fn, user);

	simplex_ids[n_simplex] = other_ids[0];
	vertex = cell->vertices->v[other_ids[0]].vertex;
	bset = cell->vertices->bset;

	ids = isl_alloc_array(ctx, int, n_other - 1);
	if (!ids)
		goto error;
	for (i = 0; i < bset->n_ineq; ++i) {
		if (isl_seq_first_non_zero(bset->ineq[i] + 1 + nparam, d) == -1)
			continue;
		if (vertex_on_facet(vertex, bset, i, v))
			continue;

		for (j = 1, k = 0; j < n_other; ++j) {
			isl_basic_set *ov;
			ov = cell->vertices->v[other_ids[j]].vertex;
			if (vertex_on_facet(ov, bset, i, v))
				ids[k++] = other_ids[j];
		}
		if (k == 0)
			continue;

		if (triangulate(cell, v, simplex_ids, n_simplex + 1,
				ids, k, fn, user) < 0)
			goto error;
	}
	free(ids);

	return isl_stat_ok;
error:
	free(ids);
	return isl_stat_error;
}

/* Triangulate the given cell and call "fn" on each of the resulting
 * simplices.
 */
isl_stat isl_cell_foreach_simplex(__isl_take isl_cell *cell,
	isl_stat (*fn)(__isl_take isl_cell *simplex, void *user), void *user)
{
	isl_size d, total;
	isl_stat r;
	isl_ctx *ctx;
	isl_vec *v = NULL;
	int *simplex_ids = NULL;

	if (!cell)
		return isl_stat_error;

	d = isl_basic_set_dim(cell->vertices->bset, isl_dim_set);
	total = isl_basic_set_dim(cell->vertices->bset, isl_dim_all);
	if (d < 0 || total < 0)
		return isl_stat_error;

	if (cell->n_vertices == d + 1)
		return fn(cell, user);

	ctx = isl_cell_get_ctx(cell);
	simplex_ids = isl_alloc_array(ctx, int, d + 1);
	if (!simplex_ids)
		goto error;

	v = isl_vec_alloc(ctx, 1 + total);
	if (!v)
		goto error;

	r = triangulate(cell, v, simplex_ids, 0,
			cell->ids, cell->n_vertices, fn, user);

	isl_vec_free(v);
	free(simplex_ids);

	isl_cell_free(cell);

	return r;
error:
	free(simplex_ids);
	isl_vec_free(v);
	isl_cell_free(cell);
	return isl_stat_error;
}