Open
Graph Drawing
Framework

 v. 2023.09 (Elderberry)
 

Loading...
Searching...
No Matches
Heap.h
Go to the documentation of this file.
1/******************************************************************************************[Heap.h]
2Copyright (c) 2003-2006, Niklas Een, Niklas Sorensson
3Copyright (c) 2007-2010, Niklas Sorensson
4
5Permission is hereby granted, free of charge, to any person obtaining a copy of this software and
6associated documentation files (the "Software"), to deal in the Software without restriction,
7including without limitation the rights to use, copy, modify, merge, publish, distribute,
8sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
9furnished to do so, subject to the following conditions:
10
11The above copyright notice and this permission notice shall be included in all copies or
12substantial portions of the Software.
13
14THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT
15NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
16NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
17DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT
18OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
19**************************************************************************************************/
20
21#pragma once
22
24
25#pragma GCC visibility push(default)
26namespace Minisat {
27namespace Internal {
28
29//=================================================================================================
30// A heap implementation with support for decrease/increase key.
31
32
33template<class Comp>
34class Heap {
35 Comp lt; // The heap is a minimum-heap with respect to this comparator
36 vec<int> heap; // Heap of integers
37 vec<int> indices; // Each integers position (index) in the Heap
38
39 // Index "traversal" functions
40 static inline int left (int i) { return i*2+1; }
41 static inline int right (int i) { return (i+1)*2; }
42 static inline int parent(int i) { return (i-1) >> 1; }
43
44
45 void percolateUp(int i)
46 {
47 int x = heap[i];
48 int p = parent(i);
49
50 while (i != 0 && lt(x, heap[p])){
51 heap[i] = heap[p];
52 indices[heap[p]] = i;
53 i = p;
54 p = parent(p);
55 }
56 heap [i] = x;
57 indices[x] = i;
58 }
59
60
61 void percolateDown(int i)
62 {
63 int x = heap[i];
64 while (left(i) < heap.size()){
65 int child = right(i) < heap.size() && lt(heap[right(i)], heap[left(i)]) ? right(i) : left(i);
66 if (!lt(heap[child], x)) break;
67 heap[i] = heap[child];
68 indices[heap[i]] = i;
69 i = child;
70 }
71 heap [i] = x;
72 indices[x] = i;
73 }
74
75
76 public:
77 Heap(const Comp& c) : lt(c) { }
78
79 int size () const { return heap.size(); }
80 bool empty () const { return heap.size() == 0; }
81 bool inHeap (int n) const { return n < indices.size() && indices[n] >= 0; }
82 int operator[](int index) const { assert(index < heap.size()); return heap[index]; }
83
84
85 void decrease (int n) { assert(inHeap(n)); percolateUp (indices[n]); }
86 void increase (int n) { assert(inHeap(n)); percolateDown(indices[n]); }
87
88
89 // Safe variant of insert/decrease/increase:
90 void update(int n)
91 {
92 if (!inHeap(n))
93 insert(n);
94 else {
97 }
98
99
100 void insert(int n)
101 {
102 indices.growTo(n+1, -1);
103 assert(!inHeap(n));
104
105 indices[n] = heap.size();
106 heap.push(n);
108 }
109
110
112 {
113 int x = heap[0];
114 heap[0] = heap.last();
115 indices[heap[0]] = 0;
116 indices[x] = -1;
117 heap.pop();
118 if (heap.size() > 1) percolateDown(0);
119 return x;
120 }
121
122
123 // Rebuild the heap from scratch, using the elements in 'ns':
124 void build(vec<int>& ns) {
125 for (int i = 0; i < heap.size(); i++)
126 indices[heap[i]] = -1;
127 heap.clear();
128
129 for (int i = 0; i < ns.size(); i++){
130 indices[ns[i]] = i;
131 heap.push(ns[i]); }
132
133 for (int i = heap.size() / 2 - 1; i >= 0; i--)
134 percolateDown(i);
135 }
136
137 void clear(bool dealloc = false)
138 {
139 for (int i = 0; i < heap.size(); i++)
140 indices[heap[i]] = -1;
141 heap.clear(dealloc);
142 }
143};
144
145
146//=================================================================================================
147} // namespace Internal
148} // namespace Minisat
149#pragma GCC visibility pop
void update(int n)
Definition Heap.h:90
void insert(int n)
Definition Heap.h:100
bool inHeap(int n) const
Definition Heap.h:81
int size() const
Definition Heap.h:79
vec< int > indices
Definition Heap.h:37
static int right(int i)
Definition Heap.h:41
static int parent(int i)
Definition Heap.h:42
Heap(const Comp &c)
Definition Heap.h:77
void increase(int n)
Definition Heap.h:86
bool empty() const
Definition Heap.h:80
void percolateDown(int i)
Definition Heap.h:61
int operator[](int index) const
Definition Heap.h:82
void clear(bool dealloc=false)
Definition Heap.h:137
void percolateUp(int i)
Definition Heap.h:45
void build(vec< int > &ns)
Definition Heap.h:124
static int left(int i)
Definition Heap.h:40
void decrease(int n)
Definition Heap.h:85
vec< int > heap
Definition Heap.h:36
int size(void) const
Definition Vec.h:64
void pop(void)
Definition Vec.h:77
void clear(bool dealloc=false)
Definition Vec.h:122
void push(void)
Definition Vec.h:74
void growTo(int size)
Definition Vec.h:114
const T & last(void) const
Definition Vec.h:83