Пример #1
0
        /// <summary>
        /// Removes from this list all the elements that are Contained in
        /// the specified collection
        /// </summary>
        /// <param name="c">collection that defines which elements will be Removed from the list.</param>
        /// <returns>return true if this list Changed as a result of the call.</returns>
        public bool RemoveAll(IntList c)
        {
            bool rval = false;

            for (int j = 0; j < c._limit; j++)
            {
                if (RemoveValue(c._array[j]))
                {
                    rval = true;
                }
            }
            return(rval);
        }
Пример #2
0
 /// <summary>
 /// Appends all of the elements in the specified collection to the
 /// end of this list, in the order that they are returned by the
 /// specified collection's iterator.  The behavior of this
 /// operation is unspecified if the specified collection is
 /// modified while the operation is in progress.  (Note that this
 /// will occur if the specified collection is this list, and it's
 /// nonempty.)
 /// </summary>
 /// <param name="c">collection whose elements are to be Added to this list.</param>
 /// <returns>return true if this list Changed as a result of the call.</returns>
 public bool AddAll(IntList c)
 {
     if (c._limit != 0)
     {
         if ((_limit + c._limit) > _array.Length)
         {
             growArray(_limit + c._limit);
         }
         Array.Copy(c._array, 0, _array, _limit, c._limit);
         _limit += c._limit;
     }
     return(true);
 }
Пример #3
0
        public void TestConstructors()
        {
            IntList list = new IntList();

            Assert.IsTrue(list.IsEmpty());
            list.Add(0);
            list.Add(1);
            IntList list2 = new IntList(list);

            //Assert.AreEqual(list, list2);
            Assert.IsTrue(list.Equals(list2));
            IntList list3 = new IntList(2);

            Assert.IsTrue(list3.IsEmpty());
        }
Пример #4
0
        /// <summary>
        /// Returns true if this list Contains all of the elements of the
        /// specified collection.
        /// </summary>
        /// <param name="c">collection to be Checked for Containment in this list.</param>
        /// <returns>return true if this list Contains all of the elements of the specified collection.</returns>
        public bool ContainsAll(IntList c)
        {
            bool rval = true;

            if (this != c)
            {
                for (int j = 0; rval && (j < c._limit); j++)
                {
                    if (!Contains(c._array[j]))
                    {
                        rval = false;
                    }
                }
            }
            return(rval);
        }
Пример #5
0
        /// <summary>
        /// Retains only the elements in this list that are Contained in
        /// the specified collection.  In other words, Removes from this
        /// list all the elements that are not Contained in the specified
        /// collection.
        /// </summary>
        /// <param name="c">collection that defines which elements this Set will retain.</param>
        /// <returns>return true if this list Changed as a result of the call.</returns>
        public bool RetainAll(IntList c)
        {
            bool rval = false;

            for (int j = 0; j < _limit;)
            {
                if (!c.Contains(_array[j]))
                {
                    Remove(j);
                    rval = true;
                }
                else
                {
                    j++;
                }
            }
            return(rval);
        }
Пример #6
0
        /// <summary>
        /// Compares the specified object with this list for Equality.
        /// Returns true if and only if the specified object is also a
        /// list, both lists have the same size, and all corresponding
        /// pairs of elements in the two lists are Equal.  (Two elements e1
        /// and e2 are equal if e1 == e2.)  In other words, two lists are
        /// defined to be equal if they contain the same elements in the
        /// same order.  This defInition ensures that the Equals method
        /// works properly across different implementations of the List
        /// interface.
        /// </summary>
        /// <param name="o">the object to be Compared for Equality with this list.</param>
        /// <returns>return true if the specified object is equal to this list.</returns>
        public override bool Equals(Object o)
        {
            bool rval = this == o;

            if (!rval && (o != null) && (o.GetType() == this.GetType()))
            {
                IntList other = (IntList)o;

                if (other._limit == _limit)
                {
                    // assume match
                    rval = true;
                    for (int j = 0; rval && (j < _limit); j++)
                    {
                        rval = _array[j] == other._array[j];
                    }
                }
            }
            return(rval);
        }
