58#ifndef TPL_RING_BUFFER_H
59#define TPL_RING_BUFFER_H
116 static_assert(std::is_move_constructible_v<T>,
117 "RingBuffer requires a move-constructible element type");
118 static_assert(std::is_move_assignable_v<T>,
"RingBuffer requires a move-assignable element type");
127 return std::allocator<T>().allocate(
m);
132 std::allocator<T>().deallocate(p,
m);
136 size_t phys(
const size_t i)
const noexcept
144 for (
size_t i = 0; i <
n_; ++i)
162 template <
bool IsConst>
165 using BufPtr = std::conditional_t<IsConst, const RingBuffer *, RingBuffer *>;
173 return static_cast<size_t>(-(i + 1)) + 1;
177 static size_t add_offset(
const size_t idx,
const std::ptrdiff_t i)
noexcept
183 static size_t sub_offset(
const size_t idx,
const std::ptrdiff_t i)
noexcept
192 using reference = std::conditional_t<IsConst, const T &, T &>;
193 using pointer = std::conditional_t<IsConst, const T *, T *>;
227 return &(*rb_)(
idx_);
309 return not (*
this == it);
354 requires std::is_copy_constructible_v<T>
381 requires std::is_copy_constructible_v<T>
414 std::swap(
n_,
rb.n_);
563 template <
class...
Args>
580 requires std::is_copy_constructible_v<T>
593 return emplace(std::move(item));
598 requires std::is_copy_constructible_v<T>
606 return put(std::move(item));
620 requires(std::is_copy_constructible_v<T>
and std::is_copy_assignable_v<T>)
634 requires std::is_move_assignable_v<T>
638 put(std::move(item));
656 std::destroy_at(
slot);
715 template <
class Operation>
718 for (
size_t i = 0; i <
n_; ++i)
729 template <
class Operation>
732 for (
size_t i = 0; i <
n_; ++i)
749 for (
size_t i = 0; i <
n_; ++i)
750 if (
not ((*
this)(i) ==
rb(i)))
758 return not (*
this ==
rb);
Exception handling system with formatted messages for Aleph-w.
#define ah_out_of_range_error_if(C)
Throws std::out_of_range if condition holds.
#define ah_underflow_error_if(C)
Throws std::underflow_error if condition holds.
#define ah_overflow_error_if(C)
Throws std::overflow_error if condition holds.
#define ah_invalid_argument_if(C)
Throws std::invalid_argument if condition holds.
Random-access iterator over the logical window.
basic_iterator(BufPtr rb, const size_t idx) noexcept
Construct from a buffer and a logical index (internal use).
std::conditional_t< IsConst, const RingBuffer *, RingBuffer * > BufPtr
size_t index() const noexcept
Current logical index (internal use).
bool operator==(const basic_iterator &it) const noexcept
Equality: same buffer and logical position.
static size_t negative_magnitude(const std::ptrdiff_t i) noexcept
Return abs(i) for negative signed offsets without signed overflow.
basic_iterator & operator-=(const difference_type i) noexcept
Retreat by i logical positions.
basic_iterator & operator--() noexcept
Pre-decrement.
bool operator>(const basic_iterator &it) const noexcept
Strict ordering by logical position.
static size_t sub_offset(const size_t idx, const std::ptrdiff_t i) noexcept
Subtract signed offset i from logical index idx.
std::conditional_t< IsConst, const T &, T & > reference
Ref.
std::conditional_t< IsConst, const T *, T * > pointer
Ptr.
T value_type
Element type.
std::ptrdiff_t difference_type
Signed distance type.
bool operator>=(const basic_iterator &it) const noexcept
Ordering by logical position.
bool operator!=(const basic_iterator &it) const noexcept
Inequality: different buffer or logical position.
bool operator<=(const basic_iterator &it) const noexcept
Ordering by logical position.
bool operator<(const basic_iterator &it) const noexcept
Strict ordering by logical position.
std::random_access_iterator_tag iterator_category
Category.
basic_iterator & operator++() noexcept
Pre-increment.
basic_iterator(const basic_iterator< B > &it) noexcept
Convert a mutable iterator into a const iterator.
basic_iterator operator+(difference_type i) const noexcept
Iterator i positions forward.
basic_iterator & operator+=(const difference_type i) noexcept
Advance by i logical positions.
pointer operator->() const noexcept
Member access on the current element.
reference operator[](const difference_type i) const noexcept
Element i logical positions away.
BufPtr buffer() const noexcept
Buffer this iterator walks (internal use).
basic_iterator()=default
Construct a singular iterator.
static size_t add_offset(const size_t idx, const std::ptrdiff_t i) noexcept
Add signed offset i to logical index idx.
reference operator*() const noexcept
Dereference to the current element.
basic_iterator operator-(difference_type i) const noexcept
Iterator i positions backward.
Fixed-capacity circular FIFO buffer over contiguous storage.
bool operator!=(const RingBuffer &rb) const
Inequality: negation of operator==.
const_iterator end() const noexcept
Const iterator past the newest element. O(1).
void empty() noexcept
Destroy all elements (Aleph convention).
T & front()
Oldest element (checked). Alias of get_first().
const_iterator begin() const noexcept
Const iterator on the oldest element. O(1).
const T & get_first() const
size_t size() const noexcept
Return the number of stored elements. O(1).
basic_iterator< false > iterator
Mutable iterator.
iterator begin() noexcept
Iterator on the oldest element. O(1).
T & push(const T &item)
Queue-style alias of put(const T &).
T & emplace(Args &&...args)
Construct an element in place at the tail.
RingBuffer & operator=(const RingBuffer &rb)
Copy assignment (requires copyable T).
T & put(const T &item)
Append a copy of item at the tail.
T & put(T &&item)
Append item at the tail by moving.
size_t phys(const size_t i) const noexcept
bool is_empty() const noexcept
Return true if no elements are stored. O(1).
const_iterator cend() const noexcept
Const iterator past the newest element. O(1).
T & get_last()
Newest element — the last one inserted (checked).
RingBuffer(RingBuffer &&rb) noexcept
Move constructor: steals the storage in O(1).
void clear() noexcept
Destroy all elements. Alias of empty(). Capacity is kept.
bool put_overwrite(T &&item)
T & get_first()
Oldest element — the next to leave (checked).
void swap(RingBuffer &rb) noexcept
Swap contents with rb in O(1).
bool is_full() const noexcept
Return true if the buffer holds capacity() elements. O(1).
bool traverse(Operation operation) const
Const traversal from oldest to newest.
iterator end() noexcept
Iterator past the newest element. O(1).
const_iterator cbegin() const noexcept
Const iterator on the oldest element. O(1).
T & back()
Newest element (checked). Alias of get_last().
T get()
Extract the oldest element from the head.
T & push(T &&item)
Queue-style alias of put(T &&).
const T & get_last() const
T pop()
Queue-style alias of get().
bool operator==(const RingBuffer &rb) const
Equality: same logical contents (capacity is not compared).
static T * allocate(size_t m)
RingBuffer(const RingBuffer &rb)
Copy constructor (requires copyable T).
T value_type
STL convention: value type.
T & operator[](const size_t i)
Checked access to the i-th logical element (0 = oldest).
T Item_Type
Aleph convention: element type.
RingBuffer(const size_t cap)
Construct a buffer with an exact capacity.
static void deallocate(T *p, size_t m) noexcept
bool traverse(Operation operation)
Traverse from oldest to newest while operation returns true.
size_t size_type
STL convention: size type.
void destroy_all() noexcept
size_t capacity() const noexcept
Return the fixed capacity chosen at construction. O(1).
T & operator()(const size_t i) noexcept
Unchecked access to the i-th logical element (must be < size()).
basic_iterator< true > const_iterator
Const iterator.
bool put_overwrite(const T &item)
Append at the tail, evicting the oldest element when full.
size_t available() const noexcept
Return the number of free slots. O(1).
size_t blossom_maximum_cardinality_matching(const GT &g, DynDlist< typename GT::Arc * > &matching, SA sa=SA())
Alias of compute_maximum_cardinality_general_matching().
Main namespace for Aleph-w library functions.
and
Check uniqueness with explicit hash + equality functors.
std::decay_t< typename HeadC::Item_Type > T
FooMap m(5, fst_unit_pair_hash, snd_unit_pair_hash)