示例#1
0
        public void ClasEnqueueTest()
        {
            int maxSize = 3;

            FixedSizePriorityQueue <float, int> testQ = new FixedSizePriorityQueue <float, int>(maxSize);

            Assert.IsNull(testQ.Peek());
            Assert.AreEqual(0, testQ.Count);

            float key   = -5;
            int   value = 1;

            testQ.Enqueue(key, value);

            Assert.AreEqual(1, testQ.Count);
            Assert.AreEqual(key, testQ.Peek().Value.Key);
            Assert.AreEqual(value, testQ.Peek().Value.Value);

            key   = 10;
            value = 2;

            testQ.Enqueue(key, value);

            Assert.AreEqual(2, testQ.Count);
            Assert.AreEqual(-5, testQ.Peek().Value.Key);
            Assert.AreEqual(1, testQ.Peek().Value.Value);

            //Updates the top of the queue when higher prio elements are added
            key   = -10;
            value = 3;

            testQ.Enqueue(key, value);

            Assert.AreEqual(3, testQ.Count);
            Assert.AreEqual(key, testQ.Peek().Value.Key);
            Assert.AreEqual(value, testQ.Peek().Value.Value);

            //When the queue is full, doesn't change its size
            //and doesn't insert a new element with lower prio than top's
            key   = -11;
            value = 4;

            testQ.Enqueue(key, value);

            Assert.AreEqual(3, testQ.Count);
            Assert.AreEqual(-10, testQ.Peek().Value.Key);
            Assert.AreEqual(3, testQ.Peek().Value.Value);

            //When the queue is full, doesn't change its size
            //and does insert a new element with hiher prio than top's
            key   = -9;
            value = 5;

            testQ.Enqueue(key, value);

            Assert.AreEqual(3, testQ.Count);
            Assert.AreEqual(key, testQ.Peek().Value.Key);
            Assert.AreEqual(value, testQ.Peek().Value.Value);
        }
示例#2
0
        public void ClasShouldOnlyStoreNLArgestValuesTest()
        {
            foreach (int size in new List <int>()
            {
                10, 100, 150
            })
            {
                FixedSizePriorityQueue <int, bool> testQ = new FixedSizePriorityQueue <int, bool>(size);
                int        n    = 4 * size;
                List <int> keys = new List <int>(n);
                for (int i = 0; i < n; i++)
                {
                    int key;
                    lock (syncLock)
                    {
                        key = Rnd.Next();
                    }

                    keys.Add(key);
                    testQ.Enqueue(key, true);
                }
                keys.Sort();
                List <int> largestKeys = keys.Skip(n - size).ToList();
                Assert.AreEqual(size, largestKeys.Count);

                var result = testQ.ToList()
                             .Select(kvp => kvp.Key)
                             .ToList();
                result.Sort();
                Assert.IsTrue(largestKeys.SequenceEqual(result));
            }
        }
示例#3
0
        static public List <BeamSearchStatus> GetTopNBSS(List <BeamSearchStatus> bssList, int topN)
        {
            FixedSizePriorityQueue <ComparableItem <BeamSearchStatus> > q = new FixedSizePriorityQueue <ComparableItem <BeamSearchStatus> >(topN, new ComparableItemComparer <BeamSearchStatus>(false));

            for (int i = 0; i < bssList.Count; i++)
            {
                q.Enqueue(new ComparableItem <BeamSearchStatus>(bssList[i].Score, bssList[i]));
            }

            return(q.Select(x => x.Value).ToList());
        }
示例#4
0
        public static List <BeamSearchStatus> GetTopNBSS(List <BeamSearchStatus> bssList, int topN)
        {
            var q = new FixedSizePriorityQueue <ComparableItem <BeamSearchStatus> >(topN, new ComparableItemComparer <BeamSearchStatus>(false));

            foreach (var status in bssList)
            {
                q.Enqueue(new ComparableItem <BeamSearchStatus>(status.Score, status));
            }

            return(q.Select(x => x.Value).ToList());
        }
示例#5
0
        public List <int> GetTopNMaxWeightIdx(int topN)
        {
            float[] weights = ToWeightArray();
            FixedSizePriorityQueue <ComparableItem <int> > q = new FixedSizePriorityQueue <ComparableItem <int> >(topN, new ComparableItemComparer <int>(true));

            for (int i = 0; i < weights.Length; i++)
            {
                q.Enqueue(new ComparableItem <int>(weights[i], i));
            }

            return(q.Select(x => x.Value).ToList());
        }