isl_list_templ.c 15.9 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
/*
 * Copyright 2008-2009 Katholieke Universiteit Leuven
 * Copyright 2011      INRIA Saclay
 * Copyright 2012-2013 Ecole Normale Superieure
 * Copyright 2017      Sven Verdoolaege
 *
 * 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
 */

#include <isl_sort.h>
#include <isl_tarjan.h>
#include <isl/printer.h>

#include <isl_list_macro.h>

#define xS(TYPE,NAME) struct TYPE ## _ ## NAME
#define S(TYPE,NAME) xS(TYPE,NAME)

isl_ctx *FN(LIST(EL),get_ctx)(__isl_keep LIST(EL) *list)
{
	return list ? list->ctx : NULL;
}

__isl_give LIST(EL) *FN(LIST(EL),alloc)(isl_ctx *ctx, int n)
{
	LIST(EL) *list;

	if (n < 0)
		isl_die(ctx, isl_error_invalid,
			"cannot create list of negative length",
			return NULL);
	list = isl_alloc(ctx, LIST(EL),
			 sizeof(LIST(EL)) + (n - 1) * sizeof(struct EL *));
	if (!list)
		return NULL;

	list->ctx = ctx;
	isl_ctx_ref(ctx);
	list->ref = 1;
	list->size = n;
	list->n = 0;
	return list;
}

__isl_give LIST(EL) *FN(LIST(EL),copy)(__isl_keep LIST(EL) *list)
{
	if (!list)
		return NULL;

	list->ref++;
	return list;
}

__isl_give LIST(EL) *FN(LIST(EL),dup)(__isl_keep LIST(EL) *list)
{
	int i;
	LIST(EL) *dup;

	if (!list)
		return NULL;

	dup = FN(LIST(EL),alloc)(FN(LIST(EL),get_ctx)(list), list->n);
	if (!dup)
		return NULL;
	for (i = 0; i < list->n; ++i)
		dup = FN(LIST(EL),add)(dup, FN(EL,copy)(list->p[i]));
	return dup;
}

__isl_give LIST(EL) *FN(LIST(EL),cow)(__isl_take LIST(EL) *list)
{
	if (!list)
		return NULL;

	if (list->ref == 1)
		return list;
	list->ref--;
	return FN(LIST(EL),dup)(list);
}

/* Make sure "list" has room for at least "n" more pieces.
 * Always return a list with a single reference.
 *
 * If there is only one reference to list, we extend it in place.
 * Otherwise, we create a new LIST(EL) and copy the elements.
 */
static __isl_give LIST(EL) *FN(LIST(EL),grow)(__isl_take LIST(EL) *list, int n)
{
	isl_ctx *ctx;
	int i, new_size;
	LIST(EL) *res;

	if (!list)
		return NULL;
	if (list->ref == 1 && list->n + n <= list->size)
		return list;

	ctx = FN(LIST(EL),get_ctx)(list);
	new_size = ((list->n + n + 1) * 3) / 2;
	if (list->ref == 1) {
		res = isl_realloc(ctx, list, LIST(EL),
			    sizeof(LIST(EL)) + (new_size - 1) * sizeof(EL *));
		if (!res)
			return FN(LIST(EL),free)(list);
		res->size = new_size;
		return res;
	}

	if (list->n + n <= list->size && list->size < new_size)
		new_size = list->size;

	res = FN(LIST(EL),alloc)(ctx, new_size);
	if (!res)
		return FN(LIST(EL),free)(list);

	for (i = 0; i < list->n; ++i)
		res = FN(LIST(EL),add)(res, FN(EL,copy)(list->p[i]));

	FN(LIST(EL),free)(list);
	return res;
}

/* Check that "index" is a valid position in "list".
 */
static isl_stat FN(LIST(EL),check_index)(__isl_keep LIST(EL) *list, int index)
{
	if (!list)
		return isl_stat_error;
	if (index < 0 || index >= list->n)
		isl_die(FN(LIST(EL),get_ctx)(list), isl_error_invalid,
			"index out of bounds", return isl_stat_error);
	return isl_stat_ok;
}

