// 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); }
// Toposort 0, adding each node when finished, after its descendants. // Classic depth-first search. Does not terminate on cyclic graphs. public static IList <Node <T> > Toposort0 <T>(params Node <T>[] starts) { HashedLinkedList <Node <T> > sorted = new HashedLinkedList <Node <T> >(); foreach (Node <T> start in starts) { if (!sorted.Contains(start)) { AddNode0(sorted, start); } } return(sorted); }
public bool TryEnqueue(T item) { lock (@lock) { if (collection.Count < maxQueueLength && !collection.Contains(item)) { collection.InsertLast(item); return(true); } } return(false); }
// Toposort 1, using hash index to add each node before its descendants. // Terminates also on cyclic graphs. public static IList <MyNode <T> > Toposort1 <T>(params MyNode <T>[] starts) { var sorted = new HashedLinkedList <MyNode <T> >(); foreach (var start in starts) { if (!sorted.Contains(start)) { sorted.InsertLast(start); AddNode1(sorted, start); } } return(sorted); }
public void Main() { // Construct hashed array list using collection initializer var list = new HashedLinkedList <int> { 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59 }; // Chose from list list.Choose(); var array = new int[list.Count]; // Copy list to array list.CopyTo(array, 0); // Add item to the end of list list.Add(61); // Add range to list array = new[] { 67, 71, 73, 79, 83 }; list.AddRange(array); // Check if list contains an item list.Contains(list.Choose()); // Check if list contains all items in enumerable list.ContainsRange(array); // Count all occuerence of an item list.CountDuplicates(list.Choose()); // Find an item in list var itemToFind = list.Last; list.Find(ref itemToFind); // Return all occurence of an item list.FindDuplicates(itemToFind); // Remove within range list.RemoveIndexRange(0, 3); var range = new[] { list.First, list.Last }; // Remove all items in enumerable from list list.RemoveRange(range); // Retain all items in enumarable from list list.RetainRange(list.ToArray()); var lastItem = list.Last; // Find last index of an item list.LastIndexOf(lastItem); // Insert at the end of list list.InsertLast(100); // Insert at the beginning of list list.InsertFirst(-100); // Reverse list list.Reverse(); // Shuffle list list.Shuffle(); // Sort list list.Sort(); // Check if list is sorted var isSorted = list.IsSorted(); // Print all items in list by indexer var index = 0; foreach (var item in list) { Console.WriteLine($"list[{index++}] = {item}"); } // Clear list list.Clear(); }