-
-
Notifications
You must be signed in to change notification settings - Fork 33
Expand file tree
/
Copy pathHashCodeHelper.cs
More file actions
109 lines (96 loc) · 4.67 KB
/
HashCodeHelper.cs
File metadata and controls
109 lines (96 loc) · 4.67 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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
using System;
using System.Collections.Generic;
using System.Runtime.CompilerServices;
using UnityMvvmToolkit.Core.Interfaces;
namespace UnityMvvmToolkit.Core.Internal.Helpers
{
internal static class HashCodeHelper
{
private static Dictionary<(int, int), int> s_CombinedHashCodes = new Dictionary<(int, int), int>();
private static int s_StaticHashCodeId;
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static int GetMemberHashCode(Type contextType, string memberName)
{
var contextTypeHash = contextType.GetHashCode();
var memberNameHash = StringComparer.OrdinalIgnoreCase.GetHashCode(memberName);
return CombineHashCode(contextTypeHash, memberNameHash);
}
/// <summary>
/// <para><c>If name is null:</c></para>
/// <b>TargetTypeHash + SourceTypeHash</b>
/// <para><c>Else:</c></para>
/// <b>NameHash + TargetTypeHash + SourceTypeHash</b>
/// </summary>
/// <param name="converter">Converter.</param>
/// <param name="converterName">Converter name.</param>
/// <returns>Hash code.</returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static int GetPropertyConverterHashCode(IPropertyValueConverter converter, string converterName = null)
{
return GetPropertyWrapperConverterId(converter.TargetType, converter.SourceType, converterName);
}
/// <summary>
/// <para><c>If name is null:</c></para>
/// <b>TargetTypeHash</b>
/// <para><c>Else:</c></para>
/// <b>NameHash + TargetTypeHash</b>
/// </summary>
/// <param name="converter">Converter.</param>
/// <param name="converterName">Converter name.</param>
/// <returns>Hash code.</returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static int GetParameterConverterHashCode(IParameterValueConverter converter, string converterName = null)
{
var targetTypeHash = converter.TargetType.GetHashCode();
return string.IsNullOrWhiteSpace(converterName)
? targetTypeHash
: CombineHashCode(converterName.GetHashCode(), targetTypeHash);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static int GetPropertyWrapperConverterId(IPropertyValueConverter converter, string converterName = null)
{
return GetPropertyConverterHashCode(converter, converterName);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static int GetPropertyWrapperConverterId(Type targetType, Type sourceType, string converterName = null)
{
return string.IsNullOrWhiteSpace(converterName)
? CombineHashCode(targetType.GetHashCode(), sourceType.GetHashCode())
: CombineHashCode(converterName.GetHashCode(), targetType.GetHashCode(), sourceType.GetHashCode());
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static int GetCommandWrapperConverterId(IParameterValueConverter converter, string converterName = null)
{
return GetParameterConverterHashCode(converter, converterName);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static int GetCommandWrapperConverterId(Type targetType, string converterName = null)
{
return string.IsNullOrWhiteSpace(converterName)
? targetType.GetHashCode()
: CombineHashCode(converterName.GetHashCode(), targetType.GetHashCode());
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static int GetCommandWrapperId(Type contextType, Type targetType, string converterName = null)
{
return string.IsNullOrWhiteSpace(converterName)
? CombineHashCode(contextType.GetHashCode(), targetType.GetHashCode())
: CombineHashCode(contextType.GetHashCode(), converterName.GetHashCode(), targetType.GetHashCode());
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static int CombineHashCode(int hash1, int hash2)
{
if (!s_CombinedHashCodes.TryGetValue((hash1, hash2), out var hashCode))
{
hashCode = s_StaticHashCodeId++;
s_CombinedHashCodes.Add((hash1, hash2), hashCode);
}
return hashCode;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static int CombineHashCode(int hash1, int hash2, int hash3)
{
return CombineHashCode(CombineHashCode(hash1, hash2), hash3);
}
}
}