Skip to content

Commit 57978be

Browse files
committed
Merge pull request brandondahler#2 from brandondahler/ReadonlyHashSize
Major updates to make IHashFunction immutable.
2 parents 74ea541 + 55546ca commit 57978be

76 files changed

Lines changed: 7156 additions & 2906 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

BernsteinHash/BernsteinHash.cs

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -32,22 +32,26 @@ namespace System.Data.HashFunction
3232
public class BernsteinHash
3333
: HashFunctionBase
3434
{
35-
/// <inheritdoc/>
36-
public override IEnumerable<int> ValidHashSizes { get { return new[] { 32 }; } }
37-
38-
/// <summary>Constructs new <see cref="BernsteinHash"/> instance.</summary>
39-
/// <remarks>HashSize defaults to 32 bits.</remarks>
35+
/// <summary>
36+
/// Initializes a new instance of the <see cref="BernsteinHash"/> class.
37+
/// </summary>
38+
/// <remarks>
39+
/// HashSize defaults to 32 bits.
40+
/// </remarks>
41+
/// <inheritdoc cref="HashFunctionBase(int)" />
4042
public BernsteinHash()
4143
: base(32)
4244
{
4345

4446
}
4547

46-
/// <inheritdoc/>
48+
49+
/// <exception cref="System.InvalidOperationException">HashSize set to an invalid value.</exception>
50+
/// <inheritdoc />
4751
protected override byte[] ComputeHashInternal(Stream data)
4852
{
4953
if (HashSize != 32)
50-
throw new ArgumentOutOfRangeException("HashSize");
54+
throw new InvalidOperationException("HashSize set to an invalid value.");
5155

5256

5357
UInt32 h = 0;

BernsteinHash/Data.HashFunction.BernsteinHash.csproj

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
<ErrorReport>prompt</ErrorReport>
2222
<WarningLevel>4</WarningLevel>
2323
<DocumentationFile>bin\Debug\System.Data.HashFunction.BernsteinHash.XML</DocumentationFile>
24+
<CodeAnalysisRuleSet>BasicDesignGuidelineRules.ruleset</CodeAnalysisRuleSet>
2425
</PropertyGroup>
2526
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
2627
<DebugType>pdbonly</DebugType>
@@ -30,13 +31,26 @@
3031
<ErrorReport>prompt</ErrorReport>
3132
<WarningLevel>4</WarningLevel>
3233
<DocumentationFile>bin\Release\System.Data.HashFunction.BernsteinHash.XML</DocumentationFile>
34+
<CodeAnalysisRuleSet>BasicDesignGuidelineRules.ruleset</CodeAnalysisRuleSet>
3335
</PropertyGroup>
3436
<PropertyGroup>
3537
<SignAssembly>true</SignAssembly>
3638
</PropertyGroup>
3739
<PropertyGroup>
3840
<AssemblyOriginatorKeyFile>..\Data.HashFunction.pfx</AssemblyOriginatorKeyFile>
3941
</PropertyGroup>
42+
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'NuGetDeploy|AnyCPU'">
43+
<OutputPath>bin\NuGetDeploy\</OutputPath>
44+
<DefineConstants>TRACE</DefineConstants>
45+
<DocumentationFile>bin\NuGetDeploy\System.Data.HashFunction.BernsteinHash.XML</DocumentationFile>
46+
<Optimize>true</Optimize>
47+
<DebugType>pdbonly</DebugType>
48+
<PlatformTarget>AnyCPU</PlatformTarget>
49+
<ErrorReport>prompt</ErrorReport>
50+
<CodeAnalysisRuleSet>BasicDesignGuidelineRules.ruleset</CodeAnalysisRuleSet>
51+
<AssemblyOriginatorKeyFile>..\Data.HashFunction.Production.pfx</AssemblyOriginatorKeyFile>
52+
<RunCodeAnalysis>true</RunCodeAnalysis>
53+
</PropertyGroup>
4054
<ItemGroup>
4155
<Reference Include="System" />
4256
<Reference Include="System.Core" />
@@ -58,7 +72,9 @@
5872
<None Include="..\Data.HashFunction.pfx">
5973
<Link>Properties\Data.HashFunction.pfx</Link>
6074
</None>
61-
<None Include="Data.HashFunction.BernsteinHash.nuspec" />
75+
<None Include="Data.HashFunction.BernsteinHash.nuspec">
76+
<SubType>Designer</SubType>
77+
</None>
6278
</ItemGroup>
6379
<ItemGroup>
6480
<ProjectReference Include="..\Core\Data.HashFunction.Core.csproj">

BernsteinHash/ModifiedBernsteinHash.cs

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -36,23 +36,26 @@ namespace System.Data.HashFunction
3636
public class ModifiedBernsteinHash
3737
: HashFunctionBase
3838
{
39-
/// <inheritdoc/>
40-
public override IEnumerable<int> ValidHashSizes { get { return new[] { 32 }; } }
41-
42-
/// <summary>Construct new <see cref="ModifiedBernsteinHash"/> instance.</summary>
43-
/// <remarks>HashSize defaults to 32 bits.</remarks>
39+
/// <summary>
40+
/// Initializes a new instance of the <see cref="ModifiedBernsteinHash"/> class.
41+
/// </summary>
42+
/// <remarks>
43+
/// <see cref="HashFunctionBase.HashSize "/> defaults to 32 bits.
44+
/// </remarks>
45+
/// <inheritdoc cref="HashFunctionBase(int)" />
4446
public ModifiedBernsteinHash()
4547
: base(32)
4648
{
4749

4850
}
4951

5052

51-
/// <inheritdoc/>
53+
/// <exception cref="System.InvalidOperationException">HashSize set to an invalid value.</exception>
54+
/// <inheritdoc />
5255
protected override byte[] ComputeHashInternal(Stream data)
5356
{
5457
if (HashSize != 32)
55-
throw new ArgumentOutOfRangeException("HashSize");
58+
throw new InvalidOperationException("HashSize set to an invalid value.");
5659

5760

5861
UInt32 h = 0;

BuzHash/BuzhashBase.cs

Lines changed: 92 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -19,34 +19,21 @@ namespace System.Data.HashFunction
1919
public abstract class BuzHashBase
2020
: HashFunctionBase
2121
{
22-
/// <summary>
23-
/// Table of 256 (preferably random and distinct) UInt64 values.
24-
/// </summary>
25-
/// <remarks>
26-
/// It is strongly recommended to return a reference to a static readonly private variable, otherwise each hashing will
27-
/// construct the table again.
28-
/// </remarks>
29-
public abstract UInt64[] Rtab { get; }
22+
/// <summary>Table of 256 (preferably random and distinct) UInt64 values.</summary>
23+
public IReadOnlyList<UInt64> Rtab { get { return _Rtab; } }
3024

31-
/// <summary>
32-
/// Direction that the circular shift step should use.
33-
/// </summary>
34-
public abstract CircularShiftDirection ShiftDirection { get; }
25+
/// <summary>Direction that the circular shift step should use.</summary>
26+
public CircularShiftDirection ShiftDirection { get { return _ShiftDirection; } }
3527

36-
/// <summary>
37-
/// Initialization value to use for the hash.
38-
/// </summary>
39-
public virtual UInt64 InitVal { get { return 0; } }
28+
/// <summary>Initialization value to use for the hash.</summary>
29+
public UInt64 InitVal { get { return _InitVal; } }
4030

41-
/// <inheritdoc/>
42-
public override IEnumerable<int> ValidHashSizes
43-
{
44-
get { return new[] { 8, 16, 32, 64 }; }
45-
}
4631

47-
/// <summary>
48-
/// Enumeration of possible directions a circular shift can be defined for.
49-
/// </summary>
32+
/// <summary>The list of possible hash sizes that can be provided to the <see cref="BuzHashBase"/> constructor.</summary>
33+
public static IEnumerable<int> ValidHashSizes { get { return _ValidHashSizes; } }
34+
35+
36+
/// <summary>Enumeration of possible directions a circular shift can be defined for.</summary>
5037
public enum CircularShiftDirection
5138
{
5239
/// <summary>Shift bits left.</summary>
@@ -56,18 +43,69 @@ public enum CircularShiftDirection
5643
}
5744

5845

46+
private readonly IReadOnlyList<UInt64> _Rtab;
47+
private readonly CircularShiftDirection _ShiftDirection;
48+
private readonly UInt64 _InitVal;
49+
50+
private static readonly IEnumerable<int> _ValidHashSizes = new[] { 8, 16, 32, 64 };
51+
52+
53+
54+
/// <remarks>
55+
/// Defaults <see cref="HashFunctionBase.HashSize"/> to 64. <inheritdoc cref="BuzHashBase(IReadOnlyList{UInt64}, CircularShiftDirection, int)"/>
56+
/// </remarks>
57+
/// <inheritdoc cref="BuzHashBase(IReadOnlyList{UInt64}, CircularShiftDirection, int)"/>
58+
protected BuzHashBase(IReadOnlyList<UInt64> rtab, CircularShiftDirection shiftDirection)
59+
: this(rtab, shiftDirection, 64)
60+
{
61+
62+
}
63+
64+
/// <remarks>
65+
/// Defaults <see cref="InitVal"/> to 0. <inheritdoc cref="BuzHashBase(IReadOnlyList{UInt64}, CircularShiftDirection, UInt64, int)"/>
66+
/// </remarks>
67+
/// <inheritdoc cref="BuzHashBase(IReadOnlyList{UInt64}, CircularShiftDirection, UInt64, int)"/>
68+
protected BuzHashBase(IReadOnlyList<UInt64> rtab, CircularShiftDirection shiftDirection, int hashSize)
69+
: this(rtab, shiftDirection, 0U, hashSize)
70+
{
71+
72+
}
73+
74+
/// <remarks>
75+
/// Defaults <see cref="HashFunctionBase.HashSize"/> to 64.
76+
/// </remarks>
77+
/// <inheritdoc cref="BuzHashBase(IReadOnlyList{UInt64}, CircularShiftDirection, UInt64, int)"/>
78+
protected BuzHashBase(IReadOnlyList<UInt64> rtab, CircularShiftDirection shiftDirection, UInt64 initVal)
79+
: this(rtab, shiftDirection, initVal, 64)
80+
{
81+
82+
}
83+
5984
/// <summary>
60-
/// Creates new instance of <see cref="BuzHashBase"/>.
85+
/// Initializes a new instance of the <see cref="BuzHashBase" /> class.
6186
/// </summary>
62-
/// <param name="defaultHashSize">Hash size to pass down to <see cref="HashFunctionBase" />.</param>
63-
protected BuzHashBase(int defaultHashSize = 64)
64-
: base(defaultHashSize)
87+
/// <param name="rtab"><inheritdoc cref="Rtab" /></param>
88+
/// <param name="shiftDirection"><inheritdoc cref="ShiftDirection" /></param>
89+
/// <param name="initVal"><inheritdoc cref="InitVal" /></param>
90+
/// <param name="hashSize"><inheritdoc cref="HashFunctionBase(int)" select="param[name=hashSize]" /></param>
91+
/// <exception cref="System.ArgumentOutOfRangeException">hashSize;hashSize must be contained within <see cref="ValidHashSizes" />.</exception>
92+
/// <inheritdoc cref="HashFunctionBase(int)" />
93+
protected BuzHashBase(IReadOnlyList<UInt64> rtab, CircularShiftDirection shiftDirection, UInt64 initVal, int hashSize)
94+
: base(hashSize)
6595
{
96+
if (!ValidHashSizes.Contains(hashSize))
97+
throw new ArgumentOutOfRangeException("hashSize", "hashSize must be contained within BuzHashBase.ValidHashSizes.");
6698

99+
_Rtab = rtab;
100+
_ShiftDirection = shiftDirection;
101+
102+
_InitVal = initVal;
67103
}
68104

69105

70-
/// <inheritdoc/>
106+
107+
/// <exception cref="System.InvalidOperationException">HashSize set to an invalid value.</exception>
108+
/// <inheritdoc />
71109
protected override byte[] ComputeHashInternal(Stream data)
72110
{
73111
switch (HashSize)
@@ -85,7 +123,7 @@ protected override byte[] ComputeHashInternal(Stream data)
85123
return ComputeHash64(data);
86124

87125
default:
88-
throw new ArgumentOutOfRangeException("HashSize");
126+
throw new InvalidOperationException("HashSize set to an invalid value.");
89127
}
90128
}
91129

@@ -94,7 +132,9 @@ protected override byte[] ComputeHashInternal(Stream data)
94132
/// 8-bit implementation of ComputeHash.
95133
/// </summary>
96134
/// <param name="data">Data to be hashed.</param>
97-
/// <returns>1-byte array containing the hash value.</returns>
135+
/// <returns>
136+
/// 1-byte array containing the hash value.
137+
/// </returns>
98138
protected byte[] ComputeHash8(Stream data)
99139
{
100140
byte h = (byte) InitVal;
@@ -110,7 +150,9 @@ protected byte[] ComputeHash8(Stream data)
110150
/// 16-bit implementation of ComputeHash.
111151
/// </summary>
112152
/// <param name="data">Data to be hashed.</param>
113-
/// <returns>2-byte array containing the hash value.</returns>
153+
/// <returns>
154+
/// 2-byte array containing the hash value.
155+
/// </returns>
114156
protected byte[] ComputeHash16(Stream data)
115157
{
116158
UInt16 h = (UInt16) InitVal;
@@ -125,7 +167,9 @@ protected byte[] ComputeHash16(Stream data)
125167
/// 32-bit implementation of ComputeHash.
126168
/// </summary>
127169
/// <param name="data">Data to be hashed.</param>
128-
/// <returns>4-byte array containing the hash value.</returns>
170+
/// <returns>
171+
/// 4-byte array containing the hash value.
172+
/// </returns>
129173
protected byte[] ComputeHash32(Stream data)
130174
{
131175
UInt32 h = (UInt32) InitVal;
@@ -140,7 +184,9 @@ protected byte[] ComputeHash32(Stream data)
140184
/// 64-bit implementation of ComputeHash.
141185
/// </summary>
142186
/// <param name="data">Data to be hashed.</param>
143-
/// <returns>8-byte array containing the hash value.</returns>
187+
/// <returns>
188+
/// 8-byte array containing the hash value.
189+
/// </returns>
144190
protected byte[] ComputeHash64(Stream data)
145191
{
146192
UInt64 h = InitVal;
@@ -157,7 +203,9 @@ protected byte[] ComputeHash64(Stream data)
157203
/// </summary>
158204
/// <param name="n">Byte value to shift.</param>
159205
/// <param name="shiftCount">Number of bits to shift the integer by.</param>
160-
/// <returns>Byte value after rotating by the specified amount of bits.</returns>
206+
/// <returns>
207+
/// Byte value after rotating by the specified amount of bits.
208+
/// </returns>
161209
[MethodImpl(MethodImplOptions.AggressiveInlining)]
162210
protected byte CShift(byte n, int shiftCount)
163211
{
@@ -172,7 +220,9 @@ protected byte CShift(byte n, int shiftCount)
172220
/// </summary>
173221
/// <param name="n">UInt16 value to shift.</param>
174222
/// <param name="shiftCount">Number of bits to shift the integer by.</param>
175-
/// <returns>UInt16 value after rotating by the specified amount of bits.</returns>
223+
/// <returns>
224+
/// UInt16 value after rotating by the specified amount of bits.
225+
/// </returns>
176226
[MethodImpl(MethodImplOptions.AggressiveInlining)]
177227
protected UInt16 CShift(UInt16 n, int shiftCount)
178228
{
@@ -187,7 +237,9 @@ protected UInt16 CShift(UInt16 n, int shiftCount)
187237
/// </summary>
188238
/// <param name="n">UInt32 value to shift.</param>
189239
/// <param name="shiftCount">Number of bits to shift the integer by.</param>
190-
/// <returns>UInt32 value after rotating by the specified amount of bits.</returns>
240+
/// <returns>
241+
/// UInt32 value after rotating by the specified amount of bits.
242+
/// </returns>
191243
[MethodImpl(MethodImplOptions.AggressiveInlining)]
192244
protected UInt32 CShift(UInt32 n, int shiftCount)
193245
{
@@ -202,7 +254,9 @@ protected UInt32 CShift(UInt32 n, int shiftCount)
202254
/// </summary>
203255
/// <param name="n">UInt64 value to shift.</param>
204256
/// <param name="shiftCount">Number of bits to shift the integer by.</param>
205-
/// <returns>UInt64 value after rotating by the specified amount of bits.</returns>
257+
/// <returns>
258+
/// UInt64 value after rotating by the specified amount of bits.
259+
/// </returns>
206260
[MethodImpl(MethodImplOptions.AggressiveInlining)]
207261
protected UInt64 CShift(UInt64 n, int shiftCount)
208262
{

BuzHash/Data.HashFunction.BuzHash.csproj

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
<ErrorReport>prompt</ErrorReport>
2222
<WarningLevel>4</WarningLevel>
2323
<DocumentationFile>bin\Debug\System.Data.HashFunction.Buzhash.XML</DocumentationFile>
24+
<CodeAnalysisRuleSet>BasicDesignGuidelineRules.ruleset</CodeAnalysisRuleSet>
2425
</PropertyGroup>
2526
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
2627
<DebugType>pdbonly</DebugType>
@@ -30,13 +31,26 @@
3031
<ErrorReport>prompt</ErrorReport>
3132
<WarningLevel>4</WarningLevel>
3233
<DocumentationFile>bin\Release\System.Data.HashFunction.Buzhash.XML</DocumentationFile>
34+
<CodeAnalysisRuleSet>BasicDesignGuidelineRules.ruleset</CodeAnalysisRuleSet>
3335
</PropertyGroup>
3436
<PropertyGroup>
3537
<SignAssembly>true</SignAssembly>
3638
</PropertyGroup>
3739
<PropertyGroup>
3840
<AssemblyOriginatorKeyFile>..\Data.HashFunction.pfx</AssemblyOriginatorKeyFile>
3941
</PropertyGroup>
42+
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'NuGetDeploy|AnyCPU'">
43+
<OutputPath>bin\NuGetDeploy\</OutputPath>
44+
<DefineConstants>TRACE</DefineConstants>
45+
<DocumentationFile>bin\NuGetDeploy\System.Data.HashFunction.Buzhash.XML</DocumentationFile>
46+
<Optimize>true</Optimize>
47+
<DebugType>pdbonly</DebugType>
48+
<PlatformTarget>AnyCPU</PlatformTarget>
49+
<ErrorReport>prompt</ErrorReport>
50+
<CodeAnalysisRuleSet>BasicDesignGuidelineRules.ruleset</CodeAnalysisRuleSet>
51+
<AssemblyOriginatorKeyFile>..\Data.HashFunction.Production.pfx</AssemblyOriginatorKeyFile>
52+
<RunCodeAnalysis>true</RunCodeAnalysis>
53+
</PropertyGroup>
4054
<ItemGroup>
4155
<Reference Include="System" />
4256
<Reference Include="System.Core" />

0 commit comments

Comments
 (0)