Пример #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);
 }
Пример #2
0
        public bool TryGetValue(TKey key, Func <TKey, TKey, int> compareFunction, out TValue value)
        {
            VersioningTree <TKey, TValue> rightChild;
            VersioningTree <TKey, TValue> versioningTree = this;

            while (versioningTree != null)
            {
                int num = compareFunction(key, versioningTree.Key);
                if (num != 0)
                {
                    if (num < 0)
                    {
                        VersioningTree <TKey, TValue> leftChild = versioningTree.LeftChild;
                        rightChild     = leftChild;
                        versioningTree = leftChild;
                    }
                    else
                    {
                        rightChild = versioningTree.RightChild;
                    }
                    versioningTree = rightChild;
                }
                else
                {
                    value = versioningTree.Value;
                    return(true);
                }
            }
            value = default(TValue);
            return(false);
        }
Пример #3
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;
 }
Пример #4
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)));
        }
Пример #5
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));
        }
Пример #6
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));
        }
Пример #7
0
 private static int GetHeight(VersioningTree <TKey, TValue> tree)
 {
     if (tree == null)
     {
         return(0);
     }
     else
     {
         return(tree.Height);
     }
 }
Пример #8
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);
            }
Пример #9
0
            public override bool TryGetValue(TKey key, out TValue value)
            {
                VersioningTree <TKey, TValue> tree = this.treeBuckets[this.GetBucket(key)];

                if (tree == null)
                {
                    value = default(TValue);
                    return(false);
                }
                else
                {
                    return(tree.TryGetValue(key, this.CompareFunction, out value));
                }
            }
Пример #10
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);
        }
Пример #11
0
        public VersioningTree <TKey, TValue> SetKeyValue(TKey key, TValue value, Func <TKey, TKey, int> compareFunction)
        {
            VersioningTree <TKey, TValue> leftChild;
            VersioningTree <TKey, TValue> rightChild;
            TKey   tKey;
            TValue tValue;
            VersioningTree <TKey, TValue> versioningTree;
            VersioningTree <TKey, TValue> rightChild1;
            TKey   tKey1;
            TValue tValue1;
            VersioningTree <TKey, TValue> leftChild1      = this.LeftChild;
            VersioningTree <TKey, TValue> versioningTree1 = this.RightChild;
            int num = compareFunction(key, this.Key);

            if (num >= 0)
            {
                if (num != 0)
                {
                    if (VersioningTree <TKey, TValue> .GetHeight(leftChild1) >= VersioningTree <TKey, TValue> .GetHeight(versioningTree1))
                    {
                        return(new VersioningTree <TKey, TValue>(this.Key, this.Value, leftChild1, VersioningTree <TKey, TValue> .SetKeyValue(versioningTree1, key, value, compareFunction)));
                    }
                    else
                    {
                        int    num1    = compareFunction(key, versioningTree1.Key);
                        TKey   tKey2   = this.Key;
                        TValue tValue2 = this.Value;
                        VersioningTree <TKey, TValue> versioningTree2 = leftChild1;
                        if (num1 < 0)
                        {
                            leftChild = VersioningTree <TKey, TValue> .SetKeyValue(versioningTree1.LeftChild, key, value, compareFunction);
                        }
                        else
                        {
                            leftChild = versioningTree1.LeftChild;
                        }
                        VersioningTree <TKey, TValue> versioningTree3 = new VersioningTree <TKey, TValue>(tKey2, tValue2, versioningTree2, leftChild);
                        if (num1 > 0)
                        {
                            rightChild = VersioningTree <TKey, TValue> .SetKeyValue(versioningTree1.RightChild, key, value, compareFunction);
                        }
                        else
                        {
                            rightChild = versioningTree1.RightChild;
                        }
                        VersioningTree <TKey, TValue> versioningTree4 = rightChild;
                        if (num1 == 0)
                        {
                            tKey = key;
                        }
                        else
                        {
                            tKey = versioningTree1.Key;
                        }
                        if (num1 == 0)
                        {
                            tValue = value;
                        }
                        else
                        {
                            tValue = versioningTree1.Value;
                        }
                        return(new VersioningTree <TKey, TValue>(tKey, tValue, versioningTree3, versioningTree4));
                    }
                }
                else
                {
                    return(new VersioningTree <TKey, TValue>(key, value, leftChild1, versioningTree1));
                }
            }
            else
            {
                if (VersioningTree <TKey, TValue> .GetHeight(leftChild1) <= VersioningTree <TKey, TValue> .GetHeight(versioningTree1))
                {
                    return(new VersioningTree <TKey, TValue>(this.Key, this.Value, VersioningTree <TKey, TValue> .SetKeyValue(leftChild1, key, value, compareFunction), versioningTree1));
                }
                else
                {
                    int num2 = compareFunction(key, leftChild1.Key);
                    if (num2 < 0)
                    {
                        versioningTree = VersioningTree <TKey, TValue> .SetKeyValue(leftChild1.LeftChild, key, value, compareFunction);
                    }
                    else
                    {
                        versioningTree = leftChild1.LeftChild;
                    }
                    VersioningTree <TKey, TValue> versioningTree5 = versioningTree;
                    TKey   tKey3   = this.Key;
                    TValue tValue3 = this.Value;
                    if (num2 > 0)
                    {
                        rightChild1 = VersioningTree <TKey, TValue> .SetKeyValue(leftChild1.RightChild, key, value, compareFunction);
                    }
                    else
                    {
                        rightChild1 = leftChild1.RightChild;
                    }
                    VersioningTree <TKey, TValue> versioningTree6 = new VersioningTree <TKey, TValue>(tKey3, tValue3, rightChild1, versioningTree1);
                    if (num2 == 0)
                    {
                        tKey1 = key;
                    }
                    else
                    {
                        tKey1 = leftChild1.Key;
                    }
                    if (num2 == 0)
                    {
                        tValue1 = value;
                    }
                    else
                    {
                        tValue1 = leftChild1.Value;
                    }
                    return(new VersioningTree <TKey, TValue>(tKey1, tValue1, versioningTree5, versioningTree6));
                }
            }
        }
Пример #12
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)));
                }
            }
        }
Пример #13
0
 private static int GetHeight(VersioningTree <TKey, TValue> tree)
 {
     return(tree == null ? 0 : tree.Height);
 }
Пример #14
0
 public TreeDictionary(Func <TKey, TKey, int> compareFunction, VersioningTree <TKey, TValue> tree)
     : base(compareFunction)
 {
     this.tree = tree;
 }
Пример #15
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);
 }