Queue.h 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  1. /*
  2. Copyright (c) 2013 Daniele Bartolini, Michele Rossi
  3. Copyright (c) 2012 Daniele Bartolini, Simone Boscaratto
  4. Permission is hereby granted, free of charge, to any person
  5. obtaining a copy of this software and associated documentation
  6. files (the "Software"), to deal in the Software without
  7. restriction, including without limitation the rights to use,
  8. copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. copies of the Software, and to permit persons to whom the
  10. Software is furnished to do so, subject to the following
  11. conditions:
  12. The above copyright notice and this permission notice shall be
  13. included in all copies or substantial portions of the Software.
  14. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  15. EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
  16. OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  17. NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
  18. HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
  19. WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  20. FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  21. OTHER DEALINGS IN THE SOFTWARE.
  22. */
  23. #pragma once
  24. #include "ContainerTypes.h"
  25. #include "Array.h"
  26. #include "Assert.h"
  27. #include <cstring>
  28. namespace crown
  29. {
  30. namespace queue
  31. {
  32. /// Returns whether the queue is empty.
  33. template<typename T> bool empty(const Queue<T>& q);
  34. /// Returns the number of items in the queue
  35. template<typename T> uint32_t size(const Queue<T>& q);
  36. /// Returns the number of items the queue can hold before
  37. /// a resize must occur.
  38. template<typename T> uint32_t space(const Queue<T>& q);
  39. /// Increase or decrease the capacity of the queue.
  40. /// @note
  41. /// Old items will be copied to the newly created queue.
  42. /// If the new @a capacity is smaller than the previous one, the
  43. /// queue will be truncated.
  44. template<typename T> void increase_capacity(Queue<T>& q, uint32_t capacity);
  45. /// Grows the queue to contain at least @a min_capacity items.
  46. /// If @a min_capacity is set to 0, the queue automatically
  47. /// determines the new capacity based on its size at the
  48. /// time of call.
  49. template<typename T> void grow(Queue<T>& q, uint32_t min_capacity);
  50. /// Appends an @a item to the back of the queue
  51. template<typename T> void push_back(Queue<T>& q, const T& item);
  52. /// Removes the last item from the queue
  53. template<typename T> void pop_back(Queue<T>& q);
  54. /// Appends an @a item to the front of the queue
  55. template<typename T> void push_front(Queue<T>& q, const T& item);
  56. /// Removes the first item from the queue
  57. template<typename T> void pop_front(Queue<T>& q);
  58. /// Appends @a n @a items to the back of the queue
  59. template<typename T> void push(Queue<T>& q, const T *items, uint32_t n);
  60. /// Removes @a n items from the front of the queue
  61. template<typename T> void pop(Queue<T>& q, uint32_t n);
  62. /// Clears the content of the queue.
  63. /// @note
  64. /// Does not free memory nor call destructors, it only zeroes
  65. /// the number of items in the queue for efficiency.
  66. template<typename T> void clear(Queue<T>& q);
  67. template<typename T> T* begin(Queue<T>& q);
  68. template<typename T> const T* begin(const Queue<T>& q);
  69. template<typename T> T* end(Queue<T>& q);
  70. template<typename T> const T* end(const Queue<T>& q);
  71. template<typename T> T& front(Queue<T>& q);
  72. template<typename T> const T& front(const Queue<T>& q);
  73. template<typename T> T& back(Queue<T>& q);
  74. template<typename T> const T& back(const Queue<T>& q);
  75. } // namespace queue
  76. namespace queue
  77. {
  78. //-----------------------------------------------------------------------------
  79. template <typename T>
  80. inline bool empty(const Queue<T>& q)
  81. {
  82. return q.m_size == 0;
  83. }
  84. //-----------------------------------------------------------------------------
  85. template <typename T>
  86. inline uint32_t size(const Queue<T>& q)
  87. {
  88. return q.m_size;
  89. }
  90. //-----------------------------------------------------------------------------
  91. template <typename T>
  92. inline uint32_t space(const Queue<T>& q)
  93. {
  94. return array::size(q.m_queue) - q.m_size;
  95. }
  96. //-----------------------------------------------------------------------------
  97. template <typename T>
  98. inline void increase_capacity(Queue<T>& q, uint32_t capacity)
  99. {
  100. uint32_t old_size = array::size(q.m_queue);
  101. array::resize(q.m_queue, capacity);
  102. if (q.m_read + q.m_size > old_size)
  103. {
  104. memmove(array::begin(q.m_queue) + capacity - (old_size - q.m_read), array::begin(q.m_queue) + q.m_read, (old_size - q.m_read) * sizeof(T));
  105. q.m_read += (capacity - old_size);
  106. }
  107. }
  108. //-----------------------------------------------------------------------------
  109. template <typename T>
  110. inline void grow(Queue<T>& q, uint32_t min_capacity)
  111. {
  112. uint32_t new_capacity = array::size(q.m_queue) * 2 + 1;
  113. if (new_capacity < min_capacity)
  114. {
  115. new_capacity = min_capacity;
  116. }
  117. increase_capacity(q, new_capacity);
  118. }
  119. //-----------------------------------------------------------------------------
  120. template <typename T>
  121. inline void push_back(Queue<T>& q, const T& item)
  122. {
  123. if (space(q) == 0)
  124. {
  125. grow(q, 0);
  126. }
  127. q[q.m_size] = item;
  128. q.m_size++;
  129. }
  130. //-----------------------------------------------------------------------------
  131. template <typename T>
  132. inline void pop_back(Queue<T>& q)
  133. {
  134. CE_ASSERT(q.m_size > 0, "The queue is empty");
  135. q.m_size--;
  136. }
  137. //-----------------------------------------------------------------------------
  138. template <typename T>
  139. inline void push_front(Queue<T>& q, const T& item)
  140. {
  141. if (space(q) == 0)
  142. {
  143. grow(q, 0);
  144. }
  145. q.m_read = (q.m_read - 1 + array::size(q.m_queue)) % array::size(q.m_queue);
  146. q[0] = item;
  147. q.m_size++;
  148. }
  149. //-----------------------------------------------------------------------------
  150. template <typename T>
  151. inline void pop_front(Queue<T>& q)
  152. {
  153. CE_ASSERT(q.m_size > 0, "The queue is empty");
  154. q.m_read = (q.m_read + 1) % array::size(q.m_queue);
  155. q.m_size--;
  156. }
  157. //-----------------------------------------------------------------------------
  158. template <typename T>
  159. inline void push(Queue<T>& q, const T *items, uint32_t n)
  160. {
  161. if (q.space() < n)
  162. {
  163. q.grow(q.size() + n);
  164. }
  165. const uint32_t size = array::size(q.m_queue);
  166. const uint32_t insert = (q.m_read + q.m_size) % size;
  167. uint32_t to_insert = n;
  168. if (insert + to_insert > size)
  169. {
  170. to_insert = size - insert;
  171. }
  172. memcpy(array::begin(q.m_queue) + insert, items, to_insert * sizeof(T));
  173. q.m_size += to_insert;
  174. items += to_insert;
  175. n -= to_insert;
  176. memcpy(array::begin(q.m_queue), items, n * sizeof(T));
  177. q.m_size += n;
  178. }
  179. //-----------------------------------------------------------------------------
  180. template <typename T>
  181. inline void pop(Queue<T>& q, uint32_t n)
  182. {
  183. CE_ASSERT(q.m_size > 0, "The queue is empty");
  184. q.m_read = (q.m_read + n) % array::size(q.m_queue);
  185. q.m_size -= n;
  186. }
  187. //-----------------------------------------------------------------------------
  188. template <typename T>
  189. inline void clear(Queue<T>& q)
  190. {
  191. q.m_read = 0;
  192. q.m_size = 0;
  193. }
  194. //-----------------------------------------------------------------------------
  195. template <typename T>
  196. inline T* begin(Queue<T>& q)
  197. {
  198. return array::begin(q.m_queue) + q.m_read;
  199. }
  200. //-----------------------------------------------------------------------------
  201. template <typename T>
  202. inline const T* begin(const Queue<T>& q)
  203. {
  204. return array::begin(q.m_queue) + q.m_read;
  205. }
  206. //-----------------------------------------------------------------------------
  207. template <typename T>
  208. inline T* end(Queue<T>& q)
  209. {
  210. uint32_t end = q.m_read + q.m_size;
  211. return end >= array::size(q.m_queue) ? array::end(q.m_queue) : array::begin(q.m_queue) + end;
  212. }
  213. //-----------------------------------------------------------------------------
  214. template <typename T>
  215. inline const T* end(const Queue<T>& q)
  216. {
  217. uint32_t end = q.m_read + q.m_size;
  218. return end >= array::size(q.m_queue) ? array::end(q.m_queue) : array::begin(q.m_queue) + end;
  219. }
  220. //-----------------------------------------------------------------------------
  221. template <typename T>
  222. inline T& front(Queue<T>& q)
  223. {
  224. CE_ASSERT(q.m_size > 0, "The queue is empty");
  225. return q.m_queue[q.m_read];
  226. }
  227. //-----------------------------------------------------------------------------
  228. template <typename T>
  229. inline const T& front(const Queue<T>& q)
  230. {
  231. CE_ASSERT(q.m_size > 0, "The queue is empty");
  232. return q.m_queue[q.m_read];
  233. }
  234. //-----------------------------------------------------------------------------
  235. template <typename T>
  236. inline T& back(Queue<T>& q)
  237. {
  238. CE_ASSERT(q.m_size > 0, "The queue is empty");
  239. return q[q.m_size - 1];
  240. }
  241. //-----------------------------------------------------------------------------
  242. template <typename T>
  243. inline const T& back(const Queue<T>& q)
  244. {
  245. CE_ASSERT(q.m_size > 0, "The queue is empty");
  246. return q[q.m_size - 1];
  247. }
  248. } // namespace queue
  249. //-----------------------------------------------------------------------------
  250. template <typename T>
  251. inline Queue<T>::Queue(Allocator& allocator)
  252. : m_read(0)
  253. , m_size(0)
  254. , m_queue(allocator)
  255. {
  256. }
  257. //-----------------------------------------------------------------------------
  258. template <typename T>
  259. inline T& Queue<T>::operator[](uint32_t index)
  260. {
  261. return m_queue[(m_read + index) % array::size(m_queue)];
  262. }
  263. //-----------------------------------------------------------------------------
  264. template <typename T>
  265. inline const T& Queue<T>::operator[](uint32_t index) const
  266. {
  267. return m_queue[(m_read + index) % array::size(m_queue)];
  268. }
  269. } // namespace crown