add isl_mat_extend
authorSven Verdoolaege <skimo@kotnet.org>
Wed, 11 Mar 2009 16:23:09 +0000 (17:23 +0100)
committerSven Verdoolaege <skimo@kotnet.org>
Fri, 20 Mar 2009 14:21:04 +0000 (15:21 +0100)
isl_mat.c
isl_mat.h

index 1230a18..e15967f 100644 (file)
--- a/isl_mat.c
+++ b/isl_mat.c
@@ -36,6 +36,51 @@ error:
        return NULL;
 }
 
+struct isl_mat *isl_mat_extend(struct isl_ctx *ctx, struct isl_mat *mat,
+       unsigned n_row, unsigned n_col)
+{
+       int i;
+
+       if (!mat)
+               return NULL;
+
+       if (mat->n_col >= n_col && mat->n_row >= n_row)
+               return mat;
+
+       if (mat->n_col < n_col) {
+               struct isl_mat *new_mat;
+
+               new_mat = isl_mat_alloc(ctx, n_row, n_col);
+               if (!new_mat)
+                       goto error;
+               for (i = 0; i < mat->n_row; ++i)
+                       isl_seq_cpy(new_mat->row[i], mat->row[i], mat->n_col);
+               isl_mat_free(ctx, mat);
+               return new_mat;
+       }
+
+       mat = isl_mat_cow(ctx, mat);
+       if (!mat)
+               goto error;
+
+       assert(mat->ref == 1);
+       mat->block = isl_blk_extend(ctx, mat->block, n_row * mat->n_col);
+       if (isl_blk_is_error(mat->block))
+               goto error;
+       mat->row = isl_realloc_array(ctx, mat->row, isl_int *, n_row);
+       if (!mat->row)
+               goto error;
+
+       for (i = 0; i < n_row; ++i)
+               mat->row[i] = mat->block.data + i * mat->n_col;
+       mat->n_row = n_row;
+
+       return mat;
+error:
+       isl_mat_free(ctx, mat);
+       return NULL;
+}
+
 struct isl_mat *isl_mat_sub_alloc(struct isl_ctx *ctx, isl_int **row,
        unsigned first_row, unsigned n_row, unsigned first_col, unsigned n_col)
 {
index f1ebc8c..72aa593 100644 (file)
--- a/isl_mat.h
+++ b/isl_mat.h
@@ -29,6 +29,8 @@ struct isl_mat {
 
 struct isl_mat *isl_mat_alloc(struct isl_ctx *ctx,
        unsigned n_row, unsigned n_col);
+struct isl_mat *isl_mat_extend(struct isl_ctx *ctx, struct isl_mat *mat,
+       unsigned n_row, unsigned n_col);
 struct isl_mat *isl_mat_identity(struct isl_ctx *ctx, unsigned n_row);
 struct isl_mat *isl_mat_copy(struct isl_ctx *ctx, struct isl_mat *mat);
 struct isl_mat *isl_mat_cow(struct isl_ctx *ctx, struct isl_mat *mat);