Exemplo n.º 1
0
        public override E Put(E element)
        {
            //validate element
            if (element == null)
            {
                throw new ArgumentNullException("Null element is not supported.");
            }
            if (!(element is LightWeightGSet.LinkedElement))
            {
                throw new HadoopIllegalArgumentException("!(element instanceof LinkedElement), element.getClass()="
                                                         + element.GetType());
            }
            LightWeightGSet.LinkedElement e = (LightWeightGSet.LinkedElement)element;
            //find index
            int index = GetIndex(element);
            //remove if it already exists
            E existing = Remove(index, element);

            //insert the element to the head of the linked list
            modification++;
            size++;
            e.SetNext(entries[index]);
            entries[index] = e;
            return(existing);
        }
Exemplo n.º 2
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);
         }
     }
 }