Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 0d59ff5

Browse files
committed
O(n) implementation of Grouper.__iter__
svn path=/trunk/matplotlib/; revision=5711
1 parent 8d3f454 commit 0d59ff5

1 file changed

Lines changed: 17 additions & 6 deletions

File tree

lib/matplotlib/cbook.py

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1030,7 +1030,7 @@ class Grouper(object):
10301030
>>> g.join('a', 'b')
10311031
>>> g.join('b', 'c')
10321032
>>> g.join('d', 'e')
1033-
>>> list(g.get())
1033+
>>> list(g)
10341034
[['a', 'b', 'c'], ['d', 'e']]
10351035
>>> g.joined('a', 'b')
10361036
True
@@ -1079,13 +1079,24 @@ def joined(self, a, b):
10791079

10801080
def __iter__(self):
10811081
"""
1082-
Returns an iterator yielding each of the disjoint sets as a list.
1082+
Iterate over each of the disjoint sets as a list.
1083+
1084+
The iterator is invalid if interleaved with calls to join().
10831085
"""
1084-
seen = set()
1085-
for elem, group in self._mapping.iteritems():
1086-
if elem not in seen:
1086+
class Token: pass
1087+
token = Token()
1088+
1089+
# Mark each group as we come across if by appending a token,
1090+
# and don't yield it twice
1091+
for group in self._mapping.itervalues():
1092+
if not group[-1] is token:
10871093
yield group
1088-
seen.update(group)
1094+
group.append(token)
1095+
1096+
# Cleanup the tokens
1097+
for group in self._mapping.itervalues():
1098+
if group[-1] is token:
1099+
del group[-1]
10891100

10901101
def get_siblings(self, a):
10911102
"""

0 commit comments

Comments
 (0)