1
0

queue.h 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. #ifndef HV_QUEUE_H_
  2. #define HV_QUEUE_H_
  3. /*
  4. * queue
  5. * FIFO: push_back,pop_front
  6. * stack
  7. * LIFO: push_back,pop_back
  8. */
  9. #include <assert.h> // for assert
  10. #include <stddef.h> // for NULL
  11. #include <stdlib.h> // for malloc,realloc,free
  12. #include <string.h> // for memset,memmove
  13. #include "hbase.h" // for HV_ALLOC, HV_FREE
  14. #define QUEUE_INIT_SIZE 16
  15. // #include <deque>
  16. // typedef std::deque<type> qtype;
  17. #define QUEUE_DECL(type, qtype) \
  18. struct qtype { \
  19. type* ptr; \
  20. size_t size; \
  21. size_t maxsize;\
  22. size_t _offset;\
  23. }; \
  24. typedef struct qtype qtype;\
  25. \
  26. static inline type* qtype##_data(qtype* p) {\
  27. return p->ptr + p->_offset;\
  28. }\
  29. \
  30. static inline int qtype##_size(qtype* p) {\
  31. return p->size;\
  32. }\
  33. \
  34. static inline int qtype##_maxsize(qtype* p) {\
  35. return p->maxsize;\
  36. }\
  37. \
  38. static inline int qtype##_empty(qtype* p) {\
  39. return p->size == 0;\
  40. }\
  41. \
  42. static inline type* qtype##_front(qtype* p) {\
  43. return p->size == 0 ? NULL : p->ptr + p->_offset;\
  44. }\
  45. \
  46. static inline type* qtype##_back(qtype* p) {\
  47. return p->size == 0 ? NULL : p->ptr + p->_offset + p->size-1;\
  48. }\
  49. \
  50. static inline void qtype##_init(qtype* p, int maxsize) {\
  51. p->_offset = 0;\
  52. p->size = 0;\
  53. p->maxsize = maxsize;\
  54. HV_ALLOC(p->ptr, sizeof(type) * maxsize);\
  55. }\
  56. \
  57. static inline void qtype##_clear(qtype* p) {\
  58. p->_offset = 0;\
  59. p->size = 0;\
  60. memset(p->ptr, 0, sizeof(type) * p->maxsize);\
  61. }\
  62. \
  63. static inline void qtype##_cleanup(qtype* p) {\
  64. HV_FREE(p->ptr);\
  65. p->_offset = p->size = p->maxsize = 0;\
  66. }\
  67. \
  68. static inline void qtype##_resize(qtype* p, int maxsize) {\
  69. if (maxsize == 0) maxsize = QUEUE_INIT_SIZE;\
  70. p->ptr = (type*)safe_realloc(p->ptr, sizeof(type)*maxsize, sizeof(type)*p->maxsize);\
  71. p->maxsize = maxsize;\
  72. }\
  73. \
  74. static inline void qtype##_double_resize(qtype* p) {\
  75. qtype##_resize(p, p->maxsize*2);\
  76. }\
  77. \
  78. static inline void qtype##_push_back(qtype* p, type* elem) {\
  79. if (p->size == p->maxsize) {\
  80. qtype##_double_resize(p);\
  81. }\
  82. else if (p->_offset + p->size == p->maxsize) {\
  83. memmove(p->ptr, p->ptr + p->_offset, p->size);\
  84. p->_offset = 0;\
  85. }\
  86. p->ptr[p->_offset + p->size] = *elem;\
  87. p->size++;\
  88. }\
  89. static inline void qtype##_pop_front(qtype* p) {\
  90. assert(p->size > 0);\
  91. p->size--;\
  92. if (++p->_offset == p->maxsize) p->_offset = 0;\
  93. }\
  94. \
  95. static inline void qtype##_pop_back(qtype* p) {\
  96. assert(p->size > 0);\
  97. p->size--;\
  98. }\
  99. #endif // HV_QUEUE_H_