Skip to content

Commit

Permalink
Merge pull request #111 from greymistcube/refactor/remove-comparers
Browse files Browse the repository at this point in the history
🧹 Remove unnecessary comparers
  • Loading branch information
greymistcube authored Oct 19, 2023
2 parents f7f265d + ed81201 commit 4b015ce
Show file tree
Hide file tree
Showing 7 changed files with 18 additions and 155 deletions.
31 changes: 0 additions & 31 deletions Bencodex.Tests/Misc/ByteArrayComparerTest.cs

This file was deleted.

48 changes: 0 additions & 48 deletions Bencodex.Tests/Misc/FingerprintComparerTest.cs

This file was deleted.

39 changes: 0 additions & 39 deletions Bencodex/Misc/ByteArrayComparer.cs

This file was deleted.

26 changes: 0 additions & 26 deletions Bencodex/Misc/FingerprintComparer.cs

This file was deleted.

8 changes: 2 additions & 6 deletions Bencodex/Misc/KeyComparer.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
using System;
using System.Collections.Generic;
using System.Text;
using Bencodex.Types;

namespace Bencodex.Misc
Expand All @@ -16,8 +14,6 @@ public sealed class KeyComparer : IComparer<IKey>
/// </summary>
public static readonly KeyComparer Instance = new KeyComparer();

private static readonly ByteArrayComparer _binaryComparer = default;

private KeyComparer()
{
}
Expand All @@ -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);
Expand Down
19 changes: 14 additions & 5 deletions Bencodex/Types/Binary.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,6 @@ namespace Bencodex.Types
IComparable,
IEnumerable<byte>
{
private static readonly ByteArrayComparer ByteArrayComparer =
default(ByteArrayComparer);

private readonly ImmutableArray<byte> _value;
private readonly int?[] _hashCode;
private readonly ImmutableArray<byte>?[] _digest;
Expand Down Expand Up @@ -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)
{
Expand Down
2 changes: 2 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,15 @@ 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
[#107]: https://github.com/planetarium/bencodex.net/pull/107
[#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
Expand Down

0 comments on commit 4b015ce

Please sign in to comment.