Ejemplo n.º 1
0
        [Test] public void TestBinarySearch()
        {
            IList <int> list = new int[] { };

            Assert.AreEqual(~0, ListExt.BinarySearch(list, 15));
            Assert.AreEqual(~0, ListExt.BinarySearch(list, -15));
            list = new int[] { 5 };
            Assert.AreEqual(0, ListExt.BinarySearch(list, 5));
            Assert.AreEqual(~0, ListExt.BinarySearch(list, 0));
            Assert.AreEqual(~1, ListExt.BinarySearch(list, 10));
            list = new int[] { 5, 7 };
            Assert.AreEqual(~0, ListExt.BinarySearch(list, 0));
            Assert.AreEqual(0, ListExt.BinarySearch(list, 5));
            Assert.AreEqual(~1, ListExt.BinarySearch(list, 6));
            Assert.AreEqual(1, ListExt.BinarySearch(list, 7));
            Assert.AreEqual(~2, ListExt.BinarySearch(list, 10));
            list = new int[] { 1, 5, 7, 13, 17, 29, 29, 31 };
            Assert.AreEqual(~0, ListExt.BinarySearch(list, -1));
            Assert.AreEqual(0, ListExt.BinarySearch(list, 1));
            Assert.AreEqual(~1, ListExt.BinarySearch(list, 2));
            Assert.AreEqual(1, ListExt.BinarySearch(list, 5));
            Assert.AreEqual(~2, ListExt.BinarySearch(list, 6));
            Assert.AreEqual(2, ListExt.BinarySearch(list, 7));
            Assert.AreEqual(~3, ListExt.BinarySearch(list, 10));
            Assert.AreEqual(3, ListExt.BinarySearch(list, 13));
            Assert.AreEqual(~4, ListExt.BinarySearch(list, 16));
            Assert.AreEqual(4, ListExt.BinarySearch(list, 17));
            Assert.AreEqual(~5, ListExt.BinarySearch(list, 28));
            int i = ListExt.BinarySearch(list, 29);

            Assert.IsTrue(i == 5 || i == 6);
            Assert.AreEqual(~7, ListExt.BinarySearch(list, 30));
            Assert.AreEqual(7, ListExt.BinarySearch(list, 31));
            Assert.AreEqual(~8, ListExt.BinarySearch(list, 1000));
        }
Ejemplo n.º 2
0
 public void Resize(int newSize)
 {
     if (newSize < Count)
     {
         RemoveRange(newSize, Count - newSize);
     }
     else if (newSize > Count)
     {
         InsertRange(Count, (IReadOnlyCollection <T>)ListExt.Repeat(default(T), newSize - Count));
     }
 }
Ejemplo n.º 3
0
        public static void Resize <T>(this IListRangeMethods <T> list, int newSize)
        {
            int count = list.Count;

            if (newSize < count)
            {
                list.RemoveRange(newSize, count - newSize);
            }
            else if (newSize > count)
            {
                list.InsertRange(count, (IListSource <T>)ListExt.Repeat(default(T), newSize - count));
            }
        }
Ejemplo n.º 4
0
        public void InsertRange(int index, IEnumerable <T> list)
        {
            int spaceNeeded = list.Count();
            int index2      = _list.Count - index;

            ListExt.InsertRangeHelper(_list, index2, spaceNeeded);
            index2 += spaceNeeded;
            var e = list.GetEnumerator();

            while (e.MoveNext())
            {
                _list[--index2] = e.Current;
            }
        }
Ejemplo n.º 5
0
        public void InsertRange(int index, IReadOnlyCollection <T> list)
        {
            int spaceNeeded = list.Count;
            int index2      = _list.Count - index;

            ListExt.InsertRangeHelper(_list, index2, spaceNeeded);
            index2 += spaceNeeded - 1;
            var e = list.GetEnumerator();

            for (int i = 0; i < spaceNeeded; i++)
            {
                G.Verify(e.MoveNext());
                _list[index2 - i] = e.Current;
            }
        }
