internal static IEnumerable <IEnumerable <TNode> > FindOrderedItems(IEnumerable <TNode> database) { var seedEnumerator = database.GetEnumerator(); if (seedEnumerator.MoveNext()) { var seedItem = new TreePredictableItem <TNode>(null, seedEnumerator); var queue = new List <TreePredictableItem <TNode> > { seedItem }; while (queue.Count != 0) { var item = queue[0]; queue.RemoveAt(0); if (item._isReal) { yield return(item); if (item._enumerator.MoveNext()) { var nextItem = new TreePredictableItem <TNode>(item._parent, item._enumerator); nextItem.Enqueue(queue); } else { item._enumerator.Dispose(); } if (1 < item._count) { item.MakePotential(); item.Enqueue(queue); } } else { item.ExpandPotentialParent(queue); } } } }
private void ExpandPotentialParent(List <TreePredictableItem <TNode> > queue) { var item = this; var done = false; while (!done) { var node = item._node; var enumerator = node.GetChildren().GetEnumerator(); if (enumerator.MoveNext()) { var firstNode = enumerator.Current; if (enumerator.MoveNext()) { item = new TreePredictableItem <TNode>(item, enumerator); item.Enqueue(queue); done = true; } else { // Walking down single branch. enumerator.Dispose(); item = new TreePredictableItem <TNode>(item, firstNode); } } else { // No potential next node. enumerator.Dispose(); done = true; } } }