Exemplo n.º 1
0
        //============================================================
        // <T>尝试追加一个内容。</T>
        // <P>内容不允许为空。</P>
        //
        // @param value 内容
        // @return 设置内容的位置
        //============================================================
        public virtual int TryPush(V value)
        {
            // 检查内容
            if (null == value)
            {
                throw new NullReferenceException();
            }
            // 获得内容位置
            int hash  = InnerCode(value);
            int index = hash % _entries.Length;

            RHash.FEntry entry = _entries[index];
            while (null != entry)
            {
                if (entry._hash == hash)
                {
                    if (InnerEquals(value, _values[entry._index]))
                    {
                        return(-1);
                    }
                }
                entry = entry._next;
            }
            // 调整哈希表大小
            if (_count + 1 > _values.Length)
            {
                EnsureSize(_count + 1);
                index = hash % _entries.Length;
            }
            // 新建内容
            _entries[index] = new RHash.FEntry(hash, _count, _entries[index]);
            _values[_count] = value;
            return(_count++);
        }
Exemplo n.º 2
0
        //============================================================
        // <T>移除指定名称的内容。</T>
        //
        // @param name 名称
        //============================================================
        public virtual V Remove(N name)
        {
            // 检查名称
            if (null == name)
            {
                throw new NullReferenceException();
            }
            // 查找节点
            int index = -1;
            int hash  = InnerCode(name);
            int pos   = hash % _entries.Length;

            RHash.FEntry prior = _entries[pos];
            RHash.FEntry entry = prior;
            while (null != entry)
            {
                if (entry._hash == hash)
                {
                    if (InnerEquals(name, _names[entry._index]))
                    {
                        index = entry._index;
                        if (entry == prior)
                        {
                            _entries[pos] = entry._next;
                        }
                        else
                        {
                            prior._next = entry._next;
                        }
                        break;
                    }
                }
                prior = entry;
                entry = entry._next;
            }
            // 有移除操作
            if (-1 != index)
            {
                V old = _values[index];
                // 修正哈希入口列表
                RHash.Remove(_entries, index);
                // 修正名称和内容数组
                int move = _count - index - 1;
                if (move > 0)
                {
                    Array.Copy(_names, index + 1, _names, index, move);
                    Array.Copy(_values, index + 1, _values, index, move);
                }
                // 修正最后元素
                _count--;
                _names[_count]  = default(N);
                _values[_count] = default(V);
                return(old);
            }
            return(default(V));
        }
Exemplo n.º 3
0
 //============================================================
 // <T>获得内容的索引位置。</T>
 //
 // @param value 内容
 // @return 索引位置
 //============================================================
 public int IndexOf(V value)
 {
     if ((null != value) && (_count > 0))
     {
         int          hash  = InnerCode(value);
         int          index = hash % _entries.Length;
         RHash.FEntry entry = _entries[index];
         while (null != entry)
         {
             if (entry._hash == hash)
             {
                 if (InnerEquals(value, _values[entry._index]))
                 {
                     return(entry._index);
                 }
             }
             entry = entry._next;
         }
     }
     return(-1);
 }
Exemplo n.º 4
0
        //============================================================
        // <T>根据名称,设置内容。</T>
        // <P>如果内容存在则替换内容,不存在则新建名称和内容。</P>
        // <P>名称不允许为空。</P>
        //
        // @param name 名称
        // @param value 内容
        //============================================================
        public virtual void Set(N name, V value)
        {
            // 检查名称
            if (null == name)
            {
                throw new NullReferenceException();
            }
            // 获得名称位置
            int hash  = InnerCode(name);
            int index = hash % _entries.Length;

            RHash.FEntry entry = _entries[index];
            while (null != entry)
            {
                if (entry._hash == hash)
                {
                    if (InnerEquals(name, _names[entry._index]))
                    {
                        // 名称存在的情况
                        _values[entry._index] = value;
                        return;
                    }
                }
                entry = entry._next;
            }
            // 调整哈希表大小
            if (_count + 1 > _values.Length)
            {
                EnsureSize(_count + 1);
                index = hash % _entries.Length;
            }
            // 新建名称和内容
            _entries[index] = new RHash.FEntry(hash, _count, _entries[index]);
            _names[_count]  = name;
            _values[_count] = value;
            _count++;
        }