public void IndexMinPQ_Contains()
    {
        var q = new IndexPriorityQueue<int>();

        Assert.IsFalse(q.Contains(1));

        q.Enqueue(1, 10);
        q.Enqueue(1, 20);
        q.Enqueue(1, 30);

        Assert.IsTrue(q.Contains(1));
        q.Dequeue();
        Assert.IsTrue(q.Contains(1));
        q.Dequeue();
        Assert.IsTrue(q.Contains(1));
        q.Dequeue();
        Assert.IsFalse(q.Contains(1));
    }
예제 #2
0
        static void time_index_pq(int MAXID, int MAXCOUNT, int mod, int rounds, LocalProfiler profiler)
        {
            profiler.Start("index_all");

            profiler.Start("index_initialize");
            IndexPriorityQueue PQ_Index = new IndexPriorityQueue(MAXID);

            profiler.StopAndAccumulate("index_initialize");

            for (int ri = 0; ri < rounds; ++ri)
            {
                profiler.Start("index_push");

                int count = 0;
                int id    = 0;
                while (count < MAXCOUNT)
                {
                    id = (id + mod) % MAXID;
                    PQ_Index.Enqueue(id, count);
                    count++;
                }

                profiler.StopAndAccumulate("index_push");
                profiler.Start("index_update");

                Random r = new Random(31337);
                id = 0; count = 0;
                while (count++ < MAXCOUNT)
                {
                    id = (id + mod) % MAXID;
                    float new_p = count + ((r.Next() % 1000) - 1000);
                    PQ_Index.Update(id, new_p);
                }

                profiler.StopAndAccumulate("index_update");
                profiler.Start("index_pop");

                while (PQ_Index.Count > 0)
                {
                    int index_id = PQ_Index.Dequeue();
                }

                profiler.StopAndAccumulate("index_pop");
            }

            profiler.StopAndAccumulate("index_all");
        }
    public void IndexMinPQ_Dequeue()
    {
        var q = new IndexPriorityQueue<int>();
        for (int i = 0; i < items.Length; i++)
        {
            q.Enqueue(i, items[i]);
        }

        var sortedItems = new List<int>(items);
        sortedItems.Sort();

        foreach (var item in sortedItems)
        {
            // The top of the queue returns the items in sorted order 
            var minIndex = q.Dequeue();
            Assert.IsFalse(q.Contains(minIndex));
            Assert.AreEqual(items[minIndex], item);
        }
    }
예제 #4
0
        public static void test_pq_debuggable()
        {
            int MAXID = 10;

            IndexPriorityQueue QIndex = new IndexPriorityQueue(MAXID);
            DynamicPriorityQueue <TestDynamicNode> QDynamic = new DynamicPriorityQueue <TestDynamicNode>();

            TestDynamicNode[] dyn_nodes = new TestDynamicNode[MAXID];

            bool verbose = false;

            //int n = 1;
            for (int i = 0; i < MAXID; ++i)
            {
                //n = (n + 17) % 17;
                int   id       = i;
                float priority = 1.0f - (float)i / 10.0f;
                QIndex.Enqueue(id, priority);
                if (verbose)
                {
                    System.Console.WriteLine("i = {0}", i);
                }
                QIndex.DebugPrint();
                if (verbose)
                {
                    System.Console.WriteLine("---", i);
                }
                dyn_nodes[i] = new TestDynamicNode()
                {
                    id = id
                };
                QDynamic.Enqueue(dyn_nodes[i], priority);
                QDynamic.DebugPrint();
            }

            System.Console.WriteLine("Dequeing...");


            for (int i = 0; i < MAXID; ++i)
            {
                float newp = (float)((i + MAXID / 2) % MAXID) / 10.0f;
                QIndex.Update(i, newp);
                QDynamic.Update(dyn_nodes[i], newp);
                //System.Console.WriteLine("UPDATE {0} {1}", QIndex.First, QDynamic.First.id);
                //QIndex.DebugPrint();
                //System.Console.WriteLine("---", i);
                //QDynamic.DebugPrint();
                Util.gDevAssert(QIndex.First == QDynamic.First.id);
            }


            for (int i = 0; i < MAXID; ++i)
            {
                int id   = QIndex.Dequeue();
                var node = QDynamic.Dequeue();
                Util.gDevAssert(id == node.id);
                if (verbose)
                {
                    System.Console.WriteLine("DEQUEUE {0} {1}", id, node.id);
                }

                if (verbose)
                {
                    QIndex.DebugPrint();
                }
                if (verbose)
                {
                    System.Console.WriteLine("---", i);
                }
                if (verbose)
                {
                    QDynamic.DebugPrint();
                }
            }
        }
예제 #5
0
        public static void test_pq()
        {
            System.Console.WriteLine("testing priority queues...");

            int MAXID    = 1000000;
            int MAXCOUNT = 100;
            int mod      = 31337;

            for (int kk = 0; kk < 3; ++kk)
            {
                if (kk == 1)
                {
                    MAXCOUNT = MAXID / 10;
                }
                else if (kk == 2)
                {
                    MAXCOUNT = MAXID;
                }

                IndexPriorityQueue PQ_Index = new IndexPriorityQueue(MAXID);
                DynamicPriorityQueue <TestDynamicNode> PQ_Dynamic   = new DynamicPriorityQueue <TestDynamicNode>();
                MemoryPool <TestDynamicNode>           Dynamic_Pool = new MemoryPool <TestDynamicNode>();
                Dictionary <int, TestDynamicNode>      DynamicNodes = new Dictionary <int, TestDynamicNode>();

                System.Console.WriteLine("inserting {0} of {1}", MAXCOUNT, MAXID);

                int count = 0;
                int id    = 0;
                while (count < MAXCOUNT)
                {
                    id = (id + mod) % MAXID;

                    PQ_Index.Enqueue(id, count);
                    TestDynamicNode node = Dynamic_Pool.Allocate();
                    node.Initialize(id);
                    PQ_Dynamic.Enqueue(node, count);
                    DynamicNodes[id] = node;
                    count++;
                }
                Util.gDevAssert(PQ_Index.IsValidQueue());
                Util.gDevAssert(PQ_Dynamic.IsValidQueue());
                Util.gDevAssert(PQ_Index.Count == PQ_Dynamic.Count);
                Util.gDevAssert(PQ_Index.First == PQ_Dynamic.First.id);
                check_same(PQ_Index, PQ_Dynamic);

                Random r = new Random(31337);

                System.Console.WriteLine("updating...");

                id = 0; count = 0;
                while (count++ < MAXCOUNT)
                {
                    id = (id + mod) % MAXID;
                    float new_p = count + ((r.Next() % 1000) - 1000);
                    PQ_Index.Update(id, new_p);
                    PQ_Dynamic.Update(DynamicNodes[id], new_p);
                }
                Util.gDevAssert(PQ_Index.IsValidQueue());
                Util.gDevAssert(PQ_Dynamic.IsValidQueue());
                check_same(PQ_Index, PQ_Dynamic);

                System.Console.WriteLine("removing...");

                while (PQ_Index.Count > 0)
                {
                    int             index_id = PQ_Index.Dequeue();
                    TestDynamicNode node     = PQ_Dynamic.Dequeue();
                    Util.gDevAssert(index_id == node.id);
                }
            }
        }