1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
|
#include "path-search-astar.hpp"
#include "compat/int-hash.hpp"
#include <utility>
namespace floormat {
size_t astar_hash::operator()(const astar_edge& e) const
{
static_assert(sizeof e == 16);
if constexpr(sizeof(void*) > 4)
return fnvhash_64(&e, sizeof e);
else
return fnvhash_32(&e, sizeof e);
}
bool astar_edge::operator==(const astar_edge&) const noexcept = default;
astar_edge::astar_edge(global_coords coord1, Vector2b off1,
global_coords coord2, Vector2b off2) :
astar_edge {
chunk_coords_{coord1}, coord1.local(), off1,
chunk_coords_{coord2}, coord2.local(), off2,
}
{
}
size_t astar_edge::hash() const
{
static_assert(sizeof *this == 16);
if constexpr(sizeof nullptr > 4)
return fnvhash_64(this, sizeof *this);
else
return fnvhash_32(this, sizeof *this);
}
astar_edge astar_edge::swapped() const
{
auto e = *this;
std::exchange(e.from_cx, e.to_cx);
std::exchange(e.from_cy, e.to_cy);
std::exchange(e.from_cz, e.to_cz);
std::exchange(e.from_t, e.to_t);
std::exchange(e.from_offx, e.to_offx);
std::exchange(e.from_offy, e.to_offy);
return e;
}
astar_edge::astar_edge() {}
bool operator<(const astar_edge_tuple& a, const astar_edge_tuple& b)
{
return b.dist < a.dist;
}
void astar::reserve(size_t count)
{
Q.reserve(count);
seen.reserve(2*count);
}
bool astar::add_visited(const astar_edge& value)
{
return seen.insert(value).second && seen.insert(value.swapped()).second;
}
void astar::push(const astar_edge& value, uint32_t dist)
{
Q.emplace_back(value, dist);
std::push_heap(Q.begin(), Q.end());
}
astar_edge_tuple astar::pop()
{
fm_debug_assert(!Q.empty());
auto ret = Q.back();
std::pop_heap(Q.begin(), Q.end());
return ret;
}
void astar::clear()
{
Q.clear();
seen.clear();
}
} // namespace floormat
|