OSDN Git Service

GapBufferが更新されていた
[fooeditengine/FooEditEngine.git] / Core / GapBuffer.cs
index 6c644ae..e40b7c1 100644 (file)
@@ -246,11 +246,10 @@ namespace Slusser.Collections.Generic
                /// <param name="collection">The collection whose elements should be inserted into the <see cref="GapBuffer{T}"/>. 
                /// The collection itself cannot be null, but it can contain elements that are null, if 
                /// type <typeparamref name="T"/> is a reference type.</param>
-        /// <param name="count">The count of collction.count is -1 when this method is slowly</param>
         /// <exception cref="ArgumentNullException"><paramref name="collection"/> is a null reference.</exception>
-               public void AddRange(IEnumerable<T> collection,int count = -1)
+               public void AddRange(IEnumerable<T> collection)
                {
-                       InsertRange(Count, collection,count);
+                       InsertRange(Count, collection);
                }
 
 
@@ -501,14 +500,13 @@ namespace Slusser.Collections.Generic
                /// <param name="collection">The collection whose elements should be inserted into the <see cref="GapBuffer{T}"/>. 
                /// The collection itself cannot be null, but it can contain elements that are null, if 
                /// type <typeparamref name="T"/> is a reference type.</param>
-        /// <param name="preallocatCount">The count of collction.count is -1 when this method is slowly</param>
                /// <exception cref="ArgumentNullException"><paramref name="collection"/> is a null reference.</exception>
                /// <exception cref="ArgumentOutOfRangeException">
                /// <paramref name="index"/> is less than 0.
                /// <para>-or-</para>
                /// <paramref name="index"/> is greater than <see cref="Count"/>.
                /// </exception>
-        public void InsertRange(int index, IEnumerable<T> collection, int preallocatCount = -1)
+        public void InsertRange(int index, IEnumerable<T> collection)
         {
             if (collection == null)
                 throw new ArgumentNullException("collection");
@@ -516,34 +514,31 @@ namespace Slusser.Collections.Generic
             if (index < 0 || index > Count)
                 throw new ArgumentOutOfRangeException("index", "");
 
-            if (preallocatCount == -1)
+            ICollection<T> col = collection as ICollection<T>;
+            if (col != null)
             {
-                // Add the items to the buffer one-at-a-time :(
-                using (IEnumerator<T> enumerator = collection.GetEnumerator())
+                int count = col.Count;
+                if (count > 0)
                 {
-                    while (enumerator.MoveNext())
-                    {
-                        Insert(index, enumerator.Current);
-                        index++;
-                    }
+                    PlaceGapStart(index);
+                    EnsureGapCapacity(count);
+
+                    // Copy the collection directly into the buffer
+                    col.CopyTo(this._buffer, this._gapStart);
+                    this._gapStart += count;
                 }
             }
-            else if(preallocatCount > 0)
+            else
             {
-                PlaceGapStart(index);
-                EnsureGapCapacity(preallocatCount);
-                int collectionLength = 0;
+                // Add the items to the buffer one-at-a-time :(
                 using (IEnumerator<T> enumerator = collection.GetEnumerator())
                 {
                     while (enumerator.MoveNext())
                     {
-                        this._buffer[index] = enumerator.Current;
+                        Insert(index, enumerator.Current);
                         index++;
-                        collectionLength++;
                     }
                 }
-                this._gapStart += collectionLength;
-
             }
             this._version++;
         }