From c50defc21c956a5cb24db7281aa85448af2986fc Mon Sep 17 00:00:00 2001 From: loboho Date: Tue, 17 Sep 2019 18:09:43 +0800 Subject: [PATCH 1/3] optimize percolate_up --- astar/astar.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/astar/astar.h b/astar/astar.h index e17f61e..6877997 100644 --- a/astar/astar.h +++ b/astar/astar.h @@ -150,7 +150,7 @@ class AStar /** * 二叉堆上滤 */ - void percolate_up(size_t hole); + void percolate_up(size_t hole, Node *node); /** * 获取节点索引 From f77805973e3529055f6fd5227294c53b7b3dbc9e Mon Sep 17 00:00:00 2001 From: loboho Date: Tue, 17 Sep 2019 18:13:03 +0800 Subject: [PATCH 2/3] optimize percolate_up --- astar/astar.cpp | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/astar/astar.cpp b/astar/astar.cpp index c7c1678..38f17f9 100644 --- a/astar/astar.cpp +++ b/astar/astar.cpp @@ -102,7 +102,7 @@ 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) @@ -110,14 +110,15 @@ void AStar::percolate_up(size_t hole) parent = (hole - 1) / 2; if (open_list_[hole]->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值 @@ -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 { From 58aadc5e671970861bc7ffd2885b899f0d74eeb9 Mon Sep 17 00:00:00 2001 From: loboho Date: Tue, 17 Sep 2019 18:32:42 +0800 Subject: [PATCH 3/3] optimize percolate_up --- astar/astar.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/astar/astar.cpp b/astar/astar.cpp index 38f17f9..839d58a 100644 --- a/astar/astar.cpp +++ b/astar/astar.cpp @@ -108,7 +108,7 @@ void AStar::percolate_up(size_t hole, Node *node) while (hole > 0) { parent = (hole - 1) / 2; - if (open_list_[hole]->f() < open_list_[parent]->f()) + if (node->f() < open_list_[parent]->f()) { open_list_[hole] = open_list_[parent]; hole = parent;