13 template <
typename T,
typename Compare = std::less<T>,
typename Allocator = PolymorphicAllocator<>>
24 using allocator_type = std::allocator_traits<Allocator>::template rebind_alloc<T>;
36 requires std::default_initializable<Compare>
43 noexcept(
noexcept(
container_type(allocator)) &&
noexcept(Compare()))
44 requires std::default_initializable<Compare>
52 noexcept(
noexcept(
container_type(allocator)) &&
noexcept(Compare(std::move(comparator))))
53 requires !std::default_initializable<Compare>
64 noexcept(
noexcept(
container_type(std::move(rhs.data_), allocator)) &&
noexcept(Compare(std::move(rhs.comparator_))))
73 template <std::input_iterator It>
84 template <std::input_iterator It>
94 return data_.get_allocator();
101 constexpr std::size_t
size() const noexcept {
108 return data_.capacity();
113 constexpr bool empty() const noexcept {
114 return data_.empty();
119 const T&
top() const noexcept {
120 assert(!
data_.empty() &&
"call top on an empty heap");
121 return data_.front();
149 template <
typename ...Args>
151 data_.emplaceBack(std::forward<Args>(args)...);
152 T value = std::move(
data_.back());
158 noexcept(std::is_nothrow_move_assignable_v<T>) {
159 assert(!
data_.empty() &&
"call drop on an empty heap");
168 noexcept(std::is_nothrow_move_constructible_v<T> && std::is_nothrow_move_assignable_v<T>) {
169 assert(!
data_.empty() &&
"call pop on an empty heap");
170 T value = std::move(
data_.front());
179 noexcept(std::is_nothrow_move_assignable_v<T>) {
182 if (child >=
data_.size()) {
187 std::size_t rightChild = child + 1;
198 void fixUp(std::size_t index, std::size_t
top, T&& value)
199 noexcept(std::is_nothrow_move_assignable_v<T>) {
200 while (index >
top) {
209 data_[index] = std::move(value);
213 noexcept(std::is_nothrow_move_constructible_v<T>&& std::is_nothrow_move_assignable_v<T>) {
214 if (
data_.size() < 2) {
217 for (std::size_t i =
data_.size() / 2; i-- > 0 ;) {
218 T value = std::move(data_[i]);
219 std::size_t hole = fixDownWithHole(i);
220 fixUp(hole, i, std::move(value));
224 static constexpr bool hasParent(std::size_t i)
noexcept {
228 static constexpr std::size_t
getParent(std::size_t i)
noexcept {
229 assert(i > 0 &&
"calling parent on the root node");
constexpr BinaryHeap(Compare comparator, const allocator_type &allocator={}) noexcept(noexcept(container_type(allocator)) &&noexcept(Compare(std::move(comparator))))
Constructs an empty heap with a specific allocator.
constexpr bool empty() const noexcept
Return true if empty.
std::size_t fixDownWithHole(std::size_t index) noexcept(std::is_nothrow_move_assignable_v< T >)
constexpr BinaryHeap(BinaryHeap &&)=default
constexpr BinaryHeap(const BinaryHeap &rhs, const allocator_type &allocator)
constexpr std::size_t capacity() const noexcept
Return the heap capacity.
std::ptrdiff_t difference_type
Vector< T, Allocator > container_type
void heapify() noexcept(std::is_nothrow_move_constructible_v< T > &&std::is_nothrow_move_assignable_v< T >)
void push(T &&value)
Push the value to the heap.
const T & top() const noexcept
Return the min element.
void clear() noexcept
Clear the heap.
constexpr BinaryHeap(BinaryHeap &&rhs, const allocator_type &allocator) noexcept(noexcept(container_type(std::move(rhs.data_), allocator)) &&noexcept(Compare(std::move(rhs.comparator_))))
const T & const_reference
allocator_type get_allocator() const noexcept
void reserve(std::size_t capacity)
Reserve the heap capacity.
static constexpr std::size_t getLeftChild(std::size_t i) noexcept
constexpr BinaryHeap(const BinaryHeap &)=default
constexpr BinaryHeap(It first, It last, Compare comparator, const allocator_type &allocator={})
Iterator range constructor.
constexpr BinaryHeap() noexcept(noexcept(container_type()) &&noexcept(Compare()))
Constructs an empty heap with default comparator and allocator. Comparator must be default initializa...
static constexpr std::size_t getParent(std::size_t i) noexcept
constexpr std::size_t size() const noexcept
Return the heap size.
T pop() noexcept(std::is_nothrow_move_constructible_v< T > &&std::is_nothrow_move_assignable_v< T >)
Pop the min element.
void push(const T &value)
Push the value to the heap.
std::allocator_traits< Allocator >::template rebind_alloc< T > allocator_type
void emplace(Args &&... args)
Construct the value inplace in the heap.
constexpr BinaryHeap(It first, It last, const allocator_type &allocator={})
Iterator range constructor. Comparator must be default initializable.
static constexpr bool hasParent(std::size_t i) noexcept
constexpr BinaryHeap(const allocator_type &allocator) noexcept(noexcept(container_type(allocator)) &&noexcept(Compare()))
Constructs an empty heap with a specific allocator. Comparator must be default initializable.
void drop() noexcept(std::is_nothrow_move_assignable_v< T >)
Drop the min element.
void fixUp(std::size_t index, std::size_t top, T &&value) noexcept(std::is_nothrow_move_assignable_v< T >)