mirror of
https://github.com/pocketpy/pocketpy
synced 2025-10-20 11:30:18 +00:00
optimize dict
This commit is contained in:
parent
db9ce1dbfc
commit
addd0843ea
@ -2545,6 +2545,13 @@ def __list4index(self, value):
|
|||||||
list.index = __list4index
|
list.index = __list4index
|
||||||
del __list4index
|
del __list4index
|
||||||
|
|
||||||
|
def __list4pop(self, i=-1):
|
||||||
|
res = self[i]
|
||||||
|
del self[i]
|
||||||
|
return res
|
||||||
|
list.pop = __list4pop
|
||||||
|
del __list4pop
|
||||||
|
|
||||||
def __list4__mul__(self, n):
|
def __list4__mul__(self, n):
|
||||||
a = []
|
a = []
|
||||||
for i in range(n):
|
for i in range(n):
|
||||||
@ -2587,8 +2594,8 @@ list.__new__ = lambda obj: [i for i in obj]
|
|||||||
|
|
||||||
# https://github.com/python/cpython/blob/main/Objects/dictobject.c
|
# https://github.com/python/cpython/blob/main/Objects/dictobject.c
|
||||||
class dict:
|
class dict:
|
||||||
def __init__(self):
|
def __init__(self, capacity=16):
|
||||||
self._capacity = 8
|
self._capacity = capacity
|
||||||
self._a = [None] * self._capacity
|
self._a = [None] * self._capacity
|
||||||
self._len = 0
|
self._len = 0
|
||||||
|
|
||||||
@ -2600,7 +2607,7 @@ class dict:
|
|||||||
while self._a[i] is not None:
|
while self._a[i] is not None:
|
||||||
if self._a[i][0] == key:
|
if self._a[i][0] == key:
|
||||||
return True, i
|
return True, i
|
||||||
i = ((5*i) + 1) % self._capacity
|
i = (i + 1) % self._capacity
|
||||||
return False, i
|
return False, i
|
||||||
|
|
||||||
def __getitem__(self, key):
|
def __getitem__(self, key):
|
||||||
@ -2620,8 +2627,9 @@ class dict:
|
|||||||
else:
|
else:
|
||||||
self._a[i] = [key, value]
|
self._a[i] = [key, value]
|
||||||
self._len += 1
|
self._len += 1
|
||||||
if self._len > self._capacity * 0.6:
|
if self._len > self._capacity * 0.8:
|
||||||
self.__resize_2x()
|
self._capacity *= 2
|
||||||
|
self.__rehash()
|
||||||
|
|
||||||
def __delitem__(self, key):
|
def __delitem__(self, key):
|
||||||
ok, i = self.__probe(key)
|
ok, i = self.__probe(key)
|
||||||
@ -2630,9 +2638,8 @@ class dict:
|
|||||||
self._a[i] = None
|
self._a[i] = None
|
||||||
self._len -= 1
|
self._len -= 1
|
||||||
|
|
||||||
def __resize_2x(self):
|
def __rehash(self):
|
||||||
old_a = self._a
|
old_a = self._a
|
||||||
self._capacity *= 2
|
|
||||||
self._a = [None] * self._capacity
|
self._a = [None] * self._capacity
|
||||||
self._len = 0
|
self._len = 0
|
||||||
for kv in old_a:
|
for kv in old_a:
|
||||||
@ -2703,6 +2710,10 @@ def map(f, iterable):
|
|||||||
def zip(a, b):
|
def zip(a, b):
|
||||||
return [(a[i], b[i]) for i in range(min(len(a), len(b)))]
|
return [(a[i], b[i]) for i in range(min(len(a), len(b)))]
|
||||||
|
|
||||||
|
def reversed(iterable):
|
||||||
|
a = list(iterable)
|
||||||
|
return [a[i] for i in range(len(a)-1, -1, -1)]
|
||||||
|
|
||||||
def sorted(iterable, key=None, reverse=False):
|
def sorted(iterable, key=None, reverse=False):
|
||||||
if key is None:
|
if key is None:
|
||||||
key = lambda x: x
|
key = lambda x: x
|
||||||
@ -6626,15 +6637,6 @@ void __initializeBuiltinFunctions(VM* _vm) {
|
|||||||
return vm->PyList(vm->PyList_AS_C(args[0]));
|
return vm->PyList(vm->PyList_AS_C(args[0]));
|
||||||
});
|
});
|
||||||
|
|
||||||
_vm->bindMethod("list", "pop", [](VM* vm, const pkpy::ArgList& args) {
|
|
||||||
vm->__checkArgSize(args, 1, true);
|
|
||||||
PyVarList& _self = vm->PyList_AS_C(args[0]);
|
|
||||||
if(_self.empty()) vm->indexError("pop from empty list");
|
|
||||||
PyVar ret = _self.back();
|
|
||||||
_self.pop_back();
|
|
||||||
return ret;
|
|
||||||
});
|
|
||||||
|
|
||||||
_vm->bindMethod("list", "__add__", [](VM* vm, const pkpy::ArgList& args) {
|
_vm->bindMethod("list", "__add__", [](VM* vm, const pkpy::ArgList& args) {
|
||||||
const PyVarList& _self = vm->PyList_AS_C(args[0]);
|
const PyVarList& _self = vm->PyList_AS_C(args[0]);
|
||||||
const PyVarList& _obj = vm->PyList_AS_C(args[1]);
|
const PyVarList& _obj = vm->PyList_AS_C(args[1]);
|
||||||
|
@ -1 +1 @@
|
|||||||
Subproject commit 6a2bba868bc7d65869e8c754972b6d183516e94b
|
Subproject commit 5d14df87dcf63ba0ef6ed0da534ac9057ed5210c
|
@ -2545,6 +2545,13 @@ def __list4index(self, value):
|
|||||||
list.index = __list4index
|
list.index = __list4index
|
||||||
del __list4index
|
del __list4index
|
||||||
|
|
||||||
|
def __list4pop(self, i=-1):
|
||||||
|
res = self[i]
|
||||||
|
del self[i]
|
||||||
|
return res
|
||||||
|
list.pop = __list4pop
|
||||||
|
del __list4pop
|
||||||
|
|
||||||
def __list4__mul__(self, n):
|
def __list4__mul__(self, n):
|
||||||
a = []
|
a = []
|
||||||
for i in range(n):
|
for i in range(n):
|
||||||
@ -2587,8 +2594,8 @@ list.__new__ = lambda obj: [i for i in obj]
|
|||||||
|
|
||||||
# https://github.com/python/cpython/blob/main/Objects/dictobject.c
|
# https://github.com/python/cpython/blob/main/Objects/dictobject.c
|
||||||
class dict:
|
class dict:
|
||||||
def __init__(self):
|
def __init__(self, capacity=16):
|
||||||
self._capacity = 8
|
self._capacity = capacity
|
||||||
self._a = [None] * self._capacity
|
self._a = [None] * self._capacity
|
||||||
self._len = 0
|
self._len = 0
|
||||||
|
|
||||||
@ -2600,7 +2607,7 @@ class dict:
|
|||||||
while self._a[i] is not None:
|
while self._a[i] is not None:
|
||||||
if self._a[i][0] == key:
|
if self._a[i][0] == key:
|
||||||
return True, i
|
return True, i
|
||||||
i = ((5*i) + 1) % self._capacity
|
i = (i + 1) % self._capacity
|
||||||
return False, i
|
return False, i
|
||||||
|
|
||||||
def __getitem__(self, key):
|
def __getitem__(self, key):
|
||||||
@ -2620,8 +2627,9 @@ class dict:
|
|||||||
else:
|
else:
|
||||||
self._a[i] = [key, value]
|
self._a[i] = [key, value]
|
||||||
self._len += 1
|
self._len += 1
|
||||||
if self._len > self._capacity * 0.6:
|
if self._len > self._capacity * 0.8:
|
||||||
self.__resize_2x()
|
self._capacity *= 2
|
||||||
|
self.__rehash()
|
||||||
|
|
||||||
def __delitem__(self, key):
|
def __delitem__(self, key):
|
||||||
ok, i = self.__probe(key)
|
ok, i = self.__probe(key)
|
||||||
@ -2630,9 +2638,8 @@ class dict:
|
|||||||
self._a[i] = None
|
self._a[i] = None
|
||||||
self._len -= 1
|
self._len -= 1
|
||||||
|
|
||||||
def __resize_2x(self):
|
def __rehash(self):
|
||||||
old_a = self._a
|
old_a = self._a
|
||||||
self._capacity *= 2
|
|
||||||
self._a = [None] * self._capacity
|
self._a = [None] * self._capacity
|
||||||
self._len = 0
|
self._len = 0
|
||||||
for kv in old_a:
|
for kv in old_a:
|
||||||
@ -2703,6 +2710,10 @@ def map(f, iterable):
|
|||||||
def zip(a, b):
|
def zip(a, b):
|
||||||
return [(a[i], b[i]) for i in range(min(len(a), len(b)))]
|
return [(a[i], b[i]) for i in range(min(len(a), len(b)))]
|
||||||
|
|
||||||
|
def reversed(iterable):
|
||||||
|
a = list(iterable)
|
||||||
|
return [a[i] for i in range(len(a)-1, -1, -1)]
|
||||||
|
|
||||||
def sorted(iterable, key=None, reverse=False):
|
def sorted(iterable, key=None, reverse=False):
|
||||||
if key is None:
|
if key is None:
|
||||||
key = lambda x: x
|
key = lambda x: x
|
||||||
@ -6626,15 +6637,6 @@ void __initializeBuiltinFunctions(VM* _vm) {
|
|||||||
return vm->PyList(vm->PyList_AS_C(args[0]));
|
return vm->PyList(vm->PyList_AS_C(args[0]));
|
||||||
});
|
});
|
||||||
|
|
||||||
_vm->bindMethod("list", "pop", [](VM* vm, const pkpy::ArgList& args) {
|
|
||||||
vm->__checkArgSize(args, 1, true);
|
|
||||||
PyVarList& _self = vm->PyList_AS_C(args[0]);
|
|
||||||
if(_self.empty()) vm->indexError("pop from empty list");
|
|
||||||
PyVar ret = _self.back();
|
|
||||||
_self.pop_back();
|
|
||||||
return ret;
|
|
||||||
});
|
|
||||||
|
|
||||||
_vm->bindMethod("list", "__add__", [](VM* vm, const pkpy::ArgList& args) {
|
_vm->bindMethod("list", "__add__", [](VM* vm, const pkpy::ArgList& args) {
|
||||||
const PyVarList& _self = vm->PyList_AS_C(args[0]);
|
const PyVarList& _self = vm->PyList_AS_C(args[0]);
|
||||||
const PyVarList& _obj = vm->PyList_AS_C(args[1]);
|
const PyVarList& _obj = vm->PyList_AS_C(args[1]);
|
||||||
|
@ -113,8 +113,8 @@ list.__new__ = lambda obj: [i for i in obj]
|
|||||||
|
|
||||||
# https://github.com/python/cpython/blob/main/Objects/dictobject.c
|
# https://github.com/python/cpython/blob/main/Objects/dictobject.c
|
||||||
class dict:
|
class dict:
|
||||||
def __init__(self):
|
def __init__(self, capacity=16):
|
||||||
self._capacity = 8
|
self._capacity = capacity
|
||||||
self._a = [None] * self._capacity
|
self._a = [None] * self._capacity
|
||||||
self._len = 0
|
self._len = 0
|
||||||
|
|
||||||
@ -126,7 +126,7 @@ class dict:
|
|||||||
while self._a[i] is not None:
|
while self._a[i] is not None:
|
||||||
if self._a[i][0] == key:
|
if self._a[i][0] == key:
|
||||||
return True, i
|
return True, i
|
||||||
i = ((5*i) + 1) % self._capacity
|
i = (i + 1) % self._capacity
|
||||||
return False, i
|
return False, i
|
||||||
|
|
||||||
def __getitem__(self, key):
|
def __getitem__(self, key):
|
||||||
@ -146,8 +146,9 @@ class dict:
|
|||||||
else:
|
else:
|
||||||
self._a[i] = [key, value]
|
self._a[i] = [key, value]
|
||||||
self._len += 1
|
self._len += 1
|
||||||
if self._len > self._capacity * 0.6:
|
if self._len > self._capacity * 0.8:
|
||||||
self.__resize_2x()
|
self._capacity *= 2
|
||||||
|
self.__rehash()
|
||||||
|
|
||||||
def __delitem__(self, key):
|
def __delitem__(self, key):
|
||||||
ok, i = self.__probe(key)
|
ok, i = self.__probe(key)
|
||||||
@ -156,9 +157,8 @@ class dict:
|
|||||||
self._a[i] = None
|
self._a[i] = None
|
||||||
self._len -= 1
|
self._len -= 1
|
||||||
|
|
||||||
def __resize_2x(self):
|
def __rehash(self):
|
||||||
old_a = self._a
|
old_a = self._a
|
||||||
self._capacity *= 2
|
|
||||||
self._a = [None] * self._capacity
|
self._a = [None] * self._capacity
|
||||||
self._len = 0
|
self._len = 0
|
||||||
for kv in old_a:
|
for kv in old_a:
|
||||||
|
Loading…
x
Reference in New Issue
Block a user