@@ -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 {
0 commit comments