/*Array2D.h*/
#ifndef __ARRAY_2D
#define __ARRAY_2D
typedef struct Array2D {
double *data;
int w, h;
}Array2D;
Array2D *a2d_create(int h, int w);
void a2d_destroy(Array2D *a);
double a2d_get(Array2D *a, int row, int col);
void a2d_set(Array2D *a, int row, int col, double d);
#endif
/*************Array2D.c*******************/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <assert.h>
#include "array2d.h"
Array2D *a2d_create(int h, int w) {
Array2D *a = (Array2D *)calloc(1, sizeof(*a));
assert(a != NULL);
a->w = w;
a->h = h;
a->data = (double *)calloc(w * h, sizeof(*a->data));
assert(a->data != NULL);
return a;
}
void a2d_destroy(Array2D *a) {
if(a != NULL) {
if(a->data != NULL)
free(a->data);
free(a);
}
}
double a2d_get(Array2D *a, int y, int x) {
assert(a != NULL);
assert(x < a->w);
assert(y < a->h);
return *(a->data + x + y*a->w);
}
void a2d_set(Array2D *a, int y, int x, double d) {
assert(a != NULL);
assert(x < a->w);
assert(y < a->h);
*(a->data + x + y*a->w) = d;
}