summaryrefslogtreecommitdiffhomepage
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/search-astar.cpp3
-rw-r--r--src/search-result.cpp79
-rw-r--r--src/search-result.hpp2
3 files changed, 59 insertions, 25 deletions
diff --git a/src/search-astar.cpp b/src/search-astar.cpp
index e86e6bee..77239669 100644
--- a/src/search-astar.cpp
+++ b/src/search-astar.cpp
@@ -135,6 +135,9 @@ void set_result_from_idx(path_search_result& result,
for (auto i = idx; i != (uint32_t)-1; i = nodes[i].prev)
len++;
+ if (!len) [[unlikely]]
+ return;
+
fm_debug_assert(idx != (uint32_t)-1);
arrayResize(temp_nodes, 0);
arrayReserve(temp_nodes, len+1);
diff --git a/src/search-result.cpp b/src/search-result.cpp
index ed9c5194..1afd09df 100644
--- a/src/search-result.cpp
+++ b/src/search-result.cpp
@@ -11,27 +11,11 @@ namespace floormat {
Pointer<path_search_result::node> path_search_result::_pool; // NOLINT
-path_search_result::path_search_result()
-{
- constexpr auto min_length = TILE_MAX_DIM*2;
- if (_pool)
- {
- auto ptr = move(_pool);
- fm_debug_assert(ptr->vec.empty());
- auto next = move(ptr->_next);
- _node = move(ptr);
- _pool = move(next);
- }
- else
- {
- _node = Pointer<node>{InPlaceInit};
- _node->vec.reserve(min_length);
- }
-}
+path_search_result::path_search_result() = default;
path_search_result::~path_search_result() noexcept
{
- if (_node && _node->vec.capacity() > 0) [[likely]]
+ if (_node && _node->vec.capacity() > 0)
{
_node->vec.clear();
_node->_next = move(_pool);
@@ -52,7 +36,7 @@ path_search_result& path_search_result::operator=(const path_search_result& x) n
{
fm_debug_assert(_node);
fm_debug_assert(!_node->_next);
- if (&x != this)
+ if (&x != this) [[likely]]
_node->vec = x._node->vec;
_cost = x._cost;
return *this;
@@ -79,11 +63,59 @@ path_search_result& path_search_result::operator=(path_search_result&& other) no
return *this;
}
-size_t path_search_result::size() const { return _node->vec.size(); }
-bool path_search_result::empty() const { return _node->vec.empty(); }
+void path_search_result::allocate_node()
+{
+ constexpr auto min_length = TILE_MAX_DIM*2;
+
+ if (_node)
+ return;
+
+ if (_pool)
+ {
+ auto ptr = move(_pool);
+ fm_debug_assert(ptr->vec.empty());
+ auto next = move(ptr->_next);
+ _node = move(ptr);
+ _pool = move(next);
+ }
+ else
+ {
+ _node = Pointer<node>{InPlaceInit};
+ _node->vec.reserve(min_length);
+ }
+}
+
+ArrayView<const point> path_search_result::path() const
+{
+ if (!_node)
+ return {};
+ return { _node->vec.data(), _node->vec.size() };
+}
+
+vector_wrapper<point, vector_wrapper_repr::ref> path_search_result::raw_path()
+{
+ if (!_node)
+ allocate_node();
+ return {_node->vec};
+}
+
+size_t path_search_result::size() const
+{
+ return _node ? _node->vec.size() : 0;
+}
+
+bool path_search_result::empty() const
+{
+ return !_node || _node->vec.empty();
+}
+
+path_search_result::operator bool() const
+{
+ return _node && !_node->vec.empty();
+}
+
path_search_result::node::node() noexcept = default;
float path_search_result::time() const { return _time; }
-
uint32_t path_search_result::cost() const { return _cost; }
void path_search_result::set_cost(uint32_t value) { _cost = value; }
void path_search_result::set_time(float time) { _time = time; }
@@ -91,8 +123,5 @@ bool path_search_result::is_found() const { return _found; }
void path_search_result::set_found(bool value) { _found = value; }
uint32_t path_search_result::distance() const { return _distance; }
void path_search_result::set_distance(uint32_t dist) { _distance = dist; }
-path_search_result::operator bool() const { return !_node->vec.empty(); }
-ArrayView<const point> path_search_result::path() const { fm_assert(_node); return {_node->vec.data(), _node->vec.size()}; }
-vector_wrapper<point, vector_wrapper_repr::ref> path_search_result::raw_path() { fm_assert(_node); return {_node->vec}; }
} // namespace floormat
diff --git a/src/search-result.hpp b/src/search-result.hpp
index aa27c374..afbca8af 100644
--- a/src/search-result.hpp
+++ b/src/search-result.hpp
@@ -37,6 +37,8 @@ struct path_search_result final
~path_search_result() noexcept;
private:
+ void allocate_node();
+
Pointer<node> _node;
float _time = 0;
uint32_t _cost = 0, _distance = (uint32_t)-1;