forked from KSemenenko/TimePeriodLibrary
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHashTool.cs
More file actions
84 lines (74 loc) · 2.58 KB
/
HashTool.cs
File metadata and controls
84 lines (74 loc) · 2.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
// -- FILE ------------------------------------------------------------------
// name : HashTool.cs
// project : Itenso Time Period
// created : Jani Giannoudis - 2011.02.18
// language : C# 4.0
// environment: .NET 2.0
// copyright : (c) 2011-2012 by Itenso GmbH, Switzerland
// --------------------------------------------------------------------------
using System.Collections;
namespace Itenso.TimePeriod
{
// ------------------------------------------------------------------------
/// <summary>
/// Some hash utility methods for use in the implementation of value types
/// and collections.
/// </summary>
public static class HashTool
{
// ----------------------------------------------------------------------
public static int AddHashCode( int hash, object obj )
{
int combinedHash = obj != null ? obj.GetHashCode() : nullValue;
// if ( hash != 0 ) // perform this check to prevent FxCop warning 'op could overflow'
// {
combinedHash += hash * factor;
// }
return combinedHash;
} // AddHashCode
// ----------------------------------------------------------------------
public static int AddHashCode( int hash, int objHash )
{
int combinedHash = objHash;
// if ( hash != 0 ) // perform this check to prevent FxCop warning 'op could overflow'
// {
combinedHash += hash * factor;
// }
return combinedHash;
} // AddHashCode
// ----------------------------------------------------------------------
public static int ComputeHashCode( object obj )
{
return obj != null ? obj.GetHashCode() : nullValue;
} // ComputeHashCode
// ----------------------------------------------------------------------
public static int ComputeHashCode( params object[] objs )
{
int hash = initValue;
if ( objs != null )
{
foreach ( object obj in objs )
{
hash = hash * factor + ( obj != null ? obj.GetHashCode() : nullValue );
}
}
return hash;
} // ComputeHashCode
// ----------------------------------------------------------------------
public static int ComputeHashCode( IEnumerable enumerable )
{
int hash = initValue;
foreach ( object item in enumerable )
{
hash = hash * factor + ( item != null ? item.GetHashCode() : nullValue );
}
return hash;
} // ComputeHashCode
// ----------------------------------------------------------------------
// members
private const int nullValue = 0;
private const int initValue = 1;
private const int factor = 31;
} // class HashTool
} // namespace Itenso.TimePeriod
// -- EOF -------------------------------------------------------------------