예제 #1
0
        public AvlNode <T> SetItem(int index, T val)
        {
            AvlNode <T> newLeft  = this.left;
            AvlNode <T> newRight = this.right;

            if (index < this.left._count)
            {
                newLeft = this.ToMutableIfNecessary(this.left).SetItem(index, val);
            }
            else
            {
                if (index <= this.left._count)
                {
                    return(this.NewOrMutate(val, newLeft, newRight));
                }
                newRight = this.ToMutableIfNecessary(this.right).SetItem(index - this.left._count - 1, val);
            }
            return(this.NewOrMutate(this.Value, newLeft, newRight));
        }
예제 #2
0
        public IEnumerator <T> GetEnumerator(bool reverse)
        {
            Stack <AvlNode <T> > to_visit = new Stack <AvlNode <T> >();

            to_visit.Push(this);
            while (to_visit.Count > 0)
            {
                AvlNode <T> this_d = to_visit.Pop();
                while (!this_d.IsEmpty)
                {
                    if (reverse)
                    {
                        if (this_d.right.IsEmpty)
                        {
                            yield return(this_d.Value);

                            this_d = this_d.left;
                        }
                        else
                        {
                            to_visit.Push(this_d.left);
                            to_visit.Push(new AvlNode <T>(this_d.Value));
                            this_d = this_d.right;
                        }
                    }
                    else if (this_d.left.IsEmpty)
                    {
                        yield return(this_d.Value);

                        this_d = this_d.right;
                    }
                    else
                    {
                        if (!this_d.right.IsEmpty)
                        {
                            to_visit.Push(this_d.right);
                        }
                        to_visit.Push(new AvlNode <T>(this_d.Value));
                        this_d = this_d.left;
                    }
                }
            }
        }
예제 #3
0
        public AvlNode <T> InsertIntoNew(int index, T val)
        {
            if (this.IsEmpty)
            {
                return(new AvlNode <T>(val));
            }
            AvlNode <T> newLeft  = this.left;
            AvlNode <T> newRight = this.right;

            if (index <= this.left._count)
            {
                newLeft = this.ToMutableIfNecessary(this.left).InsertIntoNew(index, val);
            }
            else
            {
                newRight = this.ToMutableIfNecessary(this.right).InsertIntoNew(index - this.left._count - 1, val);
            }
            return(this.NewOrMutate(this.Value, newLeft, newRight).FixRootBalance());
        }
예제 #4
0
        public AvlNode <T> SearchNode(T value, Comparison <T> comparer)
        {
            AvlNode <T> avlNode = this;

            while (avlNode != AvlNode <T> .Empty)
            {
                int num = comparer(avlNode.Value, value);
                if (num < 0)
                {
                    avlNode = avlNode.right;
                }
                else
                {
                    if (num <= 0)
                    {
                        return(avlNode);
                    }
                    avlNode = avlNode.left;
                }
            }
            return(AvlNode <T> .Empty);
        }
예제 #5
0
 public AvlNode()
 {
     this.right = AvlNode <T> .Empty;
     this.left  = AvlNode <T> .Empty;
 }
예제 #6
0
 public override AvlNode <T> ToMutableIfNecessary(AvlNode <T> node)
 {
     return(node.ToMutable());
 }
예제 #7
0
 public MutableAvlNode(T val, AvlNode <T> lt, AvlNode <T> gt)
     : base(val, lt, gt)
 {
 }
예제 #8
0
 public virtual AvlNode <T> ToMutableIfNecessary(AvlNode <T> node)
 {
     return(node);
 }
예제 #9
0
        public bool Contains(KeyValuePair <TKey, TValue> kv)
        {
            AvlNode <KeyValuePair <TKey, TValue> > avlNode = this.root.SearchNode(kv, new Comparison <KeyValuePair <TKey, TValue> >(ImmutableDictionary <TKey, TValue> .CompareKV));

            return(!avlNode.IsEmpty && this.valueComparer.Equals(avlNode.Value.Value, kv.Value));
        }
예제 #10
0
 public void Clear()
 {
     this.root = new AvlNode <KeyValuePair <TKey, TValue> >().ToMutable();
 }
예제 #11
0
 public void Add(KeyValuePair <TKey, TValue> item)
 {
     this.root = this.root.InsertIntoNew(item, new Comparison <KeyValuePair <TKey, TValue> >(ImmutableDictionary <TKey, TValue> .CompareKV));
 }
예제 #12
0
 internal Builder(AvlNode <T> immutableRoot, IEqualityComparer <T> comparer)
 {
     this.root          = immutableRoot.ToMutable();
     this.valueComparer = comparer;
 }
예제 #13
0
 internal ImmutableList(AvlNode <T> root, IEqualityComparer <T> equalityComparer)
 {
     this.root          = root;
     this.valueComparer = equalityComparer;
 }