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
25namespace Minisat {
26namespace Internal {
27
28//=================================================================================================
29// A heap implementation with support for decrease/increase key.
30
31
32template<class Comp>
33class Heap {
34 Comp lt; // The heap is a minimum-heap with respect to this comparator
35 vec<int> heap; // Heap of integers
36 vec<int> indices; // Each integers position (index) in the Heap
37
38 // Index "traversal" functions
39 static inline int left (int i) { return i*2+1; }
40 static inline int right (int i) { return (i+1)*2; }
41 static inline int parent(int i) { return (i-1) >> 1; }
42
43
44 void percolateUp(int i)
45 {
46 int x = heap[i];
47 int p = parent(i);
48
49 while (i != 0 && lt(x, heap[p])){
50 heap[i] = heap[p];
51 indices[heap[p]] = i;
52 i = p;
53 p = parent(p);
54 }
55 heap [i] = x;
56 indices[x] = i;
57 }
58
59
60 void percolateDown(int i)
61 {
62 int x = heap[i];
63 while (left(i) < heap.size()){
64 int child = right(i) < heap.size() && lt(heap[right(i)], heap[left(i)]) ? right(i) : left(i);
65 if (!lt(heap[child], x)) break;
66 heap[i] = heap[child];
67 indices[heap[i]] = i;
68 i = child;
69 }
70 heap [i] = x;
71 indices[x] = i;
72 }
73
74
75 public:
76 Heap(const Comp& c) : lt(c) { }
77
78 int size () const { return heap.size(); }
79 bool empty () const { return heap.size() == 0; }
80 bool inHeap (int n) const { return n < indices.size() && indices[n] >= 0; }
81 int operator[](int index) const { assert(index < heap.size()); return heap[index]; }
82
83
84 void decrease (int n) { assert(inHeap(n)); percolateUp (indices[n]); }
85 void increase (int n) { assert(inHeap(n)); percolateDown(indices[n]); }
86
87
88 // Safe variant of insert/decrease/increase:
89 void update(int n)
90 {
91 if (!inHeap(n))
92 insert(n);
93 else {
96 }
97
98
99 void insert(int n)
100 {
101 indices.growTo(n+1, -1);
102 assert(!inHeap(n));
103
104 indices[n] = heap.size();
105 heap.push(n);
107 }
108
109
111 {
112 int x = heap[0];
113 heap[0] = heap.last();
114 indices[heap[0]] = 0;
115 indices[x] = -1;
116 heap.pop();
117 if (heap.size() > 1) percolateDown(0);
118 return x;
119 }
120
121
122 // Rebuild the heap from scratch, using the elements in 'ns':
124 for (int i = 0; i < heap.size(); i++)
125 indices[heap[i]] = -1;
126 heap.clear();
127
128 for (int i = 0; i < ns.size(); i++){
129 indices[ns[i]] = i;
130 heap.push(ns[i]); }
131
132 for (int i = heap.size() / 2 - 1; i >= 0; i--)
133 percolateDown(i);
134 }
135
136 void clear(bool dealloc = false)
137 {
138 for (int i = 0; i < heap.size(); i++)
139 indices[heap[i]] = -1;
141 }
142};
143
144
145//=================================================================================================
146} // namespace Internal
147} // namespace Minisat
void update(int n)
Definition Heap.h:89
void insert(int n)
Definition Heap.h:99
bool inHeap(int n) const
Definition Heap.h:80
int size() const
Definition Heap.h:78
vec< int > indices
Definition Heap.h:36
static int right(int i)
Definition Heap.h:40
static int parent(int i)
Definition Heap.h:41
Heap(const Comp &c)
Definition Heap.h:76
void increase(int n)
Definition Heap.h:85
bool empty() const
Definition Heap.h:79
void percolateDown(int i)
Definition Heap.h:60
int operator[](int index) const
Definition Heap.h:81
void clear(bool dealloc=false)
Definition Heap.h:136
void percolateUp(int i)
Definition Heap.h:44
void build(vec< int > &ns)
Definition Heap.h:123
static int left(int i)
Definition Heap.h:39
void decrease(int n)
Definition Heap.h:84
vec< int > heap
Definition Heap.h:35
int size(void) const
Definition Vec.h:63
void pop(void)
Definition Vec.h:76
void clear(bool dealloc=false)
Definition Vec.h:121
void push(void)
Definition Vec.h:73
void growTo(int size)
Definition Vec.h:113
const T & last(void) const
Definition Vec.h:82
static MultilevelBuilder * getDoubleFactoredZeroAdjustedMerger()