public void Add(T item) { var updates = BuildUpdateTable(item); var current = updates[0]; if (current[0] != null && _comparer.Compare(current[0].Value, item) == 0) { // Duplicate value return; } var newNode = new SkipListNode <T>(item, ChooseRandomHeight(_head.Height + 1)); _count++; if (newNode.Height > _head.Height) { _head.IncrementHeight(); _head[_head.Height - 1] = newNode; } for (int i = 0; i < newNode.Height; i++) { newNode[i] = updates[i][i]; updates[i][i] = newNode; } }
public bool Remove(T value) { var updates = BuildUpdateTable(value); SkipListNode <T> current = updates[0][0]; if (current != null && _comparer.Compare(current.Value, value) == 0) { _count--; for (int i = 0; i < _head.Height; i++) { if (updates[i][i] != current) { break; } else { updates[i][i] = current[i]; } } if (_head[_head.Height - 1] == null) { _head.DecrementHeight(); } return(true); } else { return(false); } }
public SkipList(int randomSeed, IComparer <T> comparer) { _head = new SkipListNode <T>(1); _count = 0; if (randomSeed < 0) { _randomNumber = new Random(); } else { _randomNumber = new Random(randomSeed); } _comparer = comparer == null ? _comparer : comparer; }
public SkipListNode <T> BuildUpdateTable(T item) { var updates = new SkipListNode <T>(_head.Height); var current = _head; for (int i = _head.Height - 1; i > 0; i--) { while (current[i] != null && _comparer.Compare(current[i].Value, item) < 0) { current = current[i]; } updates[i] = current; } return(updates); }