Example #1
0
 public void TestGetFromEmptyQueueAfterSomeAdds()
 {
     FastQueue<string> q = new FastQueue<string>();
     q.Enqueue("a");
     q.Enqueue("b");
     q.Dequeue();
     q.Dequeue();
     q.Dequeue();
 }
Example #2
0
        public void TestGetFromEmptyQueueAfterSomeAdds()
        {
            FastQueue <string> q = new FastQueue <string>();

            q.Enqueue("a");
            q.Enqueue("b");
            q.Dequeue();
            q.Dequeue();
            q.Dequeue();
        }
Example #3
0
        public void TestQueueThenRemoveAll()
        {
            FastQueue <string> q = new FastQueue <string>();

            q.Enqueue("a");
            q.Enqueue("b");
            q.Enqueue("c");
            q.Enqueue("d");
            q.Enqueue("e");
            StringBuilder buf = new StringBuilder();

            while (q.Count > 0)
            {
                string o = q.Dequeue();
                buf.Append(o);
                if (q.Count > 0)
                {
                    buf.Append(" ");
                }
            }
            Assert.AreEqual(0, q.Count, "queue should be empty");
            string expecting = "a b c d e";
            string found     = buf.ToString();

            Assert.AreEqual(expecting, found);
        }
Example #4
0
        public void DequeuePerformanceTest()
        {
            List <int> storage = new List <int>(maxNumber);

            for (int i = 0; i <= maxNumber; i++)
            {
                storage.Add(i);
            }

            List <int>       list   = new List <int>(storage);
            Queue <int>      q      = new Queue <int>(storage);
            LinkedList <int> linked = new LinkedList <int>(storage);
            FastQueue <int>  fq     = new FastQueue <int>(storage);

            Stopwatch listTimer = Stopwatch.StartNew();

            while (list.Any())
            {
                list.RemoveAt(0);
                //list.TrimExcess();
            }
            listTimer.Stop();
            float listTime = listTimer.ElapsedTicks;

            Stopwatch qTimer = Stopwatch.StartNew();

            while (q.Any())
            {
                q.Dequeue();
                //q.TrimExcess();
            }
            qTimer.Stop();
            float qTime = qTimer.ElapsedTicks;

            Stopwatch linkedTimer = Stopwatch.StartNew();

            while (linked.Any())
            {
                linked.RemoveFirst();
            }
            linkedTimer.Stop();
            float linkedTime = linkedTimer.ElapsedTicks;

            Stopwatch fqTimer = Stopwatch.StartNew();

            while (fq.Any())
            {
                fq.Dequeue();
            }
            fqTimer.Stop();
            float fqTime = fqTimer.ElapsedTicks;

            Assert.IsTrue(listTime > fqTime, "List is faster than FastQueue! {0} to {1}, listTime, fqTime");
            Assert.IsTrue(qTime > fqTime, "Queue is faster than FastQueue! {0} to {1}", qTime, fqTime);
            Assert.IsTrue(linkedTime > fqTime, "linked list is faster than FastQueue! {0} to {1}", linkedTime, fqTime);
        }
        private void WriteToFile()
        {
            int errorCount = 0;

            if (streamsToWrite.Count == 0)
            {
                return;
            }
            if (!Monitor.TryEnter(writeToFileLocker))
            {
                throw new InvalidOperationException("Only one thread at a time allowed for this method.");
            }
            try
            {
                do
                {
                    MemoryStream memory;
                    try
                    {
                        using (memoryLocker.Using())
                        {
                            streamsToWrite.Peek(out memory);
                        }
                        if (trace)
                        {
                            log.Trace("Writing buffer size: " + memory.Position);
                        }
                        fs.Write(memory.GetBuffer(), 0, (int)memory.Position);
                        fs.Flush();
                        memory.Position = 0;
                        using (memoryLocker.Using())
                        {
                            streamsToWrite.Dequeue(out memory);
                            streamsAvailable.Enqueue(memory, 0L);
                        }
                        if (errorCount > 0)
                        {
                            log.Notice(Symbol + ": Retry successful.");
                        }
                        errorCount          = 0;
                        currentSleepSeconds = origSleepSeconds;
                    }
                    catch (IOException e)
                    {
                        errorCount++;
                        log.Debug(Symbol + ": " + e.Message + "\nPausing " + currentSleepSeconds + " seconds before retry.");
                        Factory.Parallel.Sleep(3000);
                    }
                } while (errorCount > 0);
            }
            finally
            {
                Monitor.Exit(writeToFileLocker);
            }
        }
