• Dictionary源码解析(未完)


    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.线程安全

  • 相关阅读:
    洛谷P1012拼数(简单题排序技巧)
    欧拉函数(模板,相关问题持续更新中)
    欧几里得,扩展欧几里得(模板)
    快速幂(模板)
    读入读出挂
    webpack 使用style-loader,css-loader添加css样式
    webpack-dev-server工具
    webpack4 配置
    获取自定义属性值
    安装PS
  • 原文地址:https://www.cnblogs.com/lwzz/p/2361858.html
Copyright © 2020-2023  润新知