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
|
#pragma once
#include "compat/int-hash.hpp"
#include "global-coords.hpp"
#include "tile.hpp"
#include "chunk.hpp"
#include <unordered_map>
#include <memory>
#include <optional>
namespace floormat {
struct chunk;
struct world final
{
private:
void maybe_collect();
static constexpr std::size_t initial_capacity = 64, collect_every = 32;
static constexpr float max_load_factor = .5;
static constexpr auto hasher = [](chunk_coords c) constexpr -> std::size_t {
return int_hash((std::size_t)c.y << 16 | (std::size_t)c.x);
};
std::unordered_map<chunk_coords, chunk, decltype(hasher)> _chunks{initial_capacity, hasher};
mutable std::tuple<chunk*, chunk_coords>_last_chunk;
std::size_t _last_collection = 0;
public:
explicit world();
template<typename Hash, typename Alloc, typename Pred>
explicit world(std::unordered_map<chunk_coords, chunk, Hash, Alloc, Pred>&& chunks);
chunk& operator[](chunk_coords c) noexcept;
std::tuple<chunk&, tile&> operator[](global_coords pt) noexcept;
bool contains(chunk_coords c) const noexcept;
void clear();
void collect(bool force = false);
[[deprecated]] const auto& chunks() const noexcept { return _chunks; } // only for serialization
fm_DECLARE_DEPRECATED_COPY_ASSIGNMENT(world);
fm_DECLARE_DEFAULT_MOVE_ASSIGNMENT_(world);
};
template<typename Hash, typename Alloc, typename Pred>
world::world(std::unordered_map<chunk_coords, chunk, Hash, Alloc, Pred>&& chunks) :
_chunks{std::max(initial_capacity, std::size_t(1/max_load_factor * 2 * chunks.size())), hasher}
{
for (auto&& [coord, c] : chunks)
operator[](coord) = std::move(c);
}
} // namespace floormat
|