f823d8c43119014f513e23a03b94acf9ba5a0377
[satune.git] / src / Collections / array.h
1 #ifndef ARRAY_H
2 #define ARRAY_H
3
4 #define ArrayDef(name, type)                                                                                                                                                                            \
5         struct Array ## name {                                                                                                                                                                                          \
6                 type * array;                                                       \
7                 uint size;                                                                                                                                                                                                                                      \
8         };                                                                    \
9         typedef struct Array ## name Array ## name;                                                                                                             \
10         static inline Array ## name * allocArray ## name(uint size) {                                                           \
11                 Array ## name * tmp = (Array ## name *)ourmalloc(sizeof(type));                 \
12                 tmp->size = size;                                                                                                                                                                                                               \
13                 tmp->array = (type *) ourcalloc(1, sizeof(type) * size);                                                \
14                 return tmp;                                                         \
15         }                                                                     \
16         static inline Array ## name * allocArrayInit ## name(type * array, uint size)  { \
17                 Array ## name * tmp = allocArray ## name(size);                                                                                 \
18                 memcpy(tmp->array, array, size * sizeof(type));                                                                                 \
19                 return tmp;                                                         \
20         }                                                                     \
21         static inline void removeElementArray ## name(Array ## name * This, uint index) { \
22                 This->size--;                                                                                                                                                                                                                           \
23                 for(;index<This->size;index++) {                                                                                                                                                \
24                         This->array[index]=This->array[index+1];                                                                                                        \
25                 }                                                                                                                                                                                                                                                                               \
26         }                                                                                                                                                                                                                                                                                       \
27         static inline type getArray ## name(Array ## name * This, uint index) { \
28                 return This->array[index];                                                                                                                                                                      \
29         }                                                                     \
30         static inline void setArray ## name(Array ## name * This, uint index, type item) {      \
31                 This->array[index]=item;                                                                                                                                                                                \
32         }                                                                     \
33         static inline uint getSizeArray ## name(Array ## name *This) {                                                          \
34                 return This->size;                                                                                                                                                                                                      \
35         }                                                                     \
36         static inline void deleteArray ## name(Array ## name *This) {                                                           \
37                 ourfree(This->array);                                                                                                                                                                                           \
38                 ourfree(This);                                                                                                                                                                                                                  \
39         }                                                                     \
40         static inline type * exposeCArray ## name(Array ## name * This) {                                                       \
41                 return This->array;                                                                                                                                                                                                     \
42         }                                                                                                                                                                                                                                                                                       \
43         static inline void deleteInlineArray ## name(Array ## name *This) {                                     \
44                 ourfree(This->array);                                                                                                                                                                                           \
45         }                                                                                                                                                                                                                                                                                       \
46         static inline void initArray ## name(Array ## name * This, uint size) {                 \
47                 This->size = size;                                                                                                                                                                                                      \
48                 This->array = (type *) ourcalloc(1, sizeof(type) * size);                                               \
49         }                                                                                                                                                                                                                                                                                       \
50         static inline void initArrayInit ## name(Array ## name * This, type *array, uint size) { \
51                 initArray ##name(This, size);                                                                                                                                                           \
52                 memcpy(This->array, array, size * sizeof(type));                                                                                \
53         }
54
55 #endif