AvlNode <T> RemoveMin(out AvlNode <T> min) { if (IsEmpty) { min = Empty; return(Empty); } if (left.IsEmpty) { //We are the minimum: min = this; return(right); } //Go down: var newlt = ToMutableIfNecessary(left).RemoveMin(out min); var newroot = NewOrMutate(Value, newlt, right); return(newroot.FixRootBalance()); }
public AvlNode <T> SetItem(int index, T val) { AvlNode <T> newlt = left; AvlNode <T> newgt = right; if (index < left._count) { newlt = ToMutableIfNecessary(left).SetItem(index, val); } else if (index > left._count) { newgt = ToMutableIfNecessary(right).SetItem(index - left._count - 1, val); } else { return(NewOrMutate(val, newlt, newgt)); } return(NewOrMutate(Value, newlt, newgt)); }
AvlNode <T> RemoveMax(out AvlNode <T> max) { if (IsEmpty) { max = Empty; return(Empty); } if (right.IsEmpty) { //We are the max: max = this; return(left); } else { //Go down: var newgt = ToMutableIfNecessary(right).RemoveMax(out max); var newroot = NewOrMutate(Value, left, newgt); return(newroot.FixRootBalance()); } }
/// <summary> /// Return a new tree with the key-value pair inserted /// If the key is already present, it replaces the value /// This operation is O(Log N) where N is the number of keys /// </summary> public AvlNode <T> InsertIntoNew(int index, T val) { if (IsEmpty) { return(new AvlNode <T>(val)); } AvlNode <T> newlt = left; AvlNode <T> newgt = right; if (index <= left._count) { newlt = ToMutableIfNecessary(left).InsertIntoNew(index, val); } else { newgt = ToMutableIfNecessary(right).InsertIntoNew(index - left._count - 1, val); } var newroot = NewOrMutate(Value, newlt, newgt); return(newroot.FixRootBalance()); }
internal ImmutableDictionary(AvlNode <KeyValuePair <TKey, TValue> > root, IEqualityComparer <TKey> keyComparer, IEqualityComparer <TValue> valueComparer) { this.root = root; this.keyComparer = keyComparer; this.valueComparer = valueComparer; }
public void Clear() { root = new AvlNode <KeyValuePair <TKey, TValue> >().ToMutable(); }
public void Add(KeyValuePair <TKey, TValue> item) { root = root.InsertIntoNew(item, CompareKV); }
internal Builder(AvlNode <KeyValuePair <TKey, TValue> > root, IEqualityComparer <TKey> keyComparer, IEqualityComparer <TValue> valueComparer) { this.root = root.ToMutable(); this.keyComparer = keyComparer; this.valueComparer = valueComparer; }
internal Builder(AvlNode <T> immutableRoot, IEqualityComparer <T> comparer) { root = immutableRoot.ToMutable(); valueComparer = comparer; }
internal ImmutableList(AvlNode <T> root, IEqualityComparer <T> equalityComparer) { this.root = root; this.valueComparer = equalityComparer; }
public override AvlNode <T> ToMutableIfNecessary(AvlNode <T> node) { return(node.ToMutable()); }
public MutableAvlNode(T val, AvlNode <T> lt, AvlNode <T> gt) : base(val, lt, gt) { }
public virtual AvlNode <T> ToMutableIfNecessary(AvlNode <T> node) { return(node); }
public virtual AvlNode <T> NewOrMutate(T newValue, AvlNode <T> newLeft, AvlNode <T> newRight) { return(new AvlNode <T>(newValue, newLeft, newRight)); }
public override AvlNode <T> NewOrMutate(T newValue, AvlNode <T> newLeft, AvlNode <T> newRight) { throw new NotSupportedException(); }
public AvlNode() { right = Empty; left = Empty; }