// Toposort 2, node rescanning using a view. // Uses no method call stack and no extra data structures, but slower. public static IList <MyNode <T> > Toposort2 <T>(params MyNode <T>[] starts) { var sorted = new HashedLinkedList <MyNode <T> >(); foreach (MyNode <T> start in starts) { if (!sorted.Contains(start)) { sorted.InsertLast(start); using (IList <MyNode <T> > cursor = sorted.View(sorted.Count - 1, 1)) { do { MyNode <T> child; while ((child = PendingChild(sorted, cursor.First)) != null) { cursor.InsertFirst(child); cursor.Slide(0, 1); } }while (cursor.TrySlide(+1)); } } } return(sorted); }
public void Main() { var list = new HashedLinkedList <int> { 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59 }; // View of item list.ViewOf(2); // Last view of item list.LastViewOf(11); // View in range var view = list.View(0, 3); // Slide with offset view.Slide(3); // Slide with offset and set lenght view.Slide(-3, 3); // Try slide with offset and set lenght var hasSucceed = view.TrySlide(-10, 3); // Try slide with offset and set lenght view.TrySlide(3, -3); var view2 = list.View(0, 3); var view3 = list.View(1, 3); // Check if views overlap Overlap(view2, view3); // Check if views contained in each other ContainsView(view2, view3); // Span views var spannedView = view2.Span(view3); // Invalidate all views by shuffle list.Shuffle(); // Check if view is valid Console.WriteLine($"View is valid? {view.IsValid}"); }
public bool TryPeek(out System.Collections.Generic.IList <T> items, int count) { items = new List <T>(); lock (@lock) { var dequeueCount = Math.Min(collection.Count, count); ((List <T>)items).AddRange(collection.View(0, dequeueCount)); } return(items.Any()); }