CVC3
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