Skip to content

Commit

Permalink
Simplify PycDict key/value storage a bit
Browse files Browse the repository at this point in the history
  • Loading branch information
zrax committed Nov 9, 2023
1 parent e27faa1 commit 21c35b2
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 37 deletions.
17 changes: 7 additions & 10 deletions bytecode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -219,20 +219,17 @@ void print_const(std::ostream& pyc_output, PycRef<PycObject> obj, PycModule* mod
case PycObject::TYPE_DICT:
{
pyc_output << "{";
PycDict::key_t keys = obj.cast<PycDict>()->keys();
PycDict::value_t values = obj.cast<PycDict>()->values();
auto ki = keys.cbegin();
auto vi = values.cbegin();
if (ki != keys.cend()) {
print_const(pyc_output, *ki, mod);
auto it = values.cbegin();
if (it != values.cend()) {
print_const(pyc_output, std::get<0>(*it), mod);
pyc_output << ": ";
print_const(pyc_output, *vi, mod);
while (++ki != keys.cend()) {
++vi;
print_const(pyc_output, std::get<1>(*it), mod);
while (++it != values.cend()) {
pyc_output << ", ";
print_const(pyc_output, *ki, mod);
print_const(pyc_output, std::get<0>(*it), mod);
pyc_output << ": ";
print_const(pyc_output, *vi, mod);
print_const(pyc_output, std::get<1>(*it), mod);
}
}
pyc_output << "}";
Expand Down
23 changes: 8 additions & 15 deletions pyc_sequence.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,7 @@ void PycDict::load(PycData* stream, PycModule* mod)
if (key == NULL)
break;
val = LoadObject(stream, mod);
m_keys.push_back(key);
m_values.push_back(val);
m_values.emplace_back(std::make_tuple(key, val));
}
}

Expand All @@ -65,23 +64,17 @@ bool PycDict::isEqual(PycRef<PycObject> obj) const
return false;

PycRef<PycDict> dictObj = obj.cast<PycDict>();
if (m_keys.size() != dictObj->m_keys.size())
if (m_values.size() != dictObj->m_values.size())
return false;

auto ki1 = m_keys.cbegin();
auto ki2 = dictObj->m_keys.cbegin();
while (ki1 != m_keys.cend()) {
if (!(*ki1)->isEqual(*ki2))
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;
++ki1, ++ki2;
}

auto vi1 = m_values.cbegin();
auto vi2 = dictObj->m_values.cbegin();
while (vi1 != m_values.cend()) {
if (!(*vi1)->isEqual(*vi2))
if (!std::get<1>(*it1)->isEqual(std::get<1>(*it2)))
return false;
++vi1, ++vi2;
++it1, ++it2;
}
return true;
}
7 changes: 3 additions & 4 deletions pyc_sequence.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#define _PYC_SEQUENCE_H

#include "pyc_object.h"
#include <tuple>
#include <vector>

class PycSequence : public PycObject {
Expand Down Expand Up @@ -54,20 +55,18 @@ class PycSet : public PycSimpleSequence {

class PycDict : public PycObject {
public:
typedef std::vector<PycRef<PycObject>> key_t;
typedef std::vector<PycRef<PycObject>> value_t;
typedef std::tuple<PycRef<PycObject>, PycRef<PycObject>> item_t;
typedef std::vector<item_t> value_t;

PycDict(int type = TYPE_DICT) : PycObject(type) { }

bool isEqual(PycRef<PycObject> obj) const override;

void load(class PycData* stream, class PycModule* mod) override;

const key_t& keys() const { return m_keys; }
const value_t& values() const { return m_values; }

private:
key_t m_keys;
value_t m_values;
};

Expand Down
11 changes: 3 additions & 8 deletions pycdas.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -183,14 +183,9 @@ void output_object(PycRef<PycObject> obj, PycModule* mod, int indent,
case PycObject::TYPE_DICT:
{
iputs(pyc_output, indent, "{\n");
PycDict::key_t keys = obj.cast<PycDict>()->keys();
PycDict::value_t values = obj.cast<PycDict>()->values();
PycDict::key_t::const_iterator ki = keys.begin();
PycDict::value_t::const_iterator vi = values.begin();
while (ki != keys.end()) {
output_object(*ki, mod, indent + 1, flags, pyc_output);
output_object(*vi, mod, indent + 2, flags, pyc_output);
++ki, ++vi;
for (const auto& val : obj.cast<PycDict>()->values()) {
output_object(std::get<0>(val), mod, indent + 1, flags, pyc_output);
output_object(std::get<1>(val), mod, indent + 2, flags, pyc_output);
}
iputs(pyc_output, indent, "}\n");
}
Expand Down

0 comments on commit 21c35b2

Please sign in to comment.