diff options
author | Stanislaw Halik <sthalik@misaki.pl> | 2024-02-04 15:18:32 +0100 |
---|---|---|
committer | Stanislaw Halik <sthalik@misaki.pl> | 2024-02-04 15:18:32 +0100 |
commit | a65aaeb43df61a9127aed0c4eb8dd4cb0b3b0bd6 (patch) | |
tree | 020f69f766545fa6fc960b3fdfcb4a7cda20313d | |
parent | 7fa7b3bc844d61ecc986f77819a11d1e4a526c57 (diff) |
b
-rw-r--r-- | compat/vector-wrapper.hpp | 15 | ||||
-rw-r--r-- | editor/tests/path-test.cpp | 24 | ||||
-rw-r--r-- | src/dijkstra.cpp | 3 | ||||
-rw-r--r-- | src/path-search-node.hpp | 24 | ||||
-rw-r--r-- | src/path-search-result.cpp | 13 | ||||
-rw-r--r-- | src/path-search-result.hpp | 26 | ||||
-rw-r--r-- | test/dijkstra.cpp | 4 | ||||
-rw-r--r-- | test/path-search-result.cpp | 1 |
8 files changed, 71 insertions, 39 deletions
diff --git a/compat/vector-wrapper.hpp b/compat/vector-wrapper.hpp new file mode 100644 index 00000000..1b6f0617 --- /dev/null +++ b/compat/vector-wrapper.hpp @@ -0,0 +1,15 @@ +#pragma once +#include <vector> + +namespace floormat { + +template<typename T> +struct vector_wrapper final +{ + using vector_type = std::conditional_t<std::is_const_v<T>, + const std::vector<std::remove_const_t<T>>, + std::vector<T>>; + vector_type& vec; +}; + +} // namespace floormat diff --git a/editor/tests/path-test.cpp b/editor/tests/path-test.cpp index 43e2b041..672c7aa3 100644 --- a/editor/tests/path-test.cpp +++ b/editor/tests/path-test.cpp @@ -1,6 +1,7 @@ #include "../tests-private.hpp" #include "../app.hpp" #include "compat/shared-ptr-wrapper.hpp" +#include "compat/vector-wrapper.hpp" #include "floormat/main.hpp" #include "src/path-search.hpp" #include "src/critter.hpp" @@ -137,19 +138,16 @@ void path_test::update_pre(app& a) auto& astar = M.astar(); auto res = astar.Dijkstra(w, pending.from, pending.to, pending.own_id, pending.max_dist, pending.own_size, 1); - if (res) - { - has_result = true; - result = { - .from = pending.from, - .to = pending.to, - .path = std::move(res.path()), - .time = res.time(), - .cost = res.cost(), - .distance = res.distance(), - .found = res.is_found(), - }; - } + has_result = !!res; + result = { + .from = pending.from, + .to = pending.to, + .path = std::move(res.raw_path().vec), + .time = res.time(), + .cost = res.cost(), + .distance = res.distance(), + .found = res.is_found(), + }; } void path_test::update_post(app& a) diff --git a/src/dijkstra.cpp b/src/dijkstra.cpp index 9081d5a7..c9433132 100644 --- a/src/dijkstra.cpp +++ b/src/dijkstra.cpp @@ -1,6 +1,7 @@ #include "path-search.hpp" #include "compat/format.hpp" #include "compat/debug.hpp" +#include "compat/vector-wrapper.hpp" #include "compat/heap.hpp" #include "object.hpp" #include "point.hpp" @@ -106,7 +107,7 @@ void set_result_from_idx(path_search_result& result, const Array<visited>& nodes { fm_debug_assert(idx != (uint32_t)-1); - auto& path = result.path(); + auto& path = result.raw_path().vec; path.clear(); const auto& to_node = nodes[idx]; diff --git a/src/path-search-node.hpp b/src/path-search-node.hpp new file mode 100644 index 00000000..ecad6a77 --- /dev/null +++ b/src/path-search-node.hpp @@ -0,0 +1,24 @@ +#pragma once +#include "compat/defs.hpp" +#include "path-search-result.hpp" +#include <vector> +#include <Corrade/Containers/Pointer.h> + +namespace floormat { + +struct path_search_result::node +{ + friend struct path_search_result; + friend struct test_app; + + node() noexcept; + fm_DECLARE_DELETED_COPY_ASSIGNMENT(node); + fm_DECLARE_DEFAULT_MOVE_ASSIGNMENT_(node); + + std::vector<point> vec; + +private: + Pointer<node> _next; +}; + +} // namespace floormat diff --git a/src/path-search-result.cpp b/src/path-search-result.cpp index bc0ad31f..d805bd31 100644 --- a/src/path-search-result.cpp +++ b/src/path-search-result.cpp @@ -1,7 +1,9 @@ -#include "path-search.hpp" #include "path-search-result.hpp" -#include "src/point.hpp" +//#include "path-search.hpp" #include "compat/assert.hpp" +#include "compat/vector-wrapper.hpp" +#include "path-search-node.hpp" +#include "src/point.hpp" #include <Corrade/Containers/ArrayView.h> #include <utility> @@ -61,6 +63,9 @@ path_search_result& path_search_result::operator=(const path_search_result& x) n return *this; } +path_search_result::path_search_result(path_search_result&&) noexcept = default; +path_search_result& path_search_result::operator=(path_search_result&&) noexcept = default; + size_t path_search_result::size() const { return _node->vec.size(); } path_search_result::node::node() noexcept = default; float path_search_result::time() const { return _time; } @@ -88,7 +93,7 @@ const point& path_search_result::operator[](size_t index) const fm_debug_assert(index < _node->vec.size()); return data()[index]; } -auto path_search_result::path() -> std::vector<point>& { fm_assert(_node); return _node->vec; } -auto path_search_result::path() const -> const std::vector<point>& { fm_assert(_node); return _node->vec; } +vector_wrapper<point> path_search_result::raw_path() { fm_assert(_node); return {_node->vec}; } +ArrayView<const point> path_search_result::path() const { fm_assert(_node); return {_node->vec.data(), _node->vec.size()}; } } // namespace floormat diff --git a/src/path-search-result.hpp b/src/path-search-result.hpp index fc02e1ba..8987a041 100644 --- a/src/path-search-result.hpp +++ b/src/path-search-result.hpp @@ -1,11 +1,11 @@ #pragma once #include "src/global-coords.hpp" #include "compat/defs.hpp" -#include <vector> #include <Corrade/Containers/Pointer.h> namespace floormat { +template<typename T> struct vector_wrapper; struct point; struct path_search_result final @@ -24,32 +24,20 @@ struct path_search_result final uint32_t distance() const; void set_distance(uint32_t dist); - std::vector<point>& path(); - const std::vector<point>& path() const; + vector_wrapper<point> raw_path(); + ArrayView<const point> path() const; explicit operator ArrayView<const point>() const; explicit operator bool() const; - fm_DECLARE_DEFAULT_MOVE_ASSIGNMENT_(path_search_result); + path_search_result(); path_search_result(const path_search_result& x) noexcept; path_search_result& operator=(const path_search_result& x) noexcept; - path_search_result(); + path_search_result(path_search_result&&) noexcept; + path_search_result& operator=(path_search_result&&) noexcept; ~path_search_result() noexcept; private: - struct node - { - friend struct path_search_result; - friend struct test_app; - - node() noexcept; - fm_DECLARE_DELETED_COPY_ASSIGNMENT(node); - fm_DECLARE_DEFAULT_MOVE_ASSIGNMENT_(node); - - std::vector<point> vec; - - private: - Pointer<node> _next; - }; + struct node; static Pointer<node> _pool; // NOLINT(*-avoid-non-const-global-variables) diff --git a/test/dijkstra.cpp b/test/dijkstra.cpp index b09d7fd5..d5412559 100644 --- a/test/dijkstra.cpp +++ b/test/dijkstra.cpp @@ -59,7 +59,7 @@ void test_app::test_dijkstra() auto result = run(debug); fm_assert(!result.is_found()); - fm_assert(!result.path().empty()); + fm_assert(!result.path().isEmpty()); fm_assert(result.size() > 4); fm_assert(result.cost() > 1000); fm_assert(result.cost() < 3000); @@ -74,7 +74,7 @@ void test_app::test_dijkstra() auto result = run(debug); fm_assert(result.is_found()); - fm_assert(!result.path().empty()); + fm_assert(!result.path().isEmpty()); fm_assert(result.size() > 4); fm_assert(result.cost() > 1000); fm_assert(result.cost() < 3000); diff --git a/test/path-search-result.cpp b/test/path-search-result.cpp index 705eca78..2ce61ff6 100644 --- a/test/path-search-result.cpp +++ b/test/path-search-result.cpp @@ -1,6 +1,7 @@ #include "app.hpp" #include "compat/assert.hpp" #include "src/path-search-result.hpp" +#include "src/path-search-node.hpp" namespace floormat { |