Esempio n. 1
0
 private TreeDictionary <TKey, TValue, TComparer> Balanced(TValue value, TreeDictionary <TKey, TValue, TComparer> left, TreeDictionary <TKey, TValue, TComparer> right)
 {
     return(EqualityComparer <TValue> .Default.Equals(_value, value) &&
            ReferenceEquals(_left, left) && ReferenceEquals(_right, right)
                ? this
                : Balanced(_key, value, left, right));
 }
Esempio n. 2
0
        private TreeDictionary <TKey, TValue, TComparer> AddRecursive(TKey key, TValue value, ref bool needRebalance)
        {
            if (_count == 0)
            {
                needRebalance = true;
                return(TreeDictionary.Single <TKey, TValue, TComparer>(key, value));
            }
            int comparison = _Comparer.Compare(key, _key);

            if (comparison == 0)
            {
                needRebalance = false;
                return(ReplaceValue(value));
            }
            if (comparison < 0)
            {
                var newLeft = _left.AddRecursive(key, value, ref needRebalance);
                return(needRebalance
                           ? NearlyBalanced(newLeft, _right)
                           : Balanced(newLeft, _right));
            }
            else
            {
                var newRight = _right.AddRecursive(key, value, ref needRebalance);
                return(needRebalance
                           ? NearlyBalanced(_left, newRight)
                           : Balanced(_left, newRight));
            }
        }
Esempio n. 3
0
        private void SplitRecursive(
            TKey key,
            out TreeDictionary <TKey, TValue, TComparer> less,
            out Optional <TValue> value,
            out TreeDictionary <TKey, TValue, TComparer> greater)
        {
            if (Count == 0)
            {
                less    = Empty;
                value   = Optional <TValue> .None;
                greater = Empty;
                return;
            }
            int comparison = _Comparer.Compare(key, _key);

            if (comparison == 0)
            {
                less    = _left;
                value   = Optional.Some(_value);
                greater = _right;
                return;
            }
            if (comparison < 0)
            {
                _left.SplitRecursive(key, out less, out value, out greater);
                greater = Unbalanced(_key, _value, greater, _right);
                return;
            }
            else
            {
                _right.SplitRecursive(key, out less, out value, out greater);
                less = Unbalanced(_key, _value, _left, less);
                return;
            }
        }
Esempio n. 4
0
 private TreeDictionary <TKey, TValue, TComparer> Balanced(
     TreeDictionary <TKey, TValue, TComparer> left, TreeDictionary <TKey, TValue, TComparer> right)
 {
     return(ReferenceEquals(_left, left) && ReferenceEquals(_right, right)
                ? this
                : Balanced(_key, _value, left, right));
 }
Esempio n. 5
0
 private static TreeDictionary <TKey, TValue, TComparer> RotateRight(
     TKey key, TValue value, TreeDictionary <TKey, TValue, TComparer> left, TreeDictionary <TKey, TValue, TComparer> right)
 {
     return(left._right.Count() < RATIO *left._left.Count()
                ? RotateSingleRight(key, value, left, right)
                : RotateDoubleRight(key, value, left, right));
 }
Esempio n. 6
0
 private static TreeDictionary <TKey, TValue, TComparer> RotateSingleRight(
     TKey key, TValue value, TreeDictionary <TKey, TValue, TComparer> left, TreeDictionary <TKey, TValue, TComparer> right)
 {
     return(Balanced(
                left._key, left._value,
                left._left,
                Balanced(key, value, left._right, right)));
 }
Esempio n. 7
0
 private TreeDictionary <TKey, TValue, TComparer> InsertMax(TKey key, TValue value)
 {
     if (Count == 0)
     {
         return(TreeDictionary.Single <TKey, TValue, TComparer>(key, value));
     }
     return(NearlyBalanced(_left, _right.InsertMax(key, value)));
 }
Esempio n. 8
0
 /// <summary>
 /// Returns a dictionary with the same keys. The value for each key is
 /// obtained by calling <c>func(key, this[key])</c>.
 /// </summary>
 /// <typeparam name="TValue1">The type of the values in the returned dictionary.</typeparam>
 /// <param name="func">The function mapped.</param>
 /// <returns>The resulting dictionary.</returns>
 public TreeDictionary <TKey, TValue1, TComparer> Map <TValue1>(Func <TKey, TValue, TValue1> func)
 {
     if (Count == 0)
     {
         return(TreeDictionary <TKey, TValue1, TComparer> .Empty);
     }
     return(TreeDictionary <TKey, TValue1, TComparer> .Balanced(
                _key, func(_key, _value), _left.Map(func), _right.Map(func)));
 }
Esempio n. 9
0
 internal TreeDictionary(
     int count, TKey key, TValue value, TreeDictionary <TKey, TValue, TComparer> left, TreeDictionary <TKey, TValue, TComparer> right)
 {
     _count = count;
     _right = right;
     _left  = left;
     _value = value;
     _key   = key;
     AssertInvariant();
 }
Esempio n. 10
0
        private static TreeDictionary <TKey, TValue, TComparer> RotateDoubleLeft(
            TKey key, TValue value, TreeDictionary <TKey, TValue, TComparer> left, TreeDictionary <TKey, TValue, TComparer> right)
        {
            TreeDictionary <TKey, TValue, TComparer> rightLeft = right._left;

            return(Balanced(
                       rightLeft._key, rightLeft._value,
                       Balanced(key, value, left, rightLeft._left),
                       Balanced(right._key, right._value, rightLeft._right, right._right)));
        }
