Esempio n. 1
0
 public void TryRemove_Testing()
 {
     // removing a non-existing value should return false
     {
         IAvlTree <int> tree = AvlTreeLinked.New <int>();
         tree.Add(1);
         tree.Add(3);
         Assert.IsFalse(tree.TryRemove(2).Success);
     }
     // normal remove checking
     {
         IAvlTree <int> tree = AvlTreeLinked.New <int>();
         tree.Add(1);
         tree.Add(2);
         tree.Add(3);
         Assert.IsTrue(tree.Count is 3);
         Assert.IsTrue(tree.TryRemove(1).Success);
         Assert.IsFalse(tree.Contains(1));
         Assert.IsTrue(tree.Count is 2);
         Assert.IsTrue(tree.TryRemove(2).Success);
         Assert.IsFalse(tree.Contains(2));
         Assert.IsTrue(tree.Count is 1);
         Assert.IsTrue(tree.TryRemove(3).Success);
         Assert.IsFalse(tree.Contains(3));
         Assert.IsTrue(tree.Count is 0);
     }
     {
         IAvlTree <int> tree = AvlTreeLinked.New <int>();
         Iterate(100, i => tree.Add(i));
         Assert.IsTrue(tree.Count is 100);
         Iterate(100, i => tree.Remove(i));
         Assert.IsTrue(tree.Count is 0);
     }
 }
Esempio n. 2
0
 /// <summary>Wrapper for the remove function to handle exceptions.</summary>
 /// <typeparam name="T">The generic type of this data structure.</typeparam>
 /// <typeparam name="Key">The type of the key.</typeparam>
 /// <param name="avlTree">This structure.</param>
 /// <param name="key">The key.</param>
 /// <param name="compare">The sorting technique (must synchronize with this structure's sorting).</param>
 /// <returns>True if successful, False if not.</returns>
 public static bool TryRemove <T, Key>(this IAvlTree <T> avlTree, Key key, Compare <T, Key> compare)
 {
     try
     {
         avlTree.Remove(key, compare);
         return(true);
     }
     catch
     {
         return(false);
     }
 }
Esempio n. 3
0
 /// <summary>Wrapper for the remove function to handle exceptions.</summary>
 /// <typeparam name="T">The generic type of this data structure.</typeparam>
 /// <param name="avlTree">This structure.</param>
 /// <param name="compare">The sorting technique (must synchronize with this structure's sorting).</param>
 /// <returns>True if successful, False if not.</returns>
 public static bool TryRemove <T>(this IAvlTree <T> avlTree, CompareToKnownValue <T> compare)
 {
     try
     {
         avlTree.Remove(compare);
         return(true);
     }
     catch
     {
         return(false);
     }
 }
Esempio n. 4
0
        public void Remove_Testing()
        {
            // removing a non-existing value should throw exceptions
            {
                IAvlTree <int> tree = AvlTreeLinked.New <int>();
                tree.Add(1);
                tree.Add(3);
                Assert.ThrowsException <ArgumentException>(() => tree.Remove(2));
            }
            // normal remove checking
            {
                IAvlTree <int> tree = AvlTreeLinked.New <int>();
                tree.Add(1);
                tree.Add(2);
                tree.Add(3);
                Assert.IsTrue(tree.Count is 3);
                tree.Remove(1);
                Assert.IsFalse(tree.Contains(1));
                Assert.IsTrue(tree.Count is 2);
                tree.Remove(2);
                Assert.IsFalse(tree.Contains(2));
                Assert.IsTrue(tree.Count is 1);
                tree.Remove(3);
                Assert.IsFalse(tree.Contains(3));
                Assert.IsTrue(tree.Count is 0);
            }
            {
                IAvlTree <int> tree = AvlTreeLinked.New <int>();
                Iterate(100, i => tree.Add(i));
                Assert.IsTrue(tree.Count is 100);
                Iterate(100, i => tree.Remove(i));
                Assert.IsTrue(tree.Count is 0);
            }
            // large randomized data set
            {
                const int      count = 1000;
                IAvlTree <int> tree  = AvlTreeLinked.New <int>();
                for (int i = 0; i < count; i++)
                {
                    Assert.IsTrue(tree.Count == i);
                    tree.Add(i);
                    Assert.IsTrue(tree.Count == i + 1);
                    Assert.IsTrue(tree.Contains(i));

                    int stepperCount = 0;
                    tree.Stepper(x => stepperCount++);
                    Assert.IsTrue(stepperCount == tree.Count);
                }
                int[] values = new int[count];
                for (int i = 0; i < count; i++)
                {
                    values[i] = i;
                }
                Random random = new(7);
                Shuffle <int>(values, random);
                for (int i = 0; i < count; i++)
                {
                    Assert.IsTrue(tree.Count == count - i);
                    tree.Remove(values[i]);
                    Assert.IsTrue(tree.Count == count - i - 1);
                    Assert.IsFalse(tree.Contains(values[i]));

                    int stepperCount = 0;
                    tree.Stepper(x => stepperCount++);
                    Assert.IsTrue(stepperCount == tree.Count);
                }
            }
        }