10000 [Set] Add `set.intersection(_update)` by guilhermeleobas · Pull Request #152906 · pytorch/pytorch · GitHub
[go: up one dir, main page]

Skip to content

[Set] Add set.intersection(_update) #152906

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
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
29 changes: 27 additions & 2 deletions test/dynamo/test_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -1747,7 +1747,8 @@ def fn(a):
def test_set_intersection(a, b):
set1 = {"apple", "banana", "cherry"}
set2 = {"google", "microsoft", "apple"}
intersection_set = set1.intersection(set2)
set3 = {"shoes", "flipflops", "apple"}
intersection_set = set1.intersection(set2, set3)
if "apple" in intersection_set:
x = a + b
else:
Expand All @@ -1756,7 +1757,31 @@ def test_set_intersection(a, b):
y = a + b
else:
y = a - b
return x, y
if "shoes" in intersection_set:
z = a + b
else:
z = a - b
return x, y, z

@make_test
def test_set_intersection_update(a, b):
set1 = {"apple", "banana", "cherry"}
set2 = {"google", "microsoft", "apple"}
set3 = {"shoes", "flipflops", "apple"}
set1.intersection_update(set2, set3)
if "apple" in set1:
x = a + b
else:
x = a - b
if "banana" in set1:
y = a + b
else:
y = a - b
if "shoes" in set1:
z = a + b
else:
z = a - b
return x, y, z

@make_test
def test_set_union(a, b):
Expand Down
16 changes: 14 additions & 2 deletions torch/_dynamo/polyfills/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -125,14 +125,26 @@ def set_isdisjoint(set1, set2):
return True


def set_intersection(set1, set2):
def set_intersection(set1, *others):
if len(others) == 0:
return set1.copy()

intersection_set = set()
for x in set1:
if x in set2:
for set2 in others:
if x not in set2:
break
else:
intersection_set.add(x)
return intersection_set


def set_intersection_update(set1, *others):
result = set1.intersection(*others)
set1.clear()
set1.update(result)


def set_union(set1, set2):
union_set = set1.copy()
set_update(union_set, set2)
Expand Down
8 changes: 6 additions & 2 deletions torch/_dynamo/variables/dicts.py
Original file line number Diff line number Diff line change
Expand Up @@ -776,10 +776,14 @@ def call_method(
).call_function(tx, [self, args[0]], {})
elif name == "intersection":
assert not kwargs
assert len(args) == 1
return variables.UserFunctionVariable(
polyfills.set_intersection
).call_function(tx, [self, args[0]], {})
).call_function(tx, [self, *args], {})
elif name == "intersection_update":
assert not kwargs
return variables.UserFunctionVariable(
polyfills.set_intersection_update
).call_function(tx, [self, *args], {})
elif name == "union":
assert not kwargs
assert len(args) == 1
Expand Down
Loading
0