Esempio n. 1
0
        private void AssertInvariant()
        {
            int  leftCount  = _left.Count();
            int  rightCount = _right.Count();
            bool searchTree = true;

            // ReSharper disable PossibleNullReferenceException
            if (leftCount > 0)
            {
                searchTree = _Comparer.Compare(_left._key, _key) < 0;
            }
            if (rightCount > 0)
            {
                searchTree = searchTree && _Comparer.Compare(_key, _right._key) < 0;
            }
            // ReSharper restore PossibleNullReferenceException
            Debug.Assert(_count == 0 || _count == 1 + leftCount + rightCount);
            Debug.Assert(searchTree);
            Debug.Assert(_count == 0 || (_left != null && _right != null));
            Debug.Assert(_count <= 2 || (leftCount <= DELTA * rightCount && rightCount <= DELTA * leftCount));
        }
Esempio n. 2
0
        private static TreeDictionary <TKey, TValue, TComparer> GlueBalanced(
            TreeDictionary <TKey, TValue, TComparer> left, TreeDictionary <TKey, TValue, TComparer> right)
        {
            if (left.Count() == 0)
            {
                return(right);
            }
            if (right.Count() == 0)
            {
                return(left);
            }
            TKey   minKeyRight;
            TValue minKeyValueRight;
            var    newRight = right.FindAndRemoveMinKeyFromNonEmpty(out minKeyRight, out minKeyValueRight);

            return(NearlyBalanced(minKeyRight, minKeyValueRight, left, newRight));
        }
Esempio n. 3
0
 private static TreeDictionary <TKey, TValue, TComparer> Unbalanced(
     TKey key, TValue value, TreeDictionary <TKey, TValue, TComparer> left, TreeDictionary <TKey, TValue, TComparer> right)
 {
     if (left.Count() == 0)
     {
         return(right.InsertMin(key, value));
     }
     if (right.Count() == 0)
     {
         return(left.InsertMax(key, value));
     }
     if (DELTA * left.Count <= right.Count)
     {
         return(right.NearlyBalanced(Unbalanced(key, value, left, right._left), right._right));
     }
     if (DELTA * right.Count <= left.Count)
     {
         return(left.NearlyBalanced(left._left, Unbalanced(key, value, left._right, right)));
     }
     return(Balanced(key, value, left, right));
 }
Esempio n. 4
0
 private static TreeDictionary <TKey, TValue, TComparer> GlueUnbalanced(
     TreeDictionary <TKey, TValue, TComparer> left, TreeDictionary <TKey, TValue, TComparer> right)
 {
     if (left.Count() == 0)
     {
         return(right);
     }
     if (right.Count() == 0)
     {
         return(left);
     }
     if (DELTA * left.Count <= right.Count)
     {
         return(right.NearlyBalanced(GlueUnbalanced(left, right._left), right._right));
     }
     if (DELTA * right.Count <= left.Count)
     {
         return(left.NearlyBalanced(left._left, GlueUnbalanced(left._right, right)));
     }
     return(GlueBalanced(left, right));
 }
Esempio n. 5
0
        private static TreeDictionary <TKey, TValue, TComparer> NearlyBalanced(
            TKey key, TValue value, TreeDictionary <TKey, TValue, TComparer> left, TreeDictionary <TKey, TValue, TComparer> right)
        {
            int countLeft  = left.Count();
            int countRight = right.Count();
            int count      = 1 + countLeft + countRight;

            if (countLeft + countRight <= 1)
            {
                return(new TreeDictionary <TKey, TValue, TComparer>(count, key, value, left, right));
            }
            if (countLeft >= DELTA * countRight) // >= in Data.Map
            {
                return(RotateRight(key, value, left, right));
            }
            if (countRight >= DELTA * countLeft) // >= in Data.Map
            {
                return(RotateLeft(key, value, left, right));
            }
            // Balanced already
            return(new TreeDictionary <TKey, TValue, TComparer>(count, key, value, left, right));
        }
Esempio n. 6
0
 private TreeDictionary(
     TKey key, TValue value, TreeDictionary <TKey, TValue, TComparer> left, TreeDictionary <TKey, TValue, TComparer> right) :
     this(1 + left.Count() + right.Count(), key, value, left, right)
 {
 }