From d0d60fa7ee28f3e33d5604af93f0b008ecd8c589 Mon Sep 17 00:00:00 2001 From: Say Cheong Date: Thu, 19 Oct 2023 11:18:24 +0900 Subject: [PATCH 1/3] Refactor KeyComparer --- Bencodex/Misc/KeyComparer.cs | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/Bencodex/Misc/KeyComparer.cs b/Bencodex/Misc/KeyComparer.cs index 233cef5..b7c3504 100644 --- a/Bencodex/Misc/KeyComparer.cs +++ b/Bencodex/Misc/KeyComparer.cs @@ -1,6 +1,4 @@ -using System; using System.Collections.Generic; -using System.Text; using Bencodex.Types; namespace Bencodex.Misc @@ -16,8 +14,6 @@ public sealed class KeyComparer : IComparer /// public static readonly KeyComparer Instance = new KeyComparer(); - private static readonly ByteArrayComparer _binaryComparer = default; - private KeyComparer() { } @@ -27,11 +23,11 @@ public int Compare(IKey x, IKey y) { if (x is Binary xb && y is Binary yb) { - return _binaryComparer.Compare(xb.ByteArray, yb.ByteArray); + return xb.CompareTo(yb); } else if (x is Text xt && y is Text yt) { - return string.CompareOrdinal(xt.Value, yt.Value); + return xt.CompareTo(yt); } return (x.Kind == ValueKind.Text).CompareTo(y.Kind == ValueKind.Text); From 05c9acfba97cd64bae6e5eed3887edbfa43ce628 Mon Sep 17 00:00:00 2001 From: Say Cheong Date: Thu, 19 Oct 2023 11:53:14 +0900 Subject: [PATCH 2/3] Removed unnecessary comparers --- Bencodex.Tests/Misc/ByteArrayComparerTest.cs | 31 ------------ .../Misc/FingerprintComparerTest.cs | 48 ------------------- Bencodex/Misc/ByteArrayComparer.cs | 39 --------------- Bencodex/Misc/FingerprintComparer.cs | 26 ---------- Bencodex/Types/Binary.cs | 19 ++++++-- 5 files changed, 14 insertions(+), 149 deletions(-) delete mode 100644 Bencodex.Tests/Misc/ByteArrayComparerTest.cs delete mode 100644 Bencodex.Tests/Misc/FingerprintComparerTest.cs delete mode 100644 Bencodex/Misc/ByteArrayComparer.cs delete mode 100644 Bencodex/Misc/FingerprintComparer.cs diff --git a/Bencodex.Tests/Misc/ByteArrayComparerTest.cs b/Bencodex.Tests/Misc/ByteArrayComparerTest.cs deleted file mode 100644 index 87d7144..0000000 --- a/Bencodex.Tests/Misc/ByteArrayComparerTest.cs +++ /dev/null @@ -1,31 +0,0 @@ -using System.Collections.Generic; -using System.Collections.Immutable; -using Bencodex.Misc; -using Xunit; - -namespace Bencodex.Tests.Misc -{ - public class ByteArrayComparerTest - { - [Fact] - public void CompareImmutableArrays() - { - var comparer = default(ByteArrayComparer); - ComparerTestUtils.TestComparison( - comparer, - new List>() - { - ImmutableArray.Empty, - new byte[] { 0x00 }.ToImmutableArray(), - new byte[] { 0x00, 0x00 }.ToImmutableArray(), - new byte[] { 0x00, 0x80 }.ToImmutableArray(), - new byte[] { 0x00, 0xff }.ToImmutableArray(), - new byte[] { 0x01 }.ToImmutableArray(), - new byte[] { 0x01, 0x01 }.ToImmutableArray(), - new byte[] { 0x01, 0x80 }.ToImmutableArray(), - new byte[] { 0x01, 0xff }.ToImmutableArray(), - } - ); - } - } -} diff --git a/Bencodex.Tests/Misc/FingerprintComparerTest.cs b/Bencodex.Tests/Misc/FingerprintComparerTest.cs deleted file mode 100644 index 0ef2a45..0000000 --- a/Bencodex.Tests/Misc/FingerprintComparerTest.cs +++ /dev/null @@ -1,48 +0,0 @@ -using System.Collections.Generic; -using System.Text; -using Bencodex.Misc; -using Bencodex.Types; -using Xunit; -using static Bencodex.Misc.ImmutableByteArrayExtensions; -using F = Bencodex.Types.Fingerprint; - -namespace Bencodex.Tests.Misc -{ - public class FingerprintComparerTest - { - [Fact] - public void Compare() - { - var n = new F(ValueKind.Null, 1); - var f = new F(ValueKind.Boolean, 1, new byte[] { 0 }); - var t = new F(ValueKind.Boolean, 1, new byte[] { 1 }); - var i0 = new F(ValueKind.Integer, 3, new byte[] { 0 }); - var i45 = new F(ValueKind.Integer, 4, new byte[] { 45 }); - var iM123 = new F(ValueKind.Integer, 6, new byte[] { 0b10000101 }); - var b = new F(ValueKind.Binary, 2); - var bHello = new F(ValueKind.Binary, 7, Encoding.ASCII.GetBytes("hello")); - var b445 = new F(ValueKind.Binary, 449, ParseHex("cd36b370758a259b34845084a6cc38473cb95e27")); - var u = new F(ValueKind.Text, 3); - var uNihao = new F(ValueKind.Text, 9, new byte[] { 0xe4, 0xbd, 0xa0, 0xe5, 0xa5, 0xbd }); - var u42 = new F(ValueKind.Text, 46, ParseHex("e72dcfd0ae50a80aaa8c1a78b27e2e11bef66488")); - var l = new F(ValueKind.List, 2); - var l1 = new F(ValueKind.List, 3, ParseHex("ae7fca60943c2ef2f6cf5420477da41acf29b01d")); - var l2 = new F(ValueKind.List, 18, ParseHex("22852139f287a01cdb803fd86ed70e4c4d121254")); - var lNest = new F(ValueKind.List, 26, ParseHex("24caa983a5225522ca798be3b31a1abecdb36fe5")); - var d = new F(ValueKind.Dictionary, 2); - var d1 = new F(ValueKind.Dictionary, 14, ParseHex("9312bb9fe32ec11b4e6478f557ea821b0c44523d")); - var unordered = new List - { - lNest, l2, u, n, bHello, i0, l1, uNihao, - d1, f, iM123, d, b445, l, i45, t, u42, b, - }; - unordered.Sort(new FingerprintComparer()); - Fingerprint[] ordered = - { - n, f, t, i0, i45, iM123, b, bHello, b445, - u, uNihao, u42, l, l1, l2, lNest, d, d1, - }; - Assert.Equal(ordered, unordered); - } - } -} diff --git a/Bencodex/Misc/ByteArrayComparer.cs b/Bencodex/Misc/ByteArrayComparer.cs deleted file mode 100644 index 4bfb28c..0000000 --- a/Bencodex/Misc/ByteArrayComparer.cs +++ /dev/null @@ -1,39 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Collections.Immutable; -using System.Linq; - -namespace Bencodex.Misc -{ - /// - /// Similar to but for s instead of Unicode - /// s. - /// - public struct ByteArrayComparer : IComparer> - { - private static readonly ByteArrayComparer> _immutableArrayComparer = - new ByteArrayComparer>(); - - public int Compare(ImmutableArray x, ImmutableArray y) => - _immutableArrayComparer.Compare(x, y); - } - - internal class ByteArrayComparer : IComparer - where T : IReadOnlyList - { - public int Compare(T x, T y) - { - int shortestLength = Math.Min(x.Count, y.Count); - for (int i = 0; i < shortestLength; i++) - { - int c = x[i].CompareTo(y[i]); - if (c != 0) - { - return c; - } - } - - return x.Count.CompareTo(y.Count); - } - } -} diff --git a/Bencodex/Misc/FingerprintComparer.cs b/Bencodex/Misc/FingerprintComparer.cs deleted file mode 100644 index 5e6fc73..0000000 --- a/Bencodex/Misc/FingerprintComparer.cs +++ /dev/null @@ -1,26 +0,0 @@ -using System.Collections.Generic; -using Bencodex.Types; - -namespace Bencodex.Misc -{ - /// - /// Compares two values. There is no meaning for the order, - /// but it just purposes to make the order deterministic. - /// - public sealed class FingerprintComparer : IComparer - { - private static readonly ByteArrayComparer _digestComparer = default; - - /// - public int Compare(Fingerprint x, Fingerprint y) - { - if (x.Kind != y.Kind) - { - return x.Kind < y.Kind ? -1 : 1; - } - - int encLenCmp = x.EncodingLength.CompareTo(y.EncodingLength); - return encLenCmp != 0 ? encLenCmp : _digestComparer.Compare(x.Digest, y.Digest); - } - } -} diff --git a/Bencodex/Types/Binary.cs b/Bencodex/Types/Binary.cs index fb4dbb2..4f0d933 100644 --- a/Bencodex/Types/Binary.cs +++ b/Bencodex/Types/Binary.cs @@ -19,9 +19,6 @@ namespace Bencodex.Types IComparable, IEnumerable { - private static readonly ByteArrayComparer ByteArrayComparer = - default(ByteArrayComparer); - private readonly ImmutableArray _value; private readonly int?[] _hashCode; private readonly ImmutableArray?[] _digest; @@ -256,8 +253,20 @@ public override int GetHashCode() return hash; } - public int CompareTo(Binary other) => - ByteArrayComparer.Compare(ByteArray, other.ByteArray); + public int CompareTo(Binary other) + { + int minLength = Math.Min(ByteArray.Length, other.ByteArray.Length); + for (int i = 0; i < minLength; i++) + { + int c = ByteArray[i].CompareTo(other.ByteArray[i]); + if (c != 0) + { + return c; + } + } + + return ByteArray.Length.CompareTo(other.ByteArray.Length); + } public int CompareTo(object? obj) { From ed81201c103a5a910cd810fd712166e504e37775 Mon Sep 17 00:00:00 2001 From: Say Cheong Date: Thu, 19 Oct 2023 11:56:45 +0900 Subject: [PATCH 3/3] Changelog --- CHANGES.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/CHANGES.md b/CHANGES.md index aeec158..70677d9 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -20,6 +20,7 @@ To be released. `ByteArrayComparer`. [[#110]] - Changed the behaviors of `Binary.Equals()` and `Binary.CompareTo()` to be more consistent. [[#106], [#110]] + - Removed `ByteArrayComparer` and `FingerprintComparer`. [[#111]] [#104]: https://github.com/planetarium/bencodex.net/issues/104 [#106]: https://github.com/planetarium/bencodex.net/issues/106 @@ -27,6 +28,7 @@ To be released. [#108]: https://github.com/planetarium/bencodex.net/pull/108 [#109]: https://github.com/planetarium/bencodex.net/pull/109 [#110]: https://github.com/planetarium/bencodex.net/pull/110 +[#111]: https://github.com/planetarium/bencodex.net/pull/111 Version 0.14.0