UCFD_SPARSE  v1.0
Documentation
Loading...
Searching...
No Matches
arrays.c
Go to the documentation of this file.
1
22#include <stdio.h>
23#include <stdlib.h>
24#include "arrays.h"
25
26
33void **malloc_2d(const size_t rows, const size_t cols, const size_t T)
34{
35 void *data= (void *) malloc(rows*cols*T);
36 void **ar = (void **)malloc(rows*sizeof(void*));
37 int i;
38 char *p = (char*)data;
39 for (i=0; i<rows; i++)
40 ar[i] = &(p[cols*i*T]);
41 return ar;
42}
43
44
52void ***malloc_3d(const size_t rows, const size_t cols, const size_t depth, const size_t T)
53{
54 void *data = (void *) malloc(rows*cols*depth*T); // using calloc
55 void **ar2 = (void **)malloc(rows*cols*sizeof(void*));
56 void ***ar = (void ***)malloc(rows*sizeof(void**));
57 int i,j;
58 char *p = (char*)data;
59 for (i=0; i<rows; i++){
60 for (j=0; j<cols; j++){
61 ar2[cols*i+j] = &(p[(cols*i+j)*depth*T]);
62 }
63 ar[i] = &(ar2[cols*i]);
64 }
65 return ar;
66}
67
68
69void ****malloc_4d(const size_t rows, const size_t cols, const size_t depth, const size_t dims, const size_t T) {
70 void *data = (void *) malloc(rows*cols*depth*dims*T);
71 void **ar3 = (void **)malloc(rows*cols*depth*sizeof(void*));
72 void ***ar2 = (void ***)malloc(rows*cols*sizeof(void**));
73 void ****ar = (void ****)malloc(rows*sizeof(void ***));
74 int i, j, k, idx;
75 char *p = (char*)data;
76 for (i=0; i<rows; i++){
77 for (j=0; j<cols; j++){
78 for (k=0; k<depth; k++){
79 ar3[(i*cols+j)*depth+k] = &p[((i*cols+j)*depth+k)*dims*T];
80 }
81 ar2[i*cols+j] = &ar3[(i*cols+j)*depth];
82 }
83 ar[i] = &ar2[i*cols];
84 }
85 return ar;
86}
87
88
93void dealloc_2d(void **mat)
94{
95 free(*mat);
96 free(mat);
97}
98
99
104void dealloc_3d(void ***mat)
105{
106 free(**mat);
107 free(*mat);
108 free(mat);
109}
110
111void dealloc_4d(void ****mat) {
112 free(***mat);
113 free(**mat);
114 free(*mat);
115 free(mat);
116}
void dealloc_2d(void **mat)
Deallocate 2D array.
Definition: arrays.c:93
void ** malloc_2d(const size_t rows, const size_t cols, const size_t T)
Allocate 2D array.
Definition: arrays.c:33
void *** malloc_3d(const size_t rows, const size_t cols, const size_t depth, const size_t T)
Allocate 3D array.
Definition: arrays.c:52
void **** malloc_4d(const size_t rows, const size_t cols, const size_t depth, const size_t dims, const size_t T)
Definition: arrays.c:69
void dealloc_4d(void ****mat)
Definition: arrays.c:111
void dealloc_3d(void ***mat)
Deallocate 3D array.
Definition: arrays.c:104