コード例 #1
0
ファイル: Program.cs プロジェクト: radtek/AmdUtil
        static void Main(string[] args)
        {
            IQueue <int> q = new ArrayQueue <int>(10);

            for (int i = 0; i < 10; i++)
            {
                q.Enqueue(i);
            }
            print(q);
            q.Enqueue(10);
            print(q);
            q.Enqueue(11);
            print(q);
            for (int i = 12; i < 100; i++)
            {
                q.Enqueue(i);
            }
            print(q);
            Console.WriteLine();

            Random r = new Random();

            for (int i = 0; i < length; i++)
            {
                arr[i] = r.NextDouble();
            }
            Console.WriteLine("testing measurement functions");
            Diagnostics.MeasureAndPrintToConsole("Random for loop", 10, MeasureMerge);
            Diagnostics.MeasureAndPrintToConsole("Random for loop", 10, MeasureBubble);

            Console.ReadKey();
        }
コード例 #2
0
 public void Setup()
 {
     ArrayQueue.Enqueue("Phil");
     ArrayQueue.Enqueue("Bob");
     ArrayQueue.Enqueue("Marry");
     ArrayQueue.Enqueue("Bobby");
 }
コード例 #3
0
        public void EnqueueAndDequeue()
        {
            var students = new Student[]
            {
                new Student {
                    No = 1, Name = "李1", Score = 22.1
                },
                new Student {
                    No = 2, Name = "李2", Score = 32.3
                },
                new Student {
                    No = 3, Name = "李3", Score = 42.5
                },
                new Student {
                    No = 4, Name = "李4", Score = 45
                }
            };

            var arrayQueue = new ArrayQueue <Student>(3);

            foreach (var student in students)
            {
                var result = arrayQueue.Enqueue(student);
            }


            var deqStudent = new Student();

            arrayQueue.Dequeue(out deqStudent);

            arrayQueue.Enqueue(students[3]);
        }
コード例 #4
0
ファイル: ArrayQueueTests.cs プロジェクト: fcurdi/DSA
        public void EnqueuingAfterClearingCollection()
        {
            var stack = new ArrayQueue <int>();

            int itemCount = 500000;

            for (int i = 0; i < itemCount; i++)
            {
                stack.Enqueue(i);
            }

            stack.Clear();

            for (int i = 0; i < itemCount; i++)
            {
                stack.Enqueue(i);
            }

            int trueCount = 0;
            int lastItem  = int.MinValue;

            foreach (var item in stack)
            {
                if (lastItem > item)
                {
                    Assert.Fail();
                }
                lastItem = item;
                trueCount++;
            }

            Assert.IsTrue(stack.Count == itemCount &&
                          stack.Count == trueCount);
        }
        private void BreadthFirstSearch(LinkedList.LinkedList <T> visitedNodes)
        {
            IQueue <int> queue = new ArrayQueue <int>(_nodes.Count);

            bool[] visited = new bool[_nodes.Count];
            bool[] inqueue = new bool[_nodes.Count];

            queue.Enqueue(0);
            inqueue[0] = true;

            while (!queue.IsEmpty())
            {
                var n = queue.Dequeue();
                visited[n] = true;
                inqueue[n] = false;
                visitedNodes.Insert(_nodes[n]);

                foreach (var v in GetAdjacentVertices(n))
                {
                    if (!visited[v] && !inqueue[v])
                    {
                        queue.Enqueue(v);
                        inqueue[v] = true;
                    }
                }
            }
        }
コード例 #6
0
        public void EnqueueTest() {
            for (int i = 0; i < 6; i++) {
                queue.Enqueue(i);
            }

            Assert.AreEqual("{0, 1, 2, 3, 4, 5}", queue.ToString());
        }
コード例 #7
0
        public void intNumbEnter()
        {
            //arrange
            int[] expected_ar = { 5, 6, 7 };

            //act
            ArrayQueue <int> arr = new ArrayQueue <int>(3);

            arr.Enqueue(5);
            arr.Enqueue(6);
            arr.Enqueue(7);
            int[] actual_ar = new int[3];
            int   i         = 0;

            while (!arr.IsEmpty())
            {
                actual_ar[i] = arr.Peek();
                arr.Dequeue();
                i++;
            }
            //foreach(var elem in arr)
            //{
            //    actual_ar[i] = elem;
            //    i++;
            //}

            //assert
            Assert.AreEqual(true, arr.isEmpty);
            CollectionAssert.AreEqual(expected_ar, actual_ar);
        }
