@@ -589,20 +589,15 @@ cdef class lazy_list_generic(object):
589
589
sage: lazy_list( [0,1,2,3 ])
590
590
lazy list [0, 1, 2, ... ]
591
591
"""
592
- cdef Py_ssize_t num_elts = 1 + (self .stop- self .start- 1 ) / self .step
593
- cdef Py_ssize_t length = len (self .cache)
594
-
595
- if (length <= self .start + self .preview* self .step and
596
- num_elts != length / self .step):
597
- self ._fit(self .start + self .preview* self .step)
598
- num_elts = 1 + (self .stop- self .start- 1 ) / self .step
599
-
600
592
cdef str s = self .name
601
593
if s:
602
594
s += ' '
603
595
s += self .opening_delimiter
604
- cdef list E = list (' {!r}' .format(self .get(n))
605
- for n in xrange (min (num_elts, self .preview)))
596
+ cdef list P = list (self [:self .preview+ 1 ])
597
+ cdef list E = list (' {!r}' .format(e)
598
+ for e in P[:self .preview])
599
+ cdef Py_ssize_t num_elts = 1 + (self .stop- self .start- 1 ) / self .step
600
+
606
601
if num_elts > self .preview:
607
602
E.append(self .more)
608
603
s += self .separator.join(E)
@@ -796,9 +791,16 @@ cdef class lazy_list_generic(object):
796
791
start 0
797
792
stop 5
798
793
step 1
794
+
795
+ ::
796
+
797
+ sage: tuple( lazy_list( Primes( )) . dropwhile( lambda x: x < 10) [:5 ])
798
+ ( 11, 13, 17, 19, 23)
799
799
"""
800
800
cdef Py_ssize_t i
801
801
802
+ if self ._fit(self .start):
803
+ return
802
804
i = self .start
803
805
while i < self .stop:
804
806
if self ._fit(i):
@@ -1574,11 +1576,11 @@ cdef class lazy_list_takewhile(lazy_list_generic):
1574
1576
sage: lazy_list_takewhile( Z( ) , lambda x: x != 3) [4 ]
1575
1577
4
1576
1578
"""
1577
-
1578
1579
lazy_list_generic.__init__ (
1579
1580
self , master = master, cache = master._get_cache_(), ** kwds)
1580
1581
self .predicate = predicate
1581
1582
self .taking = True
1583
+ self .to_test = self .start
1582
1584
1583
1585
1584
1586
cpdef int update_cache_up_to(self , Py_ssize_t i) except - 1 :
@@ -1599,35 +1601,88 @@ cdef class lazy_list_takewhile(lazy_list_generic):
1599
1601
sage: L = lazy_list_takewhile(
1600
1602
.... : lazy_list( Primes( )) , lambda x: x <= 10) ; L
1601
1603
lazy list [2, 3, 5, ... ]
1604
+ sage: L. _info( )
1605
+ cache length 4
1606
+ start 0
1607
+ stop 9223372036854775807
1608
+ step 1
1602
1609
sage: L. update_cache_up_to( 10)
1603
1610
1
1611
+ sage: tuple( L)
1612
+ ( 2, 3, 5, 7)
1604
1613
sage: L. _info( )
1605
1614
cache length 5
1606
1615
start 0
1607
1616
stop 4
1608
1617
step 1
1618
+
1619
+ ::
1620
+
1621
+ sage: P = lazy_list( Primes( ))
1622
+ sage: a = tuple( P. dropwhile( lambda x: x < 10) . takewhile( lambda x: x < 20)) ; a
1623
+ ( 11, 13, 17, 19)
1624
+ sage: b = tuple( P. dropwhile( lambda x: x < 10) . takewhile( lambda x: x < 20))
1625
+ sage: a == b
1626
+ True
1627
+
1628
+ ::
1629
+
1630
+ sage: F = lazy_list( lambda x: fibonacci( x))
1631
+ sage: tuple( F. dropwhile( lambda x: x < 10) . takewhile( lambda x: x < 20))
1632
+ ( 13,)
1633
+
1634
+ ::
1635
+
1636
+ sage: P = lazy_list( srange( 1,30,2))
1637
+ sage: a = tuple( P. dropwhile( lambda x: x < 10) . takewhile( lambda x: x < 20)) ; a
1638
+ ( 11, 13, 15, 17, 19)
1639
+ sage: b = tuple( P. dropwhile( lambda x: x < 10) . takewhile( lambda x: x < 20))
1640
+ sage: a == b
1641
+ True
1642
+
1643
+ ::
1644
+
1645
+ sage: P = lazy_list( Primes( ))
1646
+ sage: Q = P. takewhile( lambda x: x < 20) ; c = tuple( Q) ; c
1647
+ ( 2, 3, 5, 7, 11, 13, 17, 19)
1648
+ sage: Q. _info( )
1649
+ cache length 9
1650
+ start 0
1651
+ stop 8
1652
+ step 1
1653
+ sage: Q = P. takewhile( lambda x: x < 20) ; d = tuple( Q)
1654
+ sage: Q. _info( )
1655
+ cache length 9
1656
+ start 0
1657
+ stop 8
1658
+ step 1
1659
+ sage: c == d
1660
+ True
1609
1661
"""
1610
- cdef Py_ssize_t length = len (self .cache)
1611
1662
if not self .taking:
1612
- if length <= i:
1613
- return 1
1614
- else :
1615
- return 0
1616
- cdef Py_ssize_t m
1617
- while length <= i:
1618
- if self .master._fit(length):
1619
- return 1
1620
- m = length
1621
- length = len (self .cache)
1622
- while m < length and self .predicate(self .cache[m]):
1623
- m += 1
1624
- if m < length:
1625
- self .stop = m
1663
+ return 0
1664
+
1665
+ if self .master._fit(self .to_test):
1666
+ self .stop = self .to_test
1667
+ self .taking = False
1668
+ return 1
1669
+ self .to_test = max (self .to_test, self .start)
1670
+
1671
+ while self .to_test <= i:
1672
+ if self .master._fit(self .to_test):
1673
+ self .stop = self .to_test
1626
1674
self .taking = False
1627
- if m <= i:
1628
- return 1
1629
- else :
1630
- return 0
1675
+ return 1
1676
+ if not (self .to_test < len (self .cache) and
1677
+ self .predicate(self .cache[self .to_test])):
1678
+ break
1679
+ self .to_test += 1
1680
+
1681
+ if self .to_test <= i:
1682
+ self .stop = self .to_test
1683
+ self .taking = False
1684
+ return 1
1685
+
1631
1686
return 0
1632
1687
1633
1688
@@ -1670,6 +1725,13 @@ cdef class lazy_list_dropwhile(lazy_list_generic):
1670
1725
start 6
1671
1726
stop 9223372036854775807
1672
1727
step 1
1728
+
1729
+ ::
1730
+
1731
+ sage: lazy_list( Primes( )) . dropwhile( lambda x: x < 10) . takewhile( lambda x: x < 20)
1732
+ lazy list [11, 13, 17, ... ]
1733
+ sage: tuple( lazy_list( Primes( )) . dropwhile( lambda x: x < 10) . takewhile( lambda x: x < 20))
1734
+ ( 11, 13, 17, 19)
1673
1735
"""
1674
1736
lazy_list_generic.__init__ (
1675
1737
self , master = master, cache = master._get_cache_(), ** kwds)
@@ -1711,8 +1773,8 @@ cdef class lazy_list_dropwhile(lazy_list_generic):
1711
1773
m = self .start
1712
1774
lazy_list_generic._fit(self , m)
1713
1775
while m < len (self .cache) and self .predicate(self .cache[m]):
1714
- m += 1
1715
- lazy_list_generic._fit(self , m)
1776
+ m += 1
1777
+ lazy_list_generic._fit(self , m)
1716
1778
self .start = m
1717
1779
self .dropping = False
1718
1780
n += m
0 commit comments