Esempio n. 1
0
        public void HeapWithIndicesTest(int numberOfIterations, int heapSize)
        {
            List <int>                 actions  = new List <int>();
            List <int>                 actions1 = new List <int>();
            Random                     rand     = new Random(55);
            HeapWithIndices <long>     heap     = new HeapWithIndices <long>(new LongComparer());
            ListHeapWithIndices <long> list     = new ListHeapWithIndices <long>(new LongComparer());

            System.Collections.Generic.HashSet <int> listKeys = new System.Collections.Generic.HashSet <int>();
            System.Collections.Generic.HashSet <int> heapKeys = new System.Collections.Generic.HashSet <int>();
            for (int i = 0; i < heapSize; ++i)
            {
                int x = rand.Next() % 100;
                list.Add(x);
                heap.Add(x);
                actions.Add(x);
                actions1.Add(x);
            }
            for (int i = 0; i < numberOfIterations; ++i)
            {
                if (i < heapSize)
                {
                    actions[i] = actions[i] + rand.Next() % 10000000;
                }
                else
                {
                    actions.Add(actions[i - heapSize] + rand.Next() % 10000000);
                }
                if (i < heapSize)
                {
                    actions1[i] = actions1[i] + rand.Next() % 10000000;
                }
                else
                {
                    actions1.Add(actions1[i - heapSize] + rand.Next() % 10000000);
                }
            }
            long      heapSum = 0;
            long      listSum = 0;
            Stopwatch timer   = new Stopwatch();

            timer.Start();
            for (int i = 0; i < actions.Count; ++i)
            {
                ListHeapWithIndices <long> .HeapEntry heapEntry = list.Pop();
                listSum += heapEntry.Value;
                listKeys.Remove(heapEntry.Key);
                listKeys.Add(list.Add(actions[i]));
                if (i % 10 == 0)
                {
                    int key = listKeys.First();
                    listKeys.Remove(key);
                    list.Remove(key);
                    listKeys.Add(list.Add(actions1[i]));
                }
            }

            timer.Stop();

            System.Console.WriteLine(timer.ElapsedMilliseconds);

            timer.Reset();
            timer.Start();
            for (int i = 0; i < actions.Count; ++i)
            {
                HeapWithIndices <long> .HeapEntry heapEntry = heap.Pop();
                heapSum += heapEntry.Value;
                heapKeys.Remove(heapEntry.Key);
                heapKeys.Add(heap.Add(actions[i]));
                if (i % 10 == 0)
                {
                    int key = heapKeys.First();
                    heapKeys.Remove(key);
                    heap.Remove(key);
                    heapKeys.Add(heap.Add(actions1[i]));
                }
            }

            timer.Stop();
            System.Console.WriteLine(timer.ElapsedMilliseconds);

            Assert.AreEqual(heapSum, listSum);
        }