__isl_give LIST(EL) *FN(LIST(EL),add)(__isl_take LIST(EL) *list,
	__isl_take struct EL *el)
{
	list = FN(LIST(EL),grow)(list, 1);
	if (!list || !el)
		goto error;
	list->p[list->n] = el;
	list->n++;
	return list;
error:
	FN(EL,free)(el);
	FN(LIST(EL),free)(list);
	return NULL;
}

/* Remove the "n" elements starting at "first" from "list".
 */
__isl_give LIST(EL) *FN(LIST(EL),drop)(__isl_take LIST(EL) *list,
	unsigned first, unsigned n)
{
	int i;

	if (!list)
		return NULL;
	if (first + n > list->n || first + n < first)
		isl_die(list->ctx, isl_error_invalid,
			"index out of bounds", return FN(LIST(EL),free)(list));
	if (n == 0)
		return list;
	list = FN(LIST(EL),cow)(list);
	if (!list)
		return NULL;
	for (i = 0; i < n; ++i)
		FN(EL,free)(list->p[first + i]);
	for (i = first; i + n < list->n; ++i)
		list->p[i] = list->p[i + n];
	list->n -= n;
	return list;
}

/* Remove all elements from "list".
 */
__isl_give LIST(EL) *FN(LIST(EL),clear)(__isl_take LIST(EL) *list)
{
	if (!list)
		return NULL;
	return FN(LIST(EL),drop)(list, 0, list->n);
}

/* Insert "el" at position "pos" in "list".
 *
 * If there is only one reference to "list" and if it already has space
 * for one extra element, we insert it directly into "list".
 * Otherwise, we create a new list consisting of "el" and copied
 * elements from "list".
 */
__isl_give LIST(EL) *FN(LIST(EL),insert)(__isl_take LIST(EL) *list,
	unsigned pos, __isl_take struct EL *el)
{
	int i;
	isl_ctx *ctx;
	LIST(EL) *res;

	if (!list || !el)
		goto error;
	ctx = FN(LIST(EL),get_ctx)(list);
	if (pos > list->n)
		isl_die(ctx, isl_error_invalid,
			"index out of bounds", goto error);

	if (list->ref == 1 && list->size > list->n) {
		for (i = list->n; i > pos; --i)
			list->p[i] = list->p[i - 1];
		list->n++;
		list->p[pos] = el;
		return list;
	}

	res = FN(LIST(EL),alloc)(ctx, list->n + 1);
	for (i = 0; i < pos; ++i)
		res = FN(LIST(EL),add)(res, FN(EL,copy)(list->p[i]));
	res = FN(LIST(EL),add)(res, el);
	for (i = pos; i < list->n; ++i)
		res = FN(LIST(EL),add)(res, FN(EL,copy)(list->p[i]));
	FN(LIST(EL),free)(list);

	return res;
error:
	FN(EL,free)(el);
	FN(LIST(EL),free)(list);
	return NULL;
}

__isl_null LIST(EL) *FN(LIST(EL),free)(__isl_take LIST(EL) *list)
{
	int i;

	if (!list)
		return NULL;

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

	isl_ctx_deref(list->ctx);
	for (i = 0; i < list->n; ++i)
		FN(EL,free)(list->p[i]);
	free(list);

	return NULL;
}

/* Return the number of elements in "list".
 */
isl_size FN(LIST(EL),size)(__isl_keep LIST(EL) *list)
{
	return list ? list->n : isl_size_error;
}

/* This is an alternative name for the function above.
 */
isl_size FN(FN(LIST(EL),n),EL_BASE)(__isl_keep LIST(EL) *list)
{
	return FN(LIST(EL),size)(list);
}

/* Return the element at position "index" in "list".
 */
static __isl_keep EL *FN(LIST(EL),peek)(__isl_keep LIST(EL) *list, int index)
{
	if (FN(LIST(EL),check_index)(list, index) < 0)
		return NULL;
	return list->p[index];
}

/* Return a copy of the element at position "index" in "list".
 */
__isl_give EL *FN(LIST(EL),get_at)(__isl_keep LIST(EL) *list, int index)
{
	return FN(EL,copy)(FN(LIST(EL),peek)(list, index));
}

/* This is an alternative name for the function above.
 */
__isl_give EL *FN(FN(LIST(EL),get),EL_BASE)(__isl_keep LIST(EL) *list,
	int index)
{
	return FN(LIST(EL),get_at)(list, index);
}

