Exemplo n.º 1
0
        /// <summary>
        /// Moves the generation one tick ahead (in a immutable fashion.)
        /// </summary>
        /// <returns>The next generation (Star Wars reference win!)</returns>
        public Generation Tick()
        {
            // A little LINQ to calculate the cells which should be allowed to stay alive.
            var stayAlive = from c in _coords.AsParallel()
                            let count = GetLiveNeighboursCount(c.Key)
                                        where count >= TooFewLimit && count <= TooManyLimit
                                        select c;

            // Easily calculate the 'dead' neighbouring cells (then we will check the freq. of the dead neighbouring cells and if freq. = 3, BOOM! LIFE!)
            var newBorn = _coords.AsParallel()
                          .SelectMany(c => GetDeadNeighbours(c.Key))
                          .Distinct()
                          .Where(c => GetLiveNeighboursCount(c) == PerfectAmount)
                          .Select(c => new KeyValuePair <Coordinate, Cell>(c, new Cell(CellState.Alive)));

            return(new Generation(stayAlive.Union(newBorn).OrderBy(cc => cc.Key)));
        }
Exemplo n.º 2
0
        public static void SortedListDemo()
        {
            SortedList sortedList = new SortedList
            {
                { 3, 1 },
                { 15, 5 },
                { 5, 55 },
                { 9, 55 }
            };
            ParallelQuery pq = sortedList.AsParallel();

            foreach (DictionaryEntry entry in sortedList)
            {
                Console.WriteLine(entry.Key + " " + entry.Value);
            }
        }
Exemplo n.º 3
0
        /// <summary>
        /// returns a new sorted ICollection
        /// </summary>
        /// <param name="source"></param>
        /// <returns>An Enumerable sorted collection by LastName</returns>
        public async static Task <ICollection <string> > SortByLastNameAsync(this IEnumerable <string> source)
        {
            if (source == null)
            {
                throw new ArgumentNullException(nameof(source));
            }

            int rowsCount = source.Count();

            if (rowsCount == 0)
            {
                return((ICollection <string>)source);
            }

            byte   lastspaceIndex = 0;
            string firstName, lastName = null;
            var    orderedList = new SortedList <string, string>()
            {
                Capacity = rowsCount
            };

            await Task.Run(() =>
            {
                foreach (var item in source)
                {
                    var spaces     = item.Count(char.IsWhiteSpace);
                    lastspaceIndex = (byte)item.IndexOfNth(' ', spaces - 1);

                    firstName          = item.Substring(0, lastspaceIndex);
                    var lastNameLength = item.Length - firstName.Length;
                    lastName           = item.Substring(lastspaceIndex, lastNameLength);

                    orderedList.Add(lastName, firstName);
                }
            });

            return(orderedList.AsParallel()
                   .AsOrdered()
                   .Select((fullName) => fullName.Value + fullName.Key).ToList());
        }
Exemplo n.º 4
0
        static void Main(string[] args)
        {
            SortedList <string, string> s = new SortedList <string, string>();

            s.Add("9", "languages");
            s.Add("1", "Bigdata");
            s.Add("8", "AI");
            s.Add("2", "Java");
            s.Add("4", "Haddop");
            s.Add("6", "CSharp");
            s.Add("5", "Andriod");
            s.Add("3", "python");
            Console.WriteLine("the elements in the sorted dictionary is");
            foreach (KeyValuePair <string, string> sd in s)
            {
                Console.Write(sd.Key + " " + sd.Value);
                Console.WriteLine();
            }
            Console.WriteLine(s.AsParallel());

            Console.ReadLine();
        }
Exemplo n.º 5
0
        /// <summary>
        /// returns a new sorted ICollection
        /// </summary>
        /// <param name="source"></param>
        /// <returns>An Enumerable sorted collection by LastName</returns>
        public static async Task <ICollection <string> > SortByLastNameAsync(this IEnumerable <string> source)
        {
            Guard.Break.IfArgumentIsNull(source, nameof(source));

            var enumerable = source as string[] ?? source.ToArray();
            var rowsCount  = enumerable.Count();

            if (rowsCount == 0)
            {
                return((ICollection <string>)source);
            }

            byte   lastSpaceIndex;
            string firstName, lastName;
            var    orderedList = new SortedList <string, string>()
            {
                Capacity = rowsCount
            };

            await Task.Run(() =>
            {
                foreach (var item in enumerable)
                {
                    var spaces     = item.Count(char.IsWhiteSpace);
                    lastSpaceIndex = (byte)item.IndexOfNth(' ', spaces - 1);

                    firstName          = item.Substring(0, lastSpaceIndex);
                    var lastNameLength = item.Length - firstName.Length;
                    lastName           = item.Substring(lastSpaceIndex, lastNameLength);

                    orderedList.Add(lastName, firstName);
                }
            });

            return(orderedList.AsParallel().AsOrdered()
                   .Select((fullName) => fullName.Value + fullName.Key).ToList());
        }
Exemplo n.º 6
0
        public SortedList <TKey, TValue> TailMap(TKey key)
        {
            SortedList <TKey, TValue> lstR = new SortedList <TKey, TValue>();
            var map = lst.AsParallel().Where((k) =>
            {
                int result = k.Key.CompareTo(key);
                if (result >= 0)
                {
                    return(true);
                }
                else
                {
                    return(false);
                }
            });
            var r = map.ToList();

            lstR = new SortedList <TKey, TValue>(r.Count);
            foreach (var item in r)
            {
                lstR.Add(item.Key, item.Value);
            }
            return(lstR);
        }