Пример #7
0
        /// <summary>
        /// Inserts all of the elements in the specified collection into
        /// this list at the specified position.  Shifts the element
        /// currently at that position (if any) and any subsequent elements
        /// to the right (increases their indices).  The new elements will
        /// appear in this list in the order that they are returned by the
        /// specified collection's iterator.  The behavior of this
        /// operation is unspecified if the specified collection is
        /// modified while the operation is in progress.  (Note that this
        /// will occur if the specified collection is this list, and it's
        /// nonempty.)
        /// </summary>
        /// <param name="index">index at which to insert first element from the specified collection.</param>
        /// <param name="c">elements to be inserted into this list.</param>
        /// <returns>return true if this list Changed as a result of the call.</returns>
        public bool AddAll(int index, IntList c)
        {
            if (index > _limit)
            {
                throw new IndexOutOfRangeException();
            }
            if (c._limit != 0)
            {
                if ((_limit + c._limit) > _array.Length)
                {
                    growArray(_limit + c._limit);
                }

                // make a hole
                Array.Copy(_array, index, _array, index + c._limit,
                           _limit - index);

                // fill it in
                Array.Copy(c._array, 0, _array, index, c._limit);
                _limit += c._limit;
            }
            return(true);
        }
Пример #8
0
        public void TestToArray()
        {
            IntList list = new IntList();

            for (int j = 0; j < 1000; j++)
            {
                list.Add(j);
            }
            int[] a1 = list.ToArray();

            Assert.AreEqual(a1.Length, list.Count);
            for (int j = 0; j < 1000; j++)
            {
                Assert.AreEqual(a1[j], list.Get(j));
            }
            int[] a2 = new int[list.Count];
            int[] a3 = list.ToArray(a2);

            Assert.AreSame(a2, a3);
            for (int j = 0; j < 1000; j++)
            {
                Assert.AreEqual(a2[j], list.Get(j));
            }
            int[] ashort = new int[list.Count - 1];
            int[] aLong = new int[list.Count + 1];
            int[] a4 = list.ToArray(ashort);
            int[] a5 = list.ToArray(aLong);

            Assert.IsTrue(a4 != ashort);
            Assert.IsTrue(a5 != aLong);
            Assert.AreEqual(a4.Length, list.Count);
            for (int j = 0; j < 1000; j++)
            {
                Assert.AreEqual(a3[j], list.Get(j));
            }
            Assert.AreEqual(a5.Length, list.Count);
            for (int j = 0; j < 1000; j++)
            {
                Assert.AreEqual(a5[j], list.Get(j));
            }
        }
Пример #9
0
        public void TestSet()
        {
            IntList list = new IntList();

            for (int j = 0; j < 1000; j++)
            {
                list.Add(j);
            }
            for (int j = 0; j < 1001; j++)
            {
                try
                {
                    list.Set(j, j + 1);
                    if (j == 1000)
                    {
                        Assert.Fail("Should have gotten exception");
                    }
                    Assert.AreEqual(j + 1, list.Get(j));
                }
                catch (IndexOutOfRangeException)
                {
                    if (j != 1000)
                    {
                        Assert.Fail("premature exception");
                    }
                }
            }
        }
Пример #10
0
        public void TestSize()
        {
            IntList list = new IntList();

            for (int j = 0; j < 1000; j++)
            {
                Assert.AreEqual(j, list.Count);
                list.Add(j);
                Assert.AreEqual(j + 1, list.Count);
            }
            for (int j = 0; j < 1000; j++)
            {
                Assert.AreEqual(1000 - j, list.Count);
                list.RemoveValue(j);
                Assert.AreEqual(999 - j, list.Count);
            }
        }