/* Replace the element at position "index" in "list" by "el".
 */
__isl_give LIST(EL) *FN(FN(LIST(EL),set),EL_BASE)(__isl_take LIST(EL) *list,
	int index, __isl_take EL *el)
{
	if (!list || !el)
		goto error;
	if (FN(LIST(EL),check_index)(list, index) < 0)
		goto error;
	if (list->p[index] == el) {
		FN(EL,free)(el);
		return list;
	}
	list = FN(LIST(EL),cow)(list);
	if (!list)
		goto error;
	FN(EL,free)(list->p[index]);
	list->p[index] = el;
	return list;
error:
	FN(EL,free)(el);
	FN(LIST(EL),free)(list);
	return NULL;
}

/* Return the element at position "index" of "list".
 * This may be either a copy or the element itself
 * if there is only one reference to "list".
 * This allows the element to be modified inplace
 * if both the list and the element have only a single reference.
 * The caller is not allowed to modify "list" between
 * this call to isl_list_*_take_* and a subsequent call
 * to isl_list_*_restore_*.
 * The only exception is that isl_list_*_free can be called instead.
 */
static __isl_give EL *FN(FN(LIST(EL),take),EL_BASE)(__isl_keep LIST(EL) *list,
	int index)
{
	EL *el;

	if (FN(LIST(EL),check_index)(list, index) < 0)
		return NULL;
	if (list->ref != 1)
		return FN(FN(LIST(EL),get),EL_BASE)(list, index);
	el = list->p[index];
	list->p[index] = NULL;
	return el;
}

/* Set the element at position "index" of "list" to "el",
 * where the position may be empty due to a previous call
 * to isl_list_*_take_*.
 */
static __isl_give LIST(EL) *FN(FN(LIST(EL),restore),EL_BASE)(
	__isl_take LIST(EL) *list, int index, __isl_take EL *el)
{
	return FN(FN(LIST(EL),set),EL_BASE)(list, index, el);
}

/* Swap the elements of "list" in positions "pos1" and "pos2".
 */
__isl_give LIST(EL) *FN(LIST(EL),swap)(__isl_take LIST(EL) *list,
	unsigned pos1, unsigned pos2)
{
	EL *el1, *el2;

	if (pos1 == pos2)
		return list;
	el1 = FN(FN(LIST(EL),take),EL_BASE)(list, pos1);
	el2 = FN(FN(LIST(EL),take),EL_BASE)(list, pos2);
	list = FN(FN(LIST(EL),restore),EL_BASE)(list, pos1, el2);
	list = FN(FN(LIST(EL),restore),EL_BASE)(list, pos2, el1);
	return list;
}

/* Reverse the elements of "list".
 */
__isl_give LIST(EL) *FN(LIST(EL),reverse)(__isl_take LIST(EL) *list)
{
	int i, n;

	n = FN(LIST(EL),size)(list);
	for (i = 0; i < n - 1 - i; ++i)
		list = FN(LIST(EL),swap)(list, i, n - 1 - i);
	return list;
}

isl_stat FN(LIST(EL),foreach)(__isl_keep LIST(EL) *list,
	isl_stat (*fn)(__isl_take EL *el, void *user), void *user)
{
	int i;

	if (!list)
		return isl_stat_error;

	for (i = 0; i < list->n; ++i) {
		EL *el = FN(EL,copy)(list->p[i]);
		if (!el)
			return isl_stat_error;
		if (fn(el, user) < 0)
			return isl_stat_error;
	}

	return isl_stat_ok;
}

/* Does "test" succeed on every element of "list"?
 */
isl_bool FN(LIST(EL),every)(__isl_keep LIST(EL) *list,
	isl_bool (*test)(__isl_keep EL *el, void *user), void *user)
{
	int i;

	if (!list)
		return isl_bool_error;

	for (i = 0; i < list->n; ++i) {
		isl_bool r;

		r = test(list->p[i], user);
		if (r < 0 || !r)
			return r;
	}

	return isl_bool_true;
}

/* Replace each element in "list" by the result of calling "fn"
 * on the element.
 */
