示例#1
0
        public void Should_Check_ExtractTop_MinHeap_Multiple()
        {
            //arrange
            var heap = new MyBinaryHeap <int>(MyBinaryHeapType.MinHeap);

            heap.Insert(1);
            heap.Insert(3);
            heap.Insert(6);
            heap.Insert(5);
            heap.Insert(9);
            heap.Insert(8);

            //act

            //assert
            heap.ExtractTop().ShouldBeEquivalentTo(1);
            heap.ExtractTop().ShouldBeEquivalentTo(3);
            heap.ExtractTop().ShouldBeEquivalentTo(5);
            heap.ExtractTop().ShouldBeEquivalentTo(6);
            heap.ExtractTop().ShouldBeEquivalentTo(8);
            heap.ExtractTop().ShouldBeEquivalentTo(9);

            heap.Root.ShouldBeEquivalentTo(null);
            heap.Count.ShouldBeEquivalentTo(0);
            heap.HeapType.ShouldBeEquivalentTo(MyBinaryHeapType.MinHeap);
        }
示例#2
0
        public void Should_Insert_MaxHeap()
        {
            //arrange
            var result = new MyBinaryHeap <int>(MyBinaryHeapType.MaxHeap);

            //act
            result.Insert(100);
            result.Insert(19);
            result.Insert(36);
            result.Insert(17);
            result.Insert(3);
            result.Insert(25);
            result.Insert(1);
            result.Insert(2);
            result.Insert(7);

            //assert
            result.Count.ShouldBeEquivalentTo(9);
            result.HeapType.ShouldBeEquivalentTo(MyBinaryHeapType.MaxHeap);

            result.Root.Data.ShouldBeEquivalentTo(100);
            result.Root.Left.Data.ShouldBeEquivalentTo(19);
            result.Root.Right.Data.ShouldBeEquivalentTo(36);
            result.Root.Left.Left.Data.ShouldBeEquivalentTo(17);
            result.Root.Left.Right.Data.ShouldBeEquivalentTo(3);
            result.Root.Right.Left.Data.ShouldBeEquivalentTo(25);
            result.Root.Right.Right.Data.ShouldBeEquivalentTo(1);
            result.Root.Left.Left.Left.Data.ShouldBeEquivalentTo(2);
            result.Root.Left.Left.Right.Data.ShouldBeEquivalentTo(7);
        }
示例#3
0
        public void Should_Decrease_Min_Heap()
        {
            //arrange
            var heap = new MyBinaryHeap <int>(MyBinaryHeapType.MinHeap);

            heap.Insert(6);
            heap.Insert(7);
            heap.Insert(12);
            heap.Insert(10);
            heap.Insert(15);
            heap.Insert(17);

            //act
            heap.Decrease(10, 2);

            //assert
            heap.Root.Data.ShouldBeEquivalentTo(2);
            heap.Root.Left.Data.ShouldBeEquivalentTo(6);
            heap.Root.Right.Data.ShouldBeEquivalentTo(12);
            heap.Root.Left.Left.Data.ShouldBeEquivalentTo(7);
            heap.Root.Left.Right.Data.ShouldBeEquivalentTo(15);
            heap.Root.Right.Left.Data.ShouldBeEquivalentTo(17);

            heap.Count.ShouldBeEquivalentTo(6);
            heap.HeapType.ShouldBeEquivalentTo(MyBinaryHeapType.MinHeap);
        }
示例#4
0
        public void Should_Check_ExtractTop_MaxHeap()
        {
            //arrange
            var heap = new MyBinaryHeap <int>(MyBinaryHeapType.MaxHeap);

            heap.Insert(100);
            heap.Insert(19);
            heap.Insert(36);
            heap.Insert(17);
            heap.Insert(3);
            heap.Insert(25);
            heap.Insert(1);
            heap.Insert(2);
            heap.Insert(7);

            //act
            var result = heap.ExtractTop();

            //assert
            result.ShouldBeEquivalentTo(100);
            heap.Count.ShouldBeEquivalentTo(8);
            heap.HeapType.ShouldBeEquivalentTo(MyBinaryHeapType.MaxHeap);

            heap.Root.Data.ShouldBeEquivalentTo(36);
            heap.Root.Left.Data.ShouldBeEquivalentTo(19);
            heap.Root.Right.Data.ShouldBeEquivalentTo(25);
            heap.Root.Left.Left.Data.ShouldBeEquivalentTo(17);
            heap.Root.Left.Right.Data.ShouldBeEquivalentTo(3);
            heap.Root.Right.Left.Data.ShouldBeEquivalentTo(7);
            heap.Root.Right.Right.Data.ShouldBeEquivalentTo(1);
            heap.Root.Left.Left.Left.Data.ShouldBeEquivalentTo(2);
            heap.Root.Left.Left.Right.ShouldBeEquivalentTo(null);
        }
