Added new Range Manipulation APIs for Collection<T> which propogate up to ObservableC...
authorAndrew Hoefling <andrew@hoeflingsoftware.com>
Tue, 5 Mar 2019 01:16:16 +0000 (20:16 -0500)
committerSantiago Fernandez Madero <safern@microsoft.com>
Fri, 8 Mar 2019 19:20:58 +0000 (11:20 -0800)
src/System.Private.CoreLib/shared/System/Collections/ObjectModel/Collection.cs

index c96a157..f0814e9 100644 (file)
@@ -69,6 +69,8 @@ namespace System.Collections.ObjectModel
             InsertItem(index, item);
         }
 
+        public void AddRange(IEnumerable<T> collection) => InsertItemsRange(items.Count > 0 ? items.Count : 0, collection);
+
         public void Clear()
         {
             if (items.IsReadOnly)
@@ -114,6 +116,8 @@ namespace System.Collections.ObjectModel
             InsertItem(index, item);
         }
 
+        public void InsertRange(int index, IEnumerable<T> collection) => InsertItemsRange(index, collection);
+
         public bool Remove(T item)
         {
             if (items.IsReadOnly)
@@ -127,6 +131,14 @@ namespace System.Collections.ObjectModel
             return true;
         }
 
+        public void RemoveRange(int index, int count) => RemoveItemsRange(index, count);
+
+        public void ReplaceRange(int index, int count, IEnumerable<T> collection)
+        {
+            RemoveItemsRange(index, count);
+            InsertItemsRange(index, collection);
+        }
+
         public void RemoveAt(int index)
         {
             if (items.IsReadOnly)
@@ -162,6 +174,47 @@ namespace System.Collections.ObjectModel
             items[index] = item;
         }
 
+        protected virtual void InsertItemsRange(int index, IEnumerable<T> collection)
+        {
+            if (collection == null)
+            {
+                ThrowHelper.ThrowArgumentNullException(ExceptionArgument.list);
+            }
+
+            if (items.IsReadOnly)
+            {
+                ThrowHelper.ThrowNotSupportedException(ExceptionResource.NotSupported_ReadOnlyCollection);
+            }
+
+            if (index < 0 || index > items.Count)
+            {
+                ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.index, ExceptionResource.ArgumentOutOfRange_ListInsert);
+            }
+
+            int i = 0;
+            foreach (var item in collection)
+            {
+                Insert(index + i, item);
+                i++;
+            }
+        }
+
+        protected virtual void RemoveItemsRange(int index, int count)
+        {
+            if (items.IsReadOnly)
+            {
+                ThrowHelper.ThrowNotSupportedException(ExceptionResource.NotSupported_ReadOnlyCollection);
+            }
+
+            if (index < 0 || index > items.Count)
+            {
+                ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.index, ExceptionResource.ArgumentOutOfRange_ListInsert);
+            }
+
+            for (int i = index; i < (index + count); i++)
+                RemoveAt(index);
+        }
+
         bool ICollection<T>.IsReadOnly
         {
             get