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.
 
 
 

226 lines
9.8 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. #include <atomic>
  36. namespace tbx {
  37. /*!
  38. * \class deque
  39. * \brief
  40. * A statically allocated deque based on a ring buffer
  41. *
  42. * The deque uses two ring_iterators one for the front and one for the rear. The iterators
  43. * are pointing to the next available spot, not on the last inserted spot. This way at the
  44. * initialization the iterators wont "pretend" to point to a valid item .
  45. *
  46. * We use a ring buffer of size \c N+1. We start the front iterator at the last location of the buffer
  47. * and the rear on the first. This way when the queue is full the iterators are pointing to the same location.
  48. *
  49. * \tparam Data_t The char-like queued item type. Usually \c char
  50. * \tparam N The size of deque
  51. * \tparam SemiAtomic True for semi-atomic operation. In that case the \c ring_iterator is also atomic.
  52. * \note
  53. * SemiAtomic means it is safe to access different ends from different threads. For example one thread can
  54. * push only from front and another can pop from back to implement a queue.
  55. */
  56. template <typename Data_t, size_t N, bool SemiAtomic =false>
  57. class deque {
  58. public:
  59. // meta-identity type
  60. using type = deque<Data_t, N>;
  61. using buffer_t = std::array<Data_t, N+1>; // We need N+1 spaces ring buffer for N spaces deque
  62. using iterator_t = ring_iterator<Data_t*, N+1, SemiAtomic>;
  63. using range_t = range<iterator_t>;
  64. // STL
  65. using value_type = Data_t;
  66. using reference = Data_t&;
  67. using const_reference = const Data_t&;
  68. using pointer = Data_t*;
  69. using const_pointer = const Data_t*;
  70. using iterator = iterator_t;
  71. using const_iterator = const iterator_t;
  72. using reverse_iterator = std::reverse_iterator<iterator>;
  73. using const_reverse_iterator = std::reverse_iterator<const_iterator>;
  74. //! \name Constructor / Destructor
  75. //! @{
  76. public:
  77. //! Default constructor
  78. constexpr deque () noexcept :
  79. data_{},
  80. f{data_.data(), N},
  81. r{data_.data()} {
  82. if constexpr (SemiAtomic)
  83. std::atomic_thread_fence(std::memory_order_release);
  84. }
  85. //! fill contructor
  86. constexpr deque(const Data_t& value) noexcept {
  87. data_.fill(value);
  88. f = iterator(data_.data(), N);
  89. r = iterator(data_.data(), N);
  90. if constexpr (SemiAtomic)
  91. std::atomic_thread_fence(std::memory_order_release);
  92. }
  93. //! Initializer list contructor
  94. template <typename ...It>
  95. constexpr deque(It&& ...it) noexcept :
  96. data_{{std::forward<It>(it)...}},
  97. f(data_.data(), N),
  98. r(data_.data(), sizeof...(It)) {
  99. if constexpr (SemiAtomic)
  100. std::atomic_thread_fence(std::memory_order_release);
  101. }
  102. deque(const deque&) = delete; //!< No copies
  103. deque& operator= (const deque&) = delete; //!< No copy assignments
  104. ~deque () = default; //!< default destructor
  105. //! @}
  106. //! \name Iterators
  107. //! @{
  108. public:
  109. constexpr iterator begin() noexcept { iterator ret = f; return ++ret; }
  110. constexpr const_iterator begin() const noexcept { iterator ret = f; return ++ret; }
  111. constexpr const_iterator cbegin() const noexcept { iterator ret = f; return ++ret; }
  112. constexpr iterator end() noexcept { return r; }
  113. constexpr const_iterator end() const noexcept { return r; }
  114. constexpr const_iterator cend() const noexcept { return r; }
  115. constexpr reverse_iterator rbegin() noexcept { return r; }
  116. constexpr const_reverse_iterator rbegin() const noexcept { return r; }
  117. constexpr const_reverse_iterator crbegin() const noexcept { return r; }
  118. constexpr reverse_iterator rend() noexcept { reverse_iterator ret = f; return ++ret; }
  119. constexpr const_reverse_iterator rend() const noexcept { reverse_iterator ret = f; return ++ret; }
  120. constexpr const_reverse_iterator crend() const noexcept { reverse_iterator ret = f; return ++ret; }
  121. //! @}
  122. //! \name Capacity
  123. //! @{
  124. public:
  125. //! \return The size of the deque. The items currently in queue.
  126. constexpr size_t size() noexcept {
  127. return r - (f +1);
  128. }
  129. constexpr size_t size() const noexcept {
  130. return r - (f +1);
  131. }
  132. //! \return The maximum size of the deque. The items the queue can hold.
  133. constexpr size_t max_size() noexcept { return N; }
  134. //! \return The capacity of the deque. The items the queue can hold.
  135. constexpr size_t capacity() noexcept { return N; }
  136. //! \return True if the deque is empty
  137. constexpr bool empty() noexcept { return size() == 0 ? true : false; }
  138. //! \return True if the deque is full
  139. constexpr bool full() noexcept { return size() == N ? true : false; }
  140. //! @}
  141. //! \name Member access
  142. //! @{
  143. public:
  144. //! \brief Clears-empty the deque and return it to init state, without
  145. //! really deleting the contents.
  146. constexpr void clear() noexcept {
  147. f = iterator_t(data_.data(), N);
  148. r = iterator_t(data_.data());
  149. if constexpr (SemiAtomic)
  150. std::atomic_thread_fence(std::memory_order_release);
  151. }
  152. //! \brief Push an item in the front of the deque
  153. //! \param it The item to push
  154. constexpr void push_front (const Data_t& it) noexcept {
  155. if (full()) return;
  156. *f = it;
  157. --f; // keep this separate for thread safety
  158. }
  159. //! \brief Push an item in the back of the deque
  160. //! \param it The item to push
  161. constexpr void push_back (const Data_t& it) noexcept {
  162. if (full()) return;
  163. *r = it;
  164. ++r; // keep this separate for thread safety
  165. }
  166. //! \brief Extract an item from the front of the deque and remove it from the deque
  167. //! \param it The item to push
  168. constexpr Data_t pop_front () noexcept {
  169. if (empty()) return Data_t{};
  170. return *++f;
  171. }
  172. //! \brief Extract an item from the back of the deque and remove it from the deque
  173. //! \param it The item to push
  174. constexpr Data_t pop_back () noexcept {
  175. if (empty()) return Data_t{};
  176. return *--r;
  177. }
  178. //! \brief Get a reference to the item in the front of the deque without extracting it.
  179. //! \return Reference to the item
  180. constexpr Data_t& front() noexcept { iterator_t it = f; return *++it; }
  181. constexpr const Data_t& front() const noexcept { iterator_t it = f; return *++it; }
  182. //! \brief Get a reference to the item in the front of the deque without extracting it.
  183. //! \return Reference to the item
  184. constexpr Data_t& back() noexcept { iterator_t it = r; return *--it; }
  185. constexpr const Data_t& back() const noexcept { iterator_t it = r; return *--it; }
  186. //! \brief Get a pointer to the begin of the items on the deque
  187. //! \return
  188. constexpr Data_t* data() noexcept { return &front(); }
  189. constexpr const Data_t* data() const noexcept { return &front(); }
  190. //! \brief Get a range for the data in queue
  191. //! \return A begin-end iterator pair struct
  192. constexpr range_t contents () noexcept { iterator_t b = f; return {++b, r}; }
  193. constexpr const range_t contents () const noexcept { iterator_t b = f; return {++b, r}; }
  194. //! @}
  195. private:
  196. buffer_t data_{}; //!< The statically allocated buffer
  197. iterator_t f{data_.data(), N}; //!< A ring iterator for the front (points to the next available location)
  198. iterator_t r{data_.data()}; //!< A ring iterator for the rear (points to the next available location).
  199. };
  200. }
  201. #endif /* TBX_CONT_ADEQUE_H_ */