Пример #11
0
        public void TestRetainAll()
        {
            IntList list = new IntList();

            for (int j = 0; j < 1000; j++)
            {
                list.Add(j);
            }
            IntList listCopy = new IntList(list);
            IntList listOdd = new IntList();
            IntList listEven = new IntList();

            for (int j = 0; j < 1000; j++)
            {
                if (j % 2 == 0)
                {
                    listEven.Add(j);
                }
                else
                {
                    listOdd.Add(j);
                }
            }
            list.RetainAll(listOdd);
            //Assert.AreEqual(list, listOdd);
            Assert.IsTrue(list.Equals(listOdd));
            list.RetainAll(listEven);
            Assert.IsTrue(list.IsEmpty());
            listCopy.RetainAll(listEven);
            //Assert.AreEqual(listCopy, listEven);
            Assert.IsTrue(listCopy.Equals(listEven));
            listCopy.RetainAll(listOdd);
            Assert.IsTrue(listCopy.IsEmpty());
        }
Пример #12
0
        public void TestAdd()
        {
            IntList list = new IntList();
            int[] testArray =
        {
            0, 1, 2, 3, 5
        };

            for (int j = 0; j < testArray.Length; j++)
            {
                list.Add(testArray[j]);
            }
            for (int j = 0; j < testArray.Length; j++)
            {
                Assert.AreEqual(testArray[j], list.Get(j));
            }
            Assert.AreEqual(testArray.Length, list.Count);

            // add at the beginning
            list.Add(0, -1);
            Assert.AreEqual(-1, list.Get(0));
            Assert.AreEqual(testArray.Length + 1, list.Count);
            for (int j = 0; j < testArray.Length; j++)
            {
                Assert.AreEqual(testArray[j], list.Get(j + 1));
            }

            // add in the middle
            list.Add(5, 4);
            Assert.AreEqual(4, list.Get(5));
            Assert.AreEqual(testArray.Length + 2, list.Count);
            for (int j = 0; j < list.Count; j++)
            {
                Assert.AreEqual(j - 1, list.Get(j));
            }

            // add at the end
            list.Add(list.Count, 6);
            Assert.AreEqual(testArray.Length + 3, list.Count);
            for (int j = 0; j < list.Count; j++)
            {
                Assert.AreEqual(j - 1, list.Get(j));
            }

            // add past end
            try
            {
                list.Add(list.Count + 1, 8);
                Assert.Fail("should have thrown exception");
            }
            catch (IndexOutOfRangeException)
            {

                // as expected
            }

            // Test growth
            list = new IntList(0);
            for (int j = 0; j < 1000; j++)
            {
                list.Add(j);
            }
            Assert.AreEqual(1000, list.Count);
            for (int j = 0; j < 1000; j++)
            {
                Assert.AreEqual(j, list.Get(j));
            }
            list = new IntList(0);
            for (int j = 0; j < 1000; j++)
            {
                list.Add(0, j);
            }
            Assert.AreEqual(1000, list.Count);
            for (int j = 0; j < 1000; j++)
            {
                Assert.AreEqual(j, list.Get(999 - j));
            }
        }
Пример #13
0
        public void TestClear()
        {
            IntList list = new IntList();

            for (int j = 0; j < 500; j++)
            {
                list.Add(j);
            }
            Assert.AreEqual(500, list.Count);
            list.Clear();
            Assert.AreEqual(0, list.Count);
            for (int j = 0; j < 500; j++)
            {
                list.Add(j + 1);
            }
            Assert.AreEqual(500, list.Count);
            for (int j = 0; j < 500; j++)
            {
                Assert.AreEqual(j + 1, list.Get(j));
            }
        }
Пример #14
0
        public void TestRemoveValue()
        {
            IntList list = new IntList();

            for (int j = 0; j < 1000; j++)
            {
                list.Add(j / 2);
            }
            for (int j = 0; j < 1000; j++)
            {
                if (j < 500)
                {
                    Assert.IsTrue(list.RemoveValue(j));
                    Assert.IsTrue(list.RemoveValue(j));
                }
                Assert.IsTrue(!list.RemoveValue(j));
            }
        }
