update Tuple's constructor

This commit is contained in:
blueloveTH 2024-01-20 23:21:42 +08:00
parent 067c333de9
commit 068abe347f
6 changed files with 17 additions and 10 deletions

View File

@ -172,7 +172,7 @@ struct PyLuaTable: PyLuaObject{
lua_rawgeti(_L, LUA_REGISTRYINDEX, self.r); lua_rawgeti(_L, LUA_REGISTRYINDEX, self.r);
List ret; List ret;
table_apply(vm, [&](PyObject* key, PyObject* val){ table_apply(vm, [&](PyObject* key, PyObject* val){
PyObject* item = VAR(Tuple({key, val})); PyObject* item = VAR(Tuple(key, val));
ret.push_back(item); ret.push_back(item);
}); });
lua_pop(_L, 1); lua_pop(_L, 1);

View File

@ -13,7 +13,6 @@
#include <map> #include <map>
#include <set> #include <set>
#include <algorithm> #include <algorithm>
#include <initializer_list>
#include <variant> #include <variant>
#include <type_traits> #include <type_traits>
#include <random> #include <random>

View File

@ -15,12 +15,14 @@ struct Tuple {
int _size; int _size;
Tuple(int n); Tuple(int n);
Tuple(std::initializer_list<PyObject*> list);
Tuple(const Tuple& other); Tuple(const Tuple& other);
Tuple(Tuple&& other) noexcept; Tuple(Tuple&& other) noexcept;
Tuple(List&& other) noexcept; Tuple(List&& other) noexcept;
~Tuple(); ~Tuple();
Tuple(PyObject*, PyObject*);
Tuple(PyObject*, PyObject*, PyObject*);
bool is_inlined() const { return _args == _inlined; } bool is_inlined() const { return _args == _inlined; }
PyObject*& operator[](int i){ return _args[i]; } PyObject*& operator[](int i){ return _args[i]; }
PyObject* operator[](int i) const { return _args[i]; } PyObject* operator[](int i) const { return _args[i]; }

View File

@ -205,7 +205,7 @@ void add_module_math(VM* vm){
vm->bind_func<1>(mod, "modf", [](VM* vm, ArgsView args) { vm->bind_func<1>(mod, "modf", [](VM* vm, ArgsView args) {
f64 i; f64 i;
f64 f = std::modf(CAST_F(args[0]), &i); f64 f = std::modf(CAST_F(args[0]), &i);
return VAR(Tuple({VAR(f), VAR(i)})); return VAR(Tuple(VAR(f), VAR(i)));
}); });
vm->bind_func<1>(mod, "factorial", [](VM* vm, ArgsView args) { vm->bind_func<1>(mod, "factorial", [](VM* vm, ArgsView args) {

View File

@ -164,7 +164,7 @@ void init_builtins(VM* _vm) {
i64 rhs = CAST(i64, args[1]); i64 rhs = CAST(i64, args[1]);
if(rhs == 0) vm->ZeroDivisionError(); if(rhs == 0) vm->ZeroDivisionError();
auto res = std::div(lhs, rhs); auto res = std::div(lhs, rhs);
return VAR(Tuple({VAR(res.quot), VAR(res.rem)})); return VAR(Tuple(VAR(res.quot), VAR(res.rem)));
}else{ }else{
return vm->call_method(args[0], __divmod__, args[1]); return vm->call_method(args[0], __divmod__, args[1]);
} }
@ -1056,7 +1056,7 @@ void init_builtins(VM* _vm) {
MappingProxy& self = _CAST(MappingProxy&, args[0]); MappingProxy& self = _CAST(MappingProxy&, args[0]);
List items; List items;
for(auto& item : self.attr().items()){ for(auto& item : self.attr().items()){
PyObject* t = VAR(Tuple({VAR(item.first.sv()), item.second})); PyObject* t = VAR(Tuple(VAR(item.first.sv()), item.second));
items.push_back(std::move(t)); items.push_back(std::move(t));
} }
return VAR(std::move(items)); return VAR(std::move(items));
@ -1216,7 +1216,7 @@ void init_builtins(VM* _vm) {
Tuple items(self.size()); Tuple items(self.size());
int j = 0; int j = 0;
self.apply([&](PyObject* k, PyObject* v){ self.apply([&](PyObject* k, PyObject* v){
items[j++] = VAR(Tuple({k, v})); items[j++] = VAR(Tuple(k, v));
}); });
return VAR(std::move(items)); return VAR(std::move(items));
}); });

View File

@ -33,9 +33,15 @@ Tuple::Tuple(List&& other) noexcept {
other._data = nullptr; other._data = nullptr;
} }
Tuple::Tuple(std::initializer_list<PyObject*> list): Tuple(list.size()){ Tuple::Tuple(PyObject* _0, PyObject* _1): Tuple(2){
int i = 0; _args[0] = _0;
for(PyObject* obj: list) _args[i++] = obj; _args[1] = _1;
}
Tuple::Tuple(PyObject* _0, PyObject* _1, PyObject* _2): Tuple(3){
_args[0] = _0;
_args[1] = _1;
_args[2] = _2;
} }
Tuple::~Tuple(){ if(!is_inlined()) pool64_dealloc(_args); } Tuple::~Tuple(){ if(!is_inlined()) pool64_dealloc(_args); }