-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
2225046
commit 33d1e6f
Showing
3 changed files
with
139 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |