Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .semrelease/this_release
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# This file has been cleaned up post-releasing version 0.0.2.
# Generate its content quickly using the following command:
# git log origin..HEAD | grep "^\s" > .semrelease/this_release
#!VERSION=0.0.3
- impr: Expose shared static Factory on XxHash128Hasher, XxHash3Hasher and Crc32Hasher for cached reuse via Signum.
25 changes: 25 additions & 0 deletions Ddth.Signum.Tests/HasherTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,31 @@ public static IEnumerable<object[]> Hashers()
yield return new object[] { (Func<IHasher>)(() => new Crc32Hasher()), 4 };
}

[Fact]
public void Factory_CreatesNewInstanceOfExpectedType()
{
Assert.IsType<XxHash128Hasher>(XxHash128Hasher.Factory());
Assert.IsType<XxHash3Hasher>(XxHash3Hasher.Factory());
Assert.IsType<Crc32Hasher>(Crc32Hasher.Factory());

// Each invocation returns a distinct instance.
Assert.NotSame(XxHash3Hasher.Factory(), XxHash3Hasher.Factory());
}

[Fact]
public void Factory_MatchesEquivalentInlineFactory()
{
Assert.Equal(
Signum.ChecksumHex("hello", () => new XxHash3Hasher()),
Signum.ChecksumHex("hello", XxHash3Hasher.Factory));
}

[Fact]
public void XxHash128Factory_MatchesDefault()
{
Assert.Equal(Signum.ChecksumHex("hello"), Signum.ChecksumHex("hello", XxHash128Hasher.Factory));
}

[Theory]
[MemberData(nameof(Hashers))]
public void Hasher_ProducesExpectedWidth(Func<IHasher> factory, int expectedWidth)
Expand Down
3 changes: 3 additions & 0 deletions Ddth.Signum/Crc32Hasher.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ namespace Ddth.Signum;
/// </summary>
public sealed class Crc32Hasher : IHasher
{
/// <summary>Shared factory that creates a new <see cref="Crc32Hasher"/> instance.</summary>
public static readonly Func<IHasher> Factory = () => new Crc32Hasher();

private readonly Crc32 _inner = new();

/// <inheritdoc/>
Expand Down
3 changes: 2 additions & 1 deletion Ddth.Signum/Signum.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ namespace Ddth.Signum;
/// </remarks>
public static class Signum
{
private static readonly Fingerprinter Default = new();
private static readonly Fingerprinter Default =
new(new FingerprintOptions { HasherFactory = XxHash128Hasher.Factory });

private static readonly ConcurrentDictionary<Func<IHasher>, Fingerprinter> Cache = new();

Expand Down
3 changes: 3 additions & 0 deletions Ddth.Signum/XxHash128Hasher.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ namespace Ddth.Signum;
/// </summary>
public sealed class XxHash128Hasher : IHasher
{
/// <summary>Shared factory that creates a new <see cref="XxHash128Hasher"/> instance.</summary>
public static readonly Func<IHasher> Factory = () => new XxHash128Hasher();

private readonly XxHash128 _inner = new();

/// <inheritdoc/>
Expand Down
3 changes: 3 additions & 0 deletions Ddth.Signum/XxHash3Hasher.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ namespace Ddth.Signum;
/// </summary>
public sealed class XxHash3Hasher : IHasher
{
/// <summary>Shared factory that creates a new <see cref="XxHash3Hasher"/> instance.</summary>
public static readonly Func<IHasher> Factory = () => new XxHash3Hasher();

private readonly XxHash3 _inner = new();

/// <inheritdoc/>
Expand Down
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,10 @@ The library ships three hashers (all from `System.IO.Hashing`): `XxHash128Hasher
`FingerprintOptions.HasherFactory`, or via the optional parameter on the `Signum` helper:

```csharp
// Use a built-in hasher through the static helper:
string hex = Signum.ChecksumHex(myObject, () => new XxHash3Hasher());
// Use a built-in hasher through the static helper. Each hasher exposes a shared
// `Factory` delegate, which lets the Signum helper cache and reuse a single
// Fingerprinter per hasher type:
string hex = Signum.ChecksumHex(myObject, XxHash3Hasher.Factory);
```

Provide your own `IHasher` to use any other algorithm:
Expand Down
Loading