Exemplo n.º 1
0
        private static void SwapExample()
        {
            var a = 10;
            var b = 20;

            Swapper.Swap(ref a, ref b);
            // V premennych by mali byt tieto hodnoty:
            // a = 20, b = 10
            Console.WriteLine($"{a} {b}");

            var d1 = Math.PI;
            var d2 = Math.E;

            Swapper.Swap(ref d1, ref d2);
            // V premennych by mali byt tieto hodnoty:
            // d1 = Math.E, d2 = Math.PI
            Console.WriteLine($"{d1} {d2}");

            var hello = "Ahoj";
            var world = "svet";

            Swapper.Swap(ref hello, ref world);
            // V premennych by mali byt tieto hodnoty:
            // hello = "svet", world = "Ahoj"
            Console.WriteLine($"{hello} {world}");

            // Pozadovany vystup:
            // 20 10
            // 2,718281828459045 3,141592653589793
            // svet Ahoj
        }
Exemplo n.º 2
0
        public static bool ContainsAny(FlagSet a, FlagSet b)
        {
            if (a == null || a.IsEmpty || b == null || b.IsEmpty)
            {
                return(false);
            }
            if (a.Count == 1)
            {
                return(b.Contains(a[0]));
            }
            if (b.Count == 1)
            {
                return(a.Contains(b[0]));
            }

            if (a.Count > b.Count)
            {
                Swapper.Swap(ref a, ref b);
            }

            foreach (var item in a)
            {
                if (b.Contains(item))
                {
                    return(true);
                }
            }

            return(false);
        }
        public SortingResult Sort(Data[] array)
        {
            SortingResult sortingResult = new SortingResult {
                ArrayOfData = array
            };
            var watch = Stopwatch.StartNew();

            for (int i = 1; i < array.Length; i++)
            {
                bool continueComparing = true;
                for (int g = i; g > 0 && continueComparing; g--)
                {
                    continueComparing = (_compareMethod(array[g - 1], array[g]) == 1);
                    sortingResult.CountOfCompares++;

                    if (continueComparing)
                    {
                        Swapper.Swap(ref array[g], ref array[g - 1]);
                        sortingResult.CountOfSwaps++;
                    }
                }
            }


            watch.Stop();
            sortingResult.Time = watch.Elapsed;

            return(sortingResult);
        }
        public SortingResult Sort(Data[] array)
        {
            SortingResult sortingResult = new SortingResult {
                ArrayOfData = array
            };
            var  watch     = Stopwatch.StartNew();
            bool isOrdered = false;

            for (int i = 0; i < array.Length && !isOrdered; i++)
            {
                for (int g = 0; g < (array.Length - 1); g++)
                {
                    sortingResult.CountOfCompares++;
                    if (_compareMethod(array[g], array[g + 1]) == 1)
                    {
                        Swapper.Swap(ref array[g], ref array[g + 1]);
                        sortingResult.CountOfSwaps++;
                    }
                }

                isOrdered = CheckState(array, sortingResult);
            }

            watch.Stop();
            sortingResult.Time = watch.Elapsed;

            return(sortingResult);
        }
        public SortingResult Sort(Data[] array)
        {
            SortingResult sortingResult = new SortingResult {
                ArrayOfData = array
            };
            var watch = Stopwatch.StartNew();

            for (int i = 0; i < array.Length; i++)
            {
                for (int g = 0; g < (array.Length - 1); g++)
                {
                    sortingResult.CountOfCompares++;
                    if (_compareMethod.Invoke(array[g], array[g + 1]) == 1)
                    {
                        Swapper.Swap(ref array[g], ref array[g + 1]);
                        sortingResult.CountOfSwaps++;
                    }
                }
            }

            watch.Stop();
            sortingResult.Time = watch.Elapsed;


            return(sortingResult);
        }
