1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
|
#ifndef AL_VECTOR_H
#define AL_VECTOR_H
#include <cstring>
#include <vector>
#include "almalloc.h"
#define TYPEDEF_VECTOR(T, N) typedef struct { \
std::size_t Capacity; \
std::size_t Size; \
T Data[]; \
} _##N; \
typedef _##N* N; \
typedef const _##N* const_##N;
#define VECTOR(T) struct { \
std::size_t Capacity; \
std::size_t Size; \
T Data[]; \
}*
#define VECTOR_INIT(_x) do { (_x) = nullptr; } while(0)
#define VECTOR_INIT_STATIC() nullptr
#define VECTOR_DEINIT(_x) do { al_free((_x)); (_x) = nullptr; } while(0)
template<typename T>
inline void do_vector_resize(T *&vec, std::size_t size, std::size_t cap)
{
if(size > cap)
cap = size;
if(!vec && cap == 0)
return;
if((vec ? vec->Capacity : 0) < cap)
{
ptrdiff_t data_offset = vec ? (char*)(vec->Data) - (char*)(vec) : sizeof(*vec);
size_t old_size = (vec ? vec->Size : 0);
auto temp = static_cast<T*>(al_calloc(16, data_offset + sizeof(vec->Data[0])*cap));
if(vec) std::memcpy(temp->Data, vec->Data, sizeof(vec->Data[0])*old_size);
al_free(vec);
vec = temp;
vec->Capacity = cap;
}
vec->Size = size;
}
#define VECTOR_RESIZE(_x, _s, _c) do_vector_resize(_x, _s, _c)
#define VECTOR_CAPACITY(_x) ((_x) ? (_x)->Capacity : 0)
#define VECTOR_SIZE(_x) ((_x) ? (_x)->Size : 0)
#define VECTOR_BEGIN(_x) ((_x) ? (_x)->Data + 0 : NULL)
#define VECTOR_END(_x) ((_x) ? (_x)->Data + (_x)->Size : NULL)
#define VECTOR_PUSH_BACK(_x, _obj) do { \
std::size_t _pbsize = VECTOR_SIZE(_x)+1; \
VECTOR_RESIZE(_x, _pbsize, _pbsize); \
(_x)->Data[(_x)->Size-1] = (_obj); \
} while(0)
#define VECTOR_POP_BACK(_x) ((void)((_x)->Size--))
#define VECTOR_BACK(_x) ((_x)->Data[(_x)->Size-1])
#define VECTOR_FRONT(_x) ((_x)->Data[0])
#define VECTOR_ELEM(_x, _o) ((_x)->Data[(_o)])
#define VECTOR_FOR_EACH(_t, _x, _f) do { \
_t *_iter = VECTOR_BEGIN((_x)); \
_t *_end = VECTOR_END((_x)); \
for(;_iter != _end;++_iter) \
_f(_iter); \
} while(0)
#define VECTOR_FIND_IF(_i, _t, _x, _f) do { \
_t *_iter = VECTOR_BEGIN((_x)); \
_t *_end = VECTOR_END((_x)); \
for(;_iter != _end;++_iter) \
{ \
if(_f(_iter)) \
break; \
} \
(_i) = _iter; \
} while(0)
namespace al {
template<typename T, size_t alignment=DEF_ALIGN>
using vector = std::vector<T, al::allocator<T, alignment>>;
} // namespace al
#endif /* AL_VECTOR_H */
|