extract out shared isl_pw_*_un_op
[isl.git] / isl_pw_insert_dims_templ.c
blob91bb5fb02c118b7b8cff251637eef1e11b986cca
1 /*
2 * Copyright 2010 INRIA Saclay
4 * Use of this software is governed by the MIT license
6 * Written by Sven Verdoolaege, INRIA Saclay - Ile-de-France,
7 * Parc Club Orsay Universite, ZAC des vignes, 4 rue Jacques Monod,
8 * 91893 Orsay, France
9 */
11 __isl_give PW *FN(PW,insert_dims)(__isl_take PW *pw, enum isl_dim_type type,
12 unsigned first, unsigned n)
14 int i;
15 isl_size n_piece;
16 enum isl_dim_type set_type;
17 isl_space *space;
19 n_piece = FN(PW,n_piece)(pw);
20 if (n_piece < 0)
21 return FN(PW,free)(pw);
22 if (n == 0 && !isl_space_is_named_or_nested(pw->dim, type))
23 return pw;
25 set_type = type == isl_dim_in ? isl_dim_set : type;
27 space = FN(PW,take_space)(pw);
28 space = isl_space_insert_dims(space, type, first, n);
29 pw = FN(PW,restore_space)(pw, space);
31 pw = FN(PW,cow)(pw);
32 if (!pw)
33 return NULL;
35 for (i = 0; i < n_piece; ++i) {
36 EL *el;
38 pw->p[i].set = isl_set_insert_dims(pw->p[i].set,
39 set_type, first, n);
40 if (!pw->p[i].set)
41 goto error;
42 el = FN(PW,take_base_at)(pw, i);
43 el = FN(EL,insert_dims)(el, type, first, n);
44 pw = FN(PW,restore_base_at)(pw, i, el);
45 if (!pw)
46 return NULL;
49 return pw;
50 error:
51 FN(PW,free)(pw);
52 return NULL;
55 __isl_give PW *FN(PW,add_dims)(__isl_take PW *pw, enum isl_dim_type type,
56 unsigned n)
58 isl_size pos;
60 pos = FN(PW,dim)(pw, type);
61 if (pos < 0)
62 return FN(PW,free)(pw);
64 return FN(PW,insert_dims)(pw, type, pos, n);