// helper methods private bool addInternal(T element, OccurrenceMode checkOccurrence) { if (this.treeValue == null) { this.treeValue = new RBCell(element); this.incCount(); } else { RBCell t = this.treeValue; for (;;) { int diff = this.cmpValue.Compare(element, t.element()); if (diff == 0 && checkOccurrence == OccurrenceMode.AddIfMissing) { return(false); } if (diff == 0 && checkOccurrence == OccurrenceMode.OverwriteIfExisting) { t.element(element); return(false); } if (diff <= 0) { if (t.left() != null) { t = t.left(); } else { this.treeValue = t.insertLeft( new RBCell(element), this.treeValue); this.incCount(); return(true); } } else { if (t.right() != null) { t = t.right(); } else { this.treeValue = t.insertRight( new RBCell(element), this.treeValue); this.incCount(); return(true); } } } } return(true); }
// <summary>Return number of nodes of current sub-tree containing // element. Uses IComparer <paramref name='cmp '/> to find and to // check equality.</summary> // <param name='element'></param> // <param name='cmp'>An IComparer object.</param> // <returns>A 32-bit signed integer.</returns> public int count(T element, IComparer <T> cmp) { var c = 0; RBCell t = this; while (t != null) { int diff = cmp.Compare(element, t.element()); if (diff == 0) { ++c; if (t.leftValue == null) { t = t.rightValue; } else if (t.rightValue == null) { t = t.leftValue; } else { c += t.rightValue.count(element, cmp); t = t.leftValue; } } else { t = (diff < 0) ? t.leftValue : t.rightValue; } } return(c); }
// <summary>Copies this object's data to a new array.</summary> // <param name='array'>A T[] object.</param> // <param name='arrayIndex'>Starting index to copy to.</param> public void CopyTo(T[] array, int arrayIndex) { if (array == null) { throw new ArgumentNullException("array"); } if (arrayIndex < 0) { throw new ArgumentException("arrayIndex (" + arrayIndex + ") is less than 0"); } if (this.treeValue != null) { RBCell t = this.treeValue.leftmost(); while (t != null && arrayIndex < array.Length) { T v = t.element(); if (arrayIndex >= 0 && arrayIndex < array.Length) { array[arrayIndex] = v; } ++arrayIndex; t = t.successor(); } } }
// <summary>Implements collections.UpdatableCollection.take. Time // complexity: O(log n). Takes the least element. @see // collections.UpdatableCollection#take.</summary> // <returns>A T object.</returns> public T Pop() { if (this.countValue != 0) { RBCell p = this.treeValue.leftmost(); T v = p.element(); this.treeValue = p.delete(this.treeValue); this.decCount(); return(v); } return(default(T)); }
private IEnumerable <T> Iterator() { if (this.treeValue != null) { RBCell t = this.treeValue.leftmost(); while (t != null) { T v = t.element(); yield return(v); t = t.successor(); } } }
public bool Find(T element, out T outval) { if (this.countValue == 0) { outval = default(T); return(false); } RBCell cell = this.treeValue.find(element, this.cmpValue); if (cell == null) { outval = default(T); return(false); } outval = cell.element(); return(true); }
// <summary>Return node of current sub-tree containing element as // element(), if it exists, else null. Uses IComparer <paramref // name='cmp '/> to find and to check equality.</summary> // <param name='element'></param> // <param name='cmp'>An IComparer object.</param> // <returns>A RBCell object.</returns> public RBCell find(T element, IComparer <T> cmp) { RBCell t = this; for (;;) { int diff = cmp.Compare(element, t.element()); if (diff == 0) { return(t); } t = (diff < 0) ? t.leftValue : t.rightValue; if (t == null) { return(null); } } }