Beispiel #1
0
 public void PopTest()
 {
     simpleHeap = new Heap <int>();
     heap       = new HeapWithIndices <int>();
     for (int i = 1; i <= 10; ++i)
     {
         simpleHeap.Add(i);
     }
     for (int i = 1; i <= 10; ++i)
     {
         heap.Add(i);
     }
     Assert.AreEqual(1, simpleHeap.Peek);
     Assert.AreEqual(1, heap.Peek.Value);
     simpleHeap.Add(-1);
     heap.Add(-1);
     Assert.AreEqual(-1, simpleHeap.Peek);
     Assert.AreEqual(-1, heap.Peek.Value);
     for (int i = 0; i < 5; ++i)
     {
         simpleHeap.Pop();
     }
     for (int i = 0; i < 5; ++i)
     {
         heap.Pop();
     }
     Assert.AreEqual(5, simpleHeap.Peek);
     Assert.AreEqual(5, heap.Peek.Value);
 }
Beispiel #2
0
        public void HeapWithDictionaryStressTest(int k)
        {
            HeapWithIndices <long, long> heap = new HeapWithIndices <long, long>(10);
            SortedSet <long>             keys = new SortedSet <long>();
            Random rand = new Random(55);

            for (int i = 0; i < k; ++i)
            {
                if (rand.Next() % 3 == 1)
                {
                    if (rand.Next() % 2 == 0)
                    {
                        if (heap.Count > 0)
                        {
                            keys.Remove(heap.Pop().Key);
                        }
                    }
                    else
                    {
                        if (keys.Count == 0)
                        {
                            continue;
                        }
                        heap.Remove(keys.First());
                        keys.Remove(keys.First());
                    }
                }
                else
                {
                    keys.Add(i);
                    heap.Add(rand.Next(), i);
                }
            }
        }
Beispiel #3
0
 public void RemoveTest()
 {
     heap = new HeapWithIndices <int>();
     for (int i = 1; i <= 10; ++i)
     {
         heap.Add(i);
     }
     Assert.AreEqual(1, heap.Peek.Value);
     heap.Add(-1);
     Assert.AreEqual(-1, heap.Peek.Value);
     for (int i = 10; i > 5; --i)
     {
         heap.Remove(i);
     }
     heap.Remove(0);
     Assert.AreEqual(2, heap.Peek.Value);
 }
Beispiel #4
0
        public void UserKeyTest()
        {
            HeapWithIndices <int, String> heap = new HeapWithIndices <int, String>();

            for (int i = 1; i <= 10; ++i)
            {
                heap.Add(i, "abacaba" + i.ToString());
            }
            Assert.AreEqual(1, heap.Peek.Value);
            heap.Add(-1, "abacaba-1");
            Assert.AreEqual(-1, heap.Peek.Value);
            for (int i = 10; i > 5; --i)
            {
                heap.Remove("abacaba" + i.ToString());
            }
            heap.Remove("abacaba-1");
            heap.Remove("abacaba1");
            Assert.AreEqual(2, heap.Peek.Value);
        }
Beispiel #5
0
        public void HeapWithIndicesStressTestModifyTop(int k)
        {
            HeapWithIndices <long>       heap = new HeapWithIndices <long>();
            HeapWithIndices <long, long> headWithDictionary = new HeapWithIndices <long, long>();
            SortedSet <int> keys = new SortedSet <int>();
            Random          rand = new Random(55);

            for (int i = 0; i < k; ++i)
            {
                if (rand.Next() % 3 == 1)
                {
                    heap.ModifyTop(rand.Next());
                }
                else
                {
                    keys.Add(heap.Add(rand.Next()));
                }
            }
        }
Beispiel #6
0
        public void ModifyTest()
        {
            Heap <int> simpleHeap = new Heap <int>();
            HeapWithIndices <int, String> heap = new HeapWithIndices <int, string>();

            for (int i = 0; i < 10; ++i)
            {
                simpleHeap.Add(i);
            }
            for (int i = 0; i <= 10; ++i)
            {
                heap.Add(i, i.ToString());
            }
            simpleHeap.ModifyTop(12);
            Assert.AreEqual(1, simpleHeap.Peek);
            simpleHeap.ModifyTop(-11);
            Assert.AreEqual(-11, simpleHeap.Peek);
            for (int i = 0; i < 9; ++i)
            {
                simpleHeap.Pop();
            }
            Assert.AreEqual(12, simpleHeap.Peek);
            heap.Modify("10", -1111);
            Assert.AreEqual(-1111, heap.Peek.Value);
            Assert.AreEqual("10", heap.Peek.Key);
            heap.Pop();
            Assert.AreEqual("0", heap.Peek.Key);
            Assert.AreEqual(0, heap.Peek.Value);
            for (int i = 0; i < 9; ++i)
            {
                heap.Pop();
            }
            Assert.AreEqual("9", heap.Peek.Key);
            Assert.AreEqual(9, heap.Peek.Value);
            heap.ModifyTop(1111);
            Assert.AreEqual("9", heap.Peek.Key);
            Assert.AreEqual(1111, heap.Peek.Value);
        }
