Ejemplo n.º 1
0
 public void RotateRendezvousServers()
 {
     lock (@lock)
     {
         config.InsertLast(config.RemoveFirst());
     }
 }
Ejemplo n.º 2
0
        // 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);
        }
Ejemplo n.º 3
0
        public IDestination SelectNextDestination(params IDestination[] receivers)
        {
            receivers = receivers.Where(d => d != null)
                        .ToArray();
            if (receivers.Any())
            {
                var indexes = new List <int>();
                foreach (var receiver in receivers)
                {
                    var index = destinations.IndexOf(receiver);
                    if (index < 0)
                    {
                        logger.Warn($"Destination [{receiver}] is not found in {GetType().Name}");
                        return(receiver);
                    }
                    indexes.Add(index);
                }

                var nextDestinationIndex = indexes.Min();
                var destination          = destinations.RemoveAt(nextDestinationIndex);
                destinations.InsertLast(destination);

                return(destination);
            }

            return(null);
        }
Ejemplo n.º 4
0
 public void RotateRendezvousServers()
 {
     lock (@lock)
     {
         var oldCurrent = config.RemoveFirst();
         oldCurrent.RefreshUri();
         config.InsertLast(oldCurrent);
     }
 }
Ejemplo n.º 5
0
        public bool TryEnqueue(T item)
        {
            lock (@lock)
            {
                if (collection.Count < maxQueueLength && !collection.Contains(item))
                {
                    collection.InsertLast(item);
                    return(true);
                }
            }

            return(false);
        }
Ejemplo n.º 6
0
        // Toposort 1, using hash index to add each node before its descendants.
        // Terminates also on cyclic graphs.

        public static IList <Node <T> > Toposort1 <T>(params Node <T>[] starts)
        {
            HashedLinkedList <Node <T> > sorted = new HashedLinkedList <Node <T> >();

            foreach (Node <T> start in starts)
            {
                if (!sorted.Contains(start))
                {
                    sorted.InsertLast(start);
                    AddNode1(sorted, start);
                }
            }
            return(sorted);
        }
Ejemplo n.º 7
0
        private static T Get <T>(HashedLinkedList <T> hashSet)
        {
            var count = hashSet.Count;

            if (count > 0)
            {
                var first = (count > 1) ? hashSet.RemoveFirst() : hashSet.First;
                if (count > 1)
                {
                    hashSet.InsertLast(first);
                }
                return(first);
            }

            return(default(T));
        }
Ejemplo n.º 8
0
        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();
        }