Beispiel #1
0
        async public Task <SlowString> Last(SlowString other)
        {
            if (Debug)
            {
                Console.WriteLine("Poczatek Last");
            }

            SlowString result = await Task.Run <SlowString>(() =>
            {
                Thread.Sleep(Delay);

                int comparison = Value.CompareTo(other.Value);

                if (comparison < 0)
                {
                    return(other);
                }

                return(this);
            });

            if (Debug)
            {
                Console.WriteLine("Koniec Last");
            }

            return(result);
        }
Beispiel #2
0
        async public static Task <SlowString> Last(this SlowString[] array, bool debug = false)
        {
            if (array.Length == 0)
            {
                return(null);
            }

            SlowString result = array[0];

            for (int i = 1; i < array.Length; i++)
            {
                result = await result.Last(array[i]);
            }

            return(result);
        }
Beispiel #3
0
        async public Task <bool> Equal(SlowString other)
        {
            if (Debug)
            {
                Console.WriteLine("Początek Equal");
            }
            bool result = await Task.Run <bool>(() =>
            {
                Thread.Sleep(Delay);
                return(other.Value.Equals(Value));
            });

            if (Debug)
            {
                Console.WriteLine("Koniec Equal");
            }
            return(result);
        }
Beispiel #4
0
        async public Task <bool> GreaterThan(SlowString other)
        {
            if (Debug)
            {
                Console.WriteLine("Poczatek GreaterThan");
            }
            bool result = await Task.Run <bool>(() =>
            {
                Thread.Sleep(Delay);
                return(Value.CompareTo(other.Value) > 0);
            });

            if (Debug)
            {
                Console.WriteLine("Koniec GreaterThan");
            }

            return(result);
        }
Beispiel #5
0
        async public Task <SlowString> Concatenate(SlowString other)
        {
            if (Debug)
            {
                Console.WriteLine("Poczatek Concatenate");
            }

            SlowString result = await Task.Run <SlowString>(() =>
            {
                Thread.Sleep(Delay);

                return(new SlowString(Value + other.Value, Debug, Delay));
            });

            if (Debug)
            {
                Console.WriteLine("Koniec Concatenate");
            }
            return(result);
        }
Beispiel #6
0
        async public static Task <SlowString> Concatenate(this SlowString[] array)
        {
            if (array.Length == 0)
            {
                return(null);
            }

            SlowString[] elementsLeft = array;
            while (elementsLeft.Length > 1)
            {
                int newSize = (int)Math.Ceiling((decimal)elementsLeft.Length / 2);
                Task <SlowString>[] tasks = new Task <SlowString> [newSize];
                for (int i = 0; i < newSize; i++)
                {
                    if (2 * i + 1 >= elementsLeft.Length)
                    {
                        tasks[i] = Task.Run <SlowString>(() => elementsLeft[2 * i - 2]);
                    }
                    else
                    {
                        tasks[i] = elementsLeft[2 * i].Concatenate(elementsLeft[2 * i + 1]);
                    }
                }

                Task.WaitAll(tasks);

                SlowString[] newElements = new SlowString[newSize];
                for (int i = 0; i < tasks.Length; i++)
                {
                    newElements[i] = tasks[i].Result;
                }
                elementsLeft = newElements;
            }

            return(elementsLeft[0]);
        }
Beispiel #7
0
        private async static Task RunTestsAsync()
        {
            Console.WriteLine("=== ETAP 1 ===");
            var a = new SlowString("ab", true);
            var b = new SlowString("c", true);

            var result1 = a.Equal(b);
            var result2 = b.Last(b);
            var result3 = b.GreaterThan(a);

            result1.Wait();
            result2.Wait();
            result3.Wait();

            Console.WriteLine();
            Console.WriteLine("Results: {0},   {1},   {2}", result1.Result, result2.Result, result3.Result);
            Console.WriteLine();

            var result4 = await a.Equal(a);

            var result5 = await a.Last(b);

            var result6 = await a.GreaterThan(a);

            Console.WriteLine();
            Console.WriteLine("Results: {0},   {1},   {2}", result4, result5, result6);
            Console.WriteLine();

            Console.WriteLine("=== ETAP 2 ===");

            var array1 = new SlowString[]
            {
                new SlowString("a", true), new SlowString("b", true), new SlowString("abc", true), new SlowString("bc", true)
            };

            var last1 = await array1.Last(true);

            var array2 = new SlowString[60];

            for (int i = 0; i < 60; ++i)
            {
                array2[i] = new SlowString(i.ToString(), false, 1);
            }

            var last2 = await array2.Last();

            Console.WriteLine();
            Console.WriteLine("Results: {0},   {1}", last1, last2);
            Console.WriteLine();

            Console.WriteLine("=== ETAP 3 ===");

            var max1 = array1.Concatenate();
            var max2 = array2.Concatenate();

            max1.Wait();
            max2.Wait();

            Console.WriteLine();
            Console.WriteLine("Results: {0},   {1}", max1.Result, max2.Result);
            Console.WriteLine();
        }