Package advene :: Package util :: Module itertools
[hide private]
[frames] | no frames]

Source Code for Module advene.util.itertools

 1  from heapq import heapify, heapreplace, heappop 
 2   
3 -class _IterHead(object):
4 __slots__ = ["head", "iter"] 5
6 - def __init__(self, iter):
7 self.iter = iter 8 self.fetch_next()
9
10 - def fetch_next(self):
11 try: 12 self.head = self.iter.next() 13 except StopIteration: 14 self.head = None
15
16 - def __cmp__(self, other):
17 return cmp(self.head, other.head)
18
19 -def interclass(*iterables):
20 """ 21 Takes a number of sorted iterables, and inteclass them, suppressing 22 doublons. 23 """ 24 h = [ _IterHead(iter(i)) for i in iterables ] 25 h = [ ih for ih in h if ih.head is not None ] # remove empty iterators 26 heapify(h) 27 prev = None 28 29 while h: 30 ih = h[0] 31 ihh = ih.head 32 if prev is None or ihh != prev: 33 yield ihh 34 prev = ihh 35 ih.fetch_next() 36 # ih.head has probably changed with fetch_next, don't use ihh anymore: 37 if ih.head is not None: 38 heapreplace(h, ih) 39 else: 40 heappop(h)
41