Example #6
0
        static void Main(string[] args)
        {
            FastQueue <int> queue = new FastQueue <int>();

            for (int i = 1; i <= 5; i++)
            {
                queue.Enqueue(i);
            }
            queue.Dequeue();

            Console.WriteLine(string.Join(", ", queue));
        }
Example #7
0
        public void TestQueueThenRemoveOneByOne()
        {
            StringBuilder      buf = new StringBuilder();
            FastQueue <string> q   = new FastQueue <string>();

            q.Enqueue("a");
            buf.Append(q.Dequeue());
            q.Enqueue("b");
            buf.Append(q.Dequeue());
            q.Enqueue("c");
            buf.Append(q.Dequeue());
            q.Enqueue("d");
            buf.Append(q.Dequeue());
            q.Enqueue("e");
            buf.Append(q.Dequeue());
            Assert.AreEqual(0, q.Count, "queue should be empty");
            string expecting = "abcde";
            string found     = buf.ToString();

            Assert.AreEqual(expecting, found);
        }
Example #8
0
        public void TestGetFromEmptyQueueAfterSomeAdds()
        {
            FastQueue <string> q = new FastQueue <string>();

            q.Enqueue("a");
            q.Enqueue("b");
            q.Dequeue();
            q.Dequeue();
            string msg = null;

            try
            {
                q.Dequeue();
            }
            catch (InvalidOperationException nsee)
            {
                msg = nsee.Message;
            }
            string expecting = "queue index 0 > last index -1";
            string found     = msg;

            Assert.AreEqual(expecting, found);
        }
Example #9
0
        public void ConstructorWithCollectionAndInterationTest()
        {
            List <string> food = new List <string>();

            food.Add("Pizza");

            FastQueue <string> q = new FastQueue <string>(food);

            Assert.IsTrue(q.Count == 1, "FastQueue should have one item but doesn't!");
            Assert.IsTrue(q.Peek() == food[0], "FastQueue is missing the expected value!");

            food.Add("Eggs");
            q = new FastQueue <string>(food);
            Assert.IsTrue(q.Count == 2, "FastQueue should have two items but doesn't!");
            Assert.IsTrue(q.Peek() == food[0]);
            Assert.IsTrue(q.Dequeue() == food[0]);
            Assert.IsTrue(q.Peek() == food[1]);
            Assert.IsTrue(q.Dequeue() == food[1]);
            Assert.IsTrue(q.Count == 0);
            Assert.IsFalse(q.Any());

            food.Add("Chicken");
            food.Add("Bratz");
            food.Add("Cheese");

            q = new FastQueue <string>(food);
            Assert.IsTrue(q.Count == 5);

            for (int i = 0; i < food.Count; i++)
            {
                string f = food[i];
                Assert.IsTrue(f == q.Peek());
                Assert.IsTrue(f == q.Dequeue());
            }
            Assert.ThrowsException <InvalidOperationException>(() => q.Peek());
        }
Example #10
0
 public void TestGetFromEmptyQueue()
 {
     FastQueue<string> q = new FastQueue<string>();
     string msg = null;
     try
     {
         q.Dequeue();
     }
     catch (IndexOutOfRangeException nsee)
     {
         msg = nsee.Message;
     }
     string expecting = "queue index 0 > last index -1";
     string found = msg;
     Assert.AreEqual( expecting, found );
 }
