Created
November 13, 2009 17:17
-
-
Save chrisdickinson/234002 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import itertools | |
def complex_itermerge(compare, *iters): | |
_i = [iter(i) for i in iters] | |
backlog = [] | |
for _i_vals in itertools.izip(*_i): | |
assoc = zip(_i, _i_vals) | |
state_length = len(assoc) | |
for i in range(0, state_length): | |
lhs = assoc[i] | |
we_are_true = True | |
other_values = [] | |
for j in range(0, state_length): | |
if i == j: | |
continue | |
we_are_true = we_are_true and compare(lhs[1], assoc[j][1]) | |
other_values.append(assoc[j][1]) | |
if we_are_true: | |
for_removal = [] | |
for item in backlog: | |
if compare(item, lhs[1]): | |
yield item | |
for_removal.append(item) | |
for item in for_removal: | |
backlog.remove(item) | |
yield lhs[1] | |
backlog = backlog + other_values | |
for iterator in _i: | |
for _i_val in iterator: | |
for_removal = [] | |
for item in backlog: | |
if compare(item, _i_val): | |
yield item | |
for_removal.append(item) | |
for item in for_removal: | |
backlog.remove(item) | |
yield _i_val | |
for value in backlog: | |
yield value | |
raise StopIteration |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment