Esempio n. 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));
        }
Esempio n. 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;
                    }
                }
            }
        }
Esempio n. 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());
        }
Esempio n. 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);
        }
Esempio n. 5
0
 public AvlNode()
 {
     this.right = AvlNode <T> .Empty;
     this.left  = AvlNode <T> .Empty;
 }
Esempio n. 6
0
 public override AvlNode <T> ToMutableIfNecessary(AvlNode <T> node)
 {
     return(node.ToMutable());
 }
Esempio n. 7
0
 public MutableAvlNode(T val, AvlNode <T> lt, AvlNode <T> gt)
     : base(val, lt, gt)
 {
 }
Esempio n. 8
0
 public virtual AvlNode <T> ToMutableIfNecessary(AvlNode <T> node)
 {
     return(node);
 }
Esempio n. 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));
        }
Esempio n. 10
0
 public void Clear()
 {
     this.root = new AvlNode <KeyValuePair <TKey, TValue> >().ToMutable();
 }
Esempio n. 11
0
 public void Add(KeyValuePair <TKey, TValue> item)
 {
     this.root = this.root.InsertIntoNew(item, new Comparison <KeyValuePair <TKey, TValue> >(ImmutableDictionary <TKey, TValue> .CompareKV));
 }
Esempio n. 12
0
 internal Builder(AvlNode <T> immutableRoot, IEqualityComparer <T> comparer)
 {
     this.root          = immutableRoot.ToMutable();
     this.valueComparer = comparer;
 }
Esempio n. 13
0
 internal ImmutableList(AvlNode <T> root, IEqualityComparer <T> equalityComparer)
 {
     this.root          = root;
     this.valueComparer = equalityComparer;
 }