@kv  0.0.4
Library for graph problems in C and Python
array.h
Go to the documentation of this file.
1 
19 #if !defined(AT_CORE_H_INSIDE)
20 #error "Only <at/core.h> can be included directly."
21 #endif
22 
23 #ifndef AT_ARRAY_H
24 #define AT_ARRAY_H
25 #include <stdint.h>
26 #include <stdlib.h>
27 #include <string.h>
28 #include <stdbool.h>
29 #include <at/core.h>
30 AT_BEGIN_DECLS
31 /*=============================================================================
32  STRUCTURE
33  ============================================================================*/
35 typedef struct AtArrayHeader AtArrayHeader;
36 
41  uint64_t* shape; /*00+8*/
42  uint64_t* step; /*08+8*/
43  uint64_t* cstep; /*16+8*/
44  uint64_t nelem; /*24+8*/
45  uint8_t dim; /*32+1*/
46  uint8_t owns_data; /*33+1*/
47  uint8_t elemsize; /*34+1*/
48  uint8_t alignment[5]; /*35+5*/
49 }; // Total: 40B
50 
60 #define AT_DECLARE_ARRAY(UPPER,type) \
61 typedef struct AtArray##UPPER{ \
62  AtArrayHeader h; /*00-31*/ \
63  type *data; /*32-39*/ \
64 }AtArray##UPPER; // Total: 40B
65 AT_DECLARE_ARRAY(U8,uint8_t)
66 AT_DECLARE_ARRAY(U16,uint16_t)
67 AT_DECLARE_ARRAY(U32,uint32_t)
68 AT_DECLARE_ARRAY(U64,uint64_t)
69 AT_DECLARE_ARRAY(I8,int8_t)
70 AT_DECLARE_ARRAY(I16,int16_t)
71 AT_DECLARE_ARRAY(I32,int32_t)
72 AT_DECLARE_ARRAY(I64,int64_t)
73 AT_DECLARE_ARRAY(F32,float)
74 AT_DECLARE_ARRAY(D64,double)
75 #undef AT_DECLARE_ARRAY
76 /*=============================================================================
77  FUNCTIONS
78  ============================================================================*/
86 void
87 at_index_to_nd(uint8_t dim, uint64_t* step, uint64_t s, uint64_t* s_nd);
95 void
96 at_index_to_1d(uint8_t dim, uint64_t* step, int64_t* s_nd, uint64_t* s);
100 #define at_array_index_to_nd(array, s, s_nd) at_index_to_nd(array->h.dim, array->h.step,s,s_nd)
101 
104 #define at_array_index_to_1d(array, s_nd, s) at_index_to_1d(array->h.dim, array->h.step,s_nd,s)
105 
108 #define at_array_max(array) _Generic((array), \
109  AtArrayU8*:at_arrayu8_max)(array)
110 
115 void
117 
124 void
125 at_array_header_set(AtArrayHeader* header, uint8_t dim, uint64_t* shape);
126 
131 void
133 
137 #define at_array_destroy(array_ptr) _Generic((array_ptr), \
138  AtArrayU8**: at_arrayu8_destroy \
139 )(array_ptr)
140 
142 #define AT_DECLARE_ARRAY_OP(op) \
143  op(u8 ,U8 ) \
144  op(u16,U16) \
145  op(u32,U32) \
146  op(u64,U64) \
147  op(i8 ,I8 ) \
148  op(i16,I16) \
149  op(i32,I32) \
150  op(i64,I64) \
151  op(f32,F32) \
152  op(d64,D64)
153 
154 #define AT_DECLARE_ARRAY_OP3(op) \
155  op(u8 ,U8 ,uint8_t ) \
156  op(u16,U16,uint16_t) \
157  op(u32,U32,uint32_t) \
158  op(u64,U64,uint64_t) \
159  op(i8 ,I8 , int8_t ) \
160  op(i16,I16, int16_t) \
161  op(i32,I32, int32_t) \
162  op(i64,I64, int64_t) \
163  op(f32,F32,float) \
164  op(d64,D64,double)
165 
166 // at_array_create
167 #define AT_DECLARE_ARRAY_CREATE(lower, UPPER) AtArray##UPPER* at_array##lower##_create();
168 #define AT_DECLARE_ARRAY_NEW(lower, UPPER) AtArray##UPPER* at_array##lower##_new(uint8_t dim, uint64_t* shape);
169 #define AT_DECLARE_ARRAY_NEW_WITH_DATA(lower, UPPER, type) AtArray##UPPER* at_array##lower##_new_with_data(uint8_t dim, uint64_t* shape, type* data, bool copy);
170 #define AT_DECLARE_ARRAY_ZEROS(lower, UPPER) AtArray##UPPER* at_array##lower##_zeros(uint8_t dim, uint64_t* shape);
171 #define AT_DECLARE_ARRAY_ONES(lower, UPPER) AtArray##UPPER* at_array##lower##_ones(uint8_t dim, uint64_t* shape);
172 #define AT_DECLARE_ARRAY_FILL(lower, UPPER, type) void at_array##lower##_fill(AtArray##UPPER* array, type value);
173 #define AT_DECLARE_ARRAY_ADD_SCALAR(lower, UPPER, type) void at_array##lower##_add_scalar(AtArray##UPPER* array, type value);
174 #define AT_DECLARE_ARRAY_ADD_SCALAR_CLAMPED(lower, UPPER, type) void at_array##lower##_add_scalar_clamped(AtArray##UPPER* array, type value);
175 #define AT_DECLARE_ARRAY_MAX(lower, UPPER, type) type at_array##lower##_max(AtArray##UPPER* array);
176 #define AT_DECLARE_ARRAY_DESTROY(lower, UPPER) void at_array##lower##_destroy(AtArray##UPPER** array);
177 #define AT_DECLARE_ARRAY_GET_1D(lower, UPPER, type) type at_array##lower##_get_1d(AtArray##UPPER* array, uint64_t idx);
178 #define AT_DECLARE_ARRAY_GET_ND(lower, UPPER, type) type at_array##lower##_get_nd(AtArray##UPPER* array, uint64_t* idx);
179 #define AT_DECLARE_ARRAY_SET_1D(lower, UPPER, type) void at_array##lower##_set_1d(AtArray##UPPER* array, uint64_t idx, type value);
180 #define AT_DECLARE_ARRAY_SET_ND(lower, UPPER, type) void at_array##lower##_set_nd(AtArray##UPPER* array, uint64_t* idx, type value);
181 #define AT_DECLARE_ARRAY_SUB(lower, UPPER, type) void at_array##lower##_sub(AtArray##UPPER* array, AtRange* ranges, AtArray##UPPER** outputp, uint8_t copy);
182 
183 AT_DECLARE_ARRAY_OP(AT_DECLARE_ARRAY_CREATE)
184 AT_DECLARE_ARRAY_OP(AT_DECLARE_ARRAY_NEW)
185 AT_DECLARE_ARRAY_OP3(AT_DECLARE_ARRAY_NEW_WITH_DATA)
186 AT_DECLARE_ARRAY_OP(AT_DECLARE_ARRAY_ZEROS)
187 AT_DECLARE_ARRAY_OP(AT_DECLARE_ARRAY_ONES)
188 AT_DECLARE_ARRAY_OP3(AT_DECLARE_ARRAY_FILL)
189 AT_DECLARE_ARRAY_OP3(AT_DECLARE_ARRAY_ADD_SCALAR)
190 AT_DECLARE_ARRAY_OP3(AT_DECLARE_ARRAY_ADD_SCALAR_CLAMPED)
191 AT_DECLARE_ARRAY_OP3(AT_DECLARE_ARRAY_MAX)
192 AT_DECLARE_ARRAY_OP(AT_DECLARE_ARRAY_DESTROY)
193 AT_DECLARE_ARRAY_OP3(AT_DECLARE_ARRAY_GET_1D)
194 AT_DECLARE_ARRAY_OP3(AT_DECLARE_ARRAY_GET_ND)
195 AT_DECLARE_ARRAY_OP3(AT_DECLARE_ARRAY_SET_1D)
196 AT_DECLARE_ARRAY_OP3(AT_DECLARE_ARRAY_SET_ND)
197 AT_DECLARE_ARRAY_OP3(AT_DECLARE_ARRAY_SUB)
198 
199 #undef AT_DECLARE_ARRAY_CREATE
200 #undef AT_DECLARE_ARRAY_NEW
201 #undef AT_DECLARE_ARRAY_NEW_WITH_DATA
202 #undef AT_DECLARE_ARRAY_ZEROS
203 #undef AT_DECLARE_ARRAY_ONES
204 #undef AT_DECLARE_ARRAY_FILL
205 #undef AT_DECLARE_ARRAY_ADD_SCALAR
206 #undef AT_DECLARE_ARRAY_ADD_SCALAR_CLAMPED
207 #undef AT_DECLARE_ARRAY_MAX
208 #undef AT_DECLARE_ARRAY_DESTROY
209 #undef AT_DECLARE_ARRAY_GET_1D
210 #undef AT_DECLARE_ARRAY_GET_ND
211 #undef AT_DECLARE_ARRAY_SET_1D
212 #undef AT_DECLARE_ARRAY_SET_ND
213 #undef AT_DECLARE_ARRAY_SUB
214 
215 #undef AT_DECLARE_ARRAY_OP
216 #undef AT_DECLARE_ARRAY_OP3
217 
218 #define at_arrayu8_squeeze(ar) at_arrayheader_squeeze(&ar->h)
219 #define at_arrayu16_squeeze(ar) at_arrayheader_squeeze(&ar->h)
220 #define at_arrayu32_squeeze(ar) at_arrayheader_squeeze(&ar->h)
221 #define at_arrayu64_squeeze(ar) at_arrayheader_squeeze(&ar->h)
222 #define at_arrayi8_squeeze(ar) at_arrayheader_squeeze(&ar->h)
223 #define at_arrayi16_squeeze(ar) at_arrayheader_squeeze(&ar->h)
224 #define at_arrayi32_squeeze(ar) at_arrayheader_squeeze(&ar->h)
225 #define at_arrayi64_squeeze(ar) at_arrayheader_squeeze(&ar->h)
226 #define at_arrayf32_squeeze(ar) at_arrayheader_squeeze(&ar->h)
227 #define at_arrayd64_squeeze(ar) at_arrayheader_squeeze(&ar->h)
228 
230 
234 #define at_array_save(arrays,names,num,filename) at_arrayu8_save((AtArrayU8**)arrays,names,num,filename);
235 
242 void
243 at_arrayu8_save(AtArrayU8** arrays, char** names, uint8_t num, const char* filename);
251 AtArrayU8*
252 at_array_load(char*** namesp, uint8_t *nump, const char* filename);
259 void
260 at_arrayu8_set_nd_many(AtArrayU8* ar, uint64_t *coords, uint8_t* value);
267 void
268 at_arrayu16_sub_u8(AtArrayU16* ar, AtRange* ranges, AtArrayU8 **output);
273 void
275 
276 AT_END_DECLS
277 #endif
uint8_t owns_data
Definition: array.h:46
AtArrayU8 * at_array_load(char ***namesp, uint8_t *nump, const char *filename)
at_array_load
uint64_t * cstep
Definition: array.h:43
void at_array_header_dispose(AtArrayHeader *header)
at_array_header_dispose
uint8_t elemsize
Definition: array.h:47
void at_index_to_nd(uint8_t dim, uint64_t *step, uint64_t s, uint64_t *s_nd)
at_index_to_nd
void at_arrayheader_squeeze(AtArrayHeader *h)
at_arrayheader_squeeze
void at_array_header_set(AtArrayHeader *header, uint8_t dim, uint64_t *shape)
at_array_header_set
#define AT_DECLARE_ARRAY(UPPER, type)
Vetor Multidimensional.
Definition: array.h:60
void at_array_header_init(AtArrayHeader *header)
at_array_header_init
uint64_t nelem
Definition: array.h:44
void at_arrayu8_set_nd_many(AtArrayU8 *ar, uint64_t *coords, uint8_t *value)
at_arrayu8_set_nd_many
void at_arrayu8_save(AtArrayU8 **arrays, char **names, uint8_t num, const char *filename)
at_arrayu8_save
void at_arrayu16_sub_u8(AtArrayU16 *ar, AtRange *ranges, AtArrayU8 **output)
at_arrayu16_sub_u8
uint64_t * step
Definition: array.h:42
void at_index_to_1d(uint8_t dim, uint64_t *step, int64_t *s_nd, uint64_t *s)
at_index_to_1d
uint64_t * shape
Definition: array.h:41
A directed weighted grid graph.
Definition: array.h:40
Definition: range.h:29
uint8_t dim
Definition: array.h:45
uint8_t alignment[5]
Definition: array.h:48