1
0

array.h 3.4 KB

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