Microprocessor and peripheral 2 assignments for AUTH
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. /*!
  2. * \file deque08.c
  3. * \brief
  4. * This file provides double ended queue capability based on a ring buffer
  5. *
  6. * Author: Christos Choutouridis AEM: 8997
  7. * email : <cchoutou@ece.auth.gr>
  8. *
  9. */
  10. #include "deque08.h"
  11. /*
  12. * ============= Private Queue API =============
  13. */
  14. static iterator_t _preInc(deque08_t *q, iterator_t* it) {
  15. return (++*it >= q->capacity) ? *it=0 : *it;
  16. }
  17. static iterator_t _preDec(deque08_t *q, iterator_t* it) {
  18. return (--*it < 0) ? *it=q->capacity-1 : *it;
  19. }
  20. static iterator_t _postInc(deque08_t *q, iterator_t* it) {
  21. iterator_t ret = *it;
  22. if (++*it >= q->capacity) *it=0;
  23. return ret;
  24. }
  25. static iterator_t _postDec(deque08_t *q, iterator_t* it) {
  26. iterator_t ret = *it;
  27. if (--*it < 0) *it=q->capacity-1;
  28. return ret;
  29. }
  30. /*
  31. * ============= Public Queue API =============
  32. */
  33. /*
  34. * Link and Glue functions
  35. */
  36. void deque08_link_buffer (deque08_t *q, byte_t* buf) {
  37. q->m = buf;
  38. }
  39. /*
  40. * Set functions
  41. */
  42. inline void deque08_set_capacity (deque08_t *q, size_t capacity) {
  43. q->capacity = capacity;
  44. }
  45. /*
  46. * User Functions
  47. */
  48. /*!
  49. * \brief
  50. * Check if deque is full
  51. * \param q Which deque to check
  52. * \return
  53. * \arg 0 Not full
  54. * \arg 1 Full
  55. */
  56. int deque08_is_full (deque08_t *q) {
  57. return (q->items == q->capacity) ? 1:0;
  58. }
  59. /*!
  60. * \brief
  61. * Check if deque is empty
  62. * \param q Which deque to check
  63. * \return
  64. * \arg 0 Not empty
  65. * \arg 1 Empty
  66. */
  67. int deque08_is_empty (deque08_t *q) {
  68. return (q->items) ? 0:1;
  69. }
  70. /*!
  71. * \brief
  72. * Return the number of items on deque
  73. * \param q Which deque to check
  74. */
  75. int deque08_size (deque08_t *q) {
  76. return q->items;
  77. }
  78. /*!
  79. * \brief
  80. * Discard all items in deque
  81. * \param q Which deque to check
  82. */
  83. void deque08_flush (deque08_t *q) {
  84. deque08_init(q);
  85. }
  86. /*!
  87. * \brief
  88. * Initialize the queue
  89. * \param queue Which queue to init
  90. */
  91. void deque08_init (deque08_t *q) {
  92. q->f = 0;
  93. q->r = -1;
  94. q->items =0;
  95. }
  96. /*!
  97. * \brief
  98. * This function push a byte in front of deque.
  99. * \param q Pointer to deque to use
  100. * \param b byte to push
  101. * \return
  102. * \arg 0 Full queue
  103. * \arg 1 Done
  104. */
  105. int deque08_push_front (deque08_t *q, byte_t b) {
  106. if (deque08_is_full (q) == 1) //full queue
  107. return 0;
  108. q->m [_preDec (q, &q->f)] = b;
  109. ++q->items;
  110. return 1;
  111. }
  112. /*!
  113. * \brief
  114. * This function pops a byte from the front of the deque.
  115. * \param q Pointer to deque to use
  116. * \param b Pointer to byte to return
  117. * \return
  118. * \arg 0 Empty queue
  119. * \arg 1 Done
  120. */
  121. int deque08_pop_front (deque08_t *q, byte_t *b) {
  122. if (deque08_is_empty (q) == 1) //empty queue
  123. return 0;
  124. *b = q->m [_postInc (q, &q->f)];
  125. --q->items;
  126. return 1;
  127. }
  128. /*!
  129. * \brief
  130. * This function push a byte in the back of deque.
  131. * \param q Pointer to deque to use
  132. * \param b byte to push
  133. * \return
  134. * \arg 0 Full queue
  135. * \arg 1 Done
  136. */
  137. int deque08_push_back (deque08_t *q, byte_t b) {
  138. if (deque08_is_full (q) == 1) //full queue
  139. return 0;
  140. q->m [_preInc (q, &q->r)] = b;
  141. ++q->items;
  142. return 1;
  143. }
  144. /*!
  145. * \brief
  146. * This function pops a byte from the back of the deque.
  147. * \param q Pointer to deque to use
  148. * \param b Pointer to byte to return
  149. * \return
  150. * \arg 0 Empty queue
  151. * \arg 1 Done
  152. */
  153. int deque08_pop_back (deque08_t *q, byte_t *b) {
  154. if (deque08_is_empty (q) == 1) //empty queue
  155. return 0;
  156. *b = q->m [_postDec (q, &q->r)];
  157. --q->items;
  158. return 1;
  159. }
  160. /*!
  161. * \brief
  162. * This function gives the last item in the back of deque.
  163. * \param q Pointer to deque to use
  164. * \param b Pointer to byte to return
  165. * \return
  166. * \arg 0 Empty queue
  167. * \arg 1 Done
  168. */
  169. int deque08_back (deque08_t *q, byte_t *b) {
  170. if (deque08_is_empty (q) == 1) //empty queue
  171. return 0;
  172. *b = q->m [q->r];
  173. return 1;
  174. }
  175. /*!
  176. * \brief
  177. * This function gives the first item in the front of deque.
  178. * \param q Pointer to deque to use
  179. * \param b Pointer to byte to return
  180. * \return
  181. * \arg 0 Empty queue
  182. * \arg 1 Done
  183. */
  184. int deque08_front (deque08_t *q, byte_t *b) {
  185. if (deque08_is_empty (q) == 1) //empty queue
  186. return 0;
  187. *b = q->m [q->f];
  188. return 1;
  189. }