示例#1
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);
                            }
                        }
                    }
                }
            }
        }