mkp224o/vec.h

67 lines
1.5 KiB
C
Raw Normal View History

#define VEC_STRUCT(typename,inttype) \
2017-09-24 21:13:16 +02:00
struct typename { \
inttype *buf; \
size_t len, alen; \
}
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))
#define VEC_ADD(ctl,val) { \
VEC_ADD1(ctl); \
(ctl).buf[(ctl).len - 1] = (val); \
}
void vec_addn(struct vec_basestruct *ctl,size_t sz,size_t n);
#define VEC_ADDN(ctl,n) \
2017-10-09 22:17:01 +02:00
vec_addn((struct vec_basestruct *)&(ctl),VEC_ELSIZE(ctl),(n))
2017-09-24 21:13:16 +02:00
2017-10-09 22:17:01 +02:00
#define VEC_REMOVEN(ctl,n,m) { \
(ctl).len -= m; \
memmove( \
2017-10-09 17:24:50 +02:00
&(ctl).buf[(n)], \
2017-10-09 22:17:01 +02:00
&(ctl).buf[(n + m)], \
((ctl).len - (n)) * VEC_ELSIZE(ctl)); \
}
2017-10-09 22:17:01 +02:00
#define VEC_REMOVE(ctl,n) VEC_REMOVEN(ctl,n,1)
2017-10-09 17:24:50 +02:00
#define VEC_INSERT1(ctl,n) { \
VEC_ADD1(ctl); \
memmove( \
2017-10-09 17:24:50 +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-09 17:24:50 +02:00
}
#define VEC_INSERT(ctl,n,val) { \
VEC_INSERT1(ctl,n); \
(ctl).buf[n] = (val); \
}
2017-10-09 17:24:50 +02:00
#define VEC_INSERTN(ctl,n,m) { \
VEC_ADDN(ctl,m); \
memmove( \
&(ctl).buf[(n + m)], \
&(ctl).buf[(n)], \
2017-10-09 22:17:01 +02:00
((ctl).len - (n) - (m)) * VEC_ELSIZE(ctl)); \
2017-10-09 17:24:50 +02:00
}
#define VEC_ZERO(ctl) { \
if ((ctl).buf) \
2017-10-09 22:17:01 +02:00
memset((ctl).buf,0,(ctl).len * VEC_ELSIZE(ctl)); \
}
#define VEC_FREE(ctl) { \
free((ctl).buf); \
2017-10-09 22:17:01 +02:00
memset(&(ctl), 0, sizeof(ctl)); \
}
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)