|
26 | 26 | package com.oracle.graal.python.builtins.objects.set;
|
27 | 27 |
|
28 | 28 | import static com.oracle.graal.python.nodes.SpecialMethodNames.__AND__;
|
| 29 | +import static com.oracle.graal.python.nodes.SpecialMethodNames.__OR__; |
29 | 30 | import static com.oracle.graal.python.nodes.SpecialMethodNames.__CONTAINS__;
|
30 | 31 | import static com.oracle.graal.python.nodes.SpecialMethodNames.__EQ__;
|
31 | 32 | import static com.oracle.graal.python.nodes.SpecialMethodNames.__GE__;
|
|
73 | 74 | import com.oracle.truffle.api.dsl.GenerateNodeFactory;
|
74 | 75 | import com.oracle.truffle.api.dsl.NodeFactory;
|
75 | 76 | import com.oracle.truffle.api.dsl.Specialization;
|
| 77 | +import com.oracle.truffle.api.nodes.Node; |
76 | 78 | import com.oracle.truffle.api.profiles.ConditionProfile;
|
77 | 79 | import com.oracle.truffle.api.profiles.ValueProfile;
|
78 | 80 |
|
@@ -208,7 +210,67 @@ PBaseSet doPBaseSet(PFrozenSet left, PBaseSet right) {
|
208 | 210 |
|
209 | 211 | @Fallback
|
210 | 212 | Object doAnd(Object self, Object other) {
|
211 |
| - throw raise(PythonErrorType.TypeError, "unsupported operand type(s) for &=: '%p' and '%p'", self, other); |
| 213 | + throw raise(PythonErrorType.TypeError, "unsupported operand type(s) for &: '%p' and '%p'", self, other); |
| 214 | + } |
| 215 | + } |
| 216 | + |
| 217 | + @Builtin(name = __OR__, fixedNumOfPositionalArgs = 2) |
| 218 | + @GenerateNodeFactory |
| 219 | + abstract static class OrNode extends PythonBinaryBuiltinNode { |
| 220 | + @Node.Child private HashingStorageNodes.UnionNode unionNode; |
| 221 | + @Node.Child private HashingStorageNodes.SetItemNode setItemNode; |
| 222 | + |
| 223 | + private HashingStorageNodes.SetItemNode getSetItemNode() { |
| 224 | + if (setItemNode == null) { |
| 225 | + CompilerDirectives.transferToInterpreterAndInvalidate(); |
| 226 | + setItemNode = insert(HashingStorageNodes.SetItemNode.create()); |
| 227 | + } |
| 228 | + return setItemNode; |
| 229 | + } |
| 230 | + |
| 231 | + @TruffleBoundary |
| 232 | + private HashingStorage getStringAsHashingStorage(String str) { |
| 233 | + HashingStorage storage = EconomicMapStorage.create(str.length(), true); |
| 234 | + for (int i = 0; i < str.length(); i++) { |
| 235 | + String key = String.valueOf(str.charAt(i)); |
| 236 | + getSetItemNode().execute(storage, key, PNone.NO_VALUE); |
| 237 | + } |
| 238 | + return storage; |
| 239 | + } |
| 240 | + |
| 241 | + @Specialization |
| 242 | + PBaseSet doPBaseSet(PSet left, String right) { |
| 243 | + return factory().createSet(getUnionNode().execute(left.getDictStorage(), getStringAsHashingStorage(right))); |
| 244 | + } |
| 245 | + |
| 246 | + @Specialization |
| 247 | + PBaseSet doPBaseSet(PFrozenSet left, String right) { |
| 248 | + return factory().createFrozenSet(getUnionNode().execute(left.getDictStorage(), getStringAsHashingStorage(right))); |
| 249 | + } |
| 250 | + |
| 251 | + private HashingStorageNodes.UnionNode getUnionNode() { |
| 252 | + if (unionNode == null) { |
| 253 | + CompilerDirectives.transferToInterpreterAndInvalidate(); |
| 254 | + unionNode = insert(HashingStorageNodes.UnionNode.create()); |
| 255 | + } |
| 256 | + return unionNode; |
| 257 | + } |
| 258 | + |
| 259 | + @Specialization |
| 260 | + PBaseSet doPBaseSet(PSet left, PBaseSet right) { |
| 261 | + HashingStorage intersectedStorage = getUnionNode().execute(left.getDictStorage(), right.getDictStorage()); |
| 262 | + return factory().createSet(intersectedStorage); |
| 263 | + } |
| 264 | + |
| 265 | + @Specialization |
| 266 | + PBaseSet doPBaseSet(PFrozenSet left, PBaseSet right) { |
| 267 | + HashingStorage intersectedStorage = getUnionNode().execute(left.getDictStorage(), right.getDictStorage()); |
| 268 | + return factory().createFrozenSet(intersectedStorage); |
| 269 | + } |
| 270 | + |
| 271 | + @Fallback |
| 272 | + Object doAnd(Object self, Object other) { |
| 273 | + throw raise(PythonErrorType.TypeError, "unsupported operand type(s) for |: '%p' and '%p'", self, other); |
212 | 274 | }
|
213 | 275 | }
|
214 | 276 |
|
|
0 commit comments