Exemplo n.º 6
0
        private (double tMin, double tMax) CheckAxis(double origin, double direction, double min, double max)
        {
            var tMinNumerator = min - origin;
            var tMaxNumerator = max - origin;

            double tMin, tMax;

            if (Math.Abs(direction) >= C.Epsilon)
            {
                tMin = tMinNumerator / direction;
                tMax = tMaxNumerator / direction;
            }
            else
            {
                tMin = tMinNumerator * double.PositiveInfinity;
                tMax = tMaxNumerator * double.PositiveInfinity;
            }

            if (tMin > tMax)
            {
                Swapper.Swap(ref tMin, ref tMax);
            }

            return(tMin, tMax);
        }
Exemplo n.º 7
0
        private IEnumerable <Intersection> IntersectWalls(Ray localRay)
        {
            var a = Math.Pow(localRay.Direction.x, 2) - Math.Pow(localRay.Direction.y, 2) + Math.Pow(localRay.Direction.z, 2);

            var b =
                2 * localRay.Origin.x * localRay.Direction.x -
                2 * localRay.Origin.y * localRay.Direction.y +
                2 * localRay.Origin.z * localRay.Direction.z;

            var c =
                Math.Pow(localRay.Origin.x, 2) -
                Math.Pow(localRay.Origin.y, 2) +
                Math.Pow(localRay.Origin.z, 2);

            if (Math.Abs(a) < C.Epsilon)
            {
                // Ray is parallell to one of the cones halves
                if (Math.Abs(b) < C.Epsilon)
                {
                    yield break;
                }
                var t = -c / (2 * b);
                var y = localRay.Origin.y + t * localRay.Direction.y;
                if (Min < y && y < Max)
                {
                    yield return(new Intersection(t, this));
                }
                yield break;
            }

            var disc = Math.Pow(b, 2) - 4 * a * c;

            if (disc < 0)
            {
                // Ray does not intersect the cone
                yield break;
            }

            var t0 = (-b - Math.Sqrt(disc)) / (2 * a);
            var t1 = (-b + Math.Sqrt(disc)) / (2 * a);

            if (t0 > t1)
            {
                Swapper.Swap(ref t0, ref t1);
            }

            var y0 = localRay.Origin.y + t0 * localRay.Direction.y;

            if (Min < y0 && y0 < Max)
            {
                yield return(new Intersection(t0, this));
            }

            var y1 = localRay.Origin.y + t1 * localRay.Direction.y;

            if (Min < y1 && y1 < Max)
            {
                yield return(new Intersection(t1, this));
            }
        }
Exemplo n.º 8
0
        internal int Pivot(IList array, int lower, int upper)
        {
            // Pivot with first element
            var left  = lower + 1;
            var pivot = array[lower];
            var right = upper;

            // Partition array elements
            while (left <= right)
            {
                // Find item out of place
                while ((left <= right) && (Comparer.Compare(array[left], pivot) <= 0))
                {
                    ++left;
                }

                while ((left <= right) && (Comparer.Compare(array[right], pivot) > 0))
                {
                    --right;
                }

                // Swap values if necessary
                if (left < right)
                {
                    Swapper.Swap(array, left, right);
                    ++left;
                    --right;
                }
            }

            // Move pivot element
            Swapper.Swap(array, lower, right);
            return(right);
        }
Exemplo n.º 9
0
        private void DownHeap(IList list, int k, int n)
        {
            var loop = true;

            while (k <= n / 2 && loop)
            {
                var j = k + k;
                if (j < n)
                {
                    if (Comparer.Compare(list[j - 1], list[j]) < 0)
                    {
                        j++;
                    }
                }

                if (Comparer.Compare(list[k - 1], list[j - 1]) >= 0)
                {
                    loop = false;
                }
                else
                {
                    Swapper.Swap(list, k - 1, j - 1);
                    k = j;
                }
            }
        }
Exemplo n.º 10
0
        public override void Sort(IList list)
        {
            var i = 0;
            var k = list.Count - 1;

            while (i < k)
            {
                var min = i;
                var max = i;
                int j;
                for (j = i + 1; j <= k; j++)
                {
                    if (Comparer.Compare(list[j], list[min]) < 0)
                    {
                        min = j;
                    }

                    if (Comparer.Compare(list[j], list[max]) > 0)
                    {
                        max = j;
                    }
                }

                Swapper.Swap(list, min, i);
                Swapper.Swap(list, max == i ? min : max, k);
                i++;
                k--;
            }
        }
