Ejemplo n.º 1
0
    /** 设置映射到指定键的值 */
    public object put(int key, object val)
    {
        IntKeyHashMapEntry[] array = this.array;
        int i = (key & 0x7fffffff) % array.Length;
        IntKeyHashMapEntry n = array [i];

        if (n != null)
        {
            while (true)
            {
                if (n.key == key)
                {
                    object old = n.val;
                    n.val = val;
                    return(old);
                }
                if (n.next == null)
                {
                    break;
                }
                n = n.next;
            }
            n.next = new IntKeyHashMapEntry(key, val);
        }
        else
        {
            array [i] = new IntKeyHashMapEntry(key, val);
        }
        _size++;
        if (_size >= threshold)
        {
            rehash(array.Length + 1);
        }
        return(null);
    }
Ejemplo n.º 2
0
    /** 根据新的容量,重新分布哈希码 */
    public void rehash(int capacity)
    {
        IntKeyHashMapEntry[] array = this.array;
        int len = array.Length;

        if (capacity <= len)
        {
            return;
        }
        int c = len;

        for (; c < capacity; c = (c << 1) + 1)
        {
            ;
        }
        IntKeyHashMapEntry[] temp = new IntKeyHashMapEntry[c];
        IntKeyHashMapEntry   n, next, old;

        for (int i = len - 1, j = 0; i >= 0; i--)
        {
            n = array [i];
            // 将哈希条目从旧数组中挪到新数组中,链表中条目的次序被反转
            while (n != null)
            {
                next     = n.next;
                j        = (n.key & 0x7fffffff) % c;
                old      = temp [j];
                temp [j] = n;
                n.next   = old;
                n        = next;
            }
        }
        this.array = temp;
        threshold  = (int)(c * loadFactor);
    }
Ejemplo n.º 3
0
    /** 移除映射到指定键的值 */
    public object remove(int key)
    {
        IntKeyHashMapEntry[] array = this.array;
        int i = (key & 0x7fffffff) % array.Length;
        IntKeyHashMapEntry n      = array [i];
        IntKeyHashMapEntry parent = null;

        while (n != null)
        {
            if (n.key == key)
            {
                object old = n.val;
                if (parent != null)
                {
                    parent.next = n.next;
                }
                else
                {
                    array [i] = n.next;
                }
                _size--;
                return(old);
            }
            parent = n;
            n      = n.next;
        }
        return(null);
    }
Ejemplo n.º 4
0
    /** 选择方法,用指定的选择器对象选出表中的元素,返回值参考常量定义 */
    public int select(Selector selector)
    {
        IntKeyHashMapEntry[] array = this.array;
        IntKeyHashMapEntry   n, next;
        IntKeyHashMapEntry   parent = null;
        int t;
        int r = SelectorKit.FALSE;

        for (int i = array.Length - 1; i >= 0; i--)
        {
            n = array [i];
            while (n != null)
            {
                t    = selector.select(n);
                next = n.next;
                if (t == SelectorKit.FALSE)
                {
                    n = next;
                    continue;
                }
                if (t == SelectorKit.TRUE)
                {
                    if (parent != null)
                    {
                        parent.next = next;
                    }
                    else
                    {
                        array [i] = next;
                    }
                    _size--;
                    r = t;
                    n = next;
                    continue;
                }
                if (t == SelectorKit.TRUE_BREAK)
                {
                    if (parent != null)
                    {
                        parent.next = next;
                    }
                    else
                    {
                        array [i] = next;
                    }
                    _size--;
                }
                return(t);
            }
        }
        return(r);
    }
Ejemplo n.º 5
0
    /* methods */
    /** 获取映射到指定键的值 */
    public object get(int key)
    {
        IntKeyHashMapEntry[] array = this.array;
        IntKeyHashMapEntry   n     = array [(key & 0x7fffffff) % array.Length];

        while (n != null)
        {
            if (n.key == key)
            {
                return(n.val);
            }
            n = n.next;
        }
        return(null);
    }