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
|
// ==++==
//
// Copyright (c) Microsoft Corporation. All rights reserved.
//
// ==--==
// =+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+
//
// HashLookup.cs
//
// <OWNER>Microsoft</OWNER>
//
// =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
using System.Collections.Generic;
using System.Diagnostics.Contracts;
namespace System.Linq.Parallel
{
/// <summary>
/// A simple hash map data structure, derived from the LINQ set we also use.
/// </summary>
/// <typeparam name="TKey">The kind of keys contained within.</typeparam>
/// <typeparam name="TValue">The kind of values contained within.</typeparam>
internal class HashLookup<TKey, TValue>
{
int[] buckets;
Slot[] slots;
int count;
int freeList;
IEqualityComparer<TKey> comparer;
internal HashLookup() : this(null)
{
}
internal HashLookup(IEqualityComparer<TKey> comparer)
{
this.comparer = comparer;
buckets = new int[7];
slots = new Slot[7];
freeList = -1;
}
// If value is not in set, add it and return true; otherwise return false
internal bool Add(TKey key, TValue value)
{
return !Find(key, true, false, ref value);
}
// Check whether value is in set
internal bool TryGetValue(TKey key, ref TValue value)
{
return Find(key, false, false, ref value);
}
internal TValue this[TKey key]
{
set
{
TValue v = value;
Find(key, false, true, ref v);
}
}
private int GetKeyHashCode(TKey key)
{
return 0x7fffffff &
(comparer == null ?
(key == null ? 0 : key.GetHashCode()) :
comparer.GetHashCode(key));
}
private bool AreKeysEqual(TKey key1, TKey key2)
{
return
(comparer == null ?
((key1 == null && key2 == null) || (key1 != null && key1.Equals(key2))) :
comparer.Equals(key1, key2));
}
// If value is in set, remove it and return true; otherwise return false
internal bool Remove(TKey key)
{
int hashCode = GetKeyHashCode(key);
int bucket = hashCode % buckets.Length;
int last = -1;
for (int i = buckets[bucket] - 1; i >= 0; last = i, i = slots[i].next)
{
if (slots[i].hashCode == hashCode && AreKeysEqual(slots[i].key, key))
{
if (last < 0)
{
buckets[bucket] = slots[i].next + 1;
}
else
{
slots[last].next = slots[i].next;
}
slots[i].hashCode = -1;
slots[i].key = default(TKey);
slots[i].value = default(TValue);
slots[i].next = freeList;
freeList = i;
return true;
}
}
return false;
}
private bool Find(TKey key, bool add, bool set, ref TValue value)
{
int hashCode = GetKeyHashCode(key);
for (int i = buckets[hashCode % buckets.Length] - 1; i >= 0; i = slots[i].next)
{
if (slots[i].hashCode == hashCode && AreKeysEqual(slots[i].key, key))
{
if (set)
{
slots[i].value = value;
return true;
}
else
{
value = slots[i].value;
return true;
}
}
}
if (add)
{
int index;
if (freeList >= 0)
{
index = freeList;
freeList = slots[index].next;
}
else
{
if (count == slots.Length) Resize();
index = count;
count++;
}
int bucket = hashCode % buckets.Length;
slots[index].hashCode = hashCode;
slots[index].key = key;
slots[index].value = value;
slots[index].next = buckets[bucket] - 1;
buckets[bucket] = index + 1;
}
return false;
}
void Resize()
{
int newSize = checked(count * 2 + 1);
int[] newBuckets = new int[newSize];
Slot[] newSlots = new Slot[newSize];
Array.Copy(slots, 0, newSlots, 0, count);
for (int i = 0; i < count; i++)
{
int bucket = newSlots[i].hashCode % newSize;
newSlots[i].next = newBuckets[bucket] - 1;
newBuckets[bucket] = i + 1;
}
buckets = newBuckets;
slots = newSlots;
}
internal int Count
{
get { return count; }
}
internal KeyValuePair<TKey, TValue> this[int index]
{
get { return new KeyValuePair<TKey, TValue>(slots[index].key, slots[index].value); }
}
internal struct Slot
{
internal int hashCode;
internal TKey key;
internal TValue value;
internal int next;
}
}
}
|