From d3d7a0013f72bdb7466f3020ed2bd17302991ccf Mon Sep 17 00:00:00 2001 From: Katja Date: Wed, 5 Jun 2024 04:12:16 +0200 Subject: [PATCH] Optimized MultilevelAStarEx::generate_path() --- src/MultilevelAStarEx.cpp | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/src/MultilevelAStarEx.cpp b/src/MultilevelAStarEx.cpp index 74fc974..61a267a 100644 --- a/src/MultilevelAStarEx.cpp +++ b/src/MultilevelAStarEx.cpp @@ -222,11 +222,19 @@ bool MultilevelAStarEx::can_move(const Node *current, int x, int y) const TypedArray MultilevelAStarEx::generate_path(const Node *current) const { - TypedArray arr; + // count the number of nodes in the path + int cnt = 0; + for (const Node *n = current; n != nullptr; n = n->parent) cnt++; + cnt--; // because we're not adding the starting node + + TypedArray arr; + arr.resize(cnt); + + int i = cnt; while (current->parent != nullptr) { - arr.insert(0, Vector2i(current->x, current->y) + _trans); + arr[--cnt] = Vector2i(current->x, current->y) + _trans; current = current->parent; }