Esempio n. 11
0
        private static TreeDictionary <TKey, TValue, TComparer> RotateDoubleRight(
            TKey key, TValue value, TreeDictionary <TKey, TValue, TComparer> left, TreeDictionary <TKey, TValue, TComparer> right)
        {
            var leftRight = left._right;

            return(Balanced(
                       leftRight._key, leftRight._value,
                       Balanced(left._key, left._value, left._left, leftRight._left),
                       Balanced(key, value, leftRight._right, right)));
        }
Esempio n. 12
0
        /// <summary>
        /// Updates values of all keys. For each key in the dictionary, the
        /// value is replaced with <code>func(key, this[key])</code>.
        /// </summary>
        /// <typeparam name="TValue1">The type of the values in the returned dictionary.</typeparam>
        /// <param name="func">The updating function.</param>
        /// <returns>The resulting dictionary.</returns>
        public TreeDictionary <TKey, TValue1, TComparer> MapPartial <TValue1>(Func <TKey, TValue, Optional <TValue1> > func)
        {
            if (Count == 0)
            {
                return(TreeDictionary <TKey, TValue1, TComparer> .Empty);
            }
            var newValue = func(_key, _value);

            return(newValue.HasValue
                       ? TreeDictionary <TKey, TValue1, TComparer> .Unbalanced(_key, newValue.Value, _left.MapPartial(func), _right.MapPartial(func))
                       : TreeDictionary <TKey, TValue1, TComparer> .GlueUnbalanced(_left.MapPartial(func), _right.MapPartial(func)));
        }
Esempio n. 13
0
 /// <summary>
 /// Indicates whether the current object is equal to another object of
 /// the same type. Dictionaries are considered equal, if they have the
 /// same keys and values.
 /// </summary>
 /// <returns>
 /// <c>true</c> if the current object is equal to the
 /// <paramref name="other"/> parameter; otherwise, <c>false</c>.
 /// </returns>
 /// <param name="other">An object to compare with this object.
 /// </param>
 public bool Equals(TreeDictionary <TKey, TValue, TComparer> other)
 {
     if (ReferenceEquals(null, other))
     {
         return(false);
     }
     if (ReferenceEquals(this, other))
     {
         return(true);
     }
     return(_count == other._count && this.SequenceEqual(other));
 }
Esempio n. 14
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. 15
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. 16
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. 17
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. 18
0
 private TreeDictionary <TKey, TValue, TComparer> UnionTrim(
     TreeDictionary <TKey, TValue, TComparer> otherDict,
     TKey low, TKey high,
     Func <TKey, TValue, TValue, TValue> combiner)
 {
     if (otherDict.Count == 0)
     {
         return(this);
     }
     if (Count == 0)
     {
         var otherLeft  = otherDict._left.FilterLess(high);
         var otherRight = otherDict._right.FilterGreater(low);
         return(ReferenceEquals(otherDict._left, otherLeft) && ReferenceEquals(otherDict._right, otherRight)
                    ? otherDict
                    : otherDict.Unbalanced(otherDict._value, otherLeft, otherRight));
     }
     Debug.Assert(_Comparer.Compare(low, _key) < 0 && _Comparer.Compare(high, _key) > 0);
     return(Unbalanced(
                _value,
                _left.UnionTrim(otherDict.Trim(low, _key), low, _key, combiner),
                _right.UnionTrim(otherDict.Trim(_key, high), _key, high, combiner)));
 }
Esempio n. 19
0
 /// <summary>
 /// Returns the dictionary containing all keys present in both
 /// dictionaries. The value is obtained by using the
 /// <paramref name="combiner"/>.
 /// </summary>
 /// <param name="otherDict">
 /// The other dictionary.
 /// </param>
 /// <param name="combiner">
 /// The function used for combining values of duplicate keys.
 /// </param>
 /// <returns>
 /// The difference of two dictionaries.
 /// </returns>
 public TreeDictionary <TKey, TValue, TComparer> Intersection(
     TreeDictionary <TKey, TValue, TComparer> otherDict, Func <TKey, TValue, TValue, TValue> combiner)
 {
     throw new NotImplementedException();
 }
Esempio n. 20
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)
 {
 }
Esempio n. 21
0
 private static TreeDictionary <TKey, TValue, TComparer> Balanced(
     TKey key, TValue value, TreeDictionary <TKey, TValue, TComparer> left, TreeDictionary <TKey, TValue, TComparer> right)
 {
     return(new TreeDictionary <TKey, TValue, TComparer>(key, value, left, right));
 }
Esempio n. 22
0
 private TreeDictionary <TKey, TValue, TComparer> NearlyBalanced(TreeDictionary <TKey, TValue, TComparer> left, TreeDictionary <TKey, TValue, TComparer> right)
 {
     return(NearlyBalanced(_key, _value, left, right));
 }
Esempio n. 23
0
 public TreeDictionary <TKey, TValue, TComparer> Difference(
     TreeDictionary <TKey, TValue, TComparer> otherDict)
 {
     throw new NotImplementedException();
 }
Esempio n. 24
0
 /// <summary>
 /// Returns the dictionary containing all keys present in both
 /// dictionaries. The value from <c>this</c> dictionary is used.
 /// </summary>
 /// <param name="otherDict">
 /// The other dictionary.
 /// </param>
 /// <param name="combiner">
 /// The function used for combining values of duplicate keys.
 /// </param>
 /// <returns>
 /// The difference of two dictionaries.
 /// </returns>
 public TreeDictionary <TKey, TValue, TComparer> IntersectionLeftBiased(TreeDictionary <TKey, TValue, TComparer> otherDict)
 {
     throw new NotImplementedException();
 }