-
-
Notifications
You must be signed in to change notification settings - Fork 52
Expand file tree
/
Copy pathScopeView.cpp
More file actions
90 lines (76 loc) · 2.47 KB
/
ScopeView.cpp
File metadata and controls
90 lines (76 loc) · 2.47 KB
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
#include <Ark/VM/ScopeView.hpp>
#include <limits>
namespace Ark::internal
{
ScopeView::ScopeView(pair_t* storage, const std::size_t start) noexcept :
m_storage(storage), m_start(start), m_size(0), m_min_id(std::numeric_limits<uint16_t>::max()), m_max_id(0)
{}
void ScopeView::push_back(uint16_t id, Value&& val) noexcept
{
if (id < m_min_id)
m_min_id = id;
if (id > m_max_id)
m_max_id = id;
m_storage[m_start + m_size] = std::make_pair(id, std::move(val));
++m_size;
}
void ScopeView::push_back(uint16_t id, const Value& val) noexcept
{
if (id < m_min_id)
m_min_id = id;
if (id > m_max_id)
m_max_id = id;
m_storage[m_start + m_size] = std::make_pair(id, val);
++m_size;
}
bool ScopeView::maybeHas(const uint16_t id) const noexcept
{
return m_min_id <= id && id <= m_max_id;
}
Value* ScopeView::operator[](const uint16_t id_to_look_for) noexcept
{
if (!maybeHas(id_to_look_for))
return nullptr;
for (std::size_t i = m_start; i < m_start + m_size; ++i)
{
auto& [id, value] = m_storage[i];
if (id == id_to_look_for)
return &value;
}
return nullptr;
}
const Value* ScopeView::operator[](const uint16_t id_to_look_for) const noexcept
{
if (!maybeHas(id_to_look_for))
return nullptr;
for (std::size_t i = m_start; i < m_start + m_size; ++i)
{
auto& [id, value] = m_storage[i];
if (id == id_to_look_for)
return &value;
}
return nullptr;
}
uint16_t ScopeView::idFromValue(const Value& val) const noexcept
{
for (std::size_t i = m_start; i < m_start + m_size; ++i)
{
const auto& [id, value] = m_storage[i];
if (value == val)
return id;
}
return std::numeric_limits<uint16_t>::max();
}
void ScopeView::reset() noexcept
{
m_size = 0;
m_min_id = std::numeric_limits<uint16_t>::max();
m_max_id = 0;
}
bool operator==(const ScopeView& A, const ScopeView& B) noexcept
{
// if we have two scopes with the same number of elements and starting at the same position,
// they must be identical, as we have a single storage for all scopes
return A.m_size == B.m_size && A.m_start == B.m_start;
}
}