generalize isl_basic_set_list to generic lists
authorSven Verdoolaege <skimo@kotnet.org>
Sat, 16 Apr 2011 12:40:23 +0000 (14:40 +0200)
committerSven Verdoolaege <skimo@kotnet.org>
Sat, 4 Jun 2011 11:19:22 +0000 (13:19 +0200)
Signed-off-by: Sven Verdoolaege <skimo@kotnet.org>
Makefile.am
doc/user.pod
include/isl/list.h
isl_list.c
isl_list_private.h [new file with mode: 0644]
isl_list_templ.c [new file with mode: 0644]
isl_list_templ.h [new file with mode: 0644]
isl_map.c
isl_map_private.h

index 71e98d8..b724eb1 100644 (file)
@@ -70,6 +70,7 @@ libisl_la_SOURCES = \
        isl_ilp.c \
        isl_input.c \
        isl_list.c \
+       isl_list_private.h \
        isl_local_space_private.h \
        isl_local_space.c \
        isl_lp.c \
@@ -216,6 +217,8 @@ pkginclude_HEADERS = \
 
 EXTRA_DIST = \
        basis_reduction_templ.c \
+       isl_list_templ.c \
+       isl_list_templ.h \
        isl_pw_templ.c \
        isl_union_templ.c \
        isl.py \
index 72f93fd..2002615 100644 (file)
@@ -2001,6 +2001,23 @@ In case of union relations, the optimum is computed per space.
        __isl_give isl_union_map *isl_union_map_lexmax(
                __isl_take isl_union_map *umap);
 
+=head2 Lists
+
+Lists are defined over several element types, including
+C<isl_basic_set> and C<isl_set>.
+Here we take lists of C<isl_set>s as an example.
+
+       #include <isl/list.h>
+       __isl_give isl_set_list *isl_set_list_alloc(
+               isl_ctx *ctx, int n);
+       __isl_give isl_set_list *isl_set_list_add(
+               __isl_take isl_set_list *list,
+               __isl_take isl_set *el);
+       void isl_set_list_free(__isl_take isl_set_list *list);
+
+C<isl_set_list_alloc> creates an empty list with a capacity for
+C<n> elements.
+
 =head2 Matrices
 
 Matrices can be created, copied and freed using the following functions.
index caef4c9..dba1294 100644 (file)
 extern "C" {
 #endif
 
-struct isl_basic_set;
-
-struct isl_basic_set_list {
-       int ref;
-       struct isl_ctx *ctx;
-
-       int n;
-
-       size_t size;
-       struct isl_basic_set *p[1];
-};
-
-struct isl_basic_set_list *isl_basic_set_list_alloc(struct isl_ctx *ctx, int n);
-void isl_basic_set_list_free(struct isl_basic_set_list *list);
-struct isl_basic_set_list *isl_basic_set_list_add(
-       struct isl_basic_set_list *list,
-       struct isl_basic_set *bset);
+#define ISL_DECLARE_LIST(EL)                                           \
+struct isl_##EL;                                                       \
+struct isl_##EL##_list;                                                        \
+typedef struct isl_##EL##_list isl_##EL##_list;                                \
+__isl_give isl_##EL##_list *isl_##EL##_list_alloc(isl_ctx *ctx, int n);        \
+void isl_##EL##_list_free(__isl_take isl_##EL##_list *list);           \
+__isl_give isl_##EL##_list *isl_##EL##_list_add(                       \
+       __isl_take isl_##EL##_list *list,                               \
+       __isl_take struct isl_##EL *el);
+
+ISL_DECLARE_LIST(basic_set)
+ISL_DECLARE_LIST(set)
 
 #if defined(__cplusplus)
 }
index db2a608..c6aeffc 100644 (file)
@@ -7,56 +7,15 @@
  * Computerwetenschappen, Celestijnenlaan 200A, B-3001 Leuven, Belgium
  */
 
-#include <isl/list.h>
+#include <isl_list_private.h>
 #include <isl/set.h>
 
-struct isl_basic_set_list *isl_basic_set_list_alloc(struct isl_ctx *ctx, int n)
-{
-       struct isl_basic_set_list *list;
+#undef BASE
+#define BASE basic_set
 
-       isl_assert(ctx, n >= 0, return NULL);
-       list = isl_alloc(ctx, struct isl_basic_set_list,
-                        sizeof(struct isl_basic_set_list) +
-                        (n - 1) * sizeof(struct isl_basic_set *));
-       if (!list)
-               return NULL;
+#include <isl_list_templ.c>
 
-       list->ctx = ctx;
-       isl_ctx_ref(ctx);
-       list->ref = 1;
-       list->size = n;
-       list->n = 0;
-       return list;
-}
+#undef BASE
+#define BASE set
 
