mkp224o/vec.h

74 lines
1.6 KiB
C
Raw Normal View History

#define VEC_STRUCT(typename,inttype) \
2017-09-24 21:13:16 +02:00
struct typename { \
inttype *buf; \
2017-10-10 05:14:45 +02:00
size_t len,alen; \
2017-09-24 21:13:16 +02:00
}
VEC_STRUCT(vec_basestruct,void) ;
2017-09-24 21:13:16 +02:00
#define VEC_INIT(ctl) memset(&ctl,0,sizeof(ctl))
2017-09-24 21:13:16 +02:00
2017-10-09 22:17:01 +02:00
#define VEC_ELSIZE(ctl) (sizeof(*(ctl).buf))
void vec_add1(struct vec_basestruct *ctl,size_t sz);
#define VEC_ADD1(ctl) \
2017-10-09 22:17:01 +02:00
vec_add1((struct vec_basestruct *)&(ctl),VEC_ELSIZE(ctl))
2017-10-21 20:42:19 +02:00
#define VEC_ADD(ctl,val) \
do { \
VEC_ADD1(ctl); \
(ctl).buf[(ctl).len - 1] = (val); \
2017-10-21 20:42:19 +02:00
} while (0)
void vec_addn(struct vec_basestruct *ctl,size_t sz,size_t n);
#define VEC_ADDN(ctl,n) \
2018-02-23 01:03:17 +01:00
vec_addn((struct vec_basestruct *)&(ctl),VEC_ELSIZE(ctl),(size_t)(n))
2017-09-24 21:13:16 +02:00
2017-10-21 20:42:19 +02:00
#define VEC_REMOVEN(ctl,n,m) \
do { \
2017-10-09 22:17:01 +02:00
(ctl).len -= m; \
memmove( \
2017-10-10 05:14:45 +02:00
&(ctl).buf[n], \
&(ctl).buf[(n) + (m)], \
2017-10-09 22:17:01 +02:00
((ctl).len - (n)) * VEC_ELSIZE(ctl)); \
2017-10-21 20:42:19 +02:00
} while (0)
2017-10-09 22:17:01 +02:00
#define VEC_REMOVE(ctl,n) VEC_REMOVEN(ctl,n,1)
2017-10-21 20:42:19 +02:00
#define VEC_INSERT1(ctl,n) \
do { \
VEC_ADD1(ctl); \
memmove( \
2017-10-10 05:14:45 +02:00
&(ctl).buf[(n) + 1], \
&(ctl).buf[n], \
2017-10-09 22:17:01 +02:00
((ctl).len - (n) - 1) * VEC_ELSIZE(ctl)); \
2017-10-21 20:42:19 +02:00
} while (0)
#define VEC_INSERT(ctl,n,val) \
do { \
2017-10-09 17:24:50 +02:00
VEC_INSERT1(ctl,n); \
(ctl).buf[n] = (val); \
2017-10-21 20:42:19 +02:00
} while (0)
2017-10-21 20:42:19 +02:00
#define VEC_INSERTN(ctl,n,m) \
do { \
2017-10-09 17:24:50 +02:00
VEC_ADDN(ctl,m); \
memmove( \
2017-10-10 05:14:45 +02:00
&(ctl).buf[(n) + (m)], \
&(ctl).buf[n], \
2017-10-09 22:17:01 +02:00
((ctl).len - (n) - (m)) * VEC_ELSIZE(ctl)); \
2017-10-21 20:42:19 +02:00
} while (0)
2017-10-09 17:24:50 +02:00
2017-10-21 20:42:19 +02:00
#define VEC_ZERO(ctl) \
do { \
if ((ctl).buf) \
2017-10-09 22:17:01 +02:00
memset((ctl).buf,0,(ctl).len * VEC_ELSIZE(ctl)); \
2017-10-21 20:42:19 +02:00
} while (0)
2017-10-21 20:42:19 +02:00
#define VEC_FREE(ctl) \
do { \
free((ctl).buf); \
2017-10-09 22:17:01 +02:00
memset(&(ctl), 0, sizeof(ctl)); \
2017-10-21 20:42:19 +02:00
} while (0)
2017-09-24 21:13:16 +02:00
#define VEC_LENGTH(ctl) ((ctl).len)
#define VEC_BUF(ctl,num) ((ctl).buf[num])
#define VEC_FOR(ctl,it) for (size_t it = 0;it < VEC_LENGTH((ctl));++it)