-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest-equals-hashcode.linq
211 lines (182 loc) · 5.4 KB
/
test-equals-hashcode.linq
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
<Query Kind="Program" />
/*
example using dictionary and hashes to do lookup by region + id and
reverse lookup by region + data
*/
void Main()
{
var list = new List<MyClass>()
{
new MyClass("alpha", "A100", new byte[] { 1, 4, 200, 34, 70, 23 }),
new MyClass("alpha", "A101", new byte[] { 2, 4, 200, 30, 70, 23 }),
new MyClass("alpha", "A102", new byte[] { 3, 4, 200, 39, 70, 23 }),
new MyClass("alpha", "A103", new byte[] { 4, 4, 200, 36, 70, 23 }),
new MyClass("alpha", "A104", new byte[] { 5, 4, 200, 34, 70, 23 }),
new MyClass("alpha", "A105", new byte[] { 6, 4, 200, 37, 70, 23 }),
new MyClass("alpha", "A106", new byte[] { 7, 4, 200, 32, 70, 23 }),
new MyClass("alpha", "A107", new byte[] { 8, 4, 200, 33, 70, 23 }),
new MyClass("alpha", "A108", new byte[] { 9, 4, 200, 39, 70, 23 }),
new MyClass("beta", "B100", new byte[] { 10, 4, 200, 38, 70, 23 }),
new MyClass("beta", "B101", new byte[] { 11, 4, 200, 37, 70, 23 }),
new MyClass("beta", "B102", new byte[] { 12, 4, 200, 34, 70, 23 }),
new MyClass("beta", "B103", new byte[] { 13, 4, 200, 31, 70, 23 }),
new MyClass("beta", "B104", new byte[] { 14, 4, 200, 31, 70, 23 }),
new MyClass("beta", "B105", new byte[] { 15, 4, 200, 32, 70, 23 }),
new MyClass("beta", "B106", new byte[] { 16, 4, 200, 33, 70, 23 }),
};
//init with some starting values
foreach(var i in list)
{
var kh = i.GetKeyHasher();
dict.Add(kh, i);
tcid.Add(i.GetDataHasher(), kh);
}
// manually check collections
Console.WriteLine(dict[MyClass.CreateKeyHasher("alpha", "A107")].ToString());
Console.WriteLine(tcid[MyClass.CreateDataHasher("beta", new byte[] { 16, 4, 200, 33, 70, 23 })].ToString());
// use Find() and Add() methods
Console.WriteLine(Add("beta", new byte[] { 16, 4, 200, 33, 70, 99 }));
Console.WriteLine(Add("beta", new byte[] { 16, 4, 200, 33, 70, 99 }));
Console.WriteLine(Add("beta", new byte[] { 16, 4, 200, 33, 70, 101 }));
Console.WriteLine(Add("zeta", new byte[] { 16, 4, 200, 33, 70, 101 }));
Console.WriteLine(Find("beta", "B103"));
}
Dictionary<MyClass.MyClassKeyHasher, MyClass> dict = new Dictionary<MyClass.MyClassKeyHasher, MyClass>();
Dictionary<MyClass.MyClassDataHasher, MyClass.MyClassKeyHasher> tcid = new Dictionary<MyClass.MyClassDataHasher, MyClass.MyClassKeyHasher>();
// add new values. if value already exist in given region
// then return same id instead of adding again
string Add(string RegionName, byte[] Data)
{
string rc = null;
var dh = MyClass.CreateDataHasher(RegionName, Data);
if(tcid.ContainsKey(dh))
{
rc = tcid[dh].Id;
}
else
{
rc = Guid.NewGuid().ToString("n");
var n = new MyClass(RegionName, rc, Data);
var kh = n.GetKeyHasher();
dict.Add(kh, n);
tcid.Add(dh, kh);
}
return rc;
}
MyClass Find(string RegionName, string Id)
{
return dict[MyClass.CreateKeyHasher(RegionName, Id)];
}
public class MyClass
{
readonly string RegionName;
readonly string Id;
byte[] Data;
public MyClass(string RegionName, string Id, byte[] Data)
{
this.RegionName = RegionName;
this.Id = Id;
this.Data = Data;
}
public override int GetHashCode()
{
return RegionName.GetHashCode() + Id.GetHashCode() + ((IStructuralEquatable)Data).GetHashCode(EqualityComparer<byte>.Default);
}
public MyClassKeyHasher GetKeyHasher()
{
return new MyClassKeyHasher(this.RegionName, this.Id);
}
public MyClassDataHasher GetDataHasher()
{
return new MyClassDataHasher(this.RegionName, this.Data);
}
public override bool Equals(object obj)
{
bool rc = false;
if(obj is MyClass)
{
var o = (MyClass)obj;
rc = string.Equals(this.RegionName, o.RegionName) &&
string.Equals(this.Id, o.Id) &&
Array.Equals(this.Data, o.Data);
}
return rc;
}
public static MyClassKeyHasher CreateKeyHasher(string RegionName, string Id)
{
return new MyClassKeyHasher(RegionName, Id);
}
public static MyClassDataHasher CreateDataHasher(string RegionName, byte[] Data)
{
return new MyClassDataHasher(RegionName, Data);
}
public override string ToString()
{
return $"{RegionName}/{Id}:{Data.ToHexString()}";
}
public struct MyClassKeyHasher
{
public readonly string RegionName;
public readonly string Id;
readonly int HashValue;
public MyClassKeyHasher(string RegionName, string Id)
{
this.RegionName = RegionName;
this.Id = Id;
this.HashValue = this.RegionName.GetHashCode() + Id.GetHashCode();
}
public override int GetHashCode()
{
return this.HashValue;
}
public override string ToString()
{
return $"{RegionName}/{Id}";
}
}
public struct MyClassDataHasher
{
readonly int HashValue;
public MyClassDataHasher(string RegionName, byte[] Data)
{
this.HashValue = RegionName.GetHashCode() + ((IStructuralEquatable)Data).GetHashCode(EqualityComparer<byte>.Default);
}
public override int GetHashCode()
{
return this.HashValue;
}
}
}
static class StringExt
{
public static string ToHexString(this byte[] a, bool prefix = false)
{
return HexStr(a, prefix);
}
public static string HexStr(byte[] p, bool prefix = false)
{
char[] c = null;
byte b;
int xstart;
if (prefix)
{
c = new char[p.Length * 2 + 2];
c[0] = '0';
c[1] = 'x';
xstart = 2;
}
else
{
c = new char[p.Length * 2];
xstart = 0;
}
for (int y = 0, x = xstart; y < p.Length; ++y, ++x)
{
b = ((byte)(p[y] >> 4));
c[x] = (char)(b > 9 ? b + 0x37 : b + 0x30);
b = ((byte)(p[y] & 0xF));
c[++x] = (char)(b > 9 ? b + 0x37 : b + 0x30);
}
return new string(c);
}
}