Skip to content
This repository has been archived by the owner on Jan 30, 2023. It is now read-only.

Commit

Permalink
Revert "copy code from #19048"
Browse files Browse the repository at this point in the history
This reverts commit 619cd8c.
  • Loading branch information
cheuberg committed Oct 3, 2015
1 parent 5b653f3 commit c41435f
Showing 1 changed file with 0 additions and 116 deletions.
116 changes: 0 additions & 116 deletions src/sage/misc/mrange.py
Original file line number Diff line number Diff line change
Expand Up @@ -608,119 +608,3 @@ def cartesian_product_iterator(X):
[()]
"""
return xmrange_iter(X, tuple)


def product_diagonal(A, B):
r"""
Return an iterator over the product of `A` and `B` which iterates
along the diagonal.
INPUT:
- ``A`` and ``B`` -- iterables (over a finite number of elements)
OUTPUT:
An iterator over `(a,b)` for `a \in A` and `b \in B`.
EXAMPLES::
sage: from sage.rings.asymptotic.misc import product_diagonal
sage: tuple(product_diagonal(srange(2), srange(2)))
((0, 0), (0, 1), (1, 0), (1, 1))
sage: tuple(product_diagonal(srange(4), srange(2)))
((0, 0), (0, 1), (1, 0), (1, 1), (2, 0), (2, 1), (3, 0), (3, 1))
sage: tuple(product_diagonal(srange(2), srange(3)))
((0, 0), (0, 1), (1, 0), (0, 2), (1, 1), (1, 2))
sage: tuple(''.join(p) for p in product_diagonal('abc', 'xyz'))
('ax', 'ay', 'bx', 'az', 'by', 'cx', 'bz', 'cy', 'cz')
TESTS:
Check that all pairs are returned::
sage: all(len(tuple(product_diagonal(srange(m), srange(n)))) == m*n
....: for m in srange(5) for n in srange(5))
True
Check that everthing is loaded in the correct order::
sage: def it(s, n):
....: for i in srange(n):
....: print '%s loads item number %s' % (s, i)
....: yield i
sage: for p in product_diagonal(it('A', 2), it('B', 2)):
....: print p
A loads item number 0
B loads item number 0
(0, 0)
B loads item number 1
(0, 1)
A loads item number 1
(1, 0)
(1, 1)
sage: for p in product_diagonal(it('A', 3), it('B', 2)):
....: print p
A loads item number 0
B loads item number 0
(0, 0)
B loads item number 1
(0, 1)
A loads item number 1
(1, 0)
(1, 1)
A loads item number 2
(2, 0)
(2, 1)
sage: for p in product_diagonal(it('A', 2), it('B', 4)):
....: print p
A loads item number 0
B loads item number 0
(0, 0)
B loads item number 1
(0, 1)
A loads item number 1
(1, 0)
B loads item number 2
(0, 2)
(1, 1)
B loads item number 3
(0, 3)
(1, 2)
(1, 3)
"""
# when writing this code I thought the solution would be shorter...

class iter_as_list(list):
def __init__(self, iterable):
self.it = iter(iterable)
self.newdata = True
def __getitem__(self, i):
self.newdata = False
try:
while len(self) <= i:
self.append(next(self.it))
self.newdata = True
except StopIteration:
raise
return list.__getitem__(self, i)

from itertools import count
A = iter_as_list(A)
B = iter_as_list(B)
for s in count():
for i in range(s+1):
stopped = False
try:
a = A[i]
except StopIteration:
stopped = True
try:
b = B[s-i]
except StopIteration:
stopped = True
if stopped:
continue
yield a, b
if not A.newdata and not B.newdata and s >= len(A) + len(B):
return

0 comments on commit c41435f

Please sign in to comment.