| 1 | //heap.cpp |
| 2 | |
| 3 | ////////////////////////////////////////////////////////////////////////////// |
| 4 | // The heap index (return by the push() method) is a map from the grid // |
| 5 | // address to the index of heap's internal data representation. // |
| 6 | // // |
| 7 | // c_ is the current size of the heap. // |
| 8 | // depth_ is the final size of the heap. // |
| 9 | // // |
| 10 | // distance_[] is the unsigned distance from the zero level set to each // |
| 11 | // element in the heap. // |
| 12 | // // |
| 13 | // address_[] is the (original) grid address of each element in the heap. // |
| 14 | // // |
| 15 | // backPointer_[] is a map from the index of distance_ (or address_) to the // |
| 16 | // current location of the element in the heap. // |
| 17 | // // |
| 18 | // heap_ is an array of integer indices into the distance_ (or address_) // |
| 19 | // array the heap invariant is maintained by moving elements in this list. // |
| 20 | // // |
| 21 | // if selfTest_ is true a consistency check is done after each operation. // |
| 22 | ////////////////////////////////////////////////////////////////////////////// |
| 23 | |
| 24 | ////////////////////////////////////////////////////////////////////////// |
| 25 | // Currently, the heap constructor needs to know the number of elements // |
| 26 | // that will enter the heap during the calculation. // |
| 27 | ////////////////////////////////////////////////////////////////////////// |
| 28 | |
| 29 | #include "heap.h" |
| 30 | #include <stdio.h> |
| 31 | #include <limits> |
| 32 | #include <stdexcept> |
| 33 | |
| 34 | heap::heap(int maxLength, bool selfTest) |
| 35 | { |
| 36 | maxLength_ = maxLength; |
| 37 | heapLength_ = 0; |
| 38 | listLength_ = 0; |
| 39 | selfTest_ = selfTest; |
| 40 | distance_ = new double [maxLength_]; |
| 41 | backPointer_ = new int [maxLength_]; |
| 42 | heap_ = new int [maxLength_]; |
| 43 | address_ = new int [maxLength_]; |
| 44 | } |
| 45 | |
| 46 | heap::~heap() |
| 47 | { |
| 48 | delete[] distance_; |
| 49 | delete[] backPointer_; |
| 50 | delete[] heap_; |
| 51 | delete[] address_; |
| 52 | } |
| 53 | |
| 54 | int heap::push(int address, double value) |
| 55 | { |
| 56 | if (heapLength_==maxLength_) |
| 57 | throw std::runtime_error("heap push error: heap full\n"); |
| 58 | heap_[heapLength_] = listLength_; |
| 59 | address_[listLength_] = address; |
| 60 | distance_[listLength_] = value; |
| 61 | backPointer_[listLength_] = heapLength_; |
| 62 | listLength_++; |
| 63 | heapLength_++; |
| 64 | _siftDown(0,heapLength_-1); |
| 65 | if (selfTest_) test(); |
| 66 | return listLength_-1; |
| 67 | } |
| 68 | |
| 69 | const double &heap::peek() const |
| 70 | { |
| 71 | if (heapLength_==0) |
| 72 | throw std::runtime_error("heap peek error: empty heap\n"); |
| 73 | int loc=heap_[0]; |
| 74 | return distance_[loc]; |
| 75 | } |
| 76 | |
| 77 | void heap::pop(int *address, double *value) |
| 78 | { |
| 79 | // out: address to element with min value and value |
| 80 | if (heapLength_==0) |
| 81 | throw std::runtime_error("heap pop error: empty heap\n"); |
| 82 | int loc = heap_[0]; |
| 83 | *value = distance_[loc]; |
| 84 | *address = address_[heap_[0]]; |
| 85 | heap_[0] = heap_[heapLength_-1]; |
| 86 | backPointer_[heap_[0]] = 0; |
| 87 | heapLength_--; |
| 88 | _siftUp(0); |
| 89 | if (selfTest_) test(); |
| 90 | } |
| 91 | |
| 92 | void heap::_siftDown(int startPos, int pos) |
| 93 | { |
| 94 | int newItem; |
| 95 | int parent; |
| 96 | int parentPos; |
| 97 | newItem = heap_[pos]; |
| 98 | while (pos > startPos) |
| 99 | { |
| 100 | parentPos = (pos-1)>>1; |
| 101 | parent = heap_[parentPos]; |
| 102 | if (distance_[newItem] < distance_[parent]) |
| 103 | { |
| 104 | heap_[pos]=parent; |
| 105 | backPointer_[parent]=pos; |
| 106 | pos=parentPos; |
| 107 | continue; |
| 108 | } |
| 109 | break; |
| 110 | } |
| 111 | heap_[pos]=newItem; |
| 112 | backPointer_[newItem]=pos; |
| 113 | } |
| 114 | |
| 115 | void heap::_siftUp(int pos) |
| 116 | { |
| 117 | int endPos = heapLength_; |
| 118 | int startPos = pos; |
| 119 | int newItem; |
| 120 | int rightPos; |
| 121 | newItem = heap_[pos]; |
| 122 | int childPos = 2*pos + 1; |
| 123 | while (childPos < endPos) |
| 124 | { |
| 125 | rightPos = childPos + 1; |
| 126 | if ((rightPos < endPos) && ! |
| 127 | (distance_[heap_[childPos]] < distance_[heap_[rightPos]])) |
| 128 | { |
| 129 | childPos = rightPos; |
| 130 | } |
| 131 | heap_[pos]=heap_[childPos]; |
| 132 | backPointer_[heap_[childPos]]=pos; |
| 133 | pos = childPos; |
| 134 | childPos = 2*pos + 1; |
| 135 | } |
| 136 | heap_[pos] = newItem; |
| 137 | _siftDown(startPos, pos); |
| 138 | } |
| 139 | |
| 140 | |
| 141 | void heap::set(int index, double newDistance) |
| 142 | { |
| 143 | double oldDistance = distance_[index]; |
| 144 | int pos = backPointer_[index]; |
| 145 | distance_[index]=newDistance; |
| 146 | if (newDistance > oldDistance) |
| 147 | { |
| 148 | _siftUp(pos); |
| 149 | } |
| 150 | if (distance_[heap_[pos]] != newDistance) |
| 151 | { |
| 152 | if (selfTest_) test(); |
| 153 | return; |
| 154 | } |
| 155 | _siftDown(0,pos); |
| 156 | if (selfTest_) test(); |
| 157 | } |
| 158 | |
| 159 | bool heap::empty() const |
| 160 | { |
| 161 | if (heapLength_==0) return true; |
| 162 | return false; |
| 163 | } |
| 164 | |
| 165 | void heap::test() const |
| 166 | { |
| 167 | for (int i=0; i<heapLength_; i++) |
| 168 | { |
| 169 | int c[2]; |
| 170 | c[0]=2*i+1; |
| 171 | c[1]=c[0]+1; |
| 172 | for (int j=0; j<2; j++) |
| 173 | { |
| 174 | if (c[j] < heapLength_-1) |
| 175 | { |
| 176 | double dp = distance_[heap_[i]]; |
| 177 | double dc = distance_[heap_[c[j]]]; |
| 178 | if (! (dp<=dc)) |
| 179 | { |
| 180 | throw std::runtime_error("heap invariant violation"); |
| 181 | } |
| 182 | } |
| 183 | } |
| 184 | } |
| 185 | for (int i=0; i<heapLength_; i++) |
| 186 | { |
| 187 | if (! (backPointer_[heap_[i]]==i)) |
| 188 | { |
| 189 | printf("error %i\n",i); |
| 190 | throw std::runtime_error("heap backpointer inconsistancy"); |
| 191 | } |
| 192 | } |
| 193 | } |
| 194 | |
| 195 | // void heap::print() const |
| 196 | // { |
| 197 | // for (int i=0; i<heapLength_; i++) |
| 198 | // { |
| 199 | // printf("%i: %i ", i, heap_[i]); |
| 200 | // printf("(%i)", address_[heap_[i]]); |
| 201 | // printf(" %lf", distance_[heap_[i]]); |
| 202 | // printf(" [%i]\n", backPointer_[heap_[i]]); |
| 203 | // } |
| 204 | // printf("\n"); |
| 205 | // } |