53# ifndef TPL_DYNARRAYHEAP_H
54# define TPL_DYNARRAYHEAP_H
62# include <type_traits>
71 namespace dynarrayheap_detail
77 T *
at[
sizeof(size_t) * CHAR_BIT];
97 template <
typename T,
class Compare>
109 if (
const size_t gp =
u_index(p); gp >=
l)
110 __builtin_prefetch(std::addressof(a.access(gp)), 0, 1);
112 T & ap = a.access(p);
113 if (
cmp(ap, x)) [[likely]]
115 way.at[way.len++] = std::addressof(ap);
135 template <
typename T,
class Compare>
145 if (c >
r) [[unlikely]]
149 if (
const size_t gc =
l_index(c); gc <=
r)
150 __builtin_prefetch(std::addressof(a.access(gc)), 0, 1);
152 T *ac = std::addressof(a.access(c));
155 T *ac1 = std::addressof(a.access(c + 1));
163 if (
cmp(x, *ac)) [[likely]]
165 way.at[way.len++] = ac;
182 template <
typename T>
186 T *hole = std::addressof(first);
187 for (
size_t k = 0;
k < way.
len; ++
k)
189 *hole = std::move(*way.
at[
k]);
192 *hole = std::move(x);
209 template <
typename T,
class Compare>
219 T moving = std::move(x);
237 template <
typename T,
class Compare>
246 T moving = std::move(x);
260 template <
typename T,
class Compare = Aleph::less<T>>
261 requires StrictWeakOrder<Compare, T>
272 static size_t r_index(
const size_t & i)
noexcept
295 [[gnu::always_inline]]
inline
306 key = std::move(item);
311 return array.access(i);
341 std::is_nothrow_move_constructible_v<DynArray<T>>
and
342 std::is_nothrow_copy_constructible_v<Compare>)
375 std::is_nothrow_swappable_v<T>
and std::is_nothrow_swappable_v<Compare>)
381 if constexpr (std::is_nothrow_swappable_v<T>)
382 array.swap(other.array);
386 array.swap(other.array);
407 return array.access(1);
415 return array.access(1);
439 return array.access(i);
462 T item = std::move(key);
463 return enter(key, item, place,
r);
491 return array.access(i);
504 T item = std::move(key);
539 T ret_val = std::move(
root);
582 [[nodiscard]]
long items() const noexcept
643 <<
"not current item in iterator";
680 template <
class Operation>
686 if (not
static_cast<bool>(operation(
array.access(i))))
691 template <
class Operation>
697 if (not
static_cast<bool>(operation(
static_cast<const T &
>(
array.access(i)))))
702 template <
class Operation>
704 bool traverse(Operation && operation = Operation())
const
706 return traverse<Operation>(operation);
709 template <
class Operation>
711 bool traverse(Operation && operation = Operation())
713 return traverse<Operation>(operation);
#define Args_Ctor(Name, Type)
C++20 concepts hub: comparison, BST policy, and Aleph container concepts.
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.
STL-compatible iterator adapters for Aleph containers.
#define Special_Ctors(Set_Type, Type)
Generates special constructors for containers.
Read-only view of an Aleph iterator: the iterator of a constant container.
Iterator on the items of the heap, in the order of its array, which is not the sorted one.
const T & get_curr_ne() const noexcept
The current item, without checking that there is one.
Iterator() noexcept=default
An iterator without a heap, which has no items.
long items() const noexcept
The number of items of the heap; none without a heap.
T Item_Type
The type of the items.
void prev_ne() noexcept
Move one position backward, without checking.
const T & get_curr() const
The current item, read-only: the heap order must not be broken.
void reset_first() noexcept
Put the iterator on the first item.
bool is_last() const noexcept
Return true if the iterator is on the last item.
void set_pos(const long pos) noexcept
Put the iterator on the position pos, without checking it.
bool has_curr() const noexcept
Return true if the iterator is on an item.
void reset_last() noexcept
Put the iterator on the last item.
void next_ne() noexcept
Move one position forward, without checking.
const DynArrayHeap * heap_ptr
The heap; none in an iterator built without one.
void prev()
Move one position backward, from the first item to before it too.
long get_pos() const noexcept
The position of the current item among those of the heap, from 0.
void next()
Move one position forward, from the last item to the end too.
long curr_idx
The position of the current item, from 0.
void end() noexcept
Put the iterator at the end, after the last item.
Dynamic heap (priority queue) backed by DynArray.
T & insert_direct(T &&key)
Move overload of insert_direct().
static size_t r_index(const size_t &i) noexcept
DynArrayHeap(const DynArrayHeap &other)=default
Copy the elements and the comparator of another heap.
DynArrayHeap(Compare cmp_fct=Compare())
Default constructor.
T & put(T &&key)
Alias for insert() (move overload).
void swap(DynArrayHeap &other) noexcept(std::is_nothrow_swappable_v< T > and std::is_nothrow_swappable_v< Compare >)
Exchange in O(1) the elements and the comparators of this heap and another.
void reserve(size_t n)
Ensure the underlying array has capacity for at least n elements.
constexpr bool is_empty() const noexcept
Return true if the heap is empty.
T getMin()
Remove and return the top element.
T & put(const T &key)
Alias for insert().
T & append(const T &key)
Alias for insert().
T & enter(T &key, T &item, T &place, const size_t r)
Take to its place the item moved from the key of an insertion.
bool traverse(Operation &&operation=Operation())
T & insert(const T &key)
Insert a copy of key into the heap.
T getMax()
See getMin(); useful if priority order is inverted.
T & insert(T &&key)
Insert a key by moving it into the heap.
T & top()
Return the element with highest priority (the heap top).
bool traverse(Operation &operation) const noexcept(Aleph::NothrowPredicateWith< Operation &, const T & >)
T & insert_direct(const T &key)
Insert by directly indexing into the backing array.
const T & top() const
Const overload of top().
DynArrayHeap & operator=(const DynArrayHeap &other)=default
Copy the elements and the comparator of another heap.
bool traverse(Operation &operation) noexcept(Aleph::NothrowPredicateWith< Operation &, T & >)
Traverse all elements in the heap.
bool traverse(Operation &&operation=Operation()) const
T & append(T &&key)
Alias for insert() (move overload).
DynArrayHeap(DynArrayHeap &&other) noexcept(std::is_nothrow_move_constructible_v< DynArray< T > > and std::is_nothrow_copy_constructible_v< Compare >)
Take the elements of another heap, which is left empty.
constexpr size_t size() const noexcept
Return the number of elements.
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.
__gmp_expr< T, __gmp_binary_expr< __gmp_expr< T, U >, unsigned long int, __gmp_root_function > > root(const __gmp_expr< T, U > &expr, unsigned long int l)
int cmp(const __gmp_expr< T, U > &expr1, const __gmp_expr< V, W > &expr2)
size_t up_index(DynArray< T > &a, const size_t l, const size_t r, T &x, Compare &cmp, Way< T > &way) noexcept(Aleph::NothrowPredicateWith< Compare &, T &, T & >)
Find where an item that enters at the end of a heap goes.
size_t down_index(DynArray< T > &a, const size_t l, const size_t r, T &x, Compare &cmp, Way< T > &way) noexcept(Aleph::NothrowPredicateWith< Compare &, T &, T & >)
Find where an item that enters at the root of a heap goes.
void shift(T &first, const Way< T > &way, T &x)
Move an item along its way.
Main namespace for Aleph-w library functions.
void sift_down(T *ptr, const size_t l, const size_t r, Compare &cmp)
Restore the heap property by moving the element at position l downwards.
std::decay_t< typename HeadC::Item_Type > T
constexpr size_t l_index(const size_t i)
Map a binary heap index to the index of its left child.
constexpr size_t u_index(const size_t &i)
Map a binary heap index to the index of its parent.
T & sift_up(T *ptr, const size_t l, const size_t r, Compare &cmp)
Restore the heap property by moving the element at position r upwards.
The places that an item passes, in order, when it is sifted.
size_t len
The number of places.
T * at[sizeof(size_t) *CHAR_BIT]
The places, from the first one.
Generic list of items stored in a container.
Lazy and scalable dynamic array implementation.