Skip to content

Commit

Permalink
Merge pull request LearnProgramming#15 from jgulotta/fix-ordering-bug
Browse files Browse the repository at this point in the history
Fix ordering bug and cleanup HeapSet implementation
  • Loading branch information
jgulotta committed Dec 21, 2012
2 parents 886bac4 + 37d3d88 commit 277beed
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 31 deletions.
2 changes: 1 addition & 1 deletion examples/n-puzzle/input/goal
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
1 2 3 0
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 0
1 2 3 4 5 6 7 8 0
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 0
2 changes: 1 addition & 1 deletion examples/n-puzzle/input/start
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
0 1 3 2
4 3 5 1 0 8 2 6 7
1 2 3 4 5 6 7 8 9 10 11 12 13 14 0 15
12 1 10 2 7 0 4 14 5 11 9 15 8 13 6 3
4 3 5 1 0 8 2 6 7
50 changes: 22 additions & 28 deletions include/astar/heapset.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,10 @@ class HeapSet {
private:
std::set<T,SC> states_;
std::priority_queue<T,std::vector<T>,VC> heap_;
// returns true if second arg is higher priority than the first
VC higher_priority_;
T pop_heap();
void update_heap(const T& old, const T& updated);
};

template<class T, class SC, class VC>
Expand Down Expand Up @@ -46,20 +48,8 @@ void HeapSet<T,SC,VC>::push(const T& t) {
heap_.push(t);
} else {
auto old = *it_inserted.first;
if (higher_priority_(t, old)) {
std::deque<T> queue;

do {
queue.push_back(pop_heap());
} while (queue.back() != old);

queue.back() = t;

while (!queue.empty()) {
heap_.push(queue.back());
queue.pop_back();
}

if (higher_priority_(old, t)) {
update_heap(old, t);
auto it = states_.erase(it_inserted.first);
states_.insert(it, t);
}
Expand All @@ -74,20 +64,8 @@ void HeapSet<T,SC,VC>::push(T&& t) {
heap_.push(std::forward<T>(t));
} else {
auto old = *it_inserted.first;
if (higher_priority_(t, old)) {
std::deque<T> queue;

do {
queue.push_back(pop_heap());
} while (queue.back() != old);

queue.back() = t;

while (!queue.empty()) {
heap_.push(queue.back());
queue.pop_back();
}

if (higher_priority_(old, t)) {
update_heap(old, t);
auto it = states_.erase(it_inserted.first);
states_.insert(it, std::forward<T>(t));
}
Expand All @@ -100,3 +78,19 @@ T HeapSet<T,SC,VC>::pop_heap() {
heap_.pop();
return std::move(t);
}

template<class T, class SC, class VC>
void HeapSet<T,SC,VC>::update_heap(const T& old, const T& updated) {
std::deque<T> queue;

do {
queue.push_back(pop_heap());
} while (queue.back() != old);

queue.back() = updated;

while (!queue.empty()) {
heap_.push(queue.back());
queue.pop_back();
}
}
2 changes: 1 addition & 1 deletion include/astar/solver.h
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ bool AStarSolver<T,G,H>::solve()
for (auto& n : generator_func_(snode->state_)) {
auto new_node = make_snode(*this, n, snode);
if (closed_set_.find(new_node) == end(closed_set_)) {
open_set_.push(new_node);
open_set_.push(std::move(new_node));
}
}
}
Expand Down

0 comments on commit 277beed

Please sign in to comment.