コード例 #1
0
ファイル: KeyValue.cs プロジェクト: Luke-MacNeil/COSC286_A2
        public override bool Equals(object obj)
        {
            KeyValue <K, V> kv = (KeyValue <K, V>)obj;

            return(this.Key.CompareTo(kv.Key) == 0);
        }
コード例 #2
0
        public override void Add(K key, V vValue)
        {
            // How many attepmts were made to increment
            int iAttempt = 1;

            // Get the initial hash of the key
            int iInitialHash = HashFunction(key);

            // The current location
            int iCurrentLocation = iInitialHash;

            // Wrap the key and value in a KeyValue object
            KeyValue <K, V> kvNew = new KeyValue <K, V>(key, vValue);

            // Position to add the new element
            int iPositionToAdd = -1;

            // while the data at the current location is not null (has a value), this will
            // find an empty location to add the current item
            while (oDataArray[iCurrentLocation] != null)
            {
                // If the current item is a key-value
                if (oDataArray[iCurrentLocation].GetType() == typeof(KeyValue <K, V>))
                {
                    // Check to see if the current value is the same key as the value we are adding
                    KeyValue <K, V> kv = (KeyValue <K, V>)oDataArray[iCurrentLocation];

                    if (kv.Equals(kvNew))
                    {
                        throw new ApplicationException("Item alrwady exists");
                    }
                }
                // It is a tobmstone
                else
                {
                    // If this is the first tombstone
                    if (iPositionToAdd == -1)
                    {
                        iPositionToAdd = iCurrentLocation;
                    }
                }

                // Increment to the next location
                iCurrentLocation = iInitialHash + GetIncrement(iAttempt++, key);

                // Loop back up to the top of the table if we fall off the bottom
                iCurrentLocation %= HTSize;


                // For stats
                iNumCollisions++;
            }

            // Check to see if we didn't hit a tombstone
            if (iPositionToAdd == -1)
            {
                iPositionToAdd = iCurrentLocation;
            }

            // Add the KeyValue to the current location
            oDataArray[iPositionToAdd] = kvNew;

            // Increment the count
            iCount++;


            // If the table is overloaded, then expand it
            if (IsOverLoaded())
            {
                ExpandHashTable();
            }
        }
コード例 #3
0
ファイル: KeyValue.cs プロジェクト: Luke-MacNeil/COSC286_A2
 public int CompareTo(KeyValue <K, V> other)
 {
     return(this.Key.CompareTo(other.Key));
 }