-
Notifications
You must be signed in to change notification settings - Fork 609
/
pyc_sequence.cpp
80 lines (69 loc) · 1.92 KB
/
pyc_sequence.cpp
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
#include "pyc_sequence.h"
#include "pyc_module.h"
#include "data.h"
#include <stdexcept>
/* PycSimpleSequence */
void PycSimpleSequence::load(PycData* stream, PycModule* mod)
{
m_size = stream->get32();
m_values.reserve(m_size);
for (int i=0; i<m_size; i++)
m_values.push_back(LoadObject(stream, mod));
}
bool PycSimpleSequence::isEqual(PycRef<PycObject> obj) const
{
if (type() != obj.type())
return false;
PycRef<PycSimpleSequence> seqObj = obj.cast<PycSimpleSequence>();
if (m_size != seqObj->m_size)
return false;
auto it1 = m_values.cbegin();
auto it2 = seqObj->m_values.cbegin();
while (it1 != m_values.cend()) {
if (!(*it1)->isEqual(*it2))
return false;
++it1, ++it2;
}
return true;
}
/* PycTuple */
void PycTuple::load(PycData* stream, PycModule* mod)
{
if (type() == TYPE_SMALL_TUPLE)
m_size = stream->getByte();
else
m_size = stream->get32();
m_values.resize(m_size);
for (int i=0; i<m_size; i++)
m_values[i] = LoadObject(stream, mod);
}
/* PycDict */
void PycDict::load(PycData* stream, PycModule* mod)
{
PycRef<PycObject> key, val;
for (;;) {
key = LoadObject(stream, mod);
if (key == NULL)
break;
val = LoadObject(stream, mod);
m_values.emplace_back(std::make_tuple(key, val));
}
}
bool PycDict::isEqual(PycRef<PycObject> obj) const
{
if (type() != obj.type())
return false;
PycRef<PycDict> dictObj = obj.cast<PycDict>();
if (m_values.size() != dictObj->m_values.size())
return false;
auto it1 = m_values.cbegin();
auto it2 = dictObj->m_values.cbegin();
while (it1 != m_values.cend()) {
if (!std::get<0>(*it1)->isEqual(std::get<0>(*it2)))
return false;
if (!std::get<1>(*it1)->isEqual(std::get<1>(*it2)))
return false;
++it1, ++it2;
}
return true;
}