isl_multi_*_bin_op: use isl_multi_*_get_at
[isl.git] / isl_multi_apply_templ.c
blobd8e3b038934658b17d501dc8f9cbaed2a726fdf7
1 /*
2 * Copyright 2011 Sven Verdoolaege
3 * Copyright 2012-2013 Ecole Normale Superieure
5 * Use of this software is governed by the MIT license
7 * Written by Sven Verdoolaege,
8 * Ecole Normale Superieure, 45 rue d’Ulm, 75230 Paris, France
9 */
11 #include <isl_multi_macro.h>
13 /* Transform the elements of "multi" by applying "fn" to them
14 * with extra argument "set".
16 * The parameters of "multi" and "set" are assumed to have been aligned.
18 __isl_give MULTI(BASE) *FN(FN(MULTI(BASE),apply_aligned),APPLY_DOMBASE)(
19 __isl_take MULTI(BASE) *multi, __isl_take APPLY_DOM *set,
20 __isl_give EL *(*fn)(EL *el, __isl_take APPLY_DOM *set))
22 isl_size n;
23 int i;
25 n = FN(MULTI(BASE),size)(multi);
26 if (n < 0 || !set)
27 goto error;
29 if (n == 0) {
30 FN(APPLY_DOM,free)(set);
31 return multi;
34 multi = FN(MULTI(BASE),cow)(multi);
35 if (!multi)
36 goto error;
38 for (i = 0; i < n; ++i) {
39 multi->u.p[i] = fn(multi->u.p[i], FN(APPLY_DOM,copy)(set));
40 if (!multi->u.p[i])
41 goto error;
44 FN(APPLY_DOM,free)(set);
45 return multi;
46 error:
47 FN(APPLY_DOM,free)(set);
48 FN(MULTI(BASE),free)(multi);
49 return NULL;
52 /* Transform the elements of "multi" by applying "fn" to them
53 * with extra argument "set".
55 * Align the parameters if needed and call apply_set_aligned.
57 static __isl_give MULTI(BASE) *FN(FN(MULTI(BASE),apply),APPLY_DOMBASE)(
58 __isl_take MULTI(BASE) *multi, __isl_take APPLY_DOM *set,
59 __isl_give EL *(*fn)(EL *el, __isl_take APPLY_DOM *set))
61 isl_bool aligned;
62 isl_ctx *ctx;
64 if (!multi || !set)
65 goto error;
67 aligned = FN(APPLY_DOM,space_has_equal_params)(set, multi->space);
68 if (aligned < 0)
69 goto error;
70 if (aligned)
71 return FN(FN(MULTI(BASE),apply_aligned),APPLY_DOMBASE)(multi,
72 set, fn);
73 ctx = FN(MULTI(BASE),get_ctx)(multi);
74 if (!isl_space_has_named_params(multi->space) ||
75 !isl_space_has_named_params(set->dim))
76 isl_die(ctx, isl_error_invalid,
77 "unaligned unnamed parameters", goto error);
78 multi = FN(MULTI(BASE),align_params)(multi,
79 FN(APPLY_DOM,get_space)(set));
80 set = FN(APPLY_DOM,align_params)(set, FN(MULTI(BASE),get_space)(multi));
81 return FN(FN(MULTI(BASE),apply_aligned),APPLY_DOMBASE)(multi, set, fn);
82 error:
83 FN(MULTI(BASE),free)(multi);
84 FN(APPLY_DOM,free)(set);
85 return NULL;