1/*
2 * Copyright 2014      Ecole Normale Superieure
3 *
4 * Use of this software is governed by the MIT license
5 *
6 * Written by Sven Verdoolaege,
7 * Ecole Normale Superieure, 45 rue d'Ulm, 75230 Paris, France
8 */
9
10#include <isl_multi_macro.h>
11
12/* Data structure that specifies how isl_multi_*_un_op should
13 * modify its input.
14 *
15 * If "fn_space" is set, then it is applied to the space.
16 *
17 * "fn_el" is applied to each base expression.
18 */
19S(MULTI(BASE),un_op_control) {
20	__isl_give isl_space *(*fn_space)(__isl_take isl_space *space);
21	__isl_give EL *(*fn_el)(__isl_take EL *el);
22};
23
24/* Modify "multi" based on "control".
25 */
26static __isl_give MULTI(BASE) *FN(MULTI(BASE),un_op)(
27	__isl_take MULTI(BASE) *multi, S(MULTI(BASE),un_op_control) *control)
28{
29	int i;
30	isl_size n;
31	isl_space *space;
32
33	n = FN(MULTI(BASE),size)(multi);
34	if (n < 0)
35		return FN(MULTI(BASE),free)(multi);
36
37	for (i = 0; i < n; ++i) {
38		EL *el;
39
40		el = FN(MULTI(BASE),take_at)(multi, i);
41		el = control->fn_el(el);
42		multi = FN(MULTI(BASE),restore_at)(multi, i, el);
43	}
44
45	if (!control->fn_space)
46		return multi;
47
48	space = FN(MULTI(BASE),take_space)(multi);
49	space = control->fn_space(space);
50	multi = FN(MULTI(BASE),restore_space)(multi, space);
51
52	return multi;
53}
54