public void Add(T item) { int lvl = coinflip.Next(height); SkipNode <T> add = new SkipNode <T>(lvl + 1, item); SkipNode <T> iter = head; for (int i = height - 1; i >= 0; i--) { while (iter.Next[i] != null) { int diff = item.CompareTo(iter.Next[i].Item); if (diff == 0) { iter.Next[i] = iter.Next[i].Next[i]; continue; } if (diff < 0) { break; } iter = iter.Next[i]; } if (i <= lvl) { add.Next[i] = iter.Next[i]; iter.Next[i] = add; } } }
public SkipList(int height = 32) { this.height = height; this.head = new SkipNode <T>(height); Clear(); }
public bool Remove(T item) { SkipNode <T> iter = head; bool found = false; for (int i = height - 1; i >= 0; i--) { while (iter.Next[i] != null) { int diff = item.CompareTo(iter.Next[i].Item); if (diff == 0) { found = true; iter.Next[i] = iter.Next[i].Next[i]; continue; } if (diff < 0) { break; } iter = iter.Next[i]; } } return(found); }
public IEnumerator <T> GetEnumerator() { SkipNode <T> iter = head; while (iter.Next[0] != null) { yield return(iter.Next[0].Item); iter = iter.Next[0]; } }
public bool Contains(T item) { SkipNode <T> iter = head; for (int i = height - 1; i >= 0; i--) { while (iter.Next[i] != null) { int diff = item.CompareTo(iter.Next[i].Item); if (diff == 0) { return(true); } if (diff < 0) { break; } iter = iter.Next[i]; } } return(false); }