From da4cef68e92efd0bb5303524880856148f866697 Mon Sep 17 00:00:00 2001 From: test Date: Tue, 5 Jan 2021 21:10:41 +0900 Subject: [PATCH] =?utf8?q?GapBuffer=E3=81=8C=E6=9B=B4=E6=96=B0=E3=81=95?= =?utf8?q?=E3=82=8C=E3=81=A6=E3=81=84=E3=81=9F?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit --- Core/GapBuffer.cs | 37 ++++++++++++++++--------------------- Core/StringBuffer.cs | 10 +++++----- Core/UndoCommands.cs | 8 ++++---- 3 files changed, 25 insertions(+), 30 deletions(-) diff --git a/Core/GapBuffer.cs b/Core/GapBuffer.cs index 6c644ae..e40b7c1 100644 --- a/Core/GapBuffer.cs +++ b/Core/GapBuffer.cs @@ -246,11 +246,10 @@ namespace Slusser.Collections.Generic /// The collection whose elements should be inserted into the . /// The collection itself cannot be null, but it can contain elements that are null, if /// type is a reference type. - /// The count of collction.count is -1 when this method is slowly /// is a null reference. - public void AddRange(IEnumerable collection,int count = -1) + public void AddRange(IEnumerable collection) { - InsertRange(Count, collection,count); + InsertRange(Count, collection); } @@ -501,14 +500,13 @@ namespace Slusser.Collections.Generic /// The collection whose elements should be inserted into the . /// The collection itself cannot be null, but it can contain elements that are null, if /// type is a reference type. - /// The count of collction.count is -1 when this method is slowly /// is a null reference. /// /// is less than 0. /// -or- /// is greater than . /// - public void InsertRange(int index, IEnumerable collection, int preallocatCount = -1) + public void InsertRange(int index, IEnumerable 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 col = collection as ICollection; + if (col != null) { - // Add the items to the buffer one-at-a-time :( - using (IEnumerator 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 enumerator = collection.GetEnumerator()) { while (enumerator.MoveNext()) { - this._buffer[index] = enumerator.Current; + Insert(index, enumerator.Current); index++; - collectionLength++; } } - this._gapStart += collectionLength; - } this._version++; } diff --git a/Core/StringBuffer.cs b/Core/StringBuffer.cs index d96cc5e..b1a03c9 100644 --- a/Core/StringBuffer.cs +++ b/Core/StringBuffer.cs @@ -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; diff --git a/Core/UndoCommands.cs b/Core/UndoCommands.cs index 571b7bc..da58cfd 100644 --- a/Core/UndoCommands.cs +++ b/Core/UndoCommands.cs @@ -26,7 +26,7 @@ namespace FooEditEngine this.Buffer = buf; this.ReplacementRange = new TextRange(start,str.Length); this.replacement = new GapBuffer(); - this.replacement.AddRange(Util.GetEnumrator(str),str.Length); + this.replacement.AddRange(Util.GetEnumrator(str)); this.ReplacedRange = new TextRange(start,length); this.replaced = new GapBuffer(); 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; -- 2.11.0