示例#5
0
        public void Should_Insert_MinHeap()
        {
            //arrange
            var result = new MyBinaryHeap <int>(MyBinaryHeapType.MinHeap);

            //act
            result.Insert(4);
            result.Insert(50);
            result.Insert(7);
            result.Insert(55);
            result.Insert(90);
            result.Insert(87);
            result.Insert(2);

            //assert
            result.Count.ShouldBeEquivalentTo(7);
            result.HeapType.ShouldBeEquivalentTo(MyBinaryHeapType.MinHeap);

            result.Root.Data.ShouldBeEquivalentTo(2);
            result.Root.Left.Data.ShouldBeEquivalentTo(50);
            result.Root.Right.Data.ShouldBeEquivalentTo(4);
            result.Root.Left.Left.Data.ShouldBeEquivalentTo(55);
            result.Root.Left.Right.Data.ShouldBeEquivalentTo(90);
            result.Root.Right.Left.Data.ShouldBeEquivalentTo(87);
            result.Root.Right.Right.Data.ShouldBeEquivalentTo(7);
        }
示例#6
0
        public void Should_Check_ExtractTop_MinHeap()
        {
            //arrange
            var heap = new MyBinaryHeap <int>(MyBinaryHeapType.MinHeap);

            heap.Insert(1);
            heap.Insert(50);
            heap.Insert(23);
            heap.Insert(88);
            heap.Insert(90);
            heap.Insert(32);
            heap.Insert(74);
            heap.Insert(96);

            //act
            var result = heap.ExtractTop();

            //assert
            result.ShouldBeEquivalentTo(1);
            heap.Count.ShouldBeEquivalentTo(7);
            heap.HeapType.ShouldBeEquivalentTo(MyBinaryHeapType.MinHeap);

            heap.Root.Data.ShouldBeEquivalentTo(23);
            heap.Root.Left.Data.ShouldBeEquivalentTo(50);
            heap.Root.Right.Data.ShouldBeEquivalentTo(32);
            heap.Root.Left.Left.Data.ShouldBeEquivalentTo(88);
            heap.Root.Left.Right.Data.ShouldBeEquivalentTo(90);
            heap.Root.Right.Left.Data.ShouldBeEquivalentTo(96);
            heap.Root.Right.Right.Data.ShouldBeEquivalentTo(74);
        }
 public MyPathFindingSystem(int queueInitSize = 128, Func <long> timestampFunction = null)
 {
     m_bfsQueue          = new Queue <V>(queueInitSize);
     m_reachableList     = new List <V>(128);
     m_openVertices      = new MyBinaryHeap <float, MyPathfindingData>(128);
     m_timestamp         = 0;
     m_timestampFunction = timestampFunction;
     m_enumerating       = false;
     m_enumerator        = new Enumerator();
 }
