Merge pull request #853 from palaviv/set-disjoint

Add set.isdisjoint
This commit is contained in:
coolreader18 authored and GitHub committed 2019-04-21 09:58:29 -05:00
commit 0478358fd9
2 files changed
+30 -2

No files matched your search

+8
View File
@@ -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])
+22 -2
View File
@@ -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);
@@ -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)
}
@@ -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)
}
@@ -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),
});
}