2014-03-21 00:54:37 -07:00
|
|
|
#ifndef AL_VECTOR_H
|
|
|
|
#define AL_VECTOR_H
|
|
|
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
|
|
|
#include <AL/al.h>
|
|
|
|
|
2016-05-21 03:27:51 -07:00
|
|
|
#include "almalloc.h"
|
|
|
|
|
2014-03-21 00:54:37 -07:00
|
|
|
|
2014-07-06 03:27:39 -07:00
|
|
|
#define TYPEDEF_VECTOR(T, N) typedef struct { \
|
2014-09-30 21:47:22 -07:00
|
|
|
size_t Capacity; \
|
|
|
|
size_t Size; \
|
2014-03-21 00:54:37 -07:00
|
|
|
T Data[]; \
|
2014-07-06 03:27:39 -07:00
|
|
|
} _##N; \
|
|
|
|
typedef _##N* N; \
|
|
|
|
typedef const _##N* const_##N;
|
2014-03-21 00:54:37 -07:00
|
|
|
|
2014-07-06 03:27:39 -07:00
|
|
|
#define VECTOR(T) struct { \
|
2014-09-30 21:47:22 -07:00
|
|
|
size_t Capacity; \
|
|
|
|
size_t Size; \
|
2014-07-06 03:27:39 -07:00
|
|
|
T Data[]; \
|
|
|
|
}*
|
|
|
|
|
|
|
|
#define VECTOR_INIT(_x) do { (_x) = NULL; } while(0)
|
|
|
|
#define VECTOR_INIT_STATIC() NULL
|
2016-05-21 03:27:51 -07:00
|
|
|
#define VECTOR_DEINIT(_x) do { al_free((_x)); (_x) = NULL; } while(0)
|
2014-03-21 00:54:37 -07:00
|
|
|
|
2018-10-28 05:43:51 -07:00
|
|
|
#ifndef __cplusplus
|
2016-07-26 11:45:20 -07:00
|
|
|
#define VECTOR_RESIZE(_x, _s, _c) do { \
|
|
|
|
size_t _size = (_s); \
|
|
|
|
size_t _cap = (_c); \
|
|
|
|
if(_size > _cap) \
|
|
|
|
_cap = _size; \
|
|
|
|
\
|
|
|
|
if(!(_x) && _cap == 0) \
|
|
|
|
break; \
|
|
|
|
\
|
|
|
|
if(((_x) ? (_x)->Capacity : 0) < _cap) \
|
|
|
|
{ \
|
2017-12-31 23:19:41 -08:00
|
|
|
ptrdiff_t data_offset = (_x) ? (char*)((_x)->Data) - (char*)(_x) : \
|
|
|
|
sizeof(*(_x)); \
|
2016-07-26 11:45:20 -07:00
|
|
|
size_t old_size = ((_x) ? (_x)->Size : 0); \
|
|
|
|
void *temp; \
|
|
|
|
\
|
2017-04-01 03:00:24 -07:00
|
|
|
temp = al_calloc(16, data_offset + sizeof((_x)->Data[0])*_cap); \
|
2016-07-26 11:45:20 -07:00
|
|
|
assert(temp != NULL); \
|
|
|
|
if((_x)) \
|
2017-04-01 03:00:24 -07:00
|
|
|
memcpy(((char*)temp)+data_offset, (_x)->Data, \
|
2016-07-26 11:45:20 -07:00
|
|
|
sizeof((_x)->Data[0])*old_size); \
|
|
|
|
\
|
|
|
|
al_free((_x)); \
|
|
|
|
(_x) = temp; \
|
|
|
|
(_x)->Capacity = _cap; \
|
|
|
|
} \
|
|
|
|
(_x)->Size = _size; \
|
|
|
|
} while(0) \
|
2014-03-28 05:40:24 -07:00
|
|
|
|
2018-10-28 05:43:51 -07:00
|
|
|
#else
|
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
inline void do_vector_resize(T *&vec, size_t size, 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);
|
|
|
|
T *temp;
|
|
|
|
|
|
|
|
temp = reinterpret_cast<T*>(al_calloc(16, data_offset + sizeof(vec->Data[0])*cap));
|
|
|
|
assert(temp != nullptr);
|
|
|
|
if(vec)
|
|
|
|
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)
|
|
|
|
#endif // __cplusplus
|
|
|
|
|
2014-07-01 22:27:53 -07:00
|
|
|
#define VECTOR_CAPACITY(_x) ((_x) ? (_x)->Capacity : 0)
|
|
|
|
#define VECTOR_SIZE(_x) ((_x) ? (_x)->Size : 0)
|
2014-03-21 00:54:37 -07:00
|
|
|
|
2016-04-15 12:22:54 -07:00
|
|
|
#define VECTOR_BEGIN(_x) ((_x) ? (_x)->Data + 0 : NULL)
|
|
|
|
#define VECTOR_END(_x) ((_x) ? (_x)->Data + (_x)->Size : NULL)
|
2014-03-21 00:54:37 -07:00
|
|
|
|
2016-07-26 11:45:20 -07:00
|
|
|
#define VECTOR_PUSH_BACK(_x, _obj) do { \
|
|
|
|
size_t _pbsize = VECTOR_SIZE(_x)+1; \
|
|
|
|
VECTOR_RESIZE(_x, _pbsize, _pbsize); \
|
|
|
|
(_x)->Data[(_x)->Size-1] = (_obj); \
|
|
|
|
} while(0)
|
2014-03-21 00:54:37 -07:00
|
|
|
#define VECTOR_POP_BACK(_x) ((void)((_x)->Size--))
|
|
|
|
|
2014-03-21 02:41:46 -07:00
|
|
|
#define VECTOR_BACK(_x) ((_x)->Data[(_x)->Size-1])
|
|
|
|
#define VECTOR_FRONT(_x) ((_x)->Data[0])
|
|
|
|
|
|
|
|
#define VECTOR_ELEM(_x, _o) ((_x)->Data[(_o)])
|
2014-03-21 00:54:37 -07:00
|
|
|
|
2014-04-30 12:30:02 -07:00
|
|
|
#define VECTOR_FOR_EACH(_t, _x, _f) do { \
|
2016-04-15 12:22:54 -07:00
|
|
|
_t *_iter = VECTOR_BEGIN((_x)); \
|
|
|
|
_t *_end = VECTOR_END((_x)); \
|
2014-04-30 12:30:02 -07:00
|
|
|
for(;_iter != _end;++_iter) \
|
2014-07-06 03:27:39 -07:00
|
|
|
_f(_iter); \
|
2014-04-30 12:30:02 -07:00
|
|
|
} while(0)
|
|
|
|
|
2014-07-06 05:00:05 -07:00
|
|
|
#define VECTOR_FIND_IF(_i, _t, _x, _f) do { \
|
2016-04-15 12:22:54 -07:00
|
|
|
_t *_iter = VECTOR_BEGIN((_x)); \
|
|
|
|
_t *_end = VECTOR_END((_x)); \
|
2014-07-06 22:11:29 -07:00
|
|
|
for(;_iter != _end;++_iter) \
|
2014-07-06 05:00:05 -07:00
|
|
|
{ \
|
2014-07-06 22:11:29 -07:00
|
|
|
if(_f(_iter)) \
|
2014-07-06 05:00:05 -07:00
|
|
|
break; \
|
|
|
|
} \
|
2014-07-06 22:11:29 -07:00
|
|
|
(_i) = _iter; \
|
2014-07-06 05:00:05 -07:00
|
|
|
} while(0)
|
|
|
|
|
2014-03-21 00:54:37 -07:00
|
|
|
#endif /* AL_VECTOR_H */
|