5#ifndef INCLUDE_GUARD_ARENE_BASE_ARENE_BASE_INTRUSIVE_PRIORITY_QUEUE_HPP_
6#define INCLUDE_GUARD_ARENE_BASE_ARENE_BASE_INTRUSIVE_PRIORITY_QUEUE_HPP_
13#include "arene/base/intrusive/detail/default_tag.hpp"
15#include "arene/base/stdlib_choice/climits.hpp"
16#include "arene/base/stdlib_choice/cstddef.hpp"
17#include "arene/base/stdlib_choice/exchange.hpp"
18#include "arene/base/stdlib_choice/is_base_of.hpp"
19#include "arene/base/stdlib_choice/is_const.hpp"
20#include "arene/base/stdlib_choice/less.hpp"
21#include "arene/base/stdlib_choice/move.hpp"
40template <
typename T,
typename Compare = std::less<>,
typename Tag = detail::default_tag>
114 constexpr auto top() const noexcept -> T& {
116 return static_cast<T&
>(*root_);
134 root_ =
static_cast<tree_node*
>(&value);
135 parent(*root_) = root_;
142 auto& parent_of_new_node = find_node_by_index((size_ + 1U) >> 1U);
143 if (!is_even(size_)) {
145 left(parent_of_new_node) =
static_cast<tree_node*
>(&value);
148 right(parent_of_new_node) =
static_cast<tree_node*
>(&value);
150 parent(value) = &parent_of_new_node;
160 constexpr void pop() noexcept {
179 swap(size_, other.size_);
180 swap(root_, other.root_);
205 static constexpr auto is_even(
std::size_t value)
noexcept ->
bool {
return (value &
std::size_t{1U}) == 0U; }
210 constexpr void erase_impl(tree_node& node)
noexcept {
213 parent(*root_) =
nullptr;
219 auto& last_node = find_node_by_index(size_);
220 bool reheapify_needed{
false};
222 if (
static_cast<tree_node*
>(&node) != &last_node) {
224 swap_nodes(node, last_node);
225 reheapify_needed =
true;
229 if (left(*parent(node)) ==
static_cast<tree_node*
>(&node)) {
230 left(*parent(node)) =
nullptr;
232 right(*parent(node)) =
nullptr;
234 parent(node) =
nullptr;
237 if (reheapify_needed) {
238 re_heapify(last_node);
249 constexpr auto compare_value(tree_node
const& node1, tree_node
const& node2)
const noexcept ->
bool {
250 return comp_(
static_cast<T const&
>(node1),
static_cast<T const&
>(node2));
255 constexpr void move_node_up(tree_node& node)
noexcept {
256 while ((&node != root_) &&
257 compare_value(*parent(node), node)) {
258 if (left(*parent(node)) == &node) {
259 swap_with_left(*parent(node));
261 swap_with_right(*parent(node));
268 constexpr void move_node_down(tree_node& node)
noexcept {
269 while ((left(node) !=
nullptr) || (right(node) !=
nullptr)) {
270 bool should_swap_with_left{(left(node) !=
nullptr) && compare_value(node, *left(node))};
271 bool should_swap_with_right{(right(node) !=
nullptr) && compare_value(node, *right(node))};
273 if (should_swap_with_left && should_swap_with_right) {
274 should_swap_with_left = compare_value(*right(node), *left(node));
277 if (should_swap_with_left) {
278 swap_with_left(node);
279 }
else if (should_swap_with_right) {
280 swap_with_right(node);
290 constexpr void re_heapify(tree_node& node)
noexcept {
292 move_node_down(node);
300 constexpr auto find_node_by_index(
std::size_t n)
const noexcept -> tree_node& {
303 for (
std::size_t i{1U}; i < layers_num + 1U; ++i) {
314 if (is_even(n >> (layers_num - i))) {
327 constexpr void swap_with_left(tree_node& node)
noexcept { swap_nodes(node, *left(node)); }
331 constexpr void swap_with_right(tree_node& node)
noexcept { swap_nodes(node, *right(node)); }
337 constexpr auto contains(tree_node& node)
const noexcept ->
bool {
338 auto* traversed_root = &node;
339 while ((parent(*traversed_root) !=
nullptr) && (parent(*traversed_root) != traversed_root)) {
340 traversed_root = parent(*traversed_root);
343 return traversed_root == root_;
350 constexpr void swap_nodes(tree_node& node_1, tree_node& node_2)
noexcept {
351 if ((root_ == &node_1) || (root_ == &node_2)) {
352 parent(*root_) =
nullptr;
355 if (parent(node_1) != parent(node_2)) {
361 update_child_of_parent(node_2, node_1);
364 update_child_of_parent(node_1, node_2);
366 auto* parent_node_ptr = parent(node_1);
368 auto& common_parent_node = *parent_node_ptr;
378 update_parent_of_child(node_1);
379 update_parent_of_child(node_2);
382 if (root_ == &node_1) {
384 parent(*root_) = root_;
392 static constexpr void update_child_of_parent(tree_node& node_1, tree_node& node_2)
noexcept {
393 if (parent(node_1) !=
nullptr) {
394 if (left(*parent(node_1)) == &node_2) {
395 left(*parent(node_1)) = &node_1;
397 right(*parent(node_1)) = &node_1;
406 static constexpr void update_parent_of_child(tree_node& node)
noexcept {
407 if (left(node) !=
nullptr) {
408 parent(*left(node)) = &node;
410 if (right(node) !=
nullptr) {
411 parent(*right(node)) = &node;
418 static constexpr auto parent(tree_node& node)
noexcept -> tree_node*& {
419 return get_parent_link(node, Tag{}, detail::binary_tree_node_pass_key{});
424 static constexpr auto left(tree_node& node)
noexcept -> tree_node*& {
425 return get_left_link(node, Tag{}, detail::binary_tree_node_pass_key{});
430 static constexpr auto right(tree_node& node)
noexcept -> tree_node*& {
431 return get_right_link(node, Tag{}, detail::binary_tree_node_pass_key{});
440 tree_node* root_{
nullptr};
The tree node of the intrusive container. User type can be derived from this class to support intrusi...
Definition binary_tree_node.hpp:41
T & reference
reference type
Definition priority_queue.hpp:57
constexpr void swap(priority_queue &other) noexcept
Swap the contents of two priority queues.
Definition priority_queue.hpp:177
auto operator=(priority_queue const &) -> priority_queue &=delete
Deleted copy constructor.
constexpr auto size() const noexcept -> size_type
Return the number of elements in the priority_queue.
Definition priority_queue.hpp:121
Compare value_compare
value comparison type
Definition priority_queue.hpp:51
constexpr void erase(reference node) noexcept
Erase a specific node from the priority_queue.
Definition priority_queue.hpp:169
constexpr auto operator!=(priority_queue const &) const -> bool=delete
deleted equality operator
priority_queue(priority_queue const &)=delete
Deleted copy constructor.
constexpr priority_queue() noexcept=default
Construct an empty priority_queue.
constexpr auto top() const noexcept -> T &
Return a reference to the top element of the priority_queue.
Definition priority_queue.hpp:114
constexpr void pop() noexcept
Pop the top element from the priority_queue.
Definition priority_queue.hpp:160
constexpr auto empty() const noexcept -> bool
Check whether the priority_queue is empty.
Definition priority_queue.hpp:125
T const & const_reference
const reference type
Definition priority_queue.hpp:59
std::size_t size_type
size type
Definition priority_queue.hpp:55
constexpr auto operator=(priority_queue &&other) noexcept -> priority_queue &
Move assign a priority_queue from another priority_queue. The other priority_queue will be empty afte...
Definition priority_queue.hpp:83
T value_type
value type
Definition priority_queue.hpp:53
Tag tag
tag type
Definition priority_queue.hpp:61
constexpr void push(reference value) noexcept
Push an element to the priority_queue.
Definition priority_queue.hpp:130
constexpr auto operator==(priority_queue const &) const -> bool=delete
deleted equality operator
~priority_queue()=default
Destructor.
static constexpr value_type value
Definition integral_constant.hpp:34
#define ARENE_INVARIANT(condition)
Check that an invariant holds, and terminate the application if it does not.
Definition contract.hpp:161
#define ARENE_PRECONDITION(condition)
Check that a precondition holds, and terminate the application if it does not.
Definition contract.hpp:150
Public export header for the contracts subpackage.
Public export location for content from arene/base/math.
Definition binary_tree_node.hpp:14
static ARENE_MAYBE_UNUSED constexpr auto const & swap
A Customization Point Object implementation of the constexpr compatible std::swap from C++17.
Definition swap.hpp:130
Definition accumulate.hpp:37
constexpr auto log2(std::size_t value) noexcept -> std::size_t
Computes the integer base-2 log of a value.
Definition log2.hpp:30
constexpr bool is_invocable_v
Determine if a functor type is invocable with the specified argument types.
Definition accumulate.hpp:36
constexpr auto move(T &&value) noexcept -> remove_reference_t< T > &&
Cast a value to an rvalue.
Definition move.hpp:30
::size_t size_t
A type suitable for holding the size of an object.
Definition cstddef.hpp:29
constexpr auto exchange(T &target, U &&source) noexcept(is_nothrow_move_constructible_v< T > &&is_nothrow_assignable_v< T &, U && >) -> T
Assign a new value to an object and return the old value. Implements std::exchange.
Definition exchange.hpp:33