59
59
import com .oracle .graal .python .builtins .objects .dict .PDictView .PDictItemsView ;
60
60
import com .oracle .graal .python .builtins .objects .dict .PDictView .PDictKeysView ;
61
61
import com .oracle .graal .python .builtins .objects .set .PBaseSet ;
62
+ import com .oracle .graal .python .builtins .objects .set .PSet ;
63
+ import com .oracle .graal .python .builtins .objects .set .SetNodes ;
64
+ import com .oracle .graal .python .builtins .objects .tuple .PTuple ;
62
65
import com .oracle .graal .python .nodes .function .PythonBuiltinBaseNode ;
63
66
import com .oracle .graal .python .nodes .function .PythonBuiltinNode ;
64
67
import com .oracle .graal .python .nodes .function .builtins .PythonBinaryBuiltinNode ;
68
71
import com .oracle .truffle .api .dsl .GenerateNodeFactory ;
69
72
import com .oracle .truffle .api .dsl .NodeFactory ;
70
73
import com .oracle .truffle .api .dsl .Specialization ;
74
+ import com .oracle .truffle .api .profiles .ConditionProfile ;
71
75
72
76
@ CoreFunctions (extendClasses = {PDictKeysView .class , PDictItemsView .class })
73
77
public final class DictViewBuiltins extends PythonBuiltins {
@@ -116,10 +120,23 @@ boolean containsEmpty(PDictView self, Object key) {
116
120
}
117
121
118
122
@ Specialization
119
- boolean contains (PDictView self , Object key ,
123
+ boolean contains (PDictKeysView self , Object key ,
120
124
@ Cached ("create()" ) HashingStorageNodes .ContainsKeyNode containsKeyNode ) {
121
125
return containsKeyNode .execute (self .getDict ().getDictStorage (), key );
122
126
}
127
+
128
+ @ Specialization
129
+ boolean contains (PDictItemsView self , PTuple key ,
130
+ @ Cached ("create()" ) HashingStorageNodes .GetItemNode getItemNode ,
131
+ @ Cached ("create()" ) HashingStorageNodes .PythonEquivalence equivalenceNode ,
132
+ @ Cached ("createBinaryProfile()" ) ConditionProfile tupleLenProfile ) {
133
+ if (tupleLenProfile .profile (key .len () != 2 )) {
134
+ return false ;
135
+ }
136
+ HashingStorage dictStorage = self .getDict ().getDictStorage ();
137
+ Object value = getItemNode .execute (dictStorage , key .getItem (0 ));
138
+ return value != null && equivalenceNode .equals (value , key .getItem (1 ));
139
+ }
123
140
}
124
141
125
142
@ Builtin (name = __EQ__ , fixedNumOfArguments = 2 )
@@ -133,11 +150,27 @@ boolean doKeysView(PDictKeysView self, PDictKeysView other,
133
150
}
134
151
135
152
@ Specialization
136
- boolean doItemsView (PDictKeysView self , PBaseSet other ,
153
+ boolean doKeysView (PDictKeysView self , PBaseSet other ,
137
154
@ Cached ("create()" ) HashingStorageNodes .KeysEqualsNode equalsNode ) {
138
155
return equalsNode .execute (self .getDict ().getDictStorage (), other .getDictStorage ());
139
156
}
140
157
158
+ @ Specialization
159
+ boolean doItemsView (PDictItemsView self , PDictItemsView other ,
160
+ @ Cached ("create()" ) HashingStorageNodes .EqualsNode equalsNode ) {
161
+ // the items view stores the original dict with K:V pairs so full K:V equality needs to
162
+ // be tested in this case
163
+ return equalsNode .execute (self .getDict ().getDictStorage (), other .getDict ().getDictStorage ());
164
+ }
165
+
166
+ @ Specialization
167
+ boolean doItemsView (PDictItemsView self , PBaseSet other ,
168
+ @ Cached ("create()" ) SetNodes .ConstructSetNode constructSetNode ,
169
+ @ Cached ("create()" ) HashingStorageNodes .KeysEqualsNode equalsNode ) {
170
+ PSet selfSet = constructSetNode .executeWith (self );
171
+ return equalsNode .execute (selfSet .getDictStorage (), other .getDictStorage ());
172
+ }
173
+
141
174
@ Fallback
142
175
@ SuppressWarnings ("unused" )
143
176
Object doGeneric (Object self , Object other ) {
@@ -149,16 +182,19 @@ Object doGeneric(Object self, Object other) {
149
182
@ GenerateNodeFactory
150
183
abstract static class SubNode extends PythonBinaryBuiltinNode {
151
184
@ Specialization
152
- PBaseSet doItemsView ( PDictItemsView left , PDictItemsView right ,
185
+ PBaseSet doKeysView ( PDictKeysView left , PDictKeysView right ,
153
186
@ Cached ("create()" ) HashingStorageNodes .DiffNode diffNode ) {
154
187
HashingStorage storage = diffNode .execute (left .getDict ().getDictStorage (), right .getDict ().getDictStorage ());
155
188
return factory ().createSet (storage );
156
189
}
157
190
158
191
@ Specialization
159
- PBaseSet doKeysView (PDictKeysView left , PDictKeysView right ,
160
- @ Cached ("create()" ) HashingStorageNodes .DiffNode diffNode ) {
161
- HashingStorage storage = diffNode .execute (left .getDict ().getDictStorage (), right .getDict ().getDictStorage ());
192
+ PBaseSet doItemsView (PDictItemsView self , PDictItemsView other ,
193
+ @ Cached ("create()" ) HashingStorageNodes .DiffNode diffNode ,
194
+ @ Cached ("create()" ) SetNodes .ConstructSetNode constructSetNode ) {
195
+ PSet selfSet = constructSetNode .executeWith (self );
196
+ PSet otherSet = constructSetNode .executeWith (other );
197
+ HashingStorage storage = diffNode .execute (selfSet .getDictStorage (), otherSet .getDictStorage ());
162
198
return factory ().createSet (storage );
163
199
}
164
200
}
@@ -167,16 +203,19 @@ PBaseSet doKeysView(PDictKeysView left, PDictKeysView right,
167
203
@ GenerateNodeFactory
168
204
abstract static class AndNode extends PythonBinaryBuiltinNode {
169
205
@ Specialization
170
- PBaseSet doItemsView ( PDictItemsView left , PDictItemsView right ,
206
+ PBaseSet doKeysView ( PDictKeysView self , PDictKeysView other ,
171
207
@ Cached ("create()" ) HashingStorageNodes .IntersectNode intersectNode ) {
172
- HashingStorage intersectedStorage = intersectNode .execute (left .getDict ().getDictStorage (), right .getDict ().getDictStorage ());
208
+ HashingStorage intersectedStorage = intersectNode .execute (self .getDict ().getDictStorage (), other .getDict ().getDictStorage ());
173
209
return factory ().createSet (intersectedStorage );
174
210
}
175
211
176
212
@ Specialization
177
- PBaseSet doKeysView (PDictKeysView left , PDictKeysView right ,
178
- @ Cached ("create()" ) HashingStorageNodes .IntersectNode intersectNode ) {
179
- HashingStorage intersectedStorage = intersectNode .execute (left .getDict ().getDictStorage (), right .getDict ().getDictStorage ());
213
+ PBaseSet doItemsView (PDictItemsView self , PDictItemsView other ,
214
+ @ Cached ("create()" ) HashingStorageNodes .IntersectNode intersectNode ,
215
+ @ Cached ("create()" ) SetNodes .ConstructSetNode constructSetNode ) {
216
+ PSet selfSet = constructSetNode .executeWith (self );
217
+ PSet otherSet = constructSetNode .executeWith (other );
218
+ HashingStorage intersectedStorage = intersectNode .execute (selfSet .getDictStorage (), otherSet .getDictStorage ());
180
219
return factory ().createSet (intersectedStorage );
181
220
}
182
221
}
@@ -185,31 +224,37 @@ PBaseSet doKeysView(PDictKeysView left, PDictKeysView right,
185
224
@ GenerateNodeFactory
186
225
public abstract static class OrNode extends PythonBuiltinNode {
187
226
@ Specialization
188
- PBaseSet doItemsView ( PDictItemsView self , PDictItemsView other ,
227
+ PBaseSet doKeysView ( PDictKeysView self , PDictKeysView other ,
189
228
@ Cached ("create()" ) HashingStorageNodes .UnionNode unionNode ) {
190
229
return factory ().createSet (unionNode .execute (self .getDict ().getDictStorage (), other .getDict ().getDictStorage ()));
191
230
}
192
231
193
232
@ Specialization
194
- PBaseSet doKeysView (PDictKeysView self , PDictKeysView other ,
195
- @ Cached ("create()" ) HashingStorageNodes .UnionNode unionNode ) {
196
- return factory ().createSet (unionNode .execute (self .getDict ().getDictStorage (), other .getDict ().getDictStorage ()));
233
+ PBaseSet doItemsView (PDictItemsView self , PDictItemsView other ,
234
+ @ Cached ("create()" ) HashingStorageNodes .UnionNode unionNode ,
235
+ @ Cached ("create()" ) SetNodes .ConstructSetNode constructSetNode ) {
236
+ PSet selfSet = constructSetNode .executeWith (self );
237
+ PSet otherSet = constructSetNode .executeWith (other );
238
+ return factory ().createSet (unionNode .execute (selfSet .getDictStorage (), otherSet .getDictStorage ()));
197
239
}
198
240
}
199
241
200
242
@ Builtin (name = __XOR__ , fixedNumOfArguments = 2 )
201
243
@ GenerateNodeFactory
202
244
public abstract static class XorNode extends PythonBuiltinNode {
203
245
@ Specialization
204
- PBaseSet doItemsView ( PDictItemsView self , PDictItemsView other ,
246
+ PBaseSet doKeysView ( PDictKeysView self , PDictKeysView other ,
205
247
@ Cached ("create()" ) HashingStorageNodes .ExclusiveOrNode xorNode ) {
206
248
return factory ().createSet (xorNode .execute (self .getDict ().getDictStorage (), other .getDict ().getDictStorage ()));
207
249
}
208
250
209
251
@ Specialization
210
- PBaseSet doKeysView (PDictKeysView self , PDictKeysView other ,
211
- @ Cached ("create()" ) HashingStorageNodes .ExclusiveOrNode xorNode ) {
212
- return factory ().createSet (xorNode .execute (self .getDict ().getDictStorage (), other .getDict ().getDictStorage ()));
252
+ PBaseSet doItemsView (PDictItemsView self , PDictItemsView other ,
253
+ @ Cached ("create()" ) HashingStorageNodes .ExclusiveOrNode xorNode ,
254
+ @ Cached ("create()" ) SetNodes .ConstructSetNode constructSetNode ) {
255
+ PSet selfSet = constructSetNode .executeWith (self );
256
+ PSet otherSet = constructSetNode .executeWith (other );
257
+ return factory ().createSet (xorNode .execute (selfSet .getDictStorage (), otherSet .getDictStorage ()));
213
258
}
214
259
}
215
260
}
0 commit comments