2 * Copyright 2008-2009 Katholieke Universiteit Leuven
3 * Copyright 2010 INRIA Saclay
5 * Use of this software is governed by the GNU LGPLv2.1 license
7 * Written by Sven Verdoolaege, K.U.Leuven, Departement
8 * Computerwetenschappen, Celestijnenlaan 200A, B-3001 Leuven, Belgium
9 * and INRIA Saclay - Ile-de-France, Parc Club Orsay Universite,
10 * ZAC des vignes, 4 rue Jacques Monod, 91893 Orsay, France
19 #include "isl_stream.h"
20 #include "isl_map_private.h"
25 struct variable *next;
34 static struct vars *vars_new(struct isl_ctx *ctx)
37 v = isl_alloc_type(ctx, struct vars);
46 static void variable_free(struct variable *var)
49 struct variable *next = var->next;
56 static void vars_free(struct vars *v)
64 static void vars_drop(struct vars *v, int n)
75 struct variable *next = var->next;
83 static struct variable *variable_new(struct vars *v, const char *name, int len,
87 var = isl_alloc_type(v->ctx, struct variable);
90 var->name = strdup(name);
91 var->name[len] = '\0';
100 static int vars_pos(struct vars *v, const char *s, int len)
107 for (q = v->v; q; q = q->next) {
108 if (strncmp(q->name, s, len) == 0 && q->name[len] == '\0')
115 v->v = variable_new(v, s, len, v->n);
123 static __isl_give isl_basic_map *set_name(__isl_take isl_basic_map *bmap,
124 enum isl_dim_type type, unsigned pos, char *name)
133 prime = strchr(name, '\'');
136 bmap->dim = isl_dim_set_name(bmap->dim, type, pos, name);
145 isl_basic_map_free(bmap);
149 static struct isl_vec *accept_affine(struct isl_stream *s, struct vars *v)
151 struct isl_token *tok = NULL;
155 aff = isl_vec_alloc(v->ctx, 1 + v->n);
156 isl_seq_clr(aff->el, aff->size);
161 tok = isl_stream_next_token(s);
163 isl_stream_error(s, NULL, "unexpected EOF");
166 if (tok->type == ISL_TOKEN_IDENT) {
168 int pos = vars_pos(v, tok->u.s, -1);
172 isl_stream_error(s, tok, "unknown identifier");
177 isl_int_add_ui(aff->el[1 + pos],
178 aff->el[1 + pos], 1);
180 isl_int_sub_ui(aff->el[1 + pos],
181 aff->el[1 + pos], 1);
183 } else if (tok->type == ISL_TOKEN_VALUE) {
184 struct isl_token *tok2;
187 tok2 = isl_stream_next_token(s);
188 if (tok2 && tok2->type == ISL_TOKEN_IDENT) {
189 pos = vars_pos(v, tok2->u.s, -1);
193 isl_stream_error(s, tok2,
194 "unknown identifier");
196 isl_token_free(tok2);
199 isl_token_free(tok2);
201 isl_stream_push_token(s, tok2);
203 isl_int_neg(tok->u.v, tok->u.v);
204 isl_int_add(aff->el[1 + pos],
205 aff->el[1 + pos], tok->u.v);
207 } else if (tok->type == '-') {
209 } else if (tok->type == '+') {
212 isl_stream_push_token(s, tok);
224 static __isl_give isl_basic_map *read_var_list(struct isl_stream *s,
225 __isl_take isl_basic_map *bmap, enum isl_dim_type type, struct vars *v)
228 struct isl_token *tok;
230 while ((tok = isl_stream_next_token(s)) != NULL) {
233 if (tok->type == ISL_TOKEN_IDENT) {
235 int p = vars_pos(v, tok->u.s, -1);
242 bmap = isl_basic_map_add(bmap, type, 1);
243 bmap = set_name(bmap, type, i, v->v->name);
245 } else if (tok->type == ISL_TOKEN_IDENT ||
246 tok->type == ISL_TOKEN_VALUE) {
249 if (type == isl_dim_param) {
250 isl_stream_error(s, tok,
251 "expecting unique identifier");
254 isl_stream_push_token(s, tok);
256 vec = accept_affine(s, v);
259 v->v = variable_new(v, "", 0, v->n);
263 bmap = isl_basic_map_add(bmap, type, 1);
264 bmap = isl_basic_map_extend_constraints(bmap, 1, 0);
265 k = isl_basic_map_alloc_equality(bmap);
267 isl_seq_cpy(bmap->eq[k], vec->el, vec->size);
268 isl_int_set_si(bmap->eq[k][vec->size], -1);
276 tok = isl_stream_next_token(s);
277 if (!tok || tok->type != ',')
284 isl_stream_push_token(s, tok);
289 isl_basic_map_free(bmap);
293 static __isl_give isl_mat *accept_affine_list(struct isl_stream *s,
298 struct isl_token *tok = NULL;
300 vec = accept_affine(s, v);
301 mat = isl_mat_from_row_vec(vec);
306 tok = isl_stream_next_token(s);
308 isl_stream_error(s, NULL, "unexpected EOF");
311 if (tok->type != ',') {
312 isl_stream_push_token(s, tok);
317 vec = accept_affine(s, v);
318 mat = isl_mat_vec_concat(mat, vec);
329 static struct isl_basic_map *add_div_definition(struct isl_stream *s,
330 struct vars *v, struct isl_basic_map *bmap, int k)
332 struct isl_token *tok;
336 if (isl_stream_eat(s, '['))
339 tok = isl_stream_next_token(s);
342 if (tok->type == '(') {
346 isl_stream_push_token(s, tok);
348 aff = accept_affine(s, v);
352 isl_seq_cpy(bmap->div[k] + 1, aff->el, aff->size);
356 if (seen_paren && isl_stream_eat(s, ')'))
358 if (isl_stream_eat(s, '/'))
361 tok = isl_stream_next_token(s);
364 if (tok->type != ISL_TOKEN_VALUE) {
365 isl_stream_error(s, tok, "expected denominator");
366 isl_stream_push_token(s, tok);
369 isl_int_set(bmap->div[k][0], tok->u.v);
372 if (isl_stream_eat(s, ']'))
375 if (isl_basic_map_add_div_constraints(bmap, k) < 0)
380 isl_basic_map_free(bmap);
384 static struct isl_basic_map *read_defined_var_list(struct isl_stream *s,
385 struct vars *v, struct isl_basic_map *bmap)
387 struct isl_token *tok;
389 while ((tok = isl_stream_next_token(s)) != NULL) {
393 unsigned total = isl_basic_map_total_dim(bmap);
395 if (tok->type != ISL_TOKEN_IDENT)
398 p = vars_pos(v, tok->u.s, -1);
402 isl_stream_error(s, tok, "expecting unique identifier");
407 bmap = isl_basic_map_cow(bmap);
408 bmap = isl_basic_map_extend_dim(bmap, isl_dim_copy(bmap->dim),
411 if ((k = isl_basic_map_alloc_div(bmap)) < 0)
413 isl_seq_clr(bmap->div[k], 1 + 1 + total);
415 tok = isl_stream_next_token(s);
416 if (tok && tok->type == '=') {
418 bmap = add_div_definition(s, v, bmap, k);
419 tok = isl_stream_next_token(s);
422 if (!tok || tok->type != ',')
428 isl_stream_push_token(s, tok);
433 isl_basic_map_free(bmap);
437 static __isl_give isl_basic_map *read_tuple(struct isl_stream *s,
438 __isl_take isl_basic_map *bmap, enum isl_dim_type type, struct vars *v)
440 struct isl_token *tok;
442 tok = isl_stream_next_token(s);
443 if (!tok || tok->type != '[') {
444 isl_stream_error(s, tok, "expecting '['");
448 bmap = read_var_list(s, bmap, type, v);
449 tok = isl_stream_next_token(s);
450 if (!tok || tok->type != ']') {
451 isl_stream_error(s, tok, "expecting ']'");
460 isl_basic_map_free(bmap);
464 static struct isl_basic_map *add_constraints(struct isl_stream *s,
465 struct vars *v, struct isl_basic_map *bmap);
467 static struct isl_basic_map *add_exists(struct isl_stream *s,
468 struct vars *v, struct isl_basic_map *bmap)
470 struct isl_token *tok;
477 tok = isl_stream_next_token(s);
480 if (tok->type == '(') {
484 isl_stream_push_token(s, tok);
486 bmap = read_defined_var_list(s, v, bmap);
490 if (isl_stream_eat(s, ':'))
492 bmap = add_constraints(s, v, bmap);
493 if (seen_paren && isl_stream_eat(s, ')'))
497 isl_basic_map_free(bmap);
501 static __isl_give isl_basic_map *construct_constraint(
502 __isl_take isl_basic_map *bmap, enum isl_token_type type,
503 isl_int *left, isl_int *right)
511 len = 1 + isl_basic_map_total_dim(bmap);
514 k = isl_basic_map_alloc_inequality(bmap);
517 if (type == ISL_TOKEN_LE)
518 isl_seq_combine(bmap->ineq[k], ctx->negone, left,
521 else if (type == ISL_TOKEN_GE)
522 isl_seq_combine(bmap->ineq[k], ctx->one, left,
525 else if (type == ISL_TOKEN_LT) {
526 isl_seq_combine(bmap->ineq[k], ctx->negone, left,
529 isl_int_sub_ui(bmap->ineq[k][0], bmap->ineq[k][0], 1);
530 } else if (type == ISL_TOKEN_GT) {
531 isl_seq_combine(bmap->ineq[k], ctx->one, left,
534 isl_int_sub_ui(bmap->ineq[k][0], bmap->ineq[k][0], 1);
536 isl_seq_combine(bmap->ineq[k], ctx->one, left,
539 isl_basic_map_inequality_to_equality(bmap, k);
544 isl_basic_map_free(bmap);
548 static int is_comparator(struct isl_token *tok)
565 static struct isl_basic_map *add_constraint(struct isl_stream *s,
566 struct vars *v, struct isl_basic_map *bmap)
569 unsigned total = isl_basic_map_total_dim(bmap);
570 struct isl_token *tok = NULL;
571 struct isl_mat *aff1 = NULL, *aff2 = NULL;
573 tok = isl_stream_next_token(s);
576 if (tok->type == ISL_TOKEN_EXISTS) {
578 return add_exists(s, v, bmap);
580 isl_stream_push_token(s, tok);
583 bmap = isl_basic_map_cow(bmap);
585 aff1 = accept_affine_list(s, v);
588 tok = isl_stream_next_token(s);
589 if (!is_comparator(tok)) {
590 isl_stream_error(s, tok, "missing operator");
592 isl_stream_push_token(s, tok);
596 isl_assert(aff1->ctx, aff1->n_col == 1 + total, goto error);
598 aff2 = accept_affine_list(s, v);
601 isl_assert(aff2->ctx, aff2->n_col == 1 + total, goto error);
603 bmap = isl_basic_map_extend_constraints(bmap, 0,
604 aff1->n_row * aff2->n_row);
605 for (i = 0; i < aff1->n_row; ++i)
606 for (j = 0; j < aff2->n_row; ++j)
607 bmap = construct_constraint(bmap, tok->type,
608 aff1->row[i], aff2->row[j]);
613 tok = isl_stream_next_token(s);
614 if (!is_comparator(tok)) {
616 isl_stream_push_token(s, tok);
628 isl_basic_map_free(bmap);
632 static struct isl_basic_map *add_constraints(struct isl_stream *s,
633 struct vars *v, struct isl_basic_map *bmap)
635 struct isl_token *tok;
638 bmap = add_constraint(s, v, bmap);
641 tok = isl_stream_next_token(s);
643 isl_stream_error(s, NULL, "unexpected EOF");
646 if (tok->type != ISL_TOKEN_AND)
650 isl_stream_push_token(s, tok);
656 isl_basic_map_free(bmap);
660 static struct isl_basic_map *read_disjunct(struct isl_stream *s,
661 struct vars *v, __isl_take isl_dim *dim)
664 struct isl_token *tok;
665 struct isl_basic_map *bmap;
667 bmap = isl_basic_map_alloc_dim(dim, 0, 0, 0);
671 tok = isl_stream_next_token(s);
674 if (tok->type == '(') {
678 isl_stream_push_token(s, tok);
680 bmap = add_constraints(s, v, bmap);
681 bmap = isl_basic_map_simplify(bmap);
682 bmap = isl_basic_map_finalize(bmap);
684 if (seen_paren && isl_stream_eat(s, ')'))
689 isl_basic_map_free(bmap);
693 static struct isl_map *read_disjuncts(struct isl_stream *s,
694 struct vars *v, __isl_take isl_dim *dim)
696 struct isl_token *tok;
699 tok = isl_stream_next_token(s);
701 isl_stream_error(s, NULL, "unexpected EOF");
704 if (tok->type == '}') {
705 isl_stream_push_token(s, tok);
706 return isl_map_universe(dim);
708 isl_stream_push_token(s, tok);
710 map = isl_map_empty(isl_dim_copy(dim));
712 struct isl_basic_map *bmap;
715 bmap = read_disjunct(s, v, isl_dim_copy(dim));
716 map = isl_map_union(map, isl_map_from_basic_map(bmap));
718 vars_drop(v, v->n - n);
720 tok = isl_stream_next_token(s);
721 if (!tok || tok->type != ISL_TOKEN_OR)
726 isl_stream_push_token(s, tok);
735 static int polylib_pos_to_isl_pos(__isl_keep isl_basic_map *bmap, int pos)
737 if (pos < isl_basic_map_dim(bmap, isl_dim_out))
738 return 1 + isl_basic_map_dim(bmap, isl_dim_param) +
739 isl_basic_map_dim(bmap, isl_dim_in) + pos;
740 pos -= isl_basic_map_dim(bmap, isl_dim_out);
742 if (pos < isl_basic_map_dim(bmap, isl_dim_in))
743 return 1 + isl_basic_map_dim(bmap, isl_dim_param) + pos;
744 pos -= isl_basic_map_dim(bmap, isl_dim_in);
746 if (pos < isl_basic_map_dim(bmap, isl_dim_div))
747 return 1 + isl_basic_map_dim(bmap, isl_dim_param) +
748 isl_basic_map_dim(bmap, isl_dim_in) +
749 isl_basic_map_dim(bmap, isl_dim_out) + pos;
750 pos -= isl_basic_map_dim(bmap, isl_dim_div);
752 if (pos < isl_basic_map_dim(bmap, isl_dim_param))
758 static __isl_give isl_basic_map *basic_map_read_polylib_constraint(
759 struct isl_stream *s, __isl_take isl_basic_map *bmap)
762 struct isl_token *tok;
772 nparam = isl_basic_map_dim(bmap, isl_dim_param);
773 dim = isl_basic_map_dim(bmap, isl_dim_out);
775 tok = isl_stream_next_token(s);
776 if (!tok || tok->type != ISL_TOKEN_VALUE) {
777 isl_stream_error(s, tok, "expecting coefficient");
779 isl_stream_push_token(s, tok);
782 if (!tok->on_new_line) {
783 isl_stream_error(s, tok, "coefficient should appear on new line");
784 isl_stream_push_token(s, tok);
788 type = isl_int_get_si(tok->u.v);
791 isl_assert(s->ctx, type == 0 || type == 1, goto error);
793 k = isl_basic_map_alloc_equality(bmap);
796 k = isl_basic_map_alloc_inequality(bmap);
802 for (j = 0; j < 1 + isl_basic_map_total_dim(bmap); ++j) {
804 tok = isl_stream_next_token(s);
805 if (!tok || tok->type != ISL_TOKEN_VALUE) {
806 isl_stream_error(s, tok, "expecting coefficient");
808 isl_stream_push_token(s, tok);
811 if (tok->on_new_line) {
812 isl_stream_error(s, tok,
813 "coefficient should not appear on new line");
814 isl_stream_push_token(s, tok);
817 pos = polylib_pos_to_isl_pos(bmap, j);
818 isl_int_set(c[pos], tok->u.v);
824 isl_basic_map_free(bmap);
828 static __isl_give isl_basic_map *basic_map_read_polylib(struct isl_stream *s,
832 struct isl_token *tok;
833 struct isl_token *tok2;
836 unsigned in = 0, out, local = 0;
837 struct isl_basic_map *bmap = NULL;
842 tok = isl_stream_next_token(s);
844 isl_stream_error(s, NULL, "unexpected EOF");
847 tok2 = isl_stream_next_token(s);
850 isl_stream_error(s, NULL, "unexpected EOF");
853 n_row = isl_int_get_si(tok->u.v);
854 n_col = isl_int_get_si(tok2->u.v);
855 on_new_line = tok2->on_new_line;
856 isl_token_free(tok2);
858 isl_assert(s->ctx, !on_new_line, return NULL);
859 isl_assert(s->ctx, n_row >= 0, return NULL);
860 isl_assert(s->ctx, n_col >= 2 + nparam, return NULL);
861 tok = isl_stream_next_token_on_same_line(s);
863 if (tok->type != ISL_TOKEN_VALUE) {
864 isl_stream_error(s, tok,
865 "expecting number of output dimensions");
866 isl_stream_push_token(s, tok);
869 out = isl_int_get_si(tok->u.v);
872 tok = isl_stream_next_token_on_same_line(s);
873 if (!tok || tok->type != ISL_TOKEN_VALUE) {
874 isl_stream_error(s, tok,
875 "expecting number of input dimensions");
877 isl_stream_push_token(s, tok);
880 in = isl_int_get_si(tok->u.v);
883 tok = isl_stream_next_token_on_same_line(s);
884 if (!tok || tok->type != ISL_TOKEN_VALUE) {
885 isl_stream_error(s, tok,
886 "expecting number of existentials");
888 isl_stream_push_token(s, tok);
891 local = isl_int_get_si(tok->u.v);
894 tok = isl_stream_next_token_on_same_line(s);
895 if (!tok || tok->type != ISL_TOKEN_VALUE) {
896 isl_stream_error(s, tok,
897 "expecting number of parameters");
899 isl_stream_push_token(s, tok);
902 nparam = isl_int_get_si(tok->u.v);
904 if (n_col != 1 + out + in + local + nparam + 1) {
905 isl_stream_error(s, NULL,
906 "dimensions don't match");
910 out = n_col - 2 - nparam;
911 bmap = isl_basic_map_alloc(s->ctx, nparam, in, out, local, n_row, n_row);
915 for (i = 0; i < local; ++i) {
916 int k = isl_basic_map_alloc_div(bmap);
921 for (i = 0; i < n_row; ++i)
922 bmap = basic_map_read_polylib_constraint(s, bmap);
924 tok = isl_stream_next_token_on_same_line(s);
926 isl_stream_error(s, tok, "unexpected extra token on line");
927 isl_stream_push_token(s, tok);
931 bmap = isl_basic_map_simplify(bmap);
932 bmap = isl_basic_map_finalize(bmap);
935 isl_basic_map_free(bmap);
939 static struct isl_map *map_read_polylib(struct isl_stream *s, int nparam)
941 struct isl_token *tok;
942 struct isl_token *tok2;
946 tok = isl_stream_next_token(s);
948 isl_stream_error(s, NULL, "unexpected EOF");
951 tok2 = isl_stream_next_token_on_same_line(s);
953 isl_stream_push_token(s, tok2);
954 isl_stream_push_token(s, tok);
955 return isl_map_from_basic_map(basic_map_read_polylib(s, nparam));
957 n = isl_int_get_si(tok->u.v);
960 isl_assert(s->ctx, n >= 1, return NULL);
962 map = isl_map_from_basic_map(basic_map_read_polylib(s, nparam));
964 for (i = 1; i < n; ++i)
965 map = isl_map_union(map,
966 isl_map_from_basic_map(basic_map_read_polylib(s, nparam)));
971 static struct isl_map *map_read_body(struct isl_stream *s,
972 __isl_take isl_basic_map *bmap, struct vars *v)
974 struct isl_map *map = NULL;
975 struct isl_token *tok;
978 bmap = read_tuple(s, bmap, isl_dim_in, v);
981 tok = isl_stream_next_token(s);
982 if (tok && tok->type == ISL_TOKEN_TO) {
984 bmap = read_tuple(s, bmap, isl_dim_out, v);
988 bmap = isl_basic_map_reverse(bmap);
990 isl_stream_push_token(s, tok);
992 tok = isl_stream_next_token(s);
994 isl_stream_error(s, NULL, "unexpected EOF");
997 map = isl_map_from_basic_map(isl_basic_map_copy(bmap));
998 if (tok->type == ':') {
1000 map = isl_map_intersect(map,
1001 read_disjuncts(s, v, isl_basic_map_get_dim(bmap)));
1003 isl_stream_push_token(s, tok);
1005 vars_drop(v, v->n - n);
1007 isl_basic_map_free(bmap);
1010 isl_basic_map_free(bmap);
1014 static struct isl_map *map_read(struct isl_stream *s, int nparam)
1016 struct isl_basic_map *bmap = NULL;
1017 struct isl_map *map = NULL;
1018 struct isl_token *tok;
1019 struct vars *v = NULL;
1021 tok = isl_stream_next_token(s);
1023 isl_stream_error(s, NULL, "unexpected EOF");
1026 if (tok->type == ISL_TOKEN_VALUE) {
1027 isl_stream_push_token(s, tok);
1028 return map_read_polylib(s, nparam);
1030 v = vars_new(s->ctx);
1031 bmap = isl_basic_map_alloc(s->ctx, 0, 0, 0, 0, 0, 0);
1032 if (tok->type == '[') {
1033 isl_stream_push_token(s, tok);
1034 bmap = read_tuple(s, bmap, isl_dim_param, v);
1038 isl_assert(s->ctx, nparam == v->n, goto error);
1039 tok = isl_stream_next_token(s);
1040 if (!tok || tok->type != ISL_TOKEN_TO) {
1041 isl_stream_error(s, tok, "expecting '->'");
1043 isl_stream_push_token(s, tok);
1046 isl_token_free(tok);
1047 tok = isl_stream_next_token(s);
1049 bmap = isl_basic_map_add(bmap, isl_dim_param, nparam);
1050 if (!tok || tok->type != '{') {
1051 isl_stream_error(s, tok, "expecting '{'");
1053 isl_stream_push_token(s, tok);
1056 isl_token_free(tok);
1059 isl_map *m = map_read_body(s, isl_basic_map_copy(bmap), v);
1063 map = isl_map_union(map, m);
1066 tok = isl_stream_next_token(s);
1067 if (!tok || tok->type != ';')
1069 isl_token_free(tok);
1072 if (tok && tok->type == '}') {
1073 isl_token_free(tok);
1075 isl_stream_error(s, tok, "unexpected isl_token");
1077 isl_token_free(tok);
1081 isl_basic_map_free(bmap);
1085 isl_basic_map_free(bmap);
1092 static struct isl_basic_map *basic_map_read(struct isl_stream *s, int nparam)
1094 struct isl_map *map;
1095 struct isl_basic_map *bmap;
1097 map = map_read(s, nparam);
1101 isl_assert(map->ctx, map->n <= 1, goto error);
1104 bmap = isl_basic_map_empty_like_map(map);
1106 bmap = isl_basic_map_copy(map->p[0]);
1116 __isl_give isl_basic_map *isl_basic_map_read_from_file(isl_ctx *ctx,
1117 FILE *input, int nparam)
1119 struct isl_basic_map *bmap;
1120 struct isl_stream *s = isl_stream_new_file(ctx, input);
1123 bmap = basic_map_read(s, nparam);
1128 __isl_give isl_basic_set *isl_basic_set_read_from_file(isl_ctx *ctx,
1129 FILE *input, int nparam)
1131 struct isl_basic_map *bmap;
1132 bmap = isl_basic_map_read_from_file(ctx, input, nparam);
1135 isl_assert(ctx, isl_basic_map_n_in(bmap) == 0, goto error);
1136 return (struct isl_basic_set *)bmap;
1138 isl_basic_map_free(bmap);
1142 struct isl_basic_map *isl_basic_map_read_from_str(struct isl_ctx *ctx,
1143 const char *str, int nparam)
1145 struct isl_basic_map *bmap;
1146 struct isl_stream *s = isl_stream_new_str(ctx, str);
1149 bmap = basic_map_read(s, nparam);
1154 struct isl_basic_set *isl_basic_set_read_from_str(struct isl_ctx *ctx,
1155 const char *str, int nparam)
1157 struct isl_basic_map *bmap;
1158 bmap = isl_basic_map_read_from_str(ctx, str, nparam);
1161 isl_assert(ctx, isl_basic_map_n_in(bmap) == 0, goto error);
1162 return (struct isl_basic_set *)bmap;
1164 isl_basic_map_free(bmap);
1168 __isl_give isl_map *isl_map_read_from_file(struct isl_ctx *ctx,
1169 FILE *input, int nparam)
1171 struct isl_map *map;
1172 struct isl_stream *s = isl_stream_new_file(ctx, input);
1175 map = map_read(s, nparam);
1180 __isl_give isl_map *isl_map_read_from_str(struct isl_ctx *ctx,
1181 const char *str, int nparam)
1183 struct isl_map *map;
1184 struct isl_stream *s = isl_stream_new_str(ctx, str);
1187 map = map_read(s, nparam);
1192 __isl_give isl_set *isl_set_read_from_file(struct isl_ctx *ctx,
1193 FILE *input, int nparam)
1195 struct isl_map *map;
1196 map = isl_map_read_from_file(ctx, input, nparam);
1199 isl_assert(ctx, isl_map_n_in(map) == 0, goto error);
1200 return (struct isl_set *)map;
1206 struct isl_set *isl_set_read_from_str(struct isl_ctx *ctx,
1207 const char *str, int nparam)
1209 struct isl_map *map;
1210 map = isl_map_read_from_str(ctx, str, nparam);
1213 isl_assert(ctx, isl_map_n_in(map) == 0, goto error);
1214 return (struct isl_set *)map;
1220 static char *next_line(FILE *input, char *line, unsigned len)
1225 if (!(p = fgets(line, len, input)))
1227 while (isspace(*p) && *p != '\n')
1229 } while (*p == '#' || *p == '\n');
1234 static struct isl_vec *isl_vec_read_from_file_polylib(struct isl_ctx *ctx,
1237 struct isl_vec *vec = NULL;
1246 isl_assert(ctx, next_line(input, line, sizeof(line)), return NULL);
1247 isl_assert(ctx, sscanf(line, "%u", &size) == 1, return NULL);
1249 vec = isl_vec_alloc(ctx, size);
1251 p = next_line(input, line, sizeof(line));
1252 isl_assert(ctx, p, goto error);
1254 for (j = 0; j < size; ++j) {
1255 n = sscanf(p, "%s%n", val, &offset);
1256 isl_assert(ctx, n != 0, goto error);
1257 isl_int_read(vec->el[j], val);
1267 struct isl_vec *isl_vec_read_from_file(struct isl_ctx *ctx,
1268 FILE *input, unsigned input_format)
1270 if (input_format == ISL_FORMAT_POLYLIB)
1271 return isl_vec_read_from_file_polylib(ctx, input);
1273 isl_assert(ctx, 0, return NULL);