A C++ toolbox repo until the pair uTL/dTL arives
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

194 lines
7.9 KiB

  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. constexpr deque () noexcept :
  73. data_{},
  74. f{data_.data(), N},
  75. r{data_.data()} { }
  76. //! fill contructor
  77. constexpr 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. constexpr deque(It&& ...it) noexcept :
  85. data_{{std::forward<It>(it)...}},
  86. f(data_.data(), N),
  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. constexpr iterator begin() noexcept { return f+1; }
  96. constexpr const_iterator begin() const noexcept { return f+1; }
  97. constexpr const_iterator cbegin() const noexcept { return f+1; }
  98. constexpr iterator end() noexcept { return r; }
  99. constexpr const_iterator end() const noexcept { return r; }
  100. constexpr const_iterator cend() const noexcept { return r; }
  101. constexpr reverse_iterator rbegin() noexcept { return r; }
  102. constexpr const_reverse_iterator rbegin() const noexcept { return r; }
  103. constexpr const_reverse_iterator crbegin() const noexcept { return r; }
  104. constexpr reverse_iterator rend() noexcept { return f+1; }
  105. constexpr const_reverse_iterator rend() const noexcept { return f+1; }
  106. constexpr 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. constexpr 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. constexpr size_t max_size() noexcept { return N; }
  117. //! \return The capacity of the deque. The items the queue can hold.
  118. constexpr size_t capacity() noexcept { return N; }
  119. //! \return True if the deque is empty
  120. constexpr bool empty() noexcept { return size() == 0 ? true : false; }
  121. //! \return True if the deque is full
  122. constexpr 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. constexpr 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. constexpr 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. constexpr 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. constexpr 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. constexpr 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. constexpr Data_t& front() noexcept { return *(f+1); }
  160. constexpr 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. constexpr Data_t& back() noexcept { return *(r-1); }
  164. constexpr 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_ */