+#include <isl_sort.h>
+
+enum isl_restriction_type {
+ isl_restriction_type_empty,
+ isl_restriction_type_none,
+ isl_restriction_type_input,
+ isl_restriction_type_output
+};
+
+struct isl_restriction {
+ enum isl_restriction_type type;
+
+ isl_set *source;
+ isl_set *sink;
+};
+
+/* Create a restriction of the given type.
+ */
+static __isl_give isl_restriction *isl_restriction_alloc(
+ __isl_take isl_map *source_map, enum isl_restriction_type type)
+{
+ isl_ctx *ctx;
+ isl_restriction *restr;
+
+ if (!source_map)
+ return NULL;
+
+ ctx = isl_map_get_ctx(source_map);
+ restr = isl_calloc_type(ctx, struct isl_restriction);
+ if (!restr)
+ goto error;
+
+ restr->type = type;
+
+ isl_map_free(source_map);
+ return restr;
+error:
+ isl_map_free(source_map);
+ return NULL;
+}
+
+/* Create a restriction that doesn't restrict anything.
+ */
+__isl_give isl_restriction *isl_restriction_none(__isl_take isl_map *source_map)
+{
+ return isl_restriction_alloc(source_map, isl_restriction_type_none);
+}
+
+/* Create a restriction that removes everything.
+ */
+__isl_give isl_restriction *isl_restriction_empty(
+ __isl_take isl_map *source_map)
+{
+ return isl_restriction_alloc(source_map, isl_restriction_type_empty);
+}
+
+/* Create a restriction on the input of the maximization problem
+ * based on the given source and sink restrictions.
+ */
+__isl_give isl_restriction *isl_restriction_input(
+ __isl_take isl_set *source_restr, __isl_take isl_set *sink_restr)
+{
+ isl_ctx *ctx;
+ isl_restriction *restr;
+
+ if (!source_restr || !sink_restr)
+ goto error;
+
+ ctx = isl_set_get_ctx(source_restr);
+ restr = isl_calloc_type(ctx, struct isl_restriction);
+ if (!restr)
+ goto error;
+
+ restr->type = isl_restriction_type_input;
+ restr->source = source_restr;
+ restr->sink = sink_restr;
+
+ return restr;
+error:
+ isl_set_free(source_restr);
+ isl_set_free(sink_restr);
+ return NULL;
+}
+
+/* Create a restriction on the output of the maximization problem
+ * based on the given source restriction.
+ */
+__isl_give isl_restriction *isl_restriction_output(
+ __isl_take isl_set *source_restr)
+{
+ isl_ctx *ctx;
+ isl_restriction *restr;
+
+ if (!source_restr)
+ return NULL;
+
+ ctx = isl_set_get_ctx(source_restr);
+ restr = isl_calloc_type(ctx, struct isl_restriction);
+ if (!restr)
+ goto error;
+
+ restr->type = isl_restriction_type_output;
+ restr->source = source_restr;
+
+ return restr;
+error:
+ isl_set_free(source_restr);
+ return NULL;
+}
+
+void *isl_restriction_free(__isl_take isl_restriction *restr)
+{
+ if (!restr)
+ return NULL;
+
+ isl_set_free(restr->source);
+ isl_set_free(restr->sink);
+ free(restr);
+ return NULL;
+}
+
+isl_ctx *isl_restriction_get_ctx(__isl_keep isl_restriction *restr)
+{
+ return restr ? isl_set_get_ctx(restr->source) : NULL;
+}