Skip to content

Commit c26c268

Browse files
committed
dictview add support for mixed set oprations
- with unittest
1 parent bf00ab3 commit c26c268

File tree

3 files changed

+83
-3
lines changed

3 files changed

+83
-3
lines changed

graalpython/com.oracle.graal.python.test/src/tests/test_dict.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -330,3 +330,16 @@ def test_dictview_set_operations_on_items():
330330
assert k1 | k2 == {(1, 1), (2, 2), (3, 3)}
331331
assert k1 ^ k2 == {(3, 3)}
332332
assert k1 ^ k3 == {(1, 1), (2, 2), (4, 4)}
333+
334+
335+
def test_dictview_mixed_set_operations():
336+
# Just a few for .keys()
337+
assert {1: 1}.keys() == {1}
338+
assert {1} == {1: 1}.keys()
339+
assert {1: 1}.keys() | {2} == {1, 2}
340+
assert {2} | {1: 1}.keys() == {1, 2}
341+
# And a few for .items()
342+
assert {1: 1}.items() == {(1, 1)}
343+
assert {(1, 1)} == {1: 1}.items()
344+
assert {1: 1}.items() | {2} == {(1, 1), 2}
345+
assert {2} | {1: 1}.items() == {(1, 1), 2}

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/dict/DictViewBuiltins.java

