Finish rewriting Set/Table/Vector
[satune.git] / src / 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(type * array, uint capacity); \
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 freeVector ##name(Vector ##name *vector);
20
21 #define VectorImpl(name, type, defcap)                                                                                                                                  \
22         Vector ## name * allocDefVector ## name() {                                                                                                             \
23                 return allocVector ## name(defcap);                                                                                                                                     \
24         }                                                                                                                                                                                                                                                                                       \
25         Vector ## name * allocVector ## name(uint capacity)     {                                                                       \
26                 Vector ## name * tmp = (Vector ## name *) ourmalloc(sizeof(type));      \
27                 tmp->size = 0;                                                                                                                                                                                                                  \
28                 tmp->capacity = capacity;                                                                                                                                                                               \
29                 tmp->array = (type *) ourcalloc(1, sizeof(type) * capacity);                                    \
30                 return tmp;                                                                                                                                                                                                                                     \
31         }                                                                                                                                                                                                                                                                                       \
32         Vector ## name * allocVectorArray ## name(uint capacity, type * array)  {       \
33                 Vector ## name * tmp = allocVector ## name(capacity);                                                           \
34                 memcpy(tmp->array, array, capacity * sizeof(type));                                                                     \
35                 return tmp;                                                                                                                                                                                                                                     \
36         }                                                                                                                                                                                                                                                                                       \
37         void pushVector ## name(Vector ## name *vector, type item) {                                    \
38                 if (vector->size >= vector->capacity) {                                                                                                                 \
39                         uint newcap=vector->capacity * 2;                                                                                                                                       \
40                         vector->array=(type *)ourrealloc(vector->array, newcap);                                        \
41                 }                                                                                                                                                                                                                                                                               \
42                 vector->array[vector->size++] = item;                                                                                                                           \
43         }                                                                                                                                                                                                                                                                                       \
44         type getVector ## name(Vector ## name * vector, uint index) {                                   \
45                 return vector->array[index];                                                                                                                                                            \
46         }                                                                                                                                                                                                                                                                                       \
47         void setVector ## name(Vector ## name * vector, uint index, type item) { \
48                 vector->array[index]=item;                                                                                                                                                                      \
49         }                                                                                                                                                                                                                                                                                       \
50         uint getSizeVector ## name(Vector ## name *vector) {                                                                    \
51                 return vector->size;                                                                                                                                                                                            \
52         }                                                                                                                                                                                                                                                                                       \
53         void freeVector ##name(Vector ##name *vector) {                                                                                         \
54                 ourfree(vector->array);                                                                                                                                                                                 \
55                 ourfree(vector);                                                                                                                                                                                                                \
56         }
57 #endif