Exemplo n.º 1
0
        private void initialize(IEnumerable <Line> lineSegments)
        {
            SweepLine = new Line(new Point(0, 0), new Point(0, int.MaxValue), Tolerance);

            currentlyTrackedLines = new RedBlackTree <Event>(true, pointComparer);
            intersectionEvents    = new Dictionary <Point, HashSet <Tuple <Event, Event> > >(pointComparer);

            verticalHorizontalLines = new HashSet <Event>();
            normalLines             = new HashSet <Event>();

            rightLeftEventLookUp = lineSegments
                                   .Select(x =>
            {
                if (x.Left.X < 0 || x.Left.Y < 0 || x.Right.X < 0 || x.Right.Y < 0)
                {
                    throw new Exception("Negative coordinates are not supported.");
                }

                return(new KeyValuePair <Event, Event>(
                           new Event(x.Left, pointComparer, EventType.Start, x, this),
                           new Event(x.Right, pointComparer, EventType.End, x, this)
                           ));
            }).ToDictionary(x => x.Value, x => x.Key);

            eventQueueLookUp = new HashSet <Event>(rightLeftEventLookUp.SelectMany(x => new[] {
                x.Key,
                x.Value
            }), new PointComparer());

            eventQueue = new BHeap <Event>(SortDirection.Ascending, eventQueueLookUp, new EventQueueComparer());
        }
Exemplo n.º 2
0
        /// <summary>
        /// Returns a dictionary of chosen encoding bytes for each distinct T.
        /// </summary>
        public Dictionary <T, byte[]> Compress(T[] input)
        {
            var frequencies = computeFrequency(input);

            var minHeap = new BHeap <FrequencyWrap>();

            foreach (var frequency in frequencies)
            {
                minHeap.Insert(new FrequencyWrap(
                                   frequency.Key, frequency.Value));
            }

            while (minHeap.Count > 1)
            {
                var a = minHeap.Extract();
                var b = minHeap.Extract();

                var newNode = new FrequencyWrap(
                    default(T), a.Frequency + b.Frequency);

                newNode.Left  = a;
                newNode.Right = b;

                minHeap.Insert(newNode);
            }

            var root = minHeap.Extract();

            var result = new Dictionary <T, byte[]>();

            dfs(root, new List <byte>(), result);

            return(result);
        }
        public void BHeap_SetGetCount()
        {
            BHeap <CSPPacket> newHeapArray = new BHeap <CSPPacket>();
            int expectedVal = 0;

            int actualValCount = newHeapArray.Count;

            Assert.AreEqual(expectedVal, actualValCount);
        }
Exemplo n.º 4
0
        /// <summary>
        /// Do DFS to pick smallest weight neighbour edges
        /// of current spanning tree one by one.
        /// </summary>
        /// <param name="spanTreeNeighbours"> Use Fibonacci Min Heap to pick smallest edge neighbour </param>
        /// <param name="spanTreeEdges">result MST edges</param>
        private void dfs(WeightedGraph <T, W> graph, WeightedGraphVertex <T, W> currentVertex,
                         BHeap <MSTEdge <T, W> > spanTreeNeighbours, HashSet <T> spanTreeVertices,
                         List <MSTEdge <T, W> > spanTreeEdges)
        {
            while (true)
            {
                //add all edges to Fibonacci Heap
                //So that we can pick the min edge in next step
                foreach (var edge in currentVertex.Edges)
                {
                    spanTreeNeighbours.Insert(new MSTEdge <T, W>(currentVertex.Value, edge.Key.Value, edge.Value));
                }

                //pick min edge
                var minNeighbourEdge = spanTreeNeighbours.Extract();

                //skip edges already in MST
                while (spanTreeVertices.Contains(minNeighbourEdge.Source) && spanTreeVertices.Contains(minNeighbourEdge.Destination))
                {
                    minNeighbourEdge = spanTreeNeighbours.Extract();

                    //if no more neighbours to explore
                    //time to end exploring
                    if (spanTreeNeighbours.Count == 0)
                    {
                        return;
                    }
                }

                //keep track of visited vertices
                //do not duplicate vertex
                if (!spanTreeVertices.Contains(minNeighbourEdge.Source))
                {
                    spanTreeVertices.Add(minNeighbourEdge.Source);
                }

                //Destination vertex will never be a duplicate
                //since this is an unexplored Vertex
                spanTreeVertices.Add(minNeighbourEdge.Destination);

                //add edge to result
                spanTreeEdges.Add(minNeighbourEdge);

                //now explore the destination vertex
                var graph1 = graph;
                currentVertex = graph1.Vertices[minNeighbourEdge.Destination];
            }
        }
