hide isl_map_add_basic_map
[isl.git] / isl_space.c
blob128eddfafeca17cd8928329b1ce10b3332f11d57
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 if (space->ids[offset + i]->name &&
677 !strcmp(space->ids[offset + i]->name, name))
678 return i;
680 return -1;
683 /* Reset the user pointer on all identifiers of parameters and tuples
684 * of "space".
686 __isl_give isl_space *isl_space_reset_user(__isl_take isl_space *space)
688 int i;
689 isl_ctx *ctx;
690 isl_id *id;
691 const char *name;
693 if (!space)
694 return NULL;
696 ctx = isl_space_get_ctx(space);
698 for (i = 0; i < space->nparam && i < space->n_id; ++i) {
699 if (!isl_id_get_user(space->ids[i]))
700 continue;
701 space = isl_space_cow(space);
702 if (!space)
703 return NULL;
704 name = isl_id_get_name(space->ids[i]);
705 id = isl_id_alloc(ctx, name, NULL);
706 isl_id_free(space->ids[i]);
707 space->ids[i] = id;
708 if (!id)
709 return isl_space_free(space);
712 for (i = 0; i < 2; ++i) {
713 if (!space->tuple_id[i])
714 continue;
715 if (!isl_id_get_user(space->tuple_id[i]))
716 continue;
717 space = isl_space_cow(space);
718 if (!space)
719 return NULL;
720 name = isl_id_get_name(space->tuple_id[i]);
721 id = isl_id_alloc(ctx, name, NULL);
722 isl_id_free(space->tuple_id[i]);
723 space->tuple_id[i] = id;
724 if (!id)
725 return isl_space_free(space);
728 for (i = 0; i < 2; ++i) {
729 if (!space->nested[i])
730 continue;
731 space = isl_space_cow(space);
732 if (!space)
733 return NULL;
734 space->nested[i] = isl_space_reset_user(space->nested[i]);
735 if (!space->nested[i])
736 return isl_space_free(space);
739 return space;
742 static __isl_keep isl_id *tuple_id(__isl_keep isl_space *dim,
743 enum isl_dim_type type)
745 if (!dim)
746 return NULL;
747 if (type == isl_dim_in)
748 return dim->tuple_id[0];
749 if (type == isl_dim_out)
750 return dim->tuple_id[1];
751 return NULL;
754 static __isl_keep isl_space *nested(__isl_keep isl_space *dim,
755 enum isl_dim_type type)
757 if (!dim)
758 return NULL;
759 if (type == isl_dim_in)
760 return dim->nested[0];
761 if (type == isl_dim_out)
762 return dim->nested[1];
763 return NULL;
766 /* Are the two spaces the same, apart from positions and names of parameters?
768 static int isl_space_has_equal_tuples(__isl_keep isl_space *space1,
769 __isl_keep isl_space *space2)
771 if (!space1 || !space2)
772 return -1;
773 if (space1 == space2)
774 return 1;
775 return isl_space_tuple_is_equal(space1, isl_dim_in,
776 space2, isl_dim_in) &&
777 isl_space_tuple_is_equal(space1, isl_dim_out,
778 space2, isl_dim_out);
781 /* Check if the tuple of type "type1" of "space1" is the same as
782 * the tuple of type "type2" of "space2".
784 * That is, check if the tuples have the same identifier, the same dimension
785 * and the same internal structure.
786 * The identifiers of the dimensions inside the tuples do not affect the result.
788 * Note that this function only checks the tuples themselves.
789 * If nested tuples are involved, then we need to be careful not
790 * to have result affected by possibly differing parameters
791 * in those nested tuples.
793 isl_bool isl_space_tuple_is_equal(__isl_keep isl_space *space1,
794 enum isl_dim_type type1, __isl_keep isl_space *space2,
795 enum isl_dim_type type2)
797 isl_id *id1, *id2;
798 isl_space *nested1, *nested2;
800 if (!space1 || !space2)
801 return isl_bool_error;
803 if (space1 == space2 && type1 == type2)
804 return isl_bool_true;
806 if (n(space1, type1) != n(space2, type2))
807 return isl_bool_false;
808 id1 = tuple_id(space1, type1);
809 id2 = tuple_id(space2, type2);
810 if (!id1 ^ !id2)
811 return isl_bool_false;
812 if (id1 && id1 != id2)
813 return isl_bool_false;
814 nested1 = nested(space1, type1);
815 nested2 = nested(space2, type2);
816 if (!nested1 ^ !nested2)
817 return isl_bool_false;
818 if (nested1 && !isl_space_has_equal_tuples(nested1, nested2))
819 return isl_bool_false;
820 return isl_bool_true;
823 /* This is the old, undocumented, name for isl_space_tuple_is_equal.
824 * It will be removed at some point.
826 int isl_space_tuple_match(__isl_keep isl_space *space1, enum isl_dim_type type1,
827 __isl_keep isl_space *space2, enum isl_dim_type type2)
829 return isl_space_tuple_is_equal(space1, type1, space2, type2);
832 static int match(__isl_keep isl_space *dim1, enum isl_dim_type dim1_type,
833 __isl_keep isl_space *dim2, enum isl_dim_type dim2_type)
835 int i;
837 if (dim1 == dim2 && dim1_type == dim2_type)
838 return 1;
840 if (!isl_space_tuple_is_equal(dim1, dim1_type, dim2, dim2_type))
841 return 0;
843 if (!dim1->ids && !dim2->ids)
844 return 1;
846 for (i = 0; i < n(dim1, dim1_type); ++i) {
847 if (get_id(dim1, dim1_type, i) != get_id(dim2, dim2_type, i))
848 return 0;
850 return 1;
853 int isl_space_match(__isl_keep isl_space *dim1, enum isl_dim_type dim1_type,
854 __isl_keep isl_space *dim2, enum isl_dim_type dim2_type)
856 if (!dim1 || !dim2)
857 return -1;
859 return match(dim1, dim1_type, dim2, dim2_type);
862 static void get_ids(__isl_keep isl_space *dim, enum isl_dim_type type,
863 unsigned first, unsigned n, __isl_keep isl_id **ids)
865 int i;
867 for (i = 0; i < n ; ++i)
868 ids[i] = get_id(dim, type, first + i);
871 __isl_give isl_space *isl_space_extend(__isl_take isl_space *space,
872 unsigned nparam, unsigned n_in, unsigned n_out)
874 isl_id **ids = NULL;
876 if (!space)
877 return NULL;
878 if (space->nparam == nparam &&
879 space->n_in == n_in && space->n_out == n_out)
880 return space;
882 isl_assert(space->ctx, space->nparam <= nparam, goto error);
883 isl_assert(space->ctx, space->n_in <= n_in, goto error);
884 isl_assert(space->ctx, space->n_out <= n_out, goto error);
886 space = isl_space_cow(space);
887 if (!space)
888 goto error;
890 if (space->ids) {
891 unsigned n;
892 n = nparam + n_in + n_out;
893 if (n < nparam || n < n_in || n < n_out)
894 isl_die(isl_space_get_ctx(space), isl_error_invalid,
895 "overflow in total number of dimensions",
896 goto error);
897 ids = isl_calloc_array(space->ctx, isl_id *, n);
898 if (!ids)
899 goto error;
900 get_ids(space, isl_dim_param, 0, space->nparam, ids);
901 get_ids(space, isl_dim_in, 0, space->n_in, ids + nparam);
902 get_ids(space, isl_dim_out, 0, space->n_out,
903 ids + nparam + n_in);
904 free(space->ids);
905 space->ids = ids;
906 space->n_id = nparam + n_in + n_out;
908 space->nparam = nparam;
909 space->n_in = n_in;
910 space->n_out = n_out;
912 return space;
913 error:
914 free(ids);
915 isl_space_free(space);
916 return NULL;
919 __isl_give isl_space *isl_space_add_dims(__isl_take isl_space *dim,
920 enum isl_dim_type type, unsigned n)
922 dim = isl_space_reset(dim, type);
923 if (!dim)
924 return NULL;
925 switch (type) {
926 case isl_dim_param:
927 dim = isl_space_extend(dim,
928 dim->nparam + n, dim->n_in, dim->n_out);
929 if (dim && dim->nested[0] &&
930 !(dim->nested[0] = isl_space_add_dims(dim->nested[0],
931 isl_dim_param, n)))
932 goto error;
933 if (dim && dim->nested[1] &&
934 !(dim->nested[1] = isl_space_add_dims(dim->nested[1],
935 isl_dim_param, n)))
936 goto error;
937 return dim;
938 case isl_dim_in:
939 return isl_space_extend(dim,
940 dim->nparam, dim->n_in + n, dim->n_out);
941 case isl_dim_out:
942 return isl_space_extend(dim,
943 dim->nparam, dim->n_in, dim->n_out + n);
944 default:
945 isl_die(dim->ctx, isl_error_invalid,
946 "cannot add dimensions of specified type", goto error);
948 error:
949 isl_space_free(dim);
950 return NULL;
953 static int valid_dim_type(enum isl_dim_type type)
955 switch (type) {
956 case isl_dim_param:
957 case isl_dim_in:
958 case isl_dim_out:
959 return 1;
960 default:
961 return 0;
965 /* Insert "n" dimensions of type "type" at position "pos".
966 * If we are inserting parameters, then they are also inserted in
967 * any nested spaces.
969 __isl_give isl_space *isl_space_insert_dims(__isl_take isl_space *dim,
970 enum isl_dim_type type, unsigned pos, unsigned n)
972 isl_id **ids = NULL;
974 if (!dim)
975 return NULL;
976 if (n == 0)
977 return isl_space_reset(dim, type);
979 if (!valid_dim_type(type))
980 isl_die(dim->ctx, isl_error_invalid,
981 "cannot insert dimensions of specified type",
982 goto error);
984 isl_assert(dim->ctx, pos <= isl_space_dim(dim, type), goto error);
986 dim = isl_space_cow(dim);
987 if (!dim)
988 return NULL;
990 if (dim->ids) {
991 enum isl_dim_type t, o = isl_dim_param;
992 int off;
993 int s[3];
994 ids = isl_calloc_array(dim->ctx, isl_id *,
995 dim->nparam + dim->n_in + dim->n_out + n);
996 if (!ids)
997 goto error;
998 off = 0;
999 s[isl_dim_param - o] = dim->nparam;
1000 s[isl_dim_in - o] = dim->n_in;
1001 s[isl_dim_out - o] = dim->n_out;
1002 for (t = isl_dim_param; t <= isl_dim_out; ++t) {
1003 if (t != type) {
1004 get_ids(dim, t, 0, s[t - o], ids + off);
1005 off += s[t - o];
1006 } else {
1007 get_ids(dim, t, 0, pos, ids + off);
1008 off += pos + n;
1009 get_ids(dim, t, pos, s[t - o] - pos, ids + off);
1010 off += s[t - o] - pos;
1013 free(dim->ids);
1014 dim->ids = ids;
1015 dim->n_id = dim->nparam + dim->n_in + dim->n_out + n;
1017 switch (type) {
1018 case isl_dim_param: dim->nparam += n; break;
1019 case isl_dim_in: dim->n_in += n; break;
1020 case isl_dim_out: dim->n_out += n; break;
1021 default: ;
1023 dim = isl_space_reset(dim, type);
1025 if (type == isl_dim_param) {
1026 if (dim && dim->nested[0] &&
1027 !(dim->nested[0] = isl_space_insert_dims(dim->nested[0],
1028 isl_dim_param, pos, n)))
1029 goto error;
1030 if (dim && dim->nested[1] &&
1031 !(dim->nested[1] = isl_space_insert_dims(dim->nested[1],
1032 isl_dim_param, pos, n)))
1033 goto error;
1036 return dim;
1037 error:
1038 isl_space_free(dim);
1039 return NULL;
1042 __isl_give isl_space *isl_space_move_dims(__isl_take isl_space *dim,
1043 enum isl_dim_type dst_type, unsigned dst_pos,
1044 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
1046 int i;
1048 if (!dim)
1049 return NULL;
1050 if (n == 0) {
1051 dim = isl_space_reset(dim, src_type);
1052 return isl_space_reset(dim, dst_type);
1055 isl_assert(dim->ctx, src_pos + n <= isl_space_dim(dim, src_type),
1056 goto error);
1058 if (dst_type == src_type && dst_pos == src_pos)
1059 return dim;
1061 isl_assert(dim->ctx, dst_type != src_type, goto error);
1063 dim = isl_space_reset(dim, src_type);
1064 dim = isl_space_reset(dim, dst_type);
1066 dim = isl_space_cow(dim);
1067 if (!dim)
1068 return NULL;
1070 if (dim->ids) {
1071 isl_id **ids;
1072 enum isl_dim_type t, o = isl_dim_param;
1073 int off;
1074 int s[3];
1075 ids = isl_calloc_array(dim->ctx, isl_id *,
1076 dim->nparam + dim->n_in + dim->n_out);
1077 if (!ids)
1078 goto error;
1079 off = 0;
1080 s[isl_dim_param - o] = dim->nparam;
1081 s[isl_dim_in - o] = dim->n_in;
1082 s[isl_dim_out - o] = dim->n_out;
1083 for (t = isl_dim_param; t <= isl_dim_out; ++t) {
1084 if (t == dst_type) {
1085 get_ids(dim, t, 0, dst_pos, ids + off);
1086 off += dst_pos;
1087 get_ids(dim, src_type, src_pos, n, ids + off);
1088 off += n;
1089 get_ids(dim, t, dst_pos, s[t - o] - dst_pos,
1090 ids + off);
1091 off += s[t - o] - dst_pos;
1092 } else if (t == src_type) {
1093 get_ids(dim, t, 0, src_pos, ids + off);
1094 off += src_pos;
1095 get_ids(dim, t, src_pos + n,
1096 s[t - o] - src_pos - n, ids + off);
1097 off += s[t - o] - src_pos - n;
1098 } else {
1099 get_ids(dim, t, 0, s[t - o], ids + off);
1100 off += s[t - o];
1103 free(dim->ids);
1104 dim->ids = ids;
1105 dim->n_id = dim->nparam + dim->n_in + dim->n_out;
1108 switch (dst_type) {
1109 case isl_dim_param: dim->nparam += n; break;
1110 case isl_dim_in: dim->n_in += n; break;
1111 case isl_dim_out: dim->n_out += n; break;
1112 default: ;
1115 switch (src_type) {
1116 case isl_dim_param: dim->nparam -= n; break;
1117 case isl_dim_in: dim->n_in -= n; break;
1118 case isl_dim_out: dim->n_out -= n; break;
1119 default: ;
1122 if (dst_type != isl_dim_param && src_type != isl_dim_param)
1123 return dim;
1125 for (i = 0; i < 2; ++i) {
1126 if (!dim->nested[i])
1127 continue;
1128 dim->nested[i] = isl_space_replace(dim->nested[i],
1129 isl_dim_param, dim);
1130 if (!dim->nested[i])
1131 goto error;
1134 return dim;
1135 error:
1136 isl_space_free(dim);
1137 return NULL;
1140 __isl_give isl_space *isl_space_join(__isl_take isl_space *left,
1141 __isl_take isl_space *right)
1143 isl_space *dim;
1145 if (!left || !right)
1146 goto error;
1148 isl_assert(left->ctx, match(left, isl_dim_param, right, isl_dim_param),
1149 goto error);
1150 isl_assert(left->ctx,
1151 isl_space_tuple_is_equal(left, isl_dim_out, right, isl_dim_in),
1152 goto error);
1154 dim = isl_space_alloc(left->ctx, left->nparam, left->n_in, right->n_out);
1155 if (!dim)
1156 goto error;
1158 dim = copy_ids(dim, isl_dim_param, 0, left, isl_dim_param);
1159 dim = copy_ids(dim, isl_dim_in, 0, left, isl_dim_in);
1160 dim = copy_ids(dim, isl_dim_out, 0, right, isl_dim_out);
1162 if (dim && left->tuple_id[0] &&
1163 !(dim->tuple_id[0] = isl_id_copy(left->tuple_id[0])))
1164 goto error;
1165 if (dim && right->tuple_id[1] &&
1166 !(dim->tuple_id[1] = isl_id_copy(right->tuple_id[1])))
1167 goto error;
1168 if (dim && left->nested[0] &&
1169 !(dim->nested[0] = isl_space_copy(left->nested[0])))
1170 goto error;
1171 if (dim && right->nested[1] &&
1172 !(dim->nested[1] = isl_space_copy(right->nested[1])))
1173 goto error;
1175 isl_space_free(left);
1176 isl_space_free(right);
1178 return dim;
1179 error:
1180 isl_space_free(left);
1181 isl_space_free(right);
1182 return NULL;
1185 /* Given two map spaces { A -> C } and { B -> D }, construct the space
1186 * { [A -> B] -> [C -> D] }.
1187 * Given two set spaces { A } and { B }, construct the space { [A -> B] }.
1189 __isl_give isl_space *isl_space_product(__isl_take isl_space *left,
1190 __isl_take isl_space *right)
1192 isl_space *dom1, *dom2, *nest1, *nest2;
1193 int is_set;
1195 if (!left || !right)
1196 goto error;
1198 is_set = isl_space_is_set(left);
1199 if (is_set != isl_space_is_set(right))
1200 isl_die(isl_space_get_ctx(left), isl_error_invalid,
1201 "expecting either two set spaces or two map spaces",
1202 goto error);
1203 if (is_set)
1204 return isl_space_range_product(left, right);
1206 isl_assert(left->ctx, match(left, isl_dim_param, right, isl_dim_param),
1207 goto error);
1209 dom1 = isl_space_domain(isl_space_copy(left));
1210 dom2 = isl_space_domain(isl_space_copy(right));
1211 nest1 = isl_space_wrap(isl_space_join(isl_space_reverse(dom1), dom2));
1213 dom1 = isl_space_range(left);
1214 dom2 = isl_space_range(right);
1215 nest2 = isl_space_wrap(isl_space_join(isl_space_reverse(dom1), dom2));
1217 return isl_space_join(isl_space_reverse(nest1), nest2);
1218 error:
1219 isl_space_free(left);
1220 isl_space_free(right);
1221 return NULL;
1224 /* Given two spaces { A -> C } and { B -> C }, construct the space
1225 * { [A -> B] -> C }
1227 __isl_give isl_space *isl_space_domain_product(__isl_take isl_space *left,
1228 __isl_take isl_space *right)
1230 isl_space *ran, *dom1, *dom2, *nest;
1232 if (!left || !right)
1233 goto error;
1235 if (!match(left, isl_dim_param, right, isl_dim_param))
1236 isl_die(left->ctx, isl_error_invalid,
1237 "parameters need to match", goto error);
1238 if (!isl_space_tuple_is_equal(left, isl_dim_out, right, isl_dim_out))
1239 isl_die(left->ctx, isl_error_invalid,
1240 "ranges need to match", goto error);
1242 ran = isl_space_range(isl_space_copy(left));
1244 dom1 = isl_space_domain(left);
1245 dom2 = isl_space_domain(right);
1246 nest = isl_space_wrap(isl_space_join(isl_space_reverse(dom1), dom2));
1248 return isl_space_join(isl_space_reverse(nest), ran);
1249 error:
1250 isl_space_free(left);
1251 isl_space_free(right);
1252 return NULL;
1255 __isl_give isl_space *isl_space_range_product(__isl_take isl_space *left,
1256 __isl_take isl_space *right)
1258 isl_space *dom, *ran1, *ran2, *nest;
1260 if (!left || !right)
1261 goto error;
1263 isl_assert(left->ctx, match(left, isl_dim_param, right, isl_dim_param),
1264 goto error);
1265 if (!isl_space_tuple_is_equal(left, isl_dim_in, right, isl_dim_in))
1266 isl_die(left->ctx, isl_error_invalid,
1267 "domains need to match", goto error);
1269 dom = isl_space_domain(isl_space_copy(left));
1271 ran1 = isl_space_range(left);
1272 ran2 = isl_space_range(right);
1273 nest = isl_space_wrap(isl_space_join(isl_space_reverse(ran1), ran2));
1275 return isl_space_join(isl_space_reverse(dom), nest);
1276 error:
1277 isl_space_free(left);
1278 isl_space_free(right);
1279 return NULL;
1282 /* Given a space of the form [A -> B] -> [C -> D], return the space A -> C.
1284 __isl_give isl_space *isl_space_factor_domain(__isl_take isl_space *space)
1286 space = isl_space_domain_factor_domain(space);
1287 space = isl_space_range_factor_domain(space);
1288 return space;
1291 /* Given a space of the form [A -> B] -> [C -> D], return the space B -> D.
1293 __isl_give isl_space *isl_space_factor_range(__isl_take isl_space *space)
1295 space = isl_space_domain_factor_range(space);
1296 space = isl_space_range_factor_range(space);
1297 return space;
1300 /* Given a space of the form [A -> B] -> C, return the space A -> C.
1302 __isl_give isl_space *isl_space_domain_factor_domain(
1303 __isl_take isl_space *space)
1305 isl_space *nested;
1306 isl_space *domain;
1308 if (!space)
1309 return NULL;
1310 if (!isl_space_domain_is_wrapping(space))
1311 isl_die(isl_space_get_ctx(space), isl_error_invalid,
1312 "domain not a product", return isl_space_free(space));
1314 nested = space->nested[0];
1315 domain = isl_space_copy(space);
1316 domain = isl_space_drop_dims(domain, isl_dim_in,
1317 nested->n_in, nested->n_out);
1318 if (!domain)
1319 return isl_space_free(space);
1320 if (nested->tuple_id[0]) {
1321 domain->tuple_id[0] = isl_id_copy(nested->tuple_id[0]);
1322 if (!domain->tuple_id[0])
1323 goto error;
1325 if (nested->nested[0]) {
1326 domain->nested[0] = isl_space_copy(nested->nested[0]);
1327 if (!domain->nested[0])
1328 goto error;
1331 isl_space_free(space);
1332 return domain;
1333 error:
1334 isl_space_free(space);
1335 isl_space_free(domain);
1336 return NULL;
1339 /* Given a space of the form [A -> B] -> C, return the space B -> C.
1341 __isl_give isl_space *isl_space_domain_factor_range(
1342 __isl_take isl_space *space)
1344 isl_space *nested;
1345 isl_space *range;
1347 if (!space)
1348 return NULL;
1349 if (!isl_space_domain_is_wrapping(space))
1350 isl_die(isl_space_get_ctx(space), isl_error_invalid,
1351 "domain not a product", return isl_space_free(space));
1353 nested = space->nested[0];
1354 range = isl_space_copy(space);
1355 range = isl_space_drop_dims(range, isl_dim_in, 0, nested->n_in);
1356 if (!range)
1357 return isl_space_free(space);
1358 if (nested->tuple_id[1]) {
1359 range->tuple_id[0] = isl_id_copy(nested->tuple_id[1]);
1360 if (!range->tuple_id[0])
1361 goto error;
1363 if (nested->nested[1]) {
1364 range->nested[0] = isl_space_copy(nested->nested[1]);
1365 if (!range->nested[0])
1366 goto error;
1369 isl_space_free(space);
1370 return range;
1371 error:
1372 isl_space_free(space);
1373 isl_space_free(range);
1374 return NULL;
1377 /* Given a space of the form A -> [B -> C], return the space A -> B.
1379 __isl_give isl_space *isl_space_range_factor_domain(
1380 __isl_take isl_space *space)
1382 isl_space *nested;
1383 isl_space *domain;
1385 if (!space)
1386 return NULL;
1387 if (!isl_space_range_is_wrapping(space))
1388 isl_die(isl_space_get_ctx(space), isl_error_invalid,
1389 "range not a product", return isl_space_free(space));
1391 nested = space->nested[1];
1392 domain = isl_space_copy(space);
1393 domain = isl_space_drop_dims(domain, isl_dim_out,
1394 nested->n_in, nested->n_out);
1395 if (!domain)
1396 return isl_space_free(space);
1397 if (nested->tuple_id[0]) {
1398 domain->tuple_id[1] = isl_id_copy(nested->tuple_id[0]);
1399 if (!domain->tuple_id[1])
1400 goto error;
1402 if (nested->nested[0]) {
1403 domain->nested[1] = isl_space_copy(nested->nested[0]);
1404 if (!domain->nested[1])
1405 goto error;
1408 isl_space_free(space);
1409 return domain;
1410 error:
1411 isl_space_free(space);
1412 isl_space_free(domain);
1413 return NULL;
1416 /* Given a space of the form A -> [B -> C], return the space A -> C.
1418 __isl_give isl_space *isl_space_range_factor_range(
1419 __isl_take isl_space *space)
1421 isl_space *nested;
1422 isl_space *range;
1424 if (!space)
1425 return NULL;
1426 if (!isl_space_range_is_wrapping(space))
1427 isl_die(isl_space_get_ctx(space), isl_error_invalid,
1428 "range not a product", return isl_space_free(space));
1430 nested = space->nested[1];
1431 range = isl_space_copy(space);
1432 range = isl_space_drop_dims(range, isl_dim_out, 0, nested->n_in);
1433 if (!range)
1434 return isl_space_free(space);
1435 if (nested->tuple_id[1]) {
1436 range->tuple_id[1] = isl_id_copy(nested->tuple_id[1]);
1437 if (!range->tuple_id[1])
1438 goto error;
1440 if (nested->nested[1]) {
1441 range->nested[1] = isl_space_copy(nested->nested[1]);
1442 if (!range->nested[1])
1443 goto error;
1446 isl_space_free(space);
1447 return range;
1448 error:
1449 isl_space_free(space);
1450 isl_space_free(range);
1451 return NULL;
1454 __isl_give isl_space *isl_space_map_from_set(__isl_take isl_space *dim)
1456 isl_ctx *ctx;
1457 isl_id **ids = NULL;
1459 if (!dim)
1460 return NULL;
1461 ctx = isl_space_get_ctx(dim);
1462 if (!isl_space_is_set(dim))
1463 isl_die(ctx, isl_error_invalid, "not a set space", goto error);
1464 dim = isl_space_cow(dim);
1465 if (!dim)
1466 return NULL;
1467 if (dim->ids) {
1468 ids = isl_calloc_array(dim->ctx, isl_id *,
1469 dim->nparam + dim->n_out + dim->n_out);
1470 if (!ids)
1471 goto error;
1472 get_ids(dim, isl_dim_param, 0, dim->nparam, ids);
1473 get_ids(dim, isl_dim_out, 0, dim->n_out, ids + dim->nparam);
1475 dim->n_in = dim->n_out;
1476 if (ids) {
1477 free(dim->ids);
1478 dim->ids = ids;
1479 dim->n_id = dim->nparam + dim->n_out + dim->n_out;
1480 dim = copy_ids(dim, isl_dim_out, 0, dim, isl_dim_in);
1482 isl_id_free(dim->tuple_id[0]);
1483 dim->tuple_id[0] = isl_id_copy(dim->tuple_id[1]);
1484 isl_space_free(dim->nested[0]);
1485 dim->nested[0] = isl_space_copy(dim->nested[1]);
1486 return dim;
1487 error:
1488 isl_space_free(dim);
1489 return NULL;
1492 __isl_give isl_space *isl_space_map_from_domain_and_range(
1493 __isl_take isl_space *domain, __isl_take isl_space *range)
1495 if (!domain || !range)
1496 goto error;
1497 if (!isl_space_is_set(domain))
1498 isl_die(isl_space_get_ctx(domain), isl_error_invalid,
1499 "domain is not a set space", goto error);
1500 if (!isl_space_is_set(range))
1501 isl_die(isl_space_get_ctx(range), isl_error_invalid,
1502 "range is not a set space", goto error);
1503 return isl_space_join(isl_space_reverse(domain), range);
1504 error:
1505 isl_space_free(domain);
1506 isl_space_free(range);
1507 return NULL;
1510 static __isl_give isl_space *set_ids(__isl_take isl_space *dim,
1511 enum isl_dim_type type,
1512 unsigned first, unsigned n, __isl_take isl_id **ids)
1514 int i;
1516 for (i = 0; i < n ; ++i)
1517 dim = set_id(dim, type, first + i, ids[i]);
1519 return dim;
1522 __isl_give isl_space *isl_space_reverse(__isl_take isl_space *dim)
1524 unsigned t;
1525 isl_space *nested;
1526 isl_id **ids = NULL;
1527 isl_id *id;
1529 if (!dim)
1530 return NULL;
1531 if (match(dim, isl_dim_in, dim, isl_dim_out))
1532 return dim;
1534 dim = isl_space_cow(dim);
1535 if (!dim)
1536 return NULL;
1538 id = dim->tuple_id[0];
1539 dim->tuple_id[0] = dim->tuple_id[1];
1540 dim->tuple_id[1] = id;
1542 nested = dim->nested[0];
1543 dim->nested[0] = dim->nested[1];
1544 dim->nested[1] = nested;
1546 if (dim->ids) {
1547 int n_id = dim->n_in + dim->n_out;
1548 ids = isl_alloc_array(dim->ctx, isl_id *, n_id);
1549 if (n_id && !ids)
1550 goto error;
1551 get_ids(dim, isl_dim_in, 0, dim->n_in, ids);
1552 get_ids(dim, isl_dim_out, 0, dim->n_out, ids + dim->n_in);
1555 t = dim->n_in;
1556 dim->n_in = dim->n_out;
1557 dim->n_out = t;
1559 if (dim->ids) {
1560 dim = set_ids(dim, isl_dim_out, 0, dim->n_out, ids);
1561 dim = set_ids(dim, isl_dim_in, 0, dim->n_in, ids + dim->n_out);
1562 free(ids);
1565 return dim;
1566 error:
1567 free(ids);
1568 isl_space_free(dim);
1569 return NULL;
1572 __isl_give isl_space *isl_space_drop_dims(__isl_take isl_space *dim,
1573 enum isl_dim_type type, unsigned first, unsigned num)
1575 int i;
1577 if (!dim)
1578 return NULL;
1580 if (num == 0)
1581 return isl_space_reset(dim, type);
1583 if (!valid_dim_type(type))
1584 isl_die(dim->ctx, isl_error_invalid,
1585 "cannot drop dimensions of specified type", goto error);
1587 if (first + num > n(dim, type) || first + num < first)
1588 isl_die(isl_space_get_ctx(dim), isl_error_invalid,
1589 "index out of bounds", return isl_space_free(dim));
1590 dim = isl_space_cow(dim);
1591 if (!dim)
1592 goto error;
1593 if (dim->ids) {
1594 dim = extend_ids(dim);
1595 if (!dim)
1596 goto error;
1597 for (i = 0; i < num; ++i)
1598 isl_id_free(get_id(dim, type, first + i));
1599 for (i = first+num; i < n(dim, type); ++i)
1600 set_id(dim, type, i - num, get_id(dim, type, i));
1601 switch (type) {
1602 case isl_dim_param:
1603 get_ids(dim, isl_dim_in, 0, dim->n_in,
1604 dim->ids + offset(dim, isl_dim_in) - num);
1605 case isl_dim_in:
1606 get_ids(dim, isl_dim_out, 0, dim->n_out,
1607 dim->ids + offset(dim, isl_dim_out) - num);
1608 default:
1611 dim->n_id -= num;
1613 switch (type) {
1614 case isl_dim_param: dim->nparam -= num; break;
1615 case isl_dim_in: dim->n_in -= num; break;
1616 case isl_dim_out: dim->n_out -= num; break;
1617 default: ;
1619 dim = isl_space_reset(dim, type);
1620 if (type == isl_dim_param) {
1621 if (dim && dim->nested[0] &&
1622 !(dim->nested[0] = isl_space_drop_dims(dim->nested[0],
1623 isl_dim_param, first, num)))
1624 goto error;
1625 if (dim && dim->nested[1] &&
1626 !(dim->nested[1] = isl_space_drop_dims(dim->nested[1],
1627 isl_dim_param, first, num)))
1628 goto error;
1630 return dim;
1631 error:
1632 isl_space_free(dim);
1633 return NULL;
1636 __isl_give isl_space *isl_space_drop_inputs(__isl_take isl_space *dim,
1637 unsigned first, unsigned n)
1639 if (!dim)
1640 return NULL;
1641 return isl_space_drop_dims(dim, isl_dim_in, first, n);
1644 __isl_give isl_space *isl_space_drop_outputs(__isl_take isl_space *dim,
1645 unsigned first, unsigned n)
1647 if (!dim)
1648 return NULL;
1649 return isl_space_drop_dims(dim, isl_dim_out, first, n);
1652 __isl_give isl_space *isl_space_domain(__isl_take isl_space *dim)
1654 if (!dim)
1655 return NULL;
1656 dim = isl_space_drop_outputs(dim, 0, dim->n_out);
1657 dim = isl_space_reverse(dim);
1658 dim = mark_as_set(dim);
1659 return dim;
1662 __isl_give isl_space *isl_space_from_domain(__isl_take isl_space *dim)
1664 if (!dim)
1665 return NULL;
1666 if (!isl_space_is_set(dim))
1667 isl_die(isl_space_get_ctx(dim), isl_error_invalid,
1668 "not a set space", goto error);
1669 dim = isl_space_reverse(dim);
1670 dim = isl_space_reset(dim, isl_dim_out);
1671 return dim;
1672 error:
1673 isl_space_free(dim);
1674 return NULL;
1677 __isl_give isl_space *isl_space_range(__isl_take isl_space *dim)
1679 if (!dim)
1680 return NULL;
1681 dim = isl_space_drop_inputs(dim, 0, dim->n_in);
1682 dim = mark_as_set(dim);
1683 return dim;
1686 __isl_give isl_space *isl_space_from_range(__isl_take isl_space *dim)
1688 if (!dim)
1689 return NULL;
1690 if (!isl_space_is_set(dim))
1691 isl_die(isl_space_get_ctx(dim), isl_error_invalid,
1692 "not a set space", goto error);
1693 return isl_space_reset(dim, isl_dim_in);
1694 error:
1695 isl_space_free(dim);
1696 return NULL;
1699 /* Given a map space A -> B, return the map space [A -> B] -> A.
1701 __isl_give isl_space *isl_space_domain_map(__isl_take isl_space *space)
1703 isl_space *domain;
1705 domain = isl_space_from_range(isl_space_domain(isl_space_copy(space)));
1706 space = isl_space_from_domain(isl_space_wrap(space));
1707 space = isl_space_join(space, domain);
1709 return space;
1712 /* Given a map space A -> B, return the map space [A -> B] -> B.
1714 __isl_give isl_space *isl_space_range_map(__isl_take isl_space *space)
1716 isl_space *range;
1718 range = isl_space_from_range(isl_space_range(isl_space_copy(space)));
1719 space = isl_space_from_domain(isl_space_wrap(space));
1720 space = isl_space_join(space, range);
1722 return space;
1725 __isl_give isl_space *isl_space_params(__isl_take isl_space *space)
1727 if (isl_space_is_params(space))
1728 return space;
1729 space = isl_space_drop_dims(space,
1730 isl_dim_in, 0, isl_space_dim(space, isl_dim_in));
1731 space = isl_space_drop_dims(space,
1732 isl_dim_out, 0, isl_space_dim(space, isl_dim_out));
1733 space = mark_as_params(space);
1734 return space;
1737 __isl_give isl_space *isl_space_set_from_params(__isl_take isl_space *space)
1739 if (!space)
1740 return NULL;
1741 if (!isl_space_is_params(space))
1742 isl_die(isl_space_get_ctx(space), isl_error_invalid,
1743 "not a parameter space", goto error);
1744 return isl_space_reset(space, isl_dim_set);
1745 error:
1746 isl_space_free(space);
1747 return NULL;
1750 __isl_give isl_space *isl_space_as_set_space(__isl_take isl_space *dim)
1752 dim = isl_space_cow(dim);
1753 if (!dim)
1754 return NULL;
1756 dim->n_out += dim->n_in;
1757 dim->n_in = 0;
1758 dim = isl_space_reset(dim, isl_dim_in);
1759 dim = isl_space_reset(dim, isl_dim_out);
1761 return dim;
1764 __isl_give isl_space *isl_space_underlying(__isl_take isl_space *dim,
1765 unsigned n_div)
1767 int i;
1769 if (!dim)
1770 return NULL;
1771 if (n_div == 0 &&
1772 dim->nparam == 0 && dim->n_in == 0 && dim->n_id == 0)
1773 return isl_space_reset(isl_space_reset(dim, isl_dim_in), isl_dim_out);
1774 dim = isl_space_cow(dim);
1775 if (!dim)
1776 return NULL;
1777 dim->n_out += dim->nparam + dim->n_in + n_div;
1778 dim->nparam = 0;
1779 dim->n_in = 0;
1781 for (i = 0; i < dim->n_id; ++i)
1782 isl_id_free(get_id(dim, isl_dim_out, i));
1783 dim->n_id = 0;
1784 dim = isl_space_reset(dim, isl_dim_in);
1785 dim = isl_space_reset(dim, isl_dim_out);
1787 return dim;
1790 /* Are the two spaces the same, including positions and names of parameters?
1792 isl_bool isl_space_is_equal(__isl_keep isl_space *dim1,
1793 __isl_keep isl_space *dim2)
1795 if (!dim1 || !dim2)
1796 return isl_bool_error;
1797 if (dim1 == dim2)
1798 return isl_bool_true;
1799 return match(dim1, isl_dim_param, dim2, isl_dim_param) &&
1800 isl_space_tuple_is_equal(dim1, isl_dim_in, dim2, isl_dim_in) &&
1801 isl_space_tuple_is_equal(dim1, isl_dim_out, dim2, isl_dim_out);
1804 /* Is space1 equal to the domain of space2?
1806 * In the internal version we also allow space2 to be the space of a set,
1807 * provided space1 is a parameter space.
1809 isl_bool isl_space_is_domain_internal(__isl_keep isl_space *space1,
1810 __isl_keep isl_space *space2)
1812 if (!space1 || !space2)
1813 return isl_bool_error;
1814 if (!isl_space_is_set(space1))
1815 return isl_bool_false;
1816 return match(space1, isl_dim_param, space2, isl_dim_param) &&
1817 isl_space_tuple_is_equal(space1, isl_dim_set,
1818 space2, isl_dim_in);
1821 /* Is space1 equal to the domain of space2?
1823 isl_bool isl_space_is_domain(__isl_keep isl_space *space1,
1824 __isl_keep isl_space *space2)
1826 if (!space2)
1827 return isl_bool_error;
1828 if (!isl_space_is_map(space2))
1829 return isl_bool_false;
1830 return isl_space_is_domain_internal(space1, space2);
1833 /* Is space1 equal to the range of space2?
1835 * In the internal version, space2 is allowed to be the space of a set,
1836 * in which case it should be equal to space1.
1838 isl_bool isl_space_is_range_internal(__isl_keep isl_space *space1,
1839 __isl_keep isl_space *space2)
1841 if (!space1 || !space2)
1842 return isl_bool_error;
1843 if (!isl_space_is_set(space1))
1844 return isl_bool_false;
1845 return match(space1, isl_dim_param, space2, isl_dim_param) &&
1846 isl_space_tuple_is_equal(space1, isl_dim_set,
1847 space2, isl_dim_out);
1850 /* Is space1 equal to the range of space2?
1852 isl_bool isl_space_is_range(__isl_keep isl_space *space1,
1853 __isl_keep isl_space *space2)
1855 if (!space2)
1856 return isl_bool_error;
1857 if (!isl_space_is_map(space2))
1858 return isl_bool_false;
1859 return isl_space_is_range_internal(space1, space2);
1862 int isl_space_compatible(__isl_keep isl_space *dim1,
1863 __isl_keep isl_space *dim2)
1865 return dim1->nparam == dim2->nparam &&
1866 dim1->n_in + dim1->n_out == dim2->n_in + dim2->n_out;
1869 static uint32_t isl_hash_dim(uint32_t hash, __isl_keep isl_space *dim)
1871 int i;
1872 isl_id *id;
1874 if (!dim)
1875 return hash;
1877 isl_hash_byte(hash, dim->nparam % 256);
1878 isl_hash_byte(hash, dim->n_in % 256);
1879 isl_hash_byte(hash, dim->n_out % 256);
1881 for (i = 0; i < dim->nparam; ++i) {
1882 id = get_id(dim, isl_dim_param, i);
1883 hash = isl_hash_id(hash, id);
1886 id = tuple_id(dim, isl_dim_in);
1887 hash = isl_hash_id(hash, id);
1888 id = tuple_id(dim, isl_dim_out);
1889 hash = isl_hash_id(hash, id);
1891 hash = isl_hash_dim(hash, dim->nested[0]);
1892 hash = isl_hash_dim(hash, dim->nested[1]);
1894 return hash;
1897 uint32_t isl_space_get_hash(__isl_keep isl_space *dim)
1899 uint32_t hash;
1901 if (!dim)
1902 return 0;
1904 hash = isl_hash_init();
1905 hash = isl_hash_dim(hash, dim);
1907 return hash;
1910 isl_bool isl_space_is_wrapping(__isl_keep isl_space *dim)
1912 if (!dim)
1913 return isl_bool_error;
1915 if (!isl_space_is_set(dim))
1916 return isl_bool_false;
1918 return dim->nested[1] != NULL;
1921 /* Is "space" the space of a map where the domain is a wrapped map space?
1923 isl_bool isl_space_domain_is_wrapping(__isl_keep isl_space *space)
1925 if (!space)
1926 return isl_bool_error;
1928 if (isl_space_is_set(space))
1929 return isl_bool_false;
1931 return space->nested[0] != NULL;
1934 /* Is "space" the space of a map where the range is a wrapped map space?
1936 isl_bool isl_space_range_is_wrapping(__isl_keep isl_space *space)
1938 if (!space)
1939 return isl_bool_error;
1941 if (isl_space_is_set(space))
1942 return isl_bool_false;
1944 return space->nested[1] != NULL;
1947 __isl_give isl_space *isl_space_wrap(__isl_take isl_space *dim)
1949 isl_space *wrap;
1951 if (!dim)
1952 return NULL;
1954 wrap = isl_space_set_alloc(dim->ctx,
1955 dim->nparam, dim->n_in + dim->n_out);
1957 wrap = copy_ids(wrap, isl_dim_param, 0, dim, isl_dim_param);
1958 wrap = copy_ids(wrap, isl_dim_set, 0, dim, isl_dim_in);
1959 wrap = copy_ids(wrap, isl_dim_set, dim->n_in, dim, isl_dim_out);
1961 if (!wrap)
1962 goto error;
1964 wrap->nested[1] = dim;
1966 return wrap;
1967 error:
1968 isl_space_free(dim);
1969 return NULL;
1972 __isl_give isl_space *isl_space_unwrap(__isl_take isl_space *dim)
1974 isl_space *unwrap;
1976 if (!dim)
1977 return NULL;
1979 if (!isl_space_is_wrapping(dim))
1980 isl_die(dim->ctx, isl_error_invalid, "not a wrapping space",
1981 goto error);
1983 unwrap = isl_space_copy(dim->nested[1]);
1984 isl_space_free(dim);
1986 return unwrap;
1987 error:
1988 isl_space_free(dim);
1989 return NULL;
1992 int isl_space_is_named_or_nested(__isl_keep isl_space *dim, enum isl_dim_type type)
1994 if (type != isl_dim_in && type != isl_dim_out)
1995 return 0;
1996 if (!dim)
1997 return -1;
1998 if (dim->tuple_id[type - isl_dim_in])
1999 return 1;
2000 if (dim->nested[type - isl_dim_in])
2001 return 1;
2002 return 0;
2005 int isl_space_may_be_set(__isl_keep isl_space *dim)
2007 if (!dim)
2008 return -1;
2009 if (isl_space_is_set(dim))
2010 return 1;
2011 if (isl_space_dim(dim, isl_dim_in) != 0)
2012 return 0;
2013 if (isl_space_is_named_or_nested(dim, isl_dim_in))
2014 return 0;
2015 return 1;
2018 __isl_give isl_space *isl_space_reset(__isl_take isl_space *dim,
2019 enum isl_dim_type type)
2021 if (!isl_space_is_named_or_nested(dim, type))
2022 return dim;
2024 dim = isl_space_cow(dim);
2025 if (!dim)
2026 return NULL;
2028 isl_id_free(dim->tuple_id[type - isl_dim_in]);
2029 dim->tuple_id[type - isl_dim_in] = NULL;
2030 isl_space_free(dim->nested[type - isl_dim_in]);
2031 dim->nested[type - isl_dim_in] = NULL;
2033 return dim;
2036 __isl_give isl_space *isl_space_flatten(__isl_take isl_space *dim)
2038 if (!dim)
2039 return NULL;
2040 if (!dim->nested[0] && !dim->nested[1])
2041 return dim;
2043 if (dim->nested[0])
2044 dim = isl_space_reset(dim, isl_dim_in);
2045 if (dim && dim->nested[1])
2046 dim = isl_space_reset(dim, isl_dim_out);
2048 return dim;
2051 __isl_give isl_space *isl_space_flatten_domain(__isl_take isl_space *dim)
2053 if (!dim)
2054 return NULL;
2055 if (!dim->nested[0])
2056 return dim;
2058 return isl_space_reset(dim, isl_dim_in);
2061 __isl_give isl_space *isl_space_flatten_range(__isl_take isl_space *dim)
2063 if (!dim)
2064 return NULL;
2065 if (!dim->nested[1])
2066 return dim;
2068 return isl_space_reset(dim, isl_dim_out);
2071 /* Replace the dimensions of the given type of dst by those of src.
2073 __isl_give isl_space *isl_space_replace(__isl_take isl_space *dst,
2074 enum isl_dim_type type, __isl_keep isl_space *src)
2076 dst = isl_space_cow(dst);
2078 if (!dst || !src)
2079 goto error;
2081 dst = isl_space_drop_dims(dst, type, 0, isl_space_dim(dst, type));
2082 dst = isl_space_add_dims(dst, type, isl_space_dim(src, type));
2083 dst = copy_ids(dst, type, 0, src, type);
2085 if (dst && type == isl_dim_param) {
2086 int i;
2087 for (i = 0; i <= 1; ++i) {
2088 if (!dst->nested[i])
2089 continue;
2090 dst->nested[i] = isl_space_replace(dst->nested[i],
2091 type, src);
2092 if (!dst->nested[i])
2093 goto error;
2097 return dst;
2098 error:
2099 isl_space_free(dst);
2100 return NULL;
2103 /* Given a dimension specification "dim" of a set, create a dimension
2104 * specification for the lift of the set. In particular, the result
2105 * is of the form [dim -> local[..]], with n_local variables in the
2106 * range of the wrapped map.
2108 __isl_give isl_space *isl_space_lift(__isl_take isl_space *dim, unsigned n_local)
2110 isl_space *local_dim;
2112 if (!dim)
2113 return NULL;
2115 local_dim = isl_space_dup(dim);
2116 local_dim = isl_space_drop_dims(local_dim, isl_dim_set, 0, dim->n_out);
2117 local_dim = isl_space_add_dims(local_dim, isl_dim_set, n_local);
2118 local_dim = isl_space_set_tuple_name(local_dim, isl_dim_set, "local");
2119 dim = isl_space_join(isl_space_from_domain(dim),
2120 isl_space_from_range(local_dim));
2121 dim = isl_space_wrap(dim);
2122 dim = isl_space_set_tuple_name(dim, isl_dim_set, "lifted");
2124 return dim;
2127 isl_bool isl_space_can_zip(__isl_keep isl_space *dim)
2129 if (!dim)
2130 return isl_bool_error;
2132 return dim->nested[0] && dim->nested[1];
2135 __isl_give isl_space *isl_space_zip(__isl_take isl_space *dim)
2137 isl_space *dom, *ran;
2138 isl_space *dom_dom, *dom_ran, *ran_dom, *ran_ran;
2140 if (!isl_space_can_zip(dim))
2141 isl_die(dim->ctx, isl_error_invalid, "dim cannot be zipped",
2142 goto error);
2144 if (!dim)
2145 return NULL;
2146 dom = isl_space_unwrap(isl_space_domain(isl_space_copy(dim)));
2147 ran = isl_space_unwrap(isl_space_range(dim));
2148 dom_dom = isl_space_domain(isl_space_copy(dom));
2149 dom_ran = isl_space_range(dom);
2150 ran_dom = isl_space_domain(isl_space_copy(ran));
2151 ran_ran = isl_space_range(ran);
2152 dom = isl_space_join(isl_space_from_domain(dom_dom),
2153 isl_space_from_range(ran_dom));
2154 ran = isl_space_join(isl_space_from_domain(dom_ran),
2155 isl_space_from_range(ran_ran));
2156 return isl_space_join(isl_space_from_domain(isl_space_wrap(dom)),
2157 isl_space_from_range(isl_space_wrap(ran)));
2158 error:
2159 isl_space_free(dim);
2160 return NULL;
2163 /* Can we apply isl_space_curry to "space"?
2164 * That is, does it have a nested relation in its domain?
2166 isl_bool isl_space_can_curry(__isl_keep isl_space *space)
2168 if (!space)
2169 return isl_bool_error;
2171 return !!space->nested[0];
2174 /* Given a space (A -> B) -> C, return the corresponding space
2175 * A -> (B -> C).
2177 __isl_give isl_space *isl_space_curry(__isl_take isl_space *space)
2179 isl_space *dom, *ran;
2180 isl_space *dom_dom, *dom_ran;
2182 if (!space)
2183 return NULL;
2185 if (!isl_space_can_curry(space))
2186 isl_die(space->ctx, isl_error_invalid,
2187 "space cannot be curried", goto error);
2189 dom = isl_space_unwrap(isl_space_domain(isl_space_copy(space)));
2190 ran = isl_space_range(space);
2191 dom_dom = isl_space_domain(isl_space_copy(dom));
2192 dom_ran = isl_space_range(dom);
2193 ran = isl_space_join(isl_space_from_domain(dom_ran),
2194 isl_space_from_range(ran));
2195 return isl_space_join(isl_space_from_domain(dom_dom),
2196 isl_space_from_range(isl_space_wrap(ran)));
2197 error:
2198 isl_space_free(space);
2199 return NULL;
2202 /* Can we apply isl_space_uncurry to "space"?
2203 * That is, does it have a nested relation in its range?
2205 isl_bool isl_space_can_uncurry(__isl_keep isl_space *space)
2207 if (!space)
2208 return isl_bool_error;
2210 return !!space->nested[1];
2213 /* Given a space A -> (B -> C), return the corresponding space
2214 * (A -> B) -> C.
2216 __isl_give isl_space *isl_space_uncurry(__isl_take isl_space *space)
2218 isl_space *dom, *ran;
2219 isl_space *ran_dom, *ran_ran;
2221 if (!space)
2222 return NULL;
2224 if (!isl_space_can_uncurry(space))
2225 isl_die(space->ctx, isl_error_invalid,
2226 "space cannot be uncurried",
2227 return isl_space_free(space));
2229 dom = isl_space_domain(isl_space_copy(space));
2230 ran = isl_space_unwrap(isl_space_range(space));
2231 ran_dom = isl_space_domain(isl_space_copy(ran));
2232 ran_ran = isl_space_range(ran);
2233 dom = isl_space_join(isl_space_from_domain(dom),
2234 isl_space_from_range(ran_dom));
2235 return isl_space_join(isl_space_from_domain(isl_space_wrap(dom)),
2236 isl_space_from_range(ran_ran));
2239 int isl_space_has_named_params(__isl_keep isl_space *dim)
2241 int i;
2242 unsigned off;
2244 if (!dim)
2245 return -1;
2246 if (dim->nparam == 0)
2247 return 1;
2248 off = isl_space_offset(dim, isl_dim_param);
2249 if (off + dim->nparam > dim->n_id)
2250 return 0;
2251 for (i = 0; i < dim->nparam; ++i)
2252 if (!dim->ids[off + i])
2253 return 0;
2254 return 1;
2257 /* Align the initial parameters of dim1 to match the order in dim2.
2259 __isl_give isl_space *isl_space_align_params(__isl_take isl_space *dim1,
2260 __isl_take isl_space *dim2)
2262 isl_reordering *exp;
2264 if (!isl_space_has_named_params(dim1) || !isl_space_has_named_params(dim2))
2265 isl_die(isl_space_get_ctx(dim1), isl_error_invalid,
2266 "parameter alignment requires named parameters",
2267 goto error);
2269 dim2 = isl_space_params(dim2);
2270 exp = isl_parameter_alignment_reordering(dim1, dim2);
2271 exp = isl_reordering_extend_space(exp, dim1);
2272 isl_space_free(dim2);
2273 if (!exp)
2274 return NULL;
2275 dim1 = isl_space_copy(exp->dim);
2276 isl_reordering_free(exp);
2277 return dim1;
2278 error:
2279 isl_space_free(dim1);
2280 isl_space_free(dim2);
2281 return NULL;
2284 /* Given the space of set (domain), construct a space for a map
2285 * with as domain the given space and as range the range of "model".
2287 __isl_give isl_space *isl_space_extend_domain_with_range(
2288 __isl_take isl_space *space, __isl_take isl_space *model)
2290 if (!model)
2291 goto error;
2293 space = isl_space_from_domain(space);
2294 space = isl_space_add_dims(space, isl_dim_out,
2295 isl_space_dim(model, isl_dim_out));
2296 if (isl_space_has_tuple_id(model, isl_dim_out))
2297 space = isl_space_set_tuple_id(space, isl_dim_out,
2298 isl_space_get_tuple_id(model, isl_dim_out));
2299 if (!space)
2300 goto error;
2301 if (model->nested[1]) {
2302 isl_space *nested = isl_space_copy(model->nested[1]);
2303 int n_nested, n_space;
2304 nested = isl_space_align_params(nested, isl_space_copy(space));
2305 n_nested = isl_space_dim(nested, isl_dim_param);
2306 n_space = isl_space_dim(space, isl_dim_param);
2307 if (n_nested > n_space)
2308 nested = isl_space_drop_dims(nested, isl_dim_param,
2309 n_space, n_nested - n_space);
2310 if (!nested)
2311 goto error;
2312 space->nested[1] = nested;
2314 isl_space_free(model);
2315 return space;
2316 error:
2317 isl_space_free(model);
2318 isl_space_free(space);
2319 return NULL;
2322 /* Compare the "type" dimensions of two isl_spaces.
2324 * The order is fairly arbitrary.
2326 static int isl_space_cmp_type(__isl_keep isl_space *space1,
2327 __isl_keep isl_space *space2, enum isl_dim_type type)
2329 int cmp;
2330 isl_space *nested1, *nested2;
2332 if (isl_space_dim(space1, type) != isl_space_dim(space2, type))
2333 return isl_space_dim(space1, type) -
2334 isl_space_dim(space2, type);
2336 cmp = isl_id_cmp(tuple_id(space1, type), tuple_id(space2, type));
2337 if (cmp != 0)
2338 return cmp;
2340 nested1 = nested(space1, type);
2341 nested2 = nested(space2, type);
2342 if (!nested1 != !nested2)
2343 return !nested1 - !nested2;
2345 if (nested1)
2346 return isl_space_cmp(nested1, nested2);
2348 return 0;
2351 /* Compare two isl_spaces.
2353 * The order is fairly arbitrary.
2355 int isl_space_cmp(__isl_keep isl_space *space1, __isl_keep isl_space *space2)
2357 int i;
2358 int cmp;
2360 if (space1 == space2)
2361 return 0;
2362 if (!space1)
2363 return -1;
2364 if (!space2)
2365 return 1;
2367 cmp = isl_space_cmp_type(space1, space2, isl_dim_param);
2368 if (cmp != 0)
2369 return cmp;
2370 cmp = isl_space_cmp_type(space1, space2, isl_dim_in);
2371 if (cmp != 0)
2372 return cmp;
2373 cmp = isl_space_cmp_type(space1, space2, isl_dim_out);
2374 if (cmp != 0)
2375 return cmp;
2377 if (!space1->ids && !space2->ids)
2378 return 0;
2380 for (i = 0; i < n(space1, isl_dim_param); ++i) {
2381 cmp = isl_id_cmp(get_id(space1, isl_dim_param, i),
2382 get_id(space2, isl_dim_param, i));
2383 if (cmp != 0)
2384 return cmp;
2387 return 0;