blob: 39b47db132c2ea220232badd554a824212da1f83 (
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
|
#include "world.hpp"
#include "chunk.hpp"
namespace floormat {
world::world() : world{initial_capacity}
{
}
world::world(std::size_t capacity) : _chunks{capacity, hasher}
{
_chunks.max_load_factor(max_load_factor);
}
chunk& world::operator[](chunk_coords coord) noexcept
{
maybe_collect();
if (auto& [c, coord2] = _last_chunk; c && coord == coord2)
{
return *c;
}
auto [it, inserted] = _chunks.try_emplace(coord);
auto& ret = it->second;
auto& [_c, _coord] = _last_chunk;
_c = &ret;
_coord = coord;
return ret;
}
auto world::operator[](global_coords pt) noexcept -> pair
{
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;
_chunks.clear();
_chunks.rehash(initial_capacity);
auto& [c, _] = _last_chunk;
c = nullptr;
}
void world::maybe_collect()
{
if (_last_collection + collect_every > _chunks.size())
collect();
}
void world::collect(bool force)
{
for (auto it = _chunks.begin(); it != _chunks.end(); (void)0)
{
const auto& [_, c] = *it;
if (c.empty(force))
it = _chunks.erase(it);
else
++it;
}
_last_collection = _chunks.size();
auto& [c, _] = _last_chunk;
c = nullptr;
}
} // namespace floormat
|