6#include "gaia/core/iterator.h"
7#include "gaia/mem/data_layout_policy.h"
8#include "gaia/mem/mem_utils.h"
13 namespace sringbuffer_detail {
22 template <
typename T, sringbuffer_detail::
size_type N>
42 sringbuffer_detail::size_type m_tail;
43 sringbuffer_detail::size_type m_size;
44 sringbuffer_detail::size_type m_index;
56 sringbuffer_detail::size_type
tail,
58 sringbuffer_detail::size_type size,
60 sringbuffer_detail::size_type index): m_ptr(ptr), m_tail(
tail), m_size(size), m_index(index) {}
65 return m_ptr[(m_tail + m_index) %
N];
70 return &m_ptr[(m_tail + m_index) %
N];
76 return m_ptr[(m_tail + m_index + offset) %
N];
123 return {m_index + offset};
129 return {m_index - offset};
135 GAIA_ASSERT(m_ptr == other.m_ptr);
142 GAIA_ASSERT(m_ptr == other.m_ptr);
143 return m_index == other.m_index;
149 GAIA_ASSERT(m_ptr == other.m_ptr);
150 return m_index != other.m_index;
156 GAIA_ASSERT(m_ptr == other.m_ptr);
157 return m_index > other.m_index;
163 GAIA_ASSERT(m_ptr == other.m_ptr);
164 return m_index >= other.m_index;
170 GAIA_ASSERT(m_ptr == other.m_ptr);
171 return m_index < other.m_index;
177 GAIA_ASSERT(m_ptr == other.m_ptr);
178 return m_index <= other.m_index;
186 template <
typename T, sringbuffer_detail::
size_type N>
189 static_assert(
N > 1);
231 const auto count = (
size_type)core::distance(first, last);
239 if constexpr (std::is_pointer_v<InputIt>) {
242 }
else if constexpr (std::is_same_v<typename InputIt::iterator_category, core::random_access_iterator_tag>) {
247 for (
auto it = first;
it != last; ++
it)
259 mem::copy_elements<T, false>(
m_data, other.m_data, other.
size(), 0,
extent, other.extent);
265 mem::move_elements<T, false>(
m_data, other.m_data, other.
size(), 0,
extent, other.extent);
283 GAIA_ASSERT(core::addressof(other) !=
this);
297 GAIA_ASSERT(core::addressof(other) !=
this);
299 mem::move_elements<T, false>(
m_data, other.m_data, other.
size(), 0,
extent, other.extent);
333 GAIA_ASSERT(!
empty());
342 GAIA_ASSERT(!
empty());
393 GAIA_ASSERT(!
empty());
400 GAIA_ASSERT(!
empty());
407 GAIA_ASSERT(!
empty());
415 GAIA_ASSERT(!
empty());
470 template <
typename T, uint32_t N, uint32_t... I>
471 constexpr sringbuffer<std::remove_cv_t<T>, N>
472 to_sringbuffer_impl(T (&a)[N], std::index_sequence<I...> ) {
483 template <
typename T, u
int32_t N>
484 constexpr sringbuffer<std::remove_cv_t<T>, N> to_sringbuffer(T (&a)[N]) {
485 return detail::to_sringbuffer_impl(a, std::make_index_sequence<N>{});
491 template <
typename T,
typename... U>
492 sringbuffer(T, U...) -> sringbuffer<T, 1 +
sizeof...(U)>;
Array with variable size of elements of type.
Definition darray_impl.h:27
darr_detail::size_type size_type
Unsigned type used for sizes and indices.
Definition darray_impl.h:44
GAIA_NODISCARD size_type size() const noexcept
Returns the number of elements.
Definition darray_impl.h:504
GAIA_NODISCARD auto begin() noexcept
Returns an iterator to the first element.
Definition darray_impl.h:556
darr_detail::difference_type difference_type
Type used for iterator differences.
Definition darray_impl.h:42
GAIA_NODISCARD auto end() noexcept
Returns an iterator one past the last element.
Definition darray_impl.h:592
Array of elements of type.
Definition sringbuffer.h:187
GAIA_NODISCARD constexpr reference front() noexcept
Returns the front element.
Definition sringbuffer.h:392
static constexpr size_type extent
Compile-time buffer capacity.
Definition sringbuffer.h:214
size_type m_tail
Physical index of the logical front element.
Definition sringbuffer.h:217
GAIA_NODISCARD constexpr auto cend() const noexcept
Returns the immutable end sentinel.
Definition sringbuffer.h:452
GAIA_NODISCARD constexpr auto end() noexcept
Returns the mutable end sentinel.
Definition sringbuffer.h:440
sringbuffer_detail::size_type difference_type
Type used for iterator distances.
Definition sringbuffer.h:202
sringbuffer_detail::size_type size_type
Type used for sizes and indices.
Definition sringbuffer.h:204
size_type m_size
Number of live elements.
Definition sringbuffer.h:219
GAIA_NODISCARD constexpr auto begin() noexcept
Returns an iterator to the logical front.
Definition sringbuffer.h:422
GAIA_NODISCARD constexpr const_reference back() const noexcept
Returns the back element.
Definition sringbuffer.h:414
constexpr sringbuffer(std::initializer_list< T > il) noexcept
Constructs a ring buffer from an initializer list.
Definition sringbuffer.h:254
GAIA_NODISCARD constexpr auto begin() const noexcept
Returns an iterator to the logical front.
Definition sringbuffer.h:428
constexpr sringbuffer(sringbuffer &&other) noexcept
Move-constructs a ring buffer and leaves the source empty.
Definition sringbuffer.h:264
GAIA_NODISCARD constexpr bool empty() const noexcept
Checks whether the buffer is empty.
Definition sringbuffer.h:374
constexpr void pop_front(T &out)
Removes and copies the front element.
Definition sringbuffer.h:332
GAIA_NODISCARD constexpr const_reference front() const noexcept
Returns the front element.
Definition sringbuffer.h:399
GAIA_NODISCARD constexpr auto end() const noexcept
Returns the immutable end sentinel.
Definition sringbuffer.h:446
constexpr sringbuffer & operator=(std::initializer_list< T > il) noexcept
Assigns elements from an initializer list.
Definition sringbuffer.h:274
constexpr void push_back(const T &arg)
Appends a copied element.
Definition sringbuffer.h:314
sringbuffer_iterator< T, N > iterator
Mutable random-access iterator.
Definition sringbuffer.h:207
GAIA_NODISCARD constexpr size_type max_size() const noexcept
Returns the maximum element count.
Definition sringbuffer.h:386
constexpr sringbuffer(const sringbuffer &other) noexcept
Copy-constructs a ring buffer.
Definition sringbuffer.h:258
GAIA_NODISCARD constexpr auto cbegin() const noexcept
Returns an iterator to the logical front.
Definition sringbuffer.h:434
GAIA_NODISCARD constexpr size_type capacity() const noexcept
Returns the fixed capacity.
Definition sringbuffer.h:380
constexpr sringbuffer & operator=(sringbuffer &&other) noexcept
Move-assigns a ring buffer and leaves the source empty.
Definition sringbuffer.h:296
constexpr void pop_back(T &out)
Removes and copies the back element.
Definition sringbuffer.h:350
constexpr sringbuffer & operator=(const sringbuffer &other)
Copy-assigns a ring buffer.
Definition sringbuffer.h:282
sringbuffer_iterator< const T, N > const_iterator
Immutable random-access iterator.
Definition sringbuffer.h:209
T m_data[N]
Physical element storage.
Definition sringbuffer.h:221
constexpr void pop_back(T &&out)
Removes and moves the back element.
Definition sringbuffer.h:359
constexpr void pop_front(T &&out)
Removes and moves the front element.
Definition sringbuffer.h:341
constexpr void push_back(T &&arg)
Appends a moved element.
Definition sringbuffer.h:323
GAIA_NODISCARD constexpr bool operator==(const sringbuffer &other) const
Compares corresponding physical storage positions.
Definition sringbuffer.h:459
GAIA_NODISCARD constexpr size_type size() const noexcept
Returns the number of live elements.
Definition sringbuffer.h:368
GAIA_NODISCARD constexpr reference back() noexcept
Returns the back element.
Definition sringbuffer.h:406
Random-access iterator over the logical sequence stored in a sringbuffer.
Definition sringbuffer.h:23
T & operator*() const
Dereferences the current logical element.
Definition sringbuffer.h:64
iterator operator+(size_type offset) const
Returns an iterator advanced by an offset.
Definition sringbuffer.h:122
GAIA_NODISCARD bool operator>(const iterator &other) const
Compares logical iterator positions.
Definition sringbuffer.h:155
iterator & operator++()
Advances by one logical position.
Definition sringbuffer.h:95
iterator operator++(int)
Advances by one logical position.
Definition sringbuffer.h:101
iterator operator--(int)
Moves backward by one logical position.
Definition sringbuffer.h:114
iterator operator[](size_type offset) const
Accesses storage at a logical offset using the declared iterator result type.
Definition sringbuffer.h:75
sringbuffer_iterator(pointer ptr, sringbuffer_detail::size_type tail, sringbuffer_detail::size_type size, sringbuffer_detail::size_type index)
Constructs an iterator over a ring-buffer sequence.
Definition sringbuffer.h:52
iterator & operator--()
Moves backward by one logical position.
Definition sringbuffer.h:108
GAIA_NODISCARD bool operator>=(const iterator &other) const
Compares logical iterator positions.
Definition sringbuffer.h:162
difference_type operator-(const iterator &other) const
Calculates the logical distance between iterators from the same buffer.
Definition sringbuffer.h:134
sringbuffer_detail::difference_type difference_type
Type used for iterator distances.
Definition sringbuffer.h:31
iterator & operator+=(size_type diff)
Advances by a logical offset.
Definition sringbuffer.h:82
iterator operator-(size_type offset) const
Returns an iterator moved backward by an offset.
Definition sringbuffer.h:128
sringbuffer_detail::size_type size_type
Type used for indices and offsets.
Definition sringbuffer.h:33
iterator & operator-=(size_type diff)
Moves backward by a logical offset.
Definition sringbuffer.h:89
GAIA_NODISCARD bool operator==(const iterator &other) const
Compares logical iterator positions.
Definition sringbuffer.h:141
GAIA_NODISCARD bool operator<=(const iterator &other) const
Compares logical iterator positions.
Definition sringbuffer.h:176
GAIA_NODISCARD bool operator<(const iterator &other) const
Compares logical iterator positions.
Definition sringbuffer.h:169
T * operator->() const
Accesses the current logical element.
Definition sringbuffer.h:69
GAIA_NODISCARD bool operator!=(const iterator &other) const
Compares logical iterator positions.
Definition sringbuffer.h:148