drop isl_space_as_set_space
[isl.git] / isl_space.c
blob3d97e474b8e9f135622f3ac59a9135749f2fe100
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 __isl_give isl_space *isl_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_add_dims(__isl_take isl_space *dim,
921 enum isl_dim_type type, unsigned n)
923 dim = isl_space_reset(dim, type);
924 if (!dim)
925 return NULL;
926 switch (type) {
927 case isl_dim_param:
928 dim = isl_space_extend(dim,
929 dim->nparam + n, dim->n_in, dim->n_out);
930 if (dim && dim->nested[0] &&
931 !(dim->nested[0] = isl_space_add_dims(dim->nested[0],
932 isl_dim_param, n)))
933 goto error;
934 if (dim && dim->nested[1] &&
935 !(dim->nested[1] = isl_space_add_dims(dim->nested[1],
936 isl_dim_param, n)))
937 goto error;
938 return dim;
939 case isl_dim_in:
940 return isl_space_extend(dim,
941 dim->nparam, dim->n_in + n, dim->n_out);
942 case isl_dim_out:
943 return isl_space_extend(dim,
944 dim->nparam, dim->n_in, dim->n_out + n);
945 default:
946 isl_die(dim->ctx, isl_error_invalid,
947 "cannot add dimensions of specified type", goto error);
949 error:
950 isl_space_free(dim);
951 return NULL;
954 static int valid_dim_type(enum isl_dim_type type)
956 switch (type) {
957 case isl_dim_param:
958 case isl_dim_in:
959 case isl_dim_out:
960 return 1;
961 default:
962 return 0;
966 /* Insert "n" dimensions of type "type" at position "pos".
967 * If we are inserting parameters, then they are also inserted in
968 * any nested spaces.
970 __isl_give isl_space *isl_space_insert_dims(__isl_take isl_space *dim,
971 enum isl_dim_type type, unsigned pos, unsigned n)
973 isl_id **ids = NULL;
975 if (!dim)
976 return NULL;
977 if (n == 0)
978 return isl_space_reset(dim, type);
980 if (!valid_dim_type(type))
981 isl_die(dim->ctx, isl_error_invalid,
982 "cannot insert dimensions of specified type",
983 goto error);
985 isl_assert(dim->ctx, pos <= isl_space_dim(dim, type), goto error);
987 dim = isl_space_cow(dim);
988 if (!dim)
989 return NULL;
991 if (dim->ids) {
992 enum isl_dim_type t, o = isl_dim_param;
993 int off;
994 int s[3];
995 ids = isl_calloc_array(dim->ctx, isl_id *,
996 dim->nparam + dim->n_in + dim->n_out + n);
997 if (!ids)
998 goto error;
999 off = 0;
1000 s[isl_dim_param - o] = dim->nparam;
1001 s[isl_dim_in - o] = dim->n_in;
1002 s[isl_dim_out - o] = dim->n_out;
1003 for (t = isl_dim_param; t <= isl_dim_out; ++t) {
1004 if (t != type) {
1005 get_ids(dim, t, 0, s[t - o], ids + off);
1006 off += s[t - o];
1007 } else {
1008 get_ids(dim, t, 0, pos, ids + off);
1009 off += pos + n;
1010 get_ids(dim, t, pos, s[t - o] - pos, ids + off);
1011 off += s[t - o] - pos;
1014 free(dim->ids);
1015 dim->ids = ids;
1016 dim->n_id = dim->nparam + dim->n_in + dim->n_out + n;
1018 switch (type) {
1019 case isl_dim_param: dim->nparam += n; break;
1020 case isl_dim_in: dim->n_in += n; break;
1021 case isl_dim_out: dim->n_out += n; break;
1022 default: ;
1024 dim = isl_space_reset(dim, type);
1026 if (type == isl_dim_param) {
1027 if (dim && dim->nested[0] &&
1028 !(dim->nested[0] = isl_space_insert_dims(dim->nested[0],
1029 isl_dim_param, pos, n)))
1030 goto error;
1031 if (dim && dim->nested[1] &&
1032 !(dim->nested[1] = isl_space_insert_dims(dim->nested[1],
1033 isl_dim_param, pos, n)))
1034 goto error;
1037 return dim;
1038 error:
1039 isl_space_free(dim);
1040 return NULL;
1043 __isl_give isl_space *isl_space_move_dims(__isl_take isl_space *dim,
1044 enum isl_dim_type dst_type, unsigned dst_pos,
1045 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
1047 int i;
1049 if (!dim)
1050 return NULL;
1051 if (n == 0) {
1052 dim = isl_space_reset(dim, src_type);
1053 return isl_space_reset(dim, dst_type);
1056 isl_assert(dim->ctx, src_pos + n <= isl_space_dim(dim, src_type),
1057 goto error);
1059 if (dst_type == src_type && dst_pos == src_pos)
1060 return dim;
1062 isl_assert(dim->ctx, dst_type != src_type, goto error);
1064 dim = isl_space_reset(dim, src_type);
1065 dim = isl_space_reset(dim, dst_type);
1067 dim = isl_space_cow(dim);
1068 if (!dim)
1069 return NULL;
1071 if (dim->ids) {
1072 isl_id **ids;
1073 enum isl_dim_type t, o = isl_dim_param;
1074 int off;
1075 int s[3];
1076 ids = isl_calloc_array(dim->ctx, isl_id *,
1077 dim->nparam + dim->n_in + dim->n_out);
1078 if (!ids)
1079 goto error;
1080 off = 0;
1081 s[isl_dim_param - o] = dim->nparam;
1082 s[isl_dim_in - o] = dim->n_in;
1083 s[isl_dim_out - o] = dim->n_out;
1084 for (t = isl_dim_param; t <= isl_dim_out; ++t) {
1085 if (t == dst_type) {
1086 get_ids(dim, t, 0, dst_pos, ids + off);
1087 off += dst_pos;
1088 get_ids(dim, src_type, src_pos, n, ids + off);
1089 off += n;
1090 get_ids(dim, t, dst_pos, s[t - o] - dst_pos,
1091 ids + off);
1092 off += s[t - o] - dst_pos;
1093 } else if (t == src_type) {
1094 get_ids(dim, t, 0, src_pos, ids + off);
1095 off += src_pos;
1096 get_ids(dim, t, src_pos + n,
1097 s[t - o] - src_pos - n, ids + off);
1098 off += s[t - o] - src_pos - n;
1099 } else {
1100 get_ids(dim, t, 0, s[t - o], ids + off);
1101 off += s[t - o];
1104 free(dim->ids);
1105 dim->ids = ids;
1106 dim->n_id = dim->nparam + dim->n_in + dim->n_out;
1109 switch (dst_type) {
1110 case isl_dim_param: dim->nparam += n; break;
1111 case isl_dim_in: dim->n_in += n; break;
1112 case isl_dim_out: dim->n_out += n; break;
1113 default: ;
1116 switch (src_type) {
1117 case isl_dim_param: dim->nparam -= n; break;
1118 case isl_dim_in: dim->n_in -= n; break;
1119 case isl_dim_out: dim->n_out -= n; break;
1120 default: ;
1123 if (dst_type != isl_dim_param && src_type != isl_dim_param)
1124 return dim;
1126 for (i = 0; i < 2; ++i) {
1127 if (!dim->nested[i])
1128 continue;
1129 dim->nested[i] = isl_space_replace(dim->nested[i],
1130 isl_dim_param, dim);
1131 if (!dim->nested[i])
1132 goto error;
1135 return dim;
1136 error:
1137 isl_space_free(dim);
1138 return NULL;
1141 __isl_give isl_space *isl_space_join(__isl_take isl_space *left,
1142 __isl_take isl_space *right)
1144 isl_space *dim;
1146 if (!left || !right)
1147 goto error;
1149 isl_assert(left->ctx, match(left, isl_dim_param, right, isl_dim_param),
1150 goto error);
1151 isl_assert(left->ctx,
1152 isl_space_tuple_is_equal(left, isl_dim_out, right, isl_dim_in),
1153 goto error);
1155 dim = isl_space_alloc(left->ctx, left->nparam, left->n_in, right->n_out);
1156 if (!dim)
1157 goto error;
1159 dim = copy_ids(dim, isl_dim_param, 0, left, isl_dim_param);
1160 dim = copy_ids(dim, isl_dim_in, 0, left, isl_dim_in);
1161 dim = copy_ids(dim, isl_dim_out, 0, right, isl_dim_out);
1163 if (dim && left->tuple_id[0] &&
1164 !(dim->tuple_id[0] = isl_id_copy(left->tuple_id[0])))
1165 goto error;
1166 if (dim && right->tuple_id[1] &&
1167 !(dim->tuple_id[1] = isl_id_copy(right->tuple_id[1])))
1168 goto error;
1169 if (dim && left->nested[0] &&
1170 !(dim->nested[0] = isl_space_copy(left->nested[0])))
1171 goto error;
1172 if (dim && right->nested[1] &&
1173 !(dim->nested[1] = isl_space_copy(right->nested[1])))
1174 goto error;
1176 isl_space_free(left);
1177 isl_space_free(right);
1179 return dim;
1180 error:
1181 isl_space_free(left);
1182 isl_space_free(right);
1183 return NULL;
1186 /* Given two map spaces { A -> C } and { B -> D }, construct the space
1187 * { [A -> B] -> [C -> D] }.
1188 * Given two set spaces { A } and { B }, construct the space { [A -> B] }.
1190 __isl_give isl_space *isl_space_product(__isl_take isl_space *left,
1191 __isl_take isl_space *right)
1193 isl_space *dom1, *dom2, *nest1, *nest2;
1194 int is_set;
1196 if (!left || !right)
1197 goto error;
1199 is_set = isl_space_is_set(left);
1200 if (is_set != isl_space_is_set(right))
1201 isl_die(isl_space_get_ctx(left), isl_error_invalid,
1202 "expecting either two set spaces or two map spaces",
1203 goto error);
1204 if (is_set)
1205 return isl_space_range_product(left, right);
1207 isl_assert(left->ctx, match(left, isl_dim_param, right, isl_dim_param),
1208 goto error);
1210 dom1 = isl_space_domain(isl_space_copy(left));
1211 dom2 = isl_space_domain(isl_space_copy(right));
1212 nest1 = isl_space_wrap(isl_space_join(isl_space_reverse(dom1), dom2));
1214 dom1 = isl_space_range(left);
1215 dom2 = isl_space_range(right);
1216 nest2 = isl_space_wrap(isl_space_join(isl_space_reverse(dom1), dom2));
1218 return isl_space_join(isl_space_reverse(nest1), nest2);
1219 error:
1220 isl_space_free(left);
1221 isl_space_free(right);
1222 return NULL;
1225 /* Given two spaces { A -> C } and { B -> C }, construct the space
1226 * { [A -> B] -> C }
1228 __isl_give isl_space *isl_space_domain_product(__isl_take isl_space *left,
1229 __isl_take isl_space *right)
1231 isl_space *ran, *dom1, *dom2, *nest;
1233 if (!left || !right)
1234 goto error;
1236 if (!match(left, isl_dim_param, right, isl_dim_param))
1237 isl_die(left->ctx, isl_error_invalid,
1238 "parameters need to match", goto error);
1239 if (!isl_space_tuple_is_equal(left, isl_dim_out, right, isl_dim_out))
1240 isl_die(left->ctx, isl_error_invalid,
1241 "ranges need to match", goto error);
1243 ran = isl_space_range(isl_space_copy(left));
1245 dom1 = isl_space_domain(left);
1246 dom2 = isl_space_domain(right);
1247 nest = isl_space_wrap(isl_space_join(isl_space_reverse(dom1), dom2));
1249 return isl_space_join(isl_space_reverse(nest), ran);
1250 error:
1251 isl_space_free(left);
1252 isl_space_free(right);
1253 return NULL;
1256 __isl_give isl_space *isl_space_range_product(__isl_take isl_space *left,
1257 __isl_take isl_space *right)
1259 isl_space *dom, *ran1, *ran2, *nest;
1261 if (!left || !right)
1262 goto error;
1264 isl_assert(left->ctx, match(left, isl_dim_param, right, isl_dim_param),
1265 goto error);
1266 if (!isl_space_tuple_is_equal(left, isl_dim_in, right, isl_dim_in))
1267 isl_die(left->ctx, isl_error_invalid,
1268 "domains need to match", goto error);
1270 dom = isl_space_domain(isl_space_copy(left));
1272 ran1 = isl_space_range(left);
1273 ran2 = isl_space_range(right);
1274 nest = isl_space_wrap(isl_space_join(isl_space_reverse(ran1), ran2));
1276 return isl_space_join(isl_space_reverse(dom), nest);
1277 error:
1278 isl_space_free(left);
1279 isl_space_free(right);
1280 return NULL;
1283 /* Given a space of the form [A -> B] -> [C -> D], return the space A -> C.
1285 __isl_give isl_space *isl_space_factor_domain(__isl_take isl_space *space)
1287 space = isl_space_domain_factor_domain(space);
1288 space = isl_space_range_factor_domain(space);
1289 return space;
1292 /* Given a space of the form [A -> B] -> C, return the space A -> C.
1294 __isl_give isl_space *isl_space_domain_factor_domain(
1295 __isl_take isl_space *space)
1297 isl_space *nested;
1298 isl_space *domain;
1300 if (!space)
1301 return NULL;
1302 if (!isl_space_domain_is_wrapping(space))
1303 isl_die(isl_space_get_ctx(space), isl_error_invalid,
1304 "domain not a product", return isl_space_free(space));
1306 nested = space->nested[0];
1307 domain = isl_space_copy(space);
1308 domain = isl_space_drop_dims(domain, isl_dim_in,
1309 nested->n_in, nested->n_out);
1310 if (!domain)
1311 return isl_space_free(space);
1312 if (nested->tuple_id[0]) {
1313 domain->tuple_id[0] = isl_id_copy(nested->tuple_id[0]);
1314 if (!domain->tuple_id[0])
1315 goto error;
1317 if (nested->nested[0]) {
1318 domain->nested[0] = isl_space_copy(nested->nested[0]);
1319 if (!domain->nested[0])
1320 goto error;
1323 isl_space_free(space);
1324 return domain;
1325 error:
1326 isl_space_free(space);
1327 isl_space_free(domain);
1328 return NULL;
1331 /* Given a space of the form [A -> B] -> C, return the space B -> C.
1333 __isl_give isl_space *isl_space_domain_factor_range(
1334 __isl_take isl_space *space)
1336 isl_space *nested;
1337 isl_space *range;
1339 if (!space)
1340 return NULL;
1341 if (!isl_space_domain_is_wrapping(space))
1342 isl_die(isl_space_get_ctx(space), isl_error_invalid,
1343 "domain not a product", return isl_space_free(space));
1345 nested = space->nested[0];
1346 range = isl_space_copy(space);
1347 range = isl_space_drop_dims(range, isl_dim_in, 0, nested->n_in);
1348 if (!range)
1349 return isl_space_free(space);
1350 if (nested->tuple_id[1]) {
1351 range->tuple_id[0] = isl_id_copy(nested->tuple_id[1]);
1352 if (!range->tuple_id[0])
1353 goto error;
1355 if (nested->nested[1]) {
1356 range->nested[0] = isl_space_copy(nested->nested[1]);
1357 if (!range->nested[0])
1358 goto error;
1361 isl_space_free(space);
1362 return range;
1363 error:
1364 isl_space_free(space);
1365 isl_space_free(range);
1366 return NULL;
1369 /* Given a space of the form A -> [B -> C], return the space A -> B.
1371 __isl_give isl_space *isl_space_range_factor_domain(
1372 __isl_take isl_space *space)
1374 isl_space *nested;
1375 isl_space *domain;
1377 if (!space)
1378 return NULL;
1379 if (!isl_space_range_is_wrapping(space))
1380 isl_die(isl_space_get_ctx(space), isl_error_invalid,
1381 "range not a product", return isl_space_free(space));
1383 nested = space->nested[1];
1384 domain = isl_space_copy(space);
1385 domain = isl_space_drop_dims(domain, isl_dim_out,
1386 nested->n_in, nested->n_out);
1387 if (!domain)
1388 return isl_space_free(space);
1389 if (nested->tuple_id[0]) {
1390 domain->tuple_id[1] = isl_id_copy(nested->tuple_id[0]);
1391 if (!domain->tuple_id[1])
1392 goto error;
1394 if (nested->nested[0]) {
1395 domain->nested[1] = isl_space_copy(nested->nested[0]);
1396 if (!domain->nested[1])
1397 goto error;
1400 isl_space_free(space);
1401 return domain;
1402 error:
1403 isl_space_free(space);
1404 isl_space_free(domain);
1405 return NULL;
1408 /* Internal function that selects the range of the map that is
1409 * embedded in either a set space or the range of a map space.
1410 * In particular, given a space of the form [A -> B], return the space B.
1411 * Given a space of the form A -> [B -> C], return the space A -> C.
1413 static __isl_give isl_space *range_factor_range(__isl_take isl_space *space)
1415 isl_space *nested;
1416 isl_space *range;
1418 if (!space)
1419 return NULL;
1421 nested = space->nested[1];
1422 range = isl_space_copy(space);
1423 range = isl_space_drop_dims(range, isl_dim_out, 0, nested->n_in);
1424 if (!range)
1425 return isl_space_free(space);
1426 if (nested->tuple_id[1]) {
1427 range->tuple_id[1] = isl_id_copy(nested->tuple_id[1]);
1428 if (!range->tuple_id[1])
1429 goto error;
1431 if (nested->nested[1]) {
1432 range->nested[1] = isl_space_copy(nested->nested[1]);
1433 if (!range->nested[1])
1434 goto error;
1437 isl_space_free(space);
1438 return range;
1439 error:
1440 isl_space_free(space);
1441 isl_space_free(range);
1442 return NULL;
1445 /* Given a space of the form A -> [B -> C], return the space A -> C.
1447 __isl_give isl_space *isl_space_range_factor_range(
1448 __isl_take isl_space *space)
1450 if (!space)
1451 return NULL;
1452 if (!isl_space_range_is_wrapping(space))
1453 isl_die(isl_space_get_ctx(space), isl_error_invalid,
1454 "range not a product", return isl_space_free(space));
1456 return range_factor_range(space);
1459 /* Given a space of the form [A -> B], return the space B.
1461 static __isl_give isl_space *set_factor_range(__isl_take isl_space *space)
1463 if (!space)
1464 return NULL;
1465 if (!isl_space_is_wrapping(space))
1466 isl_die(isl_space_get_ctx(space), isl_error_invalid,
1467 "not a product", return isl_space_free(space));
1469 return range_factor_range(space);
1472 /* Given a space of the form [A -> B] -> [C -> D], return the space B -> D.
1473 * Given a space of the form [A -> B], return the space B.
1475 __isl_give isl_space *isl_space_factor_range(__isl_take isl_space *space)
1477 if (!space)
1478 return NULL;
1479 if (isl_space_is_set(space))
1480 return set_factor_range(space);
1481 space = isl_space_domain_factor_range(space);
1482 space = isl_space_range_factor_range(space);
1483 return space;
1486 __isl_give isl_space *isl_space_map_from_set(__isl_take isl_space *dim)
1488 isl_ctx *ctx;
1489 isl_id **ids = NULL;
1491 if (!dim)
1492 return NULL;
1493 ctx = isl_space_get_ctx(dim);
1494 if (!isl_space_is_set(dim))
1495 isl_die(ctx, isl_error_invalid, "not a set space", goto error);
1496 dim = isl_space_cow(dim);
1497 if (!dim)
1498 return NULL;
1499 if (dim->ids) {
1500 ids = isl_calloc_array(dim->ctx, isl_id *,
1501 dim->nparam + dim->n_out + dim->n_out);
1502 if (!ids)
1503 goto error;
1504 get_ids(dim, isl_dim_param, 0, dim->nparam, ids);
1505 get_ids(dim, isl_dim_out, 0, dim->n_out, ids + dim->nparam);
1507 dim->n_in = dim->n_out;
1508 if (ids) {
1509 free(dim->ids);
1510 dim->ids = ids;
1511 dim->n_id = dim->nparam + dim->n_out + dim->n_out;
1512 dim = copy_ids(dim, isl_dim_out, 0, dim, isl_dim_in);
1514 isl_id_free(dim->tuple_id[0]);
1515 dim->tuple_id[0] = isl_id_copy(dim->tuple_id[1]);
1516 isl_space_free(dim->nested[0]);
1517 dim->nested[0] = isl_space_copy(dim->nested[1]);
1518 return dim;
1519 error:
1520 isl_space_free(dim);
1521 return NULL;
1524 __isl_give isl_space *isl_space_map_from_domain_and_range(
1525 __isl_take isl_space *domain, __isl_take isl_space *range)
1527 if (!domain || !range)
1528 goto error;
1529 if (!isl_space_is_set(domain))
1530 isl_die(isl_space_get_ctx(domain), isl_error_invalid,
1531 "domain is not a set space", goto error);
1532 if (!isl_space_is_set(range))
1533 isl_die(isl_space_get_ctx(range), isl_error_invalid,
1534 "range is not a set space", goto error);
1535 return isl_space_join(isl_space_reverse(domain), range);
1536 error:
1537 isl_space_free(domain);
1538 isl_space_free(range);
1539 return NULL;
1542 static __isl_give isl_space *set_ids(__isl_take isl_space *dim,
1543 enum isl_dim_type type,
1544 unsigned first, unsigned n, __isl_take isl_id **ids)
1546 int i;
1548 for (i = 0; i < n ; ++i)
1549 dim = set_id(dim, type, first + i, ids[i]);
1551 return dim;
1554 __isl_give isl_space *isl_space_reverse(__isl_take isl_space *dim)
1556 unsigned t;
1557 isl_space *nested;
1558 isl_id **ids = NULL;
1559 isl_id *id;
1561 if (!dim)
1562 return NULL;
1563 if (match(dim, isl_dim_in, dim, isl_dim_out))
1564 return dim;
1566 dim = isl_space_cow(dim);
1567 if (!dim)
1568 return NULL;
1570 id = dim->tuple_id[0];
1571 dim->tuple_id[0] = dim->tuple_id[1];
1572 dim->tuple_id[1] = id;
1574 nested = dim->nested[0];
1575 dim->nested[0] = dim->nested[1];
1576 dim->nested[1] = nested;
1578 if (dim->ids) {
1579 int n_id = dim->n_in + dim->n_out;
1580 ids = isl_alloc_array(dim->ctx, isl_id *, n_id);
1581 if (n_id && !ids)
1582 goto error;
1583 get_ids(dim, isl_dim_in, 0, dim->n_in, ids);
1584 get_ids(dim, isl_dim_out, 0, dim->n_out, ids + dim->n_in);
1587 t = dim->n_in;
1588 dim->n_in = dim->n_out;
1589 dim->n_out = t;
1591 if (dim->ids) {
1592 dim = set_ids(dim, isl_dim_out, 0, dim->n_out, ids);
1593 dim = set_ids(dim, isl_dim_in, 0, dim->n_in, ids + dim->n_out);
1594 free(ids);
1597 return dim;
1598 error:
1599 free(ids);
1600 isl_space_free(dim);
1601 return NULL;
1604 __isl_give isl_space *isl_space_drop_dims(__isl_take isl_space *dim,
1605 enum isl_dim_type type, unsigned first, unsigned num)
1607 int i;
1609 if (!dim)
1610 return NULL;
1612 if (num == 0)
1613 return isl_space_reset(dim, type);
1615 if (!valid_dim_type(type))
1616 isl_die(dim->ctx, isl_error_invalid,
1617 "cannot drop dimensions of specified type", goto error);
1619 if (first + num > n(dim, type) || first + num < first)
1620 isl_die(isl_space_get_ctx(dim), isl_error_invalid,
1621 "index out of bounds", return isl_space_free(dim));
1622 dim = isl_space_cow(dim);
1623 if (!dim)
1624 goto error;
1625 if (dim->ids) {
1626 dim = extend_ids(dim);
1627 if (!dim)
1628 goto error;
1629 for (i = 0; i < num; ++i)
1630 isl_id_free(get_id(dim, type, first + i));
1631 for (i = first+num; i < n(dim, type); ++i)
1632 set_id(dim, type, i - num, get_id(dim, type, i));
1633 switch (type) {
1634 case isl_dim_param:
1635 get_ids(dim, isl_dim_in, 0, dim->n_in,
1636 dim->ids + offset(dim, isl_dim_in) - num);
1637 case isl_dim_in:
1638 get_ids(dim, isl_dim_out, 0, dim->n_out,
1639 dim->ids + offset(dim, isl_dim_out) - num);
1640 default:
1643 dim->n_id -= num;
1645 switch (type) {
1646 case isl_dim_param: dim->nparam -= num; break;
1647 case isl_dim_in: dim->n_in -= num; break;
1648 case isl_dim_out: dim->n_out -= num; break;
1649 default: ;
1651 dim = isl_space_reset(dim, type);
1652 if (type == isl_dim_param) {
1653 if (dim && dim->nested[0] &&
1654 !(dim->nested[0] = isl_space_drop_dims(dim->nested[0],
1655 isl_dim_param, first, num)))
1656 goto error;
1657 if (dim && dim->nested[1] &&
1658 !(dim->nested[1] = isl_space_drop_dims(dim->nested[1],
1659 isl_dim_param, first, num)))
1660 goto error;
1662 return dim;
1663 error:
1664 isl_space_free(dim);
1665 return NULL;
1668 __isl_give isl_space *isl_space_drop_inputs(__isl_take isl_space *dim,
1669 unsigned first, unsigned n)
1671 if (!dim)
1672 return NULL;
1673 return isl_space_drop_dims(dim, isl_dim_in, first, n);
1676 __isl_give isl_space *isl_space_drop_outputs(__isl_take isl_space *dim,
1677 unsigned first, unsigned n)
1679 if (!dim)
1680 return NULL;
1681 return isl_space_drop_dims(dim, isl_dim_out, first, n);
1684 __isl_give isl_space *isl_space_domain(__isl_take isl_space *dim)
1686 if (!dim)
1687 return NULL;
1688 dim = isl_space_drop_outputs(dim, 0, dim->n_out);
1689 dim = isl_space_reverse(dim);
1690 dim = mark_as_set(dim);
1691 return dim;
1694 __isl_give isl_space *isl_space_from_domain(__isl_take isl_space *dim)
1696 if (!dim)
1697 return NULL;
1698 if (!isl_space_is_set(dim))
1699 isl_die(isl_space_get_ctx(dim), isl_error_invalid,
1700 "not a set space", goto error);
1701 dim = isl_space_reverse(dim);
1702 dim = isl_space_reset(dim, isl_dim_out);
1703 return dim;
1704 error:
1705 isl_space_free(dim);
1706 return NULL;
1709 __isl_give isl_space *isl_space_range(__isl_take isl_space *dim)
1711 if (!dim)
1712 return NULL;
1713 dim = isl_space_drop_inputs(dim, 0, dim->n_in);
1714 dim = mark_as_set(dim);
1715 return dim;
1718 __isl_give isl_space *isl_space_from_range(__isl_take isl_space *dim)
1720 if (!dim)
1721 return NULL;
1722 if (!isl_space_is_set(dim))
1723 isl_die(isl_space_get_ctx(dim), isl_error_invalid,
1724 "not a set space", goto error);
1725 return isl_space_reset(dim, isl_dim_in);
1726 error:
1727 isl_space_free(dim);
1728 return NULL;
1731 /* Given a map space A -> B, return the map space [A -> B] -> A.
1733 __isl_give isl_space *isl_space_domain_map(__isl_take isl_space *space)
1735 isl_space *domain;
1737 domain = isl_space_from_range(isl_space_domain(isl_space_copy(space)));
1738 space = isl_space_from_domain(isl_space_wrap(space));
1739 space = isl_space_join(space, domain);
1741 return space;
1744 /* Given a map space A -> B, return the map space [A -> B] -> B.
1746 __isl_give isl_space *isl_space_range_map(__isl_take isl_space *space)
1748 isl_space *range;
1750 range = isl_space_from_range(isl_space_range(isl_space_copy(space)));
1751 space = isl_space_from_domain(isl_space_wrap(space));
1752 space = isl_space_join(space, range);
1754 return space;
1757 __isl_give isl_space *isl_space_params(__isl_take isl_space *space)
1759 if (isl_space_is_params(space))
1760 return space;
1761 space = isl_space_drop_dims(space,
1762 isl_dim_in, 0, isl_space_dim(space, isl_dim_in));
1763 space = isl_space_drop_dims(space,
1764 isl_dim_out, 0, isl_space_dim(space, isl_dim_out));
1765 space = mark_as_params(space);
1766 return space;
1769 __isl_give isl_space *isl_space_set_from_params(__isl_take isl_space *space)
1771 if (!space)
1772 return NULL;
1773 if (!isl_space_is_params(space))
1774 isl_die(isl_space_get_ctx(space), isl_error_invalid,
1775 "not a parameter space", goto error);
1776 return isl_space_reset(space, isl_dim_set);
1777 error:
1778 isl_space_free(space);
1779 return NULL;
1782 __isl_give isl_space *isl_space_underlying(__isl_take isl_space *dim,
1783 unsigned n_div)
1785 int i;
1787 if (!dim)
1788 return NULL;
1789 if (n_div == 0 &&
1790 dim->nparam == 0 && dim->n_in == 0 && dim->n_id == 0)
1791 return isl_space_reset(isl_space_reset(dim, isl_dim_in), isl_dim_out);
1792 dim = isl_space_cow(dim);
1793 if (!dim)
1794 return NULL;
1795 dim->n_out += dim->nparam + dim->n_in + n_div;
1796 dim->nparam = 0;
1797 dim->n_in = 0;
1799 for (i = 0; i < dim->n_id; ++i)
1800 isl_id_free(get_id(dim, isl_dim_out, i));
1801 dim->n_id = 0;
1802 dim = isl_space_reset(dim, isl_dim_in);
1803 dim = isl_space_reset(dim, isl_dim_out);
1805 return dim;
1808 /* Are the two spaces the same, including positions and names of parameters?
1810 isl_bool isl_space_is_equal(__isl_keep isl_space *dim1,
1811 __isl_keep isl_space *dim2)
1813 if (!dim1 || !dim2)
1814 return isl_bool_error;
1815 if (dim1 == dim2)
1816 return isl_bool_true;
1817 return match(dim1, isl_dim_param, dim2, isl_dim_param) &&
1818 isl_space_tuple_is_equal(dim1, isl_dim_in, dim2, isl_dim_in) &&
1819 isl_space_tuple_is_equal(dim1, isl_dim_out, dim2, isl_dim_out);
1822 /* Is space1 equal to the domain of space2?
1824 * In the internal version we also allow space2 to be the space of a set,
1825 * provided space1 is a parameter space.
1827 isl_bool isl_space_is_domain_internal(__isl_keep isl_space *space1,
1828 __isl_keep isl_space *space2)
1830 if (!space1 || !space2)
1831 return isl_bool_error;
1832 if (!isl_space_is_set(space1))
1833 return isl_bool_false;
1834 return match(space1, isl_dim_param, space2, isl_dim_param) &&
1835 isl_space_tuple_is_equal(space1, isl_dim_set,
1836 space2, isl_dim_in);
1839 /* Is space1 equal to the domain of space2?
1841 isl_bool isl_space_is_domain(__isl_keep isl_space *space1,
1842 __isl_keep isl_space *space2)
1844 if (!space2)
1845 return isl_bool_error;
1846 if (!isl_space_is_map(space2))
1847 return isl_bool_false;
1848 return isl_space_is_domain_internal(space1, space2);
1851 /* Is space1 equal to the range of space2?
1853 * In the internal version, space2 is allowed to be the space of a set,
1854 * in which case it should be equal to space1.
1856 isl_bool isl_space_is_range_internal(__isl_keep isl_space *space1,
1857 __isl_keep isl_space *space2)
1859 if (!space1 || !space2)
1860 return isl_bool_error;
1861 if (!isl_space_is_set(space1))
1862 return isl_bool_false;
1863 return match(space1, isl_dim_param, space2, isl_dim_param) &&
1864 isl_space_tuple_is_equal(space1, isl_dim_set,
1865 space2, isl_dim_out);
1868 /* Is space1 equal to the range of space2?
1870 isl_bool isl_space_is_range(__isl_keep isl_space *space1,
1871 __isl_keep isl_space *space2)
1873 if (!space2)
1874 return isl_bool_error;
1875 if (!isl_space_is_map(space2))
1876 return isl_bool_false;
1877 return isl_space_is_range_internal(space1, space2);
1880 int isl_space_compatible_internal(__isl_keep isl_space *dim1,
1881 __isl_keep isl_space *dim2)
1883 return dim1->nparam == dim2->nparam &&
1884 dim1->n_in + dim1->n_out == dim2->n_in + dim2->n_out;
1887 int isl_space_compatible(__isl_keep isl_space *space1,
1888 __isl_keep isl_space *space2)
1890 return isl_space_compatible_internal(space1, space2);
1893 /* Update "hash" by hashing in "space".
1894 * Changes in this function should be reflected in isl_hash_space_domain.
1896 static uint32_t isl_hash_space(uint32_t hash, __isl_keep isl_space *space)
1898 int i;
1899 isl_id *id;
1901 if (!space)
1902 return hash;
1904 isl_hash_byte(hash, space->nparam % 256);
1905 isl_hash_byte(hash, space->n_in % 256);
1906 isl_hash_byte(hash, space->n_out % 256);
1908 for (i = 0; i < space->nparam; ++i) {
1909 id = get_id(space, isl_dim_param, i);
1910 hash = isl_hash_id(hash, id);
1913 id = tuple_id(space, isl_dim_in);
1914 hash = isl_hash_id(hash, id);
1915 id = tuple_id(space, isl_dim_out);
1916 hash = isl_hash_id(hash, id);
1918 hash = isl_hash_space(hash, space->nested[0]);
1919 hash = isl_hash_space(hash, space->nested[1]);
1921 return hash;
1924 /* Update "hash" by hashing in the domain of "space".
1925 * The result of this function is equal to the result of applying
1926 * isl_hash_space to the domain of "space".
1928 static uint32_t isl_hash_space_domain(uint32_t hash,
1929 __isl_keep isl_space *space)
1931 int i;
1932 isl_id *id;
1934 if (!space)
1935 return hash;
1937 isl_hash_byte(hash, space->nparam % 256);
1938 isl_hash_byte(hash, 0);
1939 isl_hash_byte(hash, space->n_in % 256);
1941 for (i = 0; i < space->nparam; ++i) {
1942 id = get_id(space, isl_dim_param, i);
1943 hash = isl_hash_id(hash, id);
1946 hash = isl_hash_id(hash, &isl_id_none);
1947 id = tuple_id(space, isl_dim_in);
1948 hash = isl_hash_id(hash, id);
1950 hash = isl_hash_space(hash, space->nested[0]);
1952 return hash;
1955 uint32_t isl_space_get_hash(__isl_keep isl_space *dim)
1957 uint32_t hash;
1959 if (!dim)
1960 return 0;
1962 hash = isl_hash_init();
1963 hash = isl_hash_space(hash, dim);
1965 return hash;
1968 /* Return the hash value of the domain of "space".
1969 * That is, isl_space_get_domain_hash(space) is equal to
1970 * isl_space_get_hash(isl_space_domain(space)).
1972 uint32_t isl_space_get_domain_hash(__isl_keep isl_space *space)
1974 uint32_t hash;
1976 if (!space)
1977 return 0;
1979 hash = isl_hash_init();
1980 hash = isl_hash_space_domain(hash, space);
1982 return hash;
1985 isl_bool isl_space_is_wrapping(__isl_keep isl_space *dim)
1987 if (!dim)
1988 return isl_bool_error;
1990 if (!isl_space_is_set(dim))
1991 return isl_bool_false;
1993 return dim->nested[1] != NULL;
1996 /* Is "space" the space of a map where the domain is a wrapped map space?
1998 isl_bool isl_space_domain_is_wrapping(__isl_keep isl_space *space)
2000 if (!space)
2001 return isl_bool_error;
2003 if (isl_space_is_set(space))
2004 return isl_bool_false;
2006 return space->nested[0] != NULL;
2009 /* Is "space" the space of a map where the range is a wrapped map space?
2011 isl_bool isl_space_range_is_wrapping(__isl_keep isl_space *space)
2013 if (!space)
2014 return isl_bool_error;
2016 if (isl_space_is_set(space))
2017 return isl_bool_false;
2019 return space->nested[1] != NULL;
2022 __isl_give isl_space *isl_space_wrap(__isl_take isl_space *dim)
2024 isl_space *wrap;
2026 if (!dim)
2027 return NULL;
2029 wrap = isl_space_set_alloc(dim->ctx,
2030 dim->nparam, dim->n_in + dim->n_out);
2032 wrap = copy_ids(wrap, isl_dim_param, 0, dim, isl_dim_param);
2033 wrap = copy_ids(wrap, isl_dim_set, 0, dim, isl_dim_in);
2034 wrap = copy_ids(wrap, isl_dim_set, dim->n_in, dim, isl_dim_out);
2036 if (!wrap)
2037 goto error;
2039 wrap->nested[1] = dim;
2041 return wrap;
2042 error:
2043 isl_space_free(dim);
2044 return NULL;
2047 __isl_give isl_space *isl_space_unwrap(__isl_take isl_space *dim)
2049 isl_space *unwrap;
2051 if (!dim)
2052 return NULL;
2054 if (!isl_space_is_wrapping(dim))
2055 isl_die(dim->ctx, isl_error_invalid, "not a wrapping space",
2056 goto error);
2058 unwrap = isl_space_copy(dim->nested[1]);
2059 isl_space_free(dim);
2061 return unwrap;
2062 error:
2063 isl_space_free(dim);
2064 return NULL;
2067 isl_bool isl_space_is_named_or_nested(__isl_keep isl_space *space,
2068 enum isl_dim_type type)
2070 if (type != isl_dim_in && type != isl_dim_out)
2071 return isl_bool_false;
2072 if (!space)
2073 return isl_bool_error;
2074 if (space->tuple_id[type - isl_dim_in])
2075 return isl_bool_true;
2076 if (space->nested[type - isl_dim_in])
2077 return isl_bool_true;
2078 return isl_bool_false;
2081 isl_bool isl_space_may_be_set(__isl_keep isl_space *space)
2083 isl_bool nested;
2085 if (!space)
2086 return isl_bool_error;
2087 if (isl_space_is_set(space))
2088 return isl_bool_true;
2089 if (isl_space_dim(space, isl_dim_in) != 0)
2090 return isl_bool_false;
2091 nested = isl_space_is_named_or_nested(space, isl_dim_in);
2092 if (nested < 0 || nested)
2093 return isl_bool_not(nested);
2094 return isl_bool_true;
2097 __isl_give isl_space *isl_space_reset(__isl_take isl_space *dim,
2098 enum isl_dim_type type)
2100 if (!isl_space_is_named_or_nested(dim, type))
2101 return dim;
2103 dim = isl_space_cow(dim);
2104 if (!dim)
2105 return NULL;
2107 isl_id_free(dim->tuple_id[type - isl_dim_in]);
2108 dim->tuple_id[type - isl_dim_in] = NULL;
2109 isl_space_free(dim->nested[type - isl_dim_in]);
2110 dim->nested[type - isl_dim_in] = NULL;
2112 return dim;
2115 __isl_give isl_space *isl_space_flatten(__isl_take isl_space *dim)
2117 if (!dim)
2118 return NULL;
2119 if (!dim->nested[0] && !dim->nested[1])
2120 return dim;
2122 if (dim->nested[0])
2123 dim = isl_space_reset(dim, isl_dim_in);
2124 if (dim && dim->nested[1])
2125 dim = isl_space_reset(dim, isl_dim_out);
2127 return dim;
2130 __isl_give isl_space *isl_space_flatten_domain(__isl_take isl_space *dim)
2132 if (!dim)
2133 return NULL;
2134 if (!dim->nested[0])
2135 return dim;
2137 return isl_space_reset(dim, isl_dim_in);
2140 __isl_give isl_space *isl_space_flatten_range(__isl_take isl_space *dim)
2142 if (!dim)
2143 return NULL;
2144 if (!dim->nested[1])
2145 return dim;
2147 return isl_space_reset(dim, isl_dim_out);
2150 /* Replace the dimensions of the given type of dst by those of src.
2152 __isl_give isl_space *isl_space_replace(__isl_take isl_space *dst,
2153 enum isl_dim_type type, __isl_keep isl_space *src)
2155 dst = isl_space_cow(dst);
2157 if (!dst || !src)
2158 goto error;
2160 dst = isl_space_drop_dims(dst, type, 0, isl_space_dim(dst, type));
2161 dst = isl_space_add_dims(dst, type, isl_space_dim(src, type));
2162 dst = copy_ids(dst, type, 0, src, type);
2164 if (dst && type == isl_dim_param) {
2165 int i;
2166 for (i = 0; i <= 1; ++i) {
2167 if (!dst->nested[i])
2168 continue;
2169 dst->nested[i] = isl_space_replace(dst->nested[i],
2170 type, src);
2171 if (!dst->nested[i])
2172 goto error;
2176 return dst;
2177 error:
2178 isl_space_free(dst);
2179 return NULL;
2182 /* Given a dimension specification "dim" of a set, create a dimension
2183 * specification for the lift of the set. In particular, the result
2184 * is of the form [dim -> local[..]], with n_local variables in the
2185 * range of the wrapped map.
2187 __isl_give isl_space *isl_space_lift(__isl_take isl_space *dim, unsigned n_local)
2189 isl_space *local_dim;
2191 if (!dim)
2192 return NULL;
2194 local_dim = isl_space_dup(dim);
2195 local_dim = isl_space_drop_dims(local_dim, isl_dim_set, 0, dim->n_out);
2196 local_dim = isl_space_add_dims(local_dim, isl_dim_set, n_local);
2197 local_dim = isl_space_set_tuple_name(local_dim, isl_dim_set, "local");
2198 dim = isl_space_join(isl_space_from_domain(dim),
2199 isl_space_from_range(local_dim));
2200 dim = isl_space_wrap(dim);
2201 dim = isl_space_set_tuple_name(dim, isl_dim_set, "lifted");
2203 return dim;
2206 isl_bool isl_space_can_zip(__isl_keep isl_space *dim)
2208 if (!dim)
2209 return isl_bool_error;
2211 return dim->nested[0] && dim->nested[1];
2214 __isl_give isl_space *isl_space_zip(__isl_take isl_space *dim)
2216 isl_space *dom, *ran;
2217 isl_space *dom_dom, *dom_ran, *ran_dom, *ran_ran;
2219 if (!isl_space_can_zip(dim))
2220 isl_die(dim->ctx, isl_error_invalid, "dim cannot be zipped",
2221 goto error);
2223 if (!dim)
2224 return NULL;
2225 dom = isl_space_unwrap(isl_space_domain(isl_space_copy(dim)));
2226 ran = isl_space_unwrap(isl_space_range(dim));
2227 dom_dom = isl_space_domain(isl_space_copy(dom));
2228 dom_ran = isl_space_range(dom);
2229 ran_dom = isl_space_domain(isl_space_copy(ran));
2230 ran_ran = isl_space_range(ran);
2231 dom = isl_space_join(isl_space_from_domain(dom_dom),
2232 isl_space_from_range(ran_dom));
2233 ran = isl_space_join(isl_space_from_domain(dom_ran),
2234 isl_space_from_range(ran_ran));
2235 return isl_space_join(isl_space_from_domain(isl_space_wrap(dom)),
2236 isl_space_from_range(isl_space_wrap(ran)));
2237 error:
2238 isl_space_free(dim);
2239 return NULL;
2242 /* Can we apply isl_space_curry to "space"?
2243 * That is, does it have a nested relation in its domain?
2245 isl_bool isl_space_can_curry(__isl_keep isl_space *space)
2247 if (!space)
2248 return isl_bool_error;
2250 return !!space->nested[0];
2253 /* Given a space (A -> B) -> C, return the corresponding space
2254 * A -> (B -> C).
2256 __isl_give isl_space *isl_space_curry(__isl_take isl_space *space)
2258 isl_space *dom, *ran;
2259 isl_space *dom_dom, *dom_ran;
2261 if (!space)
2262 return NULL;
2264 if (!isl_space_can_curry(space))
2265 isl_die(space->ctx, isl_error_invalid,
2266 "space cannot be curried", goto error);
2268 dom = isl_space_unwrap(isl_space_domain(isl_space_copy(space)));
2269 ran = isl_space_range(space);
2270 dom_dom = isl_space_domain(isl_space_copy(dom));
2271 dom_ran = isl_space_range(dom);
2272 ran = isl_space_join(isl_space_from_domain(dom_ran),
2273 isl_space_from_range(ran));
2274 return isl_space_join(isl_space_from_domain(dom_dom),
2275 isl_space_from_range(isl_space_wrap(ran)));
2276 error:
2277 isl_space_free(space);
2278 return NULL;
2281 /* Can isl_space_range_curry be applied to "space"?
2282 * That is, does it have a nested relation in its range,
2283 * the domain of which is itself a nested relation?
2285 isl_bool isl_space_can_range_curry(__isl_keep isl_space *space)
2287 isl_bool can;
2289 if (!space)
2290 return isl_bool_error;
2291 can = isl_space_range_is_wrapping(space);
2292 if (can < 0 || !can)
2293 return can;
2294 return isl_space_can_curry(space->nested[1]);
2297 /* Given a space A -> ((B -> C) -> D), return the corresponding space
2298 * A -> (B -> (C -> D)).
2300 __isl_give isl_space *isl_space_range_curry(__isl_take isl_space *space)
2302 if (!space)
2303 return NULL;
2305 if (!isl_space_can_range_curry(space))
2306 isl_die(isl_space_get_ctx(space), isl_error_invalid,
2307 "space range cannot be curried",
2308 return isl_space_free(space));
2310 space = isl_space_cow(space);
2311 if (!space)
2312 return NULL;
2313 space->nested[1] = isl_space_curry(space->nested[1]);
2314 if (!space->nested[1])
2315 return isl_space_free(space);
2317 return space;
2320 /* Can we apply isl_space_uncurry to "space"?
2321 * That is, does it have a nested relation in its range?
2323 isl_bool isl_space_can_uncurry(__isl_keep isl_space *space)
2325 if (!space)
2326 return isl_bool_error;
2328 return !!space->nested[1];
2331 /* Given a space A -> (B -> C), return the corresponding space
2332 * (A -> B) -> C.
2334 __isl_give isl_space *isl_space_uncurry(__isl_take isl_space *space)
2336 isl_space *dom, *ran;
2337 isl_space *ran_dom, *ran_ran;
2339 if (!space)
2340 return NULL;
2342 if (!isl_space_can_uncurry(space))
2343 isl_die(space->ctx, isl_error_invalid,
2344 "space cannot be uncurried",
2345 return isl_space_free(space));
2347 dom = isl_space_domain(isl_space_copy(space));
2348 ran = isl_space_unwrap(isl_space_range(space));
2349 ran_dom = isl_space_domain(isl_space_copy(ran));
2350 ran_ran = isl_space_range(ran);
2351 dom = isl_space_join(isl_space_from_domain(dom),
2352 isl_space_from_range(ran_dom));
2353 return isl_space_join(isl_space_from_domain(isl_space_wrap(dom)),
2354 isl_space_from_range(ran_ran));
2357 isl_bool isl_space_has_named_params(__isl_keep isl_space *space)
2359 int i;
2360 unsigned off;
2362 if (!space)
2363 return isl_bool_error;
2364 if (space->nparam == 0)
2365 return isl_bool_true;
2366 off = isl_space_offset(space, isl_dim_param);
2367 if (off + space->nparam > space->n_id)
2368 return isl_bool_false;
2369 for (i = 0; i < space->nparam; ++i)
2370 if (!space->ids[off + i])
2371 return isl_bool_false;
2372 return isl_bool_true;
2375 /* Align the initial parameters of dim1 to match the order in dim2.
2377 __isl_give isl_space *isl_space_align_params(__isl_take isl_space *dim1,
2378 __isl_take isl_space *dim2)
2380 isl_reordering *exp;
2382 if (!isl_space_has_named_params(dim1) || !isl_space_has_named_params(dim2))
2383 isl_die(isl_space_get_ctx(dim1), isl_error_invalid,
2384 "parameter alignment requires named parameters",
2385 goto error);
2387 dim2 = isl_space_params(dim2);
2388 exp = isl_parameter_alignment_reordering(dim1, dim2);
2389 exp = isl_reordering_extend_space(exp, dim1);
2390 isl_space_free(dim2);
2391 if (!exp)
2392 return NULL;
2393 dim1 = isl_space_copy(exp->dim);
2394 isl_reordering_free(exp);
2395 return dim1;
2396 error:
2397 isl_space_free(dim1);
2398 isl_space_free(dim2);
2399 return NULL;
2402 /* Given the space of set (domain), construct a space for a map
2403 * with as domain the given space and as range the range of "model".
2405 __isl_give isl_space *isl_space_extend_domain_with_range(
2406 __isl_take isl_space *space, __isl_take isl_space *model)
2408 if (!model)
2409 goto error;
2411 space = isl_space_from_domain(space);
2412 space = isl_space_add_dims(space, isl_dim_out,
2413 isl_space_dim(model, isl_dim_out));
2414 if (isl_space_has_tuple_id(model, isl_dim_out))
2415 space = isl_space_set_tuple_id(space, isl_dim_out,
2416 isl_space_get_tuple_id(model, isl_dim_out));
2417 if (!space)
2418 goto error;
2419 if (model->nested[1]) {
2420 isl_space *nested = isl_space_copy(model->nested[1]);
2421 int n_nested, n_space;
2422 nested = isl_space_align_params(nested, isl_space_copy(space));
2423 n_nested = isl_space_dim(nested, isl_dim_param);
2424 n_space = isl_space_dim(space, isl_dim_param);
2425 if (n_nested > n_space)
2426 nested = isl_space_drop_dims(nested, isl_dim_param,
2427 n_space, n_nested - n_space);
2428 if (!nested)
2429 goto error;
2430 space->nested[1] = nested;
2432 isl_space_free(model);
2433 return space;
2434 error:
2435 isl_space_free(model);
2436 isl_space_free(space);
2437 return NULL;
2440 /* Compare the "type" dimensions of two isl_spaces.
2442 * The order is fairly arbitrary.
2444 static int isl_space_cmp_type(__isl_keep isl_space *space1,
2445 __isl_keep isl_space *space2, enum isl_dim_type type)
2447 int cmp;
2448 isl_space *nested1, *nested2;
2450 if (isl_space_dim(space1, type) != isl_space_dim(space2, type))
2451 return isl_space_dim(space1, type) -
2452 isl_space_dim(space2, type);
2454 cmp = isl_id_cmp(tuple_id(space1, type), tuple_id(space2, type));
2455 if (cmp != 0)
2456 return cmp;
2458 nested1 = nested(space1, type);
2459 nested2 = nested(space2, type);
2460 if (!nested1 != !nested2)
2461 return !nested1 - !nested2;
2463 if (nested1)
2464 return isl_space_cmp(nested1, nested2);
2466 return 0;
2469 /* Compare two isl_spaces.
2471 * The order is fairly arbitrary.
2473 int isl_space_cmp(__isl_keep isl_space *space1, __isl_keep isl_space *space2)
2475 int i;
2476 int cmp;
2478 if (space1 == space2)
2479 return 0;
2480 if (!space1)
2481 return -1;
2482 if (!space2)
2483 return 1;
2485 cmp = isl_space_cmp_type(space1, space2, isl_dim_param);
2486 if (cmp != 0)
2487 return cmp;
2488 cmp = isl_space_cmp_type(space1, space2, isl_dim_in);
2489 if (cmp != 0)
2490 return cmp;
2491 cmp = isl_space_cmp_type(space1, space2, isl_dim_out);
2492 if (cmp != 0)
2493 return cmp;
2495 if (!space1->ids && !space2->ids)
2496 return 0;
2498 for (i = 0; i < n(space1, isl_dim_param); ++i) {
2499 cmp = isl_id_cmp(get_id(space1, isl_dim_param, i),
2500 get_id(space2, isl_dim_param, i));
2501 if (cmp != 0)
2502 return cmp;
2505 return 0;