Beispiel #7
0
        public void TryGetValueTest()
        {
            HeapWithIndices <int>         simpleHeap = new HeapWithIndices <int>();
            HeapWithIndices <int, String> heap       = new HeapWithIndices <int, string>();

            for (int i = -10; i <= 10; ++i)
            {
                simpleHeap.Add(i);
            }
            for (int i = -10; i <= 10; ++i)
            {
                heap.Add(i, i.ToString());
            }
            int returnValue = 0;

            simpleHeap.TryGetValue(1, out returnValue);
            Assert.AreEqual(true, simpleHeap.TryGetValue(1, out returnValue));
            Assert.AreEqual(-9, returnValue);
            Assert.AreEqual(false, simpleHeap.TryGetValue(50, out returnValue));
            heap.TryGetValue("-5", out returnValue);
            Assert.AreEqual(true, heap.TryGetValue("-5", out returnValue));
            Assert.AreEqual(-5, returnValue);
            Assert.AreEqual(false, heap.TryGetValue("50", out returnValue));
        }
Beispiel #8
0
        public void EnumeratorHeapTest()
        {
            Heap <int> heap = new Heap <int>();
            HeapWithAttachments <int, int> heapWithAttachments = new HeapWithAttachments <int, int>();
            HeapWithIndices <int, int>     heapWithDictionary  = new HeapWithIndices <int, int>();
            HeapWithIndices <int>          heapWithIndices     = new HeapWithIndices <int>();

            for (int i = 1; i <= 10; ++i)
            {
                heap.Add(i);
                heapWithAttachments.Add(i, i);
                heapWithDictionary.Add(i, i);
                heapWithIndices.Add(i);
            }

            int index = 0;

            foreach (int item in heap)
            {
                index += item;
            }
            Assert.AreEqual(55, index);
            foreach (HeapWithAttachments <int, int> .HeapEntry item in heapWithAttachments)
            {
                index -= (item.Attachment + item.Value);
            }
            Assert.AreEqual(-55, index);
            foreach (HeapWithIndices <int, int> .HeapEntry item in heapWithDictionary)
            {
                index += (item.Key + item.Value);
            }
            Assert.AreEqual(55, index);
            foreach (HeapWithIndices <int> .HeapEntry item in heapWithIndices)
            {
                index += (item.Value);
            }
            Assert.AreEqual(110, index);
            bool flag = false;

            try
            {
                foreach (int item in heap)
                {
                    heap.Add(1);
                }
            }
            catch (Exception)
            {
                flag = true;
            }
            Assert.AreEqual(true, flag);
            flag = false;
            try
            {
                foreach (HeapWithAttachments <int, int> .HeapEntry item in heapWithAttachments)
                {
                    heapWithAttachments.Add(1, 1);
                }
            }
            catch (Exception)
            {
                flag = true;
            }
            Assert.AreEqual(true, flag);
            flag = false;
            try
            {
                foreach (HeapWithIndices <int, int> .HeapEntry item in heapWithDictionary)
                {
                    heapWithDictionary.Add(1, 1);
                }
            }
            catch (Exception)
            {
                flag = true;
            }
            Assert.AreEqual(true, flag);
            flag = false;
            try
            {
                foreach (HeapWithIndices <int> .HeapEntry item in heapWithIndices)
                {
                    heapWithIndices.Add(1);
                }
            }
            catch (Exception)
            {
                flag = true;
            }
            Assert.AreEqual(true, flag);
        }