__isl_give LIST(EL) *FN(LIST(EL),map)(__isl_keep LIST(EL) *list,
	__isl_give EL *(*fn)(__isl_take EL *el, void *user), void *user)
{
	int i, n;

	if (!list)
		return NULL;

	n = list->n;
	for (i = 0; i < n; ++i) {
		EL *el = FN(FN(LIST(EL),take),EL_BASE)(list, i);
		if (!el)
			return FN(LIST(EL),free)(list);
		el = fn(el, user);
		list = FN(FN(LIST(EL),restore),EL_BASE)(list, i, el);
	}

	return list;
}

/* Internal data structure for isl_*_list_sort.
 *
 * "cmp" is the original comparison function.
 * "user" is a user provided pointer that should be passed to "cmp".
 */
S(LIST(EL),sort_data) {
	int (*cmp)(__isl_keep EL *a, __isl_keep EL *b, void *user);
	void *user;
};

/* Compare two entries of an isl_*_list based on the user provided
 * comparison function on pairs of isl_* objects.
 */
static int FN(LIST(EL),cmp)(const void *a, const void *b, void *user)
{
	S(LIST(EL),sort_data) *data = user;
	EL * const *el1 = a;
	EL * const *el2 = b;

	return data->cmp(*el1, *el2, data->user);
}

/* Sort the elements of "list" in ascending order according to
 * comparison function "cmp".
 */
__isl_give LIST(EL) *FN(LIST(EL),sort)(__isl_take LIST(EL) *list,
	int (*cmp)(__isl_keep EL *a, __isl_keep EL *b, void *user), void *user)
{
	S(LIST(EL),sort_data) data = { cmp, user };

	if (!list)
		return NULL;
	if (list->n <= 1)
		return list;
	list = FN(LIST(EL),cow)(list);
	if (!list)
		return NULL;

	if (isl_sort(list->p, list->n, sizeof(list->p[0]),
			&FN(LIST(EL),cmp), &data) < 0)
		return FN(LIST(EL),free)(list);

	return list;
}

/* Internal data structure for isl_*_list_foreach_scc.
 *
 * "list" is the original list.
 * "follows" is the user provided callback that defines the edges of the graph.
 */
S(LIST(EL),foreach_scc_data) {
	LIST(EL) *list;
	isl_bool (*follows)(__isl_keep EL *a, __isl_keep EL *b, void *user);
	void *follows_user;
};

/* Does element i of data->list follow element j?
 *
 * Use the user provided callback to find out.
 */
static isl_bool FN(LIST(EL),follows)(int i, int j, void *user)
{
	S(LIST(EL),foreach_scc_data) *data = user;

	return data->follows(data->list->p[i], data->list->p[j],
				data->follows_user);
}

/* Call "fn" on the sublist of "list" that consists of the elements
 * with indices specified by the "n" elements of "pos".
 */
static isl_stat FN(LIST(EL),call_on_scc)(__isl_keep LIST(EL) *list, int *pos,
	int n, isl_stat (*fn)(__isl_take LIST(EL) *scc, void *user), void *user)
{
	int i;
	isl_ctx *ctx;
	LIST(EL) *slice;

	ctx = FN(LIST(EL),get_ctx)(list);
	slice = FN(LIST(EL),alloc)(ctx, n);
	for (i = 0; i < n; ++i) {
		EL *el;

		el = FN(EL,copy)(list->p[pos[i]]);
		slice = FN(LIST(EL),add)(slice, el);
	}

	return fn(slice, user);
}

/* Call "fn" on each of the strongly connected components (SCCs) of
 * the graph with as vertices the elements of "list" and
 * a directed edge from node b to node a iff follows(a, b)
 * returns 1.  follows should return -1 on error.
 *
 * If SCC a contains a node i that follows a node j in another SCC b
 * (i.e., follows(i, j, user) returns 1), then fn will be called on SCC a
 * after being called on SCC b.
 *
 * We simply call isl_tarjan_graph_init, extract the SCCs from the result and
 * call fn on each of them.
 */
