Skip to content

Commit

Permalink
Keep PycSet objects marshalled from files in order too
Browse files Browse the repository at this point in the history
  • Loading branch information
zrax committed Feb 17, 2023
1 parent ded8adf commit 1fcbf4c
Show file tree
Hide file tree
Showing 3 changed files with 4 additions and 18 deletions.
15 changes: 1 addition & 14 deletions pyc_sequence.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ void PycSet::load(PycData* stream, PycModule* mod)
{
m_size = stream->get32();
for (int i=0; i<m_size; i++)
m_values.insert(LoadObject(stream, mod));
m_values.push_back(LoadObject(stream, mod));
}

bool PycSet::isEqual(PycRef<PycObject> obj) const
Expand All @@ -154,16 +154,3 @@ bool PycSet::isEqual(PycRef<PycObject> obj) const
}
return true;
}

PycRef<PycObject> PycSet::get(int idx) const
{
if (idx < 0)
throw std::out_of_range("Set 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("Set index out of range");
return *it;
}
5 changes: 2 additions & 3 deletions pyc_sequence.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@

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

class PycSequence : public PycObject {
public:
Expand Down Expand Up @@ -74,7 +73,7 @@ class PycDict : public PycSequence {

class PycSet : public PycSequence {
public:
typedef std::set<PycRef<PycObject>> value_t;
typedef std::vector<PycRef<PycObject>> value_t;

PycSet(int type = TYPE_SET) : PycSequence(type) { }

Expand All @@ -83,7 +82,7 @@ class PycSet : public PycSequence {
void load(class PycData* stream, class PycModule* mod) override;

const value_t& values() const { return m_values; }
PycRef<PycObject> get(int idx) const override;
PycRef<PycObject> get(int idx) const override { return m_values.at(idx); }

private:
value_t m_values;
Expand Down
2 changes: 1 addition & 1 deletion tests/tokenized/test_sets.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
a = set ( ) <EOL>
b = { 1 , 2 } <EOL>
c = { 'AB' , 'CD' } <EOL>
d = { 2 , 1 , 3 , 4 } <EOL>
d = { 1 , 2 , 3 , 4 } <EOL>

0 comments on commit 1fcbf4c

Please sign in to comment.