Beispiel #9
0
        public void HeapWithDictionaryStressTest()
        {
            int numberOfIterations = 1000000;
            SortedDictionary <long, long> priorityQueue = new SortedDictionary <long, long>();
            Dictionary <long, long>       dictionary    = new Dictionary <long, long>();
            HeapWithIndices <long, long>  myHeap        = new HeapWithIndices <long, long>(10);
            long   lastKey = 0;
            Random rand    = new Random(55);

            for (int i = 0; i < numberOfIterations; ++i)
            {
                if (rand.Next() % 3 == 0)
                {
                    if (priorityQueue.Count > 0)
                    {
                        Assert.AreEqual(priorityQueue.Count, myHeap.Count);
                        long value = priorityQueue.First().Key;
                        long key   = priorityQueue.First().Value;
                        Assert.AreEqual((long)value, (long)myHeap.PeekValue);
                        Assert.AreEqual((long)key, (long)myHeap.PeekKey);
                        dictionary.Remove(key);
                        myHeap.Pop();
                        priorityQueue.Remove(value);
                    }
                }
                else
                {
                    long x = rand.Next();
                    while (priorityQueue.ContainsKey(x))
                    {
                        x = rand.Next();
                    }
                    myHeap.Add(x, (long)i);
                    priorityQueue.Add(x, (long)i);
                    dictionary.Add((long)i, x);
                    lastKey = i;
                }
                if (rand.Next() % 3 == 0)
                {
                    if (rand.Next() % 2 == 0)
                    {
                        if (priorityQueue.Count > 0)
                        {
                            if (dictionary.ContainsKey(lastKey))
                            {
                                priorityQueue.Remove(dictionary[lastKey]);
                                myHeap.Remove((long)lastKey);
                                dictionary.Remove(lastKey);
                            }
                        }
                    }
                    else
                    {
                        if (priorityQueue.Count > 0)
                        {
                            if (rand.Next() % 2 == 0)
                            {
                                long newTop = rand.Next();
                                while (priorityQueue.ContainsKey(newTop))
                                {
                                    newTop = rand.Next();
                                }

                                long key = myHeap.PeekKey;
                                priorityQueue.Remove(dictionary[key]);
                                dictionary.Remove(key);
                                priorityQueue.Add(newTop, key);
                                dictionary.Add(key, newTop);
                                myHeap.ModifyTop(newTop);
                            }
                            else if (myHeap.ContainsKey((long)lastKey))
                            {
                                long newTop = rand.Next();
                                while (priorityQueue.ContainsKey(newTop))
                                {
                                    newTop = rand.Next();
                                }

                                long key = lastKey;
                                priorityQueue.Remove(dictionary[key]);
                                dictionary.Remove(key);
                                priorityQueue.Add(newTop, key);
                                dictionary.Add(key, newTop);
                                myHeap.Modify(key, newTop);
                            }
                        }
                    }
                }
            }
        }
Beispiel #10
0
        public void HeapWithIndicesStressTest()
        {
            int numberOfIterations = 1000000;
            SortedDictionary <long, int> priorityQueue = new SortedDictionary <long, int>();
            Dictionary <int, long>       dictionary    = new Dictionary <int, long>();
            HeapWithIndices <long>       myHeap        = new HeapWithIndices <long>(10);
            int    lastKey = 0;
            Random rand    = new Random(55);

            for (int i = 0; i < numberOfIterations; ++i)
            {
                int type = rand.Next() % 3;
                if (type == 0)
                {
                    if (priorityQueue.Count > 0)
                    {
                        Assert.AreEqual(priorityQueue.Count, myHeap.Count);
                        long value = priorityQueue.First().Key;
                        int  key   = priorityQueue.First().Value;
                        Assert.AreEqual((long)value, (long)myHeap.PeekValue);
                        Assert.AreEqual((long)key, (long)myHeap.PeekKey);
                        dictionary.Remove(key);
                        myHeap.Pop();
                        priorityQueue.Remove(value);
                    }
                }
                if (rand.NextDouble() > 0.1)
                {
                    long x = rand.Next();
                    while (priorityQueue.ContainsKey(x))
                    {
                        x = rand.Next();
                    }
                    int y = myHeap.Add(x);
                    priorityQueue.Add(x, y);
                    dictionary.Add(y, x);
                    lastKey = y;
                }
                if (type == 1)
                {
                    if (priorityQueue.Count > 0)
                    {
                        if (dictionary.ContainsKey(lastKey))
                        {
                            priorityQueue.Remove(dictionary[lastKey]);
                            myHeap.Remove(lastKey);
                            dictionary.Remove(lastKey);
                        }
                    }
                }
                if (type == 2)
                {
                    if (priorityQueue.Count > 0)
                    {
                        if (rand.NextDouble() < 0.5)
                        {
                            long newTop = rand.Next();
                            while (priorityQueue.ContainsKey(newTop))
                            {
                                newTop = rand.Next();
                            }

                            int key = myHeap.PeekKey;
                            priorityQueue.Remove(dictionary[key]);
                            dictionary.Remove(key);
                            priorityQueue.Add(newTop, key);
                            dictionary.Add(key, newTop);
                            myHeap.ModifyTop(newTop);
                        }
                        else
                        {
                            if (dictionary.ContainsKey(lastKey))
                            {
                                long newTop = rand.Next();
                                while (priorityQueue.ContainsKey(newTop))
                                {
                                    newTop = rand.Next();
                                }
                                int key = lastKey;
                                priorityQueue.Remove(dictionary[key]);
                                dictionary.Remove(key);
                                priorityQueue.Add(newTop, key);
                                dictionary.Add(key, newTop);
                                myHeap.Modify(key, newTop);
                            }
                        }
                    }
                }
            }
        }
Beispiel #11
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);
        }