ArkScript
A small, fast, functional and scripting language for video games
ScopeView.cpp
Go to the documentation of this file.
2
3#include <limits>
4
5namespace Ark::internal
6{
7 ScopeView::ScopeView(pair_t* storage, const std::size_t start) noexcept :
8 m_storage(storage), m_start(start), m_size(0), m_min_id(std::numeric_limits<uint16_t>::max()), m_max_id(0)
9 {}
10
11 void ScopeView::push_back(uint16_t id, Value&& val) noexcept
12 {
13 if (id < m_min_id)
14 m_min_id = id;
15 if (id > m_max_id)
16 m_max_id = id;
17
18 m_storage[m_start + m_size] = std::make_pair(id, std::move(val));
19 ++m_size;
20 }
21
22 void ScopeView::push_back(uint16_t id, const Value& val) noexcept
23 {
24 if (id < m_min_id)
25 m_min_id = id;
26 if (id > m_max_id)
27 m_max_id = id;
28
29 m_storage[m_start + m_size] = std::make_pair(id, val);
30 ++m_size;
31 }
32
33 bool ScopeView::maybeHas(const uint16_t id) const noexcept
34 {
35 return m_min_id <= id && id <= m_max_id;
36 }
37
38 Value* ScopeView::operator[](const uint16_t id_to_look_for) noexcept
39 {
40 if (!maybeHas(id_to_look_for))
41 return nullptr;
42
43 for (std::size_t i = m_start; i < m_start + m_size; ++i)
44 {
45 auto& [id, value] = m_storage[i];
46 if (id == id_to_look_for)
47 return &value;
48 }
49 return nullptr;
50 }
51
52 const Value* ScopeView::operator[](const uint16_t id_to_look_for) const noexcept
53 {
54 if (!maybeHas(id_to_look_for))
55 return nullptr;
56
57 for (std::size_t i = m_start; i < m_start + m_size; ++i)
58 {
59 auto& [id, value] = m_storage[i];
60 if (id == id_to_look_for)
61 return &value;
62 }
63 return nullptr;
64 }
65
66 uint16_t ScopeView::idFromValue(const Value& val) const noexcept
67 {
68 for (std::size_t i = m_start; i < m_start + m_size; ++i)
69 {
70 const auto& [id, value] = m_storage[i];
71 if (value == val)
72 return id;
73 }
74 return std::numeric_limits<uint16_t>::max();
75 }
76
77 void ScopeView::reset() noexcept
78 {
79 m_size = 0;
80 m_min_id = std::numeric_limits<uint16_t>::max();
81 m_max_id = 0;
82 }
83
84 bool operator==(const ScopeView& A, const ScopeView& B) noexcept
85 {
86 // if we have two scopes with the same number of elements and starting at the same position,
87 // they must be identical, as we have a single storage for all scopes
88 return A.m_size == B.m_size && A.m_start == B.m_start;
89 }
90}
A class to handle the VM scope more efficiently.
Definition ScopeView.hpp:27
ScopeView()=delete
Deleted constructor to avoid creating ScopeViews pointing to nothing. Helps catch bugs at compile tim...
bool maybeHas(uint16_t id) const noexcept
Check if the scope maybe holds a specific symbol in memory.
Definition ScopeView.cpp:33
uint16_t m_max_id
Maximum stored ID, used for a basic bloom filter.
std::pair< uint16_t, Value > pair_t
Definition ScopeView.hpp:29
Value * operator[](uint16_t id_to_look_for) noexcept
Get a value from its symbol id.
Definition ScopeView.cpp:38
void push_back(uint16_t id, Value &&val) noexcept
Put a value in the scope.
Definition ScopeView.cpp:11
void reset() noexcept
Reset size, min and max id for the scope, to signify it's empty.
Definition ScopeView.cpp:77
uint16_t m_min_id
Minimum stored ID, used for a basic bloom filter.
uint16_t idFromValue(const Value &val) const noexcept
Get the id of a variable based on its value ; used for debug only.
Definition ScopeView.cpp:66
bool operator==(const Namespace &A, const Namespace &B)
Definition Namespace.hpp:21