Exemplo n.º 1
0
        public void Test001_DoubleEndedUniqueCollectionBehavesLikeLinkedList()
        {
            var lklist = new LinkedList <int>();

            lklist.AddLast(456);
            lklist.AddLast(123);
            lklist.AddLast(789);
            lklist.AddLast(567);
            lklist.Remove(123);

            var delist = new DoubleEndedUniqueCollection <int>();

            delist.Add(456);
            delist.Add(123);
            delist.Add(789);
            delist.Add(567);
            delist.Remove(123);

            var expectedArray = new int[] { 456, 789, 567 };
            var lklistArray   = lklist.ToArray();
            var delistArray   = delist.ToArray();

            Assert.AreEqual(expectedArray, lklistArray);
            Assert.AreEqual(expectedArray, delistArray);
        }
        public void Test001_DoubleEndedUniqueCollectionIsMuchFasterOnRandomItemAccess()
        {
            var random = new Random((int)DateTime.Now.Millisecond);
            var accessed = new bool[DOUBLEENDEDUNIQUECOLLECTION_TEST_NUMBER_OF_ITEMS];
            var access = new int[DOUBLEENDEDUNIQUECOLLECTION_TEST_NUMBER_OF_ITEMS];
            for (var a = 0; a < access.Length; a++)
            {
                int at;
                while (accessed[at = random.Next(accessed.Length)]) ;
                accessed[at] = true;
                access[a] = at;
            }

            ICollection<int> lklist = new LinkedList<int>();
            for (var i = 0; i < accessed.Length; i++)
            {
                lklist.Add(i);
            }

            ICollection<int> delist = new DoubleEndedUniqueCollection<int>();
            for (var i = 0; i < accessed.Length; i++)
            {
                delist.Add(i);
            }

            var list = lklist;
            var time = Helpers.Measuring.Time.Start();
            for (var i = 0; i < access.Length; i++)
            {
                var value = access[i];
                list.Remove(value);
                list.Add(value);
            }
            var elapsed = time.ElapsedMilliseconds;
            Console.WriteLine();
            Console.WriteLine("LinkedList: {0} ms", elapsed.ToString("0,0"));

            list = delist;
            time = Helpers.Measuring.Time.Start();
            for (var i = 0; i < access.Length; i++)
            {
                var value = access[i];
                list.Remove(value);
                list.Add(value);
            }
            elapsed = time.ElapsedMilliseconds;
            Console.WriteLine();
            Console.WriteLine("Double-ended unique collection: {0} ms", elapsed.ToString("0,0"));

            var lklistArray = lklist.ToArray();
            var delistArray = delist.ToArray();
            Assert.AreEqual(lklistArray, delistArray);
        }
        public void Test001_DoubleEndedUniqueCollectionBehavesLikeLinkedList()
        {
            var lklist = new LinkedList<int>();
            lklist.AddLast(456);
            lklist.AddLast(123);
            lklist.AddLast(789);
            lklist.AddLast(567);
            lklist.Remove(123);

            var delist = new DoubleEndedUniqueCollection<int>();
            delist.Add(456);
            delist.Add(123);
            delist.Add(789);
            delist.Add(567);
            delist.Remove(123);

            var expectedArray = new int[] { 456, 789, 567 };
            var lklistArray = lklist.ToArray();
            var delistArray = delist.ToArray();

            Assert.AreEqual(expectedArray, lklistArray);
            Assert.AreEqual(expectedArray, delistArray);
        }
Exemplo n.º 4
0
        public void Test001_DoubleEndedUniqueCollectionIsMuchFasterOnRandomItemAccess()
        {
            var random   = new Random((int)DateTime.Now.Millisecond);
            var accessed = new bool[DOUBLEENDEDUNIQUECOLLECTION_TEST_NUMBER_OF_ITEMS];
            var access   = new int[DOUBLEENDEDUNIQUECOLLECTION_TEST_NUMBER_OF_ITEMS];

            for (var a = 0; a < access.Length; a++)
            {
                int at;
                while (accessed[at = random.Next(accessed.Length)])
                {
                    ;
                }
                accessed[at] = true;
                access[a]    = at;
            }

            ICollection <int> lklist = new LinkedList <int>();

            for (var i = 0; i < accessed.Length; i++)
            {
                lklist.Add(i);
            }

            ICollection <int> delist = new DoubleEndedUniqueCollection <int>();

            for (var i = 0; i < accessed.Length; i++)
            {
                delist.Add(i);
            }

            var list = lklist;
            var time = Helpers.Measuring.Time.Start();

            for (var i = 0; i < access.Length; i++)
            {
                var value = access[i];
                list.Remove(value);
                list.Add(value);
            }
            var elapsed = time.ElapsedMilliseconds;

            Console.WriteLine();
            Console.WriteLine("LinkedList: {0} ms", elapsed.ToString("0,0"));

            list = delist;
            time = Helpers.Measuring.Time.Start();
            for (var i = 0; i < access.Length; i++)
            {
                var value = access[i];
                list.Remove(value);
                list.Add(value);
            }
            elapsed = time.ElapsedMilliseconds;
            Console.WriteLine();
            Console.WriteLine("Double-ended unique collection: {0} ms", elapsed.ToString("0,0"));

            var lklistArray = lklist.ToArray();
            var delistArray = delist.ToArray();

            Assert.AreEqual(lklistArray, delistArray);
        }