iscc: add affine hull operation
[barvinok/uuh.git] / isl_obj_list.c
blob9c020ad6f1d75556f4ee8894c5a1c34be3c81642
1 #include <isl_obj_list.h>
3 struct isl_list *isl_list_alloc(struct isl_ctx *ctx, int n)
5 int i;
6 struct isl_list *list;
8 isl_assert(ctx, n >= 0, return NULL);
9 list = isl_alloc(ctx, struct isl_list,
10 sizeof(struct isl_list) +
11 (n - 1) * sizeof(struct isl_obj));
12 if (!list)
13 return NULL;
15 list->ctx = ctx;
16 isl_ctx_ref(list->ctx);
17 list->ref = 1;
18 list->n = n;
20 for (i = 0; i < n; ++i) {
21 list->obj[i].type = isl_obj_none;
22 list->obj[i].v = NULL;
25 return list;
28 struct isl_list *isl_list_copy(struct isl_list *list)
30 if (!list)
31 return NULL;
33 list->ref++;
34 return list;
37 void isl_list_free(struct isl_list *list)
39 int i;
41 if (!list)
42 return;
44 if (--list->ref > 0)
45 return;
47 for (i = 0; i < list->n; ++i)
48 list->obj[i].type->free(list->obj[i].v);
49 isl_ctx_deref(list->ctx);
50 free(list);
53 static void *isl_obj_list_copy(void *v)
55 return isl_list_copy((struct isl_list *)v);
58 static void isl_obj_list_free(void *v)
60 isl_list_free((struct isl_list *)v);
63 static void isl_obj_list_print(void *v, FILE *out)
65 struct isl_list *list = (struct isl_list *)v;
66 int i;
68 fprintf(out, "(");
69 for (i = 0; i < list->n; ++i) {
70 if (i)
71 fprintf(out, ", ");
72 list->obj[i].type->print(list->obj[i].v, out);
74 fprintf(out, ")");
77 static void *isl_obj_list_add(void *v1, void *v2)
79 int i;
80 struct isl_list *list1 = (struct isl_list *)v1;
81 struct isl_list *list2 = (struct isl_list *)v2;
82 struct isl_list *list;
84 if (!list1 || !list2)
85 goto error;
87 list = isl_list_alloc(list1->ctx, list1->n + list2->n);
88 if (!list)
89 goto error;
91 for (i = 0; i < list1->n; ++i) {
92 list->obj[i].type = list1->obj[i].type;
93 list->obj[i].v = list1->obj[i].type->copy(list1->obj[i].v);
96 for (i = 0; i < list2->n; ++i) {
97 list->obj[list1->n + i].type = list2->obj[i].type;
98 list->obj[list1->n + i].v = list2->obj[i].type->copy(list2->obj[i].v);
101 isl_list_free(list1);
102 isl_list_free(list2);
104 return list;
105 error:
106 isl_list_free(list1);
107 isl_list_free(list2);
108 return NULL;
111 struct isl_obj_vtable isl_obj_list_vtable = {
112 isl_obj_list_copy,
113 isl_obj_list_add,
114 isl_obj_list_print,
115 isl_obj_list_free