Exemplo n.º 1
0
 private static RedBlackTree <T> /*!*/ InsertHelper(RedBlackTree <T> /*?*/ t, T /*?*/ o, bool replace, out bool added)
 {
     if ((object)t == null)
     {
         added = true;
         return(new RedBlackTree <T>(Color.R, o, null, null));
     }
     else
     {
         int k = HashAlgorithms.CompareValues(o, t.value);
         if (k == 0)
         {
             added = false;
             return(replace ? new RedBlackTree <T>(t.color, o, t.left, t.right) : t);
         }
         else if (k < 0)
         {
             return(Balance(t.color, t.value, InsertHelper(t.left, o, replace, out added), t.right));
         }
         else
         {
             return(Balance(t.color, t.value, t.left, InsertHelper(t.right, o, replace, out added)));
         }
     }
 }
Exemplo n.º 2
0
 private static RedBlackTree <T> Relink(RedBlackTree <T> t, RedBlackTree <T> /*?*/ s)
 {
     if ((object)s == null)
     {
         return(t);
     }
     else if (HashAlgorithms.CompareValues(t.value, s.value) < 0)
     {
         return(Balance(s.color, s.value, Relink(t, s.left), s.right));
     }
     else
     {
         return(Balance(s.color, s.value, s.left, Relink(t, s.right)));
     }
 }
Exemplo n.º 3
0
 public static LobTree <T> /*?*/ Remove(LobTree <T> t, T o, out bool deleted)
 //^ ensures result != null ==> !result.Contains(o);
 //^ ensures deleted ==> result != null && t != null && t.Count = result.Count + 1;
 {
     if ((object)t == null)
     {
         deleted = false;
         return(null);
     }
     else
     {
         Base result = t.representation.Remove(o, HashAlgorithms.GetHashCode(o), 0, out deleted);
         return(deleted ? new LobTree <T>(result) : t);
     }
 }
Exemplo n.º 4
0
        public static LobTree <T> Insert(LobTree <T> /*?*/ tree, T o, bool replace, out bool added)
        //^ ensures result.Contains(o);
        //^ ensures added <==> tree != null && result.Count == tree.Count + 1;
        //^ ensures added <==> !tree.Contains(o);
        //^ ensures !added <==> Object.Equals(tree, result);              // object equals when o is contained regardless of replace
        //^ ensures (!added && !replace) <==> ((object)tree == result);   // pointer equals iff !replace and !added
        {
            int hashKey = HashAlgorithms.GetHashCode(o);

            if ((object)tree == null)
            {
                return(new LobTree <T>(Base.EmptyTree.InsertHelper(o, hashKey, replace, out added, 0)));
            }
            else
            {
                Base result = tree.representation.InsertHelper(o, hashKey, replace, out added, 0);
                return(((object)result != (object)(tree.representation)) ? new LobTree <T>(result) : tree);
            }
        }
Exemplo n.º 5
0
        /// <summary>
        /// Looks up a value associated with a given key
        /// </summary>
        /// <param name="o">The key</param>
        /// <param name="result">The value associated with this key (out parameter), or the default value if not found</param>
        /// <returns>True if there a value associated with the key was found, false otherwise.</returns>
        public bool TryGetValue(T /*?*/ o, out T /*?*/ result)
        {
            int k = HashAlgorithms.CompareValues(o, this.value);

            result = default(T);
            if (k == 0)
            {
                result = this.value;
                return(true);
            }
            else if (k < 0)
            {
                return((object)this.left == null ? false : this.left.TryGetValue(o, out result));
            }

            else //if (k > 0)
            {
                return((object)this.right == null ? false : this.right.TryGetValue(o, out result));
            }
        }
Exemplo n.º 6
0
 private static RedBlackTree <T> /*?*/ RemoveHelper(RedBlackTree <T> /*?*/ t, T /*?*/ o, out bool deleted)
 {
     if ((object)t == null)
     {
         deleted = false;
         return(t);
     }
     else
     {
         int k = HashAlgorithms.CompareValues(o, t.value);
         if (k == 0)
         {
             deleted = true;
             if (t.left == null)
             {
                 return(t.right);
             }
             else if (t.right == null)
             {
                 return(t.left);
             }
             else
             {
                 return(Relink(t.left, t.right));
             }
         }
         else if (k < 0)
         {
             return(Balance(t.color, t.value, RemoveHelper(t.left, o, out deleted), t.right));
         }
         else
         {
             return(Balance(t.color, t.value, t.left, RemoveHelper(t.right, o, out deleted)));
         }
     }
 }
Exemplo n.º 7
0
 public override int GetHashCode()
 {
     return(HashAlgorithms.CombineHashCodes(TypedHash <RedBlackTree <T> > .StaticTypeHash,
                                            GetHashCodeKeys().GetEnumerator()));
 }
Exemplo n.º 8
0
        /// <summary>
        /// Tests whether the given element is found in the collection value.
        /// </summary>
        /// <param name="item">The item to find</param>
        /// <returns>True, if the <paramref name="item"/> is in this collection value, false otherwise.</returns>
        /// <remarks>
        /// Complexity: O(log(this.Count))
        /// </remarks>
        public override bool Contains(T item)
        {
            T /*?*/ val;

            return(this.representation.TryGetValue(item, HashAlgorithms.GetHashCode(item), 0, out val));
        }
Exemplo n.º 9
0
 /// <summary>
 /// Looks up a value associated with a given key
 /// </summary>
 /// <param name="o">The key</param>
 /// <param name="valueFound">The value associated with this key (out parameter), or the default value of type T if not found</param>
 /// <returns>True if there a value associated with the key was found, false otherwise.</returns>
 public bool TryGetValue(T o, out T /*?*/ valueFound)
 //^ ensures result <==> this.Contains(o);
 //^ ensures !result ==> valueFound == default(T);
 {
     return(this.representation.TryGetValue(o, HashAlgorithms.GetHashCode(o), 0, out valueFound));
 }
Exemplo n.º 10
0
 public static int ComputeEnumeratedHash(System.Collections.IEnumerator /*?*/ values)
 {
     return(HashAlgorithms.ComputeEnumeratedHash(typeHash, values));
 }