Skip to content

Commit

Permalink
Remove unnecessary sequence interface on PycDict
Browse files Browse the repository at this point in the history
  • Loading branch information
zrax committed Nov 9, 2023
1 parent 9b384ad commit e27faa1
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 37 deletions.
27 changes: 1 addition & 26 deletions pyc_sequence.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ bool PycDict::isEqual(PycRef<PycObject> obj) const
return false;

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

auto ki1 = m_keys.cbegin();
Expand All @@ -85,28 +85,3 @@ bool PycDict::isEqual(PycRef<PycObject> obj) const
}
return true;
}

PycRef<PycObject> PycDict::get(PycRef<PycObject> key) const
{
auto ki = m_keys.cbegin();
auto vi = m_values.cbegin();
while (ki != m_keys.cend()) {
if ((*ki)->isEqual(key))
return *vi;
++ki, ++vi;
}
return NULL; // Disassembly shouldn't get non-existent keys
}

PycRef<PycObject> PycDict::get(int idx) const
{
if (idx < 0)
throw std::out_of_range("Dict index out of range");

auto it = m_values.cbegin();
while (idx-- && it != m_values.cend())
++it;
if (it == m_values.cend())
throw std::out_of_range("Dict index out of range");
return *it;
}
19 changes: 8 additions & 11 deletions pyc_sequence.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,32 +46,29 @@ class PycList : public PycSimpleSequence {
PycList(int type = TYPE_LIST) : PycSimpleSequence(type) { }
};

class PycDict : public PycSequence {
class PycSet : public PycSimpleSequence {
public:
typedef PycSimpleSequence::value_t value_t;
PycSet(int type = TYPE_SET) : PycSimpleSequence(type) { }
};

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

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

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

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

PycRef<PycObject> get(PycRef<PycObject> key) const;
const key_t& keys() const { return m_keys; }
const value_t& values() const { return m_values; }

PycRef<PycObject> get(int idx) const override;

private:
key_t m_keys;
value_t m_values;
};

class PycSet : public PycSimpleSequence {
public:
typedef PycSimpleSequence::value_t value_t;
PycSet(int type = TYPE_SET) : PycSimpleSequence(type) { }
};

#endif

0 comments on commit e27faa1

Please sign in to comment.