11 #define TYPEDEF_VECTOR(T, N) typedef struct { \
17 typedef const _##N* const_##N;
19 #define VECTOR(T) struct { \
25 #define VECTOR_INIT(_x) do { (_x) = NULL; } while(0)
26 #define VECTOR_INIT_STATIC() NULL
27 #define VECTOR_DEINIT(_x) do { al_free((_x)); (_x) = NULL; } while(0)
29 #define VECTOR_RESIZE(_x, _s, _c) do { \
30 size_t _size = (_s); \
35 if(!(_x) && _cap == 0) \
38 if(((_x) ? (_x)->Capacity : 0) < _cap) \
40 ptrdiff_t data_offset = (char*)((_x)->Data) - (char*)(_x); \
41 size_t old_size = ((_x) ? (_x)->Size : 0); \
44 temp = al_calloc(16, data_offset + sizeof((_x)->Data[0])*_cap); \
45 assert(temp != NULL); \
47 memcpy(((char*)temp)+data_offset, (_x)->Data, \
48 sizeof((_x)->Data[0])*old_size); \
52 (_x)->Capacity = _cap; \
57 #define VECTOR_CAPACITY(_x) ((_x) ? (_x)->Capacity : 0)
58 #define VECTOR_SIZE(_x) ((_x) ? (_x)->Size : 0)
60 #define VECTOR_BEGIN(_x) ((_x) ? (_x)->Data + 0 : NULL)
61 #define VECTOR_END(_x) ((_x) ? (_x)->Data + (_x)->Size : NULL)
63 #define VECTOR_PUSH_BACK(_x, _obj) do { \
64 size_t _pbsize = VECTOR_SIZE(_x)+1; \
65 VECTOR_RESIZE(_x, _pbsize, _pbsize); \
66 (_x)->Data[(_x)->Size-1] = (_obj); \
68 #define VECTOR_POP_BACK(_x) ((void)((_x)->Size--))
70 #define VECTOR_BACK(_x) ((_x)->Data[(_x)->Size-1])
71 #define VECTOR_FRONT(_x) ((_x)->Data[0])
73 #define VECTOR_ELEM(_x, _o) ((_x)->Data[(_o)])
75 #define VECTOR_FOR_EACH(_t, _x, _f) do { \
76 _t *_iter = VECTOR_BEGIN((_x)); \
77 _t *_end = VECTOR_END((_x)); \
78 for(;_iter != _end;++_iter) \
82 #define VECTOR_FIND_IF(_i, _t, _x, _f) do { \
83 _t *_iter = VECTOR_BEGIN((_x)); \
84 _t *_end = VECTOR_END((_x)); \
85 for(;_iter != _end;++_iter) \
93 #endif /* AL_VECTOR_H */