array.h 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. #ifndef HW_ARRAY_H_
  2. #define HW_ARRAY_H_
  3. #include <assert.h> // for assert
  4. #include <stdlib.h> // for malloc,realloc,free
  5. #include <string.h> // for memset,memmove
  6. // #include <vector>
  7. // typedef std::vector<type> atype;
  8. #define ARRAY_DECL(type, atype) \
  9. struct atype { \
  10. type* ptr; \
  11. size_t size; \
  12. size_t maxsize;\
  13. }; \
  14. typedef struct atype atype;\
  15. \
  16. static inline type* atype##_data(atype* p) {\
  17. return p->ptr;\
  18. }\
  19. \
  20. static inline int atype##_size(atype* p) {\
  21. return p->size;\
  22. }\
  23. \
  24. static inline int atype##_maxsize(atype* p) {\
  25. return p->maxsize;\
  26. }\
  27. \
  28. static inline int atype##_empty(atype* p) {\
  29. return p->size == 0;\
  30. }\
  31. \
  32. static inline type* atype##_at(atype* p, int pos) {\
  33. if (pos < 0) {\
  34. pos += p->size;\
  35. }\
  36. assert(pos >= 0 && pos < p->size);\
  37. return p->ptr + pos;\
  38. }\
  39. \
  40. static inline type* atype##_front(atype* p) {\
  41. return p->size == 0 ? NULL : p->ptr;\
  42. }\
  43. \
  44. static inline type* atype##_back(atype* p) {\
  45. return p->size == 0 ? NULL : p->ptr+p->size-1;\
  46. }\
  47. \
  48. static inline void atype##_init(atype* p, int maxsize) {\
  49. p->size = 0;\
  50. p->maxsize = maxsize;\
  51. size_t bytes = sizeof(type) * maxsize;\
  52. p->ptr = (type*)malloc(bytes);\
  53. memset(p->ptr, 0, bytes);\
  54. }\
  55. \
  56. static inline void atype##_cleanup(atype* p) {\
  57. if (p->ptr) {\
  58. free(p->ptr);\
  59. p->ptr = NULL;\
  60. }\
  61. p->size = p->maxsize = 0;\
  62. }\
  63. \
  64. static inline void atype##_resize(atype* p, int maxsize) {\
  65. p->maxsize = maxsize;\
  66. int bytes = sizeof(type) * maxsize;\
  67. p->ptr = (type*)realloc(p->ptr, bytes);\
  68. }\
  69. \
  70. static inline void atype##_double_resize(atype* p) {\
  71. assert(p->maxsize != 0);\
  72. return atype##_resize(p, p->maxsize*2);\
  73. }\
  74. \
  75. static inline void atype##_add(atype* p, type* elem, int pos) {\
  76. if (pos < 0) {\
  77. pos += p->size;\
  78. }\
  79. assert(pos >= 0 && pos <= p->size);\
  80. if (p->size == p->maxsize) {\
  81. atype##_double_resize(p);\
  82. }\
  83. if (pos < p->size) {\
  84. memmove(p->ptr + pos+1, p->ptr + pos, sizeof(type)*(p->size-pos));\
  85. }\
  86. p->ptr[pos] = *elem;\
  87. p->size++;\
  88. }\
  89. \
  90. static inline void atype##_del(atype* p, int pos) {\
  91. if (pos < 0) {\
  92. pos += p->size;\
  93. }\
  94. assert(pos >= 0 && pos < p->size);\
  95. p->size--;\
  96. if (pos < p->size) {\
  97. memmove(p->ptr + pos, p->ptr + pos+1, sizeof(type)*(p->size-pos));\
  98. }\
  99. }\
  100. \
  101. static inline void atype##_push_back(atype* p, type* elem) {\
  102. if (p->size == p->maxsize) {\
  103. atype##_double_resize(p);\
  104. }\
  105. p->ptr[p->size] = *elem;\
  106. p->size++;\
  107. }\
  108. \
  109. static inline void atype##_pop_back(atype* p) {\
  110. assert(p->size > 0);\
  111. p->size--;\
  112. }\
  113. \
  114. static inline void atype##_swap(atype* p, int pos1, int pos2) {\
  115. if (pos1 < 0) {\
  116. pos1 += p->size;\
  117. }\
  118. if (pos2 < 0) {\
  119. pos2 += p->size;\
  120. }\
  121. type tmp = p->ptr[pos1];\
  122. p->ptr[pos1] = p->ptr[pos2];\
  123. p->ptr[pos2] = tmp;\
  124. }\
  125. \
  126. static inline void atype##_del_nomove(atype* p, int pos) {\
  127. if (pos < 0) {\
  128. pos += p->size;\
  129. }\
  130. assert(pos >= 0 && pos < p->size);\
  131. p->size--;\
  132. if (pos < p->size) {\
  133. p->ptr[pos] = p->ptr[p->size];\
  134. }\
  135. }\
  136. #endif