Skip to content

Commit 6d68f1c

Browse files
committed
set: add intersection support
1 parent e123ddb commit 6d68f1c

File tree

2 files changed

+47
-0
lines changed

2 files changed

+47
-0
lines changed

graalpython/com.oracle.graal.python.test/src/tests/test_set.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,10 @@ def test_difference():
126126
def test_difference_update():
127127
word = 'simsalabim'
128128
otherword = 'madagascar'
129+
letters = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'
129130
s = set(word)
131+
d = dict.fromkeys(word)
132+
130133

131134
retval = s.difference_update(otherword)
132135
assert retval == None
@@ -176,3 +179,34 @@ def test_sub_and_super():
176179
assert set('abc').issuperset('a')
177180
assert not set('a').issubset('cbs')
178181
assert not set('cbs').issuperset('a')
182+
183+
184+
def test_intersection():
185+
word = 'simsalabim'
186+
otherword = 'madagascar'
187+
letters = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'
188+
s = set(word)
189+
d = dict.fromkeys(word)
190+
191+
i = s.intersection(otherword)
192+
for c in letters:
193+
assert (c in i) == (c in d and c in otherword)
194+
195+
assert s == set(word)
196+
# assert type(i) == set
197+
assert_raises(PassThru, s.intersection, check_pass_thru())
198+
199+
for C in set, frozenset, dict.fromkeys, str, list, tuple:
200+
assert set('abcba').intersection(C('cdc')) == set('cc')
201+
assert set('abcba').intersection(C('efgfe')) == set('')
202+
assert set('abcba').intersection(C('ccb')) == set('bc')
203+
assert set('abcba').intersection(C('ef')) == set('')
204+
assert set('abcba').intersection(C('cbcf'), C('bag')) == set('b')
205+
206+
# TODO: currently the id function behaves a bit differently than the one in cPython
207+
# s = set('abcba')
208+
# z = s.intersection()
209+
# if set == frozenset():
210+
# assert id(s) == id(z)
211+
# else:
212+
# assert id(s) != id(z)

graalpython/lib-graalpython/set.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,17 @@ def frozenset_difference(self, *others):
6767
return frozenset(difference(self, *others))
6868

6969

70+
def intersection(self, *others):
71+
intersect_set = self
72+
for seq in others:
73+
intersect_set = set(el for el in intersect_set if el in set(seq))
74+
return intersect_set
75+
76+
77+
def frozenset_intersection(self, *others):
78+
return frozenset(intersection(self, *others))
79+
80+
7081
def set_repr(self):
7182
if len(self):
7283
s = "{"
@@ -108,9 +119,11 @@ def frozenset_copy(self):
108119
set.update = update
109120
set.difference = difference
110121
set.difference_update = difference_update
122+
set.intersection = intersection
111123
set.__repr__ = set_repr
112124
set.copy = set_copy
113125

114126
frozenset.difference = frozenset_difference
127+
frozenset.intersection = frozenset_intersection
115128
frozenset.__repr__ = frozenset_repr
116129
frozenset.copy = frozenset_copy

0 commit comments

Comments
 (0)