queue.h 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  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 <cstring>
  25. #include "container_types.h"
  26. #include "array.h"
  27. #include "assert.h"
  28. namespace crown
  29. {
  30. /// Functions to manipulate Queue.
  31. ///
  32. /// @ingroup Containers
  33. namespace queue
  34. {
  35. /// Returns whether the queue is empty.
  36. template<typename T> bool empty(const Queue<T>& q);
  37. /// Returns the number of items in the queue
  38. template<typename T> uint32_t size(const Queue<T>& q);
  39. /// Returns the number of items the queue can hold before
  40. /// a resize must occur.
  41. template<typename T> uint32_t space(const Queue<T>& q);
  42. /// Increase or decrease the capacity of the queue.
  43. /// @note
  44. /// Old items will be copied to the newly created queue.
  45. /// If the new @a capacity is smaller than the previous one, the
  46. /// queue will be truncated.
  47. template<typename T> void increase_capacity(Queue<T>& q, uint32_t capacity);
  48. /// Grows the queue to contain at least @a min_capacity items.
  49. /// If @a min_capacity is set to 0, the queue automatically
  50. /// determines the new capacity based on its size at the
  51. /// time of call.
  52. template<typename T> void grow(Queue<T>& q, uint32_t min_capacity);
  53. /// Appends an @a item to the back of the queue
  54. template<typename T> void push_back(Queue<T>& q, const T& item);
  55. /// Removes the last item from the queue
  56. template<typename T> void pop_back(Queue<T>& q);
  57. /// Appends an @a item to the front of the queue
  58. template<typename T> void push_front(Queue<T>& q, const T& item);
  59. /// Removes the first item from the queue
  60. template<typename T> void pop_front(Queue<T>& q);
  61. /// Appends @a n @a items to the back of the queue
  62. template<typename T> void push(Queue<T>& q, const T *items, uint32_t n);
  63. /// Removes @a n items from the front of the queue
  64. template<typename T> void pop(Queue<T>& q, uint32_t n);
  65. /// Clears the content of the queue.
  66. /// @note
  67. /// Does not free memory nor call destructors, it only zeroes
  68. /// the number of items in the queue for efficiency.
  69. template<typename T> void clear(Queue<T>& q);
  70. template<typename T> T* begin(Queue<T>& q);
  71. template<typename T> const T* begin(const Queue<T>& q);
  72. template<typename T> T* end(Queue<T>& q);
  73. template<typename T> const T* end(const Queue<T>& q);
  74. template<typename T> T& front(Queue<T>& q);
  75. template<typename T> const T& front(const Queue<T>& q);
  76. template<typename T> T& back(Queue<T>& q);
  77. template<typename T> const T& back(const Queue<T>& q);
  78. } // namespace queue
  79. namespace queue
  80. {
  81. //-----------------------------------------------------------------------------
  82. template <typename T>
  83. inline bool empty(const Queue<T>& q)
  84. {
  85. return q.m_size == 0;
  86. }
  87. //-----------------------------------------------------------------------------
  88. template <typename T>
  89. inline uint32_t size(const Queue<T>& q)
  90. {
  91. return q.m_size;
  92. }
  93. //-----------------------------------------------------------------------------
  94. template <typename T>
  95. inline uint32_t space(const Queue<T>& q)
  96. {
  97. return array::size(q.m_queue) - q.m_size;
  98. }
  99. //-----------------------------------------------------------------------------
  100. template <typename T>
  101. inline void increase_capacity(Queue<T>& q, uint32_t capacity)
  102. {
  103. uint32_t old_size = array::size(q.m_queue);
  104. array::resize(q.m_queue, capacity);
  105. if (q.m_read + q.m_size > old_size)
  106. {
  107. 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));
  108. q.m_read += (capacity - old_size);
  109. }
  110. }
  111. //-----------------------------------------------------------------------------
  112. template <typename T>
  113. inline void grow(Queue<T>& q, uint32_t min_capacity)
  114. {
  115. uint32_t new_capacity = array::size(q.m_queue) * 2 + 1;
  116. if (new_capacity < min_capacity)
  117. {
  118. new_capacity = min_capacity;
  119. }
  120. increase_capacity(q, new_capacity);
  121. }
  122. //-----------------------------------------------------------------------------
  123. template <typename T>
  124. inline void push_back(Queue<T>& q, const T& item)
  125. {
  126. if (space(q) == 0)
  127. {
  128. grow(q, 0);
  129. }
  130. q[q.m_size] = item;
  131. q.m_size++;
  132. }
  133. //-----------------------------------------------------------------------------
  134. template <typename T>
  135. inline void pop_back(Queue<T>& q)
  136. {
  137. CE_ASSERT(q.m_size > 0, "The queue is empty");
  138. q.m_size--;
  139. }
  140. //-----------------------------------------------------------------------------
  141. template <typename T>
  142. inline void push_front(Queue<T>& q, const T& item)
  143. {
  144. if (space(q) == 0)
  145. {
  146. grow(q, 0);
  147. }
  148. q.m_read = (q.m_read - 1 + array::size(q.m_queue)) % array::size(q.m_queue);
  149. q[0] = item;
  150. q.m_size++;
  151. }
  152. //-----------------------------------------------------------------------------
  153. template <typename T>
  154. inline void pop_front(Queue<T>& q)
  155. {
  156. CE_ASSERT(q.m_size > 0, "The queue is empty");
  157. q.m_read = (q.m_read + 1) % array::size(q.m_queue);
  158. q.m_size--;
  159. }
  160. //-----------------------------------------------------------------------------
  161. template <typename T>
  162. inline void push(Queue<T>& q, const T *items, uint32_t n)
  163. {
  164. if (q.space() < n)
  165. {
  166. q.grow(q.size() + n);
  167. }
  168. const uint32_t size = array::size(q.m_queue);
  169. const uint32_t insert = (q.m_read + q.m_size) % size;
  170. uint32_t to_insert = n;
  171. if (insert + to_insert > size)
  172. {
  173. to_insert = size - insert;
  174. }
  175. memcpy(array::begin(q.m_queue) + insert, items, to_insert * sizeof(T));
  176. q.m_size += to_insert;
  177. items += to_insert;
  178. n -= to_insert;
  179. memcpy(array::begin(q.m_queue), items, n * sizeof(T));
  180. q.m_size += n;
  181. }
  182. //-----------------------------------------------------------------------------
  183. template <typename T>
  184. inline void pop(Queue<T>& q, uint32_t n)
  185. {
  186. CE_ASSERT(q.m_size > 0, "The queue is empty");
  187. q.m_read = (q.m_read + n) % array::size(q.m_queue);
  188. q.m_size -= n;
  189. }
  190. //-----------------------------------------------------------------------------
  191. template <typename T>
  192. inline void clear(Queue<T>& q)
  193. {
  194. q.m_read = 0;
  195. q.m_size = 0;
  196. }
  197. //-----------------------------------------------------------------------------
  198. template <typename T>
  199. inline T* begin(Queue<T>& q)
  200. {
  201. return array::begin(q.m_queue) + q.m_read;
  202. }
  203. //-----------------------------------------------------------------------------
  204. template <typename T>
  205. inline const T* begin(const Queue<T>& q)
  206. {
  207. return array::begin(q.m_queue) + q.m_read;
  208. }
  209. //-----------------------------------------------------------------------------
  210. template <typename T>
  211. inline T* end(Queue<T>& q)
  212. {
  213. uint32_t end = q.m_read + q.m_size;
  214. return end >= array::size(q.m_queue) ? array::end(q.m_queue) : array::begin(q.m_queue) + end;
  215. }
  216. //-----------------------------------------------------------------------------
  217. template <typename T>
  218. inline const T* end(const Queue<T>& q)
  219. {
  220. uint32_t end = q.m_read + q.m_size;
  221. return end >= array::size(q.m_queue) ? array::end(q.m_queue) : array::begin(q.m_queue) + end;
  222. }
  223. //-----------------------------------------------------------------------------
  224. template <typename T>
  225. inline T& front(Queue<T>& q)
  226. {
  227. CE_ASSERT(q.m_size > 0, "The queue is empty");
  228. return q.m_queue[q.m_read];
  229. }
  230. //-----------------------------------------------------------------------------
  231. template <typename T>
  232. inline const T& front(const Queue<T>& q)
  233. {
  234. CE_ASSERT(q.m_size > 0, "The queue is empty");
  235. return q.m_queue[q.m_read];
  236. }
  237. //-----------------------------------------------------------------------------
  238. template <typename T>
  239. inline T& back(Queue<T>& q)
  240. {
  241. CE_ASSERT(q.m_size > 0, "The queue is empty");
  242. return q[q.m_size - 1];
  243. }
  244. //-----------------------------------------------------------------------------
  245. template <typename T>
  246. inline const T& back(const Queue<T>& q)
  247. {
  248. CE_ASSERT(q.m_size > 0, "The queue is empty");
  249. return q[q.m_size - 1];
  250. }
  251. } // namespace queue
  252. //-----------------------------------------------------------------------------
  253. template <typename T>
  254. inline Queue<T>::Queue(Allocator& allocator)
  255. : m_read(0)
  256. , m_size(0)
  257. , m_queue(allocator)
  258. {
  259. }
  260. //-----------------------------------------------------------------------------
  261. template <typename T>
  262. inline T& Queue<T>::operator[](uint32_t index)
  263. {
  264. return m_queue[(m_read + index) % array::size(m_queue)];
  265. }
  266. //-----------------------------------------------------------------------------
  267. template <typename T>
  268. inline const T& Queue<T>::operator[](uint32_t index) const
  269. {
  270. return m_queue[(m_read + index) % array::size(m_queue)];
  271. }
  272. } // namespace crown