From 7fa7b3bc844d61ecc986f77819a11d1e4a526c57 Mon Sep 17 00:00:00 2001 From: Stanislaw Halik Date: Sun, 4 Feb 2024 14:32:32 +0100 Subject: b --- src/dijkstra.cpp | 27 ++++++++++++++------------- src/path-search-result.cpp | 4 ++-- src/path-search-result.hpp | 8 ++++---- src/path-search.hpp | 5 ++--- 4 files changed, 22 insertions(+), 22 deletions(-) (limited to 'src') diff --git a/src/dijkstra.cpp b/src/dijkstra.cpp index 765033e0..9081d5a7 100644 --- a/src/dijkstra.cpp +++ b/src/dijkstra.cpp @@ -5,7 +5,8 @@ #include "object.hpp" #include "point.hpp" #include -#include +#include +#include // todo remove #include #include #include @@ -79,8 +80,8 @@ constexpr auto directions = []() constexpr struct heap_comparator { - const std::vector& nodes; // NOLINT - inline heap_comparator(const std::vector& nodes) : nodes{nodes} {} + const Array& nodes; // NOLINT + inline heap_comparator(const Array& nodes) : nodes{nodes} {} inline bool operator()(uint32_t a, uint32_t b) const { return nodes[b].dist < nodes[a].dist; } }; @@ -100,7 +101,7 @@ inline uint32_t distance_l2(point a, point b) return (uint32_t)Math::abs(dist).sum(); } -void set_result_from_idx(path_search_result& result, const std::vector& nodes, +void set_result_from_idx(path_search_result& result, const Array& nodes, point to, const uint32_t idx) { fm_debug_assert(idx != (uint32_t)-1); @@ -132,19 +133,19 @@ astar::astar() void astar::reserve(size_t capacity) { - nodes.reserve(capacity); - Q.reserve(capacity); + arrayReserve(nodes, capacity); + arrayReserve(Q, capacity); } void astar::clear() { - nodes.clear(); - Q.clear(); + arrayResize(nodes, 0); + arrayResize(Q, 0); } void astar::add_to_heap(uint32_t id) { - Q.push_back(id); + arrayAppend(Q, id); Heap::push_heap(Q.begin(), Q.end(), heap_comparator{nodes}); } @@ -152,7 +153,7 @@ uint32_t astar::pop_from_heap() { Heap::pop_heap(Q.begin(), Q.end(), heap_comparator{nodes}); const auto id = Q.back(); - Q.pop_back(); + arrayRemoveSuffix(Q); return id; } @@ -200,7 +201,7 @@ path_search_result astar::Dijkstra(world& w, const point from, const point to, { auto idx = (uint32_t)nodes.size(); cache.add_index(pt, idx); - nodes.push_back({.dist = dist, .prev = (uint32_t)-1, .pt = pt, }); + arrayAppend(nodes, {.dist = dist, .prev = (uint32_t)-1, .pt = pt, }); add_to_heap(idx); } } @@ -209,7 +210,7 @@ path_search_result astar::Dijkstra(world& w, const point from, const point to, uint32_t closest_idx = (uint32_t)-1; auto goal_idx = (uint32_t)-1; - while (!Q.empty()) + while (!Q.isEmpty()) { const auto cur_idx = pop_from_heap(); point cur_pt; @@ -277,7 +278,7 @@ path_search_result astar::Dijkstra(world& w, const point from, const point to, .dist = dist, .prev = cur_idx, .pt = new_pt, }; - nodes.push_back(new_node); + arrayAppend(nodes, new_node); } else { diff --git a/src/path-search-result.cpp b/src/path-search-result.cpp index 50e9173a..bc0ad31f 100644 --- a/src/path-search-result.cpp +++ b/src/path-search-result.cpp @@ -13,7 +13,7 @@ constexpr size_t min_length = TILE_MAX_DIM*2; } // namespace -std::unique_ptr path_search_result::_pool; // NOLINT +Pointer path_search_result::_pool; // NOLINT path_search_result::path_search_result() { @@ -27,7 +27,7 @@ path_search_result::path_search_result() } else { - _node = std::make_unique(); + _node = Pointer{InPlaceInit}; _node->vec.reserve(min_length); } } diff --git a/src/path-search-result.hpp b/src/path-search-result.hpp index 89e240b8..fc02e1ba 100644 --- a/src/path-search-result.hpp +++ b/src/path-search-result.hpp @@ -1,8 +1,8 @@ #pragma once #include "src/global-coords.hpp" #include "compat/defs.hpp" -#include #include +#include namespace floormat { @@ -48,12 +48,12 @@ private: std::vector vec; private: - std::unique_ptr _next; + Pointer _next; }; - static std::unique_ptr _pool; // NOLINT(*-avoid-non-const-global-variables) + static Pointer _pool; // NOLINT(*-avoid-non-const-global-variables) - std::unique_ptr _node; + Pointer _node; float _time = 0; uint32_t _cost = 0, _distance = (uint32_t)-1; bool _found : 1 = false; diff --git a/src/path-search.hpp b/src/path-search.hpp index 5617765f..36c25497 100644 --- a/src/path-search.hpp +++ b/src/path-search.hpp @@ -9,7 +9,6 @@ #include "point.hpp" #include #include -#include #include #include #include @@ -124,8 +123,8 @@ private: uint32_t pop_from_heap(); struct detail_astar::cache cache; - std::vector nodes; - std::vector Q; + Array nodes; + Array Q; }; } // namespace floormat -- cgit v1.2.3