コード例 #8
0
        public void AddFiveItemsAndVerify()
        {
            int linkedListQueueLast = 0;
            int arrayQueueLast      = 0;

            for (int i = 1; i <= 5; i++)
            {
                linkedListQueue.Enqueue(i);
                arrayQueue.Enqueue(i);
            }

            Assert.AreEqual(linkedListQueue.Peek(), 1, "invalid top element");
            Assert.AreEqual(linkedListQueue.Count, 5, "invalid count");

            Assert.AreEqual(arrayQueue.Peek(), 1, "invalid top element");
            Assert.AreEqual(arrayQueue.Count, 5, "invalid count");

            while (linkedListQueue.Count > 0)
            {
                linkedListQueueLast = linkedListQueue.Dequeue();
                arrayQueueLast      = arrayQueue.Dequeue();
            }
            Assert.AreEqual(linkedListQueueLast, 5, "invalid last element");
            Assert.AreEqual(arrayQueueLast, 5, "invalid last element");
        }
コード例 #9
0
ファイル: Program.cs プロジェクト: viaches/leetcode
        static void Main(string[] args)
        {
            var arrayQueue = new ArrayQueue <string>();

            arrayQueue.Enqueue("1");
            arrayQueue.Enqueue("2");
            arrayQueue.Enqueue("3");
            arrayQueue.Enqueue("4");
            arrayQueue.Enqueue("5");

            arrayQueue.Dequeue();
            arrayQueue.Dequeue();
            arrayQueue.Dequeue();
            arrayQueue.Dequeue();

            var linkedListQueue = new LinkedListQueue <string>();

            linkedListQueue.Enqueue("1");
            linkedListQueue.Enqueue("2");
            linkedListQueue.Enqueue("3");
            linkedListQueue.Enqueue("4");
            linkedListQueue.Enqueue("5");

            linkedListQueue.Dequeue();
            linkedListQueue.Dequeue();
            linkedListQueue.Dequeue();
            linkedListQueue.Dequeue();
        }
コード例 #10
0
        static void Main(string[] args)
        {
            int count = 1;
            ArrayQueue <string> arr = new ArrayQueue <string>(count);
            string s = "cfvfd";

            for (int i = 0; i < count; i++)
            {
                arr.Enqueue("элемент" + i);
            }
            IEnumerator <string> el1 = arr.GetEnumerator();

            arr.Enqueue(s);
            arr.Enqueue(s);
            arr.Enqueue(s);
            arr.Enqueue(s);
            foreach (string el in arr)
            {
                Console.WriteLine(el);
            }
            Console.WriteLine("//--//--//");
            Console.WriteLine();

            Console.WriteLine(arr.Dequeue());
            // Console.WriteLine(arr.Peek());
            // arr.Clear();
            Console.ReadLine();
        }
