From 90f42c16757f089706c83ef82b2afaa212f462af Mon Sep 17 00:00:00 2001 From: cathugger Date: Mon, 9 Oct 2017 20:17:01 +0000 Subject: [PATCH] add .gitignore, further improve vec --- .gitignore | 2 ++ vec.h | 24 +++++++++++++----------- 2 files changed, 15 insertions(+), 11 deletions(-) create mode 100644 .gitignore diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..8ce1640 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +*.o +*.onion diff --git a/vec.h b/vec.h index c6db384..51cc710 100644 --- a/vec.h +++ b/vec.h @@ -7,9 +7,11 @@ VEC_STRUCT(vec_basestruct,void) ; #define VEC_INIT(ctl) memset(&ctl,0,sizeof(ctl)) +#define VEC_ELSIZE(ctl) (sizeof(*(ctl).buf)) + void vec_add1(struct vec_basestruct *ctl,size_t sz); #define VEC_ADD1(ctl) \ - vec_add1((struct vec_basestruct *)&(ctl),sizeof(*(ctl).buf)) + vec_add1((struct vec_basestruct *)&(ctl),VEC_ELSIZE(ctl)) #define VEC_ADD(ctl,val) { \ VEC_ADD1(ctl); \ (ctl).buf[(ctl).len - 1] = (val); \ @@ -17,24 +19,24 @@ void vec_add1(struct vec_basestruct *ctl,size_t sz); void vec_addn(struct vec_basestruct *ctl,size_t sz,size_t n); #define VEC_ADDN(ctl,n) \ - vec_addn((struct vec_basestruct *)&(ctl),sizeof(*(ctl).buf),(n)) + vec_addn((struct vec_basestruct *)&(ctl),VEC_ELSIZE(ctl),(n)) -#define VEC_REMOVE(ctl,n) { \ - --(ctl).len; \ +#define VEC_REMOVEN(ctl,n,m) { \ + (ctl).len -= m; \ memmove( \ &(ctl).buf[(n)], \ - &(ctl).buf[(n + 1)], \ - ((ctl).len - (n)) * sizeof(*(ctl).buf)); \ + &(ctl).buf[(n + m)], \ + ((ctl).len - (n)) * VEC_ELSIZE(ctl)); \ } +#define VEC_REMOVE(ctl,n) VEC_REMOVEN(ctl,n,1) #define VEC_INSERT1(ctl,n) { \ VEC_ADD1(ctl); \ memmove( \ &(ctl).buf[(n + 1)], \ &(ctl).buf[(n)], \ - ((ctl).len - (n) - 1) * sizeof(*(ctl).buf)); \ + ((ctl).len - (n) - 1) * VEC_ELSIZE(ctl)); \ } - #define VEC_INSERT(ctl,n,val) { \ VEC_INSERT1(ctl,n); \ (ctl).buf[n] = (val); \ @@ -45,17 +47,17 @@ void vec_addn(struct vec_basestruct *ctl,size_t sz,size_t n); memmove( \ &(ctl).buf[(n + m)], \ &(ctl).buf[(n)], \ - ((ctl).len - (n) - (m)) * sizeof(*(ctl).buf)); \ + ((ctl).len - (n) - (m)) * VEC_ELSIZE(ctl)); \ } #define VEC_ZERO(ctl) { \ if ((ctl).buf) \ - memset((ctl).buf,0,(ctl).len * sizeof(*(ctl).buf)); \ + memset((ctl).buf,0,(ctl).len * VEC_ELSIZE(ctl)); \ } #define VEC_FREE(ctl) { \ free((ctl).buf); \ - memset(&(ctl), 0, sizeof((ctl))); \ + memset(&(ctl), 0, sizeof(ctl)); \ } #define VEC_LENGTH(ctl) ((ctl).len)