Skip to content

Commit

Permalink
Merge pull request #113 from greymistcube/refactor/dict-equality
Browse files Browse the repository at this point in the history
Streamline `Dictionary` equality
  • Loading branch information
greymistcube authored Oct 19, 2023
2 parents c13391b + bdbeae8 commit ce4a8af
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 44 deletions.
5 changes: 0 additions & 5 deletions Bencodex.Tests/Types/DictionaryTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,10 @@
using System.Collections.Immutable;
using System.Linq;
using System.Text;
using Bencodex.Misc;
using Bencodex.Types;
using SharpYaml.Tokens;
using Xunit;
using static Bencodex.Misc.ImmutableByteArrayExtensions;
using static Bencodex.Tests.TestUtils;
using IEquatableDict = System.IEquatable<System.Collections.Immutable.IImmutableDictionary<
Bencodex.Types.IKey,
Bencodex.Types.IValue>>;

namespace Bencodex.Tests.Types
{
Expand Down
54 changes: 15 additions & 39 deletions Bencodex/Types/Dictionary.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ namespace Bencodex.Types
public sealed class Dictionary :
IValue,
IEquatable<Dictionary>,
IEquatable<IImmutableDictionary<IKey, IValue>>,
IImmutableDictionary<IKey, IValue>
{
/// <summary>
Expand Down Expand Up @@ -1697,54 +1696,31 @@ public T GetValue<T>(byte[] name)
return (T)this[name];
}

/// <inheritdoc cref="object.Equals(object)"/>
public override bool Equals(object obj) =>
obj switch
{
null => false,
Dictionary d => Equals(d),
_ => false,
};
public override bool Equals(object obj) => obj is Dictionary d && Equals(d);

/// <inheritdoc cref="IEquatable{T}.Equals(T)"/>
public bool Equals(Dictionary other) =>
Fingerprint.Equals(other.Fingerprint);
public bool Equals(IValue other) => other is Dictionary d && Equals(d);

/// <inheritdoc cref="IEquatable{T}.Equals(T)"/>
bool IEquatable<IImmutableDictionary<IKey, IValue>>.Equals(
IImmutableDictionary<IKey, IValue> other
)
public bool Equals(Dictionary other)
{
if (_dict.Count != other.Count)
{
return false;
}
else if (other is Dictionary od)
if (Count == other.Count)
{
return od.Fingerprint.Equals(Fingerprint);
}

foreach (KeyValuePair<IKey, IValue> kv in _dict)
{
if (!other.TryGetValue(kv.Key, out IValue v))
foreach (KeyValuePair<IKey, IValue> kv in _dict)
{
return false;
if (!other.TryGetValue(kv.Key, out IValue v) ||
!kv.Value.Equals(v))
{
return false;
}
}

if (!kv.Value.Equals(v))
{
return false;
}
return true;
}
else
{
return false;
}

return true;
}

/// <inheritdoc cref="IEquatable{T}.Equals(T)"/>
bool IEquatable<IValue>.Equals(IValue other) =>
other is Dictionary o &&
((IEquatable<IImmutableDictionary<IKey, IValue>>)this).Equals(o);

/// <inheritdoc cref="object.GetHashCode()"/>
public override int GetHashCode()
=> unchecked(_dict.Aggregate(GetType().GetHashCode(), (accum, next)
Expand Down
3 changes: 3 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ To be released.
to be more consistent. [[#106], [#110]]
- Removed `ByteArrayComparer` and `FingerprintComparer`. [[#111]]
- Removed `IEquatable<IImmutableList<IValue>>` from `List`. [[#104], [#112]]
- Removed `IEquatable<IImmutableDictionary<IKey, IValue>>` from `Dictionary`.
[[#104], [#113]]

[#104]: https://github.com/planetarium/bencodex.net/issues/104
[#106]: https://github.com/planetarium/bencodex.net/issues/106
Expand All @@ -31,6 +33,7 @@ To be released.
[#110]: https://github.com/planetarium/bencodex.net/pull/110
[#111]: https://github.com/planetarium/bencodex.net/pull/111
[#112]: https://github.com/planetarium/bencodex.net/pull/112
[#113]: https://github.com/planetarium/bencodex.net/pull/113


Version 0.14.0
Expand Down

0 comments on commit ce4a8af

Please sign in to comment.