コード例 #11
0
        private static void ArrayQueueExercise()
        {
            try
            {
                ArrayQueue qArray = new ArrayQueue(4);
                qArray.Enqueue(10);
                qArray.Enqueue(20);
                qArray.Enqueue(30);
                qArray.Enqueue(40);
                Console.WriteLine(qArray.ToString());

                Console.WriteLine($"Peek {qArray.Peek()}");
                Console.WriteLine($"Is Empty {qArray.isEmpty()}");
                Console.WriteLine($"Is full {qArray.isFull()}");

                Console.WriteLine(qArray.Dequeue());
                Console.WriteLine(qArray.Dequeue());
                qArray.Enqueue(50);
                Console.WriteLine(qArray.Dequeue());
                Console.WriteLine(qArray.Dequeue());
                Console.WriteLine(qArray.Dequeue());
                Console.WriteLine(qArray.Dequeue());
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
        }
コード例 #12
0
        public void TestToStringReturnsCorrectValue()
        {
            var queue = new ArrayQueue <int>(10);

            queue.Enqueue(1);
            queue.Enqueue(2);
            Assert.AreEqual("1,2", queue.ToString());
        }
コード例 #13
0
        public void EnqueueForArrayQueue_ShouldAddItemToTeQueue()
        {
            // Arrange
            _arrayQueue.Enqueue(3);

            // Act
            // Assert
            Assert.IsFalse(_arrayQueue.IsEmpty);
        }
コード例 #14
0
        public void TestEnqueueFullQueueThrows()
        {
            var queue = new ArrayQueue <int>(1);

            queue.Enqueue(1);
            var ex = Assert.Throws <InvalidOperationException>(() => queue.Enqueue(2));

            Assert.AreEqual("Queue is full", ex.Message);
        }
コード例 #15
0
        public void Peek_EnqueueTwoItems_ReturnsHeadItem()
        {
            var queue = new ArrayQueue <int>();

            queue.Enqueue(1);
            queue.Enqueue(2);

            Assert.AreEqual(1, queue.Peek());
        }
コード例 #16
0
        public void Enqueue_ValidParamsPassed_Success()
        {
            IQueue <int> queue = new ArrayQueue <int>(5);

            queue.Enqueue(1);
            queue.Enqueue(2);
            queue.Enqueue(3);

            Assert.That(queue, Is.EquivalentTo(new int[] { 1, 2, 3 }));
        }
コード例 #17
0
        public void ArrayQueue_Enqueue_AddsElementAtFront()
        {
            var queue = new ArrayQueue <string>();

            queue.Enqueue("cat");
            queue.Enqueue("dog");
            queue.Enqueue("catdog");
            Assert.AreEqual(3, queue.Size);
            Assert.AreEqual("cat", queue.Front);
        }
コード例 #18
0
        public void Enqueue_Get_Count_When_Only_Enqueue()
        {
            var queue = new ArrayQueue <int>(5);

            queue.Enqueue(1);
            queue.Enqueue(2);
            queue.Enqueue(3);

            Assert.Equal(3, queue.Count);
        }
コード例 #19
0
        public void EnumerateElements_SmallChain_Success()
        {
            IQueue <int> queue = new ArrayQueue <int>(5);

            queue.Enqueue(3);
            queue.Enqueue(1);
            queue.Enqueue(4);

            Assert.That(queue, Is.EquivalentTo(new int[] { 3, 1, 4 }));
        }
コード例 #20
0
        public void QueueIsEnptyWhenAllElementsAreRemoved()
        {
            var sut = new ArrayQueue <int>();

            sut.Enqueue(1);
            sut.Enqueue(2);
            Assert.Equal(1, sut.Dequeue());
            Assert.Equal(2, sut.Dequeue());
            Assert.True(sut.IsEmpty);
        }
コード例 #21
0
        public void EnqueueTwoItems_DequequeOne_PeakShouldBeEqualToLastItem()
        {
            var queue = new ArrayQueue <int>();

            queue.Enqueue(1);
            queue.Enqueue(2);

            queue.Dequeue();

            Assert.AreEqual(2, queue.Peek());
        }
コード例 #22
0
        public void NonEmptyQueueReadsNotEmptyTest()
        {
            ArrayQueue target = new ArrayQueue();

            target.Enqueue(0);
            for (int i = 0; i < 1000000 && !target.IsFull; i++)
            {
                target.Enqueue(i);
                Assert.IsFalse(target.IsEmpty, string.Format("ArrayQueue reads as empty after adding {0} objects and never removing any", i + 1));
            }
        }
コード例 #23
0
        public void Peek_EnqueueTwoItemsAndDequeue_ReturnsHeadElement()
        {
            var queue = new ArrayQueue <int>();

            queue.Enqueue(1);
            queue.Enqueue(2);

            queue.Dequeue();

            Assert.AreEqual(2, queue.Peek());
        }
コード例 #24
0
        public void Enqueue_QueueOverflow_Failure()
        {
            IQueue <int> queue = new ArrayQueue <int>(2);

            queue.Enqueue(3);
            queue.Enqueue(1);

            Assert.Throws <InvalidOperationException>(() =>
            {
                queue.Enqueue(4);
            });
        }
コード例 #25
0
        public void Capacity_EnqueueManyItems_DoubledCapacity()
        {
            var queue = new ArrayQueue <int>();

            queue.Enqueue(1);
            queue.Enqueue(2);
            queue.Enqueue(3);
            queue.Enqueue(4);
            queue.Enqueue(5);

            Assert.AreEqual(8, queue.Capacity);
        }
コード例 #26
0
        public void Enqueue_When_Full()
        {
            var queue = new ArrayQueue <int>(4);

            queue.Enqueue(1);
            queue.Enqueue(2);
            queue.Enqueue(3);
            queue.Enqueue(4);

            Exception ex = Assert.Throws <InvalidOperationException>(() => queue.Enqueue(5));

            Assert.IsType <InvalidOperationException>(ex);
        }
コード例 #27
0
        public void Dequeue_OK_When_Not_Empty()
        {
            var queue = new ArrayQueue <int>(4);

            queue.Enqueue(1);
            queue.Enqueue(2);
            queue.Enqueue(3);
            queue.Enqueue(4);

            int item = queue.Dequeue();

            Assert.Equal(1, item);
        }
コード例 #28
0
        public void TestGrow()
        {
            ArrayQueue <string> queue = new ArrayQueue <string>(3, 5);

            Assert.AreEqual(3, queue.Capacity);

            queue.Enqueue("a");
            queue.Enqueue("b");
            Assert.AreEqual(3, queue.Capacity);
            queue.Enqueue("c");
            Assert.AreEqual(8, queue.Capacity);

            for (int i = 0; i < 4; i++)
            {
                queue.Enqueue("" + ('d' + i));
            }
            Assert.AreEqual(8, queue.Capacity);
            for (int i = 0; i < 4; i++)
            {
                queue.Dequeue();
            }
            Assert.AreEqual(8, queue.Capacity);
            for (int i = 0; i < 4; i++)
            {
                queue.Enqueue("" + ('d' + i));
            }
            Assert.AreEqual(8, queue.Capacity);
            for (int i = 0; i < 4; i++)
            {
                queue.Dequeue();
            }
            Assert.AreEqual(8, queue.Capacity);
            for (int i = 0; i < 4; i++)
            {
                queue.Enqueue("" + ('d' + i));
            }
            Assert.AreEqual(8, queue.Capacity);

            queue.Enqueue("z");
            Assert.AreEqual(13, queue.Capacity);

            queue.Clear();
            Assert.AreEqual(13, queue.Capacity);
            for (int i = 0; i < 12; i++)
            {
                queue.Enqueue("" + ('a' + i));
            }
            Assert.AreEqual(13, queue.Capacity);
            queue.Clear();
            Assert.AreEqual(13, queue.Capacity);
            for (int i = 0; i < 12; i++)
            {
                queue.Enqueue("" + ('a' + i));
            }
            Assert.AreEqual(13, queue.Capacity);
        }
コード例 #29
0
        /// <summary>
        /// Add the the specified item to the cache. Returns 'true' if
        /// an item of the same value was not found.
        /// </summary>
        public bool Add(TValue item)
        {
            _lock.Take();

            // does the item already exist in the cache?
            int count;

            if (_lookup.TryGetValue(item, out count))
            {
                // yes, add the item to the end of the queue
                _queue.Enqueue(item);
                // increment the number of duplicate items in the lookup
                _lookup[item] = count + 1;

                _lock.Release();

                return(false);
            }
            else
            {
                // add the item to the current queue
                _queue.Enqueue(item);
                // add the item to the lookup
                _lookup.Add(item, 1);

                // has the max number of items been reached?
                if (_lookup.Count > MaxCount)
                {
                    // yes, dequeue an item
                    _queue.Next();

                    // remove it from the lookup if not already removed
                    if (_lookup.TryGetValue(_queue.Current, out count))
                    {
                        if (count == 1)
                        {
                            _lookup.Remove(_queue.Current);
                        }
                        else
                        {
                            _lookup[_queue.Current] = count - 1;
                        }
                    }
                }

                _lock.Release();

                return(true);
            }
        }
コード例 #30
0
        public void EnqueueToFullQueueThrowsQueueFullExceptionTest()
        {
            ArrayQueue target = new ArrayQueue();

            Assert.IsFalse(target.IsFull);
            int i;

            for (i = 0; i < 1000000 && !target.IsFull; i++)
            {
                target.Enqueue(i);
            }
            Assert.IsTrue(target.IsFull, "Added 1000000 elements to ArrayQueue and it still doesn't return IsFull=true");
            target.Enqueue(0);
            Assert.Fail("Enqueue to full queue didn't throw QueueEmptyException");
        }
コード例 #31
0
        public void TestGrow()
        {
            ArrayQueue<string> queue = new ArrayQueue<string>(3, 5);
            Assert.AreEqual(3, queue.Capacity);

            queue.Enqueue("a");
            queue.Enqueue("b");
            Assert.AreEqual(3, queue.Capacity);
            queue.Enqueue("c");
            Assert.AreEqual(8, queue.Capacity);

            for (int i = 0; i < 4; i++)
                queue.Enqueue("" + ('d' + i));
            Assert.AreEqual(8, queue.Capacity);
            for (int i = 0; i < 4; i++)
                queue.Dequeue();
            Assert.AreEqual(8, queue.Capacity);
            for (int i = 0; i < 4; i++)
                queue.Enqueue("" + ('d' + i));
            Assert.AreEqual(8, queue.Capacity);
            for (int i = 0; i < 4; i++)
                queue.Dequeue();
            Assert.AreEqual(8, queue.Capacity);
            for (int i = 0; i < 4; i++)
                queue.Enqueue("" + ('d' + i));
            Assert.AreEqual(8, queue.Capacity);

            queue.Enqueue("z");
            Assert.AreEqual(13, queue.Capacity);

            queue.Clear();
            Assert.AreEqual(13, queue.Capacity);
            for (int i = 0; i < 12; i++)
                queue.Enqueue("" + ('a' + i));
            Assert.AreEqual(13, queue.Capacity);
            queue.Clear();
            Assert.AreEqual(13, queue.Capacity);
            for (int i = 0; i < 12; i++)
                queue.Enqueue("" + ('a' + i));
            Assert.AreEqual(13, queue.Capacity);
        }
コード例 #32
0
        public void TestRemove()
        {
            ArrayQueue<string> queue = new ArrayQueue<string>(3,3);

            queue.Enqueue("0");
            queue.Enqueue("x");

            for (int i=1;i<100;i++)
            {
                queue.Enqueue(""+i);
                queue.Enqueue("x");
                queue.RemoveAt(queue.Count-3);
                queue[queue.Count-3] = queue[queue.Count-3]+"!";
            }

            for (int i = 0; i < 99; i++)
            {
                Assert.AreEqual(i + "!", queue[i]);
            }
        }
コード例 #33
0
        public void TestRemoveAt1()
        {
            ArrayQueue<object> queue = new ArrayQueue<object>(100, 3);

            for (int i = 1; i <= 16000; i++)
            {
                Assert.AreEqual(i, queue.Enqueue(i.ToString()));
            }

            Assert.AreEqual(16000, queue.Count);

            for (int i = 1; i <= 200; i++)
            {
                Assert.IsTrue(queue.Remove((500+i).ToString()));
                Assert.AreEqual(16000 - i, queue.Count);
            }

            for (int i = 1; i <= 100; i++)
            {
                Assert.IsTrue(queue.Remove(i.ToString()));
                Assert.AreEqual((16000-200) - i, queue.Count);
            }

            Assert.IsFalse(queue.Remove(16001.ToString()));

            for (int i = queue.Count; i-- > 0; )
            {
                queue.Dequeue();
            }

            Assert.AreEqual(0, queue.Count);
        }
コード例 #34
0
ファイル: Program.cs プロジェクト: Yuanxiangz/WorkSpace
        static void test8()
        {
            ArrayQueue<int> queue = new ArrayQueue<int>(3);
            queue.Enqueue(1);
            queue.Enqueue(2);
            queue.Dequeue();
            queue.Enqueue(3);
            queue.Dequeue();
            queue.Enqueue(4);

            Console.WriteLine(queue.Dequeue());
            Console.WriteLine(queue.Dequeue());
            Console.WriteLine(queue.Dequeue());
            Console.WriteLine(queue.Dequeue());
            Console.WriteLine(queue.Dequeue());
            Console.WriteLine(queue.Dequeue());
        }
コード例 #35
0
        public void TestRemoveAt2()
        {
            ArrayQueue<object> queue = new ArrayQueue<object>(100, 3);

            int dataSize = 16000;
            List<object> list = new List<object>();

            for (int i = 1; i <= dataSize; i++)
            {
                object o = new object();
                list.Add(o);
                queue.Add(o);
            }

            Assert.AreEqual(list.Count, queue.Count);

            foreach (object item in queue)
            {
                Assert.IsNotNull(item);
            }

            Random rand = new Random((int)(DateTime.UtcNow.Ticks/1000));

            for (int i = 0; i < list.Count; i++)
            {
                int r = rand.Next(0, list.Count - 1);
                object temp = list[i];
                list[i] = list[r];
                list[r] = temp;
            }

            foreach (object item in list)
            {
                Assert.IsTrue(queue.Remove(item));
                queue.Enqueue(item);
                Assert.IsTrue(queue.Remove(item));
                Assert.IsFalse(queue.Remove(item));
            }

            Assert.AreEqual(0, queue.Count);
        }