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];
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);
228 mem::copy_elements<T, false>(
231 mem::copy_elements<T, false>(
240 mem::move_elements<T, false>(
243 mem::move_elements<T, false>(
250 constexpr sringbuffer() noexcept = default;
256 template <typename InputIt>
258 const auto count = (
size_type)core::distance(first, last);
266 if constexpr (std::is_pointer_v<InputIt>) {
269 }
else if constexpr (std::is_same_v<typename InputIt::iterator_category, core::random_access_iterator_tag>) {
274 for (
auto it = first;
it != last; ++
it)
286 copy_live_data(
other);
292 move_live_data(
other);
310 GAIA_ASSERT(core::addressof(
other) !=
this);
314 copy_live_data(
other);
323 GAIA_ASSERT(core::addressof(
other) !=
this);
327 move_live_data(
other);
358 GAIA_ASSERT(!
empty());
367 GAIA_ASSERT(!
empty());
376 GAIA_ASSERT(!
empty());
385 GAIA_ASSERT(!
empty());
418 GAIA_ASSERT(!
empty());
425 GAIA_ASSERT(!
empty());
432 GAIA_ASSERT(!
empty());
440 GAIA_ASSERT(!
empty());
505 template <
typename T, uint32_t N, uint32_t... I>
506 constexpr sringbuffer<std::remove_cv_t<T>, N>
507 to_sringbuffer_impl(T (&a)[N], std::index_sequence<I...> ) {
518 template <
typename T, u
int32_t N>
519 constexpr sringbuffer<std::remove_cv_t<T>, N> to_sringbuffer(T (&a)[N]) {
520 return detail::to_sringbuffer_impl(a, std::make_index_sequence<N>{});
526 template <
typename T,
typename... U>
527 sringbuffer(T, U...) -> sringbuffer<T, 1 +
sizeof...(U)>;
Array of elements of type.
Definition darray_ext_impl.h:31
GAIA_NODISCARD auto begin() noexcept
Returns an iterator to the first element.
Definition darray_ext_impl.h:613
GAIA_NODISCARD auto end() noexcept
Returns an iterator one past the last element.
Definition darray_ext_impl.h:649
darr_ext_detail::size_type size_type
Unsigned type used for sizes and indices.
Definition darray_ext_impl.h:50
static constexpr size_type extent
Fixed capacity of the container.
Definition darray_ext_impl.h:66
darr_ext_detail::difference_type difference_type
Type used for iterator differences.
Definition darray_ext_impl.h:48
Array of elements of type.
Definition sringbuffer.h:187
GAIA_NODISCARD constexpr reference front() noexcept
Returns the front element.
Definition sringbuffer.h:417
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:477
GAIA_NODISCARD constexpr auto end() noexcept
Returns the mutable end sentinel.
Definition sringbuffer.h:465
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:447
GAIA_NODISCARD constexpr const_reference back() const noexcept
Returns the back element.
Definition sringbuffer.h:439
constexpr sringbuffer(std::initializer_list< T > il) noexcept
Constructs a ring buffer from an initializer list.
Definition sringbuffer.h:281
GAIA_NODISCARD constexpr auto begin() const noexcept
Returns an iterator to the logical front.
Definition sringbuffer.h:453
constexpr sringbuffer(sringbuffer &&other) noexcept
Move-constructs a ring buffer and leaves the source empty.
Definition sringbuffer.h:291
GAIA_NODISCARD constexpr bool empty() const noexcept
Checks whether the buffer is empty.
Definition sringbuffer.h:399
constexpr void pop_front(T &out)
Removes and copies the front element.
Definition sringbuffer.h:357
GAIA_NODISCARD constexpr const_reference front() const noexcept
Returns the front element.
Definition sringbuffer.h:424
GAIA_NODISCARD constexpr bool operator!=(const sringbuffer &other) const
Checks whether live logical sequences differ.
Definition sringbuffer.h:498
GAIA_NODISCARD constexpr auto end() const noexcept
Returns the immutable end sentinel.
Definition sringbuffer.h:471
constexpr sringbuffer & operator=(std::initializer_list< T > il) noexcept
Assigns elements from an initializer list.
Definition sringbuffer.h:301
constexpr void push_back(const T &arg)
Appends a copied element.
Definition sringbuffer.h:339
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:411
constexpr sringbuffer(const sringbuffer &other) noexcept
Copy-constructs a ring buffer.
Definition sringbuffer.h:285
GAIA_NODISCARD constexpr auto cbegin() const noexcept
Returns an iterator to the logical front.
Definition sringbuffer.h:459
GAIA_NODISCARD constexpr size_type capacity() const noexcept
Returns the fixed capacity.
Definition sringbuffer.h:405
constexpr sringbuffer & operator=(sringbuffer &&other) noexcept
Move-assigns a ring buffer and leaves the source empty.
Definition sringbuffer.h:322
constexpr void pop_back(T &out)
Removes and copies the back element.
Definition sringbuffer.h:375
constexpr sringbuffer & operator=(const sringbuffer &other)
Copy-assigns a ring buffer.
Definition sringbuffer.h:309
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:384
sringbuffer_detail::difference_type difference_type
Type used for iterator distances.
Definition sringbuffer.h:202
constexpr void pop_front(T &&out)
Removes and moves the front element.
Definition sringbuffer.h:366
constexpr void push_back(T &&arg)
Appends a moved element.
Definition sringbuffer.h:348
GAIA_NODISCARD constexpr bool operator==(const sringbuffer &other) const
Compares live elements in logical order.
Definition sringbuffer.h:484
GAIA_NODISCARD constexpr size_type size() const noexcept
Returns the number of live elements.
Definition sringbuffer.h:393
GAIA_NODISCARD constexpr reference back() noexcept
Returns the back element.
Definition sringbuffer.h:431
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
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+(difference_type offset) const
Returns an iterator advanced by an offset.
Definition sringbuffer.h:122
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
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-=(difference_type diff)
Moves backward by a logical offset.
Definition sringbuffer.h:89
sringbuffer_detail::size_type size_type
Type used for indices and offsets.
Definition sringbuffer.h:33
iterator operator[](difference_type offset) const
Returns an iterator at a logical offset from the current position.
Definition sringbuffer.h:75
iterator & operator+=(difference_type diff)
Advances by a logical offset.
Definition sringbuffer.h:82
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
iterator operator-(difference_type offset) const
Returns an iterator moved backward by an offset.
Definition sringbuffer.h:128
GAIA_NODISCARD bool operator!=(const iterator &other) const
Compares logical iterator positions.
Definition sringbuffer.h:148