A C++ toolbox repo until the pair uTL/dTL arives
Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

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