blob: 5018b5c9c30a4d0e4d175df6e077e8166ecff657 (
plain)
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
88
89
90
91
|
#include "world.hpp"
#include "chunk.hpp"
namespace floormat {
struct chunk_pointer_maker final
{
operator std::shared_ptr<chunk>() const { return std::make_shared<chunk>(); }
};
world::world()
{
_chunks.max_load_factor(max_load_factor);
}
std::shared_ptr<chunk> world::operator[](chunk_coords c) noexcept
{
auto [it, inserted] = _chunks.try_emplace(c, chunk_pointer_maker{});
maybe_collect(); return it->second;
}
std::shared_ptr<const chunk> world::maybe_chunk(chunk_coords c) const noexcept
{
if (const auto it = _chunks.find(c); it != _chunks.cend())
return it->second;
else
return nullptr;
}
std::shared_ptr<chunk> world::maybe_chunk(chunk_coords c) noexcept
{
return std::const_pointer_cast<chunk>(const_cast<const world&>(*this).maybe_chunk(c));
}
bool world::contains(chunk_coords c) const noexcept
{
return _chunks.find(c) != _chunks.cend();
}
void world::clear()
{
_last_collection = 0;
_chunks.rehash(initial_capacity);
}
void world::maybe_collect()
{
if (_last_collection + collect_every > _chunks.size())
collect();
}
void world::collect()
{
for (auto it = _chunks.begin(); it != _chunks.end(); (void)0)
{
const auto& [k, c] = *it;
if (c->empty())
it = _chunks.erase(it);
else
it++;
}
_last_collection = _chunks.size();
}
std::size_t world::hasher::operator()(chunk_coords c) const noexcept
{
std::size_t x = (std::size_t)c.y << 16 | (std::size_t)c.x;
if constexpr(sizeof(std::size_t) == 4)
{
// by Chris Wellons <https://nullprogram.com/blog/2018/07/31/>
x ^= x >> 15;
x *= 0x2c1b3c6dU;
x ^= x >> 12;
x *= 0x297a2d39U;
x ^= x >> 15;
}
else if constexpr(sizeof(std::size_t) == 8)
{
// splitmix64 by George Marsaglia
x ^= x >> 30;
x *= 0xbf58476d1ce4e5b9U;
x ^= x >> 27;
x *= 0x94d049bb133111ebU;
x ^= x >> 31;
}
return x;
}
} // namespace floormat
|