/// <summary>Clear the collection /// </summary> public void Clear() { numElements = 0; first = null; last = null; hash.Clear(); }
/// <summary>Get the n'th element of the list</summary> internal HashedListElement getNthElement(int n) { if (n < 0) { throw new System.Exception("Invalid index"); } else { HashedListElement current = first; while (n > 0 && current.next != null) { n -= 1; current = current.next; } if (n > 0) { throw new System.Exception("Index beyond end of list"); } if (current == null) { throw new System.Exception("Empty list"); } return(current); } }
/// <summary> /// Insert an element in front of pos. /// If pos is null, add to the end of the list. /// </summary> /// <param name="pos">The element in front of which to add the new element</param> /// <param name="key">The key of the new element</param> /// <param name="val">The value of the new element</param> public void Insert(HashedListElement pos, Object key, Object val) { if (pos == null) { Add(key, val); } else { HashedListElement prev = pos.prev; HashedListElement newElement = new HashedListElement(key, val, prev, pos); pos.prev = newElement; if (prev != null) { prev.next = newElement; } if (first == pos) { first = newElement; } // Put a pointer in the hash. if (key != null) { hash[key] = newElement; } numElements += 1; } }
/// <summary>Remove an element from the list. /// This method is also called by the HashedListCursor. /// </summary> /// <param name="e">The element to be removed.</param> private void RemoveElement(HashedListElement e) { if (e == null) { return; } if (e.prev != null) { e.prev.next = e.next; } else { first = e.next; } if (e.next != null) { e.next.prev = e.prev; } else { last = e.prev; } if (e.Key != null) { hash.Remove(e.Key); } numElements -= 1; }
internal HashedListCursor(HashedList enclosingInstance, HashedListElement start) { this.enclosingInstance = enclosingInstance; startElement = start; beforeStart = true; current = null; last = null; }
/// <summary>Remove a keyed object from the list. Unkeyed /// objects can be removed from the list using a /// HashedListCursor.</summary> public void Remove(System.Object key) { if (key == null) { return; } HashedListElement h = (HashedListElement)hash[key]; if (h != null) { RemoveElement(h); } }
/// <summary>Return a Cursor starting with the n'th entry.</summary> public HashedListCursor GetCursor(int n) { if (n == 0 && numElements == 0) { return(new HashedListCursor(this, first)); } HashedListElement e = getElement(n); HashedListCursor c = (HashedListCursor)GetCursor(); // c.Current = e; c.Key = e.Key; return(c); }
/// <summary>Remove the current entry. Note that remove can /// be called only after a call to next, and only once per such /// call. Remove cannot be called after a call to prev.</summary> public void Remove() { if (current == null) { throw new System.SystemException("Removed called in invalid cursor state"); } else { HashedListElement e = current.prev; Enclosing_Instance.RemoveElement(current); current = e; last = current == null ? null : current.prev; } }
/// <summary>Replace the key of a given element.</summary> /// <param name="oldKey"> The previous key. This key must /// be present in the hash.</param> /// <param name="newKey"> The new key. This key /// must not be present in the hash.</param> /// <returns>if the replacement was successful.</returns> public bool ReplaceKey(Object oldKey, Object newKey) { if (!hash.ContainsKey(oldKey) || hash.ContainsKey(newKey)) { return(false); } HashedListElement e = (HashedListElement)hash[oldKey]; hash.Remove(oldKey); e.Key = newKey; hash[newKey] = e; return(true); }
public bool ContainsValue(Object val) { if (val == null) { return(false); } bool result = hash.ContainsValue(val); for (HashedListElement e = first; (!result) && e != null;) { result = val.Equals(e.Value); e = e.next; } return(result); }
/// <summary>Add an element to the list.</summary> /// <param name="pos">The element after which the current element /// be placed. If pos is null put the element at the end of the list.</param> /// <param name="key">The hash key for the new object. This may be null /// for an unkeyed entry.</param> /// <param name="reference">The actual object being stored.</param> internal HashedListElement Add(HashedListElement pos, System.Object key, System.Object val) { // First check if we need to delete another reference. if (key != null) { // Does not do anything if key not found. Remove((HashedListElement)hash[key]); } HashedListElement e = new HashedListElement(); e.Key = key; e.Value = val; // Now put it in the list. if (pos == null) { // At the end... e.prev = last; if (last != null) { last.next = e; } else { // Empty list... first = e; } last = e; e.next = null; } else { if (pos.next == null) { // At the end... e.next = null; e.prev = pos; pos.next = e; last = e; } else { // In the middle... e.prev = pos; e.next = pos.next; pos.next = e; e.next.prev = e; } } // Put a pointer in the hash. if (key != null) { hash[key] = e; } numElements += 1; return(e); }
/// <summary>Add a keyed entry at the current location. The new entry is inserted /// before the entry that would be returned in the next invocation of /// 'next'. The return value for that call is unaffected. /// Note: this method is not in the IEnumerator interface. /// </summary> public void Add(System.Object key, System.Object val) { HashedListElement newObj = Enclosing_Instance.Add(current, key, val); MoveNext(); }
public HashedListElement(Object key, Object val, HashedListElement prev, HashedListElement next) : this(key, val) { this.prev = prev; this.next = next; }