diff options
author | Stanislaw Halik <sthalik@misaki.pl> | 2024-02-04 14:32:32 +0100 |
---|---|---|
committer | Stanislaw Halik <sthalik@misaki.pl> | 2024-02-04 14:32:32 +0100 |
commit | 7fa7b3bc844d61ecc986f77819a11d1e4a526c57 (patch) | |
tree | 872ba871b481b5b5d397ffc20f2dca0c3812024e /src | |
parent | a85eb5f8f15cf8712246c3d765708b636603ca14 (diff) |
b
Diffstat (limited to 'src')
-rw-r--r-- | src/dijkstra.cpp | 27 | ||||
-rw-r--r-- | src/path-search-result.cpp | 4 | ||||
-rw-r--r-- | src/path-search-result.hpp | 8 | ||||
-rw-r--r-- | src/path-search.hpp | 5 |
4 files changed, 22 insertions, 22 deletions
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 <cstdio> -#include <Corrade/Containers/StaticArray.h> +#include <Corrade/Containers/GrowableArray.h> +#include <Corrade/Containers/StaticArray.h> // todo remove #include <Magnum/Math/Vector2.h> #include <Magnum/Math/Functions.h> #include <Magnum/Timeline.h> @@ -79,8 +80,8 @@ constexpr auto directions = []() constexpr struct heap_comparator { - const std::vector<visited>& nodes; // NOLINT - inline heap_comparator(const std::vector<visited>& nodes) : nodes{nodes} {} + const Array<visited>& nodes; // NOLINT + inline heap_comparator(const Array<visited>& 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<visited>& nodes, +void set_result_from_idx(path_search_result& result, const Array<visited>& 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::node> path_search_result::_pool; // NOLINT +Pointer<path_search_result::node> 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>(); + _node = Pointer<node>{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 <memory> #include <vector> +#include <Corrade/Containers/Pointer.h> namespace floormat { @@ -48,12 +48,12 @@ private: std::vector<point> vec; private: - std::unique_ptr<node> _next; + Pointer<node> _next; }; - static std::unique_ptr<node> _pool; // NOLINT(*-avoid-non-const-global-variables) + static Pointer<node> _pool; // NOLINT(*-avoid-non-const-global-variables) - std::unique_ptr<node> _node; + Pointer<node> _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 <bit> #include <array> -#include <vector> #include <bitset> #include <Corrade/Containers/Array.h> #include <Magnum/Math/Vector2.h> @@ -124,8 +123,8 @@ private: uint32_t pop_from_heap(); struct detail_astar::cache cache; - std::vector<visited> nodes; - std::vector<uint32_t> Q; + Array<visited> nodes; + Array<uint32_t> Q; }; } // namespace floormat |