mirror of https://github.com/python/cpython.git
[3.11] gh-102088 Optimize iter_index itertools recipe (GH-102360) (GH-102363)
This commit is contained in:
parent
90ec292ab8
commit
3effccee48
|
@ -875,9 +875,12 @@ which incur interpreter overhead.
|
||||||
except AttributeError:
|
except AttributeError:
|
||||||
# Slow path for general iterables
|
# Slow path for general iterables
|
||||||
it = islice(iterable, start, None)
|
it = islice(iterable, start, None)
|
||||||
for i, element in enumerate(it, start):
|
i = start - 1
|
||||||
if element is value or element == value:
|
try:
|
||||||
yield i
|
while True:
|
||||||
|
yield (i := i + operator.indexOf(it, value) + 1)
|
||||||
|
except ValueError:
|
||||||
|
pass
|
||||||
else:
|
else:
|
||||||
# Fast path for sequences
|
# Fast path for sequences
|
||||||
i = start - 1
|
i = start - 1
|
||||||
|
|
|
@ -208,6 +208,9 @@ def test_indexOf(self):
|
||||||
nan = float("nan")
|
nan = float("nan")
|
||||||
self.assertEqual(operator.indexOf([nan, nan, 21], nan), 0)
|
self.assertEqual(operator.indexOf([nan, nan, 21], nan), 0)
|
||||||
self.assertEqual(operator.indexOf([{}, 1, {}, 2], {}), 0)
|
self.assertEqual(operator.indexOf([{}, 1, {}, 2], {}), 0)
|
||||||
|
it = iter('leave the iterator at exactly the position after the match')
|
||||||
|
self.assertEqual(operator.indexOf(it, 'a'), 2)
|
||||||
|
self.assertEqual(next(it), 'v')
|
||||||
|
|
||||||
def test_invert(self):
|
def test_invert(self):
|
||||||
operator = self.module
|
operator = self.module
|
||||||
|
|
Loading…
Reference in New Issue