Merge branch 'maint'
[isl.git] / isl_multi_bin_val_templ.c
bloba7a3913da9b119177c2b3455b83ec0ea5f475fe9
1 /*
2 * Copyright 2013 Ecole Normale Superieure
4 * Use of this software is governed by the MIT license
6 * Written by Sven Verdoolaege,
7 * Ecole Normale Superieure, 45 rue d'Ulm, 75230 Paris, France
8 */
10 /* Apply "fn" to each of the elements of "multi" with as second argument "v".
12 static __isl_give MULTI(BASE) *FN(MULTI(BASE),fn_val)(
13 __isl_take MULTI(BASE) *multi,
14 __isl_give EL *(*fn)(__isl_take EL *el, __isl_take isl_val *v),
15 __isl_take isl_val *v)
17 isl_size n;
18 int i;
20 n = FN(MULTI(BASE),size)(multi);
21 if (n < 0 || !v)
22 goto error;
24 for (i = 0; i < n; ++i) {
25 EL *el;
27 el = FN(MULTI(BASE),take_at)(multi, i);
28 el = fn(el, isl_val_copy(v));
29 multi = FN(MULTI(BASE),restore_at)(multi, i, el);
32 isl_val_free(v);
33 return multi;
34 error:
35 isl_val_free(v);
36 FN(MULTI(BASE),free)(multi);
37 return NULL;
40 #undef TYPE
41 #define TYPE MULTI(BASE)
42 #include "isl_type_check_match_range_multi_val.c"
44 /* Elementwise apply "fn" to "multi" and "mv".
46 static __isl_give MULTI(BASE) *FN(MULTI(BASE),fn_multi_val)(
47 __isl_take MULTI(BASE) *multi,
48 __isl_give EL *(*fn)(__isl_take EL *el, __isl_take isl_val *v),
49 __isl_take isl_multi_val *mv)
51 isl_size n;
52 int i;
54 n = FN(MULTI(BASE),size)(multi);
55 if (n < 0 || FN(MULTI(BASE),check_match_range_multi_val)(multi, mv) < 0)
56 goto error;
58 for (i = 0; i < n; ++i) {
59 isl_val *v;
60 EL *el;
62 v = isl_multi_val_get_val(mv, i);
63 el = FN(MULTI(BASE),take_at)(multi, i);
64 el = fn(el, v);
65 multi = FN(MULTI(BASE),restore_at)(multi, i, el);
68 isl_multi_val_free(mv);
69 return multi;
70 error:
71 isl_multi_val_free(mv);
72 return FN(MULTI(BASE),free)(multi);