-struct isl_basic_set_list *isl_basic_set_list_add(
-       struct isl_basic_set_list *list,
-       struct isl_basic_set *bset)
-{
-       if (!list || !bset)
-               goto error;
-       isl_assert(list->ctx, list->n < list->size, goto error);
-       list->p[list->n] = bset;
-       list->n++;
-       return list;
-error:
-       isl_basic_set_free(bset);
-       isl_basic_set_list_free(list);
-       return NULL;
-}
-
-void isl_basic_set_list_free(struct isl_basic_set_list *list)
-{
-       int i;
-
-       if (!list)
-               return;
-
-       if (--list->ref > 0)
-               return;
-
-       isl_ctx_deref(list->ctx);
-       for (i = 0; i < list->n; ++i)
-               isl_basic_set_free(list->p[i]);
-       free(list);
-}
+#include <isl_list_templ.c>
diff --git a/isl_list_private.h b/isl_list_private.h
new file mode 100644 (file)
index 0000000..3b13b06
--- /dev/null
@@ -0,0 +1,16 @@
+#ifndef ISL_LIST_PRIVATE_H
+#define ISL_LIST_PRIVATE_H
+
+#include <isl/list.h>
+
+#undef EL
+#define EL isl_basic_set
+
+#include <isl_list_templ.h>
+
+#undef EL
+#define EL isl_set
+
+#include <isl_list_templ.h>
+
+#endif
diff --git a/isl_list_templ.c b/isl_list_templ.c
new file mode 100644 (file)
index 0000000..0f901b3
--- /dev/null
@@ -0,0 +1,72 @@
+/*
+ * Copyright 2008-2009 Katholieke Universiteit Leuven
+ * Copyright 2011      INRIA Saclay
+ *
+ * Use of this software is governed by the GNU LGPLv2.1 license
+ *
+ * Written by Sven Verdoolaege, K.U.Leuven, Departement
+ * Computerwetenschappen, Celestijnenlaan 200A, B-3001 Leuven, Belgium
+ * and INRIA Saclay - Ile-de-France, Parc Club Orsay Universite,
+ * ZAC des vignes, 4 rue Jacques Monod, 91893 Orsay, France
+ */
+
+#define xCAT(A,B) A ## B
+#define CAT(A,B) xCAT(A,B)
+#undef EL
+#define EL CAT(isl_,BASE)
+#define xFN(TYPE,NAME) TYPE ## _ ## NAME
+#define FN(TYPE,NAME) xFN(TYPE,NAME)
+#define xLIST(EL) EL ## _list
+#define LIST(EL) xLIST(EL)
+
+__isl_give LIST(EL) *FN(LIST(EL),alloc)(isl_ctx *ctx, int n)
+{
+       LIST(EL) *list;
+
+       if (n < 0)
+               isl_die(ctx, isl_error_invalid,
+                       "cannot create list of negative length",
+                       return NULL);
+       list = isl_alloc(ctx, LIST(EL),
+                        sizeof(LIST(EL)) + (n - 1) * sizeof(struct EL *));
+       if (!list)
+               return NULL;
+
+       list->ctx = ctx;
+       isl_ctx_ref(ctx);
+       list->ref = 1;
+       list->size = n;
+       list->n = 0;
+       return list;
+}
+
+__isl_give LIST(EL) *FN(LIST(EL),add)(__isl_take LIST(EL) *list,
+       __isl_take struct EL *el)
+{
+       if (!list || !el)
+               goto error;
+       isl_assert(list->ctx, list->n < list->size, goto error);
+       list->p[list->n] = el;
+       list->n++;
+       return list;
+error:
+       FN(EL,free)(el);
+       FN(LIST(EL),free)(list);
+       return NULL;
+}
+
+void FN(LIST(EL),free)(__isl_take LIST(EL) *list)
+{
+       int i;
+
+       if (!list)
+               return;
+
+       if (--list->ref > 0)
+               return;
+
+       isl_ctx_deref(list->ctx);
+       for (i = 0; i < list->n; ++i)
+               FN(EL,free)(list->p[i]);
+       free(list);
+}
diff --git a/isl_list_templ.h b/isl_list_templ.h
new file mode 100644 (file)
index 0000000..6ceb966
--- /dev/null
@@ -0,0 +1,14 @@
+#define xFN(TYPE,NAME) TYPE ## _ ## NAME
+#define FN(TYPE,NAME) xFN(TYPE,NAME)
+#define xLIST(EL) EL ## _list
+#define LIST(EL) xLIST(EL)
+
+struct LIST(EL) {
+       int ref;
+       isl_ctx *ctx;
+
+       int n;
+
+       size_t size;
+       struct EL *p[1];
+};
index 9d0ffbf..2e3213b 100644 (file)
--- a/isl_map.c
+++ b/isl_map.c
@@ -17,7 +17,7 @@
 #include <isl/blk.h>
 #include "isl_dim_private.h"
 #include "isl_equalities.h"
-#include <isl/list.h>
+#include <isl_list_private.h>
 #include <isl/lp.h>
 #include <isl/seq.h>
 #include <isl/set.h>
index 415965f..848ef85 100644 (file)
@@ -12,6 +12,7 @@
 
 #define isl_basic_set  isl_basic_map
 #define isl_set                isl_map
+#define isl_basic_set_list     isl_basic_map_list
 #include <isl/set.h>
 #include <isl/map.h>
 #include <isl_reordering.h>