Used macro instead of function to get the array index

This commit is contained in:
Fabio Salvini
2016-11-17 14:14:59 +01:00
parent c38550c8aa
commit 3c4dfda3ee
4 changed files with 29 additions and 30 deletions

View File

@@ -1,5 +1,6 @@
#include <stdio.h>
#include <stdlib.h>
#include "../utils/utils.h"
double *create_sa_matrix(int rows, int cols) {
double *x;
@@ -12,16 +13,11 @@ void destroy_sa_matrix(double *x) {
free(x);
}
int sa_index(int cols, int r, int c) {
return r * cols + c;
}
void print_sa_matrix(double *x, int rows, int cols) {
int i, j;
for (i = 0; i < rows; i++) {
for (j = 0; j < cols; j++) {
printf("%f\t", x[sa_index(cols, i, j)]);
printf("%f\t", x[IDX(cols, i, j)]);
}
printf("\n");
}

View File

@@ -9,6 +9,13 @@
# define LOG(x) (void) 0
#endif
/*
* Macro used with single array matrices to
* get the array index given the number of columns,
* the row index and the column index.
*/
#define IDX(cols, r, c) ((r) * (cols) + (c))
typedef struct borders {
double north;
double east;
@@ -27,10 +34,6 @@ double *create_sa_matrix(int rows, int cols);
*/
void destroy_sa_matrix(double *x);
/*
* Get the index for the single array matrix
* that correspond to the given row and column.
*/
int sa_index(int cols, int r, int c);
/*