Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions tests/snippets/set.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,10 @@ def __hash__(self):
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([]))

Expand Down Expand Up @@ -227,6 +231,10 @@ def __hash__(self):
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])
Expand Down
24 changes: 22 additions & 2 deletions vm/src/obj/objset.rs
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,16 @@ impl PySetInner {
Ok(new_inner)
}

fn isdisjoint(&self, other: PyIterable, vm: &VirtualMachine) -> PyResult<bool> {
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);
Expand Down Expand Up @@ -426,6 +436,10 @@ impl PySetRef {
))
}

fn isdisjoint(self, other: PyIterable, vm: &VirtualMachine) -> PyResult<bool> {
self.inner.borrow().isdisjoint(other, vm)
}

fn or(self, other: SetIterable, vm: &VirtualMachine) -> PyResult {
self.union(other.iterable, vm)
}
Expand Down Expand Up @@ -633,6 +647,10 @@ impl PyFrozenSetRef {
))
}

fn isdisjoint(self, other: PyIterable, vm: &VirtualMachine) -> PyResult<bool> {
self.inner.isdisjoint(other, vm)
}

fn or(self, other: SetIterable, vm: &VirtualMachine) -> PyResult {
self.union(other.iterable, vm)
}
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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),
});
}