示例#8
0
        private Dictionary <T, T?> ComputePaths(T source, MyGraphAdj <T> graph)
        {
            var vertexes = graph.GetAllVertexes().ToList();
            var edges    = graph.GetAllEdges().ToList();

            Dictionary <T, int> distances        = new Dictionary <T, int>();
            Dictionary <T, T?>  parent           = new Dictionary <T, T?>();
            Dictionary <T, int> heapMinDistances = new Dictionary <T, int>();

            var heap = new MyBinaryHeap <MyBinaryHeapKeyNode <T, int> >(MyBinaryHeapType.MinHeap);

            foreach (var vertex in vertexes)
            {
                heap.Insert(new MyBinaryHeapKeyNode <T, int>(vertex.Key, vertex.Key.CompareTo(source) == 0 ? 0 : Int32.MaxValue));
                heapMinDistances.Add(vertex.Key, Int32.MaxValue);
            }
            parent[source]    = null;
            distances[source] = 0;

            while (heap.Count > 0)
            {
                var current = heap.ExtractTop();
                var nodes   = edges.Where(x => x[0].Equals(
                                              vertexes.First(y => y.Key.CompareTo(current.Key) == 0).Value)).ToList();
                if (nodes.Count == 0)
                {
                    break;
                }
                var parentNode = vertexes.First(x => x.Key.CompareTo(current.Key) == 0);
                foreach (var node in nodes)
                {
                    var vertex = vertexes.First(x => x.Value == node[1]);
                    if (!heap.Contains(new MyBinaryHeapKeyNode <T, int>(vertex.Key, vertex.Value)))
                    {
                        continue;
                    }

                    var currentDistance = edges.First(x =>
                                                      x[0] == parentNode.Value && x[1] == vertex.Value)[2];
                    distances[vertex.Key] = distances[current.Key] + currentDistance;

                    if (heapMinDistances[vertex.Key] >= distances[vertex.Key])
                    {
                        parent[vertex.Key] = current.Key;

                        heap.Decrease(new MyBinaryHeapKeyNode <T, int>(vertex.Key, Int32.MaxValue),
                                      new MyBinaryHeapKeyNode <T, int>(vertex.Key, currentDistance));
                        heapMinDistances[vertex.Key] = currentDistance;
                    }
                }
            }

            return(parent);
        }
示例#9
0
        public void Should_Create_Default()
        {
            //arrange

            //act
            var result = new MyBinaryHeap <int>(MyBinaryHeapType.MinHeap);

            //assert
            result.Root.ShouldBeEquivalentTo(null);
            result.HeapType.ShouldBeEquivalentTo(MyBinaryHeapType.MinHeap);
            result.Count.ShouldBeEquivalentTo(0);
        }
示例#10
0
        public void Should_Check_ExtractTop_MaxHeap_Throw_If_Empty()
        {
            //arrange
            var heap = new MyBinaryHeap <int>(MyBinaryHeapType.MaxHeap);

            //act
            Action act = () => heap.ExtractTop();

            //assert
            act.ShouldThrow <InvalidOperationException>();

            heap.Root.ShouldBeEquivalentTo(null);
            heap.Count.ShouldBeEquivalentTo(0);
            heap.HeapType.ShouldBeEquivalentTo(MyBinaryHeapType.MaxHeap);
        }
        public void BuildMyBinaryHeapTest()
        {
            //Assign
            var val = new List <int>()
            {
                9, 6, 5, 2, 3
            };
            var expected = new List <int>()
            {
                0, 2, 3, 5, 6, 9
            };
            var myBinaryHeap = new MyBinaryHeap <int>();

            //Act
            var result = myBinaryHeap.BuildHeapList(val);

            //Assert
            CollectionAssert.AreEqual(expected, result, "Result differs with what is expected");
        }
示例#12
0
        public IEnumerable <T> HeapSort(IEnumerable <T> list)
        {
            if (list == null)
            {
                throw new ArgumentNullException();
            }
            var array = list.ToArray();

            var heap = new MyBinaryHeap <T>(MyBinaryHeapType.MinHeap);

            foreach (var item in array)
            {
                heap.Insert(item);
            }

            while (heap.Count > 0)
            {
                yield return(heap.ExtractTop());
            }
        }
示例#13
0
        public void Should_Throw_Decrease_If_Lower_Value_MaxHeap()
        {
            //arrange
            var heap = new MyBinaryHeap <int>(MyBinaryHeapType.MaxHeap);

            heap.Insert(17);
            heap.Insert(15);
            heap.Insert(10);
            heap.Insert(6);
            heap.Insert(10);
            heap.Insert(7);

            //act
            Action act = () => heap.Decrease(17, 11);

            //assert
            act.ShouldThrow <ArgumentException>();

            heap.Count.ShouldBeEquivalentTo(6);
            heap.HeapType.ShouldBeEquivalentTo(MyBinaryHeapType.MaxHeap);
        }
