예제 #1
0
        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());
        }
예제 #2
0
        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));
        }
예제 #3
0
 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());
     }
 }
예제 #4
0
        /// <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());
        }
예제 #5
0
 internal ImmutableDictionary(AvlNode <KeyValuePair <TKey, TValue> > root, IEqualityComparer <TKey> keyComparer, IEqualityComparer <TValue> valueComparer)
 {
     this.root          = root;
     this.keyComparer   = keyComparer;
     this.valueComparer = valueComparer;
 }
예제 #6
0
 public void Clear()
 {
     root = new AvlNode <KeyValuePair <TKey, TValue> >().ToMutable();
 }
예제 #7
0
 public void Add(KeyValuePair <TKey, TValue> item)
 {
     root = root.InsertIntoNew(item, CompareKV);
 }
예제 #8
0
 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;
 }
예제 #11
0
 public override AvlNode <T> ToMutableIfNecessary(AvlNode <T> node)
 {
     return(node.ToMutable());
 }
예제 #12
0
 public MutableAvlNode(T val, AvlNode <T> lt, AvlNode <T> gt) : base(val, lt, gt)
 {
 }
예제 #13
0
 public virtual AvlNode <T> ToMutableIfNecessary(AvlNode <T> node)
 {
     return(node);
 }
예제 #14
0
 public virtual AvlNode <T> NewOrMutate(T newValue, AvlNode <T> newLeft, AvlNode <T> newRight)
 {
     return(new AvlNode <T>(newValue, newLeft, newRight));
 }
예제 #15
0
 public override AvlNode <T> NewOrMutate(T newValue, AvlNode <T> newLeft, AvlNode <T> newRight)
 {
     throw new NotSupportedException();
 }
예제 #16
0
 public AvlNode()
 {
     right = Empty;
     left  = Empty;
 }