isl_stat FN(LIST(EL),foreach_scc)(__isl_keep LIST(EL) *list,
	isl_bool (*follows)(__isl_keep EL *a, __isl_keep EL *b, void *user),
	void *follows_user,
	isl_stat (*fn)(__isl_take LIST(EL) *scc, void *user), void *fn_user)
{
	S(LIST(EL),foreach_scc_data) data = { list, follows, follows_user };
	int i, n;
	isl_ctx *ctx;
	struct isl_tarjan_graph *g;

	if (!list)
		return isl_stat_error;
	if (list->n == 0)
		return isl_stat_ok;
	if (list->n == 1)
		return fn(FN(LIST(EL),copy)(list), fn_user);

	ctx = FN(LIST(EL),get_ctx)(list);
	n = list->n;
	g = isl_tarjan_graph_init(ctx, n, &FN(LIST(EL),follows), &data);
	if (!g)
		return isl_stat_error;

	i = 0;
	do {
		int first;

		if (g->order[i] == -1)
			isl_die(ctx, isl_error_internal, "cannot happen",
				break);
		first = i;
		while (g->order[i] != -1) {
			++i; --n;
		}
		if (first == 0 && n == 0) {
			isl_tarjan_graph_free(g);
			return fn(FN(LIST(EL),copy)(list), fn_user);
		}
		if (FN(LIST(EL),call_on_scc)(list, g->order + first, i - first,
					    fn, fn_user) < 0)
			break;
		++i;
	} while (n);

	isl_tarjan_graph_free(g);

	return n > 0 ? isl_stat_error : isl_stat_ok;
}

__isl_give LIST(EL) *FN(FN(LIST(EL),from),EL_BASE)(__isl_take EL *el)
{
	isl_ctx *ctx;
	LIST(EL) *list;

	if (!el)
		return NULL;
	ctx = FN(EL,get_ctx)(el);
	list = FN(LIST(EL),alloc)(ctx, 1);
	if (!list)
		goto error;
	list = FN(LIST(EL),add)(list, el);
	return list;
error:
	FN(EL,free)(el);
	return NULL;
}

/* Append the elements of "list2" to "list1", where "list1" is known
 * to have only a single reference and enough room to hold
 * the extra elements.
 */
static __isl_give LIST(EL) *FN(LIST(EL),concat_inplace)(
	__isl_take LIST(EL) *list1, __isl_take LIST(EL) *list2)
{
	int i;

	for (i = 0; i < list2->n; ++i)
		list1 = FN(LIST(EL),add)(list1, FN(EL,copy)(list2->p[i]));
	FN(LIST(EL),free)(list2);
	return list1;
}

/* Concatenate "list1" and "list2".
 * If "list1" has only one reference and has enough room
 * for the elements of "list2", the add the elements to "list1" itself.
 * Otherwise, create a new list to store the result.
 */
__isl_give LIST(EL) *FN(LIST(EL),concat)(__isl_take LIST(EL) *list1,
	__isl_take LIST(EL) *list2)
{
	int i;
	isl_ctx *ctx;
	LIST(EL) *res;

	if (!list1 || !list2)
		goto error;

	if (list1->ref == 1 && list1->n + list2->n <= list1->size)
		return FN(LIST(EL),concat_inplace)(list1, list2);

	ctx = FN(LIST(EL),get_ctx)(list1);
	res = FN(LIST(EL),alloc)(ctx, list1->n + list2->n);
	for (i = 0; i < list1->n; ++i)
		res = FN(LIST(EL),add)(res, FN(EL,copy)(list1->p[i]));
	for (i = 0; i < list2->n; ++i)
		res = FN(LIST(EL),add)(res, FN(EL,copy)(list2->p[i]));

	FN(LIST(EL),free)(list1);
	FN(LIST(EL),free)(list2);
	return res;
error:
	FN(LIST(EL),free)(list1);
	FN(LIST(EL),free)(list2);
	return NULL;
}

__isl_give isl_printer *CAT(isl_printer_print_,LIST(EL_BASE))(
	__isl_take isl_printer *p, __isl_keep LIST(EL) *list)
{
	int i;

	if (!p || !list)
		goto error;
	p = isl_printer_print_str(p, "(");
	for (i = 0; i < list->n; ++i) {
		if (i)
			p = isl_printer_print_str(p, ",");
		p = CAT(isl_printer_print_,EL_BASE)(p, list->p[i]);
	}
	p = isl_printer_print_str(p, ")");
	return p;
error:
	isl_printer_free(p);
	return NULL;
}

#undef BASE
#define BASE LIST(EL_BASE)

#define PRINT_DUMP_DEFAULT	0
#include "print_templ.c"
#undef PRINT_DUMP_DEFAULT