引用:
https://www.cnblogs.com/InCerry/p/10325290.html
字典原理分析:
1、用数组存元素,元素的结构体存hashCode、next、key、value。 初始化的时候该数组的长度和桶的长度都是size(后续存在remove操作,数组并不是一一对应字典元素)
2、add操作通过hash算法获得hashCode,求余获得桶位置。元素实体该赋值赋值,桶记录实体idx,如果桶冲突,利用实体的next用单链表解决。(链地址法)
3、remove操作,处理对应桶的链表,重置元素实体的各种字段。记录freeCount, count-freeCount==真实字典长度
4、其他细节:字典有个版本字段,增删改都会导致+1。(freeList;//记录最近一次remove的实体的idx)
private void Resize(int newSize, bool forceNewHashCodes) {
Contract.Assert(newSize >= entries.Length);
//创建新的桶数组
int[] newBuckets = new int[newSize];
for (int i = 0; i < newBuckets.Length; i++) newBuckets[i] = -1;
//创建新的实体数组
Entry[] newEntries = new Entry[newSize];
//拷贝实体数组
Array.Copy(entries, 0, newEntries, 0, count);
//强制重新计算hashCode
if(forceNewHashCodes) {
for (int i = 0; i < count; i++) {
if(newEntries[i].hashCode != -1) {
newEntries[i].hashCode = (comparer.GetHashCode(newEntries[i].key) & 0x7FFFFFFF);
}
}
}
for (int i = 0; i < count; i++) {
if (newEntries[i].hashCode >= 0) {
//当hashCode大于-1(该元素有值),赋值桶的值
int bucket = newEntries[i].hashCode % newSize;
newEntries[i].next = newBuckets[bucket];
newBuckets[bucket] = i;
}
}
buckets = newBuckets;
entries = newEntries;
}
Find操作
private int FindEntry(TKey key) {
...
int hashCode = comparer.GetHashCode(key) & 0x7FFFFFFF;
//对应的桶拿到该桶位置最近一次的实体的idx, 遍历桶位置的单链表,如果对应的key和参数key相等,则return
for (int i = buckets[hashCode % buckets.Length]; i >= 0; i = entries[i].next) {
if (entries[i].hashCode == hashCode && comparer.Equals(entries[i].key, key)) return i;
}
...
return -1;
}