isl_space_map_from_set: rename "dim" argument to "space"
[isl.git] / isl_space.c
blob207900b0ed42d5548693dbebef9ad8da6f313da2
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 int 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 1;
841 if (!isl_space_tuple_is_equal(space1, type1, space2, type2))
842 return 0;
844 if (!space1->ids && !space2->ids)
845 return 1;
847 for (i = 0; i < n(space1, type1); ++i) {
848 if (get_id(space1, type1, i) != get_id(space2, type2, i))
849 return 0;
851 return 1;
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 *space,
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 space = isl_space_reset(space, src_type);
1050 space = isl_space_reset(space, dst_type);
1051 if (!space)
1052 return NULL;
1053 if (n == 0)
1054 return space;
1056 isl_assert(space->ctx, src_pos + n <= isl_space_dim(space, src_type),
1057 goto error);
1059 if (dst_type == src_type && dst_pos == src_pos)
1060 return space;
1062 isl_assert(space->ctx, dst_type != src_type, goto error);
1064 space = isl_space_cow(space);
1065 if (!space)
1066 return NULL;
1068 if (space->ids) {
1069 isl_id **ids;
1070 enum isl_dim_type t, o = isl_dim_param;
1071 int off;
1072 int s[3];
1073 ids = isl_calloc_array(space->ctx, isl_id *,
1074 space->nparam + space->n_in + space->n_out);
1075 if (!ids)
1076 goto error;
1077 off = 0;
1078 s[isl_dim_param - o] = space->nparam;
1079 s[isl_dim_in - o] = space->n_in;
1080 s[isl_dim_out - o] = space->n_out;
1081 for (t = isl_dim_param; t <= isl_dim_out; ++t) {
1082 if (t == dst_type) {
1083 get_ids(space, t, 0, dst_pos, ids + off);
1084 off += dst_pos;
1085 get_ids(space, src_type, src_pos, n, ids + off);
1086 off += n;
1087 get_ids(space, t, dst_pos, s[t - o] - dst_pos,
1088 ids + off);
1089 off += s[t - o] - dst_pos;
1090 } else if (t == src_type) {
1091 get_ids(space, t, 0, src_pos, ids + off);
1092 off += src_pos;
1093 get_ids(space, t, src_pos + n,
1094 s[t - o] - src_pos - n, ids + off);
1095 off += s[t - o] - src_pos - n;
1096 } else {
1097 get_ids(space, t, 0, s[t - o], ids + off);
1098 off += s[t - o];
1101 free(space->ids);
1102 space->ids = ids;
1103 space->n_id = space->nparam + space->n_in + space->n_out;
1106 switch (dst_type) {
1107 case isl_dim_param: space->nparam += n; break;
1108 case isl_dim_in: space->n_in += n; break;
1109 case isl_dim_out: space->n_out += n; break;
1110 default: ;
1113 switch (src_type) {
1114 case isl_dim_param: space->nparam -= n; break;
1115 case isl_dim_in: space->n_in -= n; break;
1116 case isl_dim_out: space->n_out -= n; break;
1117 default: ;
1120 if (dst_type != isl_dim_param && src_type != isl_dim_param)
1121 return space;
1123 for (i = 0; i < 2; ++i) {
1124 if (!space->nested[i])
1125 continue;
1126 space->nested[i] = isl_space_replace(space->nested[i],
1127 isl_dim_param, space);
1128 if (!space->nested[i])
1129 goto error;
1132 return space;
1133 error:
1134 isl_space_free(space);
1135 return NULL;
1138 __isl_give isl_space *isl_space_join(__isl_take isl_space *left,
1139 __isl_take isl_space *right)
1141 isl_space *dim;
1143 if (!left || !right)
1144 goto error;
1146 isl_assert(left->ctx, match(left, isl_dim_param, right, isl_dim_param),
1147 goto error);
1148 isl_assert(left->ctx,
1149 isl_space_tuple_is_equal(left, isl_dim_out, right, isl_dim_in),
1150 goto error);
1152 dim = isl_space_alloc(left->ctx, left->nparam, left->n_in, right->n_out);
1153 if (!dim)
1154 goto error;
1156 dim = copy_ids(dim, isl_dim_param, 0, left, isl_dim_param);
1157 dim = copy_ids(dim, isl_dim_in, 0, left, isl_dim_in);
1158 dim = copy_ids(dim, isl_dim_out, 0, right, isl_dim_out);
1160 if (dim && left->tuple_id[0] &&
1161 !(dim->tuple_id[0] = isl_id_copy(left->tuple_id[0])))
1162 goto error;
1163 if (dim && right->tuple_id[1] &&
1164 !(dim->tuple_id[1] = isl_id_copy(right->tuple_id[1])))
1165 goto error;
1166 if (dim && left->nested[0] &&
1167 !(dim->nested[0] = isl_space_copy(left->nested[0])))
1168 goto error;
1169 if (dim && right->nested[1] &&
1170 !(dim->nested[1] = isl_space_copy(right->nested[1])))
1171 goto error;
1173 isl_space_free(left);
1174 isl_space_free(right);
1176 return dim;
1177 error:
1178 isl_space_free(left);
1179 isl_space_free(right);
1180 return NULL;
1183 /* Given two map spaces { A -> C } and { B -> D }, construct the space
1184 * { [A -> B] -> [C -> D] }.
1185 * Given two set spaces { A } and { B }, construct the space { [A -> B] }.
1187 __isl_give isl_space *isl_space_product(__isl_take isl_space *left,
1188 __isl_take isl_space *right)
1190 isl_space *dom1, *dom2, *nest1, *nest2;
1191 int is_set;
1193 if (!left || !right)
1194 goto error;
1196 is_set = isl_space_is_set(left);
1197 if (is_set != isl_space_is_set(right))
1198 isl_die(isl_space_get_ctx(left), isl_error_invalid,
1199 "expecting either two set spaces or two map spaces",
1200 goto error);
1201 if (is_set)
1202 return isl_space_range_product(left, right);
1204 isl_assert(left->ctx, match(left, isl_dim_param, right, isl_dim_param),
1205 goto error);
1207 dom1 = isl_space_domain(isl_space_copy(left));
1208 dom2 = isl_space_domain(isl_space_copy(right));
1209 nest1 = isl_space_wrap(isl_space_join(isl_space_reverse(dom1), dom2));
1211 dom1 = isl_space_range(left);
1212 dom2 = isl_space_range(right);
1213 nest2 = isl_space_wrap(isl_space_join(isl_space_reverse(dom1), dom2));
1215 return isl_space_join(isl_space_reverse(nest1), nest2);
1216 error:
1217 isl_space_free(left);
1218 isl_space_free(right);
1219 return NULL;
1222 /* Given two spaces { A -> C } and { B -> C }, construct the space
1223 * { [A -> B] -> C }
1225 __isl_give isl_space *isl_space_domain_product(__isl_take isl_space *left,
1226 __isl_take isl_space *right)
1228 isl_space *ran, *dom1, *dom2, *nest;
1230 if (!left || !right)
1231 goto error;
1233 if (!match(left, isl_dim_param, right, isl_dim_param))
1234 isl_die(left->ctx, isl_error_invalid,
1235 "parameters need to match", goto error);
1236 if (!isl_space_tuple_is_equal(left, isl_dim_out, right, isl_dim_out))
1237 isl_die(left->ctx, isl_error_invalid,
1238 "ranges need to match", goto error);
1240 ran = isl_space_range(isl_space_copy(left));
1242 dom1 = isl_space_domain(left);
1243 dom2 = isl_space_domain(right);
1244 nest = isl_space_wrap(isl_space_join(isl_space_reverse(dom1), dom2));
1246 return isl_space_join(isl_space_reverse(nest), ran);
1247 error:
1248 isl_space_free(left);
1249 isl_space_free(right);
1250 return NULL;
1253 __isl_give isl_space *isl_space_range_product(__isl_take isl_space *left,
1254 __isl_take isl_space *right)
1256 isl_space *dom, *ran1, *ran2, *nest;
1258 if (!left || !right)
1259 goto error;
1261 isl_assert(left->ctx, match(left, isl_dim_param, right, isl_dim_param),
1262 goto error);
1263 if (!isl_space_tuple_is_equal(left, isl_dim_in, right, isl_dim_in))
1264 isl_die(left->ctx, isl_error_invalid,
1265 "domains need to match", goto error);
1267 dom = isl_space_domain(isl_space_copy(left));
1269 ran1 = isl_space_range(left);
1270 ran2 = isl_space_range(right);
1271 nest = isl_space_wrap(isl_space_join(isl_space_reverse(ran1), ran2));
1273 return isl_space_join(isl_space_reverse(dom), nest);
1274 error:
1275 isl_space_free(left);
1276 isl_space_free(right);
1277 return NULL;
1280 /* Given a space of the form [A -> B] -> [C -> D], return the space A -> C.
1282 __isl_give isl_space *isl_space_factor_domain(__isl_take isl_space *space)
1284 space = isl_space_domain_factor_domain(space);
1285 space = isl_space_range_factor_domain(space);
1286 return space;
1289 /* Given a space of the form [A -> B] -> C, return the space A -> C.
1291 __isl_give isl_space *isl_space_domain_factor_domain(
1292 __isl_take isl_space *space)
1294 isl_space *nested;
1295 isl_space *domain;
1297 if (!space)
1298 return NULL;
1299 if (!isl_space_domain_is_wrapping(space))
1300 isl_die(isl_space_get_ctx(space), isl_error_invalid,
1301 "domain not a product", return isl_space_free(space));
1303 nested = space->nested[0];
1304 domain = isl_space_copy(space);
1305 domain = isl_space_drop_dims(domain, isl_dim_in,
1306 nested->n_in, nested->n_out);
1307 if (!domain)
1308 return isl_space_free(space);
1309 if (nested->tuple_id[0]) {
1310 domain->tuple_id[0] = isl_id_copy(nested->tuple_id[0]);
1311 if (!domain->tuple_id[0])
1312 goto error;
1314 if (nested->nested[0]) {
1315 domain->nested[0] = isl_space_copy(nested->nested[0]);
1316 if (!domain->nested[0])
1317 goto error;
1320 isl_space_free(space);
1321 return domain;
1322 error:
1323 isl_space_free(space);
1324 isl_space_free(domain);
1325 return NULL;
1328 /* Given a space of the form [A -> B] -> C, return the space B -> C.
1330 __isl_give isl_space *isl_space_domain_factor_range(
1331 __isl_take isl_space *space)
1333 isl_space *nested;
1334 isl_space *range;
1336 if (!space)
1337 return NULL;
1338 if (!isl_space_domain_is_wrapping(space))
1339 isl_die(isl_space_get_ctx(space), isl_error_invalid,
1340 "domain not a product", return isl_space_free(space));
1342 nested = space->nested[0];
1343 range = isl_space_copy(space);
1344 range = isl_space_drop_dims(range, isl_dim_in, 0, nested->n_in);
1345 if (!range)
1346 return isl_space_free(space);
1347 if (nested->tuple_id[1]) {
1348 range->tuple_id[0] = isl_id_copy(nested->tuple_id[1]);
1349 if (!range->tuple_id[0])
1350 goto error;
1352 if (nested->nested[1]) {
1353 range->nested[0] = isl_space_copy(nested->nested[1]);
1354 if (!range->nested[0])
1355 goto error;
1358 isl_space_free(space);
1359 return range;
1360 error:
1361 isl_space_free(space);
1362 isl_space_free(range);
1363 return NULL;
1366 /* Given a space of the form A -> [B -> C], return the space A -> B.
1368 __isl_give isl_space *isl_space_range_factor_domain(
1369 __isl_take isl_space *space)
1371 isl_space *nested;
1372 isl_space *domain;
1374 if (!space)
1375 return NULL;
1376 if (!isl_space_range_is_wrapping(space))
1377 isl_die(isl_space_get_ctx(space), isl_error_invalid,
1378 "range not a product", return isl_space_free(space));
1380 nested = space->nested[1];
1381 domain = isl_space_copy(space);
1382 domain = isl_space_drop_dims(domain, isl_dim_out,
1383 nested->n_in, nested->n_out);
1384 if (!domain)
1385 return isl_space_free(space);
1386 if (nested->tuple_id[0]) {
1387 domain->tuple_id[1] = isl_id_copy(nested->tuple_id[0]);
1388 if (!domain->tuple_id[1])
1389 goto error;
1391 if (nested->nested[0]) {
1392 domain->nested[1] = isl_space_copy(nested->nested[0]);
1393 if (!domain->nested[1])
1394 goto error;
1397 isl_space_free(space);
1398 return domain;
1399 error:
1400 isl_space_free(space);
1401 isl_space_free(domain);
1402 return NULL;
1405 /* Internal function that selects the range of the map that is
1406 * embedded in either a set space or the range of a map space.
1407 * In particular, given a space of the form [A -> B], return the space B.
1408 * Given a space of the form A -> [B -> C], return the space A -> C.
1410 static __isl_give isl_space *range_factor_range(__isl_take isl_space *space)
1412 isl_space *nested;
1413 isl_space *range;
1415 if (!space)
1416 return NULL;
1418 nested = space->nested[1];
1419 range = isl_space_copy(space);
1420 range = isl_space_drop_dims(range, isl_dim_out, 0, nested->n_in);
1421 if (!range)
1422 return isl_space_free(space);
1423 if (nested->tuple_id[1]) {
1424 range->tuple_id[1] = isl_id_copy(nested->tuple_id[1]);
1425 if (!range->tuple_id[1])
1426 goto error;
1428 if (nested->nested[1]) {
1429 range->nested[1] = isl_space_copy(nested->nested[1]);
1430 if (!range->nested[1])
1431 goto error;
1434 isl_space_free(space);
1435 return range;
1436 error:
1437 isl_space_free(space);
1438 isl_space_free(range);
1439 return NULL;
1442 /* Given a space of the form A -> [B -> C], return the space A -> C.
1444 __isl_give isl_space *isl_space_range_factor_range(
1445 __isl_take isl_space *space)
1447 if (!space)
1448 return NULL;
1449 if (!isl_space_range_is_wrapping(space))
1450 isl_die(isl_space_get_ctx(space), isl_error_invalid,
1451 "range not a product", return isl_space_free(space));
1453 return range_factor_range(space);
1456 /* Given a space of the form [A -> B], return the space B.
1458 static __isl_give isl_space *set_factor_range(__isl_take isl_space *space)
1460 if (!space)
1461 return NULL;
1462 if (!isl_space_is_wrapping(space))
1463 isl_die(isl_space_get_ctx(space), isl_error_invalid,
1464 "not a product", return isl_space_free(space));
1466 return range_factor_range(space);
1469 /* Given a space of the form [A -> B] -> [C -> D], return the space B -> D.
1470 * Given a space of the form [A -> B], return the space B.
1472 __isl_give isl_space *isl_space_factor_range(__isl_take isl_space *space)
1474 if (!space)
1475 return NULL;
1476 if (isl_space_is_set(space))
1477 return set_factor_range(space);
1478 space = isl_space_domain_factor_range(space);
1479 space = isl_space_range_factor_range(space);
1480 return space;
1483 __isl_give isl_space *isl_space_map_from_set(__isl_take isl_space *space)
1485 isl_ctx *ctx;
1486 isl_id **ids = NULL;
1488 if (!space)
1489 return NULL;
1490 ctx = isl_space_get_ctx(space);
1491 if (!isl_space_is_set(space))
1492 isl_die(ctx, isl_error_invalid, "not a set space", goto error);
1493 space = isl_space_cow(space);
1494 if (!space)
1495 return NULL;
1496 if (space->ids) {
1497 ids = isl_calloc_array(space->ctx, isl_id *,
1498 space->nparam + space->n_out + space->n_out);
1499 if (!ids)
1500 goto error;
1501 get_ids(space, isl_dim_param, 0, space->nparam, ids);
1502 get_ids(space, isl_dim_out, 0, space->n_out,
1503 ids + space->nparam);
1505 space->n_in = space->n_out;
1506 if (ids) {
1507 free(space->ids);
1508 space->ids = ids;
1509 space->n_id = space->nparam + space->n_out + space->n_out;
1510 space = copy_ids(space, isl_dim_out, 0, space, isl_dim_in);
1512 isl_id_free(space->tuple_id[0]);
1513 space->tuple_id[0] = isl_id_copy(space->tuple_id[1]);
1514 isl_space_free(space->nested[0]);
1515 space->nested[0] = isl_space_copy(space->nested[1]);
1516 return space;
1517 error:
1518 isl_space_free(space);
1519 return NULL;
1522 __isl_give isl_space *isl_space_map_from_domain_and_range(
1523 __isl_take isl_space *domain, __isl_take isl_space *range)
1525 if (!domain || !range)
1526 goto error;
1527 if (!isl_space_is_set(domain))
1528 isl_die(isl_space_get_ctx(domain), isl_error_invalid,
1529 "domain is not a set space", goto error);
1530 if (!isl_space_is_set(range))
1531 isl_die(isl_space_get_ctx(range), isl_error_invalid,
1532 "range is not a set space", goto error);
1533 return isl_space_join(isl_space_reverse(domain), range);
1534 error:
1535 isl_space_free(domain);
1536 isl_space_free(range);
1537 return NULL;
1540 static __isl_give isl_space *set_ids(__isl_take isl_space *dim,
1541 enum isl_dim_type type,
1542 unsigned first, unsigned n, __isl_take isl_id **ids)
1544 int i;
1546 for (i = 0; i < n ; ++i)
1547 dim = set_id(dim, type, first + i, ids[i]);
1549 return dim;
1552 __isl_give isl_space *isl_space_reverse(__isl_take isl_space *dim)
1554 unsigned t;
1555 isl_space *nested;
1556 isl_id **ids = NULL;
1557 isl_id *id;
1559 if (!dim)
1560 return NULL;
1561 if (match(dim, isl_dim_in, dim, isl_dim_out))
1562 return dim;
1564 dim = isl_space_cow(dim);
1565 if (!dim)
1566 return NULL;
1568 id = dim->tuple_id[0];
1569 dim->tuple_id[0] = dim->tuple_id[1];
1570 dim->tuple_id[1] = id;
1572 nested = dim->nested[0];
1573 dim->nested[0] = dim->nested[1];
1574 dim->nested[1] = nested;
1576 if (dim->ids) {
1577 int n_id = dim->n_in + dim->n_out;
1578 ids = isl_alloc_array(dim->ctx, isl_id *, n_id);
1579 if (n_id && !ids)
1580 goto error;
1581 get_ids(dim, isl_dim_in, 0, dim->n_in, ids);
1582 get_ids(dim, isl_dim_out, 0, dim->n_out, ids + dim->n_in);
1585 t = dim->n_in;
1586 dim->n_in = dim->n_out;
1587 dim->n_out = t;
1589 if (dim->ids) {
1590 dim = set_ids(dim, isl_dim_out, 0, dim->n_out, ids);
1591 dim = set_ids(dim, isl_dim_in, 0, dim->n_in, ids + dim->n_out);
1592 free(ids);
1595 return dim;
1596 error:
1597 free(ids);
1598 isl_space_free(dim);
1599 return NULL;
1602 __isl_give isl_space *isl_space_drop_dims(__isl_take isl_space *dim,
1603 enum isl_dim_type type, unsigned first, unsigned num)
1605 int i;
1607 if (!dim)
1608 return NULL;
1610 if (num == 0)
1611 return isl_space_reset(dim, type);
1613 if (!valid_dim_type(type))
1614 isl_die(dim->ctx, isl_error_invalid,
1615 "cannot drop dimensions of specified type", goto error);
1617 if (first + num > n(dim, type) || first + num < first)
1618 isl_die(isl_space_get_ctx(dim), isl_error_invalid,
1619 "index out of bounds", return isl_space_free(dim));
1620 dim = isl_space_cow(dim);
1621 if (!dim)
1622 goto error;
1623 if (dim->ids) {
1624 dim = extend_ids(dim);
1625 if (!dim)
1626 goto error;
1627 for (i = 0; i < num; ++i)
1628 isl_id_free(get_id(dim, type, first + i));
1629 for (i = first+num; i < n(dim, type); ++i)
1630 set_id(dim, type, i - num, get_id(dim, type, i));
1631 switch (type) {
1632 case isl_dim_param:
1633 get_ids(dim, isl_dim_in, 0, dim->n_in,
1634 dim->ids + offset(dim, isl_dim_in) - num);
1635 case isl_dim_in:
1636 get_ids(dim, isl_dim_out, 0, dim->n_out,
1637 dim->ids + offset(dim, isl_dim_out) - num);
1638 default:
1641 dim->n_id -= num;
1643 switch (type) {
1644 case isl_dim_param: dim->nparam -= num; break;
1645 case isl_dim_in: dim->n_in -= num; break;
1646 case isl_dim_out: dim->n_out -= num; break;
1647 default: ;
1649 dim = isl_space_reset(dim, type);
1650 if (type == isl_dim_param) {
1651 if (dim && dim->nested[0] &&
1652 !(dim->nested[0] = isl_space_drop_dims(dim->nested[0],
1653 isl_dim_param, first, num)))
1654 goto error;
1655 if (dim && dim->nested[1] &&
1656 !(dim->nested[1] = isl_space_drop_dims(dim->nested[1],
1657 isl_dim_param, first, num)))
1658 goto error;
1660 return dim;
1661 error:
1662 isl_space_free(dim);
1663 return NULL;
1666 __isl_give isl_space *isl_space_drop_inputs(__isl_take isl_space *dim,
1667 unsigned first, unsigned n)
1669 if (!dim)
1670 return NULL;
1671 return isl_space_drop_dims(dim, isl_dim_in, first, n);
1674 __isl_give isl_space *isl_space_drop_outputs(__isl_take isl_space *dim,
1675 unsigned first, unsigned n)
1677 if (!dim)
1678 return NULL;
1679 return isl_space_drop_dims(dim, isl_dim_out, first, n);
1682 __isl_give isl_space *isl_space_domain(__isl_take isl_space *dim)
1684 if (!dim)
1685 return NULL;
1686 dim = isl_space_drop_outputs(dim, 0, dim->n_out);
1687 dim = isl_space_reverse(dim);
1688 dim = mark_as_set(dim);
1689 return dim;
1692 __isl_give isl_space *isl_space_from_domain(__isl_take isl_space *dim)
1694 if (!dim)
1695 return NULL;
1696 if (!isl_space_is_set(dim))
1697 isl_die(isl_space_get_ctx(dim), isl_error_invalid,
1698 "not a set space", goto error);
1699 dim = isl_space_reverse(dim);
1700 dim = isl_space_reset(dim, isl_dim_out);
1701 return dim;
1702 error:
1703 isl_space_free(dim);
1704 return NULL;
1707 __isl_give isl_space *isl_space_range(__isl_take isl_space *dim)
1709 if (!dim)
1710 return NULL;
1711 dim = isl_space_drop_inputs(dim, 0, dim->n_in);
1712 dim = mark_as_set(dim);
1713 return dim;
1716 __isl_give isl_space *isl_space_from_range(__isl_take isl_space *dim)
1718 if (!dim)
1719 return NULL;
1720 if (!isl_space_is_set(dim))
1721 isl_die(isl_space_get_ctx(dim), isl_error_invalid,
1722 "not a set space", goto error);
1723 return isl_space_reset(dim, isl_dim_in);
1724 error:
1725 isl_space_free(dim);
1726 return NULL;
1729 /* Given a map space A -> B, return the map space [A -> B] -> A.
1731 __isl_give isl_space *isl_space_domain_map(__isl_take isl_space *space)
1733 isl_space *domain;
1735 domain = isl_space_from_range(isl_space_domain(isl_space_copy(space)));
1736 space = isl_space_from_domain(isl_space_wrap(space));
1737 space = isl_space_join(space, domain);
1739 return space;
1742 /* Given a map space A -> B, return the map space [A -> B] -> B.
1744 __isl_give isl_space *isl_space_range_map(__isl_take isl_space *space)
1746 isl_space *range;
1748 range = isl_space_from_range(isl_space_range(isl_space_copy(space)));
1749 space = isl_space_from_domain(isl_space_wrap(space));
1750 space = isl_space_join(space, range);
1752 return space;
1755 __isl_give isl_space *isl_space_params(__isl_take isl_space *space)
1757 if (isl_space_is_params(space))
1758 return space;
1759 space = isl_space_drop_dims(space,
1760 isl_dim_in, 0, isl_space_dim(space, isl_dim_in));
1761 space = isl_space_drop_dims(space,
1762 isl_dim_out, 0, isl_space_dim(space, isl_dim_out));
1763 space = mark_as_params(space);
1764 return space;
1767 __isl_give isl_space *isl_space_set_from_params(__isl_take isl_space *space)
1769 if (!space)
1770 return NULL;
1771 if (!isl_space_is_params(space))
1772 isl_die(isl_space_get_ctx(space), isl_error_invalid,
1773 "not a parameter space", goto error);
1774 return isl_space_reset(space, isl_dim_set);
1775 error:
1776 isl_space_free(space);
1777 return NULL;
1780 __isl_give isl_space *isl_space_as_set_space(__isl_take isl_space *dim)
1782 dim = isl_space_cow(dim);
1783 if (!dim)
1784 return NULL;
1786 dim->n_out += dim->n_in;
1787 dim->n_in = 0;
1788 dim = isl_space_reset(dim, isl_dim_in);
1789 dim = isl_space_reset(dim, isl_dim_out);
1791 return dim;
1794 __isl_give isl_space *isl_space_underlying(__isl_take isl_space *dim,
1795 unsigned n_div)
1797 int i;
1799 if (!dim)
1800 return NULL;
1801 if (n_div == 0 &&
1802 dim->nparam == 0 && dim->n_in == 0 && dim->n_id == 0)
1803 return isl_space_reset(isl_space_reset(dim, isl_dim_in), isl_dim_out);
1804 dim = isl_space_cow(dim);
1805 if (!dim)
1806 return NULL;
1807 dim->n_out += dim->nparam + dim->n_in + n_div;
1808 dim->nparam = 0;
1809 dim->n_in = 0;
1811 for (i = 0; i < dim->n_id; ++i)
1812 isl_id_free(get_id(dim, isl_dim_out, i));
1813 dim->n_id = 0;
1814 dim = isl_space_reset(dim, isl_dim_in);
1815 dim = isl_space_reset(dim, isl_dim_out);
1817 return dim;
1820 /* Are the two spaces the same, including positions and names of parameters?
1822 isl_bool isl_space_is_equal(__isl_keep isl_space *dim1,
1823 __isl_keep isl_space *dim2)
1825 if (!dim1 || !dim2)
1826 return isl_bool_error;
1827 if (dim1 == dim2)
1828 return isl_bool_true;
1829 return match(dim1, isl_dim_param, dim2, isl_dim_param) &&
1830 isl_space_tuple_is_equal(dim1, isl_dim_in, dim2, isl_dim_in) &&
1831 isl_space_tuple_is_equal(dim1, isl_dim_out, dim2, isl_dim_out);
1834 /* Is space1 equal to the domain of space2?
1836 * In the internal version we also allow space2 to be the space of a set,
1837 * provided space1 is a parameter space.
1839 isl_bool isl_space_is_domain_internal(__isl_keep isl_space *space1,
1840 __isl_keep isl_space *space2)
1842 if (!space1 || !space2)
1843 return isl_bool_error;
1844 if (!isl_space_is_set(space1))
1845 return isl_bool_false;
1846 return match(space1, isl_dim_param, space2, isl_dim_param) &&
1847 isl_space_tuple_is_equal(space1, isl_dim_set,
1848 space2, isl_dim_in);
1851 /* Is space1 equal to the domain of space2?
1853 isl_bool isl_space_is_domain(__isl_keep isl_space *space1,
1854 __isl_keep isl_space *space2)
1856 if (!space2)
1857 return isl_bool_error;
1858 if (!isl_space_is_map(space2))
1859 return isl_bool_false;
1860 return isl_space_is_domain_internal(space1, space2);
1863 /* Is space1 equal to the range of space2?
1865 * In the internal version, space2 is allowed to be the space of a set,
1866 * in which case it should be equal to space1.
1868 isl_bool isl_space_is_range_internal(__isl_keep isl_space *space1,
1869 __isl_keep isl_space *space2)
1871 if (!space1 || !space2)
1872 return isl_bool_error;
1873 if (!isl_space_is_set(space1))
1874 return isl_bool_false;
1875 return match(space1, isl_dim_param, space2, isl_dim_param) &&
1876 isl_space_tuple_is_equal(space1, isl_dim_set,
1877 space2, isl_dim_out);
1880 /* Is space1 equal to the range of space2?
1882 isl_bool isl_space_is_range(__isl_keep isl_space *space1,
1883 __isl_keep isl_space *space2)
1885 if (!space2)
1886 return isl_bool_error;
1887 if (!isl_space_is_map(space2))
1888 return isl_bool_false;
1889 return isl_space_is_range_internal(space1, space2);
1892 int isl_space_compatible_internal(__isl_keep isl_space *dim1,
1893 __isl_keep isl_space *dim2)
1895 return dim1->nparam == dim2->nparam &&
1896 dim1->n_in + dim1->n_out == dim2->n_in + dim2->n_out;
1899 int isl_space_compatible(__isl_keep isl_space *space1,
1900 __isl_keep isl_space *space2)
1902 return isl_space_compatible_internal(space1, space2);
1905 /* Update "hash" by hashing in "space".
1906 * Changes in this function should be reflected in isl_hash_space_domain.
1908 static uint32_t isl_hash_space(uint32_t hash, __isl_keep isl_space *space)
1910 int i;
1911 isl_id *id;
1913 if (!space)
1914 return hash;
1916 isl_hash_byte(hash, space->nparam % 256);
1917 isl_hash_byte(hash, space->n_in % 256);
1918 isl_hash_byte(hash, space->n_out % 256);
1920 for (i = 0; i < space->nparam; ++i) {
1921 id = get_id(space, isl_dim_param, i);
1922 hash = isl_hash_id(hash, id);
1925 id = tuple_id(space, isl_dim_in);
1926 hash = isl_hash_id(hash, id);
1927 id = tuple_id(space, isl_dim_out);
1928 hash = isl_hash_id(hash, id);
1930 hash = isl_hash_space(hash, space->nested[0]);
1931 hash = isl_hash_space(hash, space->nested[1]);
1933 return hash;
1936 /* Update "hash" by hashing in the domain of "space".
1937 * The result of this function is equal to the result of applying
1938 * isl_hash_space to the domain of "space".
1940 static uint32_t isl_hash_space_domain(uint32_t hash,
1941 __isl_keep isl_space *space)
1943 int i;
1944 isl_id *id;
1946 if (!space)
1947 return hash;
1949 isl_hash_byte(hash, space->nparam % 256);
1950 isl_hash_byte(hash, 0);
1951 isl_hash_byte(hash, space->n_in % 256);
1953 for (i = 0; i < space->nparam; ++i) {
1954 id = get_id(space, isl_dim_param, i);
1955 hash = isl_hash_id(hash, id);
1958 hash = isl_hash_id(hash, &isl_id_none);
1959 id = tuple_id(space, isl_dim_in);
1960 hash = isl_hash_id(hash, id);
1962 hash = isl_hash_space(hash, space->nested[0]);
1964 return hash;
1967 uint32_t isl_space_get_hash(__isl_keep isl_space *dim)
1969 uint32_t hash;
1971 if (!dim)
1972 return 0;
1974 hash = isl_hash_init();
1975 hash = isl_hash_space(hash, dim);
1977 return hash;
1980 /* Return the hash value of the domain of "space".
1981 * That is, isl_space_get_domain_hash(space) is equal to
1982 * isl_space_get_hash(isl_space_domain(space)).
1984 uint32_t isl_space_get_domain_hash(__isl_keep isl_space *space)
1986 uint32_t hash;
1988 if (!space)
1989 return 0;
1991 hash = isl_hash_init();
1992 hash = isl_hash_space_domain(hash, space);
1994 return hash;
1997 isl_bool isl_space_is_wrapping(__isl_keep isl_space *dim)
1999 if (!dim)
2000 return isl_bool_error;
2002 if (!isl_space_is_set(dim))
2003 return isl_bool_false;
2005 return dim->nested[1] != NULL;
2008 /* Is "space" the space of a map where the domain is a wrapped map space?
2010 isl_bool isl_space_domain_is_wrapping(__isl_keep isl_space *space)
2012 if (!space)
2013 return isl_bool_error;
2015 if (isl_space_is_set(space))
2016 return isl_bool_false;
2018 return space->nested[0] != NULL;
2021 /* Is "space" the space of a map where the range is a wrapped map space?
2023 isl_bool isl_space_range_is_wrapping(__isl_keep isl_space *space)
2025 if (!space)
2026 return isl_bool_error;
2028 if (isl_space_is_set(space))
2029 return isl_bool_false;
2031 return space->nested[1] != NULL;
2034 __isl_give isl_space *isl_space_wrap(__isl_take isl_space *dim)
2036 isl_space *wrap;
2038 if (!dim)
2039 return NULL;
2041 wrap = isl_space_set_alloc(dim->ctx,
2042 dim->nparam, dim->n_in + dim->n_out);
2044 wrap = copy_ids(wrap, isl_dim_param, 0, dim, isl_dim_param);
2045 wrap = copy_ids(wrap, isl_dim_set, 0, dim, isl_dim_in);
2046 wrap = copy_ids(wrap, isl_dim_set, dim->n_in, dim, isl_dim_out);
2048 if (!wrap)
2049 goto error;
2051 wrap->nested[1] = dim;
2053 return wrap;
2054 error:
2055 isl_space_free(dim);
2056 return NULL;
2059 __isl_give isl_space *isl_space_unwrap(__isl_take isl_space *dim)
2061 isl_space *unwrap;
2063 if (!dim)
2064 return NULL;
2066 if (!isl_space_is_wrapping(dim))
2067 isl_die(dim->ctx, isl_error_invalid, "not a wrapping space",
2068 goto error);
2070 unwrap = isl_space_copy(dim->nested[1]);
2071 isl_space_free(dim);
2073 return unwrap;
2074 error:
2075 isl_space_free(dim);
2076 return NULL;
2079 int isl_space_is_named_or_nested(__isl_keep isl_space *dim, enum isl_dim_type type)
2081 if (type != isl_dim_in && type != isl_dim_out)
2082 return 0;
2083 if (!dim)
2084 return -1;
2085 if (dim->tuple_id[type - isl_dim_in])
2086 return 1;
2087 if (dim->nested[type - isl_dim_in])
2088 return 1;
2089 return 0;
2092 int isl_space_may_be_set(__isl_keep isl_space *dim)
2094 if (!dim)
2095 return -1;
2096 if (isl_space_is_set(dim))
2097 return 1;
2098 if (isl_space_dim(dim, isl_dim_in) != 0)
2099 return 0;
2100 if (isl_space_is_named_or_nested(dim, isl_dim_in))
2101 return 0;
2102 return 1;
2105 __isl_give isl_space *isl_space_reset(__isl_take isl_space *dim,
2106 enum isl_dim_type type)
2108 if (!isl_space_is_named_or_nested(dim, type))
2109 return dim;
2111 dim = isl_space_cow(dim);
2112 if (!dim)
2113 return NULL;
2115 isl_id_free(dim->tuple_id[type - isl_dim_in]);
2116 dim->tuple_id[type - isl_dim_in] = NULL;
2117 isl_space_free(dim->nested[type - isl_dim_in]);
2118 dim->nested[type - isl_dim_in] = NULL;
2120 return dim;
2123 __isl_give isl_space *isl_space_flatten(__isl_take isl_space *dim)
2125 if (!dim)
2126 return NULL;
2127 if (!dim->nested[0] && !dim->nested[1])
2128 return dim;
2130 if (dim->nested[0])
2131 dim = isl_space_reset(dim, isl_dim_in);
2132 if (dim && dim->nested[1])
2133 dim = isl_space_reset(dim, isl_dim_out);
2135 return dim;
2138 __isl_give isl_space *isl_space_flatten_domain(__isl_take isl_space *dim)
2140 if (!dim)
2141 return NULL;
2142 if (!dim->nested[0])
2143 return dim;
2145 return isl_space_reset(dim, isl_dim_in);
2148 __isl_give isl_space *isl_space_flatten_range(__isl_take isl_space *dim)
2150 if (!dim)
2151 return NULL;
2152 if (!dim->nested[1])
2153 return dim;
2155 return isl_space_reset(dim, isl_dim_out);
2158 /* Replace the dimensions of the given type of dst by those of src.
2160 __isl_give isl_space *isl_space_replace(__isl_take isl_space *dst,
2161 enum isl_dim_type type, __isl_keep isl_space *src)
2163 dst = isl_space_cow(dst);
2165 if (!dst || !src)
2166 goto error;
2168 dst = isl_space_drop_dims(dst, type, 0, isl_space_dim(dst, type));
2169 dst = isl_space_add_dims(dst, type, isl_space_dim(src, type));
2170 dst = copy_ids(dst, type, 0, src, type);
2172 if (dst && type == isl_dim_param) {
2173 int i;
2174 for (i = 0; i <= 1; ++i) {
2175 if (!dst->nested[i])
2176 continue;
2177 dst->nested[i] = isl_space_replace(dst->nested[i],
2178 type, src);
2179 if (!dst->nested[i])
2180 goto error;
2184 return dst;
2185 error:
2186 isl_space_free(dst);
2187 return NULL;
2190 /* Given a dimension specification "dim" of a set, create a dimension
2191 * specification for the lift of the set. In particular, the result
2192 * is of the form [dim -> local[..]], with n_local variables in the
2193 * range of the wrapped map.
2195 __isl_give isl_space *isl_space_lift(__isl_take isl_space *dim, unsigned n_local)
2197 isl_space *local_dim;
2199 if (!dim)
2200 return NULL;
2202 local_dim = isl_space_dup(dim);
2203 local_dim = isl_space_drop_dims(local_dim, isl_dim_set, 0, dim->n_out);
2204 local_dim = isl_space_add_dims(local_dim, isl_dim_set, n_local);
2205 local_dim = isl_space_set_tuple_name(local_dim, isl_dim_set, "local");
2206 dim = isl_space_join(isl_space_from_domain(dim),
2207 isl_space_from_range(local_dim));
2208 dim = isl_space_wrap(dim);
2209 dim = isl_space_set_tuple_name(dim, isl_dim_set, "lifted");
2211 return dim;
2214 isl_bool isl_space_can_zip(__isl_keep isl_space *dim)
2216 if (!dim)
2217 return isl_bool_error;
2219 return dim->nested[0] && dim->nested[1];
2222 __isl_give isl_space *isl_space_zip(__isl_take isl_space *dim)
2224 isl_space *dom, *ran;
2225 isl_space *dom_dom, *dom_ran, *ran_dom, *ran_ran;
2227 if (!isl_space_can_zip(dim))
2228 isl_die(dim->ctx, isl_error_invalid, "dim cannot be zipped",
2229 goto error);
2231 if (!dim)
2232 return NULL;
2233 dom = isl_space_unwrap(isl_space_domain(isl_space_copy(dim)));
2234 ran = isl_space_unwrap(isl_space_range(dim));
2235 dom_dom = isl_space_domain(isl_space_copy(dom));
2236 dom_ran = isl_space_range(dom);
2237 ran_dom = isl_space_domain(isl_space_copy(ran));
2238 ran_ran = isl_space_range(ran);
2239 dom = isl_space_join(isl_space_from_domain(dom_dom),
2240 isl_space_from_range(ran_dom));
2241 ran = isl_space_join(isl_space_from_domain(dom_ran),
2242 isl_space_from_range(ran_ran));
2243 return isl_space_join(isl_space_from_domain(isl_space_wrap(dom)),
2244 isl_space_from_range(isl_space_wrap(ran)));
2245 error:
2246 isl_space_free(dim);
2247 return NULL;
2250 /* Can we apply isl_space_curry to "space"?
2251 * That is, does it have a nested relation in its domain?
2253 isl_bool isl_space_can_curry(__isl_keep isl_space *space)
2255 if (!space)
2256 return isl_bool_error;
2258 return !!space->nested[0];
2261 /* Given a space (A -> B) -> C, return the corresponding space
2262 * A -> (B -> C).
2264 __isl_give isl_space *isl_space_curry(__isl_take isl_space *space)
2266 isl_space *dom, *ran;
2267 isl_space *dom_dom, *dom_ran;
2269 if (!space)
2270 return NULL;
2272 if (!isl_space_can_curry(space))
2273 isl_die(space->ctx, isl_error_invalid,
2274 "space cannot be curried", goto error);
2276 dom = isl_space_unwrap(isl_space_domain(isl_space_copy(space)));
2277 ran = isl_space_range(space);
2278 dom_dom = isl_space_domain(isl_space_copy(dom));
2279 dom_ran = isl_space_range(dom);
2280 ran = isl_space_join(isl_space_from_domain(dom_ran),
2281 isl_space_from_range(ran));
2282 return isl_space_join(isl_space_from_domain(dom_dom),
2283 isl_space_from_range(isl_space_wrap(ran)));
2284 error:
2285 isl_space_free(space);
2286 return NULL;
2289 /* Can isl_space_range_curry be applied to "space"?
2290 * That is, does it have a nested relation in its range,
2291 * the domain of which is itself a nested relation?
2293 isl_bool isl_space_can_range_curry(__isl_keep isl_space *space)
2295 isl_bool can;
2297 if (!space)
2298 return isl_bool_error;
2299 can = isl_space_range_is_wrapping(space);
2300 if (can < 0 || !can)
2301 return can;
2302 return isl_space_can_curry(space->nested[1]);
2305 /* Given a space A -> ((B -> C) -> D), return the corresponding space
2306 * A -> (B -> (C -> D)).
2308 __isl_give isl_space *isl_space_range_curry(__isl_take isl_space *space)
2310 if (!space)
2311 return NULL;
2313 if (!isl_space_can_range_curry(space))
2314 isl_die(isl_space_get_ctx(space), isl_error_invalid,
2315 "space range cannot be curried",
2316 return isl_space_free(space));
2318 space = isl_space_cow(space);
2319 if (!space)
2320 return NULL;
2321 space->nested[1] = isl_space_curry(space->nested[1]);
2322 if (!space->nested[1])
2323 return isl_space_free(space);
2325 return space;
2328 /* Can we apply isl_space_uncurry to "space"?
2329 * That is, does it have a nested relation in its range?
2331 isl_bool isl_space_can_uncurry(__isl_keep isl_space *space)
2333 if (!space)
2334 return isl_bool_error;
2336 return !!space->nested[1];
2339 /* Given a space A -> (B -> C), return the corresponding space
2340 * (A -> B) -> C.
2342 __isl_give isl_space *isl_space_uncurry(__isl_take isl_space *space)
2344 isl_space *dom, *ran;
2345 isl_space *ran_dom, *ran_ran;
2347 if (!space)
2348 return NULL;
2350 if (!isl_space_can_uncurry(space))
2351 isl_die(space->ctx, isl_error_invalid,
2352 "space cannot be uncurried",
2353 return isl_space_free(space));
2355 dom = isl_space_domain(isl_space_copy(space));
2356 ran = isl_space_unwrap(isl_space_range(space));
2357 ran_dom = isl_space_domain(isl_space_copy(ran));
2358 ran_ran = isl_space_range(ran);
2359 dom = isl_space_join(isl_space_from_domain(dom),
2360 isl_space_from_range(ran_dom));
2361 return isl_space_join(isl_space_from_domain(isl_space_wrap(dom)),
2362 isl_space_from_range(ran_ran));
2365 int isl_space_has_named_params(__isl_keep isl_space *dim)
2367 int i;
2368 unsigned off;
2370 if (!dim)
2371 return -1;
2372 if (dim->nparam == 0)
2373 return 1;
2374 off = isl_space_offset(dim, isl_dim_param);
2375 if (off + dim->nparam > dim->n_id)
2376 return 0;
2377 for (i = 0; i < dim->nparam; ++i)
2378 if (!dim->ids[off + i])
2379 return 0;
2380 return 1;
2383 /* Align the initial parameters of dim1 to match the order in dim2.
2385 __isl_give isl_space *isl_space_align_params(__isl_take isl_space *dim1,
2386 __isl_take isl_space *dim2)
2388 isl_reordering *exp;
2390 if (!isl_space_has_named_params(dim1) || !isl_space_has_named_params(dim2))
2391 isl_die(isl_space_get_ctx(dim1), isl_error_invalid,
2392 "parameter alignment requires named parameters",
2393 goto error);
2395 dim2 = isl_space_params(dim2);
2396 exp = isl_parameter_alignment_reordering(dim1, dim2);
2397 exp = isl_reordering_extend_space(exp, dim1);
2398 isl_space_free(dim2);
2399 if (!exp)
2400 return NULL;
2401 dim1 = isl_space_copy(exp->dim);
2402 isl_reordering_free(exp);
2403 return dim1;
2404 error:
2405 isl_space_free(dim1);
2406 isl_space_free(dim2);
2407 return NULL;
2410 /* Given the space of set (domain), construct a space for a map
2411 * with as domain the given space and as range the range of "model".
2413 __isl_give isl_space *isl_space_extend_domain_with_range(
2414 __isl_take isl_space *space, __isl_take isl_space *model)
2416 if (!model)
2417 goto error;
2419 space = isl_space_from_domain(space);
2420 space = isl_space_add_dims(space, isl_dim_out,
2421 isl_space_dim(model, isl_dim_out));
2422 if (isl_space_has_tuple_id(model, isl_dim_out))
2423 space = isl_space_set_tuple_id(space, isl_dim_out,
2424 isl_space_get_tuple_id(model, isl_dim_out));
2425 if (!space)
2426 goto error;
2427 if (model->nested[1]) {
2428 isl_space *nested = isl_space_copy(model->nested[1]);
2429 int n_nested, n_space;
2430 nested = isl_space_align_params(nested, isl_space_copy(space));
2431 n_nested = isl_space_dim(nested, isl_dim_param);
2432 n_space = isl_space_dim(space, isl_dim_param);
2433 if (n_nested > n_space)
2434 nested = isl_space_drop_dims(nested, isl_dim_param,
2435 n_space, n_nested - n_space);
2436 if (!nested)
2437 goto error;
2438 space->nested[1] = nested;
2440 isl_space_free(model);
2441 return space;
2442 error:
2443 isl_space_free(model);
2444 isl_space_free(space);
2445 return NULL;
2448 /* Compare the "type" dimensions of two isl_spaces.
2450 * The order is fairly arbitrary.
2452 static int isl_space_cmp_type(__isl_keep isl_space *space1,
2453 __isl_keep isl_space *space2, enum isl_dim_type type)
2455 int cmp;
2456 isl_space *nested1, *nested2;
2458 if (isl_space_dim(space1, type) != isl_space_dim(space2, type))
2459 return isl_space_dim(space1, type) -
2460 isl_space_dim(space2, type);
2462 cmp = isl_id_cmp(tuple_id(space1, type), tuple_id(space2, type));
2463 if (cmp != 0)
2464 return cmp;
2466 nested1 = nested(space1, type);
2467 nested2 = nested(space2, type);
2468 if (!nested1 != !nested2)
2469 return !nested1 - !nested2;
2471 if (nested1)
2472 return isl_space_cmp(nested1, nested2);
2474 return 0;
2477 /* Compare two isl_spaces.
2479 * The order is fairly arbitrary.
2481 int isl_space_cmp(__isl_keep isl_space *space1, __isl_keep isl_space *space2)
2483 int i;
2484 int cmp;
2486 if (space1 == space2)
2487 return 0;
2488 if (!space1)
2489 return -1;
2490 if (!space2)
2491 return 1;
2493 cmp = isl_space_cmp_type(space1, space2, isl_dim_param);
2494 if (cmp != 0)
2495 return cmp;
2496 cmp = isl_space_cmp_type(space1, space2, isl_dim_in);
2497 if (cmp != 0)
2498 return cmp;
2499 cmp = isl_space_cmp_type(space1, space2, isl_dim_out);
2500 if (cmp != 0)
2501 return cmp;
2503 if (!space1->ids && !space2->ids)
2504 return 0;
2506 for (i = 0; i < n(space1, isl_dim_param); ++i) {
2507 cmp = isl_id_cmp(get_id(space1, isl_dim_param, i),
2508 get_id(space2, isl_dim_param, i));
2509 if (cmp != 0)
2510 return cmp;
2513 return 0;