Example #11
0
 private void MoveMemoryToQueue()
 {
     using (memoryLocker.Using())
     {
         streamsToWrite.Enqueue(fileBlock, 0L);
     }
     if (streamsAvailable.Count == 0)
     {
         fileBlock = new FileBlock(fileHeader.blockSize);
     }
     else
     {
         using (memoryLocker.Using())
         {
             streamsAvailable.Dequeue(out fileBlock);
         }
     }
 }
 private void MoveMemoryToQueue()
 {
     using (memoryLocker.Using())
     {
         streamsToWrite.Enqueue(memory, 0L);
     }
     if (streamsAvailable.Count == 0)
     {
         memory = new MemoryStream();
     }
     else
     {
         using (memoryLocker.Using())
         {
             streamsAvailable.Dequeue(out memory);
         }
     }
 }
Example #13
0
        public void EnqueueDequeueTest()
        {
            FastQueue <int> q = new FastQueue <int>();

            for (int i = 0; i < maxNumber; i++)
            {
                q.Enqueue(i);
            }

            Assert.IsTrue(q.Count == maxNumber);

            int expected = 0;

            for (int i = maxNumber - 1; i >= 0; i--)
            {
                Assert.IsTrue(q.Peek() == expected, "Peek() does not equal i");
                Assert.IsTrue(q.Dequeue() == expected++, "Dequeue() does not equal i");
            }

            Assert.IsTrue(q.Count == 0, "Count is not 0");
        }
Example #14
0
 private void WriteToFile()
 {
     if (streamsToWrite.Count == 0)
     {
         return;
     }
     if (!Monitor.TryEnter(writeToFileLocker))
     {
         throw new InvalidOperationException("Only one thread at a time allowed for this method.");
     }
     try
     {
         while (streamsToWrite.Count > 0)
         {
             FileBlock fileBlock;
             using (memoryLocker.Using())
             {
                 streamsToWrite.Peek(out fileBlock);
             }
             if (trace)
             {
                 log.Trace(streamsToWrite.Count + " blocks in queue.");
             }
             fileBlock.Write(fs);
             lastTimeWritten = fileBlock.LastUtcTimeStamp;
             using (memoryLocker.Using())
             {
                 streamsToWrite.Dequeue(out fileBlock);
                 streamsAvailable.Enqueue(fileBlock, 0L);
             }
         }
     }
     finally
     {
         Monitor.Exit(writeToFileLocker);
     }
 }
Example #15
0
        public void EmptyConstructorAndEmptyQueueTest()
        {
            FastQueue <int> q = new FastQueue <int>();

            Assert.IsTrue(q.Count == 0, "Count is non-zero!");
            Assert.IsTrue(!q.Any(), "FastQueue has a member when empty!");

            q.Clear();

            Assert.IsTrue(q.Count() == 0, "Count is non-zero!");
            Assert.IsTrue(!q.Any(), "FastQueue has a member when empty!");

            bool looped = false;

            foreach (int i in q)
            {
                looped = true;
            }
            Assert.IsTrue(!looped, "FastQueue looped even though empty!");

            Assert.ThrowsException <InvalidOperationException>(() => q.Dequeue(), "FastQueue didn't throw InvalidOperationException on empty Dequeue()");

            Assert.ThrowsException <InvalidOperationException>(() => q.Peek(), "FastQueue didn't throw InvalidOperationException on empty Peek()");
        }
