@@ -176,16 +176,69 @@ boolean contains(Object self, Object key) {
176
176
177
177
}
178
178
179
+ @ Builtin (name = ISDISJOINT , minNumOfPositionalArgs = 2 )
180
+ @ GenerateNodeFactory
181
+ public abstract static class IsDisjointNode extends PythonBinaryBuiltinNode {
182
+
183
+ @ Specialization (guards = {"self == other" })
184
+ boolean disjointSame (PDictView self , @ SuppressWarnings ("unused" ) PDictView other ) {
185
+ return self .getWrappedDict ().size () == 0 ;
186
+ }
187
+
188
+ @ Specialization (guards = {"self != other" }, limit = "1" )
189
+ boolean disjointNotSame (VirtualFrame frame , PDictView self , PDictView other ,
190
+ @ Cached ConditionProfile sizeProfile ,
191
+ @ CachedLibrary ("other" ) PythonObjectLibrary lib ,
192
+ @ Cached ("create(false)" ) ContainedInNode contained ) {
193
+ return disjointImpl (frame , self , other , sizeProfile , lib , contained );
194
+ }
195
+
196
+ @ Specialization (limit = "1" )
197
+ boolean disjoint (VirtualFrame frame , PDictView self , PBaseSet other ,
198
+ @ Cached ConditionProfile sizeProfile ,
199
+ @ CachedLibrary ("other" ) PythonObjectLibrary lib ,
200
+ @ Cached ("create(false)" ) ContainedInNode contained ) {
201
+ return disjointImpl (frame , self , other , sizeProfile , lib , contained );
202
+ }
203
+
204
+ private static boolean disjointImpl (VirtualFrame frame , PDictView self , Object other , ConditionProfile sizeProfile , PythonObjectLibrary lib , ContainedInNode contained ) {
205
+ if (sizeProfile .profile (self .size () <= lib .length (other ))) {
206
+ return !contained .execute (frame , self , other );
207
+ } else {
208
+ return !contained .execute (frame , other , self );
209
+ }
210
+ }
211
+
212
+ @ Specialization (guards = {"lib.isIterable(other)" , "!isAnySet(other)" , "!isDictView(other)" }, limit = "1" )
213
+ boolean disjoint (VirtualFrame frame , PDictView self , Object other ,
214
+ @ SuppressWarnings ("unused" ) @ CachedLibrary ("other" ) PythonObjectLibrary lib ,
215
+ @ Cached ("create(false)" ) ContainedInNode contained ) {
216
+ return !contained .execute (frame , other , self );
217
+ }
218
+
219
+ @ SuppressWarnings ("unused" )
220
+ @ Specialization (guards = "!lib.isIterable(other)" , limit = "1" )
221
+ boolean disjoint (PDictView self , Object other ,
222
+ @ CachedLibrary ("other" ) PythonObjectLibrary lib ) {
223
+ throw raise (PythonBuiltinClassType .TypeError , ErrorMessages .OBJ_NOT_ITERABLE , other );
224
+ }
225
+ }
226
+
179
227
/**
180
- * See CPython's dictobject.c all_contained_in. The semantics of dict view comparisons dictates
181
- * that we need to use iteration to compare them in the general case.
228
+ * See CPython's dictobject.c all_contained_in and dictviews_isdisjoint . The semantics of dict
229
+ * view comparisons dictates that we need to use iteration to compare them in the general case.
182
230
*/
183
- protected static class AllContainedInNode extends PNodeWithContext {
231
+ protected static class ContainedInNode extends PNodeWithContext {
184
232
@ Child GetIteratorNode iter = GetIteratorNode .create ();
185
233
@ Child GetNextNode next ;
186
234
@ Child LookupAndCallBinaryNode contains ;
187
235
@ Child CoerceToBooleanNode cast ;
188
236
@ CompilationFinal IsBuiltinClassProfile stopProfile ;
237
+ private final boolean checkAll ;
238
+
239
+ public ContainedInNode (boolean checkAll ) {
240
+ this .checkAll = checkAll ;
241
+ }
189
242
190
243
private GetNextNode getNext () {
191
244
if (next == null ) {
@@ -221,9 +274,9 @@ private IsBuiltinClassProfile getStopProfile() {
221
274
222
275
public boolean execute (VirtualFrame frame , Object self , Object other ) {
223
276
Object iterator = iter .executeWith (frame , self );
224
- boolean ok = true ;
277
+ boolean ok = checkAll ;
225
278
try {
226
- while (ok ) {
279
+ while (checkAll && ok || ! checkAll && ! ok ) {
227
280
Object item = getNext ().execute (frame , iterator );
228
281
ok = getCast ().executeBoolean (frame , getContains ().executeObject (frame , other , item ));
229
282
}
@@ -233,8 +286,12 @@ public boolean execute(VirtualFrame frame, Object self, Object other) {
233
286
return ok ;
234
287
}
235
288
236
- public static AllContainedInNode create () {
237
- return new AllContainedInNode ();
289
+ static ContainedInNode create () {
290
+ return new ContainedInNode (true );
291
+ }
292
+
293
+ static ContainedInNode create (boolean all ) {
294
+ return new ContainedInNode (all );
238
295
}
239
296
}
240
297
@@ -253,7 +310,7 @@ protected boolean lenCompare(@SuppressWarnings("unused") int lenSelf, @SuppressW
253
310
boolean doView (VirtualFrame frame , PDictView self , PBaseSet other ,
254
311
@ CachedLibrary ("self.getWrappedDict().getDictStorage()" ) HashingStorageLibrary selflib ,
255
312
@ CachedLibrary ("other.getDictStorage()" ) HashingStorageLibrary otherlib ,
256
- @ Cached AllContainedInNode allContained ) {
313
+ @ Cached ContainedInNode allContained ) {
257
314
int lenSelf = selflib .length (self .getWrappedDict ().getDictStorage ());
258
315
int lenOther = otherlib .length (other .getDictStorage ());
259
316
return lenCompare (lenSelf , lenOther ) && (reverse () ? allContained .execute (frame , other , self ) : allContained .execute (frame , self , other ));
@@ -263,7 +320,7 @@ boolean doView(VirtualFrame frame, PDictView self, PBaseSet other,
263
320
boolean doView (VirtualFrame frame , PDictView self , PDictView other ,
264
321
@ CachedLibrary ("self.getWrappedDict().getDictStorage()" ) HashingStorageLibrary selflib ,
265
322
@ CachedLibrary ("other.getWrappedDict().getDictStorage()" ) HashingStorageLibrary otherlib ,
266
- @ Cached AllContainedInNode allContained ) {
323
+ @ Cached ContainedInNode allContained ) {
267
324
int lenSelf = selflib .length (self .getWrappedDict ().getDictStorage ());
268
325
int lenOther = otherlib .length (other .getWrappedDict ().getDictStorage ());
269
326
return lenCompare (lenSelf , lenOther ) && (reverse () ? allContained .execute (frame , other , self ) : allContained .execute (frame , self , other ));
0 commit comments