fixed_size_queue.hpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402
  1. /*=============================================================================
  2. Copyright (c) 2001, Daniel C. Nuffer
  3. Copyright (c) 2003, Hartmut Kaiser
  4. http://spirit.sourceforge.net/
  5. Distributed under the Boost Software License, Version 1.0. (See accompanying
  6. file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  7. =============================================================================*/
  8. #ifndef FIXED_SIZE_QUEUE
  9. #define FIXED_SIZE_QUEUE
  10. #include <cstdlib>
  11. #include <iterator>
  12. #include <cstddef>
  13. #include <boost/spirit/home/classic/namespace.hpp>
  14. #include <boost/spirit/home/classic/core/assert.hpp> // for BOOST_SPIRIT_ASSERT
  15. // FIXES for broken compilers
  16. #include <boost/config.hpp>
  17. #include <boost/iterator_adaptors.hpp>
  18. #define BOOST_SPIRIT_ASSERT_FSQ_SIZE \
  19. BOOST_SPIRIT_ASSERT(((m_tail + N + 1) - m_head) % (N+1) == m_size % (N+1)) \
  20. /**/
  21. ///////////////////////////////////////////////////////////////////////////////
  22. namespace boost { namespace spirit {
  23. BOOST_SPIRIT_CLASSIC_NAMESPACE_BEGIN
  24. ///////////////////////////////////////////////////////////////////////////////
  25. namespace impl {
  26. #if !defined(BOOST_ITERATOR_ADAPTORS_VERSION) || \
  27. BOOST_ITERATOR_ADAPTORS_VERSION < 0x0200
  28. #error "Please use at least Boost V1.31.0 while compiling the fixed_size_queue class!"
  29. #else // BOOST_ITERATOR_ADAPTORS_VERSION < 0x0200
  30. template <typename QueueT, typename T, typename PointerT>
  31. class fsq_iterator
  32. : public boost::iterator_adaptor<
  33. fsq_iterator<QueueT, T, PointerT>,
  34. PointerT,
  35. T,
  36. std::random_access_iterator_tag
  37. >
  38. {
  39. public:
  40. typedef typename QueueT::position_t position;
  41. typedef boost::iterator_adaptor<
  42. fsq_iterator<QueueT, T, PointerT>, PointerT, T,
  43. std::random_access_iterator_tag
  44. > base_t;
  45. fsq_iterator() {}
  46. fsq_iterator(position const &p_) : p(p_) {}
  47. position const &get_position() const { return p; }
  48. private:
  49. friend class boost::iterator_core_access;
  50. typename base_t::reference dereference() const
  51. {
  52. return p.self->m_queue[p.pos];
  53. }
  54. void increment()
  55. {
  56. ++p.pos;
  57. if (p.pos == QueueT::MAX_SIZE+1)
  58. p.pos = 0;
  59. }
  60. void decrement()
  61. {
  62. if (p.pos == 0)
  63. p.pos = QueueT::MAX_SIZE;
  64. else
  65. --p.pos;
  66. }
  67. template <
  68. typename OtherDerivedT, typename OtherIteratorT,
  69. typename V, typename C, typename R, typename D
  70. >
  71. bool equal(iterator_adaptor<OtherDerivedT, OtherIteratorT, V, C, R, D>
  72. const &x) const
  73. {
  74. position const &rhs_pos =
  75. static_cast<OtherDerivedT const &>(x).get_position();
  76. return (p.self == rhs_pos.self) && (p.pos == rhs_pos.pos);
  77. }
  78. template <
  79. typename OtherDerivedT, typename OtherIteratorT,
  80. typename V, typename C, typename R, typename D
  81. >
  82. typename base_t::difference_type distance_to(
  83. iterator_adaptor<OtherDerivedT, OtherIteratorT, V, C, R, D>
  84. const &x) const
  85. {
  86. typedef typename base_t::difference_type diff_t;
  87. position const &p2 =
  88. static_cast<OtherDerivedT const &>(x).get_position();
  89. std::size_t pos1 = p.pos;
  90. std::size_t pos2 = p2.pos;
  91. // Undefined behaviour if the iterators come from different
  92. // containers
  93. BOOST_SPIRIT_ASSERT(p.self == p2.self);
  94. if (pos1 < p.self->m_head)
  95. pos1 += QueueT::MAX_SIZE;
  96. if (pos2 < p2.self->m_head)
  97. pos2 += QueueT::MAX_SIZE;
  98. if (pos2 > pos1)
  99. return diff_t(pos2 - pos1);
  100. else
  101. return -diff_t(pos1 - pos2);
  102. }
  103. void advance(typename base_t::difference_type n)
  104. {
  105. // Notice that we don't care values of n that can
  106. // wrap around more than one time, since it would
  107. // be undefined behaviour anyway (going outside
  108. // the begin/end range). Negative wrapping is a bit
  109. // cumbersome because we don't want to case p.pos
  110. // to signed.
  111. if (n < 0)
  112. {
  113. n = -n;
  114. if (p.pos < (std::size_t)n)
  115. p.pos = QueueT::MAX_SIZE+1 - (n - p.pos);
  116. else
  117. p.pos -= n;
  118. }
  119. else
  120. {
  121. p.pos += n;
  122. if (p.pos >= QueueT::MAX_SIZE+1)
  123. p.pos -= QueueT::MAX_SIZE+1;
  124. }
  125. }
  126. private:
  127. position p;
  128. };
  129. #endif // BOOST_ITERATOR_ADAPTORS_VERSION < 0x0200
  130. ///////////////////////////////////////////////////////////////////////////////
  131. } /* namespace impl */
  132. template <typename T, std::size_t N>
  133. class fixed_size_queue
  134. {
  135. private:
  136. struct position
  137. {
  138. fixed_size_queue* self;
  139. std::size_t pos;
  140. position() : self(0), pos(0) {}
  141. // The const_cast here is just to avoid to have two different
  142. // position structures for the const and non-const case.
  143. // The const semantic is guaranteed by the iterator itself
  144. position(const fixed_size_queue* s, std::size_t p)
  145. : self(const_cast<fixed_size_queue*>(s)), pos(p)
  146. {}
  147. };
  148. public:
  149. // Declare the iterators
  150. typedef impl::fsq_iterator<fixed_size_queue<T, N>, T, T*> iterator;
  151. typedef impl::fsq_iterator<fixed_size_queue<T, N>, T const, T const*>
  152. const_iterator;
  153. typedef position position_t;
  154. friend class impl::fsq_iterator<fixed_size_queue<T, N>, T, T*>;
  155. friend class impl::fsq_iterator<fixed_size_queue<T, N>, T const, T const*>;
  156. fixed_size_queue();
  157. fixed_size_queue(const fixed_size_queue& x);
  158. fixed_size_queue& operator=(const fixed_size_queue& x);
  159. ~fixed_size_queue();
  160. void push_back(const T& e);
  161. void push_front(const T& e);
  162. void serve(T& e);
  163. void pop_front();
  164. bool empty() const
  165. {
  166. return m_size == 0;
  167. }
  168. bool full() const
  169. {
  170. return m_size == N;
  171. }
  172. iterator begin()
  173. {
  174. return iterator(position(this, m_head));
  175. }
  176. const_iterator begin() const
  177. {
  178. return const_iterator(position(this, m_head));
  179. }
  180. iterator end()
  181. {
  182. return iterator(position(this, m_tail));
  183. }
  184. const_iterator end() const
  185. {
  186. return const_iterator(position(this, m_tail));
  187. }
  188. std::size_t size() const
  189. {
  190. return m_size;
  191. }
  192. T& front()
  193. {
  194. return m_queue[m_head];
  195. }
  196. const T& front() const
  197. {
  198. return m_queue[m_head];
  199. }
  200. private:
  201. // Redefine the template parameters to avoid using partial template
  202. // specialization on the iterator policy to extract N.
  203. BOOST_STATIC_CONSTANT(std::size_t, MAX_SIZE = N);
  204. std::size_t m_head;
  205. std::size_t m_tail;
  206. std::size_t m_size;
  207. T m_queue[N+1];
  208. };
  209. template <typename T, std::size_t N>
  210. inline
  211. fixed_size_queue<T, N>::fixed_size_queue()
  212. : m_head(0)
  213. , m_tail(0)
  214. , m_size(0)
  215. {
  216. BOOST_SPIRIT_ASSERT(m_size <= N+1);
  217. BOOST_SPIRIT_ASSERT_FSQ_SIZE;
  218. BOOST_SPIRIT_ASSERT(m_head <= N+1);
  219. BOOST_SPIRIT_ASSERT(m_tail <= N+1);
  220. }
  221. template <typename T, std::size_t N>
  222. inline
  223. fixed_size_queue<T, N>::fixed_size_queue(const fixed_size_queue& x)
  224. : m_head(x.m_head)
  225. , m_tail(x.m_tail)
  226. , m_size(x.m_size)
  227. {
  228. copy(x.begin(), x.end(), begin());
  229. BOOST_SPIRIT_ASSERT(m_size <= N+1);
  230. BOOST_SPIRIT_ASSERT_FSQ_SIZE;
  231. BOOST_SPIRIT_ASSERT(m_head <= N+1);
  232. BOOST_SPIRIT_ASSERT(m_tail <= N+1);
  233. }
  234. template <typename T, std::size_t N>
  235. inline fixed_size_queue<T, N>&
  236. fixed_size_queue<T, N>::operator=(const fixed_size_queue& x)
  237. {
  238. if (this != &x)
  239. {
  240. m_head = x.m_head;
  241. m_tail = x.m_tail;
  242. m_size = x.m_size;
  243. copy(x.begin(), x.end(), begin());
  244. }
  245. BOOST_SPIRIT_ASSERT(m_size <= N+1);
  246. BOOST_SPIRIT_ASSERT_FSQ_SIZE;
  247. BOOST_SPIRIT_ASSERT(m_head <= N+1);
  248. BOOST_SPIRIT_ASSERT(m_tail <= N+1);
  249. return *this;
  250. }
  251. template <typename T, std::size_t N>
  252. inline
  253. fixed_size_queue<T, N>::~fixed_size_queue()
  254. {
  255. BOOST_SPIRIT_ASSERT(m_size <= N+1);
  256. BOOST_SPIRIT_ASSERT_FSQ_SIZE;
  257. BOOST_SPIRIT_ASSERT(m_head <= N+1);
  258. BOOST_SPIRIT_ASSERT(m_tail <= N+1);
  259. }
  260. template <typename T, std::size_t N>
  261. inline void
  262. fixed_size_queue<T, N>::push_back(const T& e)
  263. {
  264. BOOST_SPIRIT_ASSERT(m_size <= N+1);
  265. BOOST_SPIRIT_ASSERT_FSQ_SIZE;
  266. BOOST_SPIRIT_ASSERT(m_head <= N+1);
  267. BOOST_SPIRIT_ASSERT(m_tail <= N+1);
  268. BOOST_SPIRIT_ASSERT(!full());
  269. m_queue[m_tail] = e;
  270. ++m_size;
  271. ++m_tail;
  272. if (m_tail == N+1)
  273. m_tail = 0;
  274. BOOST_SPIRIT_ASSERT(m_size <= N+1);
  275. BOOST_SPIRIT_ASSERT_FSQ_SIZE;
  276. BOOST_SPIRIT_ASSERT(m_head <= N+1);
  277. BOOST_SPIRIT_ASSERT(m_tail <= N+1);
  278. }
  279. template <typename T, std::size_t N>
  280. inline void
  281. fixed_size_queue<T, N>::push_front(const T& e)
  282. {
  283. BOOST_SPIRIT_ASSERT(m_size <= N+1);
  284. BOOST_SPIRIT_ASSERT_FSQ_SIZE;
  285. BOOST_SPIRIT_ASSERT(m_head <= N+1);
  286. BOOST_SPIRIT_ASSERT(m_tail <= N+1);
  287. BOOST_SPIRIT_ASSERT(!full());
  288. if (m_head == 0)
  289. m_head = N;
  290. else
  291. --m_head;
  292. m_queue[m_head] = e;
  293. ++m_size;
  294. BOOST_SPIRIT_ASSERT(m_size <= N+1);
  295. BOOST_SPIRIT_ASSERT_FSQ_SIZE;
  296. BOOST_SPIRIT_ASSERT(m_head <= N+1);
  297. BOOST_SPIRIT_ASSERT(m_tail <= N+1);
  298. }
  299. template <typename T, std::size_t N>
  300. inline void
  301. fixed_size_queue<T, N>::serve(T& e)
  302. {
  303. BOOST_SPIRIT_ASSERT(m_size <= N+1);
  304. BOOST_SPIRIT_ASSERT_FSQ_SIZE;
  305. BOOST_SPIRIT_ASSERT(m_head <= N+1);
  306. BOOST_SPIRIT_ASSERT(m_tail <= N+1);
  307. e = m_queue[m_head];
  308. pop_front();
  309. }
  310. template <typename T, std::size_t N>
  311. inline void
  312. fixed_size_queue<T, N>::pop_front()
  313. {
  314. BOOST_SPIRIT_ASSERT(m_size <= N+1);
  315. BOOST_SPIRIT_ASSERT_FSQ_SIZE;
  316. BOOST_SPIRIT_ASSERT(m_head <= N+1);
  317. BOOST_SPIRIT_ASSERT(m_tail <= N+1);
  318. ++m_head;
  319. if (m_head == N+1)
  320. m_head = 0;
  321. --m_size;
  322. BOOST_SPIRIT_ASSERT(m_size <= N+1);
  323. BOOST_SPIRIT_ASSERT_FSQ_SIZE;
  324. BOOST_SPIRIT_ASSERT(m_head <= N+1);
  325. BOOST_SPIRIT_ASSERT(m_tail <= N+1);
  326. }
  327. ///////////////////////////////////////////////////////////////////////////////
  328. BOOST_SPIRIT_CLASSIC_NAMESPACE_END
  329. }} // namespace BOOST_SPIRIT_CLASSIC_NS
  330. #undef BOOST_SPIRIT_ASSERT_FSQ_SIZE
  331. #endif