Skip to content
This repository has been archived by the owner on May 13, 2022. It is now read-only.

Commit

Permalink
Merge pull request #2 from brandondahler/ReadonlyHashSize
Browse files Browse the repository at this point in the history
Major updates to make IHashFunction immutable.
  • Loading branch information
brandondahler committed Aug 27, 2014
2 parents 74ea541 + 55546ca commit 57978be
Show file tree
Hide file tree
Showing 76 changed files with 7,156 additions and 2,906 deletions.
18 changes: 11 additions & 7 deletions BernsteinHash/BernsteinHash.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,22 +32,26 @@ namespace System.Data.HashFunction
public class BernsteinHash
: HashFunctionBase
{
/// <inheritdoc/>
public override IEnumerable<int> ValidHashSizes { get { return new[] { 32 }; } }

/// <summary>Constructs new <see cref="BernsteinHash"/> instance.</summary>
/// <remarks>HashSize defaults to 32 bits.</remarks>
/// <summary>
/// Initializes a new instance of the <see cref="BernsteinHash"/> class.
/// </summary>
/// <remarks>
/// HashSize defaults to 32 bits.
/// </remarks>
/// <inheritdoc cref="HashFunctionBase(int)" />
public BernsteinHash()
: base(32)
{

}

/// <inheritdoc/>

/// <exception cref="System.InvalidOperationException">HashSize set to an invalid value.</exception>
/// <inheritdoc />
protected override byte[] ComputeHashInternal(Stream data)
{
if (HashSize != 32)
throw new ArgumentOutOfRangeException("HashSize");
throw new InvalidOperationException("HashSize set to an invalid value.");


UInt32 h = 0;
Expand Down
18 changes: 17 additions & 1 deletion BernsteinHash/Data.HashFunction.BernsteinHash.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<DocumentationFile>bin\Debug\System.Data.HashFunction.BernsteinHash.XML</DocumentationFile>
<CodeAnalysisRuleSet>BasicDesignGuidelineRules.ruleset</CodeAnalysisRuleSet>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<DebugType>pdbonly</DebugType>
Expand All @@ -30,13 +31,26 @@
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<DocumentationFile>bin\Release\System.Data.HashFunction.BernsteinHash.XML</DocumentationFile>
<CodeAnalysisRuleSet>BasicDesignGuidelineRules.ruleset</CodeAnalysisRuleSet>
</PropertyGroup>
<PropertyGroup>
<SignAssembly>true</SignAssembly>
</PropertyGroup>
<PropertyGroup>
<AssemblyOriginatorKeyFile>..\Data.HashFunction.pfx</AssemblyOriginatorKeyFile>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'NuGetDeploy|AnyCPU'">
<OutputPath>bin\NuGetDeploy\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<DocumentationFile>bin\NuGetDeploy\System.Data.HashFunction.BernsteinHash.XML</DocumentationFile>
<Optimize>true</Optimize>
<DebugType>pdbonly</DebugType>
<PlatformTarget>AnyCPU</PlatformTarget>
<ErrorReport>prompt</ErrorReport>
<CodeAnalysisRuleSet>BasicDesignGuidelineRules.ruleset</CodeAnalysisRuleSet>
<AssemblyOriginatorKeyFile>..\Data.HashFunction.Production.pfx</AssemblyOriginatorKeyFile>
<RunCodeAnalysis>true</RunCodeAnalysis>
</PropertyGroup>
<ItemGroup>
<Reference Include="System" />
<Reference Include="System.Core" />
Expand All @@ -58,7 +72,9 @@
<None Include="..\Data.HashFunction.pfx">
<Link>Properties\Data.HashFunction.pfx</Link>
</None>
<None Include="Data.HashFunction.BernsteinHash.nuspec" />
<None Include="Data.HashFunction.BernsteinHash.nuspec">
<SubType>Designer</SubType>
</None>
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\Core\Data.HashFunction.Core.csproj">
Expand Down
17 changes: 10 additions & 7 deletions BernsteinHash/ModifiedBernsteinHash.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,23 +36,26 @@ namespace System.Data.HashFunction
public class ModifiedBernsteinHash
: HashFunctionBase
{
/// <inheritdoc/>
public override IEnumerable<int> ValidHashSizes { get { return new[] { 32 }; } }

/// <summary>Construct new <see cref="ModifiedBernsteinHash"/> instance.</summary>
/// <remarks>HashSize defaults to 32 bits.</remarks>
/// <summary>
/// Initializes a new instance of the <see cref="ModifiedBernsteinHash"/> class.
/// </summary>
/// <remarks>
/// <see cref="HashFunctionBase.HashSize "/> defaults to 32 bits.
/// </remarks>
/// <inheritdoc cref="HashFunctionBase(int)" />
public ModifiedBernsteinHash()
: base(32)
{

}


/// <inheritdoc/>
/// <exception cref="System.InvalidOperationException">HashSize set to an invalid value.</exception>
/// <inheritdoc />
protected override byte[] ComputeHashInternal(Stream data)
{
if (HashSize != 32)
throw new ArgumentOutOfRangeException("HashSize");
throw new InvalidOperationException("HashSize set to an invalid value.");


UInt32 h = 0;
Expand Down
130 changes: 92 additions & 38 deletions BuzHash/BuzhashBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,34 +19,21 @@ namespace System.Data.HashFunction
public abstract class BuzHashBase
: HashFunctionBase
{
/// <summary>
/// Table of 256 (preferably random and distinct) UInt64 values.
/// </summary>
/// <remarks>
/// It is strongly recommended to return a reference to a static readonly private variable, otherwise each hashing will
/// construct the table again.
/// </remarks>
public abstract UInt64[] Rtab { get; }
/// <summary>Table of 256 (preferably random and distinct) UInt64 values.</summary>
public IReadOnlyList<UInt64> Rtab { get { return _Rtab; } }

/// <summary>
/// Direction that the circular shift step should use.
/// </summary>
public abstract CircularShiftDirection ShiftDirection { get; }
/// <summary>Direction that the circular shift step should use.</summary>
public CircularShiftDirection ShiftDirection { get { return _ShiftDirection; } }

/// <summary>
/// Initialization value to use for the hash.
/// </summary>
public virtual UInt64 InitVal { get { return 0; } }
/// <summary>Initialization value to use for the hash.</summary>
public UInt64 InitVal { get { return _InitVal; } }

/// <inheritdoc/>
public override IEnumerable<int> ValidHashSizes
{
get { return new[] { 8, 16, 32, 64 }; }
}

/// <summary>
/// Enumeration of possible directions a circular shift can be defined for.
/// </summary>
/// <summary>The list of possible hash sizes that can be provided to the <see cref="BuzHashBase"/> constructor.</summary>
public static IEnumerable<int> ValidHashSizes { get { return _ValidHashSizes; } }


/// <summary>Enumeration of possible directions a circular shift can be defined for.</summary>
public enum CircularShiftDirection
{
/// <summary>Shift bits left.</summary>
Expand All @@ -56,18 +43,69 @@ public enum CircularShiftDirection
}


private readonly IReadOnlyList<UInt64> _Rtab;
private readonly CircularShiftDirection _ShiftDirection;
private readonly UInt64 _InitVal;

private static readonly IEnumerable<int> _ValidHashSizes = new[] { 8, 16, 32, 64 };



/// <remarks>
/// Defaults <see cref="HashFunctionBase.HashSize"/> to 64. <inheritdoc cref="BuzHashBase(IReadOnlyList{UInt64}, CircularShiftDirection, int)"/>
/// </remarks>
/// <inheritdoc cref="BuzHashBase(IReadOnlyList{UInt64}, CircularShiftDirection, int)"/>
protected BuzHashBase(IReadOnlyList<UInt64> rtab, CircularShiftDirection shiftDirection)
: this(rtab, shiftDirection, 64)
{

}

/// <remarks>
/// Defaults <see cref="InitVal"/> to 0. <inheritdoc cref="BuzHashBase(IReadOnlyList{UInt64}, CircularShiftDirection, UInt64, int)"/>
/// </remarks>
/// <inheritdoc cref="BuzHashBase(IReadOnlyList{UInt64}, CircularShiftDirection, UInt64, int)"/>
protected BuzHashBase(IReadOnlyList<UInt64> rtab, CircularShiftDirection shiftDirection, int hashSize)
: this(rtab, shiftDirection, 0U, hashSize)
{

}

/// <remarks>
/// Defaults <see cref="HashFunctionBase.HashSize"/> to 64.
/// </remarks>
/// <inheritdoc cref="BuzHashBase(IReadOnlyList{UInt64}, CircularShiftDirection, UInt64, int)"/>
protected BuzHashBase(IReadOnlyList<UInt64> rtab, CircularShiftDirection shiftDirection, UInt64 initVal)
: this(rtab, shiftDirection, initVal, 64)
{

}

/// <summary>
/// Creates new instance of <see cref="BuzHashBase"/>.
/// Initializes a new instance of the <see cref="BuzHashBase" /> class.
/// </summary>
/// <param name="defaultHashSize">Hash size to pass down to <see cref="HashFunctionBase" />.</param>
protected BuzHashBase(int defaultHashSize = 64)
: base(defaultHashSize)
/// <param name="rtab"><inheritdoc cref="Rtab" /></param>
/// <param name="shiftDirection"><inheritdoc cref="ShiftDirection" /></param>
/// <param name="initVal"><inheritdoc cref="InitVal" /></param>
/// <param name="hashSize"><inheritdoc cref="HashFunctionBase(int)" select="param[name=hashSize]" /></param>
/// <exception cref="System.ArgumentOutOfRangeException">hashSize;hashSize must be contained within <see cref="ValidHashSizes" />.</exception>
/// <inheritdoc cref="HashFunctionBase(int)" />
protected BuzHashBase(IReadOnlyList<UInt64> rtab, CircularShiftDirection shiftDirection, UInt64 initVal, int hashSize)
: base(hashSize)
{
if (!ValidHashSizes.Contains(hashSize))
throw new ArgumentOutOfRangeException("hashSize", "hashSize must be contained within BuzHashBase.ValidHashSizes.");

_Rtab = rtab;
_ShiftDirection = shiftDirection;

_InitVal = initVal;
}


/// <inheritdoc/>

/// <exception cref="System.InvalidOperationException">HashSize set to an invalid value.</exception>
/// <inheritdoc />
protected override byte[] ComputeHashInternal(Stream data)
{
switch (HashSize)
Expand All @@ -85,7 +123,7 @@ protected override byte[] ComputeHashInternal(Stream data)
return ComputeHash64(data);

default:
throw new ArgumentOutOfRangeException("HashSize");
throw new InvalidOperationException("HashSize set to an invalid value.");
}
}

Expand All @@ -94,7 +132,9 @@ protected override byte[] ComputeHashInternal(Stream data)
/// 8-bit implementation of ComputeHash.
/// </summary>
/// <param name="data">Data to be hashed.</param>
/// <returns>1-byte array containing the hash value.</returns>
/// <returns>
/// 1-byte array containing the hash value.
/// </returns>
protected byte[] ComputeHash8(Stream data)
{
byte h = (byte) InitVal;
Expand All @@ -110,7 +150,9 @@ protected byte[] ComputeHash8(Stream data)
/// 16-bit implementation of ComputeHash.
/// </summary>
/// <param name="data">Data to be hashed.</param>
/// <returns>2-byte array containing the hash value.</returns>
/// <returns>
/// 2-byte array containing the hash value.
/// </returns>
protected byte[] ComputeHash16(Stream data)
{
UInt16 h = (UInt16) InitVal;
Expand All @@ -125,7 +167,9 @@ protected byte[] ComputeHash16(Stream data)
/// 32-bit implementation of ComputeHash.
/// </summary>
/// <param name="data">Data to be hashed.</param>
/// <returns>4-byte array containing the hash value.</returns>
/// <returns>
/// 4-byte array containing the hash value.
/// </returns>
protected byte[] ComputeHash32(Stream data)
{
UInt32 h = (UInt32) InitVal;
Expand All @@ -140,7 +184,9 @@ protected byte[] ComputeHash32(Stream data)
/// 64-bit implementation of ComputeHash.
/// </summary>
/// <param name="data">Data to be hashed.</param>
/// <returns>8-byte array containing the hash value.</returns>
/// <returns>
/// 8-byte array containing the hash value.
/// </returns>
protected byte[] ComputeHash64(Stream data)
{
UInt64 h = InitVal;
Expand All @@ -157,7 +203,9 @@ protected byte[] ComputeHash64(Stream data)
/// </summary>
/// <param name="n">Byte value to shift.</param>
/// <param name="shiftCount">Number of bits to shift the integer by.</param>
/// <returns>Byte value after rotating by the specified amount of bits.</returns>
/// <returns>
/// Byte value after rotating by the specified amount of bits.
/// </returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
protected byte CShift(byte n, int shiftCount)
{
Expand All @@ -172,7 +220,9 @@ protected byte CShift(byte n, int shiftCount)
/// </summary>
/// <param name="n">UInt16 value to shift.</param>
/// <param name="shiftCount">Number of bits to shift the integer by.</param>
/// <returns>UInt16 value after rotating by the specified amount of bits.</returns>
/// <returns>
/// UInt16 value after rotating by the specified amount of bits.
/// </returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
protected UInt16 CShift(UInt16 n, int shiftCount)
{
Expand All @@ -187,7 +237,9 @@ protected UInt16 CShift(UInt16 n, int shiftCount)
/// </summary>
/// <param name="n">UInt32 value to shift.</param>
/// <param name="shiftCount">Number of bits to shift the integer by.</param>
/// <returns>UInt32 value after rotating by the specified amount of bits.</returns>
/// <returns>
/// UInt32 value after rotating by the specified amount of bits.
/// </returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
protected UInt32 CShift(UInt32 n, int shiftCount)
{
Expand All @@ -202,7 +254,9 @@ protected UInt32 CShift(UInt32 n, int shiftCount)
/// </summary>
/// <param name="n">UInt64 value to shift.</param>
/// <param name="shiftCount">Number of bits to shift the integer by.</param>
/// <returns>UInt64 value after rotating by the specified amount of bits.</returns>
/// <returns>
/// UInt64 value after rotating by the specified amount of bits.
/// </returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
protected UInt64 CShift(UInt64 n, int shiftCount)
{
Expand Down
14 changes: 14 additions & 0 deletions BuzHash/Data.HashFunction.BuzHash.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<DocumentationFile>bin\Debug\System.Data.HashFunction.Buzhash.XML</DocumentationFile>
<CodeAnalysisRuleSet>BasicDesignGuidelineRules.ruleset</CodeAnalysisRuleSet>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<DebugType>pdbonly</DebugType>
Expand All @@ -30,13 +31,26 @@
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<DocumentationFile>bin\Release\System.Data.HashFunction.Buzhash.XML</DocumentationFile>
<CodeAnalysisRuleSet>BasicDesignGuidelineRules.ruleset</CodeAnalysisRuleSet>
</PropertyGroup>
<PropertyGroup>
<SignAssembly>true</SignAssembly>
</PropertyGroup>
<PropertyGroup>
<AssemblyOriginatorKeyFile>..\Data.HashFunction.pfx</AssemblyOriginatorKeyFile>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'NuGetDeploy|AnyCPU'">
<OutputPath>bin\NuGetDeploy\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<DocumentationFile>bin\NuGetDeploy\System.Data.HashFunction.Buzhash.XML</DocumentationFile>
<Optimize>true</Optimize>
<DebugType>pdbonly</DebugType>
<PlatformTarget>AnyCPU</PlatformTarget>
<ErrorReport>prompt</ErrorReport>
<CodeAnalysisRuleSet>BasicDesignGuidelineRules.ruleset</CodeAnalysisRuleSet>
<AssemblyOriginatorKeyFile>..\Data.HashFunction.Production.pfx</AssemblyOriginatorKeyFile>
<RunCodeAnalysis>true</RunCodeAnalysis>
</PropertyGroup>
<ItemGroup>
<Reference Include="System" />
<Reference Include="System.Core" />
Expand Down

0 comments on commit 57978be

Please sign in to comment.