blob: 4e47f912c7343e70c2afedfee1893218107bf94b (
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
|
#include "world.hpp"
#include "chunk.hpp"
namespace floormat {
struct chunk_pointer_maker final
{
inline 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
{
maybe_collect();
if (_last_chunk)
{
auto& [ret, pos] = *_last_chunk;
if (pos == c)
return ret;
}
auto [it, inserted] = _chunks.try_emplace(c, chunk_pointer_maker{});
auto ret = it->second;
_last_chunk = { ret, c };
return ret;
}
std::tuple<std::shared_ptr<chunk>, tile&> world::operator[](global_coords pt) noexcept
{
auto c = operator[](pt.chunk());
return { c, (*c)[pt.local()] };
}
bool world::contains(chunk_coords c) const noexcept
{
return _chunks.find(c) != _chunks.cend();
}
void world::clear()
{
_last_collection = 0;
_last_chunk = std::nullopt;
_chunks.clear();
_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& [_, c] = *it;
if (c->empty())
it = _chunks.erase(it);
else
++it;
}
_last_collection = _chunks.size();
_last_chunk = std::nullopt;
}
} // namespace floormat
|