vec.h 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. #define VEC_STRUCT(typename,inttype) \
  2. struct typename { \
  3. inttype *buf; \
  4. size_t len,alen; \
  5. }
  6. VEC_STRUCT(vec_basestruct,void) ;
  7. #define VEC_INIT(ctl) memset(&ctl,0,sizeof(ctl))
  8. #define VEC_ELSIZE(ctl) (sizeof(*(ctl).buf))
  9. void vec_add1(struct vec_basestruct *ctl,size_t sz);
  10. #define VEC_ADD1(ctl) \
  11. vec_add1((struct vec_basestruct *)&(ctl),VEC_ELSIZE(ctl))
  12. #define VEC_ADD(ctl,val) \
  13. do { \
  14. VEC_ADD1(ctl); \
  15. (ctl).buf[(ctl).len - 1] = (val); \
  16. } while (0)
  17. void vec_addn(struct vec_basestruct *ctl,size_t sz,size_t n);
  18. #define VEC_ADDN(ctl,n) \
  19. vec_addn((struct vec_basestruct *)&(ctl),VEC_ELSIZE(ctl),(size_t)(n))
  20. #define VEC_SETLENGTH(ctl,n) \
  21. do { \
  22. (ctl).len = n; \
  23. } while (0)
  24. #define VEC_REMOVEN(ctl,n,m) \
  25. do { \
  26. (ctl).len -= m; \
  27. memmove( \
  28. &(ctl).buf[n], \
  29. &(ctl).buf[(n) + (m)], \
  30. ((ctl).len - (n)) * VEC_ELSIZE(ctl)); \
  31. } while (0)
  32. #define VEC_REMOVE(ctl,n) VEC_REMOVEN(ctl,n,1)
  33. #define VEC_INSERT1(ctl,n) \
  34. do { \
  35. VEC_ADD1(ctl); \
  36. memmove( \
  37. &(ctl).buf[(n) + 1], \
  38. &(ctl).buf[n], \
  39. ((ctl).len - (n) - 1) * VEC_ELSIZE(ctl)); \
  40. } while (0)
  41. #define VEC_INSERT(ctl,n,val) \
  42. do { \
  43. VEC_INSERT1(ctl,n); \
  44. (ctl).buf[n] = (val); \
  45. } while (0)
  46. #define VEC_INSERTN(ctl,n,m) \
  47. do { \
  48. VEC_ADDN(ctl,m); \
  49. memmove( \
  50. &(ctl).buf[(n) + (m)], \
  51. &(ctl).buf[n], \
  52. ((ctl).len - (n) - (m)) * VEC_ELSIZE(ctl)); \
  53. } while (0)
  54. #define VEC_ZERO(ctl) \
  55. do { \
  56. if ((ctl).buf) \
  57. memset((ctl).buf,0,(ctl).len * VEC_ELSIZE(ctl)); \
  58. } while (0)
  59. #define VEC_FREE(ctl) \
  60. do { \
  61. free((ctl).buf); \
  62. memset(&(ctl), 0, sizeof(ctl)); \
  63. } while (0)
  64. #define VEC_LENGTH(ctl) ((ctl).len)
  65. #define VEC_BUF(ctl,num) ((ctl).buf[num])
  66. #define VEC_FOR(ctl,it) for (size_t it = 0;it < VEC_LENGTH((ctl));++it)