Exemplo n.º 1
0
 public HashTreeDictionary(Func <TKey, TKey, int> compareFunction, VersioningTree <TKey, TValue> tree, TKey key, TValue value)
     : base(compareFunction)
 {
     this.treeBuckets = new VersioningTree <TKey, TValue> [HashSize];
     this.SetKeyValues(tree);
     this.SetKeyValue(key, value);
 }
Exemplo n.º 2
0
 /// <summary>
 /// Initializes a new instance of VersioningTree.
 /// </summary>
 /// <param name="key">The key of the tree node.</param>
 /// <param name="value">The value of the tree node.</param>
 /// <param name="leftChild">A tree with all keys less than the key of the tree node. May be null.</param>
 /// <param name="rightChild">A tree with all keys greater than the key of the tree node. May be null.</param>
 public VersioningTree(TKey key, TValue value, VersioningTree <TKey, TValue> leftChild, VersioningTree <TKey, TValue> rightChild)
 {
     this.Key        = key;
     this.Value      = value;
     this.Height     = Max(GetHeight(leftChild), GetHeight(rightChild)) + 1;
     this.LeftChild  = leftChild;
     this.RightChild = rightChild;
 }
Exemplo n.º 3
0
        private VersioningTree <TKey, TValue> MakeRightmost(VersioningTree <TKey, TValue> rightmost)
        {
            if (this.RightChild == null)
            {
                return(new VersioningTree <TKey, TValue>(this.Key, this.Value, this.LeftChild, rightmost));
            }

            return(new VersioningTree <TKey, TValue>(this.Key, this.Value, this.LeftChild, this.RightChild.MakeRightmost(rightmost)));
        }
Exemplo n.º 4
0
        private VersioningTree <TKey, TValue> MakeLeftmost(VersioningTree <TKey, TValue> leftmost)
        {
            if (this.LeftChild == null)
            {
                return(new VersioningTree <TKey, TValue>(this.Key, this.Value, leftmost, this.RightChild));
            }

            return(new VersioningTree <TKey, TValue>(this.Key, this.Value, this.LeftChild.MakeLeftmost(leftmost), this.RightChild));
        }
Exemplo n.º 5
0
        private static VersioningTree <TKey, TValue> SetKeyValue(VersioningTree <TKey, TValue> me, TKey key, TValue value, Func <TKey, TKey, int> compareFunction)
        {
            if (me == null)
            {
                return(new VersioningTree <TKey, TValue>(key, value, null, null));
            }

            return(me.SetKeyValue(key, value, compareFunction));
        }
Exemplo n.º 6
0
            private void SetKeyValues(VersioningTree <TKey, TValue> tree)
            {
                if (tree == null)
                {
                    return;
                }

                this.SetKeyValue(tree.Key, tree.Value);

                this.SetKeyValues(tree.LeftChild);
                this.SetKeyValues(tree.RightChild);
            }
Exemplo n.º 7
0
            public override bool TryGetValue(TKey key, out TValue value)
            {
                VersioningTree <TKey, TValue> tree = this.treeBuckets[GetBucket(key)];

                if (tree == null)
                {
                    value = default(TValue);
                    return(false);
                }
                else
                {
                    return(tree.TryGetValue(key, this.CompareFunction, out value));
                }
            }
Exemplo n.º 8
0
        public bool TryGetValue(TKey key, Func <TKey, TKey, int> compareFunction, out TValue value)
        {
            VersioningTree <TKey, TValue> tree = this;

            while (tree != null)
            {
                int comparison = compareFunction(key, tree.Key);
                if (comparison == 0)
                {
                    value = tree.Value;
                    return(true);
                }

                tree = comparison < 0 ? tree = tree.LeftChild : tree.RightChild;
            }

            value = default(TValue);
            return(false);
        }
Exemplo n.º 9
0
 public TreeDictionary(Func <TKey, TKey, int> compareFunction, VersioningTree <TKey, TValue> tree)
     : base(compareFunction)
 {
     this.tree = tree;
 }
Exemplo n.º 10
0
 public TreeDictionary(Func <TKey, TKey, int> compareFunction, TKey firstKey, TValue firstValue, TKey secondKey, TValue secondValue, TKey thirdKey, TValue thirdValue)
     : base(compareFunction)
 {
     this.tree = new VersioningTree <TKey, TValue>(firstKey, firstValue, null, null).SetKeyValue(secondKey, secondValue, this.CompareFunction).SetKeyValue(thirdKey, thirdValue, this.CompareFunction);
 }
Exemplo n.º 11
0
        public VersioningTree <TKey, TValue> SetKeyValue(TKey key, TValue value, Func <TKey, TKey, int> compareFunction)
        {
            VersioningTree <TKey, TValue> leftChild  = this.LeftChild;
            VersioningTree <TKey, TValue> rightChild = this.RightChild;

            int comparison = compareFunction(key, this.Key);

            if (comparison < 0)
            {
                // Insert to the left.
                if (GetHeight(leftChild) > GetHeight(rightChild))
                {
                    // The left subtree is taller than the right, so rotate to the right.
                    int leftChildComparison = compareFunction(key, leftChild.Key);

                    VersioningTree <TKey, TValue> newLeft =
                        leftChildComparison < 0 ? SetKeyValue(leftChild.LeftChild, key, value, compareFunction) : leftChild.LeftChild;
                    VersioningTree <TKey, TValue> newRight =
                        new VersioningTree <TKey, TValue>(
                            this.Key,
                            this.Value,
                            leftChildComparison > 0 ? SetKeyValue(leftChild.RightChild, key, value, compareFunction) : leftChild.RightChild,
                            rightChild);
                    return
                        (new VersioningTree <TKey, TValue>(
                             leftChildComparison == 0 ? key : leftChild.Key,
                             leftChildComparison == 0 ? value : leftChild.Value,
                             newLeft,
                             newRight));
                }
                else
                {
                    return
                        (new VersioningTree <TKey, TValue>(
                             this.Key,
                             this.Value,
                             SetKeyValue(leftChild, key, value, compareFunction),
                             rightChild));
                }
            }
            else if (comparison == 0)
            {
                // Insert here.
                return(new VersioningTree <TKey, TValue>(key, value, leftChild, rightChild));
            }
            else
            {
                // Insert to the right.
                if (GetHeight(leftChild) < GetHeight(rightChild))
                {
                    // The right subtree is taller than the left, so rotate to the left.
                    int rightChildComparison = compareFunction(key, rightChild.Key);

                    VersioningTree <TKey, TValue> newLeft =
                        new VersioningTree <TKey, TValue>(
                            this.Key,
                            this.Value,
                            leftChild,
                            rightChildComparison < 0 ? SetKeyValue(rightChild.LeftChild, key, value, compareFunction) : rightChild.LeftChild);
                    VersioningTree <TKey, TValue> newRight =
                        rightChildComparison > 0 ? SetKeyValue(rightChild.RightChild, key, value, compareFunction) : rightChild.RightChild;
                    return
                        (new VersioningTree <TKey, TValue>(
                             rightChildComparison == 0 ? key : rightChild.Key,
                             rightChildComparison == 0 ? value : rightChild.Value,
                             newLeft,
                             newRight));
                }
                else
                {
                    return
                        (new VersioningTree <TKey, TValue>(
                             this.Key,
                             this.Value,
                             leftChild,
                             SetKeyValue(rightChild, key, value, compareFunction)));
                }
            }
        }
Exemplo n.º 12
0
 private static int GetHeight(VersioningTree <TKey, TValue> tree)
 {
     return(tree == null ? 0 : tree.Height);
 }