标签:替换 dict ati eth sqrt isp try div 结构体
https://www.cnblogs.com/xiaomowang/p/12405639.html
上述网址写的很详细
先从entry开始说起:
这是一个结构体(包含 哈希值,下一个entry的地址,键值,数值啊)
1.初始化
private void Initialize(int capacity) { int size = HashHelpers.GetPrime(capacity); buckets = new int[size]; for (int i = 0; i < buckets.Length; i++) buckets[i] = -1; entries = new Entry[size]; freeList = -1; }
HashHelper.cs
public static int GetPrime(int min) {
if (min < 0) throw new ArgumentException(Environment.GetResourceString("Arg_HTCapacityOverflow")); Contract.EndContractBlock(); for (int i = 0; i < primes.Length; i++) { int prime = primes[i]; if (prime >= min) return prime; } //outside of our predefined table. //compute the hard way. for (int i = (min | 1); i < Int32.MaxValue;i+=2) { if (IsPrime(i) && ((i - 1) % Hashtable.HashPrime != 0)) return i; } return min; }
public static bool IsPrime(int candidate)
{
if ((candidate & 1) != 0)//不是0
{
int limit = (int)Math.Sqrt(candidate);
for (int divisor = 3; divisor <= limit; divisor += 2)
{
if ((candidate % divisor) == 0)
return false;
}
return true;
}
return (candidate == 2);
}
2.插入(修改ASDCD)
int hashCode = comparer.GetHashCode(key) & 0x7FFFFFFF; int targetBucket = hashCode % buckets.Length; //根据这个(targetBucket)在buckets中找到对应数组并遍历这个根据entry中的next看看是不是已经有值了(替换) //如果没有对应位置获取entries中第一个空的位置并添加上,如果已经满了,增加容量并重新初始化
3.删除
根据hashcode生成的bucket找到entries,。在对应entries中找到对应值然后置空
4.查找
4.1 下标查找也是根据ashcode生成的bucket找到entries然后遍历entries
5.遍历
遍历数组entries
标签:替换 dict ati eth sqrt isp try div 结构体
原文地址:https://www.cnblogs.com/Elijah-/p/13845832.html