public bool Contains(TValue value, TPriority priority) { if (priority == null) { throw new ArgumentNullException(); } SkipListNode <TPriority, TValue> currentNode = head; for (int level = currentMaxLevel - 1; level >= 0; level--) { while (currentNode[level] != head && comparer.Compare(priority, currentNode[level].Key) > 0) { currentNode = currentNode[level]; } } currentNode = currentNode[0]; while (comparer.Compare(priority, currentNode.Key) == 0) { if (EqualityComparer <TValue> .Default.Equals(value, currentNode.Value)) { return(true); } currentNode = currentNode[0]; } return(false); }
void ICollection <KeyValuePair <TKey, TValue> > .CopyTo(KeyValuePair <TKey, TValue>[] array, int arrayIndex) { SkipListNode <TKey, TValue> current = this.head[0]; for (int i = 0; i < this.count; i++) { array[i + arrayIndex] = (KeyValuePair <TKey, TValue>)current; current = current[0]; } }
public bool MoveNext() { if (currentNode[0] != list.head) { currentNode = currentNode[0]; return(true); } else { return(false); } }
protected void Insert(TKey key, TValue value, bool addNew) { if (key == null) { throw new ArgumentNullException(); } SkipListNode <TKey, TValue> currentNode = head; SkipListNode <TKey, TValue>[] toUpdate = new SkipListNode <TKey, TValue> [currentMaxLevel]; for (int level = currentMaxLevel - 1; level >= 0; level--) { while (currentNode[level].Key != null && comparer.Compare(key, currentNode[level].Key) > 0) { currentNode = currentNode[level]; } toUpdate[level] = currentNode; } currentNode = currentNode[0]; if (comparer.Compare(currentNode.Key, key) == 0) { if (addNew) { throw new ArgumentException(""); } else { currentNode.Value = value; } } else { int newLevel = this.NewLevel(); SkipListNode <TKey, TValue> newNode = new SkipListNode <TKey, TValue>(key, value, newLevel); if (newLevel == currentMaxLevel) { head[newLevel - 1] = newNode;//Necessary? currentMaxLevel++; } for (int i = 0; i < newLevel; i++) { newNode[i] = toUpdate[i][i]; toUpdate[i][i] = newNode; } this.count++; } }
public bool Contains(TValue value) { SkipListNode <TPriority, TValue> currentNode = head; while (currentNode[0] != head) { if (EqualityComparer <TValue> .Default.Equals(value, currentNode.Value)) { return(true); } currentNode = currentNode[0]; } return(false); }
public bool TryGetValue(TKey key, out TValue value) { SkipListNode <TKey, TValue> node = GetNode(key); if (node != null) { value = node.Value; return(true); } else { value = default(TValue); return(false); } }
public SkipListPriorityQueue(int maxLevel, IComparer <TPriority> comparer, double propability, Random random) { this.propability = propability; this.random = random; this.comparer = comparer; this.maxLevel = maxLevel; this.currentMaxLevel = 1; this.count = 0; this.version = 0; this.head = new SkipListNode <TPriority, TValue>(maxLevel); for (int i = 0; i < maxLevel; i++) { this.head[i] = head; } }
/// <summary> /// Constructs a new instance of skiplist. /// </summary> /// <param name="maxLevel">Maximun level for skiplist to achieve.</param> public SkipList(int maxLevel) { random = new Random(); propability = 0.5; this.maxLevel = maxLevel; currentMaxLevel = 0; count = 0; comparer = Comparer <TKey> .Default; head = new SkipListNode <TKey, TValue>(maxLevel); for (int i = 0; i < maxLevel; i++) { head[i] = head; } random.Next(); }
public void Enqueue(TValue value, TPriority priority) { if (priority == null) { throw new ArgumentNullException(); } SkipListNode <TPriority, TValue> currentNode = head; SkipListNode <TPriority, TValue>[] toUpdate = new SkipListNode <TPriority, TValue> [currentMaxLevel]; for (int level = currentMaxLevel - 1; level >= 0; level--) { while (currentNode[level] != head && comparer.Compare(priority, currentNode[level].Key) > 0) { currentNode = currentNode[level]; } toUpdate[level] = currentNode; } currentNode = currentNode[0]; int newLevel = this.NewLevel(); SkipListNode <TPriority, TValue> newNode = new SkipListNode <TPriority, TValue>(priority, value, newLevel); if (newLevel == currentMaxLevel) { //head[newLevel - 1] = newNode; currentMaxLevel++; } for (int i = 0; i < newLevel; i++) { newNode[i] = toUpdate[i][i]; toUpdate[i][i] = newNode; } if (!newNode.Validate()) { throw new FormatException("Nawt"); } this.version++; count++; }
public bool Remove(TValue value, TPriority priority) { if (priority == null) { throw new ArgumentNullException(); } SkipListNode <TPriority, TValue> currentNode = head; SkipListNode <TPriority, TValue>[] toUpdate = new SkipListNode <TPriority, TValue> [currentMaxLevel]; for (int level = currentMaxLevel - 1; level >= 0; level--) { while (currentNode[level] != head && comparer.Compare(priority, currentNode[level].Key) > 0) { currentNode = currentNode[level]; } toUpdate[level] = currentNode; } currentNode = currentNode[0]; while (comparer.Compare(priority, currentNode.Key) == 0) { if (EqualityComparer <TValue> .Default.Equals(value, currentNode.Value)) { for (int i = 0; i < currentNode.AmountOfNodes; i++) { toUpdate[i][i] = currentNode[i]; currentNode[i] = null; } UpdateLevel(currentMaxLevel); count--; this.version++; return(true); } for (int i = 0; i < currentNode.AmountOfNodes; i++) { toUpdate[i] = currentNode; } currentNode = currentNode[0]; } return(false); }
public TValue Dequeue() { if (count == 0) { throw new InvalidOperationException("Empty"); } SkipListNode <TPriority, TValue> first = head[0]; for (int i = 0; i < first.AmountOfNodes; i++) { head[i] = first[i]; } UpdateLevel(currentMaxLevel); this.version++; count--; return(first.Value); }
public bool Remove(TKey key) { if (key == null) { throw new ArgumentNullException(); } SkipListNode <TKey, TValue> currentNode = head; SkipListNode <TKey, TValue>[] toUpdate = new SkipListNode <TKey, TValue> [currentMaxLevel]; for (int level = currentMaxLevel - 1; level >= 0; level--) { while (currentNode[level].Key != null && comparer.Compare(key, currentNode[level].Key) > 0) { currentNode = currentNode[level]; } toUpdate[level] = currentNode; } currentNode = currentNode[0]; if (comparer.Compare(currentNode.Key, key) == 0) { for (int i = 0; i < currentMaxLevel; i++) { if (toUpdate[i][i] != currentNode) { break; } toUpdate[i][i] = currentNode[i]; } while (currentMaxLevel > 0 && head[currentMaxLevel - 1] == head) { currentMaxLevel--; } return(true); } else { return(false); } }
public TValue this[TKey key] { get { SkipListNode <TKey, TValue> node = GetNode(key); if (node != null) { return(node.Value); } else { throw new KeyNotFoundException("Key not found."); } } set { Insert(key, value, false); } }
protected SkipListNode <TKey, TValue> GetNode(TKey key) { if (key == null) { throw new ArgumentNullException(); } SkipListNode <TKey, TValue> currentNode = head; for (int level = currentMaxLevel - 1; level >= 0; level--) { while (currentNode[level].Key != null && comparer.Compare(key, currentNode[level].Key) > 0) { currentNode = currentNode[level]; } } currentNode = currentNode[0]; return(comparer.Compare(currentNode.Key, key) == 0 ? currentNode : null); }
public void Dispose() { currentNode = null; list = null; }
public void Reset() { currentNode = list.head; }