Example #1
0
        public void Add(TKey key, TVal val)
        {
            HashMapNode <TKey, TVal> node = new HashMapNode <TKey, TVal>(key, val);
            int hashCode = Math.Abs(key.GetHashCode());
            int index    = GetIndex(hashCode);


            if (HashMapNode[index] != null)
            {
                HashMapNode <TKey, TVal> hashTemp = HashMapNode[index];

                do
                {
                    if (hashTemp.hashCode == hashCode)
                    {
                        Console.WriteLine("Key is already exists");
                        return;
                    }
                    else
                    {
                        hashTemp = hashTemp.nextNode;
                    }
                } while (hashTemp.nextNode != null);


                hashTemp.nextNode = node;
            }
            else
            {
                HashMapNode[index] = node;
            }
        }
Example #2
0
        public TVal GetValue(TKey key)
        {
            int hashCode = Math.Abs(key.GetHashCode());
            int index    = GetIndex(hashCode);

            if (HashMapNode[index] != null)
            {
                HashMapNode <TKey, TVal> node = HashMapNode[index];
                do
                {
                    if (node.hashCode == hashCode)
                    {
                        return(node.data);
                    }
                    else
                    {
                        node = node.nextNode;
                    }
                } while (node.nextNode != null);
                return(default(TVal));
            }
            else
            {
                Console.WriteLine("Key not exists");
                return(default(TVal));
            }
        }
Example #3
0
 public HashMapNode(TKey key, TVal val)
 {
     data     = val;
     nextNode = null;
     key      = key;
     hashCode = Math.Abs(key.GetHashCode());
 }