mirror of
https://github.com/pocketpy/pocketpy
synced 2025-11-11 06:00:16 +00:00
Compare commits
No commits in common. "648c1bedb4ce6980d17167f2f88b40997585ebbc" and "7135e1ce59239a88bcf664170ed63a617292a799" have entirely different histories.
648c1bedb4
...
7135e1ce59
@ -22,11 +22,12 @@
|
|||||||
#define K c11_sv
|
#define K c11_sv
|
||||||
#define V int
|
#define V int
|
||||||
#define NAME c11_smallmap_v2d
|
#define NAME c11_smallmap_v2d
|
||||||
#define less(a, b) (c11_sv__cmp((a), (b)) < 0)
|
#define less(a, b) (c11_sv__cmp((a), (b)) < 0)
|
||||||
#define equal(a, b) c11__sveq((a), (b))
|
#define equal(a, b) (c11_sv__cmp((a), (b)) == 0)
|
||||||
#include "pocketpy/xmacros/smallmap.h"
|
#include "pocketpy/xmacros/smallmap.h"
|
||||||
#undef SMALLMAP_T__HEADER
|
#undef SMALLMAP_T__HEADER
|
||||||
|
|
||||||
|
|
||||||
#define SMALLMAP_T__HEADER
|
#define SMALLMAP_T__HEADER
|
||||||
#define K void*
|
#define K void*
|
||||||
#define V py_i64
|
#define V py_i64
|
||||||
|
|||||||
@ -136,17 +136,16 @@ static void Dict__set_index(Dict* self, uint32_t index, uint32_t value) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Dict__probe won't raise exception for string keys
|
|
||||||
static bool Dict__probe(Dict* self,
|
static bool Dict__probe(Dict* self,
|
||||||
py_TValue* key,
|
py_TValue* key,
|
||||||
uint64_t* p_hash,
|
uint64_t* p_hash,
|
||||||
uint32_t* p_idx,
|
uint32_t* p_idx,
|
||||||
DictEntry** p_entry) {
|
DictEntry** p_entry) {
|
||||||
|
py_i64 h_user;
|
||||||
|
if(!py_hash(key, &h_user)) return false;
|
||||||
if(py_isstr(key)) {
|
if(py_isstr(key)) {
|
||||||
*p_hash = c11_sv__hash(py_tosv(key));
|
*p_hash = (uint64_t)h_user;
|
||||||
} else {
|
} else {
|
||||||
py_i64 h_user;
|
|
||||||
if(!py_hash(key, &h_user)) return false;
|
|
||||||
*p_hash = Dict__hash_2nd((uint64_t)h_user);
|
*p_hash = Dict__hash_2nd((uint64_t)h_user);
|
||||||
}
|
}
|
||||||
uint32_t mask = self->capacity - 1;
|
uint32_t mask = self->capacity - 1;
|
||||||
@ -156,23 +155,13 @@ static bool Dict__probe(Dict* self,
|
|||||||
if(idx2 == self->null_index_value) break;
|
if(idx2 == self->null_index_value) break;
|
||||||
DictEntry* entry = c11__at(DictEntry, &self->entries, idx2);
|
DictEntry* entry = c11__at(DictEntry, &self->entries, idx2);
|
||||||
if(entry->hash == (*p_hash)) {
|
if(entry->hash == (*p_hash)) {
|
||||||
if(py_isstr(&entry->key) && py_isstr(key)) {
|
int res = py_equal(&entry->key, key);
|
||||||
c11_sv lhs = py_tosv(&entry->key);
|
if(res == 1) {
|
||||||
c11_sv rhs = py_tosv(key);
|
*p_idx = idx;
|
||||||
if(c11__sveq(lhs, rhs)) {
|
*p_entry = entry;
|
||||||
*p_idx = idx;
|
return true;
|
||||||
*p_entry = entry;
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
int res = py_equal(&entry->key, key);
|
|
||||||
if(res == 1) {
|
|
||||||
*p_idx = idx;
|
|
||||||
*p_entry = entry;
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
if(res == -1) return false; // error
|
|
||||||
}
|
}
|
||||||
|
if(res == -1) return false; // error
|
||||||
}
|
}
|
||||||
// try next index
|
// try next index
|
||||||
idx = Dict__step(idx);
|
idx = Dict__step(idx);
|
||||||
@ -363,12 +352,11 @@ void py_newdict(py_OutRef out) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
static bool dict__init__(int argc, py_Ref argv) {
|
static bool dict__init__(int argc, py_Ref argv) {
|
||||||
|
py_newnone(py_retval());
|
||||||
if(argc > 2) return TypeError("dict.__init__() takes at most 2 arguments (%d given)", argc);
|
if(argc > 2) return TypeError("dict.__init__() takes at most 2 arguments (%d given)", argc);
|
||||||
if(argc == 1) {
|
if(argc == 1) return true;
|
||||||
py_newnone(py_retval());
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
assert(argc == 2);
|
assert(argc == 2);
|
||||||
|
|
||||||
py_TValue* p;
|
py_TValue* p;
|
||||||
int length = pk_arrayview(py_arg(1), &p);
|
int length = pk_arrayview(py_arg(1), &p);
|
||||||
if(length == -1) { return TypeError("dict.__init__() expects a list or tuple"); }
|
if(length == -1) { return TypeError("dict.__init__() expects a list or tuple"); }
|
||||||
@ -383,7 +371,6 @@ static bool dict__init__(int argc, py_Ref argv) {
|
|||||||
py_Ref val = py_tuple_getitem(tuple, 1);
|
py_Ref val = py_tuple_getitem(tuple, 1);
|
||||||
if(!Dict__set(self, key, val)) return false;
|
if(!Dict__set(self, key, val)) return false;
|
||||||
}
|
}
|
||||||
py_newnone(py_retval());
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -405,9 +392,7 @@ static bool dict__getitem__(int argc, py_Ref argv) {
|
|||||||
static bool dict__setitem__(int argc, py_Ref argv) {
|
static bool dict__setitem__(int argc, py_Ref argv) {
|
||||||
PY_CHECK_ARGC(3);
|
PY_CHECK_ARGC(3);
|
||||||
Dict* self = py_touserdata(argv);
|
Dict* self = py_touserdata(argv);
|
||||||
bool ok = Dict__set(self, py_arg(1), py_arg(2));
|
return Dict__set(self, py_arg(1), py_arg(2));
|
||||||
if(ok) py_newnone(py_retval());
|
|
||||||
return ok;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static bool dict__delitem__(int argc, py_Ref argv) {
|
static bool dict__delitem__(int argc, py_Ref argv) {
|
||||||
|
|||||||
@ -226,15 +226,3 @@ def finally_return():
|
|||||||
assert finally_return() == 1
|
assert finally_return() == 1
|
||||||
|
|
||||||
|
|
||||||
# nested try
|
|
||||||
def g():
|
|
||||||
try:
|
|
||||||
raise KeyError
|
|
||||||
except KeyError:
|
|
||||||
pass
|
|
||||||
|
|
||||||
try:
|
|
||||||
raise IndexError
|
|
||||||
except IndexError:
|
|
||||||
g()
|
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user