Gaia-ECS v0.9.3
A simple and powerful entity component system
Loading...
Searching...
No Matches
query_match_stamps.h
1#pragma once
2#include "gaia/config/config.h"
3
4#include <cstdint>
5#include <cstring>
6
7#include "gaia/cnt/darray.h"
8#include "gaia/core/utility.h"
9#include "gaia/mem/mem_alloc.h"
10
11namespace gaia {
12 namespace ecs {
16 static constexpr uint32_t PageBits = 10;
17 static constexpr uint32_t PageSize = 1U << PageBits;
18 static constexpr uint32_t PageMask = PageSize - 1;
19
22
23 ArchetypeMatchStamps() = default;
25 ArchetypeMatchStamps& operator=(const ArchetypeMatchStamps&) = delete;
26
27 ArchetypeMatchStamps(ArchetypeMatchStamps&& other) noexcept: pages(GAIA_MOV(other.pages)) {
28 other.pages = {};
29 }
30
31 ArchetypeMatchStamps& operator=(ArchetypeMatchStamps&& other) noexcept {
32 if (this == &other)
33 return *this;
34
35 free_pages();
36 pages = GAIA_MOV(other.pages);
37 other.pages = {};
38 return *this;
39 }
40
41 ~ArchetypeMatchStamps() {
42 free_pages();
43 }
44
45 GAIA_NODISCARD bool has(uint32_t sid) const {
46 const auto pid = sid >> PageBits;
47 return pid < pages.size() && pages[pid] != nullptr;
48 }
49
50 GAIA_NODISCARD uint32_t get(uint32_t sid) const {
51 GAIA_ASSERT(has(sid));
52 const auto pid = sid >> PageBits;
53 const auto did = sid & PageMask;
54 return pages[pid][did];
55 }
56
57 void set(uint32_t sid, uint32_t version) {
58 const auto pid = sid >> PageBits;
59 const auto did = sid & PageMask;
60 auto* page = ensure_page(pid);
61 page[did] = version;
62 }
63
64 void clear() {
65 for (auto* page: pages) {
66 if (page == nullptr)
67 continue;
71 std::memset(page, 0, sizeof(uint32_t) * PageSize);
72 }
73 }
74
75 private:
76 GAIA_NODISCARD uint32_t* ensure_page(uint32_t pid) {
77 if (pid >= pages.size())
78 pages.resize(pid + 1, nullptr);
79
80 auto*& page = pages[pid];
81 if (page == nullptr) {
82 page = mem::AllocHelper::alloc<uint32_t>("ArchetypeMatchStampPage", PageSize);
83 std::memset(page, 0, sizeof(uint32_t) * PageSize);
84 }
85
86 return page;
87 }
88
89 void free_pages() {
90 for (auto* page: pages) {
91 if (page == nullptr)
92 continue;
93 mem::AllocHelper::free("ArchetypeMatchStampPage", page);
94 }
95 pages = {};
96 }
97 };
98 } // namespace ecs
99} // namespace gaia
Array with variable size of elements of type.
Definition darray_impl.h:25
Checks if endianess was detected correctly at compile-time.
Definition bitset.h:9
Definition query_match_stamps.h:13
cnt::darray< uint32_t * > pages
Lazily allocated pages keyed by archetype id >> PageBits.
Definition query_match_stamps.h:21
void clear()
Definition query_match_stamps.h:64
static constexpr uint32_t PageBits
Keep stamp pages small enough to avoid dense high-water-mark allocations while still preserving O(1) ...
Definition query_match_stamps.h:16