private void Insert(TKey key, TValue value, bool add)
{
if (key == null)
{
ThrowHelper.ThrowArgumentNullException(ExceptionArgument.key);
}
if (this.buckets == null)
{
this.Initialize(0);
}
int num = this.comparer.GetHashCode(key) & 2147483647;
int num2 = num % this.buckets.Length;
for (int i = this.buckets[num2]; i >= 0; i = this.entries[i].next)
{
if (this.entries[i].hashCode == num && this.comparer.Equals(this.entries[i].key, key))
{
if (add)
{
ThrowHelper.ThrowArgumentException(ExceptionResource.Argument_AddingDuplicate);
}
this.entries[i].value = value;
this.version++;
return;
}
}
int num3;
if (this.freeCount > 0)
{
num3 = this.freeList;
this.freeList = this.entries[num3].next;
this.freeCount--;
}
else
{
if (this.count == this.entries.Length)
{
this.Resize();
num2 = num % this.buckets.Length;
}
num3 = this.count;
this.count++;
}
this.entries[num3].hashCode = num;
this.entries[num3].next = this.buckets[num2];
this.entries[num3].key = key;
this.entries[num3].value = value;
this.buckets[num2] = num3;
this.version++;
}

1.如果传入的是字符串的话,GetHashCode的算法和字符串的长度成正比。这导致了有时这个O(1)的时间复杂度在具体使用时可能比log(n)复杂度的还有久。

2. 如果直接重写equals方法,可能会对值类型装箱。这样就不能体现值类型在调用完就立刻回收,不会增加GC压力的这个好处了。

3.memecached

解决增加节点后的缓存失效问题,hash consistence算法。

 http://blog.csdn.net/sparkliang/article/details/5279393

 

3.线程安全

posted on 2012-02-21 20:28  一路转圈的雪人  阅读(765)  评论(0编辑  收藏  举报