Ejemplo n.º 6
0
        [Test] public void TestPredicatedBinarySearch()
        {
            Comparison <int> p    = G.ToComparison <int>();
            IList <int>      list = new int[] { };

            Assert.AreEqual(~0, ListExt.BinarySearch(list, 15, p));
            Assert.AreEqual(~0, ListExt.BinarySearch(list, -15, p));
            list = new int[] { 5 };
            Assert.AreEqual(0, ListExt.BinarySearch(list, 5, p));
            Assert.AreEqual(~0, ListExt.BinarySearch(list, 0, p));
            Assert.AreEqual(~1, ListExt.BinarySearch(list, 10, p));
            list = new int[] { 5, 7 };
            Assert.AreEqual(~0, ListExt.BinarySearch(list, 0, p));
            Assert.AreEqual(0, ListExt.BinarySearch(list, 5, p));
            Assert.AreEqual(~1, ListExt.BinarySearch(list, 6, p));
            Assert.AreEqual(1, ListExt.BinarySearch(list, 7, p));
            Assert.AreEqual(~2, ListExt.BinarySearch(list, 10, p));
            list = new int[] { 1, 5, 7, 13, 17, 29, 29, 31 };
            Assert.AreEqual(~0, ListExt.BinarySearch(list, -1, p));
            Assert.AreEqual(0, ListExt.BinarySearch(list, 1, p));
            Assert.AreEqual(~1, ListExt.BinarySearch(list, 2, p));
            Assert.AreEqual(1, ListExt.BinarySearch(list, 5, p));
            Assert.AreEqual(~2, ListExt.BinarySearch(list, 6, p));
            Assert.AreEqual(2, ListExt.BinarySearch(list, 7, p));
            Assert.AreEqual(~3, ListExt.BinarySearch(list, 10, p));
            Assert.AreEqual(3, ListExt.BinarySearch(list, 13, p));
            Assert.AreEqual(~4, ListExt.BinarySearch(list, 16, p));
            Assert.AreEqual(4, ListExt.BinarySearch(list, 17, p));
            Assert.AreEqual(~5, ListExt.BinarySearch(list, 28, p));
            int i = ListExt.BinarySearch(list, 29, p);

            Assert.IsTrue(i == 5 || i == 6);
            Assert.AreEqual(~7, ListExt.BinarySearch(list, 30, p));
            Assert.AreEqual(7, ListExt.BinarySearch(list, 31, p));
            Assert.AreEqual(~8, ListExt.BinarySearch(list, 1000, p));

            // This tests another code path in G.ToComparison<T>()
            var            p2   = G.ToComparisonFunc <string>();
            IList <string> strs = new string[] { "1", "3", "5", "7", "9" };

            Assert.AreEqual(1, ListExt.BinarySearch2(strs, "3", p2));
            Assert.AreEqual(~4, ListExt.BinarySearch2(strs, "7b", p2));
        }
Ejemplo n.º 7
0
 public void CopyTo(V[] array, int arrayIndex)
 {
     ListExt.CopyTo(this, array, arrayIndex);
 }
Ejemplo n.º 8
0
 public int RemoveAll(Predicate <T> match)
 {
     return(ListExt.RemoveAll(this, match));
 }
Ejemplo n.º 9
0
 public void AddRange(IEnumerable <T> list)
 {
     ListExt.AddRange(this, list);
 }
Ejemplo n.º 10
0
 public void AddRange(IReadOnlyCollection <T> list)
 {
     ListExt.AddRange(this, list);
 }
Ejemplo n.º 11
0
 public int RemoveAll(Predicate <T> condition)
 {
     return(ListExt.RemoveAll(this, condition));
 }
Ejemplo n.º 12
0
 private void SetHelper(uint index, T value)
 {
     if (_listChanging != null)
     {
         CallListChanging(new ListChangeInfo <T>(NotifyCollectionChangedAction.Replace, (int)index, 0, ListExt.Single(value)));
     }
     ++_version;
     if (_root.IsFrozen)
     {
         AutoCreateOrCloneRoot();
     }
     _root.SetAt(index, value, _observer);
     CheckPoint();
 }
Ejemplo n.º 13
0
        public sealed override void Insert(int index, T item)
        {
            if ((uint)index > (uint)_count)
            {
                throw new IndexOutOfRangeException();
            }
            DetectSizeOverflow(1);
            AutoThrow();
            if (_listChanging != null)
            {
                CallListChanging(new ListChangeInfo <T>(NotifyCollectionChangedAction.Add, index, 1, ListExt.Single(item)));
            }

            try {
                _freezeMode = FrozenForConcurrency;
                if (_root == null || _root.IsFrozen)
                {
                    AutoCreateOrCloneRoot();
                }

                AListNode <int, T> splitLeft, splitRight;
                splitLeft = _root.Insert((uint)index, item, out splitRight, _observer);
                if (splitLeft != null)                 // redundant 'if' optimization
                {
                    AutoSplit(splitLeft, splitRight);
                }

                ++_version;
                Debug.Assert(_count != int.MaxValue);
                ++_count;
                CheckPoint();
            } finally {
                _freezeMode = NotFrozen;
            }
        }
Ejemplo n.º 14
0
 public void CopyTo(T[] array, int arrayIndex)
 {
     ListExt.CopyTo(_obj, array, arrayIndex);
 }