mirror of
https://github.com/pocketpy/pocketpy
synced 2025-10-20 11:30:18 +00:00
add hash support for tuple
This commit is contained in:
parent
a5cea1e0a6
commit
b15066f458
10
src/vm.h
10
src/vm.h
@ -690,6 +690,16 @@ public:
|
|||||||
}
|
}
|
||||||
if (obj->isType(_tp_str)) return PyStr_AS_C(obj).hash();
|
if (obj->isType(_tp_str)) return PyStr_AS_C(obj).hash();
|
||||||
if (obj->isType(_tp_type)) return (_Int)obj.get();
|
if (obj->isType(_tp_type)) return (_Int)obj.get();
|
||||||
|
if (obj->isType(_tp_tuple)) {
|
||||||
|
_Int x = 1000003;
|
||||||
|
for (const auto& item : PyTuple_AS_C(obj)) {
|
||||||
|
_Int y = hash(item);
|
||||||
|
// this is recommended by Github Copilot
|
||||||
|
// i am not sure whether it is a good idea
|
||||||
|
x = x ^ (y + 0x9e3779b9 + (x << 6) + (x >> 2));
|
||||||
|
}
|
||||||
|
return x;
|
||||||
|
}
|
||||||
typeError("unhashable type: " + obj->getTypeName());
|
typeError("unhashable type: " + obj->getTypeName());
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user