/// <summary>Add given element to the hash table</summary> /// <returns>true if the element was not present in the table, false otherwise</returns> protected internal virtual bool AddElem(T element) { // validate element if (element == null) { throw new ArgumentException("Null element is not supported."); } // find hashCode & index int hashCode = element.GetHashCode(); int index = GetIndex(hashCode); // return false if already present if (GetContainedElem(index, element, hashCode) != null) { return(false); } modification++; size++; // update bucket linked list LightWeightHashSet.LinkedElement <T> le = new LightWeightHashSet.LinkedElement <T>( element, hashCode); le.next = entries[index]; entries[index] = le; return(true); }
/// <summary>Resize the internal table to given capacity.</summary> private void Resize(int cap) { int newCapacity = ComputeCapacity(cap); if (newCapacity == this.capacity) { return; } this.capacity = newCapacity; this.expandThreshold = (int)(capacity * maxLoadFactor); this.shrinkThreshold = (int)(capacity * minLoadFactor); this.hash_mask = capacity - 1; LightWeightHashSet.LinkedElement <T>[] temp = entries; entries = new LightWeightHashSet.LinkedElement[capacity]; for (int i = 0; i < temp.Length; i++) { LightWeightHashSet.LinkedElement <T> curr = temp[i]; while (curr != null) { LightWeightHashSet.LinkedElement <T> next = curr.next; int index = GetIndex(curr.hashCode); curr.next = entries[index]; entries[index] = curr; curr = next; } } }
public LinkedElement(T elem, int hash) { // reference to the next entry within a bucket linked list //hashCode of the element this.element = elem; this.next = null; this.hashCode = hash; }
/// <summary>Remove the element corresponding to the key.</summary> /// <returns>If such element exists, return true. Otherwise, return false.</returns> public virtual bool Remove(object key) { // validate key if (key == null) { throw new ArgumentException("Null element is not supported."); } LightWeightHashSet.LinkedElement <T> removed = RemoveElem((T)key); ShrinkIfNecessary(); return(removed == null ? false : true); }
/// <summary>Get array.length elements from the set, and put them into the array.</summary> public virtual T[] PollToArray(T[] array) { int currentIndex = 0; LightWeightHashSet.LinkedElement <T> current = null; if (array.Length == 0) { return(array); } if (array.Length > size) { array = (T[])System.Array.CreateInstance(array.GetType().GetElementType(), size); } // do fast polling if the entire set needs to be fetched if (array.Length == size) { for (int i = 0; i < entries.Length; i++) { current = entries[i]; while (current != null) { array[currentIndex++] = current.element; current = current.next; } } this.Clear(); return(array); } bool done = false; int currentBucketIndex = 0; while (!done) { current = entries[currentBucketIndex]; while (current != null) { array[currentIndex++] = current.element; current = current.next; entries[currentBucketIndex] = current; size--; modification++; if (currentIndex == array.Length) { done = true; break; } } currentBucketIndex++; } ShrinkIfNecessary(); return(array); }
/// <summary>Check if the set contains given element at given index.</summary> /// <remarks> /// Check if the set contains given element at given index. If it /// does, return that element. /// </remarks> /// <returns>the element, or null, if no element matches</returns> protected internal virtual T GetContainedElem(int index, T key, int hashCode) { for (LightWeightHashSet.LinkedElement <T> e = entries[index]; e != null; e = e.next) { // element found if (hashCode == e.hashCode && e.element.Equals(key)) { return(e.element); } } // element not found return(null); }
/// <summary>Remove all elements from the set and return them.</summary> /// <remarks>Remove all elements from the set and return them. Clear the entries.</remarks> public virtual IList <T> PollAll() { IList <T> retList = new AList <T>(size); for (int i = 0; i < entries.Length; i++) { LightWeightHashSet.LinkedElement <T> current = entries[i]; while (current != null) { retList.AddItem(current.element); current = current.next; } } this.Clear(); return(retList); }
/// <summary>Print detailed information of this object.</summary> public virtual void PrintDetails(TextWriter @out) { @out.Write(this + ", entries = ["); for (int i = 0; i < entries.Length; i++) { if (entries[i] != null) { LightWeightHashSet.LinkedElement <T> e = entries[i]; @out.Write("\n " + i + ": " + e); for (e = e.next; e != null; e = e.next) { @out.Write(" -> " + e); } } } @out.WriteLine("\n]"); }
/// <summary>Remove the element corresponding to the key, given key.hashCode() == index. /// </summary> /// <returns>If such element exists, return true. Otherwise, return false.</returns> protected internal virtual LightWeightHashSet.LinkedElement <T> RemoveElem(T key) { LightWeightHashSet.LinkedElement <T> found = null; int hashCode = key.GetHashCode(); int index = GetIndex(hashCode); if (entries[index] == null) { return(null); } else { if (hashCode == entries[index].hashCode && entries[index].element.Equals(key)) { // remove the head of the bucket linked list modification++; size--; found = entries[index]; entries[index] = found.next; } else { // head != null and key is not equal to head // search the element LightWeightHashSet.LinkedElement <T> prev = entries[index]; for (found = prev.next; found != null;) { if (hashCode == found.hashCode && found.element.Equals(key)) { // found the element, remove it modification++; size--; prev.next = found.next; found.next = null; break; } else { prev = found; found = found.next; } } } } return(found); }
public override T Next() { if (this._enclosing.modification != this.startModification) { throw new ConcurrentModificationException("modification=" + this._enclosing.modification + " != startModification = " + this.startModification); } if (this.next == null) { throw new NoSuchElementException(); } T e = this.next.element; // find the next element LightWeightHashSet.LinkedElement <T> n = this.next.next; this.next = n != null ? n : this.NextNonemptyEntry(); return(e); }
public virtual U[] ToArray <U>(U[] a) { if (a == null) { throw new ArgumentNullException("Input array can not be null"); } if (a.Length < size) { a = (U[])System.Array.CreateInstance(a.GetType().GetElementType(), size); } int currentIndex = 0; for (int i = 0; i < entries.Length; i++) { LightWeightHashSet.LinkedElement <T> current = entries[i]; while (current != null) { a[currentIndex++] = (U)current.element; current = current.next; } } return(a); }
/// <summary>Remove and return n elements from the hashtable.</summary> /// <remarks> /// Remove and return n elements from the hashtable. /// The order in which entries are removed is unspecified, and /// and may not correspond to the order in which they were inserted. /// </remarks> /// <returns>first element</returns> public virtual IList <T> PollN(int n) { if (n >= size) { return(PollAll()); } IList <T> retList = new AList <T>(n); if (n == 0) { return(retList); } bool done = false; int currentBucketIndex = 0; while (!done) { LightWeightHashSet.LinkedElement <T> current = entries[currentBucketIndex]; while (current != null) { retList.AddItem(current.element); current = current.next; entries[currentBucketIndex] = current; size--; modification++; if (--n == 0) { done = true; break; } } currentBucketIndex++; } ShrinkIfNecessary(); return(retList); }