Skip to content

Commit

Permalink
adding the grid module #23
Browse files Browse the repository at this point in the history
  • Loading branch information
yudhastyawan committed Jul 26, 2021
1 parent 2225046 commit 33d1e6f
Show file tree
Hide file tree
Showing 3 changed files with 139 additions and 2 deletions.
4 changes: 2 additions & 2 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"editor.fontSize": 16,
"window.zoomLevel": 1,
"editor.fontSize": 14,
"window.zoomLevel": 0,
"files.associations": {
"limits": "c",
"istream": "c",
Expand Down
67 changes: 67 additions & 0 deletions include/grid.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
/**
* \file grid.h
* @brief the library for grid data manipulation
*
* */

#ifndef __GRID_H
#define __GRID_H

#ifdef __cplusplus
extern "C" {
#endif
/**
* @brief grid structure data
* */
typedef struct s_grid {
/// r_shape in malloc 1d data
int *r_shape;
/// p_r_nums in malloc 1d data
double *p_r_nums;

/// the length of malloc p_r_nums
int size;

/// the length of r_shape
int ndim;
} AgxGrid;


/**
* @brief create a grid data in malloc
* @warning need to be deleted before closing the program or after data is unused using @ref agx_grid_delete
* @param args(...) input dimensional values beginning with x1, ..., xn
* @param ndim the length of dimensional data N (x1, ..., xn)
* */
AgxGrid *agx_grid_new(int ndim, ...);

/**
* @brief delete @ref agx_grid_new
* */
void agx_grid_delete(AgxGrid *grid);

/**
* @brief convert dimensional indexes to 1d index
* @param args(...) input dimensional values x1, ..., xn
* @param ndim length of dimensional input in args(...)
* */
int agx_grid_idim2index(AgxGrid *grid, int ndim, ...);

/**
* @brief convert 1d index to dimensional indexes
* @return malloc shape that need to be deleted using free()
* */
int *agx_grid_index2dim(AgxGrid *grid, int index);

/**
* @brief using @ref agx_grid_index2dim only for a certain dimensional index.
* @note the return in @ref agx_grid_index2dim has been deleted in the function.
* @return index in n-dimensional index.
* */
int agx_grid_index2idim(AgxGrid *grid, int idim, int index);

#ifdef __cplusplus
}
#endif

#endif
70 changes: 70 additions & 0 deletions src/grid.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
#include "grid.h"
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>

AgxGrid *agx_grid_new(int ndim, ...) {
AgxGrid *grid = (AgxGrid *)malloc(sizeof(AgxGrid));
grid->r_shape = (int *)malloc(ndim*sizeof(int));
grid->ndim = ndim;
grid->size = 1;

va_list args;
va_start(args,ndim);
for(int i = 0; i < ndim; i++){
int idim = va_arg(args, int);
grid->r_shape[i] = idim;
grid->size *= idim;
}
va_end(args);

grid->p_r_nums = (double *)malloc(grid->size*sizeof(double));

return grid;
}

void agx_grid_delete(AgxGrid *grid) {
free(grid->p_r_nums);
free(grid->r_shape);
free(grid);
}

int agx_grid_idim2index(AgxGrid *grid, int ndim, ...) {
int idx = 0;
va_list args;
va_start(args, ndim);

for (int i = 0; i < ndim; i++){
int val = va_arg(args, int);
int mul = 1;
for (int j = 0; j < i; j++) {
mul *= grid->r_shape[j];
}
idx += val * mul;
}

va_end(args);
return idx;
}

int *agx_grid_index2dim(AgxGrid *grid, int index) {
int mod;
int *ishape = (int *)malloc(grid->ndim*sizeof(int));
for (int i = grid->ndim - 1; i >= 0; i--) {
mod = 1;
for (int j = 0; j < i; j++) {
mod *= grid->r_shape[j];
}
ishape[i] = index / mod;
index = index % mod;
}
return ishape;
}

int agx_grid_index2idim(AgxGrid *grid, int idim, int index) {
int idx;
int *ishape = agx_grid_index2dim(grid, index);
idx = ishape[idim];
free(ishape);
return idx;
}

0 comments on commit 33d1e6f

Please sign in to comment.