45#ifndef TPL_ARRAYQUEUE_H
46#define TPL_ARRAYQUEUE_H
101 return this->
access(offset);
109 return this->
access(offset);
150 requires(std::is_copy_constructible_v<T>
and std::is_copy_assignable_v<T>)
154 for (
size_t i = 0; i < q.n; ++i)
155 this->
access(i) = q.access((q.front_index + i) % q.dim);
173 requires(std::is_copy_constructible_v<T>
and std::is_copy_assignable_v<T>)
221 if (cap <= this->
dim)
250 if (this->
n == this->
dim)
297 return put(std::forward<T>(item));
309 return put(std::forward<T>(item));
326 const size_t cap = this->
dim == 0 ? this->
Min_Dim : this->
dim;
327 const size_t max_sz = cap > std::numeric_limits<size_t>::max() / 2
328 ? std::numeric_limits<size_t>::max() - this->
n : 2 * cap - this->
n;
331 size_t remaining = sz;
332 while (remaining > 0)
334 const size_t available = this->
dim - this->
n;
342 const size_t delta = remaining < available ? remaining : available;
426 const T &
rear(
const size_t i = 0)
const
562 for (
size_t i = 0; i < this->
n / 2; ++i)
574 template <
class Operation>
583 if (not
static_cast<bool>(operation(this->
access(idx))))
589 template <
class Operation>
598 if (not
static_cast<bool>(operation(
static_cast<const T &
>(this->
access(idx)))))
604 template <
class Operation>
609 return traverse<Operation>(operation);
613 template <
class Operation>
618 return traverse<Operation>(operation);
676 assert(((i + inc) %
dim) == ((i + inc) &
mask));
703 std::swap(
dim, q.dim);
704 std::swap(
array, q.array);
707 std::swap(
mask, q.mask);
747 <<
"FixedQueue: no power of two that a size_t holds is at least " << d;
748 dim = std::bit_ceil(d);
752 <<
"FixedQueue capacity overflow";
759 if (
array !=
nullptr)
857 return put(std::forward<T>(item));
869 return put(std::forward<T>(item));
925 const T &
front(
const size_t i = 0) const noexcept
936 T &
rear(
const size_t i = 0) noexcept
944 const T &
rear(
const size_t i = 0) const noexcept
951 [[nodiscard]]
constexpr size_t size() const noexcept
957 [[nodiscard]]
constexpr bool is_empty() const noexcept
963 [[nodiscard]]
constexpr size_t capacity() const noexcept
975 template <
class Operation>
983 if (not
static_cast<bool>(operation(
array[idx])))
989 template <
class Operation>
998 if (not
static_cast<bool>(operation(
static_cast<const T &
>(
array[idx]))))
1004 template <
class Operation>
1006 bool traverse(Operation &&operation = Operation()) const
1009 return traverse<Operation>(operation);
1013 template <
class Operation>
1018 return traverse<Operation>(operation);
1037 :
Base(q.array, q.dim, q.num_items, q.front_index, (q.rear_index - 1) & q.mask)
Variadic constructor macros for containers.
Container traversal and functional operation mixins.
#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_range_error_if(C)
Throws std::range_error if condition holds.
STL-compatible iterator adapters for Aleph containers.
Debug assertion and warning utilities.
Core definitions, constants, and utility macros for Aleph-w.
#define Special_Ctors(Set_Type, Type)
Generates special constructors for containers.
Queue implemented with a single dynamic array.
const T & rear(const size_t i=0) const
This is an overloaded member function, provided for convenience. It differs from the above function o...
T & put(T &&item)
Move an item to the rear of the queue.
T & insert(T &&item)
See put(T && item).
bool traverse(Operation &operation) const noexcept(Aleph::NothrowPredicateWith< Operation &, const T & >)
See traverse(Operation & operation).
T & front(const size_t i=0)
Return the i-th oldest item of the queue.
T & push(T &&item)
Alias for put(); enqueue an item by moving it at the rear.
void empty_and_release()
Empty the queue and reduce storage to Min_Dim.
const T & operator()(const size_t i) const noexcept
This is an overloaded member function, provided for convenience. It differs from the above function o...
ArrayQueue(const size_t sz=8)
Construct a queue with capacity sz
T & top()
Alias for first(); throws std::underflow_error if empty.
const T & top() const
This is an overloaded member function, provided for convenience. It differs from the above function o...
T pop()
Alias for get(); remove the oldest item in FIFO order.
const T & first() const
This is an overloaded member function, provided for convenience. It differs from the above function o...
T & rear(const size_t i=0)
Return the i-th youngest item of the queue.
T & put(const T &item)
Copy and put an item in the queue.
T & insert(const T &item)
See put(const T & item).
T & last()
Return the youngest item, in O(1).
T & put_item(U &&item)
Shared body of both put() overloads.
bool traverse(Operation &operation) noexcept(Aleph::NothrowPredicateWith< Operation &, T & >)
Traverse all the elements from the oldest to the youngest and execute operation on each on them.
const T & last() const
This is an overloaded member function, provided for convenience. It differs from the above function o...
ArrayQueue & operator=(const ArrayQueue &q)
Copy assign (copy and swap: same queue order as the copy constructor)
const T & get_last() const
This is an overloaded member function, provided for convenience. It differs from the above function o...
void clear() noexcept
See empty().
T & complete_put() noexcept
void increase_index(size_t &i, const size_t inc=1) const noexcept
Advance a physical index modulo the allocated capacity.
T & first()
Return the oldest item, in O(1).
void empty() noexcept
Empty the queue keeping its capacity; both ends restart at slot 0.
T & get_last()
Alias for last(); throws std::underflow_error if empty.
void recenter_indices() noexcept
Reset indices after relocating the logical sequence to slot zero.
T remove_last()
Remove the youngest item.
ArrayQueue & reverse()
Reverse the logical queue in place.
T & getn(const size_t i)
Remove the i oldest items of the queue.
T remove_first()
Remove the oldest item; same complexity and exceptions as get().
ArrayQueue & operator=(ArrayQueue &&q) noexcept
Move assign.
bool traverse(Operation &&operation) noexcept(Aleph::NothrowPredicateWith< Operation &, T & >)
See traverse(Operation & operation).
T & get_first()
Alias for first(); throws std::underflow_error if empty.
const T & front(const size_t i=0) const
This is an overloaded member function, provided for convenience. It differs from the above function o...
T & operator()(const size_t i) noexcept
Access an item by FIFO position with debug assertions.
ArrayQueue(const ArrayQueue &q)
Copy constructor.
T & append(const T &item)
See put(const T & item).
T & operator[](const size_t i)
Access an item by its FIFO position.
T & putn(const size_t sz)
Add sz logical slots at the rear without initializing scalars.
T & push(const T &item)
Alias for put(); enqueue a copy at the rear.
const T & get_first() const
This is an overloaded member function, provided for convenience. It differs from the above function o...
T get()
Remove the oldest item of the queue and return a copy.
T Item_Type
Type stored in the queue.
const T & operator[](const size_t i) const
This is an overloaded member function, provided for convenience. It differs from the above function o...
void reserve(const size_t cap)
Reserve capacity for at least cap items, keeping the queue order.
ArrayQueue(ArrayQueue &&q) noexcept
Move constructor.
T & rear_item(const size_t i=0) noexcept
Access the i-th item counting backwards from the youngest item.
void swap(ArrayQueue &q) noexcept
Swap this with q
const T & rear_item(const size_t i=0) const noexcept
This is an overloaded member function, provided for convenience. It differs from the above function o...
bool traverse(Operation &&operation) const noexcept(Aleph::NothrowPredicateWith< Operation &, const T & >)
See traverse(Operation & operation).
T & append(T &&item)
See put(T && item).
Read-only view of an Aleph iterator: the iterator of a constant container.
Very simple queue implemented with a contiguous array.
T & insert(const T &item) noexcept
See put(const T& item).
T & append(const T &item) noexcept
See put(const T& item).
T & put(const T &item) noexcept
Put an item into the queue by copy.
FixedQueue & operator=(const FixedQueue &q)
Copy assign.
T & insert(T &&item) noexcept
See put(const T& item).
const T & front(const size_t i=0) const noexcept
This is an overloaded member function, provided for convenience. It differs from the above function o...
bool traverse(Operation &&operation=Operation()) noexcept(Aleph::NothrowPredicateWith< Operation &, T & >)
See traverse(Operation & operation).
const T & rear_item(const size_t i=0) const noexcept
This is an overloaded member function, provided for convenience. It differs from the above function o...
bool traverse(Operation &operation) noexcept(Aleph::NothrowPredicateWith< Operation &, T & >)
Traverse all the elements from the youngest to the oldest and execute operation on each on them.
constexpr bool is_empty() const noexcept
Return true if the queue is empty.
T & rear_item(const size_t i=0) noexcept
FixedQueue & operator=(FixedQueue &&q) noexcept
Move assign.
FixedQueue(const FixedQueue &q)
Copy constructor: the copy holds the items of q, in the same slots.
void clear() noexcept
Empties the container.
bool traverse(Operation &operation) const noexcept(Aleph::NothrowPredicateWith< Operation &, const T & >)
See traverse(Operation & operation).
T get() noexcept
Remove the oldest item of the queue.
T & putn(const size_t n) noexcept
Put n cells to the queue in constant time.
const T & rear(const size_t i=0) const noexcept
This is an overloaded member function, provided for convenience. It differs from the above function o...
void swap(FixedQueue &q) noexcept
FixedQueue(size_t d=1024)
Construct a queue of capacity the immediately two power of d
T & front(const size_t i=0) noexcept
Return the i-th oldest item.
T & append(T &&item) noexcept
See put(const T& item).
void empty() noexcept
empty the queue
void increase_index(size_t &i, const size_t inc=1) const noexcept
T & getn(const size_t n) noexcept
Remove in constant time the n oldest items of the queue.
T & rear(const size_t i=0) noexcept
Return the i-th youngest item.
constexpr size_t size() const noexcept
Return the number of items.
FixedQueue(FixedQueue &&q) noexcept
Move constructor: takes the array and the items of q.
T & put(T &&item) noexcept
See put(const T& item).
bool traverse(Operation &&operation=Operation()) const noexcept(Aleph::NothrowPredicateWith< Operation &, const T & >)
See traverse(Operation & operation).
constexpr size_t capacity() const noexcept
Return the queue capacity (maximum number of element to be stored)
Simple, scalable and fast dynamic array.
bool contract(const size_t first=0)
Test if n is lesser than contract_threshold and eventually contract the array half long and copies it...
size_t size() const noexcept
Return the number of elements.
T & grow_and_insert(const size_t pos, U &&item, const size_t first=0)
Grow a full array and insert one item: relocate_and_insert().
size_t contract_threshold
Number of items at or below which contract() halves the capacity.
void empty_and_release()
Clear the array and reduce allocated storage to Min_Dim.
static constexpr size_t Min_Dim
bool expand(const size_t first=0)
Grow a full array, including an empty moved-from array.
void disable_annotations() noexcept
Make the whole buffer accessible and stop annotating it.
constexpr size_t capacity() const noexcept
The type of element of array.
bool is_valid() const noexcept
bool is_empty() const noexcept
Return true is the array is empty.
void reallocate(const size_t cap, const size_t first=0)
Relocate the logical sequence into a new buffer.
void empty() noexcept
Empties the container.
T & access(const size_t i) noexcept
Return a modifiable reference to the ith element.
void swap(MemArray &a) noexcept
Swap in constant time this with a
Mixin providing equality comparison for sequence containers.
Common methods to the Aleph-w ( ) containers.
and
Insert a new element into the container by constructing it from the given args.
Common sequential searching methods on containers.
Mixin that adds STL begin()/end() and cbegin()/cend() to Aleph containers.
PredicateWith<F, Args...>, and neither the call nor the conversion of its result to bool can throw.
f(args...) is a valid call whose result is usable as a condition.
Singly linked list implementations with head-tail access.
const long double offset[]
Offset values indexed by symbol string length (bounded by MAX_OFFSET_INDEX)
Main namespace for Aleph-w library functions.
Itor2 copy(Itor1 sourceBeg, const Itor1 &sourceEnd, Itor2 destBeg)
Copy elements from one range to another.
std::decay_t< typename HeadC::Item_Type > T
Simple iterator on elements of a queue.
Iterator(const ArrayQueue &)=delete
A constant queue is iterated through Const_Iterator.
Iterator(ArrayQueue &q)
Iterator on the modifiable queue q, in FIFO order (from the oldest item), including an empty moved-fr...
typename MemArray< T >::Iterator Base
Simple iterator on elements of a queue.
Iterator(FixedQueue &q) noexcept
Iterator on the modifiable queue q, from the oldest item.
typename MemArray< T >::Iterator Base
Iterator(const FixedQueue &)=delete
A constant queue is iterated through Const_Iterator.
Simple iterator on elements of array.
Generic list of items stored in a container.
Dynamic doubly linked list implementation.
Simple, scalable, contiguous dynamic array.