Ejemplo n.º 1
0
        // TODO Write method Add
        public bool Add(string key)
        {
            MyHashNode NewNode = new MyHashNode(key);

            NewNode.ProbeCount++;

            if (_HashTable[NewNode.InitialAddress].HashKey == null || _HashTable[NewNode.InitialAddress].HashKey == NewNode.HashKey)
            {
                NewNode.CurrentLocation             = NewNode.InitialAddress;
                _HashTable[NewNode.CurrentLocation] = NewNode;
                _CurrentCapacity++;
                return(true);
            }
            else
            {
                // Collision has occured
                do
                {
                    bool Result = _HandleCollision(ref NewNode);
                    if (Result)
                    {
                        // Empty space was found
                        _HashTable[NewNode.CurrentLocation] = NewNode;
                        _CurrentCapacity++;
                        return(true);
                    }
                } while (NewNode.ProbeCount < _HashTable.Length);

                // Table was full
                return(false);
            }
        }
Ejemplo n.º 2
0
        // Linear collision handling
        private bool _HandleCollision(ref MyHashNode CollidedNode)
        {
            int _CollisionAddress = CollidedNode.InitialAddress + CollidedNode.ProbeCount;

            CollidedNode.ProbeCount += 1;

            // Wrap around to front of table if needed
            if (!(_CollisionAddress < _HashTable.Length))
            {
                _CollisionAddress -= _HashTable.Length;
            }

            // If wrapped around to original location, return false
            if (_CollisionAddress == CollidedNode.InitialAddress)
            {
                CollidedNode.CurrentLocation = -1;
                return(false);
            }

            if (_HashTable[_CollisionAddress].HashKey == null)
            {
                // Space is available
                CollidedNode.CurrentLocation = _CollisionAddress;
                return(true);
            }
            else
            {
                // Recursion
                // Another collision has occured
                return(_HandleCollision(ref CollidedNode));
            }
        }
Ejemplo n.º 3
0
 public MyHashTable(int Length) : this()
 {
     _HashTable  = new MyHashNode[Length];
     this.Length = Length;
     for (int node = 0; node < _HashTable.Length; node++)
     {
         _HashTable[node] = new MyHashNode();
     }
 }
Ejemplo n.º 4
0
        // Search HashTable for location of key, returns -1 if not found.
        public MyHashNode Get(string Key)
        {
            MyHashNode NewNode = new MyHashNode(Key);

            NewNode.ProbeCount++;

            if (_HashTable[NewNode.InitialAddress].HashKey == Key)
            {
                NewNode.CurrentLocation = NewNode.InitialAddress;
                return(NewNode);
            }
            else
            {
                // Begin search
                int _SearchAddress;
                // While current comparison is initialized but isn't the InitialAddress
                do
                {
                    _SearchAddress = NewNode.InitialAddress + NewNode.ProbeCount;
                    if (_SearchAddress >= _HashTable.Length)
                    {
                        _SearchAddress -= _HashTable.Length;
                    }

                    if (_HashTable[_SearchAddress].HashKey == Key)
                    {
                        // Key found at different address
                        return(_HashTable[_SearchAddress]);
                    }
                    else
                    {
                        // Compare next key
                        NewNode.ProbeCount++;
                    }
                } while (_HashTable[_SearchAddress] != null || _SearchAddress != NewNode.InitialAddress);
                // Key not found in table
                return(new MyHashNode());
            }
        }