Skip to content

Commit 0478358

Browse files
authored
Merge pull request RustPython#853 from palaviv/set-disjoint
Add set.isdisjoint
2 parents 3afb5d3 + 3986e2f commit 0478358

File tree

2 files changed

+30
-2
lines changed

2 files changed

+30
-2
lines changed

tests/snippets/set.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,10 @@ def __hash__(self):
7979
assert set([1,2,3]) ^ set([1,2,3,4,5]) == set([4,5])
8080
assert_raises(TypeError, lambda: set([1,2,3]) ^ [1,2,3,4,5])
8181

82+
assert set([1,2,3]).isdisjoint(set([5,6])) == True
83+
assert set([1,2,3]).isdisjoint(set([2,5,6])) == False
84+
assert set([1,2,3]).isdisjoint([5,6]) == True
85+
8286
assert_raises(TypeError, lambda: set([[]]))
8387
assert_raises(TypeError, lambda: set().add([]))
8488

@@ -227,6 +231,10 @@ def __hash__(self):
227231
assert frozenset([1,2,3]) ^ frozenset([1,2,3,4,5]) == frozenset([4,5])
228232
assert_raises(TypeError, lambda: frozenset([1,2,3]) ^ [1,2,3,4,5])
229233

234+
assert frozenset([1,2,3]).isdisjoint(frozenset([5,6])) == True
235+
assert frozenset([1,2,3]).isdisjoint(frozenset([2,5,6])) == False
236+
assert frozenset([1,2,3]).isdisjoint([5,6]) == True
237+
230238
assert_raises(TypeError, lambda: frozenset([[]]))
231239

232240
a = frozenset([1,2,3])

vm/src/obj/objset.rs

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,16 @@ impl PySetInner {
222222
Ok(new_inner)
223223
}
224224

225+
fn isdisjoint(&self, other: PyIterable, vm: &VirtualMachine) -> PyResult<bool> {
226+
for item in other.iter(vm)? {
227+
let obj = item?;
228+
if self.contains(obj.clone(), vm)? {
229+
return Ok(false);
230+
}
231+
}
232+
Ok(true)
233+
}
234+
225235
fn iter(&self, vm: &VirtualMachine) -> PyListIterator {
226236
let items = self.elements.values().cloned().collect();
227237
let set_list = vm.ctx.new_list(items);
@@ -426,6 +436,10 @@ impl PySetRef {
426436
))
427437
}
428438

439+
fn isdisjoint(self, other: PyIterable, vm: &VirtualMachine) -> PyResult<bool> {
440+
self.inner.borrow().isdisjoint(other, vm)
441+
}
442+
429443
fn or(self, other: SetIterable, vm: &VirtualMachine) -> PyResult {
430444
self.union(other.iterable, vm)
431445
}
@@ -633,6 +647,10 @@ impl PyFrozenSetRef {
633647
))
634648
}
635649

650+
fn isdisjoint(self, other: PyIterable, vm: &VirtualMachine) -> PyResult<bool> {
651+
self.inner.isdisjoint(other, vm)
652+
}
653+
636654
fn or(self, other: SetIterable, vm: &VirtualMachine) -> PyResult {
637655
self.union(other.iterable, vm)
638656
}
@@ -788,7 +806,8 @@ pub fn init(context: &PyContext) {
788806
"__isub__" => context.new_rustfunc(PySetRef::isub),
789807
"symmetric_difference_update" => context.new_rustfunc(PySetRef::symmetric_difference_update),
790808
"__ixor__" => context.new_rustfunc(PySetRef::ixor),
791-
"__iter__" => context.new_rustfunc(PySetRef::iter)
809+
"__iter__" => context.new_rustfunc(PySetRef::iter),
810+
"isdisjoint" => context.new_rustfunc(PySetRef::isdisjoint),
792811
});
793812

794813
let frozenset_type = &context.frozenset_type;
@@ -819,6 +838,7 @@ pub fn init(context: &PyContext) {
819838
"__doc__" => context.new_str(frozenset_doc.to_string()),
820839
"__repr__" => context.new_rustfunc(PyFrozenSetRef::repr),
821840
"copy" => context.new_rustfunc(PyFrozenSetRef::copy),
822-
"__iter__" => context.new_rustfunc(PyFrozenSetRef::iter)
841+
"__iter__" => context.new_rustfunc(PyFrozenSetRef::iter),
842+
"isdisjoint" => context.new_rustfunc(PyFrozenSetRef::isdisjoint),
823843
});
824844
}

0 commit comments

Comments
 (0)