OSDN Git Service

GapBufferが更新されていた
authortest <test@yahoo.co.jp>
Tue, 5 Jan 2021 12:10:41 +0000 (21:10 +0900)
committertest <test@yahoo.co.jp>
Tue, 5 Jan 2021 12:10:41 +0000 (21:10 +0900)
Core/GapBuffer.cs
Core/StringBuffer.cs
Core/UndoCommands.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++;
         }
index d96cc5e..b1a03c9 100644 (file)
@@ -51,7 +51,7 @@ namespace FooEditEngine
         public StringBuffer(StringBuffer buffer)
             : this()
         {
-            buf.AddRange(buffer.buf, buffer.Length);
+            buf.AddRange(buffer.buf);
         }
 
 
@@ -105,7 +105,7 @@ namespace FooEditEngine
             {
                 if (length > 0)
                     this.buf.RemoveRange(index, length);
-                this.buf.InsertRange(index, chars, count);
+                this.buf.InsertRange(index, chars);
             }
             this.Update(this, new DocumentUpdateEventArgs(UpdateType.Replace, index, length, count));
         }
@@ -128,7 +128,7 @@ namespace FooEditEngine
                 using (await this.rwlock.WriterLockAsync())
                 {
                     //str.lengthは事前に確保しておくために使用するので影響はない
-                    this.buf.InsertRange(index, internal_str, str.Length);
+                    this.buf.InsertRange(index, internal_str);
                 }
 
                 if (tokenSource != null)
@@ -186,7 +186,7 @@ namespace FooEditEngine
                     //空行は削除する必要はない
                     if (lineHeadIndex < this.buf.Count)
                         this.buf.RemoveRange(lineHeadIndex, lineLength);
-                    this.buf.InsertRange(lineHeadIndex, output, output.Length);
+                    this.buf.InsertRange(lineHeadIndex, output);
                 }
 
                 this.Update(this, new DocumentUpdateEventArgs(UpdateType.Replace, lineHeadIndex, lineLength, output.Length, i));
@@ -207,7 +207,7 @@ namespace FooEditEngine
                     using (this.rwlock.WriterLock())
                     {
                         this.buf.RemoveRange(right, target.Length);
-                        this.buf.InsertRange(right, pattern_chars, pattern.Length);
+                        this.buf.InsertRange(right, pattern_chars);
                     }
                     left = right + pattern.Length;
                     newLineLength += pattern.Length - target.Length;
index 571b7bc..da58cfd 100644 (file)
@@ -26,7 +26,7 @@ namespace FooEditEngine
             this.Buffer = buf;
             this.ReplacementRange = new TextRange(start,str.Length);
             this.replacement = new GapBuffer<char>();
-            this.replacement.AddRange(Util.GetEnumrator(str),str.Length);
+            this.replacement.AddRange(Util.GetEnumrator(str));
             this.ReplacedRange = new TextRange(start,length);
             this.replaced = new GapBuffer<char>();
             this.replaced.AddRange(this.Buffer.GetEnumerator(start, length));
@@ -57,15 +57,15 @@ namespace FooEditEngine
                 int bufferIndex = cmd.ReplacedRange.Index - this.ReplacementRange.Index;
                 if(bufferIndex < this.replacement.Count)
                     this.replacement.RemoveRange(bufferIndex, cmd.ReplacedRange.Length);
-                this.replacement.InsertRange(bufferIndex, cmd.replacement, cmd.replacement.Count);
+                this.replacement.InsertRange(bufferIndex, cmd.replacement);
                 return true;
             }
 
             if (this.ReplacedRange.Index + this.ReplacementRange.Length == cmd.ReplacedRange.Index && 
                 this.ReplacedRange.Index == this.ReplacementRange.Index)
             {
-                this.replaced.AddRange(cmd.replaced,cmd.replaced.Count);
-                this.replacement.AddRange(cmd.replacement,cmd.replacement.Count);
+                this.replaced.AddRange(cmd.replaced);
+                this.replacement.AddRange(cmd.replacement);
                 this.ReplacedRange.Length += cmd.ReplacedRange.Length;
                 this.ReplacementRange.Length += cmd.ReplacementRange.Length;
                 return true;