Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

optimize percolate_up #11

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 6 additions & 5 deletions astar/astar.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -102,22 +102,23 @@ bool AStar::get_node_index(Node *node, size_t *index)
}

// 二叉堆上滤
void AStar::percolate_up(size_t hole)
void AStar::percolate_up(size_t hole, Node *node)
{
size_t parent = 0;
while (hole > 0)
{
parent = (hole - 1) / 2;
if (open_list_[hole]->f() < open_list_[parent]->f())
if (node->f() < open_list_[parent]->f())
{
std::swap(open_list_[hole], open_list_[parent]);
open_list_[hole] = open_list_[parent];
hole = parent;
}
else
{
return;
break;
}
}
open_list_[hole] = node;
}

// 计算G值
Expand Down Expand Up @@ -224,7 +225,7 @@ void AStar::handle_found_node(Node *current, Node *destination)
size_t index = 0;
if (get_node_index(destination, &index))
{
percolate_up(index);
percolate_up(index, destination);
}
else
{
Expand Down
2 changes: 1 addition & 1 deletion astar/astar.h
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ class AStar
/**
* 二叉堆上滤
*/
void percolate_up(size_t hole);
void percolate_up(size_t hole, Node *node);

/**
* 获取节点索引
Expand Down