Exemple #1
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)));
        }
Exemple #2
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)));
 }