1/*
2 * Copyright 2012      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/space.h>
11#include <isl/local_space.h>
12
13#include <isl_multi_macro.h>
14
15/* Create a multi expression in the given space that maps each
16 * input dimension to the corresponding output dimension.
17 */
18__isl_give MULTI(BASE) *FN(MULTI(BASE),identity)(__isl_take isl_space *space)
19{
20	int i;
21	isl_size n_in, n_out;
22	isl_local_space *ls;
23	MULTI(BASE) *multi;
24
25	if (!space)
26		return NULL;
27
28	if (isl_space_is_set(space))
29		isl_die(isl_space_get_ctx(space), isl_error_invalid,
30			"expecting map space", goto error);
31
32	n_in = isl_space_dim(space, isl_dim_in);
33	n_out = isl_space_dim(space, isl_dim_out);
34	if (n_in < 0 || n_out < 0)
35		goto error;
36	if (n_in != n_out)
37		isl_die(isl_space_get_ctx(space), isl_error_invalid,
38			"number of input and output dimensions needs to be "
39			"the same", goto error);
40
41	multi = FN(MULTI(BASE),alloc)(isl_space_copy(space));
42
43	if (!n_out) {
44		isl_space_free(space);
45		return multi;
46	}
47
48	space = isl_space_domain(space);
49	ls = isl_local_space_from_space(space);
50
51	for (i = 0; i < n_out; ++i) {
52		EL *el;
53		el = FN(EL,var_on_domain)(isl_local_space_copy(ls),
54						isl_dim_set, i);
55		multi = FN(FN(MULTI(BASE),set),BASE)(multi, i, el);
56	}
57
58	isl_local_space_free(ls);
59
60	return multi;
61error:
62	isl_space_free(space);
63	return NULL;
64}
65
66/* Create a multi expression that maps elements in the given space
67 * to themselves.
68 */
69__isl_give MULTI(BASE) *FN(MULTI(BASE),identity_on_domain_space)(
70	__isl_take isl_space *space)
71{
72	return FN(MULTI(BASE),identity)(isl_space_map_from_set(space));
73}
74
75/* This function performs the same operation as
76 * isl_multi_*_identity_on_domain_space,
77 * but is considered as a function on an isl_space when exported.
78 */
79__isl_give MULTI(BASE) *FN(FN(isl_space_identity_multi,BASE),on_domain)(
80	__isl_take isl_space *space)
81{
82	return FN(MULTI(BASE),identity_on_domain_space)(space);
83}
84
85/* Create a multi expression in the same space as "multi" that maps each
86 * input dimension to the corresponding output dimension.
87 */
88__isl_give MULTI(BASE) *FN(FN(MULTI(BASE),identity_multi),BASE)(
89	__isl_take MULTI(BASE) *multi)
90{
91	isl_space *space;
92
93	space = FN(MULTI(BASE),get_space)(multi);
94	FN(MULTI(BASE),free)(multi);
95	return FN(MULTI(BASE),identity)(space);
96}
97