CVC3
2.4.1
Main Page
Related Pages
Modules
Namespaces
Classes
Files
File List
File Members
src
sat
minisat_heap.h
Go to the documentation of this file.
1
/*****************************************************************************/
2
/*!
3
*\file minisat_heap.h
4
*\brief MiniSat internal heap implementation
5
*
6
* Author: Alexander Fuchs
7
*
8
* Created: Fri Sep 08 11:04:00 2006
9
*
10
* <hr>
11
*
12
* License to use, copy, modify, sell and/or distribute this software
13
* and its documentation for any purpose is hereby granted without
14
* royalty, subject to the terms and conditions defined in the \ref
15
* LICENSE file provided with this distribution.
16
*
17
* <hr>
18
*/
19
/*****************************************************************************/
20
21
/******************************************************************************************[Heap.h]
22
MiniSat -- Copyright (c) 2003-2005, Niklas Een, Niklas Sorensson
23
24
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and
25
associated documentation files (the "Software"), to deal in the Software without restriction,
26
including without limitation the rights to use, copy, modify, merge, publish, distribute,
27
sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
28
furnished to do so, subject to the following conditions:
29
30
The above copyright notice and this permission notice shall be included in all copies or
31
substantial portions of the Software.
32
33
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT
34
NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
35
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
36
DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT
37
OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
38
**************************************************************************************************/
39
40
#ifndef _cvc3__minisat__heap_h_
41
#define _cvc3__minisat__heap_h_
42
43
//=================================================================================================
44
45
#include "
debug.h
"
46
#include <iostream>
47
48
// provides a heap over ints
49
50
namespace
MiniSat {
51
52
53
static
inline
int
left
(
int
i) {
return
i+i; }
54
static
inline
int
right
(
int
i) {
return
i+i + 1; }
55
static
inline
int
parent
(
int
i) {
return
i >> 1; }
56
57
template
<
class
C>
58
class
Heap
{
59
public
:
60
C
comp
;
61
vec<int>
heap
;
// heap of ints
62
vec<int>
indices
;
// int -> index in heap
63
64
inline
void
percolateUp
(
int
i)
65
{
66
int
x =
heap
[i];
67
while
(
parent
(i) != 0 &&
comp
(x,
heap
[
parent
(i)])){
68
heap
[i] =
heap
[
parent
(i)];
69
indices
[
heap
[i]] = i;
70
i =
parent
(i);
71
}
72
heap
[i] = x;
73
indices
[x] = i;
74
}
75
76
inline
void
percolateDown
(
int
i)
77
{
78
int
x =
heap
[i];
79
while
(
left
(i) <
heap
.
size
()){
80
int
child =
right
(i) <
heap
.
size
() &&
comp
(
heap
[
right
(i)],
heap
[
left
(i)]) ?
right
(i) :
left
(i);
81
if
(!
comp
(
heap
[child],x))
break
;
82
heap
[i] =
heap
[child];
83
indices
[
heap
[i]] = i;
84
i = child;
85
}
86
heap
[i] = x;
87
indices
[x] = i;
88
}
89
90
bool
ok
(
int
n) {
return
n >= 0 && n < (int)
indices
.
size
(); }
91
92
public
:
93
Heap
(C c) :
comp
(c) {
heap
.
push
(-1); }
94
95
void
setBounds
(
int
size) {
DebugAssert
(size >= 0,
"MiniSat::Heap::setBounds"
);
indices
.
growTo
(size,0); }
96
bool
inHeap
(
int
n) {
DebugAssert
(
ok
(n),
"MiniSat::Heap::inHeap"
);
return
indices
[n] != 0; }
97
98
void
increase
(
int
n){
99
DebugAssert
(
ok
(n),
"MiniSat::Heap::increase: ok"
);
100
DebugAssert
(
inHeap
(n),
"MiniSat::Heap::increase: inHeap"
);
101
percolateUp
(
indices
[n]);
102
}
103
104
bool
empty
() {
return
heap
.
size
() == 1; }
105
106
void
insert
(
int
n) {
107
DebugAssert
(n > 0,
"MiniSat::Heap::insert: inserting invalid var id"
);
108
if
(!
inHeap
(n)) {
109
DebugAssert
(
ok
(n),
"MiniSat::Heap::insert: ok"
);
110
indices
[n] =
heap
.
size
();
111
heap
.
push
(n);
112
percolateUp
(
indices
[n]);
113
}
114
}
115
116
int
getMin
() {
117
int
r =
heap
[1];
118
DebugAssert
(r > 0,
"MiniSatHeap:getmin: invalid var id"
);
119
heap
[1] =
heap
.
last
();
120
indices
[
heap
[1]] = 1;
121
indices
[r] = 0;
122
heap.
pop
();
123
if
(heap.size() > 1)
124
percolateDown
(1);
125
return
r; }
126
127
bool
heapProperty
() {
128
return
heapProperty
(1); }
129
130
bool
heapProperty
(
int
i) {
131
return
(
size_t
)i >=
heap
.
size
()
132
|| ((
parent
(i) == 0 || !
comp
(
heap
[i],
heap
[
parent
(i)])) &&
heapProperty
(
left
(i)) &&
heapProperty
(
right
(i))); }
133
};
134
135
}
136
137
//=================================================================================================
138
#endif
MiniSat::Heap::increase
void increase(int n)
Definition:
minisat_heap.h:98
MiniSat::Heap::ok
bool ok(int n)
Definition:
minisat_heap.h:90
debug.h
Description: Collection of debugging macros and functions.
MiniSat::Heap::insert
void insert(int n)
Definition:
minisat_heap.h:106
MiniSat::vec< int >
MiniSat::vec::pop
void pop(void)
Definition:
minisat_global.h:137
DebugAssert
#define DebugAssert(cond, str)
Definition:
debug.h:408
MiniSat::Heap::indices
vec< int > indices
Definition:
minisat_heap.h:62
MiniSat::Heap::percolateUp
void percolateUp(int i)
Definition:
minisat_heap.h:64
MiniSat::Heap::getMin
int getMin()
Definition:
minisat_heap.h:116
MiniSat::vec::push
void push(void)
Definition:
minisat_global.h:144
MiniSat::Heap::heapProperty
bool heapProperty()
Definition:
minisat_heap.h:127
MiniSat::Heap::setBounds
void setBounds(int size)
Definition:
minisat_heap.h:95
MiniSat::left
static int left(int i)
Definition:
minisat_heap.h:53
MiniSat::Heap::inHeap
bool inHeap(int n)
Definition:
minisat_heap.h:96
MiniSat::Heap::empty
bool empty()
Definition:
minisat_heap.h:104
MiniSat::Heap::comp
C comp
Definition:
minisat_heap.h:60
MiniSat::parent
static int parent(int i)
Definition:
minisat_heap.h:55
MiniSat::vec::growTo
void growTo(int size)
Definition:
minisat_global.h:177
MiniSat::vec::size
int size(void) const
Definition:
minisat_global.h:134
MiniSat::Heap::percolateDown
void percolateDown(int i)
Definition:
minisat_heap.h:76
MiniSat::right
static int right(int i)
Definition:
minisat_heap.h:54
MiniSat::Heap::heapProperty
bool heapProperty(int i)
Definition:
minisat_heap.h:130
MiniSat::Heap::Heap
Heap(C c)
Definition:
minisat_heap.h:93
MiniSat::Heap::heap
vec< int > heap
Definition:
minisat_heap.h:61
MiniSat::vec::last
const T & last(void) const
Definition:
minisat_global.h:146
MiniSat::Heap
Definition:
minisat_heap.h:58
Generated on Wed Aug 27 2014 16:11:36 for CVC3 by
1.8.7