Пример #15
0
        /// <summary>
        /// Returns true if this list Contains all of the elements of the
        /// specified collection.
        /// </summary>
        /// <param name="c">collection to be Checked for Containment in this list.</param>
        /// <returns>return true if this list Contains all of the elements of the specified collection.</returns>
        public bool ContainsAll(IntList c)
        {
            bool rval = true;

            if (this != c)
            {
                for (int j = 0; rval && (j < c._limit); j++)
                {
                    if (!Contains(c._array[j]))
                    {
                        rval = false;
                    }
                }
            }
            return rval;
        }
Пример #16
0
 /// <summary>
 /// create a copy of an existing IntList
 /// </summary>
 /// <param name="list">the existing IntList</param>
 public IntList(IntList list)
     : this(list._array.Length)
 {
     Array.Copy(list._array, 0, _array, 0, _array.Length);
     _limit = list._limit;
 }
Пример #17
0
        public void TestGet()
        {
            IntList list = new IntList();

            for (int j = 0; j < 1000; j++)
            {
                list.Add(j);
            }
            for (int j = 0; j < 1001; j++)
            {
                try
                {
                    Assert.AreEqual(j, list.Get(j));
                    if (j == 1000)
                    {
                        Assert.Fail("should have gotten exception");
                    }
                }
                catch (IndexOutOfRangeException)
                {
                    if (j != 1000)
                    {
                        Assert.Fail("unexpected IndexOutOfBoundsException");
                    }
                }
            }
        }
Пример #18
0
        /// <summary>
        /// Retains only the elements in this list that are Contained in
        /// the specified collection.  In other words, Removes from this
        /// list all the elements that are not Contained in the specified
        /// collection.
        /// </summary>
        /// <param name="c">collection that defines which elements this Set will retain.</param>
        /// <returns>return true if this list Changed as a result of the call.</returns>
        public bool RetainAll(IntList c)
        {
            bool rval = false;

            for (int j = 0; j < _limit; )
            {
                if (!c.Contains(_array[j]))
                {
                    Remove(j);
                    rval = true;
                }
                else
                {
                    j++;
                }
            }
            return rval;
        }
Пример #19
0
        public void TestEquals()
        {
            IntList list = new IntList();

            Assert.AreEqual(list, list);
            Assert.IsTrue(!list.Equals(null));
            IntList list2 = new IntList(200);

            Assert.IsTrue(list.Equals(list2));//Assert.AreEqual(list, list2);
            Assert.IsTrue(list2.Equals(list));//Assert.AreEqual(list2, list);
            Assert.AreEqual(list.GetHashCode(), list2.GetHashCode());
            list.Add(0);
            list.Add(1);
            list2.Add(1);
            list2.Add(0);
            Assert.IsTrue(!list.Equals(list2));
            list2.RemoveValue(1);
            list2.Add(1);
            Assert.IsTrue(list.Equals(list2));//Assert.AreEqual(list, list2);
            Assert.IsTrue(list2.Equals(list));//Assert.AreEqual(list2, list);
            list2.Add(2);
            Assert.IsTrue(!list.Equals(list2));
            Assert.IsTrue(!list2.Equals(list));
        }
Пример #20
0
        public void TestContainsAll()
        {
            IntList list = new IntList();

            Assert.IsTrue(list.ContainsAll(list));
            for (int j = 0; j < 10; j++)
            {
                list.Add(j);
            }
            IntList list2 = new IntList(list);

            Assert.IsTrue(list2.ContainsAll(list));
            Assert.IsTrue(list.ContainsAll(list2));
            list2.Add(10);
            Assert.IsTrue(list2.ContainsAll(list));
            Assert.IsTrue(!list.ContainsAll(list2));
            list.Add(11);
            Assert.IsTrue(!list2.ContainsAll(list));
            Assert.IsTrue(!list.ContainsAll(list2));
        }
Пример #21
0
        public void TestContains()
        {
            IntList list = new IntList();

            for (int j = 0; j < 1000; j += 2)
            {
                list.Add(j);
            }
            for (int j = 0; j < 1000; j++)
            {
                if (j % 2 == 0)
                {
                    Assert.IsTrue(list.Contains(j));
                }
                else
                {
                    Assert.IsTrue(!list.Contains(j));
                }
            }
        }
