zoukankan      html  css  js  c++  java
  • 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.线程安全

  • 相关阅读:
    PHP SOAP
    PHP 笔记
    IIS PHP
    思途CMS
    HTML系列(2)基本的HTML标签(一)
    HTML系列(1)简介
    Python学习进程(14)异常处理
    Python学习进程(13)文件与IO
    Python学习进程(12)模块
    Python学习进程(11)日期和时间
  • 原文地址:https://www.cnblogs.com/lwzz/p/2361858.html
Copyright © 2011-2022 走看看