Exemplo n.º 1
0
        static void Main(string[] args)
        {
            var instance1 = new HashTableItem <String, int>("Name", 1);
            var instance2 = new HashTableItem <String, int>("Surname", 2);

            Console.WriteLine(instance1.Key);
            Console.WriteLine(instance2.Value);

            Console.ReadKey();
        }
 /// <summary>
 /// Ser given item as linked item to this item
 /// </summary>
 /// <param name="item"></param>
 public void SetLinkedItem(HashTableItem <TT> item)
 {
     if (LinkedItem == null)
     {
         LinkedItem = item;
     }
     else
     {
         LinkedItem.SetLinkedItem(item);
     }
 }
Exemplo n.º 3
0
        public void Add(object key, object value)
        {
            int index = Hash(key);

            if (Contains(key))
            {
                throw new DuplicateNameException();
            }

            if (IsResizeRequired(index))
            {
                _array = Resize(index + 1);
            }

            _array[index]       = new HashTableItem();
            _array[index].Value = value;
        }
        private HashTableItem <T> _search(byte[] searchKey)
        {
            var hashTableItem = new HashTableItem <T>(searchKey, default(T), HashTableItems.Length);

            var itemFound = HashTableItems[hashTableItem.Hash];

            if (itemFound != null)
            {
                while (itemFound != null)
                {
                    if (hashTableItem.CompareTo(itemFound) == 0)
                    {
                        break;
                    }
                    itemFound = itemFound.LinkedItem;
                }
            }
            return(itemFound);
        }
        private void _insert(byte[] searchKey, T codeWord)
        {
            var item = new HashTableItem <T>(searchKey, codeWord, HashTableItems.Length);

            if (HashTableItems[item.Hash] == null)
            {
                HashTableItems[item.Hash] = item;
                Count++;
            }
            else
            {
                var existingItem = _search(searchKey);
                if (existingItem == null)
                {
                    HashTableItems[item.Hash].SetLinkedItem(item);
                    Count++;
                }
            }
        }
Exemplo n.º 6
0
    public void Add(TKey key, TValue val)
    {
        if (getLoadFactor() >= LOAD_FACTOR)
        {
            this.resize();
        }

        int index = hash(key);

        if (array[index] == null)
        {
            array[index] = new LinkedList <HashTableItem <TKey, TValue> >();
        }

        var hashTableItem = new HashTableItem <TKey, TValue>(key, val);

        var listNode = new LinkedListNode <HashTableItem <TKey, TValue> >(hashTableItem);

        array[index].AddFirst(listNode);

        size++;
    }
 /// <summary>
 /// Constructor for new HashTable
 /// </summary>
 /// <param name="m">Hash table size</param>
 public HashTable(int m)
 {
     HashTableItems = new HashTableItem <T> [m];
 }
 /// <summary>
 /// Compares this item to given one
 /// </summary>
 /// <param name="other">Other item</param>
 /// <returns>zero when items are equal, otherwise -1</returns>
 public int CompareTo(HashTableItem <TT> other)
 {
     return(SearchKey.Compare(other.SearchKey) ? 0 : -1);
 }
Exemplo n.º 9
0
 private HashTableItem[] Resize(int size)
 {
     HashTableItem[] temp = new HashTableItem[size];
     _array.CopyTo(temp, 0);
     return(temp);
 }