Пример #22
0
 /// <summary>
 /// Appends all of the elements in the specified collection to the
 /// end of this list, in the order that they are returned by the
 /// specified collection's iterator.  The behavior of this
 /// operation is unspecified if the specified collection is
 /// modified while the operation is in progress.  (Note that this
 /// will occur if the specified collection is this list, and it's
 /// nonempty.)
 /// </summary>
 /// <param name="c">collection whose elements are to be Added to this list.</param>
 /// <returns>return true if this list Changed as a result of the call.</returns>
 public bool AddAll(IntList c)
 {
     if (c._limit != 0)
     {
         if ((_limit + c._limit) > _array.Length)
         {
             growArray(_limit + c._limit);
         }
         Array.Copy(c._array, 0, _array, _limit, c._limit);
         _limit += c._limit;
     }
     return true;
 }
Пример #23
0
        public void TestRemove()
        {
            IntList list = new IntList();

            for (int j = 0; j < 1000; j++)
            {
                list.Add(j);
            }
            for (int j = 0; j < 1000; j++)
            {
                Assert.AreEqual(j, list.Remove(0));
                Assert.AreEqual(999 - j, list.Count);
            }
            for (int j = 0; j < 1000; j++)
            {
                list.Add(j);
            }
            for (int j = 0; j < 1000; j++)
            {
                Assert.AreEqual(999 - j, list.Remove(999 - j));
                Assert.AreEqual(999 - j, list.Count);
            }
            try
            {
                list.Remove(0);
                Assert.Fail("should have caught IndexOutOfBoundsException");
            }
            catch (IndexOutOfRangeException)
            {

                // as expected
            }
        }
Пример #24
0
        /// <summary>
        /// Inserts all of the elements in the specified collection into
        /// this list at the specified position.  Shifts the element
        /// currently at that position (if any) and any subsequent elements
        /// to the right (increases their indices).  The new elements will
        /// appear in this list in the order that they are returned by the
        /// specified collection's iterator.  The behavior of this
        /// operation is unspecified if the specified collection is
        /// modified while the operation is in progress.  (Note that this
        /// will occur if the specified collection is this list, and it's
        /// nonempty.)
        /// </summary>
        /// <param name="index">index at which to insert first element from the specified collection.</param>
        /// <param name="c">elements to be inserted into this list.</param>
        /// <returns>return true if this list Changed as a result of the call.</returns>
        public bool AddAll(int index, IntList c)
        {
            if (index > _limit)
            {
                throw new IndexOutOfRangeException();
            }
            if (c._limit != 0)
            {
                if ((_limit + c._limit) > _array.Length)
                {
                    growArray(_limit + c._limit);
                }

                // make a hole
                Array.Copy(_array, index, _array, index + c._limit,
                                 _limit - index);

                // fill it in
                Array.Copy(c._array, 0, _array, index, c._limit);
                _limit += c._limit;
            }
            return true;
        }