Example #16
0
        void CastLight(FastQueue queue, bool sunlight, byte mask, byte step)
        {
            BlockInfo info = game.BlockInfo;

            byte[] blocks = game.World.blocks;
            int    maxX = width - 1, maxY = height - 1, maxZ = length - 1;
            int    oneY    = width * length;
            byte   invMask = (byte)~mask;

            while (queue.count > 0)
            {
                int idx; queue.Dequeue(out idx);

                int  x     = idx % width;
                int  y     = idx / oneY;
                int  z     = (idx / width) % length;
                byte light = lightLevels[x, y, z];

                if (light == step)
                {
                    break;                                // doesn't cast further
                }
                light -= step;

                if (x > 0 && lightPasses[blocks[idx - 1]] && light > (lightLevels[x - 1, y, z] & mask))
                {
                    lightLevels[x - 1, y, z] &= invMask;
                    lightLevels[x - 1, y, z] |= light;
                    queue.Enqueue(idx - 1);
                }
                if (x < maxX && lightPasses[blocks[idx + 1]] && light > (lightLevels[x + 1, y, z] & mask))
                {
                    lightLevels[x + 1, y, z] &= invMask;
                    lightLevels[x + 1, y, z] |= light;
                    queue.Enqueue(idx + 1);
                }
                if (z > 0 && lightPasses[blocks[idx - width]] && light > (lightLevels[x, y, z - 1] & mask))
                {
                    lightLevels[x, y, z - 1] &= invMask;
                    lightLevels[x, y, z - 1] |= light;
                    queue.Enqueue(idx - width);
                }
                if (z < maxZ && lightPasses[blocks[idx + width]] && light > (lightLevels[x, y, z + 1] & mask))
                {
                    lightLevels[x, y, z + 1] &= invMask;
                    lightLevels[x, y, z + 1] |= light;
                    queue.Enqueue(idx + width);
                }

                if (y < maxY && lightPasses[blocks[idx + oneY]] && light > (lightLevels[x, y + 1, z] & mask))
                {
                    lightLevels[x, y + 1, z] &= invMask;
                    lightLevels[x, y + 1, z] |= light;
                }

                // sunlight should propagate downwards without losing light
                if (sunlight && light == 0xE0)
                {
                    light += step;
                }

                if (y > 0 && lightPasses[blocks[idx - oneY]] && light > (lightLevels[x, y - 1, z] & mask))
                {
                    lightLevels[x, y - 1, z] &= invMask;
                    lightLevels[x, y - 1, z] |= light;
                    queue.Enqueue(idx - oneY);
                }
            }
        }
Example #17
0
 public void TestGetFromEmptyQueue()
 {
     FastQueue<string> q = new FastQueue<string>();
     q.Dequeue();
 }
Example #18
0
        public void TestGetFromEmptyQueue()
        {
            FastQueue <string> q = new FastQueue <string>();

            q.Dequeue();
        }
Example #19
0
 public void TestQueueThenRemoveOneByOne()
 {
     StringBuilder buf = new StringBuilder();
     FastQueue<string> q = new FastQueue<string>();
     q.Enqueue( "a" );
     buf.Append( q.Dequeue() );
     q.Enqueue( "b" );
     buf.Append( q.Dequeue() );
     q.Enqueue( "c" );
     buf.Append( q.Dequeue() );
     q.Enqueue( "d" );
     buf.Append( q.Dequeue() );
     q.Enqueue( "e" );
     buf.Append( q.Dequeue() );
     Assert.AreEqual( 0, q.Count, "queue should be empty" );
     string expecting = "abcde";
     string found = buf.ToString();
     Assert.AreEqual( expecting, found );
 }
Example #20
0
 public void TestQueueThenRemoveAll()
 {
     FastQueue<string> q = new FastQueue<string>();
     q.Enqueue( "a" );
     q.Enqueue( "b" );
     q.Enqueue( "c" );
     q.Enqueue( "d" );
     q.Enqueue( "e" );
     StringBuilder buf = new StringBuilder();
     while ( q.Count > 0 )
     {
         string o = q.Dequeue();
         buf.Append( o );
         if ( q.Count > 0 )
             buf.Append( " " );
     }
     Assert.AreEqual( 0, q.Count, "queue should be empty" );
     string expecting = "a b c d e";
     string found = buf.ToString();
     Assert.AreEqual( expecting, found );
 }