47#ifndef TPL_ARRAYHEAP_H
48#define TPL_ARRAYHEAP_H
83 namespace arrayheap_detail
120 for (
auto levels = std::bit_width(
r) - std::bit_width(i); levels != 0; )
122 const size_t c =
r >> --levels;
123 std::swap(ptr[i], ptr[c]);
141 std::swap(ptr[i], ptr[
u_index(i)]);
158 template <
class A,
class Compare>
168 if (c >
r) [[unlikely]]
172 if (
const size_t gc =
l_index(c); gc <=
r and not std::is_constant_evaluated())
173 __builtin_prefetch(std::addressof(ptr[gc]), 0, 1);
176 if (
cmp(ptr[c + 1], ptr[c]))
179 if (
cmp(ptr[i], ptr[c])) [[likely]]
182 std::swap(ptr[c], ptr[i]);
207 template <
class A,
class Compare>
209 decltype(
auto)
sift_up(A ptr,
const size_t l,
const size_t r, Compare &
cmp)
214 for (
size_t p; i >
l; i = p)
219 if (
const size_t gp =
u_index(p); gp >=
l and not std::is_constant_evaluated())
220 __builtin_prefetch(std::addressof(ptr[gp]), 0, 1);
222 if (
cmp(ptr[p], ptr[i])) [[likely]]
225 std::swap(ptr[p], ptr[i]);
255 template <
typename T,
class Compare>
278 template <
typename T,
class Compare>
300 template <
typename T,
class Compare>
308 sift_up<T, Compare>(ptr,
l, i,
cmp);
332 template <
typename T,
class Compare = Aleph::less<T>>
333 requires StrictWeakOrder<Compare, T>
335 void heapsort(
T *array,
const size_t n,
const Compare &
cmp = Compare())
344 for (
size_t i = 2; i <= n; ++i)
346 for (
size_t i = n; i > 1; --i)
348 std::swap(items[1], items[i]);
368 template <
typename T,
class Compare = Aleph::less<T>>
369 requires StrictWeakOrder<Compare, T>
380 for (
size_t i = n / 2; i >= 1; --i)
382 for (
size_t i = n; i > 1; --i)
384 std::swap(items[1], items[i]);
400 template <
typename T,
class Compare = Aleph::less<T>>
401 requires StrictWeakOrder<Compare, T>
403 const Compare &
cmp = Compare())
439 template <
typename T,
class Compare = Aleph::less<T>>
440 requires StrictWeakOrder<Compare, T>
459 noexcept(
static_cast<bool>(std::declval<Compare &>()(std::declval<T &>(),
460 std::declval<T &>())));
485 <<
"ArrayHeap: capacity " << new_dim <<
" and the entry 0 do not fit in a size_t";
487 T *ptr =
new T[new_dim + 1];
503 std::swap(
array,
h.array);
504 std::swap(
dim,
h.dim);
507 std::swap(
cmp,
h.cmp);
525 ArrayHeap(
const size_t d = 1024, Compare cmp_fct = Compare())
540 ArrayHeap(
T *ptr,
const size_t & d, Compare cmp_fct = Compare())
653 entry = std::move(key);
662 key = std::move(entry);
745 T ret_val = std::move(
array[1]);
749 array[1] = std::move(last);
756 last = std::move(
array[1]);
757 array[1] = std::move(ret_val);
787 [[nodiscard]]
constexpr size_t capacity() const noexcept {
return dim; }
812 const auto i =
static_cast<size_t>(std::addressof(data) -
array);
840 const auto idx =
static_cast<size_t>(std::addressof(item) -
array);
851 item = std::move(last);
861 std::swap(item, last);
868 std::swap(item, last);
877 if constexpr (std::is_nothrow_default_constructible_v<T>
and
878 std::is_nothrow_move_assignable_v<T>)
880 else if constexpr (std::is_nothrow_move_constructible_v<T>)
881 [[maybe_unused]]
T taken(std::move(last));
929 template <
class Operation>
933 if (not
static_cast<bool>(operation(
array[i])))
940 template <
class Operation>
944 if (not
static_cast<bool>(operation(
static_cast<const T &
>(
array[i]))))
960 template <
class Operation>
974 template <
class Operation>
988 template <
class Operation>
990 bool traverse(Operation && operation = Operation()) const
1002 template <
class Operation>
Variadic constructor macros for containers.
C++20 concepts hub: comparison, BST policy, and Aleph container concepts.
Container traversal and functional operation mixins.
Exception handling system with formatted messages for Aleph-w.
#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.
STL-compatible iterator adapters for Aleph containers.
Debug assertion and warning utilities.
Core definitions, constants, and utility macros for Aleph-w.
DRY (Don't Repeat Yourself) utilities and macros.
#define Special_Ctors(Set_Type, Type)
Generates special constructors for containers.
Standard functor implementations and comparison objects.
General utility functions and helpers.
Iterator wrapper for C++ raw arrays and circular buffers.
Fixed-capacity binary heap backed by a raw array.
T & insert_ne(T &&key)
Insert an element without checking capacity (move overload).
bool traverse(Operation &&operation=Operation()) const noexcept(Aleph::NothrowPredicateWith< Operation &, const T & >)
Traverse all elements (const forwarding overload).
bool traverse(Operation &operation) const noexcept(Aleph::NothrowPredicateWith< Operation &, const T & >)
Traverse all elements (const overload).
T & put(const T &key)
Synonym for insert().
bool traverse(Operation &operation) noexcept(Aleph::NothrowPredicateWith< Operation &, T & >)
Traverse all elements.
T & insert(const T &key)
Insert an element into the heap.
T & insert_ne(const T &key)
Insert an element without checking capacity.
ArrayHeap(ArrayHeap &&h) noexcept
T getMin()
Remove the smallest element in the heap and return a copy of its value.
const T & top() const
Return a const reference to the minimum element.
constexpr bool is_empty() const noexcept
Return true if the heap is empty.
T & append(const T &key)
Synonym for insert().
ArrayHeap(const ArrayHeap &h)
bool traverse_impl(Operation &operation)
void swap(ArrayHeap &h) noexcept
Swap all state with another heap.
T & append(T &&key)
Synonym for insert() (move overload).
void update(T &data)
Update the priority of an element stored in the heap.
bool const_traverse_impl(Operation &operation) const
ArrayHeap(const size_t d=1024, Compare cmp_fct=Compare())
Construct an empty heap with internal storage.
constexpr size_t capacity() const noexcept
Return the maximum number of elements that can be stored.
ArrayHeap(T *ptr, const size_t &d, Compare cmp_fct=Compare())
Construct an empty heap using an external buffer.
T & put(T &&key)
Synonym for insert() (move overload).
static size_t r_index(const size_t &i)
T & top()
Return a mutable reference to the minimum element.
virtual ~ArrayHeap()
Destructor.
void allocate_storage(const size_t new_dim)
Allocate internal storage and reset the heap.
constexpr size_t size() const noexcept
Return the number of elements currently stored.
void remove(T &item)
Remove an element from the heap given a reference to it.
static constexpr bool nothrow_cmp
T & operator[](const size_t i)
Return a mutable reference to the i-th entry (1-based).
bool traverse(Operation &&operation=Operation()) noexcept(Aleph::NothrowPredicateWith< Operation &, T & >)
Traverse all elements (forwarding overload).
T & insert(T &&key)
Insert an element into the heap (move overload).
ArrayHeap & operator=(const ArrayHeap &h)
Iterator wrapper for C++ raw arrays.
T & get_curr() const
Get the current item with bounds checking.
T & get_curr_ne() const noexcept
Get the current item without bounds checking.
Read-only view of an Aleph iterator: the iterator of a constant container.
Performs order reversal of Compare by swapping operands.
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.
int cmp(const __gmp_expr< T, U > &expr1, const __gmp_expr< V, W > &expr2)
Singly linked list implementations with head-tail access.
constexpr void undo_sift_up(A ptr, size_t i, const size_t r)
Take back down an item that sift_up() took up.
constexpr size_t sift_down(A ptr, const size_t l, const size_t r, Compare &cmp)
Move the element at position l downwards to its place.
constexpr void undo_sift_down(A ptr, size_t i, const size_t l)
Take back up an item that sift_down() took down.
constexpr decltype(auto) sift_up(A ptr, const size_t l, const size_t r, Compare &cmp)
Move the element at position r upwards to its place.
Main namespace for Aleph-w library functions.
void sift_down_up(T *ptr, const size_t l, const size_t i, const size_t r, Compare &cmp)
Restore the heap property by sifting down and then sifting up.
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
bool valid_heap(T *array, const size_t l, const size_t r, const Compare &cmp=Compare())
Check whether a range satisfies the heap property.
constexpr size_t l_index(const size_t i)
Map a binary heap index to the index of its left child.
and
Check uniqueness with explicit hash + equality functors.
constexpr size_t u_index(const size_t &i)
Map a binary heap index to the index of its parent.
constexpr void faster_heapsort(T *array, const size_t n, const Compare &cmp=Compare())
Optimized version of heapsort.
constexpr void heapsort(T *array, const size_t n, const Compare &cmp=Compare())
Sort an array using the heapsort algorithm.
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.
const T & get_curr_ne() const noexcept
See get_curr().
Iterator(const ArrayHeap &h) noexcept
const T & get_curr() const
The current item, read-only: the heap order must not be broken.
The items of an array, numbered from 1.
constexpr T & operator[](const size_t i) const noexcept
The item of a number.
Generic list of items stored in a container.
Dynamic doubly linked list implementation.