Queue.h 8.8 KB

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