mirror of
https://github.com/pocketpy/pocketpy
synced 2025-11-09 21:20:17 +00:00
WIP: added deque iterator
This commit is contained in:
parent
fcd159ae7f
commit
fd938a7c29
@ -6,7 +6,6 @@
|
|||||||
#include "str.h"
|
#include "str.h"
|
||||||
#include "iter.h"
|
#include "iter.h"
|
||||||
#include "cffi.h"
|
#include "cffi.h"
|
||||||
#include <deque>
|
|
||||||
|
|
||||||
namespace pkpy
|
namespace pkpy
|
||||||
{
|
{
|
||||||
|
|||||||
@ -21,6 +21,7 @@
|
|||||||
#include <type_traits>
|
#include <type_traits>
|
||||||
#include <random>
|
#include <random>
|
||||||
#include <bitset>
|
#include <bitset>
|
||||||
|
#include <deque>
|
||||||
|
|
||||||
#define PK_VERSION "1.2.4"
|
#define PK_VERSION "1.2.4"
|
||||||
|
|
||||||
|
|||||||
@ -29,6 +29,21 @@ struct ArrayIter{
|
|||||||
static void _register(VM* vm, PyObject* mod, PyObject* type);
|
static void _register(VM* vm, PyObject* mod, PyObject* type);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
struct PyDequeIter{
|
||||||
|
// Iterator for the deque type
|
||||||
|
PY_CLASS(PyDequeIter, builtins, "_deque_iterator")
|
||||||
|
PyObject* ref;
|
||||||
|
std::deque<PyObject*>::iterator begin;
|
||||||
|
std::deque<PyObject*>::iterator end;
|
||||||
|
std::deque<PyObject*>::iterator current;
|
||||||
|
|
||||||
|
PyDequeIter(PyObject* ref, std::deque<PyObject*>::iterator begin, std::deque<PyObject*>::iterator end)
|
||||||
|
: ref(ref), begin(begin), end(end), current(begin) {}
|
||||||
|
|
||||||
|
void _gc_mark() const{ PK_OBJ_MARK(ref); }
|
||||||
|
static void _register(VM* vm, PyObject* mod, PyObject* type);
|
||||||
|
};
|
||||||
|
|
||||||
struct StringIter{
|
struct StringIter{
|
||||||
PY_CLASS(StringIter, builtins, "_string_iterator")
|
PY_CLASS(StringIter, builtins, "_string_iterator")
|
||||||
PyObject* ref;
|
PyObject* ref;
|
||||||
|
|||||||
@ -20,6 +20,21 @@ namespace pkpy
|
|||||||
return VAR(self.dequeItems.size());
|
return VAR(self.dequeItems.size());
|
||||||
});
|
});
|
||||||
|
|
||||||
|
vm->bind(type, "__repr__(self) -> str",
|
||||||
|
[](VM *vm, ArgsView args)
|
||||||
|
{
|
||||||
|
PyDeque &self = _CAST(PyDeque &, args[0]);
|
||||||
|
std::stringstream ss = self.getRepr(vm);
|
||||||
|
return VAR(ss.str());
|
||||||
|
});
|
||||||
|
|
||||||
|
vm->bind(type, "__iter__(self) -> deque_iterator",
|
||||||
|
[](VM *vm, ArgsView args)
|
||||||
|
{
|
||||||
|
PyDeque &self = _CAST(PyDeque &, args[0]);
|
||||||
|
return vm->heap.gcnew<PyDequeIter>(PyDequeIter::_type(vm), args[0], self.dequeItems.begin(), self.dequeItems.end());
|
||||||
|
});
|
||||||
|
|
||||||
vm->bind(type, "append(self, item) -> None",
|
vm->bind(type, "append(self, item) -> None",
|
||||||
[](VM *vm, ArgsView args)
|
[](VM *vm, ArgsView args)
|
||||||
{
|
{
|
||||||
@ -127,14 +142,6 @@ namespace pkpy
|
|||||||
return vm->None;
|
return vm->None;
|
||||||
});
|
});
|
||||||
|
|
||||||
vm->bind(type, "__repr__(self) -> str",
|
|
||||||
[](VM *vm, ArgsView args)
|
|
||||||
{
|
|
||||||
PyDeque &self = _CAST(PyDeque &, args[0]);
|
|
||||||
std::stringstream ss = self.getRepr(vm);
|
|
||||||
return VAR(ss.str());
|
|
||||||
});
|
|
||||||
|
|
||||||
vm->bind(type, "pop(self) -> PyObject",
|
vm->bind(type, "pop(self) -> PyObject",
|
||||||
[](VM *vm, ArgsView args)
|
[](VM *vm, ArgsView args)
|
||||||
{
|
{
|
||||||
@ -197,17 +204,21 @@ namespace pkpy
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
PyDeque::PyDeque(VM *vm, PyObject* iterable, PyObject* maxlen)
|
PyDeque::PyDeque(VM *vm, PyObject *iterable, PyObject *maxlen)
|
||||||
{
|
{
|
||||||
if(maxlen!=vm->None){
|
if (maxlen != vm->None)
|
||||||
|
{
|
||||||
this->maxlen = CAST(int, maxlen);
|
this->maxlen = CAST(int, maxlen);
|
||||||
this->bounded = true;
|
this->bounded = true;
|
||||||
}else{
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
this->bounded = false;
|
this->bounded = false;
|
||||||
this->maxlen = -1;
|
this->maxlen = -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
if(iterable!=vm->None){
|
if (iterable != vm->None)
|
||||||
|
{
|
||||||
auto _lock = vm->heap.gc_scope_lock();
|
auto _lock = vm->heap.gc_scope_lock();
|
||||||
PyObject *it = vm->py_iter(iterable); // strong ref
|
PyObject *it = vm->py_iter(iterable); // strong ref
|
||||||
PyObject *obj = vm->py_next(it);
|
PyObject *obj = vm->py_next(it);
|
||||||
|
|||||||
15
src/iter.cpp
15
src/iter.cpp
@ -26,6 +26,21 @@ namespace pkpy{
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void PyDequeIter::_register(VM* vm, PyObject* mod, PyObject* type){
|
||||||
|
// Iterator for the deque type
|
||||||
|
vm->_all_types[PK_OBJ_GET(Type, type)].subclass_enabled = false;
|
||||||
|
vm->bind_notimplemented_constructor<PyDequeIter>(type);
|
||||||
|
|
||||||
|
vm->bind__iter__(PK_OBJ_GET(Type, type), [](VM* vm, PyObject* obj){ return obj; });
|
||||||
|
vm->bind__next__(PK_OBJ_GET(Type, type), [](VM* vm, PyObject* obj){
|
||||||
|
PyDequeIter& self = _CAST(PyDequeIter&, obj);
|
||||||
|
if(self.current == self.end) return vm->StopIteration;
|
||||||
|
PyObject* ret = *self.current;
|
||||||
|
++self.current;
|
||||||
|
return ret;
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
void StringIter::_register(VM* vm, PyObject* mod, PyObject* type){
|
void StringIter::_register(VM* vm, PyObject* mod, PyObject* type){
|
||||||
vm->_all_types[PK_OBJ_GET(Type, type)].subclass_enabled = false;
|
vm->_all_types[PK_OBJ_GET(Type, type)].subclass_enabled = false;
|
||||||
vm->bind_notimplemented_constructor<StringIter>(type);
|
vm->bind_notimplemented_constructor<StringIter>(type);
|
||||||
|
|||||||
@ -1414,6 +1414,7 @@ void init_builtins(VM* _vm) {
|
|||||||
|
|
||||||
RangeIter::register_class(_vm, _vm->builtins);
|
RangeIter::register_class(_vm, _vm->builtins);
|
||||||
ArrayIter::register_class(_vm, _vm->builtins);
|
ArrayIter::register_class(_vm, _vm->builtins);
|
||||||
|
PyDequeIter::register_class(_vm, _vm->builtins);
|
||||||
StringIter::register_class(_vm, _vm->builtins);
|
StringIter::register_class(_vm, _vm->builtins);
|
||||||
Generator::register_class(_vm, _vm->builtins);
|
Generator::register_class(_vm, _vm->builtins);
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user