Google OR-Tools v9.11
a fast and portable software suite for combinatorial optimization
|
#include <adjustable_k_ary_heap.h>
Public Types | |
using | Aggregate = std::pair<Priority, Index> |
using | HeapIndex = Index |
Public Member Functions | |
AdjustableKAryHeap () | |
AdjustableKAryHeap (const std::vector< Aggregate > &elements, HeapIndex universe_size) | |
AdjustableKAryHeap (const std::vector< Index > &indices, const std::vector< Priority > &priorities, HeapIndex universe_size) | |
void | Clear () |
void | Load (const std::vector< Aggregate > &elements, HeapIndex universe_size) |
void | Load (const std::vector< Index > &indices, const std::vector< Priority > &priorities, HeapIndex universe_size) |
void | Pop () |
Index | TopIndex () const |
Priority | TopPriority () const |
HeapIndex | heap_size () const |
Returns the number of elements in the heap. | |
bool | IsEmpty () const |
True iff the heap is empty. | |
void | Insert (Aggregate element) |
Insert an element into the heap. | |
bool | Remove (Index index) |
void | Update (Aggregate element) |
Change the value of an element. | |
bool | Contains (Index index) const |
Checks if the element with index is in the heap. | |
bool | CheckHeapProperty () const |
Checks that the heap is well-formed. | |
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. Adjustable k-ary heap for std::pair<Priority, Index> classes containing a priority and an index referring to an array where the relevant data is stored.
The comparator is the default comparator for pairs, i.e. the index is used as a tie-breaker for the priority, thus making the code more repeatable.
Because the class uses indices and vectors, it is much faster than AdjustablePriorityQueue, even in the binary heap case.
k-ary heaps are useful when SiftDown() (aka Decrease) is called more often than Pop() (aka Extract).
Namely, Pop() has a complexity in O(k * log_k (n)), while SiftDown() is in O(log_k(n)), even when k = 2. This explains the small gain.
In the implementation below, k is denoted as Arity.
Definition at line 43 of file adjustable_k_ary_heap.h.
using AdjustableKAryHeap< Priority, Index, Arity, IsMaxHeap >::Aggregate = std::pair<Priority, Index> |
Definition at line 45 of file adjustable_k_ary_heap.h.
using AdjustableKAryHeap< Priority, Index, Arity, IsMaxHeap >::HeapIndex = Index |
Definition at line 46 of file adjustable_k_ary_heap.h.
|
inline |
Definition at line 52 of file adjustable_k_ary_heap.h.
|
inlineexplicit |
Construct a k-heap from an existing vector, tracking original indices. universe_size
is the maximum possible index in elements
.
Definition at line 56 of file adjustable_k_ary_heap.h.
|
inlineexplicit |
Definition at line 61 of file adjustable_k_ary_heap.h.
|
inline |
Checks that the heap is well-formed.
Definition at line 174 of file adjustable_k_ary_heap.h.
|
inline |
Definition at line 67 of file adjustable_k_ary_heap.h.
|
inline |
Checks if the element with index is in the heap.
Definition at line 169 of file adjustable_k_ary_heap.h.
|
inline |
Returns the number of elements in the heap.
Definition at line 122 of file adjustable_k_ary_heap.h.
|
inline |
Insert an element into the heap.
Definition at line 128 of file adjustable_k_ary_heap.h.
|
inline |
True iff the heap is empty.
Definition at line 125 of file adjustable_k_ary_heap.h.
|
inline |
Definition at line 73 of file adjustable_k_ary_heap.h.
|
inline |
Definition at line 84 of file adjustable_k_ary_heap.h.
|
inline |
Removes the top element from the heap (smallest for min-heap, largest for max-heap), and rearranges the heap. This will CHECK-fail if the heap is empty (through Top()).
Definition at line 99 of file adjustable_k_ary_heap.h.
|
inline |
Removes the element at index. Returns false if the element does not appear in the heap.
Definition at line 147 of file adjustable_k_ary_heap.h.
|
inline |
Returns the index of the top element, without modifying the heap.
Definition at line 107 of file adjustable_k_ary_heap.h.
|
inline |
Returns the index of the top element, without modifying the heap.
Definition at line 116 of file adjustable_k_ary_heap.h.
|
inline |
Change the value of an element.
Definition at line 155 of file adjustable_k_ary_heap.h.