Last active
January 4, 2016 01:38
-
-
Save beyond-code-github/8549366 to your computer and use it in GitHub Desktop.
Efficient Iterator sample
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
public class EfficientIterator<TOne, TTwo> | |
{ | |
private Action<TOne, TTwo> always = (one, two) => { }; | |
private Action<TOne, TTwo> match = (one, two) => { }; | |
private Action<TOne> oneOnly = (one) => { }; | |
private Action<TTwo> twoOnly = (two) => { }; | |
private readonly Func<TOne, TTwo, int> compare; | |
public Action<TOne, TTwo> Always { set { this.always = value; } } | |
public Action<TOne, TTwo> Match { set { this.match = value; } } | |
public Action<TOne> OneOnly { set { this.oneOnly = value; } } | |
public Action<TTwo> TwoOnly { set { this.twoOnly = value; } } | |
public EfficientIterator(Func<TOne, TTwo, int> compare) | |
{ | |
this.compare = compare; | |
} | |
public void RunToEnd(IEnumerable<TOne> listOne, IEnumerable<TTwo> listTwo) | |
{ | |
using (var listOneEnumerator = listOne.GetEnumerator()) | |
using (var listTwoEnumerator = listTwo.GetEnumerator()) | |
{ | |
var listOneIterating = listOneEnumerator.MoveNext(); | |
var listTwoIterating = listTwoEnumerator.MoveNext(); | |
while (listOneIterating || listTwoIterating) | |
{ | |
int result = 0; | |
if (listOneIterating && listTwoIterating) | |
{ | |
result = this.compare(listOneEnumerator.Current, listTwoEnumerator.Current); | |
// a and b are equal | |
if (result == 0) | |
{ | |
this.always(listOneEnumerator.Current, listTwoEnumerator.Current); | |
this.match(listOneEnumerator.Current, listTwoEnumerator.Current); | |
listOneIterating = listOneEnumerator.MoveNext(); | |
listTwoIterating = listTwoEnumerator.MoveNext(); | |
continue; | |
} | |
} | |
// list one has run out, or it's current item is greater than list two | |
if (!listOneIterating || result > 0) | |
{ | |
this.always(default(TOne), listTwoEnumerator.Current); | |
this.twoOnly(listTwoEnumerator.Current); | |
listTwoIterating = listTwoEnumerator.MoveNext(); | |
continue; | |
} | |
// list two has run out, or it's current item is greater than list one | |
if (!listTwoIterating || result < 0) | |
{ | |
this.always(listOneEnumerator.Current, default(TTwo)); | |
this.oneOnly(listOneEnumerator.Current); | |
listOneIterating = listOneEnumerator.MoveNext(); | |
continue; | |
} | |
} | |
} | |
} | |
} |
Awesome thanks! I'd just come on to make the change in the second point and seen that you've beaten me to it. Good job :)
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi Roy, I forked your sample and did some modifications:
where TOne : class, where TTwo : class
) so value types can also be used (this required replacingnull
withdefault(TOne)
anddefault(TTwo)
when invoking thealways
-action with a missing value).RunToEnd
fromList<T>
toIEnumerable<T>
. This enables you to pass in collections of any type that implementsIEnumerable<T>
, not justList<T>
.using
-construct to properly dispose the enumerators after using them.See https://gist.github.com/tommy-carlier/8556273 (or the above 3 changes as separate revisions).