Reorg code
[satune.git] / src / Collections / vector.h
1 #ifndef VECTOR_H
2 #define VECTOR_H
3 #include <string.h>
4
5 #define VectorDef(name, type, defcap)                                                                                                                                           \
6         struct Vector ## name {                                                                                                                                                                                         \
7                 uint size;                                                                                                                                                                                                                                      \
8                 uint capacity;                                                                                                                                                                                                                  \
9                 type * array;                                                                                                                                                                                                                           \
10         };                                                                                                                                                                                                                                                                              \
11         typedef struct Vector ## name Vector ## name;                                                                                                   \
12         Vector ## name * allocVector ## name(uint capacity);                                                                    \
13         Vector ## name * allocDefVector ## name();                                                                                                              \
14         Vector ## name * allocVectorArray ## name(uint capacity, type * array); \
15         void pushVector ## name(Vector ## name *vector, type item);                                             \
16         type getVector ## name(Vector ## name *vector, uint index);                                             \
17         void setVector ## name(Vector ## name *vector, uint index, type item); \
18         uint getSizeVector ##name(Vector ##name *vector);                                                                                       \
19         void deleteVector ##name(Vector ##name *vector);                                                                                                \
20         void clearVector ##name(Vector ## name *vector);
21
22 #define VectorImpl(name, type, defcap)                                                                                                                                  \
23         Vector ## name * allocDefVector ## name() {                                                                                                             \
24                 return allocVector ## name(defcap);                                                                                                                                     \
25         }                                                                                                                                                                                                                                                                                       \
26         Vector ## name * allocVector ## name(uint capacity)     {                                                                       \
27                 Vector ## name * tmp = (Vector ## name *) ourmalloc(sizeof(type));      \
28                 tmp->size = 0;                                                                                                                                                                                                                  \
29                 tmp->capacity = capacity;                                                                                                                                                                               \
30                 tmp->array = (type *) ourcalloc(1, sizeof(type) * capacity);                                    \
31                 return tmp;                                                                                                                                                                                                                                     \
32         }                                                                                                                                                                                                                                                                                       \
33         Vector ## name * allocVectorArray ## name(uint capacity, type * array)  {       \
34                 Vector ## name * tmp = allocVector ## name(capacity);                                                           \
35                 memcpy(tmp->array, array, capacity * sizeof(type));                                                                     \
36                 return tmp;                                                                                                                                                                                                                                     \
37         }                                                                                                                                                                                                                                                                                       \
38         void pushVector ## name(Vector ## name *vector, type item) {                                    \
39                 if (vector->size >= vector->capacity) {                                                                                                                 \
40                         uint newcap=vector->capacity * 2;                                                                                                                                       \
41                         vector->array=(type *)ourrealloc(vector->array, newcap);                                        \
42                 }                                                                                                                                                                                                                                                                               \
43                 vector->array[vector->size++] = item;                                                                                                                           \
44         }                                                                                                                                                                                                                                                                                       \
45         type getVector ## name(Vector ## name * vector, uint index) {                                   \
46                 return vector->array[index];                                                                                                                                                            \
47         }                                                                                                                                                                                                                                                                                       \
48         void setVector ## name(Vector ## name * vector, uint index, type item) { \
49                 vector->array[index]=item;                                                                                                                                                                      \
50         }                                                                                                                                                                                                                                                                                       \
51         uint getSizeVector ## name(Vector ## name *vector) {                                                                    \
52                 return vector->size;                                                                                                                                                                                            \
53         }                                                                                                                                                                                                                                                                                       \
54         void deleteVector ##name(Vector ##name *vector) {                                                                                       \
55                 ourfree(vector->array);                                                                                                                                                                                 \
56                 ourfree(vector);                                                                                                                                                                                                                \
57         }                                                                                                                                                                                                                                                                                       \
58         void clearVector ##name(Vector ## name *vector) {                                                                                       \
59                 vector->size=0;                                                                                                                                                                                                                 \
60         }
61 #endif