Lines changed: 62 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -182,9 +182,25 @@ Object doGeneric(Object self, Object other) {
182182
@GenerateNodeFactory
183183
abstract static class SubNode extends PythonBinaryBuiltinNode {
184184
@Specialization
185-
PBaseSet doKeysView(PDictKeysView left, PDictKeysView right,
185+
PBaseSet doKeysView(PDictKeysView self, PBaseSet other,
186186
@Cached("create()") HashingStorageNodes.DiffNode diffNode) {
187-
HashingStorage storage = diffNode.execute(left.getDict().getDictStorage(), right.getDict().getDictStorage());
187+
HashingStorage storage = diffNode.execute(self.getDict().getDictStorage(), other.getDictStorage());
188+
return factory().createSet(storage);
189+
}
190+
191+
@Specialization
192+
PBaseSet doKeysView(PDictKeysView self, PDictKeysView other,
193+
@Cached("create()") HashingStorageNodes.DiffNode diffNode) {
194+
HashingStorage storage = diffNode.execute(self.getDict().getDictStorage(), other.getDict().getDictStorage());
195+
return factory().createSet(storage);
196+
}
197+
198+
@Specialization
199+
PBaseSet doItemsView(PDictItemsView self, PBaseSet other,
200+
@Cached("create()") HashingStorageNodes.DiffNode diffNode,
201+
@Cached("create()") SetNodes.ConstructSetNode constructSetNode) {
202+
PSet selfSet = constructSetNode.executeWith(self);
203+
HashingStorage storage = diffNode.execute(selfSet.getDictStorage(), other.getDictStorage());
188204
return factory().createSet(storage);
189205
}
190206

@@ -202,13 +218,29 @@ PBaseSet doItemsView(PDictItemsView self, PDictItemsView other,
202218
@Builtin(name = __AND__, fixedNumOfArguments = 2)
203219
@GenerateNodeFactory
204220
abstract static class AndNode extends PythonBinaryBuiltinNode {
221+
@Specialization
222+
PBaseSet doKeysView(PDictKeysView self, PBaseSet other,
223+
@Cached("create()") HashingStorageNodes.IntersectNode intersectNode) {
224+
HashingStorage intersectedStorage = intersectNode.execute(self.getDict().getDictStorage(), other.getDictStorage());
225+
return factory().createSet(intersectedStorage);
226+
}
227+
205228
@Specialization
206229
PBaseSet doKeysView(PDictKeysView self, PDictKeysView other,
207230
@Cached("create()") HashingStorageNodes.IntersectNode intersectNode) {
208231
HashingStorage intersectedStorage = intersectNode.execute(self.getDict().getDictStorage(), other.getDict().getDictStorage());
209232
return factory().createSet(intersectedStorage);
210233
}
211234

235+
@Specialization
236+
PBaseSet doItemsView(PDictItemsView self, PBaseSet other,
237+
@Cached("create()") HashingStorageNodes.IntersectNode intersectNode,
238+
@Cached("create()") SetNodes.ConstructSetNode constructSetNode) {
239+
PSet selfSet = constructSetNode.executeWith(self);
240+
HashingStorage intersectedStorage = intersectNode.execute(selfSet.getDictStorage(), other.getDictStorage());
241+
return factory().createSet(intersectedStorage);
242+
}
243+
212244
@Specialization
213245
PBaseSet doItemsView(PDictItemsView self, PDictItemsView other,
214246
@Cached("create()") HashingStorageNodes.IntersectNode intersectNode,
@@ -223,12 +255,26 @@ PBaseSet doItemsView(PDictItemsView self, PDictItemsView other,
223255
@Builtin(name = __OR__, fixedNumOfArguments = 2)
224256
@GenerateNodeFactory
225257
public abstract static class OrNode extends PythonBuiltinNode {
258+
@Specialization
259+
PBaseSet doKeysView(PDictKeysView self, PBaseSet other,
260+
@Cached("create()") HashingStorageNodes.UnionNode unionNode) {
261+
return factory().createSet(unionNode.execute(self.getDict().getDictStorage(), other.getDictStorage()));
262+
}
263+
226264
@Specialization
227265
PBaseSet doKeysView(PDictKeysView self, PDictKeysView other,
228266
@Cached("create()") HashingStorageNodes.UnionNode unionNode) {
229267
return factory().createSet(unionNode.execute(self.getDict().getDictStorage(), other.getDict().getDictStorage()));
230268
}
231269

270+
@Specialization
271+
PBaseSet doItemsView(PDictItemsView self, PBaseSet other,
272+
@Cached("create()") HashingStorageNodes.UnionNode unionNode,
273+
@Cached("create()") SetNodes.ConstructSetNode constructSetNode) {
274+
PSet selfSet = constructSetNode.executeWith(self);
275+
return factory().createSet(unionNode.execute(selfSet.getDictStorage(), other.getDictStorage()));
276+
}
277+
232278
@Specialization
233279
PBaseSet doItemsView(PDictItemsView self, PDictItemsView other,
234280
@Cached("create()") HashingStorageNodes.UnionNode unionNode,
@@ -242,12 +288,26 @@ PBaseSet doItemsView(PDictItemsView self, PDictItemsView other,
242288
@Builtin(name = __XOR__, fixedNumOfArguments = 2)
243289
@GenerateNodeFactory
244290
public abstract static class XorNode extends PythonBuiltinNode {
291+
@Specialization
292+
PBaseSet doKeysView(PDictKeysView self, PBaseSet other,
293+
@Cached("create()") HashingStorageNodes.ExclusiveOrNode xorNode) {
294+
return factory().createSet(xorNode.execute(self.getDict().getDictStorage(), other.getDictStorage()));
295+
}
296+
245297
@Specialization
246298
PBaseSet doKeysView(PDictKeysView self, PDictKeysView other,
247299
@Cached("create()") HashingStorageNodes.ExclusiveOrNode xorNode) {
248300
return factory().createSet(xorNode.execute(self.getDict().getDictStorage(), other.getDict().getDictStorage()));
249301
}
250302

303+
@Specialization
304+
PBaseSet doItemsView(PDictItemsView self, PBaseSet other,
305+
@Cached("create()") HashingStorageNodes.ExclusiveOrNode xorNode,
306+
@Cached("create()") SetNodes.ConstructSetNode constructSetNode) {
307+
PSet selfSet = constructSetNode.executeWith(self);
308+
return factory().createSet(xorNode.execute(selfSet.getDictStorage(), other.getDictStorage()));
309+
}
310+
251311
@Specialization
252312
PBaseSet doItemsView(PDictItemsView self, PDictItemsView other,
253313
@Cached("create()") HashingStorageNodes.ExclusiveOrNode xorNode,

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/set/SetBuiltins.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
import com.oracle.graal.python.builtins.PythonBuiltins;
3737
import com.oracle.graal.python.builtins.objects.PNone;
3838
import com.oracle.graal.python.builtins.objects.common.HashingStorageNodes;
39+
import com.oracle.graal.python.nodes.call.special.LookupAndCallBinaryNode;
3940
import com.oracle.graal.python.nodes.function.PythonBuiltinBaseNode;
4041
import com.oracle.graal.python.nodes.function.builtins.PythonBinaryBuiltinNode;
4142
import com.oracle.graal.python.nodes.function.builtins.PythonUnaryBuiltinNode;
@@ -88,12 +89,18 @@ Object doGeneric(Object self) {
8889

8990
@Builtin(name = __OR__, fixedNumOfArguments = 2)
9091
@GenerateNodeFactory
91-
public abstract static class SetOrNode extends PythonBinaryBuiltinNode {
92+
public abstract static class OrNode extends PythonBinaryBuiltinNode {
9293
@Specialization
9394
Object doSet(PBaseSet self, PBaseSet other,
9495
@Cached("create()") HashingStorageNodes.UnionNode unionNode) {
9596
return factory().createSet(unionNode.execute(self.getDictStorage(), other.getDictStorage()));
9697
}
98+
99+
@Specialization
100+
Object doReverse(PBaseSet self, Object other,
101+
@Cached("create(__OR__)") LookupAndCallBinaryNode callOr) {
102+
return callOr.executeObject(other, self);
103+
}
97104
}
98105

99106
@Builtin(name = "remove", fixedNumOfArguments = 2)

0 commit comments

Comments
 (0)