Compare commits

...

5 Commits

Author SHA1 Message Date
blueloveTH
ea81d3539d Update 80_linalg.py 2024-11-23 02:31:01 +08:00
blueloveTH
1634defbb2 ... 2024-11-23 02:16:52 +08:00
blueloveTH
8d41987eb8 Update py_dict.c 2024-11-23 02:04:48 +08:00
blueloveTH
5847910c78 Update linalg.c 2024-11-23 02:01:09 +08:00
blueloveTH
b31795bf83 fix vec2i.__hash__ 2024-11-23 01:53:56 +08:00
4 changed files with 22 additions and 5 deletions

View File

@ -57,6 +57,7 @@ static bool disassemble(CodeObject* co) {
c11_sbuf__write_int(&ss, byte.arg); c11_sbuf__write_int(&ss, byte.arg);
switch(byte.op) { switch(byte.op) {
// TODO: see `dis.py` there is a memory issue
case OP_LOAD_CONST: { case OP_LOAD_CONST: {
py_Ref value = c11__at(py_TValue, &co->consts, byte.arg); py_Ref value = c11__at(py_TValue, &co->consts, byte.arg);
if(py_repr(value)) { if(py_repr(value)) {

View File

@ -301,11 +301,12 @@ DEF_VECTOR_OPS(3)
} \ } \
static bool vec##D##i##__hash__(int argc, py_Ref argv) { \ static bool vec##D##i##__hash__(int argc, py_Ref argv) { \
PY_CHECK_ARGC(1); \ PY_CHECK_ARGC(1); \
const uint32_t C = 2654435761; \
c11_vec##D##i v = py_tovec##D##i(argv); \ c11_vec##D##i v = py_tovec##D##i(argv); \
py_i64 hash = 0; \ uint64_t hash = 0; \
for(int i = 0; i < D; i++) \ for(int i = 0; i < D; i++) \
hash = hash * 31 + v.data[i]; \ hash = hash * 31 + (uint32_t)v.data[i] * C; \
py_newint(py_retval(), hash); \ py_newint(py_retval(), (py_i64)hash); \
return true; \ return true; \
} }

View File

@ -5,7 +5,7 @@
#include "pocketpy/objects/object.h" #include "pocketpy/objects/object.h"
#include "pocketpy/interpreter/vm.h" #include "pocketpy/interpreter/vm.h"
#define PK_DICT_MAX_COLLISION 3 #define PK_DICT_MAX_COLLISION 4
typedef struct { typedef struct {
py_i64 hash; py_i64 hash;
@ -74,7 +74,6 @@ static void Dict__rehash_2x(Dict* self) {
Dict old_dict = *self; Dict old_dict = *self;
int new_capacity = self->capacity * 2; int new_capacity = self->capacity * 2;
assert(new_capacity <= 1073741824);
do { do {
Dict__ctor(self, new_capacity); Dict__ctor(self, new_capacity);
@ -159,6 +158,9 @@ static bool Dict__set(Dict* self, py_TValue* key, py_TValue* val) {
if(res == -1) return false; // error if(res == -1) return false; // error
} }
// no empty slot found // no empty slot found
if(self->capacity >= self->entries.length * 10) {
return RuntimeError("dict has too much collision: %d/%d", self->entries.length, self->capacity);
}
Dict__rehash_2x(self); Dict__rehash_2x(self);
return Dict__set(self, key, val); return Dict__set(self, key, val);
} }

View File

@ -407,3 +407,16 @@ x, y = vec2(3.0, 4.0)
assert x == 3.0 and y == 4.0 assert x == 3.0 and y == 4.0
x, y, z = vec3(1.0, 2.0, 3.0) x, y, z = vec3(1.0, 2.0, 3.0)
assert x == 1.0 and y == 2.0 and z == 3.0 assert x == 1.0 and y == 2.0 and z == 3.0
d = {vec2i(12, 12): 1035.313708305359, vec2i(12, 11): 2059.313708305359, vec2i(12, 13): 2059.313708305359, vec2i(11, 12): 2059.313708305359, vec2i(13, 12): 2059.313708305359, vec2i(13, 11): 3083.313708305359, vec2i(13, 13): 3083.313708305359, vec2i(14, 12): 3083.313708305359, vec2i(12, 14): 3083.313708305359, vec2i(11, 13): 3083.313708305359, vec2i(12, 10): 3083.313708305359, vec2i(11, 11): 3083.313708305359, vec2i(10, 12): 3083.313708305359, vec2i(13, 14): 4107.313708305359, vec2i(14, 13): 4107.313708305359, vec2i(14, 11): 4107.313708305359, vec2i(15, 12): 4107.313708305359, vec2i(12, 15): 4107.313708305359, vec2i(11, 14): 4107.313708305359, vec2i(13, 10): 4107.313708305359, vec2i(10, 13): 4107.313708305359, vec2i(11, 10): 4107.313708305359, vec2i(10, 11): 4107.313708305359, vec2i(12, 9): 4107.313708305359, vec2i(9, 12): 4107.313708305359, vec2i(14, 14): 5131.313708305359, vec2i(15, 13): 5131.313708305359, vec2i(13, 15): 5131.313708305359, vec2i(15, 11): 5131.313708305359, vec2i(16, 12): 5131.313708305359, vec2i(12, 16): 5131.313708305359, vec2i(11, 15): 5131.313708305359, vec2i(14, 10): 5131.313708305359, vec2i(10, 14): 5131.313708305359, vec2i(13, 9): 5131.313708305359, vec2i(9, 13): 5131.313708305359}
d[vec2i(11, 9)] = 1
e = {}
for i in range(10000):
e[vec2i(12, i)] = i
e[vec2i(11, i)] = i
e[vec2i(13, i)] = i
e[vec2i(i, 12)] = i
e[vec2i(i, 11)] = i
e[vec2i(i, 13)] = i