A C++ toolbox repo until the pair uTL/dTL arives
Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.

deque.h 7.6 KiB

3 lat temu
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. /*!
  2. * \file cont/deque.h
  3. * \brief
  4. * A statically allocated deque based on a ring buffer.
  5. *
  6. * \copyright Copyright (C) 2021 Christos Choutouridis <christos@choutouridis.net>
  7. *
  8. * <dl class=\"section copyright\"><dt>License</dt><dd>
  9. * The MIT License (MIT)
  10. *
  11. * Permission is hereby granted, free of charge, to any person obtaining a copy
  12. * of this software and associated documentation files (the "Software"), to deal
  13. * in the Software without restriction, including without limitation the rights
  14. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  15. * copies of the Software, and to permit persons to whom the Software is
  16. * furnished to do so, subject to the following conditions:
  17. *
  18. * The above copyright notice and this permission notice shall be included in all
  19. * copies or substantial portions of the Software.
  20. *
  21. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  22. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  23. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  24. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  25. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  26. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  27. * SOFTWARE.
  28. * </dd></dl>
  29. */
  30. #ifndef TBX_CONT_DEQUE_H_
  31. #define TBX_CONT_DEQUE_H_
  32. #include <core/core.h>
  33. #include <core/ring_iterator.h>
  34. #include <array>
  35. namespace tbx {
  36. /*!
  37. * \class deque
  38. * \brief
  39. * A statically allocated deque based on a ring buffer
  40. *
  41. * The deque uses two ring_iterators one for the front and one for the rear. The iterators
  42. * are pointing to the next available spot, not on the last inserted spot. This way at the
  43. * initialization the iterators wont "pretend" to point to a valid item .
  44. *
  45. * We use a ring buffer of size \c N+1. We start the front iterator at the last location of the buffer
  46. * and the rear on the first. This way when the queue is full the iterators are pointing to the same location.
  47. *
  48. * \tparam Data_t The char-like queued item type. Usually \c char
  49. * \tparam N The size of deque
  50. */
  51. template <typename Data_t, size_t N>
  52. class deque {
  53. public:
  54. // meta-identity type
  55. using type = deque<Data_t, N>;
  56. using buffer_t = std::array<Data_t, N+1>; // We need N+1 spaces ring buffer for N spaces deque
  57. using iterator_t = ring_iterator<Data_t*, N+1>;
  58. // STL
  59. using value_type = Data_t;
  60. using reference = Data_t&;
  61. using const_reference = const Data_t&;
  62. using pointer = Data_t*;
  63. using const_pointer = const Data_t*;
  64. using iterator = iterator_t;
  65. using const_iterator = const iterator_t;
  66. using reverse_iterator = std::reverse_iterator<iterator>;
  67. using const_reverse_iterator = std::reverse_iterator<const_iterator>;
  68. //! \name Constructor / Destructor
  69. //! @{
  70. public:
  71. //! Default constructor
  72. deque () noexcept :
  73. data_{},
  74. f{data_.data(), N},
  75. r{data_.data()} { }
  76. //! fill contructor
  77. deque(const Data_t& value) noexcept {
  78. data_.fill(value);
  79. f = iterator(data_.data(), N);
  80. r = iterator(data_.data(), N);
  81. }
  82. //! Initializer list contructor
  83. template <typename ...It>
  84. deque(It&& ...it) noexcept :
  85. data_{{std::forward<It>(it)...}},
  86. f(data_.data(), sizeof...(It)),
  87. r(data_.data(), sizeof...(It)) { }
  88. deque(const deque&) = delete; //!< No copies
  89. deque& operator= (const deque&) = delete; //!< No copy assignments
  90. ~deque () = default; //!< default destructor
  91. //! @}
  92. //! \name Iterators
  93. //! @{
  94. public:
  95. iterator begin() noexcept { return f+1; }
  96. const_iterator begin() const noexcept { return f+1; }
  97. const_iterator cbegin() const noexcept { return f+1; }
  98. iterator end() noexcept { return r; }
  99. const_iterator end() const noexcept { return r; }
  100. const_iterator cend() const noexcept { return r; }
  101. reverse_iterator rbegin() noexcept { return r; }
  102. const_reverse_iterator rbegin() const noexcept { return r; }
  103. const_reverse_iterator crbegin() const noexcept { return r; }
  104. reverse_iterator rend() noexcept { return f+1; }
  105. const_reverse_iterator rend() const noexcept { return f+1; }
  106. const_reverse_iterator crend() const noexcept { return f+1; }
  107. //! @}
  108. //! \name Capacity
  109. //! @{
  110. public:
  111. //! \return The size of the deque. The items currently in queue.
  112. size_t size() noexcept {
  113. return full() ? N: (r - f) -1;
  114. }
  115. //! \return The maximum size of the deque. The items the queue can hold.
  116. size_t max_size() noexcept { return N; }
  117. //! \return The capacity of the deque. The items the queue can hold.
  118. size_t capacity() noexcept { return N; }
  119. //! \return True if the deque is empty
  120. bool empty() noexcept { return size() == 0 ? true : false; }
  121. //! \return True if the deque is full
  122. bool full() noexcept { return (r == f) ? true : false; }
  123. //! @}
  124. //! \name Member access
  125. //! @{
  126. public:
  127. //! \brief Clears-empty the deque and return it to init state, without
  128. //! really deleting the contents.
  129. void clear() noexcept {
  130. f = iterator_t(data_.data(), N);
  131. r = iterator_t(data_.data());
  132. }
  133. //! \brief Push an item in the front of the deque
  134. //! \param it The item to push
  135. void push_front (const Data_t& it) {
  136. if (full()) return;
  137. *f-- = it;
  138. }
  139. //! \brief Extract an item from the front of the deque and remove it from the deque
  140. //! \param it The item to push
  141. Data_t pop_front () {
  142. if (empty()) return Data_t{};
  143. return *++f;
  144. }
  145. //! \brief Push an item in the back of the deque
  146. //! \param it The item to push
  147. void push_back (const Data_t& it) {
  148. if (full()) return;
  149. *r++ = it;
  150. }
  151. //! \brief Extract an item from the back of the deque and remove it from the deque
  152. //! \param it The item to push
  153. Data_t pop_back () {
  154. if (empty()) return Data_t{};
  155. return *--r;
  156. }
  157. //! \brief Get a reference to the item in the front of the deque without extracting it.
  158. //! \return Reference to the item
  159. Data_t& front() noexcept { return *(f+1); }
  160. const Data_t& front() const noexcept { return *(f+1); }
  161. //! \brief Get a reference to the item in the front of the deque without extracting it.
  162. //! \return Reference to the item
  163. Data_t& back() noexcept { return *(r-1); }
  164. const Data_t& back() const noexcept { return *(r-1); }
  165. //! @}
  166. private:
  167. buffer_t data_{}; //!< The statically allocated buffer
  168. iterator_t f{data_.data(), N}; //!< A ring iterator for the front (points to the next available location)
  169. iterator_t r{data_.data()}; //!< A ring iterator for the rear (points to the next available location).
  170. };
  171. }
  172. #endif /* TBX_CONT_ADEQUE_H_ */