Skip to content

Instantly share code, notes, and snippets.

@vdenotaris
Last active October 3, 2015 00:57
Show Gist options
  • Save vdenotaris/a7acb733199bfa434664 to your computer and use it in GitHub Desktop.
Save vdenotaris/a7acb733199bfa434664 to your computer and use it in GitHub Desktop.
Dynamic Matrix in C
/*
* Copyright 2014 Vincenzo De Notaris
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "matrix.h"
int main (int argc, const char * argv[])
{
matrix m;
allocate(&m, 1, 1);
addrow(&m);
for (int c = 0; c < matches.ncols; c++)
*element(&m, m.nrows - 1, c) = 0;
addcol(&m);
for (int r = 0; r < m.nrows; r++)
*element(&m, r, m.ncols - 1) = 0;
}
/*
* Copyright 2014 Vincenzo De Notaris
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "matrix.h"
/* Funzione per l'allocazione in memoria di una matrice di interi */
matrix* allocate(matrix *mat, int nrows, int ncols)
{
assert(nrows > 0 && ncols > 0);
mat->nrows = nrows;
mat->ncols = ncols;
mat->data = malloc(sizeof(int) * nrows * ncols);
return mat;
}
/* Funzione per l'accesso (R/W) ad un elemento di una matrice di interi */
int *element(const matrix *mat, int row, int col)
{
assert(row >= 0 && row < mat->nrows);
assert(col >= 0 && col < mat->ncols);
return mat->data + row * mat->ncols + col;
}
/* Funzione per aggiungere una riga in una matrice di interi */
matrix *addrow(matrix *mat)
{
mat->nrows++;
mat->data = realloc(mat->data, sizeof(int) * mat->nrows * mat->ncols);
return mat;
}
/* Funzione per aggiungere una colonna in una matrice di interi */
matrix *addcol(matrix *mat)
{
mat->ncols++;
mat->data = realloc(mat->data, sizeof(int) * mat->nrows * mat->ncols);
// Shift degli elementi delle righe
for (int i = mat->nrows - 1; i > 0; i--)
{
int *dest = mat->data + i * mat->ncols,
*orig = mat->data + i * (mat->ncols - 1);
memmove(dest, orig, sizeof(int) * (mat->ncols - 1));
}
return mat;
}
/*
* Copyright 2014 Vincenzo De Notaris
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef ConnectedComponents_matrix_h
#define ConnectedComponents_matrix_h
#include <stdio.h>
#include <assert.h>
#include <stdlib.h>
#include <string.h>
/* Definizione del tipo matrix e delle struct per modellare una matrice dinamica di interi */
typedef struct matrix {
int nrows, ncols, *data;
}
matrix; // Nomenclatura associata al tipo definito
/* Funzione per l'allocazione in memoria di una matrice di interi */
matrix* allocate(matrix *mat, int nrows, int ncols);
/* Funzione per l'accesso (R/W) ad un elemento di una matrice di interi */
int *element(const matrix *mat, int row, int col);
/* Funzione per aggiungere una riga in una matrice di interi */
matrix *addrow(matrix *mat);
/* Funzione per aggiungere una colonna in una matrice di interi */
matrix *addcol(matrix *mat);
#endif
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment