36 template <
typename Key,
class Cmp = std::less<Key>, nat_t cap = 100>
47 std::swap(num_items, h.num_items);
48 std::swap(array, h.array);
49 std::swap(cmp, h.cmp);
61 : num_items(0), cmp(_cmp)
73 : num_items(h.num_items), cmp(h.cmp)
89 num_items = h.num_items;
118 return num_items == 0;
123 return num_items == cap;
139 throw std::overflow_error(
"Heap is full");
141 array[num_items] = item;
143 sift_up(array - 1, 1, num_items, cmp);
149 throw std::overflow_error(
"Heap is full");
151 array[num_items] = std::move(item);
153 sift_up(array - 1, 1, num_items, cmp);
159 throw std::underflow_error(
"Heap is empty");
167 throw std::underflow_error(
"Heap is empty");
169 Key ret_val = std::move(array[0]);
170 array[0] = std::move(array[num_items - 1]);
177 template <
typename Key,
class Cmp, nat_t cap>
180 for (
nat_t i = 1; i <= cap; ++i)
181 array[i] = h.array[i];
184 template <
typename Key,
class Cmp = std::less<Key>>
198 std::swap(cmp, h.cmp);
266 return BaseArray::is_empty();
271 return BaseArray::size();
276 BaseArray::append(item);
282 BaseArray::append(std::forward<Key>(item));
289 throw std::underflow_error(
"Heap is empty");
291 return BaseArray::get_first();
297 throw std::underflow_error(
"Heap is empty");
299 Key ret_val = std::move((*
this)[0]);
300 (*this)[0] = std::move(BaseArray::get_last());
301 BaseArray::remove_last();
307 template <
typename Key,
class Cmp>
314 while (u >= l and cmp(a[i - 1], a[u - 1]))
316 std::swap(a[i - 1], a[u - 1]);
322 template <
typename Key,
class Cmp>
332 if (cmp(a[c], a[c - 1]))
335 if (not cmp(a[c - 1], a[i - 1]))
338 std::swap(a[c - 1], a[i - 1]);
344 template <
typename Key>
346 public BaseBinTreeNode<Key, HeapNode<Key>, BinTreeNodeNullValue::NULLPTR>
353 unsigned int is_left : 4;
354 unsigned int is_leaf : 4;
357 : is_left(
true), is_leaf(
true)
408 bits.is_leaf =
value;
418 bits.is_left =
value;
428 template <
class HeapNode>
434 template <
typename Key,
class Cmp = std::less<Key>>
439 static Node * key_to_node(Key & k)
441 Node * node_zero = 0;
442 size_t offset = (size_t) &(
KEY(node_zero));
443 unsigned long addr = (
unsigned long)(&k);
444 return (
Node*) (addr - offset);
447 static bool is_in_list(
Node * p)
452 return U(
L(p)) ==
R(
L(p));
455 static bool has_sibling(
Node * p)
460 static void destroy_rec(
Node *,
Node *);
462 void swap_with_parent(
Node * p)
464 assert(num_items >= 2);
469 const bool p_has_sibling = has_sibling(p);
470 const bool p_is_in_list = is_in_list(p);
471 const bool pp_is_in_list = is_in_list(pp);
472 const bool p_has_child = not p->
is_leaf();
493 sp = p ==
L(pp) ?
R(pp) :
L(pp);
511 L(lcp) =
R(lcp) = pp;
517 L(rcp) =
R(rcp) = pp;
525 if (not p_is_in_list)
527 U(lcp) =
U(rcp) = pp;
548 if (not pp_is_in_list)
553 R(lcp) =
L(rcp) = pp;
582 void swap_last_with_root()
584 assert(num_items > 1);
585 assert(
U(root) == head);
586 assert(not root->is_leaf());
587 assert(last->is_leaf());
591 Node * lRoot =
L(root);
592 Node * rRoot =
R(root);
593 Node * f_last =
U(last);
594 Node * prev_last =
L(last);
595 Node * next_last =
R(last);
597 if (
L(f_last) == last)
603 std::swap(
U(root),
U(last));
610 std::swap(
L(root),
L(last));
611 std::swap(
R(root),
R(last));
613 U(lRoot) =
U(rRoot) = last;
621 R(prev_last) =
L(next_last) = root;
623 else if (num_items == 3)
625 assert(
R(root) == last);
626 assert(
L(last) ==
L(root) and
R(last) ==
L(root));
631 Node *s_last =
L(last);
637 L(root) =
R(root) = s_last;
638 R(s_last) =
L(s_last) = root;
642 assert(
L(root) == last);
646 R(last) =
L(last) = root;
647 R(root) =
L(root) = last;
650 std::swap(root->get_bits(), last->get_bits());
651 std::swap(root, last);
654 void replace_node(
Node * node,
Node * new_node)
656 assert(node != new_node);
657 assert(node != last);
659 Node * parent =
U(node);
660 Node * left_child =
L(node);
661 Node * right_child =
R(node);
663 U(new_node) = parent;
664 L(new_node) = left_child;
665 R(new_node) = right_child;
669 assert(
L(parent) == node);
670 L(parent) = new_node;
674 assert(
R(parent) == node);
675 R(parent) = new_node;
680 R(left_child) = new_node;
681 L(right_child) = new_node;
685 U(left_child) = new_node;
687 if (
U(right_child) == node)
688 U(right_child) = new_node;
691 assert(left_child == last);
692 R(left_child) = new_node;
693 L(right_child) = new_node;
706 assert(num_items == 0);
749 throw std::underflow_error(
"Heap is empty");
760 swap_last_with_root();
771 assert(last != root and num_items > 0);
772 assert(last->is_leaf());
774 Node * ret_val = last;
776 Node * new_last =
L(last);
800 throw std::underflow_error (
"Heap is empty");
806 return remove_last();
808 Node * q = remove_last();
829 void update(
Node * p)
852 : head(&head_node), root(
R(head)), last(nullptr), num_items(0), cmp(_cmp)
882 std::swap(root, h.root);
883 std::swap(last, h.last);
884 std::swap(num_items, h.num_items);
885 std::swap(cmp, h.cmp);
907 return num_items == 0;
913 return KEY(insert_node(p));
918 Node * p =
new Node(std::forward<Key>(k));
919 return KEY(insert_node(p));
925 throw std::underflow_error(
"Heap is empty");
932 Node * p = remove_top();
933 Key ret_val = std::move(
KEY(p));
938 void remove(Key & item)
940 Node * p =
remove(key_to_node(item));
945 template <
typename Key,
class Cmp>
954 destroy_rec(
L(p), n);
957 destroy_rec(
R(p), n);
962 template <
typename Key,
class Cmp>
965 while (p != root and cmp(
KEY(p),
KEY(
U(p))))
969 template <
typename Key,
class Cmp>
980 if (not cmp(
KEY(c),
KEY(p)))
987 template <
typename Key,
class Cmp>
1001 destroy_rec(root,
U(last));
1003 destroy_rec(root,
nullptr);
1012 # endif // DSGHEAP_H Arc< GT > * DataType
Definition: heap.H:846
HeapNode(BinTreeNodeCtor ctor)
Definition: heap.H:385
nat_t size() const
Definition: heap.H:269
LHeap(LHeap &&h)
Definition: heap.H:863
LHeap(Cmp &&_cmp=Cmp())
Definition: heap.H:857
unsigned int is_leaf() const
Definition: heap.H:401
void insert(const Key &item)
Definition: heap.H:136
T DataType
Definition: array.H:194
HeapNode(Key &&k)
Definition: heap.H:379
Key ValueType
Definition: heap.H:56
nat_t size() const
Definition: heap.H:900
void clear()
Definition: heap.H:988
DynHeap(Cmp &_cmp)
Definition: heap.H:209
nat_t SizeType
Definition: heap.H:848
Cmp CmpType
Definition: heap.H:207
FixedHeap & operator=(const FixedHeap &h)
Definition: heap.H:84
const Key & top() const
Definition: heap.H:286
void insert(Key &&item)
Definition: heap.H:280
void set_leaf(unsigned int value)
Definition: heap.H:406
~LHeap()
Definition: heap.H:869
Key DataType
Definition: heap.H:55
HeapNode *& get_parent()
Definition: heap.H:391
void clear()
Definition: heap.H:259
const Cmp & get_cmp() const
Definition: heap.H:254
void reset()
Definition: heap.H:421
HeapNode *& U(HeapNode *p)
Definition: heap.H:429
Arc< GT > * ItemType
Definition: heap.H:844
Cmp & get_cmp()
Definition: heap.H:101
void insert(const Key &item)
Definition: heap.H:274
FixedHeap(Cmp &_cmp)
Definition: heap.H:60
bool is_empty() const
Definition: heap.H:116
bool is_full() const
Definition: heap.H:121
unsigned int is_left() const
Definition: heap.H:411
LHeap(Cmp &_cmp)
Definition: heap.H:851
Key ItemType
Definition: heap.H:53
BinTreeNode *& R(BinTreeNode *p)
Definition: nodesdef.H:993
Definition: nodesdef.H:902
FixedHeap(const FixedHeap &h)
Definition: heap.H:72
const Key & insert(const Key &k)
Definition: heap.H:910
Cmp & get_cmp()
Definition: heap.H:888
const Cmp & get_cmp() const
Definition: heap.H:106
HeapNode(const Key &k)
Definition: heap.H:373
Cmp CmpType
Definition: heap.H:58
void insert(Key &&item)
Definition: heap.H:146
nat_t SizeType
Definition: heap.H:57
const Key & insert(Key &&k)
Definition: heap.H:916
nat_t size() const
Definition: heap.H:126
DynHeap(DynHeap &&h)
Definition: heap.H:227
T ItemType
Definition: array.H:192
bool is_empty() const
Definition: heap.H:264
typename GT::Node Node
Definition: graphutilities.H:78
void set_left(unsigned int value)
Definition: heap.H:416
BinTreeNode *& L(BinTreeNode *p)
Definition: nodesdef.H:987
HeapNode()
Definition: heap.H:367
Key KeyType
Definition: heap.H:54
Cmp & get_cmp()
Definition: heap.H:249
void sift_up(T *, nat_t, nat_t, Cmp &)
Definition: sort.H:336
void clear()
Definition: heap.H:111
const Cmp & get_cmp() const
Definition: heap.H:893
Arc< GT > * KeyType
Definition: heap.H:845
DynHeap(const DynHeap &h)
Definition: heap.H:221
nat_t SizeType
Definition: array.H:196
const Key & top() const
Definition: heap.H:922
FixedHeap(FixedHeap &&h)
Definition: heap.H:78
BinTreeNode::KeyType & KEY(BinTreeNode *p)
Definition: nodesdef.H:981
FixedHeap(Cmp &&_cmp=Cmp())
Definition: heap.H:66
BinTreeNodeCtor
Definition: nodesdef.H:897
T ValueType
Definition: array.H:195
void sift_down(T *, nat_t, nat_t, Cmp &)
Definition: sort.H:357
bool is_empty() const
Definition: heap.H:905
unsigned long int nat_t
Definition: types.H:50
DynHeap(Cmp &&_cmp=Cmp())
Definition: heap.H:215
nat_t get_capacity() const
Definition: heap.H:131
Value & value(MapKey< Key, Value > &item)
Definition: map.H:77
Definition: graphalgorithms.H:1212
void swap(LHeap &h)
Definition: heap.H:880
Arc< GT > * ValueType
Definition: heap.H:847
const Key & top() const
Definition: heap.H:156
HeapNodeBits & get_bits()
Definition: heap.H:396
T KeyType
Definition: array.H:193