Exemplo n.º 11
0
    protected void dgTroca_ItemCommand(object sender, DataGridCommandEventArgs e)
    {
        if (e.CommandName == "Aceitou")
        {
            List <Troca> trocas = (List <Troca>)Session["Trocas"];
            try
            {
                Troca troca = trocas[e.Item.ItemIndex];
                troca.EstaPendente   = false;
                troca.FoiAceita      = true;
                troca.FoiVisualizada = true;

                Aula   aulaAtual      = troca.AlocacaoAtual.Aula;
                Aula   aulaDesejada   = troca.AlocacaoDesejada.Aula;
                Evento eventoAtual    = troca.AlocacaoAtual.Evento;
                Evento eventoDesejado = troca.AlocacaoDesejada.Evento;

                Swapper.Swap <Aula>(ref aulaAtual, ref aulaDesejada);
                Swapper.Swap <Evento>(ref eventoAtual, ref eventoDesejado);

                trocaBO.UpDateTroca(troca);

                troca.AlocacaoAtual.Aula      = aulaAtual;
                troca.AlocacaoDesejada.Aula   = aulaDesejada;
                troca.AlocacaoAtual.Evento    = eventoAtual;
                troca.AlocacaoDesejada.Evento = eventoDesejado;

                alocBO.UpdateAlocacao(troca.AlocacaoAtual);
                alocBO.UpdateAlocacao(troca.AlocacaoDesejada);

                VerificaTrocas();
            }
            catch (Exception)
            {
                Response.Redirect("~/Default/Erro.aspx?Erro=Erro ao aceitar a troca.");
            }
        }

        if (e.CommandName == "Recusou")
        {
            List <Troca> trocas = (List <Troca>)Session["Trocas"];
            try
            {
                Troca troca = trocas[e.Item.ItemIndex];
                troca.EstaPendente   = false;
                troca.FoiAceita      = false;
                troca.FoiVisualizada = true;

                trocaBO.UpDateTroca(troca);

                VerificaTrocas();
            }
            catch (Exception)
            {
                Response.Redirect("~/Default/Erro.aspx?Erro=Erro ao recusar a troca.");
            }
        }
    }
Exemplo n.º 12
0
        public void Swap_WithValidArray_IsSuccessful()
        {
            var a = new [] { 1, 2 };

            Swapper <int> .Swap(a, 0, 1);

            Assert.Equal(2, a[0]);
            Assert.Equal(1, a[1]);
        }
Exemplo n.º 13
0
        static void NodeTest()
        {
            Console.WriteLine("I'm testing node");
            string     done       = "";
            Node <int> linkedList = new Node <int>(0);
            Node <int> root       = linkedList;

            while (done != "y")
            {
                Console.WriteLine("Insert an integer for Linked List");
                int value = Convert.ToInt32(Console.ReadLine());
                linkedList.next = new Node <int>(value);
                linkedList      = linkedList.next;
                Console.WriteLine("Are you done adding y/n?");
                done = Console.ReadLine().ToLower();
            }

            Node <int> List = new Node <int>(8);

            List.next = new Node <int>(4, List.root);
            List++;
            List.next = new Node <int>(1, List.root);
            List++;
            List.next = new Node <int>(2, List.root);
            List++;
            List.next = new Node <int>(3, List.root);
            List++;
            List.next = new Node <int>(7, List.root);
            List++;
            List = List.root;
            List.Print();
            List.next.Print();
            Swapper <int> .Swap(List.next, List.next.next);

            Console.WriteLine("THis is swaped virsooooon");
            List.Print();
            //Console.WriteLine("...");
            List.next.Print();

            /*
             *  Node<double> n0 = new Node<double>(0.0);
             *  n0.next = new Node<double>(1.0);
             *  Node<double> www = n0[0];
             *  n0[0].Print();
             *  if(n0 == n1)
             *  {
             *      Console.WriteLine("they are equal");
             *  }
             *  else
             *  {
             *      Console.WriteLine("they are NOT equal");
             *  }
             */
            //       linkedList.Print();
            //       linkedList.PrintAll();
        }
