Ejemplo n.º 1
0
 internal Enumerator(TreeSet <T> tree)
 {
     host = new RBTree.NodeEnumerator(tree.tree);
 }
Ejemplo n.º 2
0
        static void Main(string [] args)
        {
            Random r = new Random();
            Dictionary <int, int> d = new Dictionary <int, int> ();
            TreeSet <int>         t = new TreeSet <int> ();
            int iters     = args.Length == 0 ? 100000 : Int32.Parse(args [0]);
            int watermark = 1;

            for (int i = 0; i < iters; ++i)
            {
                if (i >= watermark)
                {
                    watermark += 1 + watermark / 4;
                    t.VerifyInvariants();
                }

                int n = r.Next();
                if (d.ContainsKey(n))
                {
                    continue;
                }
                d [n] = n;

                try {
                    if (t.Contains(n))
                    {
                        throw new Exception("tree says it has a number it shouldn't");
                    }
                    if (!t.Insert(n))
                    {
                        throw new Exception("tree says it has a number it shouldn't");
                    }
                } catch {
                    Console.Error.WriteLine("Exception while inserting {0} in iteration {1}", n, i);
                    throw;
                }
            }
            t.VerifyInvariants();
            if (d.Count != t.Count)
            {
                throw new Exception("tree count is wrong?");
            }

            Console.WriteLine("Tree has {0} elements", t.Count);

            foreach (int n in d.Keys)
            {
                if (!t.Contains(n))
                {
                    throw new Exception("tree says it doesn't have a number it should");
                }
            }

            Dictionary <int, int> d1 = new Dictionary <int, int> (d);

            int prev = -1;

            foreach (int n in t)
            {
                if (n < prev)
                {
                    throw new Exception("iteration out of order");
                }
                if (!d1.Remove(n))
                {
                    throw new Exception("tree has a number it shouldn't");
                }
                prev = n;
            }

            if (d1.Count != 0)
            {
                throw new Exception("tree has numbers it shouldn't");
            }

            for (int i = 0; i < iters; ++i)
            {
                int n = r.Next();
                if (!d.ContainsKey(n))
                {
                    if (t.Contains(n))
                    {
                        throw new Exception("tree says it doesn't have a number it should");
                    }
                }
                else if (!t.Contains(n))
                {
                    throw new Exception("tree says it has a number it shouldn't");
                }
            }

            int count = t.Count;

            foreach (int n in d.Keys)
            {
                if (count <= watermark)
                {
                    watermark -= watermark / 4;
                    t.VerifyInvariants();
                }
                try {
                    if (!t.Remove(n))
                    {
                        throw new Exception("tree says it doesn't have a number it should");
                    }
                    --count;
                    if (t.Count != count)
                    {
                        throw new Exception("Remove didn't remove exactly one element");
                    }
                } catch {
                    Console.Error.WriteLine("While trying to remove {0} from tree of size {1}", n, t.Count);
                    t.Dump();
                    t.VerifyInvariants();
                    throw;
                }
                if (t.Contains(n))
                {
                    throw new Exception("tree says it has a number it shouldn't");
                }
            }
            t.VerifyInvariants();

            if (t.Count != 0)
            {
                throw new Exception("tree claims to have elements");
            }
        }