Пример #25
0
        public void TestAddAll()
        {
            IntList list = new IntList();

            for (int j = 0; j < 5; j++)
            {
                list.Add(j);
            }
            IntList list2 = new IntList(0);

            list2.AddAll(list);
            list2.AddAll(list);
            Assert.AreEqual(2 * list.Count, list2.Count);
            for (int j = 0; j < 5; j++)
            {
                Assert.AreEqual(list2.Get(j), j);
                Assert.AreEqual(list2.Get(j + list.Count), j);
            }
            IntList empty = new IntList();
            int limit = list.Count;

            for (int j = 0; j < limit; j++)
            {
                Assert.IsTrue(list.AddAll(j, empty));
                Assert.AreEqual(limit, list.Count);
            }
            try
            {
                list.AddAll(limit + 1, empty);
                Assert.Fail("should have thrown an exception");
            }
            catch (IndexOutOfRangeException)
            {

                // as expected
            }

            // try add at beginning
            empty.AddAll(0, list);
            //Assert.AreEqual(empty, list);
            Assert.IsTrue(empty.Equals(list));

            // try in the middle
            empty.AddAll(1, list);
            Assert.AreEqual(2 * list.Count, empty.Count);
            Assert.AreEqual(list.Get(0), empty.Get(0));
            Assert.AreEqual(list.Get(0), empty.Get(1));
            Assert.AreEqual(list.Get(1), empty.Get(2));
            Assert.AreEqual(list.Get(1), empty.Get(6));
            Assert.AreEqual(list.Get(2), empty.Get(3));
            Assert.AreEqual(list.Get(2), empty.Get(7));
            Assert.AreEqual(list.Get(3), empty.Get(4));
            Assert.AreEqual(list.Get(3), empty.Get(8));
            Assert.AreEqual(list.Get(4), empty.Get(5));
            Assert.AreEqual(list.Get(4), empty.Get(9));

            // try at the end
            empty.AddAll(empty.Count, list);
            Assert.AreEqual(3 * list.Count, empty.Count);
            Assert.AreEqual(list.Get(0), empty.Get(0));
            Assert.AreEqual(list.Get(0), empty.Get(1));
            Assert.AreEqual(list.Get(0), empty.Get(10));
            Assert.AreEqual(list.Get(1), empty.Get(2));
            Assert.AreEqual(list.Get(1), empty.Get(6));
            Assert.AreEqual(list.Get(1), empty.Get(11));
            Assert.AreEqual(list.Get(2), empty.Get(3));
            Assert.AreEqual(list.Get(2), empty.Get(7));
            Assert.AreEqual(list.Get(2), empty.Get(12));
            Assert.AreEqual(list.Get(3), empty.Get(4));
            Assert.AreEqual(list.Get(3), empty.Get(8));
            Assert.AreEqual(list.Get(3), empty.Get(13));
            Assert.AreEqual(list.Get(4), empty.Get(5));
            Assert.AreEqual(list.Get(4), empty.Get(9));
            Assert.AreEqual(list.Get(4), empty.Get(14));
        }
Пример #26
0
        /// <summary>
        /// Removes from this list all the elements that are Contained in
        /// the specified collection
        /// </summary>
        /// <param name="c">collection that defines which elements will be Removed from the list.</param>
        /// <returns>return true if this list Changed as a result of the call.</returns>
        public bool RemoveAll(IntList c)
        {
            bool rval = false;

            for (int j = 0; j < c._limit; j++)
            {
                if (RemoveValue(c._array[j]))
                {
                    rval = true;
                }
            }
            return rval;
        }
Пример #27
0
        public void TestLastIndexOf()
        {
            IntList list = new IntList();

            for (int j = 0; j < 1000; j++)
            {
                list.Add(j / 2);
            }
            for (int j = 0; j < 1000; j++)
            {
                if (j < 500)
                {
                    Assert.AreEqual(1 + j * 2, list.LastIndexOf(j));
                }
                else
                {
                    Assert.AreEqual(-1, list.IndexOf(j));
                }
            }
        }
Пример #28
0
 /// <summary>
 /// create a copy of an existing IntList
 /// </summary>
 /// <param name="list">the existing IntList</param>
 public IntList(IntList list)
     : this(list._array.Length)
 {
     Array.Copy(list._array, 0, _array, 0, _array.Length);
     _limit = list._limit;
 }
Пример #29
0
        public void TestIsEmpty()
        {
            IntList list1 = new IntList();
            IntList list2 = new IntList(1000);
            IntList list3 = new IntList(list1);

            Assert.IsTrue(list1.IsEmpty());
            Assert.IsTrue(list2.IsEmpty());
            Assert.IsTrue(list3.IsEmpty());
            list1.Add(1);
            list2.Add(2);
            list3 = new IntList(list2);
            Assert.IsTrue(!list1.IsEmpty());
            Assert.IsTrue(!list2.IsEmpty());
            Assert.IsTrue(!list3.IsEmpty());
            list1.Clear();
            list2.Remove(0);
            list3.RemoveValue(2);
            Assert.IsTrue(list1.IsEmpty());
            Assert.IsTrue(list2.IsEmpty());
            Assert.IsTrue(list3.IsEmpty());
        }