From b15066f458c2b2805d2f4e8a50574d36f605551c Mon Sep 17 00:00:00 2001 From: blueloveTH Date: Sat, 12 Nov 2022 21:29:16 +0800 Subject: [PATCH] add hash support for tuple --- src/vm.h | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/src/vm.h b/src/vm.h index 37769daf..e2bdeca3 100644 --- a/src/vm.h +++ b/src/vm.h @@ -690,6 +690,16 @@ public: } if (obj->isType(_tp_str)) return PyStr_AS_C(obj).hash(); 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()); return 0; }