queue.h 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. #ifndef HW_QUEUE_H_
  2. #define HW_QUEUE_H_
  3. /*
  4. * queue
  5. * FIFO: push_back,pop_front
  6. */
  7. #include <assert.h> // for assert
  8. #include <stdlib.h> // for malloc,realloc,free
  9. #include <string.h> // for memset,memmove
  10. // #include <deque>
  11. // typedef std::deque<type> qtype;
  12. #define QUEUE_DECL(type, qtype) \
  13. struct qtype { \
  14. type* ptr; \
  15. size_t size; \
  16. size_t maxsize;\
  17. size_t _offset;\
  18. }; \
  19. typedef struct qtype qtype;\
  20. \
  21. static inline type* qtype##_data(qtype* p) {\
  22. return p->ptr + p->_offset;\
  23. }\
  24. \
  25. static inline int qtype##_size(qtype* p) {\
  26. return p->size;\
  27. }\
  28. \
  29. static inline int qtype##_maxsize(qtype* p) {\
  30. return p->maxsize;\
  31. }\
  32. \
  33. static inline int qtype##_empty(qtype* p) {\
  34. return p->size == 0;\
  35. }\
  36. \
  37. static inline type* qtype##_front(qtype* p) {\
  38. return p->size == 0 ? NULL : p->ptr + p->_offset;\
  39. }\
  40. \
  41. static inline type* qtype##_back(qtype* p) {\
  42. return p->size == 0 ? NULL : p->ptr + p->_offset + p->size-1;\
  43. }\
  44. \
  45. static inline void qtype##_init(qtype* p, int maxsize) {\
  46. p->_offset = 0;\
  47. p->size = 0;\
  48. p->maxsize = maxsize;\
  49. size_t bytes = sizeof(type) * maxsize;\
  50. p->ptr = (type*)malloc(bytes);\
  51. memset(p->ptr, 0, bytes);\
  52. }\
  53. static inline void qtype##_clear(qtype* p) {\
  54. p->_offset = 0;\
  55. p->size = 0;\
  56. memset(p->ptr, 0, sizeof(type) * p->maxsize);\
  57. }\
  58. \
  59. static inline void qtype##_cleanup(qtype* p) {\
  60. if (p->ptr) {\
  61. free(p->ptr);\
  62. p->ptr = NULL;\
  63. }\
  64. p->_offset = p->size = p->maxsize = 0;\
  65. }\
  66. \
  67. static inline void qtype##_resize(qtype* p, int maxsize) {\
  68. p->maxsize = maxsize;\
  69. int bytes = sizeof(type) * maxsize;\
  70. p->ptr = (type*)realloc(p->ptr, bytes);\
  71. }\
  72. \
  73. static inline void qtype##_double_resize(qtype* p) {\
  74. assert(p->maxsize != 0);\
  75. return 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 // HW_QUEUE_H_