예제 #1
0
        async public Task <SlowInt> Max(SlowInt other)
        {
            if (_showDebugInfo)
            {
                Console.WriteLine("SlowInt Max started");
            }

            SlowInt result = await Task.Run <SlowInt>(() =>
            {
                Thread.Sleep(_delay);
                if (other == null)
                {
                    return(this);
                }

                return(_value < other._value ? other : this);
            });

            if (_showDebugInfo)
            {
                Console.WriteLine("SlowInt Max finished");
            }

            return(result);
        }
예제 #2
0
        async public static Task <SlowInt> Sum(this SlowInt[] array, bool showDebugInfo = false)
        {
            SlowInt result = new SlowInt(0, showDebugInfo);

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

            return(result);
        }
예제 #3
0
        async public Task <SlowInt> Add(SlowInt other)
        {
            if (_showDebugInfo)
            {
                Console.WriteLine("SlowInt Add started");
            }
            SlowInt result = await Task.Run <SlowInt>(() => {
                Thread.Sleep(_delay);
                return(new SlowInt(_value + other._value, _showDebugInfo, _delay));;
            });

            if (_showDebugInfo)
            {
                Console.WriteLine("SlowInt Add finished");
            }
            return(result);
        }
예제 #4
0
        async public Task <bool> LowerThen(SlowInt other)
        {
            if (_showDebugInfo)
            {
                Console.WriteLine("SlowInt LowerThen started");
            }
            bool result = await Task.Run <bool>(() => {
                Thread.Sleep(_delay);
                return(other._value >= _value);
            });

            if (_showDebugInfo)
            {
                Console.WriteLine("SlowInt LowerThen finished");
            }
            return(result);
        }
예제 #5
0
        async public static Task <SlowInt> Max(this SlowInt[] array)
        {
            SlowInt[] elementsLeft = array;
            while (elementsLeft.Length > 1)
            {
                // Nie umiem użyć Math.Ceiling xD
                int newSize = elementsLeft.Length / 2;
                if (elementsLeft.Length % 2 == 1)
                {
                    newSize++;
                }

                Task <SlowInt>[] biggerElements = new Task <SlowInt> [newSize];
                for (int i = 0; i < newSize; i++)
                {
                    if (elementsLeft.Length == 2 * i + 1)
                    {
                        // Trzeba zapisać jako oddzielna zmienna, w innym przypadku i nie będzie poprawne
                        int lastIndex = 2 * i;
                        biggerElements[i] = Task.Run(() => elementsLeft[lastIndex]);
                    }
                    else
                    {
                        biggerElements[i] = elementsLeft[2 * i].Max(elementsLeft[2 * i + 1]);
                    }
                }

                Task.WaitAll(biggerElements);
                elementsLeft = new SlowInt[newSize];
                for (int i = 0; i < newSize; i++)
                {
                    elementsLeft[i] = biggerElements[i].Result;
                }
            }

            return(elementsLeft[0]);
        }
예제 #6
0
        private async static Task RunTestsAsync()
        {
            Console.WriteLine("=== ETAP 1 ===");
            var a = new SlowInt(10, true);
            var b = new SlowInt(5, true);

            var result1 = a.Equal(b);
            var result2 = b.Add(b);
            var result3 = b.LowerThen(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.Add(b);

            var result6 = await a.LowerThen(a);

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

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

            var array1 = new SlowInt[]
            {
                new SlowInt(5), new SlowInt(4), new SlowInt(12), new SlowInt(2)
            };

            var sum1 = await array1.Sum(true);

            var array2 = new SlowInt[60];

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

            var sum2 = await array2.Sum();

            Console.WriteLine();
            Console.WriteLine("Results: {0},   {1}", sum1, sum2);
            Console.WriteLine();

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

            array1 = new SlowInt[]
            {
                new SlowInt(5, true), new SlowInt(4, true), new SlowInt(12, true), new SlowInt(2, true)
            };
            var max1 = array1.Max();
            var max2 = array2.Max();

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

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