47#ifndef TPL_ARRAYHEAP_H
48#define TPL_ARRAYHEAP_H
83 template <
typename T,
class Compare>
88 for (
size_t p; i >
l; i = p)
99 std::swap(ptr[p], ptr[i]);
118 template <
typename T,
class Compare>
134 if (
cmp(ptr[c + 1], ptr[c]))
140 std::swap(ptr[c], ptr[i]);
158 template <
typename T,
class Compare>
182 template <
typename T,
class Compare = Aleph::less<T>>
184 void heapsort(
T *array,
const size_t n,
const Compare &
cmp = Compare())
189 for (
size_t i = 2; i <= n; ++i)
191 for (
size_t i = n; i > 1; --i)
193 std::swap(array[1], array[i]);
213 template <
typename T,
class Compare = Aleph::less<T>>
220 for (
size_t i = n / 2; i >= 1; --i)
222 for (
size_t i = n; i > 1; --i)
224 std::swap(array[1], array[i]);
240 template <
typename T,
class Compare = Aleph::less<T>>
242 const Compare &
cmp = Compare())
263 template <
typename T,
class Compare = Aleph::less<T>>
309 std::swap(
array,
h.array);
310 std::swap(
dim,
h.dim);
313 std::swap(
cmp,
h.cmp);
563 const auto i =
static_cast<size_t>(&data -
array);
581 const auto idx =
static_cast<size_t>(&item -
array);
624 template <
class Operation>
644 template <
class Operation>
656 template <
class Operation>
668 template <
class Operation>
680 template <
class Operation>
Variadic constructor macros for containers.
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.
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).
T & put(const T &key)
Synonym for insert().
bool traverse(Operation &&operation=Operation())
Traverse all elements (forwarding overload).
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
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.
bool traverse(Operation &operation)
Traverse all elements.
T & append(T &&key)
Synonym for insert() (move overload).
void update(T &data)
Update the priority of an element stored in the heap.
bool traverse(Operation &operation) const
Traverse all elements (const overload).
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 getMin()
Remove the smallest element in the heap and return a copy of its value.
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.
T & operator[](const size_t i)
Return a mutable reference to the i-th entry (1-based).
T & insert(T &&key)
Insert an element into the heap (move overload).
ArrayHeap & operator=(const ArrayHeap &h)
bool traverse(Operation &&operation=Operation()) const
Traverse all elements (const forwarding overload).
Iterator wrapper for C++ raw arrays.
Performs order reversal of Compare by swapping operands.
Equality test for containers.
Common methods to the Aleph-w ( ) containers.
Aleph::DynList< __T > maps(Operation &op) const
Map the elements of the container.
Common sequential searching methods on containers.
Mixin that adds STL begin()/end() and cbegin()/cend() to Aleph containers.
int cmp(const __gmp_expr< T, U > &expr1, const __gmp_expr< V, W > &expr2)
Singly linked list implementations with head-tail access.
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.
size_t l_index(const size_t i)
Map a binary heap index to the index of its left child.
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.
void heapsort(T *array, const size_t n, const Compare &cmp=Compare())
Sort an array using the heapsort algorithm.
void faster_heapsort(T *array, const size_t n, const Compare &cmp=Compare())
Optimized version of heapsort.
std::decay_t< typename HeadC::Item_Type > T
size_t u_index(const size_t &i)
Map a binary heap index to the index of its parent.
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.
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.
DynList< T > maps(const C &c, Op op)
Classic map operation.
Iterator(const ArrayHeap &h) noexcept
Generic list of items stored in a container.
Dynamic doubly linked list implementation.