Exemplo n.º 14
0
        public void SwapperSwapsStringBoxesInList()
        {
            List <Box <string> > numbers = new List <Box <string> >();

            numbers.Add(new Box <string>("Pesho"));
            numbers.Add(new Box <string>("Ivan"));


            Swapper.Swap(numbers, 0, 1);
            Assert.IsTrue(numbers[0].Item == "Ivan" && numbers[1].Item == "Pesho");
        }
Exemplo n.º 15
0
        /// <summary>
        /// This is a generic version of C.A.R Hoare's Quick Sort
        /// algorithm.  This will handle arrays that are already
        /// sorted, and arrays with duplicate keys.
        /// </summary>
        /// <remarks>
        /// If you think of a one dimensional array as going from
        /// the lowest index on the left to the highest index on the right
        /// then the parameters to this function are lowest index or
        /// left and highest index or right.  The first time you call
        /// this function it will be with the parameters 0, a.length - 1.
        /// </remarks>
        /// <param name="list">list to sort</param>
        /// <param name="l">left boundary of array partition</param>
        /// <param name="r">right boundary of array partition</param>
        internal void QuickSort(IList list, int l, int r)
        {
            const int m = 4;

            if (r - l <= m)
            {
                return;
            }

            var i = (r + l) / 2;

            if (Comparer.Compare(list[l], list[i]) > 0)
            {
                Swapper.Swap(list, l, i); // Tri-Median Methode!
            }

            if (Comparer.Compare(list[l], list[r]) > 0)
            {
                Swapper.Swap(list, l, r);
            }

            if (Comparer.Compare(list[i], list[r]) > 0)
            {
                Swapper.Swap(list, i, r);
            }

            var j = r - 1;

            Swapper.Swap(list, i, j);
            i = l;
            var v = list[j];

            for (;;)
            {
                while (Comparer.Compare(list[++i], v) > 0)
                {
                }

                while (Comparer.Compare(list[--j], v) < 0)
                {
                }

                if (j < i)
                {
                    break;
                }

                Swapper.Swap(list, i, j);
            }

            Swapper.Swap(list, i, r - 1);
            QuickSort(list, l, j);
            QuickSort(list, i + 1, r);
        }
Exemplo n.º 16
0
        public void SwapperSwapsBoxesInList()
        {
            List <Box <int> > numbers = new List <Box <int> >();

            numbers.Add(new Box <int>(2));
            numbers.Add(new Box <int>(5));


            Swapper.Swap(numbers, 0, 1);
            Assert.IsTrue(numbers[0].Item == 5 && numbers[1].Item == 2);
        }
Exemplo n.º 17
0
 public override void Sort(IList list)
 {
     for (var i = list.Count; --i >= 0;)
     {
         for (var j = 0; j < i; j++)
         {
             if (Comparer.Compare(list[j], list[j + 1]) > 0)
             {
                 Swapper.Swap(list, j, j + 1);
             }
         }
     }
 }
Exemplo n.º 18
0
        internal void SortPart1(IList list, int Lo, int Hi, int Nx, bool Up)
        {
            int c;

            for (int j = Lo; j + Nx < Hi; j += 2 * Nx)
            {
                c = Comparer.Compare(list[j], list[j + Nx]);
                if ((Up && c > 0) || !Up && c < 0)
                {
                    Swapper.Swap(list, j, j + Nx);
                }
            }
        }