Exemplo n.º 5
0
        /// <summary>
        /// Time complexity: O(nlog(n)).
        /// </summary>
        public static T[] Sort(ICollection <T> collection, SortDirection sortDirection = SortDirection.Ascending)
        {
            //heapify
            var heap = new BHeap <T>(sortDirection, collection);

            //now extract min until empty and return them as sorted array
            var sortedArray = new T[collection.Count];
            var j           = 0;

            while (heap.Count > 0)
            {
                sortedArray[j] = heap.Extract();
                j++;
            }

            return(sortedArray);
        }
Exemplo n.º 6
0
        /// <summary>
        /// Time complexity: O(nlog(n)).
        /// </summary>
        public static T[] Sort(T[] array)
        {
            //heapify
            var heap = new BHeap <T>(false, array);

            //now extract min until empty and return them as sorted array
            var sortedArray = new T[array.Length];
            var j           = 0;

            while (heap.Count > 0)
            {
                sortedArray[j] = heap.Extract();
                j++;
            }

            return(sortedArray);
        }
Exemplo n.º 7
0
        public void Max_BHeap_Test()
        {
            var rnd = new Random();

            var initial = new List <int>(Enumerable.Range(0, 51)
                                         .OrderBy(x => rnd.Next()));


            var maxHeap = new BHeap <int>(true, initial);

            for (int i = 51; i <= 99; i++)
            {
                maxHeap.Insert(i);
            }

            for (int i = 0; i <= 99; i++)
            {
                var max = maxHeap.Extract();
                Assert.AreEqual(max, 99 - i);
            }

            //IEnumerable tests.
            Assert.AreEqual(maxHeap.Count, maxHeap.Count());

            var testSeries = Enumerable.Range(1, 49)
                             .OrderBy(x => rnd.Next()).ToList();

            foreach (var item in testSeries)
            {
                maxHeap.Insert(item);
            }

            for (int i = 1; i <= 49; i++)
            {
                var max = maxHeap.Extract();
                Assert.AreEqual(max, 49 - i + 1);
            }

            //IEnumerable tests.
            Assert.AreEqual(maxHeap.Count, maxHeap.Count());
        }
Exemplo n.º 8
0
 public HumansBase(Game game, CellState role, Single movingSpeed, Point currentPos,
                   Int16 UnitsUnderControll, Single stepTime)
 {
     this.game               = game;
     this.effect             = new BasicEffect(this.game.GraphicsDevice);
     this.currentPos         = currentPos;
     this.Role               = role;
     this.isBoxConstructed   = false;
     this.canDraw            = false;
     this.movingSpeed        = movingSpeed;
     this.UnitsUnderControll = UnitsUnderControll;
     this.gameField          = this.game.Components.OfType <GameField>().ElementAt(0);
     this.openHeap           = new BHeap <PathNode>();
     this.closedList         = new Dictionary <Int32, PathNode>();
     this.pathToTarget       = new Stack <Point>();
     this.stepTime           = stepTime;
     this.currentDistance    = 0;
     this.humanWorld         = Matrix.Identity;
     this.isFound            = false;
     this.recalculateLater   = false;
     this.isRightLegTex      = false;
     this.Health             = 100;
 }
Exemplo n.º 9
0
        public void Min_BHeap_Test()
        {
            var rnd     = new Random();
            var initial = Enumerable.Range(0, 51).OrderBy(x => rnd.Next()).ToList();

            var minHeap = new BHeap <int>(false, initial);

            for (int i = 51; i <= 99; i++)
            {
                minHeap.Insert(i);
            }

            for (int i = 0; i <= 99; i++)
            {
                var min = minHeap.Extract();
                Assert.AreEqual(min, i);
            }

            //IEnumerable tests.
            Assert.AreEqual(minHeap.Count, minHeap.Count());

            var testSeries = Enumerable.Range(1, 49).OrderBy(x => rnd.Next()).ToList();

            foreach (var item in testSeries)
            {
                minHeap.Insert(item);
            }

            for (int i = 1; i <= 49; i++)
            {
                var min = minHeap.Extract();
                Assert.AreEqual(min, i);
            }

            //IEnumerable tests.
            Assert.AreEqual(minHeap.Count, minHeap.Count());
        }