public static IEnumerable <IMatch> AsEnumerable(this ISearchQuery query) { var match = query.NextMatch(); while (match != null) { yield return(match); match = query.NextMatch(); } }
public IMatch NextMatch() { while (true) { switch (state) { case State.Null: return(null); case State.Initial: leftMatch = leftQuery.NextMatch(); rightMatch = rightQuery.NextMatch(); if (leftMatch == null && rightMatch == null) { state = State.Null; return(null); } else if (leftMatch != null && rightMatch != null) { state = State.Merge; } else { state = State.Tail; return(leftMatch ?? rightMatch); } break; case State.AdvanceLeft: leftMatch = leftQuery.NextMatch(); if (leftMatch == null) { state = State.Tail; return(rightMatch); } state = State.Merge; break; case State.AdvanceRight: rightMatch = rightQuery.NextMatch(); if (rightMatch == null) { state = State.Tail; return(leftMatch); } state = State.Merge; break; case State.Tail: if (leftMatch == null) { rightMatch = rightQuery.NextMatch(); if (rightMatch == null) { state = State.Null; return(null); } return(rightMatch); } if (rightMatch == null) { leftMatch = leftQuery.NextMatch(); if (leftMatch == null) { state = State.Null; return(null); } return(leftMatch); } throw new InvalidOperationException(); case State.Merge: if (leftMatch.Left < rightMatch.Left) { state = State.AdvanceLeft; return(leftMatch); } else { state = State.AdvanceRight; return(rightMatch); } default: throw new NotSupportedException(); } } }