diff --git a/tests/snippets/set.py b/tests/snippets/set.py index 393db0754..ba20fbae6 100644 --- a/tests/snippets/set.py +++ b/tests/snippets/set.py @@ -79,6 +79,10 @@ assert set([1,2,3]) ^ set([4,5]) == set([1,2,3,4,5]) assert set([1,2,3]) ^ set([1,2,3,4,5]) == set([4,5]) assert_raises(TypeError, lambda: set([1,2,3]) ^ [1,2,3,4,5]) +assert set([1,2,3]).isdisjoint(set([5,6])) == True +assert set([1,2,3]).isdisjoint(set([2,5,6])) == False +assert set([1,2,3]).isdisjoint([5,6]) == True + assert_raises(TypeError, lambda: set([[]])) assert_raises(TypeError, lambda: set().add([])) @@ -227,6 +231,10 @@ assert frozenset([1,2,3]) ^ frozenset([4,5]) == frozenset([1,2,3,4,5]) assert frozenset([1,2,3]) ^ frozenset([1,2,3,4,5]) == frozenset([4,5]) assert_raises(TypeError, lambda: frozenset([1,2,3]) ^ [1,2,3,4,5]) +assert frozenset([1,2,3]).isdisjoint(frozenset([5,6])) == True +assert frozenset([1,2,3]).isdisjoint(frozenset([2,5,6])) == False +assert frozenset([1,2,3]).isdisjoint([5,6]) == True + assert_raises(TypeError, lambda: frozenset([[]])) a = frozenset([1,2,3]) diff --git a/vm/src/obj/objset.rs b/vm/src/obj/objset.rs index b8f9f6d3b..b4f3bfabc 100644 --- a/vm/src/obj/objset.rs +++ b/vm/src/obj/objset.rs @@ -222,6 +222,16 @@ impl PySetInner { Ok(new_inner) } + fn isdisjoint(&self, other: PyIterable, vm: &VirtualMachine) -> PyResult { + for item in other.iter(vm)? { + let obj = item?; + if self.contains(obj.clone(), vm)? { + return Ok(false); + } + } + Ok(true) + } + fn iter(&self, vm: &VirtualMachine) -> PyListIterator { let items = self.elements.values().cloned().collect(); let set_list = vm.ctx.new_list(items); @@ -426,6 +436,10 @@ impl PySetRef { )) } + fn isdisjoint(self, other: PyIterable, vm: &VirtualMachine) -> PyResult { + self.inner.borrow().isdisjoint(other, vm) + } + fn or(self, other: SetIterable, vm: &VirtualMachine) -> PyResult { self.union(other.iterable, vm) } @@ -633,6 +647,10 @@ impl PyFrozenSetRef { )) } + fn isdisjoint(self, other: PyIterable, vm: &VirtualMachine) -> PyResult { + self.inner.isdisjoint(other, vm) + } + fn or(self, other: SetIterable, vm: &VirtualMachine) -> PyResult { self.union(other.iterable, vm) } @@ -788,7 +806,8 @@ pub fn init(context: &PyContext) { "__isub__" => context.new_rustfunc(PySetRef::isub), "symmetric_difference_update" => context.new_rustfunc(PySetRef::symmetric_difference_update), "__ixor__" => context.new_rustfunc(PySetRef::ixor), - "__iter__" => context.new_rustfunc(PySetRef::iter) + "__iter__" => context.new_rustfunc(PySetRef::iter), + "isdisjoint" => context.new_rustfunc(PySetRef::isdisjoint), }); let frozenset_type = &context.frozenset_type; @@ -819,6 +838,7 @@ pub fn init(context: &PyContext) { "__doc__" => context.new_str(frozenset_doc.to_string()), "__repr__" => context.new_rustfunc(PyFrozenSetRef::repr), "copy" => context.new_rustfunc(PyFrozenSetRef::copy), - "__iter__" => context.new_rustfunc(PyFrozenSetRef::iter) + "__iter__" => context.new_rustfunc(PyFrozenSetRef::iter), + "isdisjoint" => context.new_rustfunc(PyFrozenSetRef::isdisjoint), }); }