Skip to content

Commit

Permalink
fix bug
Browse files Browse the repository at this point in the history
  • Loading branch information
lucaperju committed Aug 9, 2024
1 parent 67b0e0e commit 0f76307
Showing 1 changed file with 13 additions and 8 deletions.
21 changes: 13 additions & 8 deletions include/random_walks/uniform_accelerated_billiard_walk.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
#include <set>
#include <vector>

const double eps = 0.000000001;

template<typename NT>
class Heap {
public:
Expand All @@ -24,29 +26,31 @@ class Heap {
std::vector<std::pair<NT, int>> vec;

private:
void siftDown(int index) {
int siftDown(int index) {
while((index << 1) + 1 < heap_size) {
int child = (index << 1) + 1;
if(child + 1 < heap_size && heap[child + 1].first < heap[child].first) {
if(child + 1 < heap_size && heap[child + 1].first < heap[child].first - eps) {
child += 1;
}
if(heap[child].first < heap[index].first)
if(heap[child].first < heap[index].first - eps)
{
std::swap(heap[child], heap[index]);
std::swap(vec[heap[child].second].second, vec[heap[index].second].second);
index = child;
} else {
return;
return index;
}
}
return index;
}

void siftUp(int index) {
while(index > 0 && heap[(index - 1) >> 1].first > heap[index].first) {
int siftUp(int index) {
while(index > 0 && heap[(index - 1) >> 1].first - eps > heap[index].first) {
std::swap(heap[(index - 1) >> 1], heap[index]);
std::swap(vec[heap[(index - 1) >> 1].second].second, vec[heap[index].second].second);
index = (index - 1) >> 1;
}
return index;
}

public:
Expand Down Expand Up @@ -79,15 +83,16 @@ class Heap {
return heap[0];
}

void remove (const int index) { // takes the index from the heap
void remove (int index) { // takes the index from the heap
if(index == -1) {
return;
}
std::swap(heap[heap_size - 1], heap[index]);
std::swap(vec[heap[heap_size - 1].second].second, vec[heap[index].second].second);
vec[heap[heap_size - 1].second].second = -1;
heap_size -= 1;
siftDown(index);
index = siftDown(index);
siftUp(index);
}

void insert (const std::pair<NT, int> val) {
Expand Down

0 comments on commit 0f76307

Please sign in to comment.