Exemplo n.º 19
0
        private IEnumerable <Intersection> IntersectWalls(Ray localRay)
        {
            var a = Math.Pow(localRay.Direction.x, 2) + Math.Pow(localRay.Direction.z, 2);

            if (a < C.Epsilon)
            {
                // Ray is parallell to the y axis
                yield break;
            }

            var b =
                2 * localRay.Origin.x * localRay.Direction.x +
                2 * localRay.Origin.z * localRay.Direction.z;

            var c =
                Math.Pow(localRay.Origin.x, 2) +
                Math.Pow(localRay.Origin.z, 2) -
                1;

            var disc = Math.Pow(b, 2) - 4 * a * c;

            if (disc < 0)
            {
                // Ray does not intersect the cylinder
                yield break;
            }

            var t0 = (-b - Math.Sqrt(disc)) / (2 * a);
            var t1 = (-b + Math.Sqrt(disc)) / (2 * a);

            if (t0 > t1)
            {
                Swapper.Swap(ref t0, ref t1);
            }

            var y0 = localRay.Origin.y + t0 * localRay.Direction.y;

            if (Min < y0 && y0 < Max)
            {
                yield return(new Intersection(t0, this));
            }

            var y1 = localRay.Origin.y + t1 * localRay.Direction.y;

            if (Min < y1 && y1 < Max)
            {
                yield return(new Intersection(t1, this));
            }
        }
Exemplo n.º 20
0
 public void Possess()
 {
     if (Input.GetKeyDown(KeyCode.Space))
     {
         foreach (Collider obj in Physics.OverlapSphere(transform.position, 3))
         {
             Swapper swap = obj.GetComponent <Swapper>();
             if (swap != null && swap.transform != GetComponent <Swapper>().transform)
             {
                 swap.Swap();
                 GetComponent <Swapper>().Swap();
             }
         }
     }
 }
Exemplo n.º 21
0
        /// <summary>
        /// This is a generic version of C.A.R Hoare's Quick Sort
        /// algorithm.  This will handle arrays that are already
        /// sorted, and arrays with duplicate keys.
        /// </summary>
        /// <remarks>
        /// If you think of a one dimensional array as going from
        /// the lowest index on the left to the highest index on the right
        /// then the parameters to this function are lowest index or
        /// left and highest index or right.  The first time you call
        /// this function it will be with the parameters 0, a.length - 1.
        /// </remarks>
        /// <param name="list">list to sort</param>
        /// <param name="l">left boundary of array partition</param>
        /// <param name="r">right boundary of array partition</param>
        internal void QuickSort(IList <T> list, int l, int r)
        {
            int M = 4;
            int i;
            int j;
            T   v;

            if ((r - l) > M)
            {
                i = (r + l) / 2;
                if (Comparer.Compare(list[l], list[i]) > 0)
                {
                    Swapper.Swap(list, l, i); // Tri-Median Methode!
                }
                if (Comparer.Compare(list[l], list[r]) > 0)
                {
                    Swapper.Swap(list, l, r);
                }
                if (Comparer.Compare(list[i], list[r]) > 0)
                {
                    Swapper.Swap(list, i, r);
                }

                j = r - 1;
                Swapper.Swap(list, i, j);
                i = l;
                v = list[j];
                for (;;)
                {
                    while (Comparer.Compare(list[++i], v) > 0)
                    {
                    }

                    while (Comparer.Compare(list[--j], v) < 0)
                    {
                    }

                    if (j < i)
                    {
                        break;
                    }
                    Swapper.Swap(list, i, j);
                }
                Swapper.Swap(list, i, r - 1);
                QuickSort(list, l, j);
                QuickSort(list, i + 1, r);
            }
        }
Exemplo n.º 22
0
    static void Main(string[] args)
    {
        List <Box <string> > elements = new List <Box <string> >();
        int n = int.Parse(Console.ReadLine());

        for (int i = 0; i < n; i++)
        {
            Box <string> box = new Box <string>(Console.ReadLine());
            elements.Add(box);
        }

        int[] indexes = Console.ReadLine().Split().Select(int.Parse).ToArray();

        Swapper <List <string> > .Swap(elements, indexes[0], indexes[1])
        .ForEach(e => Console.WriteLine(e));
    }
Exemplo n.º 23
0
        private void Sort2(IList list, int low, int high)
        {
            int j;

            for (j = high; j > low; j--)
            {
                int i;
                for (i = low; i < j; i++)
                {
                    if (Comparer.Compare(list[i], list[i + 1]) > 0)
                    {
                        Swapper.Swap(list, i, i + 1);
                    }
                }
            }
        }
