Example #1
0
 /// <summary>
 /// Remove the element corresponding to the key,
 /// given key.hashCode() == index.
 /// </summary>
 /// <returns>
 /// If such element exists, return it.
 /// Otherwise, return null.
 /// </returns>
 private E Remove(int index, K key)
 {
     if (entries[index] == null)
     {
         return(null);
     }
     else
     {
         if (entries[index].Equals(key))
         {
             //remove the head of the linked list
             modification++;
             size--;
             LightWeightGSet.LinkedElement e = entries[index];
             entries[index] = e.GetNext();
             e.SetNext(null);
             return(Convert(e));
         }
         else
         {
             //head != null and key is not equal to head
             //search the element
             LightWeightGSet.LinkedElement prev = entries[index];
             for (LightWeightGSet.LinkedElement curr = prev.GetNext(); curr != null;)
             {
                 if (curr.Equals(key))
                 {
                     //found the element, remove it
                     modification++;
                     size--;
                     prev.SetNext(curr.GetNext());
                     curr.SetNext(null);
                     return(Convert(curr));
                 }
                 else
                 {
                     prev = curr;
                     curr = curr.GetNext();
                 }
             }
             //element not found
             return(null);
         }
     }
 }
Example #2
0
        public override E Get(K key)
        {
            //validate key
            if (key == null)
            {
                throw new ArgumentNullException("key == null");
            }
            //find element
            int index = GetIndex(key);

            for (LightWeightGSet.LinkedElement e = entries[index]; e != null; e = e.GetNext())
            {
                if (e.Equals(key))
                {
                    return(Convert(e));
                }
            }
            //element not found
            return(null);
        }