-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Harry
committed
Jun 24, 2022
1 parent
dcaa5bb
commit 60c2a04
Showing
8 changed files
with
52 additions
and
64 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
|
||
namespace Neo.BlockchainToolkit | ||
{ | ||
public class MemorySequenceComparer : IEqualityComparer<ReadOnlyMemory<byte>>, IComparer<ReadOnlyMemory<byte>> | ||
{ | ||
public static MemorySequenceComparer Default { get; } = new MemorySequenceComparer(false); | ||
public static MemorySequenceComparer Reverse { get; } = new MemorySequenceComparer(true); | ||
|
||
private readonly bool reverse; | ||
|
||
private MemorySequenceComparer(bool reverse = false) | ||
{ | ||
this.reverse = reverse; | ||
} | ||
|
||
public bool Equals(ReadOnlyMemory<byte> x, ReadOnlyMemory<byte> y) => Equals(x.Span, y.Span); | ||
|
||
public int GetHashCode(ReadOnlyMemory<byte> obj) => GetHashCode(obj.Span); | ||
|
||
public int Compare(ReadOnlyMemory<byte> x, ReadOnlyMemory<byte> y) | ||
{ | ||
var compare = x.Span.SequenceCompareTo(y.Span); | ||
return reverse ? -compare : compare; | ||
} | ||
|
||
public static bool Equals(ReadOnlySpan<byte> x, ReadOnlySpan<byte> y) => x.SequenceEqual(y); | ||
|
||
public static int GetHashCode(ReadOnlySpan<byte> span) | ||
{ | ||
HashCode hash = default; | ||
hash.AddBytes(span); | ||
return hash.ToHashCode(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters