isl_space_is_equal: use isl_space_has_equal_tuples
[isl.git] / isl_space.c
bloba9683d0c676a1a72c4821131ef86df3b8bfa2607
1 /*
2 * Copyright 2008-2009 Katholieke Universiteit Leuven
3 * Copyright 2010 INRIA Saclay
4 * Copyright 2013-2014 Ecole Normale Superieure
6 * Use of this software is governed by the MIT license
8 * Written by Sven Verdoolaege, K.U.Leuven, Departement
9 * Computerwetenschappen, Celestijnenlaan 200A, B-3001 Leuven, Belgium
10 * and INRIA Saclay - Ile-de-France, Parc Club Orsay Universite,
11 * ZAC des vignes, 4 rue Jacques Monod, 91893 Orsay, France
12 * and Ecole Normale Superieure, 45 rue d’Ulm, 75230 Paris, France
15 #include <stdlib.h>
16 #include <string.h>
17 #include <isl_space_private.h>
18 #include <isl_id_private.h>
19 #include <isl_reordering.h>
21 isl_ctx *isl_space_get_ctx(__isl_keep isl_space *dim)
23 return dim ? dim->ctx : NULL;
26 __isl_give isl_space *isl_space_alloc(isl_ctx *ctx,
27 unsigned nparam, unsigned n_in, unsigned n_out)
29 isl_space *dim;
31 dim = isl_alloc_type(ctx, struct isl_space);
32 if (!dim)
33 return NULL;
35 dim->ctx = ctx;
36 isl_ctx_ref(ctx);
37 dim->ref = 1;
38 dim->nparam = nparam;
39 dim->n_in = n_in;
40 dim->n_out = n_out;
42 dim->tuple_id[0] = NULL;
43 dim->tuple_id[1] = NULL;
45 dim->nested[0] = NULL;
46 dim->nested[1] = NULL;
48 dim->n_id = 0;
49 dim->ids = NULL;
51 return dim;
54 /* Mark the space as being that of a set, by setting the domain tuple
55 * to isl_id_none.
57 static __isl_give isl_space *mark_as_set(__isl_take isl_space *space)
59 space = isl_space_cow(space);
60 if (!space)
61 return NULL;
62 space = isl_space_set_tuple_id(space, isl_dim_in, &isl_id_none);
63 return space;
66 /* Is the space that of a set?
68 isl_bool isl_space_is_set(__isl_keep isl_space *space)
70 if (!space)
71 return isl_bool_error;
72 if (space->n_in != 0 || space->nested[0])
73 return isl_bool_false;
74 if (space->tuple_id[0] != &isl_id_none)
75 return isl_bool_false;
76 return isl_bool_true;
79 /* Is the given space that of a map?
81 isl_bool isl_space_is_map(__isl_keep isl_space *space)
83 if (!space)
84 return isl_bool_error;
85 return space->tuple_id[0] != &isl_id_none &&
86 space->tuple_id[1] != &isl_id_none;
89 __isl_give isl_space *isl_space_set_alloc(isl_ctx *ctx,
90 unsigned nparam, unsigned dim)
92 isl_space *space;
93 space = isl_space_alloc(ctx, nparam, 0, dim);
94 space = mark_as_set(space);
95 return space;
98 /* Mark the space as being that of a parameter domain, by setting
99 * both tuples to isl_id_none.
101 static __isl_give isl_space *mark_as_params(isl_space *space)
103 if (!space)
104 return NULL;
105 space = isl_space_set_tuple_id(space, isl_dim_in, &isl_id_none);
106 space = isl_space_set_tuple_id(space, isl_dim_out, &isl_id_none);
107 return space;
110 /* Is the space that of a parameter domain?
112 isl_bool isl_space_is_params(__isl_keep isl_space *space)
114 if (!space)
115 return isl_bool_error;
116 if (space->n_in != 0 || space->nested[0] ||
117 space->n_out != 0 || space->nested[1])
118 return isl_bool_false;
119 if (space->tuple_id[0] != &isl_id_none)
120 return isl_bool_false;
121 if (space->tuple_id[1] != &isl_id_none)
122 return isl_bool_false;
123 return isl_bool_true;
126 /* Create a space for a parameter domain.
128 __isl_give isl_space *isl_space_params_alloc(isl_ctx *ctx, unsigned nparam)
130 isl_space *space;
131 space = isl_space_alloc(ctx, nparam, 0, 0);
132 space = mark_as_params(space);
133 return space;
136 static unsigned global_pos(__isl_keep isl_space *dim,
137 enum isl_dim_type type, unsigned pos)
139 struct isl_ctx *ctx = dim->ctx;
141 switch (type) {
142 case isl_dim_param:
143 isl_assert(ctx, pos < dim->nparam,
144 return isl_space_dim(dim, isl_dim_all));
145 return pos;
146 case isl_dim_in:
147 isl_assert(ctx, pos < dim->n_in,
148 return isl_space_dim(dim, isl_dim_all));
149 return pos + dim->nparam;
150 case isl_dim_out:
151 isl_assert(ctx, pos < dim->n_out,
152 return isl_space_dim(dim, isl_dim_all));
153 return pos + dim->nparam + dim->n_in;
154 default:
155 isl_assert(ctx, 0, return isl_space_dim(dim, isl_dim_all));
157 return isl_space_dim(dim, isl_dim_all);
160 /* Extend length of ids array to the total number of dimensions.
162 static __isl_give isl_space *extend_ids(__isl_take isl_space *dim)
164 isl_id **ids;
165 int i;
167 if (isl_space_dim(dim, isl_dim_all) <= dim->n_id)
168 return dim;
170 if (!dim->ids) {
171 dim->ids = isl_calloc_array(dim->ctx,
172 isl_id *, isl_space_dim(dim, isl_dim_all));
173 if (!dim->ids)
174 goto error;
175 } else {
176 ids = isl_realloc_array(dim->ctx, dim->ids,
177 isl_id *, isl_space_dim(dim, isl_dim_all));
178 if (!ids)
179 goto error;
180 dim->ids = ids;
181 for (i = dim->n_id; i < isl_space_dim(dim, isl_dim_all); ++i)
182 dim->ids[i] = NULL;
185 dim->n_id = isl_space_dim(dim, isl_dim_all);
187 return dim;
188 error:
189 isl_space_free(dim);
190 return NULL;
193 static __isl_give isl_space *set_id(__isl_take isl_space *dim,
194 enum isl_dim_type type, unsigned pos, __isl_take isl_id *id)
196 dim = isl_space_cow(dim);
198 if (!dim)
199 goto error;
201 pos = global_pos(dim, type, pos);
202 if (pos == isl_space_dim(dim, isl_dim_all))
203 goto error;
205 if (pos >= dim->n_id) {
206 if (!id)
207 return dim;
208 dim = extend_ids(dim);
209 if (!dim)
210 goto error;
213 dim->ids[pos] = id;
215 return dim;
216 error:
217 isl_id_free(id);
218 isl_space_free(dim);
219 return NULL;
222 static __isl_keep isl_id *get_id(__isl_keep isl_space *dim,
223 enum isl_dim_type type, unsigned pos)
225 if (!dim)
226 return NULL;
228 pos = global_pos(dim, type, pos);
229 if (pos == isl_space_dim(dim, isl_dim_all))
230 return NULL;
231 if (pos >= dim->n_id)
232 return NULL;
233 return dim->ids[pos];
236 static unsigned offset(__isl_keep isl_space *dim, enum isl_dim_type type)
238 switch (type) {
239 case isl_dim_param: return 0;
240 case isl_dim_in: return dim->nparam;
241 case isl_dim_out: return dim->nparam + dim->n_in;
242 default: return 0;
246 static unsigned n(__isl_keep isl_space *dim, enum isl_dim_type type)
248 switch (type) {
249 case isl_dim_param: return dim->nparam;
250 case isl_dim_in: return dim->n_in;
251 case isl_dim_out: return dim->n_out;
252 case isl_dim_all: return dim->nparam + dim->n_in + dim->n_out;
253 default: return 0;
257 unsigned isl_space_dim(__isl_keep isl_space *dim, enum isl_dim_type type)
259 if (!dim)
260 return 0;
261 return n(dim, type);
264 unsigned isl_space_offset(__isl_keep isl_space *dim, enum isl_dim_type type)
266 if (!dim)
267 return 0;
268 return offset(dim, type);
271 static __isl_give isl_space *copy_ids(__isl_take isl_space *dst,
272 enum isl_dim_type dst_type, unsigned offset, __isl_keep isl_space *src,
273 enum isl_dim_type src_type)
275 int i;
276 isl_id *id;
278 if (!dst)
279 return NULL;
281 for (i = 0; i < n(src, src_type); ++i) {
282 id = get_id(src, src_type, i);
283 if (!id)
284 continue;
285 dst = set_id(dst, dst_type, offset + i, isl_id_copy(id));
286 if (!dst)
287 return NULL;
289 return dst;
292 __isl_take isl_space *isl_space_dup(__isl_keep isl_space *dim)
294 isl_space *dup;
295 if (!dim)
296 return NULL;
297 dup = isl_space_alloc(dim->ctx, dim->nparam, dim->n_in, dim->n_out);
298 if (!dup)
299 return NULL;
300 if (dim->tuple_id[0] &&
301 !(dup->tuple_id[0] = isl_id_copy(dim->tuple_id[0])))
302 goto error;
303 if (dim->tuple_id[1] &&
304 !(dup->tuple_id[1] = isl_id_copy(dim->tuple_id[1])))
305 goto error;
306 if (dim->nested[0] && !(dup->nested[0] = isl_space_copy(dim->nested[0])))
307 goto error;
308 if (dim->nested[1] && !(dup->nested[1] = isl_space_copy(dim->nested[1])))
309 goto error;
310 if (!dim->ids)
311 return dup;
312 dup = copy_ids(dup, isl_dim_param, 0, dim, isl_dim_param);
313 dup = copy_ids(dup, isl_dim_in, 0, dim, isl_dim_in);
314 dup = copy_ids(dup, isl_dim_out, 0, dim, isl_dim_out);
315 return dup;
316 error:
317 isl_space_free(dup);
318 return NULL;
321 __isl_give isl_space *isl_space_cow(__isl_take isl_space *dim)
323 if (!dim)
324 return NULL;
326 if (dim->ref == 1)
327 return dim;
328 dim->ref--;
329 return isl_space_dup(dim);
332 __isl_give isl_space *isl_space_copy(__isl_keep isl_space *dim)
334 if (!dim)
335 return NULL;
337 dim->ref++;
338 return dim;
341 __isl_null isl_space *isl_space_free(__isl_take isl_space *space)
343 int i;
345 if (!space)
346 return NULL;
348 if (--space->ref > 0)
349 return NULL;
351 isl_id_free(space->tuple_id[0]);
352 isl_id_free(space->tuple_id[1]);
354 isl_space_free(space->nested[0]);
355 isl_space_free(space->nested[1]);
357 for (i = 0; i < space->n_id; ++i)
358 isl_id_free(space->ids[i]);
359 free(space->ids);
360 isl_ctx_deref(space->ctx);
362 free(space);
364 return NULL;
367 /* Check if "s" is a valid dimension or tuple name.
368 * We currently only forbid names that look like a number.
370 * s is assumed to be non-NULL.
372 static int name_ok(isl_ctx *ctx, const char *s)
374 char *p;
375 long dummy;
377 dummy = strtol(s, &p, 0);
378 if (p != s)
379 isl_die(ctx, isl_error_invalid, "name looks like a number",
380 return 0);
382 return 1;
385 /* Is it possible for the given dimension type to have a tuple id?
387 static int space_can_have_id(__isl_keep isl_space *space,
388 enum isl_dim_type type)
390 if (!space)
391 return 0;
392 if (isl_space_is_params(space))
393 isl_die(space->ctx, isl_error_invalid,
394 "parameter spaces don't have tuple ids", return 0);
395 if (isl_space_is_set(space) && type != isl_dim_set)
396 isl_die(space->ctx, isl_error_invalid,
397 "set spaces can only have a set id", return 0);
398 if (type != isl_dim_in && type != isl_dim_out)
399 isl_die(space->ctx, isl_error_invalid,
400 "only input, output and set tuples can have ids",
401 return 0);
403 return 1;
406 /* Does the tuple have an id?
408 isl_bool isl_space_has_tuple_id(__isl_keep isl_space *dim,
409 enum isl_dim_type type)
411 if (!space_can_have_id(dim, type))
412 return isl_bool_error;
413 return dim->tuple_id[type - isl_dim_in] != NULL;
416 __isl_give isl_id *isl_space_get_tuple_id(__isl_keep isl_space *dim,
417 enum isl_dim_type type)
419 int has_id;
421 if (!dim)
422 return NULL;
423 has_id = isl_space_has_tuple_id(dim, type);
424 if (has_id < 0)
425 return NULL;
426 if (!has_id)
427 isl_die(dim->ctx, isl_error_invalid,
428 "tuple has no id", return NULL);
429 return isl_id_copy(dim->tuple_id[type - isl_dim_in]);
432 __isl_give isl_space *isl_space_set_tuple_id(__isl_take isl_space *dim,
433 enum isl_dim_type type, __isl_take isl_id *id)
435 dim = isl_space_cow(dim);
436 if (!dim || !id)
437 goto error;
438 if (type != isl_dim_in && type != isl_dim_out)
439 isl_die(dim->ctx, isl_error_invalid,
440 "only input, output and set tuples can have names",
441 goto error);
443 isl_id_free(dim->tuple_id[type - isl_dim_in]);
444 dim->tuple_id[type - isl_dim_in] = id;
446 return dim;
447 error:
448 isl_id_free(id);
449 isl_space_free(dim);
450 return NULL;
453 __isl_give isl_space *isl_space_reset_tuple_id(__isl_take isl_space *dim,
454 enum isl_dim_type type)
456 dim = isl_space_cow(dim);
457 if (!dim)
458 return NULL;
459 if (type != isl_dim_in && type != isl_dim_out)
460 isl_die(dim->ctx, isl_error_invalid,
461 "only input, output and set tuples can have names",
462 goto error);
464 isl_id_free(dim->tuple_id[type - isl_dim_in]);
465 dim->tuple_id[type - isl_dim_in] = NULL;
467 return dim;
468 error:
469 isl_space_free(dim);
470 return NULL;
473 /* Set the id of the given dimension of "space" to "id".
474 * If the dimension already has an id, then it is replaced.
475 * If the dimension is a parameter, then we need to change it
476 * in the nested spaces (if any) as well.
478 __isl_give isl_space *isl_space_set_dim_id(__isl_take isl_space *space,
479 enum isl_dim_type type, unsigned pos, __isl_take isl_id *id)
481 space = isl_space_cow(space);
482 if (!space || !id)
483 goto error;
485 if (type == isl_dim_param) {
486 int i;
488 for (i = 0; i < 2; ++i) {
489 if (!space->nested[i])
490 continue;
491 space->nested[i] =
492 isl_space_set_dim_id(space->nested[i],
493 type, pos, isl_id_copy(id));
494 if (!space->nested[i])
495 goto error;
499 isl_id_free(get_id(space, type, pos));
500 return set_id(space, type, pos, id);
501 error:
502 isl_id_free(id);
503 isl_space_free(space);
504 return NULL;
507 /* Reset the id of the given dimension of "space".
508 * If the dimension already has an id, then it is removed.
509 * If the dimension is a parameter, then we need to reset it
510 * in the nested spaces (if any) as well.
512 __isl_give isl_space *isl_space_reset_dim_id(__isl_take isl_space *space,
513 enum isl_dim_type type, unsigned pos)
515 space = isl_space_cow(space);
516 if (!space)
517 goto error;
519 if (type == isl_dim_param) {
520 int i;
522 for (i = 0; i < 2; ++i) {
523 if (!space->nested[i])
524 continue;
525 space->nested[i] =
526 isl_space_reset_dim_id(space->nested[i],
527 type, pos);
528 if (!space->nested[i])
529 goto error;
533 isl_id_free(get_id(space, type, pos));
534 return set_id(space, type, pos, NULL);
535 error:
536 isl_space_free(space);
537 return NULL;
540 isl_bool isl_space_has_dim_id(__isl_keep isl_space *dim,
541 enum isl_dim_type type, unsigned pos)
543 if (!dim)
544 return isl_bool_error;
545 return get_id(dim, type, pos) != NULL;
548 __isl_give isl_id *isl_space_get_dim_id(__isl_keep isl_space *dim,
549 enum isl_dim_type type, unsigned pos)
551 if (!dim)
552 return NULL;
553 if (!get_id(dim, type, pos))
554 isl_die(dim->ctx, isl_error_invalid,
555 "dim has no id", return NULL);
556 return isl_id_copy(get_id(dim, type, pos));
559 __isl_give isl_space *isl_space_set_tuple_name(__isl_take isl_space *dim,
560 enum isl_dim_type type, const char *s)
562 isl_id *id;
564 if (!dim)
565 return NULL;
567 if (!s)
568 return isl_space_reset_tuple_id(dim, type);
570 if (!name_ok(dim->ctx, s))
571 goto error;
573 id = isl_id_alloc(dim->ctx, s, NULL);
574 return isl_space_set_tuple_id(dim, type, id);
575 error:
576 isl_space_free(dim);
577 return NULL;
580 /* Does the tuple have a name?
582 isl_bool isl_space_has_tuple_name(__isl_keep isl_space *space,
583 enum isl_dim_type type)
585 isl_id *id;
587 if (!space_can_have_id(space, type))
588 return isl_bool_error;
589 id = space->tuple_id[type - isl_dim_in];
590 return id && id->name;
593 const char *isl_space_get_tuple_name(__isl_keep isl_space *dim,
594 enum isl_dim_type type)
596 isl_id *id;
597 if (!dim)
598 return NULL;
599 if (type != isl_dim_in && type != isl_dim_out)
600 return NULL;
601 id = dim->tuple_id[type - isl_dim_in];
602 return id ? id->name : NULL;
605 __isl_give isl_space *isl_space_set_dim_name(__isl_take isl_space *dim,
606 enum isl_dim_type type, unsigned pos,
607 const char *s)
609 isl_id *id;
611 if (!dim)
612 return NULL;
613 if (!s)
614 return isl_space_reset_dim_id(dim, type, pos);
615 if (!name_ok(dim->ctx, s))
616 goto error;
617 id = isl_id_alloc(dim->ctx, s, NULL);
618 return isl_space_set_dim_id(dim, type, pos, id);
619 error:
620 isl_space_free(dim);
621 return NULL;
624 /* Does the given dimension have a name?
626 isl_bool isl_space_has_dim_name(__isl_keep isl_space *space,
627 enum isl_dim_type type, unsigned pos)
629 isl_id *id;
631 if (!space)
632 return isl_bool_error;
633 id = get_id(space, type, pos);
634 return id && id->name;
637 __isl_keep const char *isl_space_get_dim_name(__isl_keep isl_space *dim,
638 enum isl_dim_type type, unsigned pos)
640 isl_id *id = get_id(dim, type, pos);
641 return id ? id->name : NULL;
644 int isl_space_find_dim_by_id(__isl_keep isl_space *dim, enum isl_dim_type type,
645 __isl_keep isl_id *id)
647 int i;
648 int offset;
649 int n;
651 if (!dim || !id)
652 return -1;
654 offset = isl_space_offset(dim, type);
655 n = isl_space_dim(dim, type);
656 for (i = 0; i < n && offset + i < dim->n_id; ++i)
657 if (dim->ids[offset + i] == id)
658 return i;
660 return -1;
663 int isl_space_find_dim_by_name(__isl_keep isl_space *space,
664 enum isl_dim_type type, const char *name)
666 int i;
667 int offset;
668 int n;
670 if (!space || !name)
671 return -1;
673 offset = isl_space_offset(space, type);
674 n = isl_space_dim(space, type);
675 for (i = 0; i < n && offset + i < space->n_id; ++i) {
676 isl_id *id = get_id(space, type, i);
677 if (id && id->name && !strcmp(id->name, name))
678 return i;
681 return -1;
684 /* Reset the user pointer on all identifiers of parameters and tuples
685 * of "space".
687 __isl_give isl_space *isl_space_reset_user(__isl_take isl_space *space)
689 int i;
690 isl_ctx *ctx;
691 isl_id *id;
692 const char *name;
694 if (!space)
695 return NULL;
697 ctx = isl_space_get_ctx(space);
699 for (i = 0; i < space->nparam && i < space->n_id; ++i) {
700 if (!isl_id_get_user(space->ids[i]))
701 continue;
702 space = isl_space_cow(space);
703 if (!space)
704 return NULL;
705 name = isl_id_get_name(space->ids[i]);
706 id = isl_id_alloc(ctx, name, NULL);
707 isl_id_free(space->ids[i]);
708 space->ids[i] = id;
709 if (!id)
710 return isl_space_free(space);
713 for (i = 0; i < 2; ++i) {
714 if (!space->tuple_id[i])
715 continue;
716 if (!isl_id_get_user(space->tuple_id[i]))
717 continue;
718 space = isl_space_cow(space);
719 if (!space)
720 return NULL;
721 name = isl_id_get_name(space->tuple_id[i]);
722 id = isl_id_alloc(ctx, name, NULL);
723 isl_id_free(space->tuple_id[i]);
724 space->tuple_id[i] = id;
725 if (!id)
726 return isl_space_free(space);
729 for (i = 0; i < 2; ++i) {
730 if (!space->nested[i])
731 continue;
732 space = isl_space_cow(space);
733 if (!space)
734 return NULL;
735 space->nested[i] = isl_space_reset_user(space->nested[i]);
736 if (!space->nested[i])
737 return isl_space_free(space);
740 return space;
743 static __isl_keep isl_id *tuple_id(__isl_keep isl_space *dim,
744 enum isl_dim_type type)
746 if (!dim)
747 return NULL;
748 if (type == isl_dim_in)
749 return dim->tuple_id[0];
750 if (type == isl_dim_out)
751 return dim->tuple_id[1];
752 return NULL;
755 static __isl_keep isl_space *nested(__isl_keep isl_space *dim,
756 enum isl_dim_type type)
758 if (!dim)
759 return NULL;
760 if (type == isl_dim_in)
761 return dim->nested[0];
762 if (type == isl_dim_out)
763 return dim->nested[1];
764 return NULL;
767 /* Are the two spaces the same, apart from positions and names of parameters?
769 isl_bool isl_space_has_equal_tuples(__isl_keep isl_space *space1,
770 __isl_keep isl_space *space2)
772 if (!space1 || !space2)
773 return isl_bool_error;
774 if (space1 == space2)
775 return isl_bool_true;
776 return isl_space_tuple_is_equal(space1, isl_dim_in,
777 space2, isl_dim_in) &&
778 isl_space_tuple_is_equal(space1, isl_dim_out,
779 space2, isl_dim_out);
782 /* Check if the tuple of type "type1" of "space1" is the same as
783 * the tuple of type "type2" of "space2".
785 * That is, check if the tuples have the same identifier, the same dimension
786 * and the same internal structure.
787 * The identifiers of the dimensions inside the tuples do not affect the result.
789 * Note that this function only checks the tuples themselves.
790 * If nested tuples are involved, then we need to be careful not
791 * to have result affected by possibly differing parameters
792 * in those nested tuples.
794 isl_bool isl_space_tuple_is_equal(__isl_keep isl_space *space1,
795 enum isl_dim_type type1, __isl_keep isl_space *space2,
796 enum isl_dim_type type2)
798 isl_id *id1, *id2;
799 isl_space *nested1, *nested2;
801 if (!space1 || !space2)
802 return isl_bool_error;
804 if (space1 == space2 && type1 == type2)
805 return isl_bool_true;
807 if (n(space1, type1) != n(space2, type2))
808 return isl_bool_false;
809 id1 = tuple_id(space1, type1);
810 id2 = tuple_id(space2, type2);
811 if (!id1 ^ !id2)
812 return isl_bool_false;
813 if (id1 && id1 != id2)
814 return isl_bool_false;
815 nested1 = nested(space1, type1);
816 nested2 = nested(space2, type2);
817 if (!nested1 ^ !nested2)
818 return isl_bool_false;
819 if (nested1 && !isl_space_has_equal_tuples(nested1, nested2))
820 return isl_bool_false;
821 return isl_bool_true;
824 /* This is the old, undocumented, name for isl_space_tuple_is_equal.
825 * It will be removed at some point.
827 int isl_space_tuple_match(__isl_keep isl_space *space1, enum isl_dim_type type1,
828 __isl_keep isl_space *space2, enum isl_dim_type type2)
830 return isl_space_tuple_is_equal(space1, type1, space2, type2);
833 static isl_bool match(__isl_keep isl_space *space1, enum isl_dim_type type1,
834 __isl_keep isl_space *space2, enum isl_dim_type type2)
836 int i;
838 if (space1 == space2 && type1 == type2)
839 return isl_bool_true;
841 if (!isl_space_tuple_is_equal(space1, type1, space2, type2))
842 return isl_bool_false;
844 if (!space1->ids && !space2->ids)
845 return isl_bool_true;
847 for (i = 0; i < n(space1, type1); ++i) {
848 if (get_id(space1, type1, i) != get_id(space2, type2, i))
849 return isl_bool_false;
851 return isl_bool_true;
854 isl_bool isl_space_match(__isl_keep isl_space *space1, enum isl_dim_type type1,
855 __isl_keep isl_space *space2, enum isl_dim_type type2)
857 if (!space1 || !space2)
858 return isl_bool_error;
860 return match(space1, type1, space2, type2);
863 static void get_ids(__isl_keep isl_space *dim, enum isl_dim_type type,
864 unsigned first, unsigned n, __isl_keep isl_id **ids)
866 int i;
868 for (i = 0; i < n ; ++i)
869 ids[i] = get_id(dim, type, first + i);
872 static __isl_give isl_space *space_extend(__isl_take isl_space *space,
873 unsigned nparam, unsigned n_in, unsigned n_out)
875 isl_id **ids = NULL;
877 if (!space)
878 return NULL;
879 if (space->nparam == nparam &&
880 space->n_in == n_in && space->n_out == n_out)
881 return space;
883 isl_assert(space->ctx, space->nparam <= nparam, goto error);
884 isl_assert(space->ctx, space->n_in <= n_in, goto error);
885 isl_assert(space->ctx, space->n_out <= n_out, goto error);
887 space = isl_space_cow(space);
888 if (!space)
889 goto error;
891 if (space->ids) {
892 unsigned n;
893 n = nparam + n_in + n_out;
894 if (n < nparam || n < n_in || n < n_out)
895 isl_die(isl_space_get_ctx(space), isl_error_invalid,
896 "overflow in total number of dimensions",
897 goto error);
898 ids = isl_calloc_array(space->ctx, isl_id *, n);
899 if (!ids)
900 goto error;
901 get_ids(space, isl_dim_param, 0, space->nparam, ids);
902 get_ids(space, isl_dim_in, 0, space->n_in, ids + nparam);
903 get_ids(space, isl_dim_out, 0, space->n_out,
904 ids + nparam + n_in);
905 free(space->ids);
906 space->ids = ids;
907 space->n_id = nparam + n_in + n_out;
909 space->nparam = nparam;
910 space->n_in = n_in;
911 space->n_out = n_out;
913 return space;
914 error:
915 free(ids);
916 isl_space_free(space);
917 return NULL;
920 __isl_give isl_space *isl_space_extend(__isl_take isl_space *space,
921 unsigned nparam, unsigned n_in, unsigned n_out)
923 return space_extend(space, nparam, n_in, n_out);
926 __isl_give isl_space *isl_space_add_dims(__isl_take isl_space *space,
927 enum isl_dim_type type, unsigned n)
929 space = isl_space_reset(space, type);
930 if (!space)
931 return NULL;
932 switch (type) {
933 case isl_dim_param:
934 space = space_extend(space,
935 space->nparam + n, space->n_in, space->n_out);
936 if (space && space->nested[0] &&
937 !(space->nested[0] = isl_space_add_dims(space->nested[0],
938 isl_dim_param, n)))
939 goto error;
940 if (space && space->nested[1] &&
941 !(space->nested[1] = isl_space_add_dims(space->nested[1],
942 isl_dim_param, n)))
943 goto error;
944 return space;
945 case isl_dim_in:
946 return space_extend(space,
947 space->nparam, space->n_in + n, space->n_out);
948 case isl_dim_out:
949 return space_extend(space,
950 space->nparam, space->n_in, space->n_out + n);
951 default:
952 isl_die(space->ctx, isl_error_invalid,
953 "cannot add dimensions of specified type", goto error);
955 error:
956 isl_space_free(space);
957 return NULL;
960 static int valid_dim_type(enum isl_dim_type type)
962 switch (type) {
963 case isl_dim_param:
964 case isl_dim_in:
965 case isl_dim_out:
966 return 1;
967 default:
968 return 0;
972 /* Insert "n" dimensions of type "type" at position "pos".
973 * If we are inserting parameters, then they are also inserted in
974 * any nested spaces.
976 __isl_give isl_space *isl_space_insert_dims(__isl_take isl_space *dim,
977 enum isl_dim_type type, unsigned pos, unsigned n)
979 isl_id **ids = NULL;
981 if (!dim)
982 return NULL;
983 if (n == 0)
984 return isl_space_reset(dim, type);
986 if (!valid_dim_type(type))
987 isl_die(dim->ctx, isl_error_invalid,
988 "cannot insert dimensions of specified type",
989 goto error);
991 isl_assert(dim->ctx, pos <= isl_space_dim(dim, type), goto error);
993 dim = isl_space_cow(dim);
994 if (!dim)
995 return NULL;
997 if (dim->ids) {
998 enum isl_dim_type t, o = isl_dim_param;
999 int off;
1000 int s[3];
1001 ids = isl_calloc_array(dim->ctx, isl_id *,
1002 dim->nparam + dim->n_in + dim->n_out + n);
1003 if (!ids)
1004 goto error;
1005 off = 0;
1006 s[isl_dim_param - o] = dim->nparam;
1007 s[isl_dim_in - o] = dim->n_in;
1008 s[isl_dim_out - o] = dim->n_out;
1009 for (t = isl_dim_param; t <= isl_dim_out; ++t) {
1010 if (t != type) {
1011 get_ids(dim, t, 0, s[t - o], ids + off);
1012 off += s[t - o];
1013 } else {
1014 get_ids(dim, t, 0, pos, ids + off);
1015 off += pos + n;
1016 get_ids(dim, t, pos, s[t - o] - pos, ids + off);
1017 off += s[t - o] - pos;
1020 free(dim->ids);
1021 dim->ids = ids;
1022 dim->n_id = dim->nparam + dim->n_in + dim->n_out + n;
1024 switch (type) {
1025 case isl_dim_param: dim->nparam += n; break;
1026 case isl_dim_in: dim->n_in += n; break;
1027 case isl_dim_out: dim->n_out += n; break;
1028 default: ;
1030 dim = isl_space_reset(dim, type);
1032 if (type == isl_dim_param) {
1033 if (dim && dim->nested[0] &&
1034 !(dim->nested[0] = isl_space_insert_dims(dim->nested[0],
1035 isl_dim_param, pos, n)))
1036 goto error;
1037 if (dim && dim->nested[1] &&
1038 !(dim->nested[1] = isl_space_insert_dims(dim->nested[1],
1039 isl_dim_param, pos, n)))
1040 goto error;
1043 return dim;
1044 error:
1045 isl_space_free(dim);
1046 return NULL;
1049 __isl_give isl_space *isl_space_move_dims(__isl_take isl_space *dim,
1050 enum isl_dim_type dst_type, unsigned dst_pos,
1051 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
1053 int i;
1055 if (!dim)
1056 return NULL;
1057 if (n == 0) {
1058 dim = isl_space_reset(dim, src_type);
1059 return isl_space_reset(dim, dst_type);
1062 isl_assert(dim->ctx, src_pos + n <= isl_space_dim(dim, src_type),
1063 goto error);
1065 if (dst_type == src_type && dst_pos == src_pos)
1066 return dim;
1068 isl_assert(dim->ctx, dst_type != src_type, goto error);
1070 dim = isl_space_reset(dim, src_type);
1071 dim = isl_space_reset(dim, dst_type);
1073 dim = isl_space_cow(dim);
1074 if (!dim)
1075 return NULL;
1077 if (dim->ids) {
1078 isl_id **ids;
1079 enum isl_dim_type t, o = isl_dim_param;
1080 int off;
1081 int s[3];
1082 ids = isl_calloc_array(dim->ctx, isl_id *,
1083 dim->nparam + dim->n_in + dim->n_out);
1084 if (!ids)
1085 goto error;
1086 off = 0;
1087 s[isl_dim_param - o] = dim->nparam;
1088 s[isl_dim_in - o] = dim->n_in;
1089 s[isl_dim_out - o] = dim->n_out;
1090 for (t = isl_dim_param; t <= isl_dim_out; ++t) {
1091 if (t == dst_type) {
1092 get_ids(dim, t, 0, dst_pos, ids + off);
1093 off += dst_pos;
1094 get_ids(dim, src_type, src_pos, n, ids + off);
1095 off += n;
1096 get_ids(dim, t, dst_pos, s[t - o] - dst_pos,
1097 ids + off);
1098 off += s[t - o] - dst_pos;
1099 } else if (t == src_type) {
1100 get_ids(dim, t, 0, src_pos, ids + off);
1101 off += src_pos;
1102 get_ids(dim, t, src_pos + n,
1103 s[t - o] - src_pos - n, ids + off);
1104 off += s[t - o] - src_pos - n;
1105 } else {
1106 get_ids(dim, t, 0, s[t - o], ids + off);
1107 off += s[t - o];
1110 free(dim->ids);
1111 dim->ids = ids;
1112 dim->n_id = dim->nparam + dim->n_in + dim->n_out;
1115 switch (dst_type) {
1116 case isl_dim_param: dim->nparam += n; break;
1117 case isl_dim_in: dim->n_in += n; break;
1118 case isl_dim_out: dim->n_out += n; break;
1119 default: ;
1122 switch (src_type) {
1123 case isl_dim_param: dim->nparam -= n; break;
1124 case isl_dim_in: dim->n_in -= n; break;
1125 case isl_dim_out: dim->n_out -= n; break;
1126 default: ;
1129 if (dst_type != isl_dim_param && src_type != isl_dim_param)
1130 return dim;
1132 for (i = 0; i < 2; ++i) {
1133 if (!dim->nested[i])
1134 continue;
1135 dim->nested[i] = isl_space_replace(dim->nested[i],
1136 isl_dim_param, dim);
1137 if (!dim->nested[i])
1138 goto error;
1141 return dim;
1142 error:
1143 isl_space_free(dim);
1144 return NULL;
1147 /* Check that "space1" and "space2" have the same parameters,
1148 * reporting an error if they do not.
1150 isl_stat isl_space_check_equal_params(__isl_keep isl_space *space1,
1151 __isl_keep isl_space *space2)
1153 isl_bool equal;
1155 equal = match(space1, isl_dim_param, space2, isl_dim_param);
1156 if (equal < 0)
1157 return isl_stat_error;
1158 if (!equal)
1159 isl_die(isl_space_get_ctx(space1), isl_error_invalid,
1160 "parameters need to match", return isl_stat_error);
1161 return isl_stat_ok;
1164 __isl_give isl_space *isl_space_join(__isl_take isl_space *left,
1165 __isl_take isl_space *right)
1167 isl_space *dim;
1169 if (isl_space_check_equal_params(left, right) < 0)
1170 goto error;
1172 isl_assert(left->ctx,
1173 isl_space_tuple_is_equal(left, isl_dim_out, right, isl_dim_in),
1174 goto error);
1176 dim = isl_space_alloc(left->ctx, left->nparam, left->n_in, right->n_out);
1177 if (!dim)
1178 goto error;
1180 dim = copy_ids(dim, isl_dim_param, 0, left, isl_dim_param);
1181 dim = copy_ids(dim, isl_dim_in, 0, left, isl_dim_in);
1182 dim = copy_ids(dim, isl_dim_out, 0, right, isl_dim_out);
1184 if (dim && left->tuple_id[0] &&
1185 !(dim->tuple_id[0] = isl_id_copy(left->tuple_id[0])))
1186 goto error;
1187 if (dim && right->tuple_id[1] &&
1188 !(dim->tuple_id[1] = isl_id_copy(right->tuple_id[1])))
1189 goto error;
1190 if (dim && left->nested[0] &&
1191 !(dim->nested[0] = isl_space_copy(left->nested[0])))
1192 goto error;
1193 if (dim && right->nested[1] &&
1194 !(dim->nested[1] = isl_space_copy(right->nested[1])))
1195 goto error;
1197 isl_space_free(left);
1198 isl_space_free(right);
1200 return dim;
1201 error:
1202 isl_space_free(left);
1203 isl_space_free(right);
1204 return NULL;
1207 /* Given two map spaces { A -> C } and { B -> D }, construct the space
1208 * { [A -> B] -> [C -> D] }.
1209 * Given two set spaces { A } and { B }, construct the space { [A -> B] }.
1211 __isl_give isl_space *isl_space_product(__isl_take isl_space *left,
1212 __isl_take isl_space *right)
1214 isl_space *dom1, *dom2, *nest1, *nest2;
1215 int is_set;
1217 if (!left || !right)
1218 goto error;
1220 is_set = isl_space_is_set(left);
1221 if (is_set != isl_space_is_set(right))
1222 isl_die(isl_space_get_ctx(left), isl_error_invalid,
1223 "expecting either two set spaces or two map spaces",
1224 goto error);
1225 if (is_set)
1226 return isl_space_range_product(left, right);
1228 if (isl_space_check_equal_params(left, right) < 0)
1229 goto error;
1231 dom1 = isl_space_domain(isl_space_copy(left));
1232 dom2 = isl_space_domain(isl_space_copy(right));
1233 nest1 = isl_space_wrap(isl_space_join(isl_space_reverse(dom1), dom2));
1235 dom1 = isl_space_range(left);
1236 dom2 = isl_space_range(right);
1237 nest2 = isl_space_wrap(isl_space_join(isl_space_reverse(dom1), dom2));
1239 return isl_space_join(isl_space_reverse(nest1), nest2);
1240 error:
1241 isl_space_free(left);
1242 isl_space_free(right);
1243 return NULL;
1246 /* Given two spaces { A -> C } and { B -> C }, construct the space
1247 * { [A -> B] -> C }
1249 __isl_give isl_space *isl_space_domain_product(__isl_take isl_space *left,
1250 __isl_take isl_space *right)
1252 isl_space *ran, *dom1, *dom2, *nest;
1254 if (isl_space_check_equal_params(left, right) < 0)
1255 goto error;
1257 if (!isl_space_tuple_is_equal(left, isl_dim_out, right, isl_dim_out))
1258 isl_die(left->ctx, isl_error_invalid,
1259 "ranges need to match", goto error);
1261 ran = isl_space_range(isl_space_copy(left));
1263 dom1 = isl_space_domain(left);
1264 dom2 = isl_space_domain(right);
1265 nest = isl_space_wrap(isl_space_join(isl_space_reverse(dom1), dom2));
1267 return isl_space_join(isl_space_reverse(nest), ran);
1268 error:
1269 isl_space_free(left);
1270 isl_space_free(right);
1271 return NULL;
1274 __isl_give isl_space *isl_space_range_product(__isl_take isl_space *left,
1275 __isl_take isl_space *right)
1277 isl_space *dom, *ran1, *ran2, *nest;
1279 if (isl_space_check_equal_params(left, right) < 0)
1280 goto error;
1282 if (!isl_space_tuple_is_equal(left, isl_dim_in, right, isl_dim_in))
1283 isl_die(left->ctx, isl_error_invalid,
1284 "domains need to match", goto error);
1286 dom = isl_space_domain(isl_space_copy(left));
1288 ran1 = isl_space_range(left);
1289 ran2 = isl_space_range(right);
1290 nest = isl_space_wrap(isl_space_join(isl_space_reverse(ran1), ran2));
1292 return isl_space_join(isl_space_reverse(dom), nest);
1293 error:
1294 isl_space_free(left);
1295 isl_space_free(right);
1296 return NULL;
1299 /* Given a space of the form [A -> B] -> [C -> D], return the space A -> C.
1301 __isl_give isl_space *isl_space_factor_domain(__isl_take isl_space *space)
1303 space = isl_space_domain_factor_domain(space);
1304 space = isl_space_range_factor_domain(space);
1305 return space;
1308 /* Given a space of the form [A -> B] -> C, return the space A -> C.
1310 __isl_give isl_space *isl_space_domain_factor_domain(
1311 __isl_take isl_space *space)
1313 isl_space *nested;
1314 isl_space *domain;
1316 if (!space)
1317 return NULL;
1318 if (!isl_space_domain_is_wrapping(space))
1319 isl_die(isl_space_get_ctx(space), isl_error_invalid,
1320 "domain not a product", return isl_space_free(space));
1322 nested = space->nested[0];
1323 domain = isl_space_copy(space);
1324 domain = isl_space_drop_dims(domain, isl_dim_in,
1325 nested->n_in, nested->n_out);
1326 if (!domain)
1327 return isl_space_free(space);
1328 if (nested->tuple_id[0]) {
1329 domain->tuple_id[0] = isl_id_copy(nested->tuple_id[0]);
1330 if (!domain->tuple_id[0])
1331 goto error;
1333 if (nested->nested[0]) {
1334 domain->nested[0] = isl_space_copy(nested->nested[0]);
1335 if (!domain->nested[0])
1336 goto error;
1339 isl_space_free(space);
1340 return domain;
1341 error:
1342 isl_space_free(space);
1343 isl_space_free(domain);
1344 return NULL;
1347 /* Given a space of the form [A -> B] -> C, return the space B -> C.
1349 __isl_give isl_space *isl_space_domain_factor_range(
1350 __isl_take isl_space *space)
1352 isl_space *nested;
1353 isl_space *range;
1355 if (!space)
1356 return NULL;
1357 if (!isl_space_domain_is_wrapping(space))
1358 isl_die(isl_space_get_ctx(space), isl_error_invalid,
1359 "domain not a product", return isl_space_free(space));
1361 nested = space->nested[0];
1362 range = isl_space_copy(space);
1363 range = isl_space_drop_dims(range, isl_dim_in, 0, nested->n_in);
1364 if (!range)
1365 return isl_space_free(space);
1366 if (nested->tuple_id[1]) {
1367 range->tuple_id[0] = isl_id_copy(nested->tuple_id[1]);
1368 if (!range->tuple_id[0])
1369 goto error;
1371 if (nested->nested[1]) {
1372 range->nested[0] = isl_space_copy(nested->nested[1]);
1373 if (!range->nested[0])
1374 goto error;
1377 isl_space_free(space);
1378 return range;
1379 error:
1380 isl_space_free(space);
1381 isl_space_free(range);
1382 return NULL;
1385 /* Given a space of the form A -> [B -> C], return the space A -> B.
1387 __isl_give isl_space *isl_space_range_factor_domain(
1388 __isl_take isl_space *space)
1390 isl_space *nested;
1391 isl_space *domain;
1393 if (!space)
1394 return NULL;
1395 if (!isl_space_range_is_wrapping(space))
1396 isl_die(isl_space_get_ctx(space), isl_error_invalid,
1397 "range not a product", return isl_space_free(space));
1399 nested = space->nested[1];
1400 domain = isl_space_copy(space);
1401 domain = isl_space_drop_dims(domain, isl_dim_out,
1402 nested->n_in, nested->n_out);
1403 if (!domain)
1404 return isl_space_free(space);
1405 if (nested->tuple_id[0]) {
1406 domain->tuple_id[1] = isl_id_copy(nested->tuple_id[0]);
1407 if (!domain->tuple_id[1])
1408 goto error;
1410 if (nested->nested[0]) {
1411 domain->nested[1] = isl_space_copy(nested->nested[0]);
1412 if (!domain->nested[1])
1413 goto error;
1416 isl_space_free(space);
1417 return domain;
1418 error:
1419 isl_space_free(space);
1420 isl_space_free(domain);
1421 return NULL;
1424 /* Internal function that selects the range of the map that is
1425 * embedded in either a set space or the range of a map space.
1426 * In particular, given a space of the form [A -> B], return the space B.
1427 * Given a space of the form A -> [B -> C], return the space A -> C.
1429 static __isl_give isl_space *range_factor_range(__isl_take isl_space *space)
1431 isl_space *nested;
1432 isl_space *range;
1434 if (!space)
1435 return NULL;
1437 nested = space->nested[1];
1438 range = isl_space_copy(space);
1439 range = isl_space_drop_dims(range, isl_dim_out, 0, nested->n_in);
1440 if (!range)
1441 return isl_space_free(space);
1442 if (nested->tuple_id[1]) {
1443 range->tuple_id[1] = isl_id_copy(nested->tuple_id[1]);
1444 if (!range->tuple_id[1])
1445 goto error;
1447 if (nested->nested[1]) {
1448 range->nested[1] = isl_space_copy(nested->nested[1]);
1449 if (!range->nested[1])
1450 goto error;
1453 isl_space_free(space);
1454 return range;
1455 error:
1456 isl_space_free(space);
1457 isl_space_free(range);
1458 return NULL;
1461 /* Given a space of the form A -> [B -> C], return the space A -> C.
1463 __isl_give isl_space *isl_space_range_factor_range(
1464 __isl_take isl_space *space)
1466 if (!space)
1467 return NULL;
1468 if (!isl_space_range_is_wrapping(space))
1469 isl_die(isl_space_get_ctx(space), isl_error_invalid,
1470 "range not a product", return isl_space_free(space));
1472 return range_factor_range(space);
1475 /* Given a space of the form [A -> B], return the space B.
1477 static __isl_give isl_space *set_factor_range(__isl_take isl_space *space)
1479 if (!space)
1480 return NULL;
1481 if (!isl_space_is_wrapping(space))
1482 isl_die(isl_space_get_ctx(space), isl_error_invalid,
1483 "not a product", return isl_space_free(space));
1485 return range_factor_range(space);
1488 /* Given a space of the form [A -> B] -> [C -> D], return the space B -> D.
1489 * Given a space of the form [A -> B], return the space B.
1491 __isl_give isl_space *isl_space_factor_range(__isl_take isl_space *space)
1493 if (!space)
1494 return NULL;
1495 if (isl_space_is_set(space))
1496 return set_factor_range(space);
1497 space = isl_space_domain_factor_range(space);
1498 space = isl_space_range_factor_range(space);
1499 return space;
1502 __isl_give isl_space *isl_space_map_from_set(__isl_take isl_space *dim)
1504 isl_ctx *ctx;
1505 isl_id **ids = NULL;
1507 if (!dim)
1508 return NULL;
1509 ctx = isl_space_get_ctx(dim);
1510 if (!isl_space_is_set(dim))
1511 isl_die(ctx, isl_error_invalid, "not a set space", goto error);
1512 dim = isl_space_cow(dim);
1513 if (!dim)
1514 return NULL;
1515 if (dim->ids) {
1516 ids = isl_calloc_array(dim->ctx, isl_id *,
1517 dim->nparam + dim->n_out + dim->n_out);
1518 if (!ids)
1519 goto error;
1520 get_ids(dim, isl_dim_param, 0, dim->nparam, ids);
1521 get_ids(dim, isl_dim_out, 0, dim->n_out, ids + dim->nparam);
1523 dim->n_in = dim->n_out;
1524 if (ids) {
1525 free(dim->ids);
1526 dim->ids = ids;
1527 dim->n_id = dim->nparam + dim->n_out + dim->n_out;
1528 dim = copy_ids(dim, isl_dim_out, 0, dim, isl_dim_in);
1530 isl_id_free(dim->tuple_id[0]);
1531 dim->tuple_id[0] = isl_id_copy(dim->tuple_id[1]);
1532 isl_space_free(dim->nested[0]);
1533 dim->nested[0] = isl_space_copy(dim->nested[1]);
1534 return dim;
1535 error:
1536 isl_space_free(dim);
1537 return NULL;
1540 __isl_give isl_space *isl_space_map_from_domain_and_range(
1541 __isl_take isl_space *domain, __isl_take isl_space *range)
1543 if (!domain || !range)
1544 goto error;
1545 if (!isl_space_is_set(domain))
1546 isl_die(isl_space_get_ctx(domain), isl_error_invalid,
1547 "domain is not a set space", goto error);
1548 if (!isl_space_is_set(range))
1549 isl_die(isl_space_get_ctx(range), isl_error_invalid,
1550 "range is not a set space", goto error);
1551 return isl_space_join(isl_space_reverse(domain), range);
1552 error:
1553 isl_space_free(domain);
1554 isl_space_free(range);
1555 return NULL;
1558 static __isl_give isl_space *set_ids(__isl_take isl_space *dim,
1559 enum isl_dim_type type,
1560 unsigned first, unsigned n, __isl_take isl_id **ids)
1562 int i;
1564 for (i = 0; i < n ; ++i)
1565 dim = set_id(dim, type, first + i, ids[i]);
1567 return dim;
1570 __isl_give isl_space *isl_space_reverse(__isl_take isl_space *dim)
1572 unsigned t;
1573 isl_space *nested;
1574 isl_id **ids = NULL;
1575 isl_id *id;
1577 if (!dim)
1578 return NULL;
1579 if (match(dim, isl_dim_in, dim, isl_dim_out))
1580 return dim;
1582 dim = isl_space_cow(dim);
1583 if (!dim)
1584 return NULL;
1586 id = dim->tuple_id[0];
1587 dim->tuple_id[0] = dim->tuple_id[1];
1588 dim->tuple_id[1] = id;
1590 nested = dim->nested[0];
1591 dim->nested[0] = dim->nested[1];
1592 dim->nested[1] = nested;
1594 if (dim->ids) {
1595 int n_id = dim->n_in + dim->n_out;
1596 ids = isl_alloc_array(dim->ctx, isl_id *, n_id);
1597 if (n_id && !ids)
1598 goto error;
1599 get_ids(dim, isl_dim_in, 0, dim->n_in, ids);
1600 get_ids(dim, isl_dim_out, 0, dim->n_out, ids + dim->n_in);
1603 t = dim->n_in;
1604 dim->n_in = dim->n_out;
1605 dim->n_out = t;
1607 if (dim->ids) {
1608 dim = set_ids(dim, isl_dim_out, 0, dim->n_out, ids);
1609 dim = set_ids(dim, isl_dim_in, 0, dim->n_in, ids + dim->n_out);
1610 free(ids);
1613 return dim;
1614 error:
1615 free(ids);
1616 isl_space_free(dim);
1617 return NULL;
1620 __isl_give isl_space *isl_space_drop_dims(__isl_take isl_space *dim,
1621 enum isl_dim_type type, unsigned first, unsigned num)
1623 int i;
1625 if (!dim)
1626 return NULL;
1628 if (num == 0)
1629 return isl_space_reset(dim, type);
1631 if (!valid_dim_type(type))
1632 isl_die(dim->ctx, isl_error_invalid,
1633 "cannot drop dimensions of specified type", goto error);
1635 if (first + num > n(dim, type) || first + num < first)
1636 isl_die(isl_space_get_ctx(dim), isl_error_invalid,
1637 "index out of bounds", return isl_space_free(dim));
1638 dim = isl_space_cow(dim);
1639 if (!dim)
1640 goto error;
1641 if (dim->ids) {
1642 dim = extend_ids(dim);
1643 if (!dim)
1644 goto error;
1645 for (i = 0; i < num; ++i)
1646 isl_id_free(get_id(dim, type, first + i));
1647 for (i = first+num; i < n(dim, type); ++i)
1648 set_id(dim, type, i - num, get_id(dim, type, i));
1649 switch (type) {
1650 case isl_dim_param:
1651 get_ids(dim, isl_dim_in, 0, dim->n_in,
1652 dim->ids + offset(dim, isl_dim_in) - num);
1653 case isl_dim_in:
1654 get_ids(dim, isl_dim_out, 0, dim->n_out,
1655 dim->ids + offset(dim, isl_dim_out) - num);
1656 default:
1659 dim->n_id -= num;
1661 switch (type) {
1662 case isl_dim_param: dim->nparam -= num; break;
1663 case isl_dim_in: dim->n_in -= num; break;
1664 case isl_dim_out: dim->n_out -= num; break;
1665 default: ;
1667 dim = isl_space_reset(dim, type);
1668 if (type == isl_dim_param) {
1669 if (dim && dim->nested[0] &&
1670 !(dim->nested[0] = isl_space_drop_dims(dim->nested[0],
1671 isl_dim_param, first, num)))
1672 goto error;
1673 if (dim && dim->nested[1] &&
1674 !(dim->nested[1] = isl_space_drop_dims(dim->nested[1],
1675 isl_dim_param, first, num)))
1676 goto error;
1678 return dim;
1679 error:
1680 isl_space_free(dim);
1681 return NULL;
1684 __isl_give isl_space *isl_space_drop_inputs(__isl_take isl_space *dim,
1685 unsigned first, unsigned n)
1687 if (!dim)
1688 return NULL;
1689 return isl_space_drop_dims(dim, isl_dim_in, first, n);
1692 __isl_give isl_space *isl_space_drop_outputs(__isl_take isl_space *dim,
1693 unsigned first, unsigned n)
1695 if (!dim)
1696 return NULL;
1697 return isl_space_drop_dims(dim, isl_dim_out, first, n);
1700 __isl_give isl_space *isl_space_domain(__isl_take isl_space *dim)
1702 if (!dim)
1703 return NULL;
1704 dim = isl_space_drop_outputs(dim, 0, dim->n_out);
1705 dim = isl_space_reverse(dim);
1706 dim = mark_as_set(dim);
1707 return dim;
1710 __isl_give isl_space *isl_space_from_domain(__isl_take isl_space *dim)
1712 if (!dim)
1713 return NULL;
1714 if (!isl_space_is_set(dim))
1715 isl_die(isl_space_get_ctx(dim), isl_error_invalid,
1716 "not a set space", goto error);
1717 dim = isl_space_reverse(dim);
1718 dim = isl_space_reset(dim, isl_dim_out);
1719 return dim;
1720 error:
1721 isl_space_free(dim);
1722 return NULL;
1725 __isl_give isl_space *isl_space_range(__isl_take isl_space *dim)
1727 if (!dim)
1728 return NULL;
1729 dim = isl_space_drop_inputs(dim, 0, dim->n_in);
1730 dim = mark_as_set(dim);
1731 return dim;
1734 __isl_give isl_space *isl_space_from_range(__isl_take isl_space *dim)
1736 if (!dim)
1737 return NULL;
1738 if (!isl_space_is_set(dim))
1739 isl_die(isl_space_get_ctx(dim), isl_error_invalid,
1740 "not a set space", goto error);
1741 return isl_space_reset(dim, isl_dim_in);
1742 error:
1743 isl_space_free(dim);
1744 return NULL;
1747 /* Given a map space A -> B, return the map space [A -> B] -> A.
1749 __isl_give isl_space *isl_space_domain_map(__isl_take isl_space *space)
1751 isl_space *domain;
1753 domain = isl_space_from_range(isl_space_domain(isl_space_copy(space)));
1754 space = isl_space_from_domain(isl_space_wrap(space));
1755 space = isl_space_join(space, domain);
1757 return space;
1760 /* Given a map space A -> B, return the map space [A -> B] -> B.
1762 __isl_give isl_space *isl_space_range_map(__isl_take isl_space *space)
1764 isl_space *range;
1766 range = isl_space_from_range(isl_space_range(isl_space_copy(space)));
1767 space = isl_space_from_domain(isl_space_wrap(space));
1768 space = isl_space_join(space, range);
1770 return space;
1773 __isl_give isl_space *isl_space_params(__isl_take isl_space *space)
1775 if (isl_space_is_params(space))
1776 return space;
1777 space = isl_space_drop_dims(space,
1778 isl_dim_in, 0, isl_space_dim(space, isl_dim_in));
1779 space = isl_space_drop_dims(space,
1780 isl_dim_out, 0, isl_space_dim(space, isl_dim_out));
1781 space = mark_as_params(space);
1782 return space;
1785 __isl_give isl_space *isl_space_set_from_params(__isl_take isl_space *space)
1787 if (!space)
1788 return NULL;
1789 if (!isl_space_is_params(space))
1790 isl_die(isl_space_get_ctx(space), isl_error_invalid,
1791 "not a parameter space", goto error);
1792 return isl_space_reset(space, isl_dim_set);
1793 error:
1794 isl_space_free(space);
1795 return NULL;
1798 __isl_give isl_space *isl_space_underlying(__isl_take isl_space *dim,
1799 unsigned n_div)
1801 int i;
1803 if (!dim)
1804 return NULL;
1805 if (n_div == 0 &&
1806 dim->nparam == 0 && dim->n_in == 0 && dim->n_id == 0)
1807 return isl_space_reset(isl_space_reset(dim, isl_dim_in), isl_dim_out);
1808 dim = isl_space_cow(dim);
1809 if (!dim)
1810 return NULL;
1811 dim->n_out += dim->nparam + dim->n_in + n_div;
1812 dim->nparam = 0;
1813 dim->n_in = 0;
1815 for (i = 0; i < dim->n_id; ++i)
1816 isl_id_free(get_id(dim, isl_dim_out, i));
1817 dim->n_id = 0;
1818 dim = isl_space_reset(dim, isl_dim_in);
1819 dim = isl_space_reset(dim, isl_dim_out);
1821 return dim;
1824 /* Are the two spaces the same, including positions and names of parameters?
1826 isl_bool isl_space_is_equal(__isl_keep isl_space *space1,
1827 __isl_keep isl_space *space2)
1829 isl_bool equal;
1831 if (!space1 || !space2)
1832 return isl_bool_error;
1833 if (space1 == space2)
1834 return isl_bool_true;
1835 equal = match(space1, isl_dim_param, space2, isl_dim_param);
1836 if (equal < 0 || !equal)
1837 return equal;
1838 return isl_space_has_equal_tuples(space1, space2);
1841 /* Is space1 equal to the domain of space2?
1843 * In the internal version we also allow space2 to be the space of a set,
1844 * provided space1 is a parameter space.
1846 isl_bool isl_space_is_domain_internal(__isl_keep isl_space *space1,
1847 __isl_keep isl_space *space2)
1849 if (!space1 || !space2)
1850 return isl_bool_error;
1851 if (!isl_space_is_set(space1))
1852 return isl_bool_false;
1853 return match(space1, isl_dim_param, space2, isl_dim_param) &&
1854 isl_space_tuple_is_equal(space1, isl_dim_set,
1855 space2, isl_dim_in);
1858 /* Is space1 equal to the domain of space2?
1860 isl_bool isl_space_is_domain(__isl_keep isl_space *space1,
1861 __isl_keep isl_space *space2)
1863 if (!space2)
1864 return isl_bool_error;
1865 if (!isl_space_is_map(space2))
1866 return isl_bool_false;
1867 return isl_space_is_domain_internal(space1, space2);
1870 /* Is space1 equal to the range of space2?
1872 * In the internal version, space2 is allowed to be the space of a set,
1873 * in which case it should be equal to space1.
1875 isl_bool isl_space_is_range_internal(__isl_keep isl_space *space1,
1876 __isl_keep isl_space *space2)
1878 if (!space1 || !space2)
1879 return isl_bool_error;
1880 if (!isl_space_is_set(space1))
1881 return isl_bool_false;
1882 return match(space1, isl_dim_param, space2, isl_dim_param) &&
1883 isl_space_tuple_is_equal(space1, isl_dim_set,
1884 space2, isl_dim_out);
1887 /* Is space1 equal to the range of space2?
1889 isl_bool isl_space_is_range(__isl_keep isl_space *space1,
1890 __isl_keep isl_space *space2)
1892 if (!space2)
1893 return isl_bool_error;
1894 if (!isl_space_is_map(space2))
1895 return isl_bool_false;
1896 return isl_space_is_range_internal(space1, space2);
1899 /* Update "hash" by hashing in "space".
1900 * Changes in this function should be reflected in isl_hash_space_domain.
1902 static uint32_t isl_hash_space(uint32_t hash, __isl_keep isl_space *space)
1904 int i;
1905 isl_id *id;
1907 if (!space)
1908 return hash;
1910 isl_hash_byte(hash, space->nparam % 256);
1911 isl_hash_byte(hash, space->n_in % 256);
1912 isl_hash_byte(hash, space->n_out % 256);
1914 for (i = 0; i < space->nparam; ++i) {
1915 id = get_id(space, isl_dim_param, i);
1916 hash = isl_hash_id(hash, id);
1919 id = tuple_id(space, isl_dim_in);
1920 hash = isl_hash_id(hash, id);
1921 id = tuple_id(space, isl_dim_out);
1922 hash = isl_hash_id(hash, id);
1924 hash = isl_hash_space(hash, space->nested[0]);
1925 hash = isl_hash_space(hash, space->nested[1]);
1927 return hash;
1930 /* Update "hash" by hashing in the domain of "space".
1931 * The result of this function is equal to the result of applying
1932 * isl_hash_space to the domain of "space".
1934 static uint32_t isl_hash_space_domain(uint32_t hash,
1935 __isl_keep isl_space *space)
1937 int i;
1938 isl_id *id;
1940 if (!space)
1941 return hash;
1943 isl_hash_byte(hash, space->nparam % 256);
1944 isl_hash_byte(hash, 0);
1945 isl_hash_byte(hash, space->n_in % 256);
1947 for (i = 0; i < space->nparam; ++i) {
1948 id = get_id(space, isl_dim_param, i);
1949 hash = isl_hash_id(hash, id);
1952 hash = isl_hash_id(hash, &isl_id_none);
1953 id = tuple_id(space, isl_dim_in);
1954 hash = isl_hash_id(hash, id);
1956 hash = isl_hash_space(hash, space->nested[0]);
1958 return hash;
1961 uint32_t isl_space_get_hash(__isl_keep isl_space *dim)
1963 uint32_t hash;
1965 if (!dim)
1966 return 0;
1968 hash = isl_hash_init();
1969 hash = isl_hash_space(hash, dim);
1971 return hash;
1974 /* Return the hash value of the domain of "space".
1975 * That is, isl_space_get_domain_hash(space) is equal to
1976 * isl_space_get_hash(isl_space_domain(space)).
1978 uint32_t isl_space_get_domain_hash(__isl_keep isl_space *space)
1980 uint32_t hash;
1982 if (!space)
1983 return 0;
1985 hash = isl_hash_init();
1986 hash = isl_hash_space_domain(hash, space);
1988 return hash;
1991 isl_bool isl_space_is_wrapping(__isl_keep isl_space *dim)
1993 if (!dim)
1994 return isl_bool_error;
1996 if (!isl_space_is_set(dim))
1997 return isl_bool_false;
1999 return dim->nested[1] != NULL;
2002 /* Is "space" the space of a map where the domain is a wrapped map space?
2004 isl_bool isl_space_domain_is_wrapping(__isl_keep isl_space *space)
2006 if (!space)
2007 return isl_bool_error;
2009 if (isl_space_is_set(space))
2010 return isl_bool_false;
2012 return space->nested[0] != NULL;
2015 /* Is "space" the space of a map where the range is a wrapped map space?
2017 isl_bool isl_space_range_is_wrapping(__isl_keep isl_space *space)
2019 if (!space)
2020 return isl_bool_error;
2022 if (isl_space_is_set(space))
2023 return isl_bool_false;
2025 return space->nested[1] != NULL;
2028 __isl_give isl_space *isl_space_wrap(__isl_take isl_space *dim)
2030 isl_space *wrap;
2032 if (!dim)
2033 return NULL;
2035 wrap = isl_space_set_alloc(dim->ctx,
2036 dim->nparam, dim->n_in + dim->n_out);
2038 wrap = copy_ids(wrap, isl_dim_param, 0, dim, isl_dim_param);
2039 wrap = copy_ids(wrap, isl_dim_set, 0, dim, isl_dim_in);
2040 wrap = copy_ids(wrap, isl_dim_set, dim->n_in, dim, isl_dim_out);
2042 if (!wrap)
2043 goto error;
2045 wrap->nested[1] = dim;
2047 return wrap;
2048 error:
2049 isl_space_free(dim);
2050 return NULL;
2053 __isl_give isl_space *isl_space_unwrap(__isl_take isl_space *dim)
2055 isl_space *unwrap;
2057 if (!dim)
2058 return NULL;
2060 if (!isl_space_is_wrapping(dim))
2061 isl_die(dim->ctx, isl_error_invalid, "not a wrapping space",
2062 goto error);
2064 unwrap = isl_space_copy(dim->nested[1]);
2065 isl_space_free(dim);
2067 return unwrap;
2068 error:
2069 isl_space_free(dim);
2070 return NULL;
2073 isl_bool isl_space_is_named_or_nested(__isl_keep isl_space *space,
2074 enum isl_dim_type type)
2076 if (type != isl_dim_in && type != isl_dim_out)
2077 return isl_bool_false;
2078 if (!space)
2079 return isl_bool_error;
2080 if (space->tuple_id[type - isl_dim_in])
2081 return isl_bool_true;
2082 if (space->nested[type - isl_dim_in])
2083 return isl_bool_true;
2084 return isl_bool_false;
2087 isl_bool isl_space_may_be_set(__isl_keep isl_space *space)
2089 isl_bool nested;
2091 if (!space)
2092 return isl_bool_error;
2093 if (isl_space_is_set(space))
2094 return isl_bool_true;
2095 if (isl_space_dim(space, isl_dim_in) != 0)
2096 return isl_bool_false;
2097 nested = isl_space_is_named_or_nested(space, isl_dim_in);
2098 if (nested < 0 || nested)
2099 return isl_bool_not(nested);
2100 return isl_bool_true;
2103 __isl_give isl_space *isl_space_reset(__isl_take isl_space *dim,
2104 enum isl_dim_type type)
2106 if (!isl_space_is_named_or_nested(dim, type))
2107 return dim;
2109 dim = isl_space_cow(dim);
2110 if (!dim)
2111 return NULL;
2113 isl_id_free(dim->tuple_id[type - isl_dim_in]);
2114 dim->tuple_id[type - isl_dim_in] = NULL;
2115 isl_space_free(dim->nested[type - isl_dim_in]);
2116 dim->nested[type - isl_dim_in] = NULL;
2118 return dim;
2121 __isl_give isl_space *isl_space_flatten(__isl_take isl_space *dim)
2123 if (!dim)
2124 return NULL;
2125 if (!dim->nested[0] && !dim->nested[1])
2126 return dim;
2128 if (dim->nested[0])
2129 dim = isl_space_reset(dim, isl_dim_in);
2130 if (dim && dim->nested[1])
2131 dim = isl_space_reset(dim, isl_dim_out);
2133 return dim;
2136 __isl_give isl_space *isl_space_flatten_domain(__isl_take isl_space *dim)
2138 if (!dim)
2139 return NULL;
2140 if (!dim->nested[0])
2141 return dim;
2143 return isl_space_reset(dim, isl_dim_in);
2146 __isl_give isl_space *isl_space_flatten_range(__isl_take isl_space *dim)
2148 if (!dim)
2149 return NULL;
2150 if (!dim->nested[1])
2151 return dim;
2153 return isl_space_reset(dim, isl_dim_out);
2156 /* Replace the dimensions of the given type of dst by those of src.
2158 __isl_give isl_space *isl_space_replace(__isl_take isl_space *dst,
2159 enum isl_dim_type type, __isl_keep isl_space *src)
2161 dst = isl_space_cow(dst);
2163 if (!dst || !src)
2164 goto error;
2166 dst = isl_space_drop_dims(dst, type, 0, isl_space_dim(dst, type));
2167 dst = isl_space_add_dims(dst, type, isl_space_dim(src, type));
2168 dst = copy_ids(dst, type, 0, src, type);
2170 if (dst && type == isl_dim_param) {
2171 int i;
2172 for (i = 0; i <= 1; ++i) {
2173 if (!dst->nested[i])
2174 continue;
2175 dst->nested[i] = isl_space_replace(dst->nested[i],
2176 type, src);
2177 if (!dst->nested[i])
2178 goto error;
2182 return dst;
2183 error:
2184 isl_space_free(dst);
2185 return NULL;
2188 /* Given a dimension specification "dim" of a set, create a dimension
2189 * specification for the lift of the set. In particular, the result
2190 * is of the form [dim -> local[..]], with n_local variables in the
2191 * range of the wrapped map.
2193 __isl_give isl_space *isl_space_lift(__isl_take isl_space *dim, unsigned n_local)
2195 isl_space *local_dim;
2197 if (!dim)
2198 return NULL;
2200 local_dim = isl_space_dup(dim);
2201 local_dim = isl_space_drop_dims(local_dim, isl_dim_set, 0, dim->n_out);
2202 local_dim = isl_space_add_dims(local_dim, isl_dim_set, n_local);
2203 local_dim = isl_space_set_tuple_name(local_dim, isl_dim_set, "local");
2204 dim = isl_space_join(isl_space_from_domain(dim),
2205 isl_space_from_range(local_dim));
2206 dim = isl_space_wrap(dim);
2207 dim = isl_space_set_tuple_name(dim, isl_dim_set, "lifted");
2209 return dim;
2212 isl_bool isl_space_can_zip(__isl_keep isl_space *dim)
2214 if (!dim)
2215 return isl_bool_error;
2217 return dim->nested[0] && dim->nested[1];
2220 __isl_give isl_space *isl_space_zip(__isl_take isl_space *dim)
2222 isl_space *dom, *ran;
2223 isl_space *dom_dom, *dom_ran, *ran_dom, *ran_ran;
2225 if (!isl_space_can_zip(dim))
2226 isl_die(dim->ctx, isl_error_invalid, "dim cannot be zipped",
2227 goto error);
2229 if (!dim)
2230 return NULL;
2231 dom = isl_space_unwrap(isl_space_domain(isl_space_copy(dim)));
2232 ran = isl_space_unwrap(isl_space_range(dim));
2233 dom_dom = isl_space_domain(isl_space_copy(dom));
2234 dom_ran = isl_space_range(dom);
2235 ran_dom = isl_space_domain(isl_space_copy(ran));
2236 ran_ran = isl_space_range(ran);
2237 dom = isl_space_join(isl_space_from_domain(dom_dom),
2238 isl_space_from_range(ran_dom));
2239 ran = isl_space_join(isl_space_from_domain(dom_ran),
2240 isl_space_from_range(ran_ran));
2241 return isl_space_join(isl_space_from_domain(isl_space_wrap(dom)),
2242 isl_space_from_range(isl_space_wrap(ran)));
2243 error:
2244 isl_space_free(dim);
2245 return NULL;
2248 /* Can we apply isl_space_curry to "space"?
2249 * That is, does it have a nested relation in its domain?
2251 isl_bool isl_space_can_curry(__isl_keep isl_space *space)
2253 if (!space)
2254 return isl_bool_error;
2256 return !!space->nested[0];
2259 /* Given a space (A -> B) -> C, return the corresponding space
2260 * A -> (B -> C).
2262 __isl_give isl_space *isl_space_curry(__isl_take isl_space *space)
2264 isl_space *dom, *ran;
2265 isl_space *dom_dom, *dom_ran;
2267 if (!space)
2268 return NULL;
2270 if (!isl_space_can_curry(space))
2271 isl_die(space->ctx, isl_error_invalid,
2272 "space cannot be curried", goto error);
2274 dom = isl_space_unwrap(isl_space_domain(isl_space_copy(space)));
2275 ran = isl_space_range(space);
2276 dom_dom = isl_space_domain(isl_space_copy(dom));
2277 dom_ran = isl_space_range(dom);
2278 ran = isl_space_join(isl_space_from_domain(dom_ran),
2279 isl_space_from_range(ran));
2280 return isl_space_join(isl_space_from_domain(dom_dom),
2281 isl_space_from_range(isl_space_wrap(ran)));
2282 error:
2283 isl_space_free(space);
2284 return NULL;
2287 /* Can isl_space_range_curry be applied to "space"?
2288 * That is, does it have a nested relation in its range,
2289 * the domain of which is itself a nested relation?
2291 isl_bool isl_space_can_range_curry(__isl_keep isl_space *space)
2293 isl_bool can;
2295 if (!space)
2296 return isl_bool_error;
2297 can = isl_space_range_is_wrapping(space);
2298 if (can < 0 || !can)
2299 return can;
2300 return isl_space_can_curry(space->nested[1]);
2303 /* Given a space A -> ((B -> C) -> D), return the corresponding space
2304 * A -> (B -> (C -> D)).
2306 __isl_give isl_space *isl_space_range_curry(__isl_take isl_space *space)
2308 if (!space)
2309 return NULL;
2311 if (!isl_space_can_range_curry(space))
2312 isl_die(isl_space_get_ctx(space), isl_error_invalid,
2313 "space range cannot be curried",
2314 return isl_space_free(space));
2316 space = isl_space_cow(space);
2317 if (!space)
2318 return NULL;
2319 space->nested[1] = isl_space_curry(space->nested[1]);
2320 if (!space->nested[1])
2321 return isl_space_free(space);
2323 return space;
2326 /* Can we apply isl_space_uncurry to "space"?
2327 * That is, does it have a nested relation in its range?
2329 isl_bool isl_space_can_uncurry(__isl_keep isl_space *space)
2331 if (!space)
2332 return isl_bool_error;
2334 return !!space->nested[1];
2337 /* Given a space A -> (B -> C), return the corresponding space
2338 * (A -> B) -> C.
2340 __isl_give isl_space *isl_space_uncurry(__isl_take isl_space *space)
2342 isl_space *dom, *ran;
2343 isl_space *ran_dom, *ran_ran;
2345 if (!space)
2346 return NULL;
2348 if (!isl_space_can_uncurry(space))
2349 isl_die(space->ctx, isl_error_invalid,
2350 "space cannot be uncurried",
2351 return isl_space_free(space));
2353 dom = isl_space_domain(isl_space_copy(space));
2354 ran = isl_space_unwrap(isl_space_range(space));
2355 ran_dom = isl_space_domain(isl_space_copy(ran));
2356 ran_ran = isl_space_range(ran);
2357 dom = isl_space_join(isl_space_from_domain(dom),
2358 isl_space_from_range(ran_dom));
2359 return isl_space_join(isl_space_from_domain(isl_space_wrap(dom)),
2360 isl_space_from_range(ran_ran));
2363 isl_bool isl_space_has_named_params(__isl_keep isl_space *space)
2365 int i;
2366 unsigned off;
2368 if (!space)
2369 return isl_bool_error;
2370 if (space->nparam == 0)
2371 return isl_bool_true;
2372 off = isl_space_offset(space, isl_dim_param);
2373 if (off + space->nparam > space->n_id)
2374 return isl_bool_false;
2375 for (i = 0; i < space->nparam; ++i)
2376 if (!space->ids[off + i])
2377 return isl_bool_false;
2378 return isl_bool_true;
2381 /* Check that "space" has only named parameters, reporting an error
2382 * if it does not.
2384 isl_stat isl_space_check_named_params(__isl_keep isl_space *space)
2386 isl_bool named;
2388 named = isl_space_has_named_params(space);
2389 if (named < 0)
2390 return isl_stat_error;
2391 if (!named)
2392 isl_die(isl_space_get_ctx(space), isl_error_invalid,
2393 "unaligned unnamed parameters", return isl_stat_error);
2395 return isl_stat_ok;
2398 /* Align the initial parameters of dim1 to match the order in dim2.
2400 __isl_give isl_space *isl_space_align_params(__isl_take isl_space *dim1,
2401 __isl_take isl_space *dim2)
2403 isl_reordering *exp;
2405 if (!isl_space_has_named_params(dim1) || !isl_space_has_named_params(dim2))
2406 isl_die(isl_space_get_ctx(dim1), isl_error_invalid,
2407 "parameter alignment requires named parameters",
2408 goto error);
2410 dim2 = isl_space_params(dim2);
2411 exp = isl_parameter_alignment_reordering(dim1, dim2);
2412 exp = isl_reordering_extend_space(exp, dim1);
2413 isl_space_free(dim2);
2414 if (!exp)
2415 return NULL;
2416 dim1 = isl_space_copy(exp->dim);
2417 isl_reordering_free(exp);
2418 return dim1;
2419 error:
2420 isl_space_free(dim1);
2421 isl_space_free(dim2);
2422 return NULL;
2425 /* Given the space of set (domain), construct a space for a map
2426 * with as domain the given space and as range the range of "model".
2428 __isl_give isl_space *isl_space_extend_domain_with_range(
2429 __isl_take isl_space *space, __isl_take isl_space *model)
2431 if (!model)
2432 goto error;
2434 space = isl_space_from_domain(space);
2435 space = isl_space_add_dims(space, isl_dim_out,
2436 isl_space_dim(model, isl_dim_out));
2437 if (isl_space_has_tuple_id(model, isl_dim_out))
2438 space = isl_space_set_tuple_id(space, isl_dim_out,
2439 isl_space_get_tuple_id(model, isl_dim_out));
2440 if (!space)
2441 goto error;
2442 if (model->nested[1]) {
2443 isl_space *nested = isl_space_copy(model->nested[1]);
2444 int n_nested, n_space;
2445 nested = isl_space_align_params(nested, isl_space_copy(space));
2446 n_nested = isl_space_dim(nested, isl_dim_param);
2447 n_space = isl_space_dim(space, isl_dim_param);
2448 if (n_nested > n_space)
2449 nested = isl_space_drop_dims(nested, isl_dim_param,
2450 n_space, n_nested - n_space);
2451 if (!nested)
2452 goto error;
2453 space->nested[1] = nested;
2455 isl_space_free(model);
2456 return space;
2457 error:
2458 isl_space_free(model);
2459 isl_space_free(space);
2460 return NULL;
2463 /* Compare the "type" dimensions of two isl_spaces.
2465 * The order is fairly arbitrary.
2467 static int isl_space_cmp_type(__isl_keep isl_space *space1,
2468 __isl_keep isl_space *space2, enum isl_dim_type type)
2470 int cmp;
2471 isl_space *nested1, *nested2;
2473 if (isl_space_dim(space1, type) != isl_space_dim(space2, type))
2474 return isl_space_dim(space1, type) -
2475 isl_space_dim(space2, type);
2477 cmp = isl_id_cmp(tuple_id(space1, type), tuple_id(space2, type));
2478 if (cmp != 0)
2479 return cmp;
2481 nested1 = nested(space1, type);
2482 nested2 = nested(space2, type);
2483 if (!nested1 != !nested2)
2484 return !nested1 - !nested2;
2486 if (nested1)
2487 return isl_space_cmp(nested1, nested2);
2489 return 0;
2492 /* Compare two isl_spaces.
2494 * The order is fairly arbitrary.
2496 int isl_space_cmp(__isl_keep isl_space *space1, __isl_keep isl_space *space2)
2498 int i;
2499 int cmp;
2501 if (space1 == space2)
2502 return 0;
2503 if (!space1)
2504 return -1;
2505 if (!space2)
2506 return 1;
2508 cmp = isl_space_cmp_type(space1, space2, isl_dim_param);
2509 if (cmp != 0)
2510 return cmp;
2511 cmp = isl_space_cmp_type(space1, space2, isl_dim_in);
2512 if (cmp != 0)
2513 return cmp;
2514 cmp = isl_space_cmp_type(space1, space2, isl_dim_out);
2515 if (cmp != 0)
2516 return cmp;
2518 if (!space1->ids && !space2->ids)
2519 return 0;
2521 for (i = 0; i < n(space1, isl_dim_param); ++i) {
2522 cmp = isl_id_cmp(get_id(space1, isl_dim_param, i),
2523 get_id(space2, isl_dim_param, i));
2524 if (cmp != 0)
2525 return cmp;
2528 return 0;