Exemplo n.º 24
0
        public override void Sort(IList list)
        {
            bool flipped = true;
            int  gap;
            int  top;
            int  i;
            int  j;

            gap = list.Count;

            do
            {
                if (gap == 2)
                {
                    gap = 1;
                }
                else
                {
                    gap = (int)(gap / c_COMBOSORTSHRINKFACTOR);
                }
                switch (gap)
                {
                case 0:
                    gap = 1;
                    break;

                case 9:
                    gap = 11;
                    break;

                case 10:
                    gap = 11;
                    break;
                }
                flipped = false;
                top     = list.Count - gap;
                for (i = 0; i < top; i++)
                {
                    j = i + gap;
                    if (Comparer.Compare(list[i], list[j]) > 0)
                    {
                        Swapper.Swap(list, i, j);
                        flipped = true;
                    }
                }
            } while (flipped || (gap > 1));
        }
Exemplo n.º 25
0
        public override void Sort(IList list)
        {
            int n;
            int i;

            n = list.Count;
            for (i = n / 2; i > 0; i--)
            {
                DownHeap(list, i, n);
            }
            do
            {
                Swapper.Swap(list, 0, n - 1);
                n = n - 1;
                DownHeap(list, 1, n);
            } while (n > 1);
        }
Exemplo n.º 26
0
        public override void Sort(IList list)
        {
            var flipped = true;

            var gap = list.Count;

            do
            {
                if (gap == 2)
                {
                    gap = 1;
                }
                else
                {
                    gap = (int)(gap / ComboSortShrinkFactor);
                }

                switch (gap)
                {
                case 0:
                    gap = 1;
                    break;

                case 9:
                case 10:
                    gap = 11;
                    break;
                }

                flipped = false;
                var top = list.Count - gap;
                int i;
                for (i = 0; i < top; i++)
                {
                    var j = i + gap;
                    if (Comparer.Compare(list[i], list[j]) <= 0)
                    {
                        continue;
                    }

                    Swapper.Swap(list, i, j);
                    flipped = true;
                }
            } while (flipped || gap > 1);
        }
Exemplo n.º 27
0
        public void SwapTest()
        {
            const int ogA = 3;
            const int ogB = 9;
            var       a   = ogA;
            var       b   = ogB;

            Assert.AreEqual(ogA, a);
            Assert.AreEqual(ogB, b);

            Assert.AreNotEqual(ogA, b);
            Assert.AreNotEqual(ogB, a);


            Swapper.Swap(ref a, ref b);

            Assert.AreEqual(ogA, b);
            Assert.AreEqual(ogB, a);
        }
Exemplo n.º 28
0
        public override void Sort(IList list)
        {
            int i;
            int j;
            int min;

            for (i = 0; i < list.Count; i++)
            {
                min = i;
                for (j = i + 1; j < list.Count; j++)
                {
                    if (Comparer.Compare(list[j], list[min]) < 0)
                    {
                        min = j;
                    }
                }
                Swapper.Swap(list, min, i);
            }
        }
        public SortingResult Sort(Data[] array)
        {
            SortingResult sortingResult = new SortingResult {
                ArrayOfData = array
            };
            var watch = Stopwatch.StartNew();

            for (int i = 0; i < array.Length; i++)
            {
                int ind = GetItem(array, i, sortingResult);

                Swapper.Swap(ref array[i], ref array[ind]);
                sortingResult.CountOfSwaps++;
            }

            watch.Stop();
            sortingResult.Time = watch.Elapsed;

            return(sortingResult);
        }
Exemplo n.º 30
0
        static void NodeTest()
        {
            Node <int> List = new Node <int>(0);

            List.next = new Node <int>(1, List.root);
            List++;
            List.next = new Node <int>(2, List.root);
            List++;
            List.next = new Node <int>(3, List.root);
            List++;
            List.next = new Node <int>(4, List.root);
            List++;
            List.next = new Node <int>(5, List.root);
            List++;
            List = List.root;
            List.next.Print();
            Swapper <int> .Swap(List, List.next);

            Console.WriteLine("This is swapp version");
            List.next.Print();
        }