示例#14
0
        public void Should_Check_Contains_True()
        {
            //arrange
            var heap = new MyBinaryHeap <int>(MyBinaryHeapType.MaxHeap);

            heap.Insert(17);
            heap.Insert(15);
            heap.Insert(10);
            heap.Insert(6);
            heap.Insert(10);
            heap.Insert(7);

            //act
            var result = heap.Contains(10);

            //assert
            result.ShouldBeEquivalentTo(true);

            heap.Count.ShouldBeEquivalentTo(6);
            heap.HeapType.ShouldBeEquivalentTo(MyBinaryHeapType.MaxHeap);
        }
示例#15
0
 public MyVoxelNavigationMesh(MyVoxelBase voxelMap, MyNavmeshCoordinator coordinator, Func <long> timestampFunction) : base(coordinator.Links, 0x10, timestampFunction)
 {
     this.LimitAddingWeight         = GetWeight(25f);
     this.m_voxelMap                = voxelMap;
     m_staticVoxelMap               = this.m_voxelMap;
     this.m_processedCells          = new MyVector3ISet();
     this.m_cellsOnWayCoords        = new HashSet <ulong>();
     this.m_cellsOnWay              = new List <Vector3I>();
     this.m_primitivesOnPath        = new List <MyHighLevelPrimitive>(0x80);
     this.m_toAdd                   = new MyBinaryHeap <float, CellToAddHeapItem>(0x80);
     this.m_heapItemList            = new List <CellToAddHeapItem>();
     this.m_markedForAddition       = new MyVector3ISet();
     this.m_cellsToChange           = new LinkedList <Vector3I>();
     this.m_cellsToChangeSet        = new MyVector3ISet();
     this.m_connectionHelper        = new MyVoxelConnectionHelper();
     this.m_navmeshCoordinator      = coordinator;
     this.m_higherLevel             = new MyHighLevelGroup(this, coordinator.HighLevelLinks, timestampFunction);
     this.m_higherLevelHelper       = new MyVoxelHighLevelHelper(this);
     this.m_debugCellEdges          = new Dictionary <ulong, List <DebugDrawEdge> >();
     voxelMap.Storage.RangeChanged += new Action <Vector3I, Vector3I, MyStorageDataTypeFlags>(this.OnStorageChanged);
     this.m_maxCellCoord            = ((Vector3I)(this.m_voxelMap.Size / 8)) - Vector3I.One;
 }
示例#16
0
        public void Should_Throw_Decrease_If_Not_Contains()
        {
            //arrange
            var heap = new MyBinaryHeap <int>(MyBinaryHeapType.MaxHeap);

            heap.Insert(17);
            heap.Insert(15);
            heap.Insert(10);
            heap.Insert(6);
            heap.Insert(10);
            heap.Insert(7);

            heap.ExtractTop();

            //act
            Action act = () => heap.Decrease(17, 11);

            //assert
            act.ShouldThrow <InvalidOperationException>();

            heap.Count.ShouldBeEquivalentTo(5);
            heap.HeapType.ShouldBeEquivalentTo(MyBinaryHeapType.MaxHeap);
        }
示例#17
0
        public void Should_Check_ExtractTop_Insert_MaxHeap_Multiple()
        {
            //arrange
            var heap = new MyBinaryHeap <int>(MyBinaryHeapType.MaxHeap);

            heap.Insert(17);
            heap.Insert(15);
            heap.Insert(10);
            heap.Insert(6);
            heap.Insert(10);
            heap.Insert(7);

            heap.ExtractTop();
            heap.ExtractTop();
            heap.ExtractTop();

            //act
            heap.Insert(1);
            heap.Insert(2);
            heap.Insert(3);
            heap.Insert(11);
            heap.Insert(6);

            //assert
            heap.Root.Data.ShouldBeEquivalentTo(11);
            heap.Root.Left.Data.ShouldBeEquivalentTo(7);
            heap.Root.Right.Data.ShouldBeEquivalentTo(10);
            heap.Root.Left.Left.Data.ShouldBeEquivalentTo(6);
            heap.Root.Left.Right.Data.ShouldBeEquivalentTo(2);
            heap.Root.Right.Left.Data.ShouldBeEquivalentTo(3);
            heap.Root.Right.Right.Data.ShouldBeEquivalentTo(6);
            heap.Root.Left.Left.Left.Data.ShouldBeEquivalentTo(1);

            heap.Count.ShouldBeEquivalentTo(8);
            heap.HeapType.ShouldBeEquivalentTo(MyBinaryHeapType.MaxHeap);
        }