Пример #1
0
        public HashTableInternal(int initCapacity)
        {
            this.arr  = new ChainList <KeyValuePair <TKey, TValue> > [initCapacity];
            this.list = new ChainList <KeyValuePair <TKey, TValue> >();

            this.m = GetModulo();
        }
Пример #2
0
        public uint Add(TKey key, TValue value)
        {
            uint i;
            ChainList <KeyValuePair <TKey, TValue> > chainList;

            var node = GetNode(key, out i, out chainList);

            if (node != null)
            {
                throw new DuplicateKeyException("Key \"" + node.element.Key + "\" 已经存在 。");
            }

            if (chainList == null)
            {
                chainList   = new ChainList <KeyValuePair <TKey, TValue> >();
                this.arr[i] = chainList;
            }

            var pair = new KeyValuePair <TKey, TValue>(key, value);

            chainList.Add(pair);

            node          = new ChainListNode <KeyValuePair <TKey, TValue> >(pair);
            pair.listNode = node;
            this.list.Add(node);

            return(i);
        }
Пример #3
0
        private ChainListNode <KeyValuePair <TKey, TValue> > GetNode(TKey key, out uint i, out ChainList <KeyValuePair <TKey, TValue> > chainList)
        {
            uint hash = (uint)key.GetHashCode();

            i = hash / m;

            chainList = arr[i];

            if (chainList == null)
            {
                return(null);
            }

            var node = chainList.head;

            if (node == null)
            {
                return(null);
            }

            while (true)
            {
                if (node.element.Key.Equals(key))
                {
                    return(node);
                }

                node = node.next;

                if (node == null)
                {
                    return(null);
                }
            }
        }
Пример #4
0
 public HashTableEnumerator(ChainList <KeyValuePair <TKey, TValue> > list)
 {
     this.list = list;
 }