From 272454b85c70194a005067051257fe13e2dab68a Mon Sep 17 00:00:00 2001 From: bleis-tift Date: Tue, 8 Oct 2013 17:53:58 +0900 Subject: [PATCH] =?UTF-8?q?object=E3=81=AE=E3=83=A1=E3=82=BD=E3=83=83?= =?UTF-8?q?=E3=83=89=E3=82=92=E3=82=AA=E3=83=BC=E3=83=90=E3=83=BC=E3=83=A9?= =?UTF-8?q?=E3=82=A4=E3=83=89=20refs=20#2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- SeqExt.Tests/Mutable/VarArrayTest.cs | 25 +++++++++++ SeqExt/Mutable/VarArray.cs | 62 +++++++++++++++++++++++++++- 2 files changed, 86 insertions(+), 1 deletion(-) diff --git a/SeqExt.Tests/Mutable/VarArrayTest.cs b/SeqExt.Tests/Mutable/VarArrayTest.cs index 2144c5b..bc5d968 100644 --- a/SeqExt.Tests/Mutable/VarArrayTest.cs +++ b/SeqExt.Tests/Mutable/VarArrayTest.cs @@ -217,5 +217,30 @@ public void 要素を反転できる(int[] xs, int[] expected) array.ReverseThis(); Assert.That(array.ToArray(), Is.EqualTo(expected)); } + + [TestCase(new string[0], "VarArray.Empty()")] + [TestCase(new[] { "" }, "VarArray()")] + [TestCase(new[] { "1", "", "2" }, "VarArray(1, , 2)")] + public void 文字列化できる(string[] xs, string expected) + { + var array = VarArray.Create(xs); + Assert.That(array.ToString(), Is.EqualTo(expected)); + } + + [TestCase(new int[0], new int[0], true)] + [TestCase(new int[0], new[] { 1 }, false)] + [TestCase(new[] { 1 }, new int[0], false)] + [TestCase(new[] { 1 }, new[] { 1 }, true)] + [TestCase(new[] { 1 }, new[] { 2 }, false)] + [TestCase(new[] { 1 }, new[] { 1, 0 }, false)] + [TestCase(new[] { 1, 2, 3 }, new[] { 1, 2, 3 }, true)] + [TestCase(new[] { 1, 2, 3 }, new[] { 1, 2 }, false)] + [TestCase(new[] { 1, 2, 3 }, new[] { 1, 2, 3, 0 }, false)] + public void VarArrayどうしの比較ができる(int[] xs, int[] ys, bool expected) + { + var array1 = VarArray.Create(xs); + var array2 = VarArray.Create(ys); + Assert.That(array1.Equals(array2), Is.EqualTo(expected)); + } } } diff --git a/SeqExt/Mutable/VarArray.cs b/SeqExt/Mutable/VarArray.cs index 6384f42..bdbfa86 100644 --- a/SeqExt/Mutable/VarArray.cs +++ b/SeqExt/Mutable/VarArray.cs @@ -98,7 +98,7 @@ public static VarArray ToVarArray(this Seq self) /// /// 可変長配列です。 /// - public sealed class VarArray : SeqBase, RandomAccessSeq + public sealed class VarArray : SeqBase, RandomAccessSeq, IEquatable> { readonly System.Collections.Generic.List value; @@ -228,5 +228,65 @@ public void ReverseThis() { this.value.Reverse(); } + + /// + /// 現在のオブジェクトが、同じ型の別のオブジェクトと等しいかどうかを判定します。 + /// + /// このオブジェクトと比較する可変長配列 + /// 現在のオブジェクトがotherで指定されたオブジェクトと等しい場合はtrue、それ以外の場合はfalse + public bool Equals(VarArray other) + { + if (this.Size != other.Size) + return false; + + for (int i = 0; i < this.Size; i++) + { + if (object.Equals(this[i], other[i]) == false) + return false; + } + return true; + } + + /// 使用しません。 + [EditorBrowsable(EditorBrowsableState.Never)] + public override bool Equals(object obj) + { + var other = obj as VarArray; + if (other == null) + return false; + return this.Equals(other); + } + + /// 使用しません。 + [EditorBrowsable(EditorBrowsableState.Never)] + public override int GetHashCode() + { + var hash = 31; + foreach (var x in this) + hash ^= x == null ? 0 : x.GetHashCode(); + return hash; + } + + /// + /// このオブジェクトの文字列表現を取得します。 + /// + public override string ToString() + { + if (this.Size == 0) + return "VarArray.Empty()"; + var buf = new System.Text.StringBuilder("VarArray("); + foreach (var x in this) + buf.Append(x.ToStr()).Append(", "); + buf.Remove(buf.Length - 2, 2); + return buf.Append(")").ToString(); + } + + /// 使用しません。 + [EditorBrowsable(EditorBrowsableState.Never)] + public static new bool Equals(object a, object b) { return object.Equals(a, b); } + + /// 使用しません。 + [EditorBrowsable(EditorBrowsableState.Never)] + public static new bool ReferenceEquals(object a, object b) { return object.ReferenceEquals(a, b); } } }