public virtual void TestIteratorOrdered()
        {
            int n = 1000;
            IntervalTree <int, Interval <int> > tree = new IntervalTree <int, Interval <int> >();
            IList <Interval <int> >             list = new List <Interval <int> >(n + 1);

            for (int i = 0; i < n; i++)
            {
                int            x        = i;
                int            y        = i + 1;
                Interval <int> interval = Interval.ToValidInterval(x, y);
                tree.Add(interval);
                list.Add(interval);
            }
            list.Sort();
            Interval <int> next = null;
            IEnumerator <Interval <int> > iterator = tree.GetEnumerator();

            for (int i_1 = 0; i_1 < list.Count; i_1++)
            {
                NUnit.Framework.Assert.IsTrue("HasItem " + i_1, iterator.MoveNext());
                next = iterator.Current;
                NUnit.Framework.Assert.AreEqual("Item " + i_1, list[i_1], next);
            }
            NUnit.Framework.Assert.IsFalse("No more items", iterator.MoveNext());
        }
        public static bool ContainsValue <E, T>(IntervalTree <E, T> node, T target)
            where E : IComparable <E>
            where T : IHasInterval <E>
        {
            IPredicate <T> containsTargetFunction = new IntervalTree.ContainsValueFunction(target);

            return(Contains(node, target.GetInterval(), containsTargetFunction));
        }
        public static IList <T> GetNonOverlapping <T, E, _T2, _T3>(IList <_T2> items, IFunction <_T3> toIntervalFunc)
            where E : IComparable <E>
            where _T2 : T
        {
            IList <T> nonOverlapping = new List <T>();
            IntervalTree <E, Interval <E> > intervals = new IntervalTree <E, Interval <E> >();

            foreach (T item in items)
            {
                Interval <E> i     = toIntervalFunc.Apply(item);
                bool         addOk = intervals.AddNonOverlapping(i);
                if (addOk)
                {
                    nonOverlapping.Add(item);
                }
            }
            return(nonOverlapping);
        }
        public static IList <T> GetNonNested <T, E, _T2, _T3, _T4>(IList <_T2> items, IFunction <_T3> toIntervalFunc, IComparator <_T4> compareFunc)
            where E : IComparable <E>
            where _T2 : T
        {
            IList <T> sorted = new List <T>(items);

            sorted.Sort(compareFunc);
            IList <T> res = new List <T>();
            IntervalTree <E, Interval <E> > intervals = new IntervalTree <E, Interval <E> >();

            foreach (T item in sorted)
            {
                Interval <E> i     = toIntervalFunc.Apply(item);
                bool         addOk = intervals.AddNonNested(i);
                if (addOk)
                {
                    res.Add(item);
                }
            }
            //        log.info("Discarding " + item);
            return(res);
        }
 public static bool ContainsInterval <E, T>(IntervalTree <E, T> n, E p, bool exact)
     where E : IComparable <E>
     where T : IHasInterval <E>
 {
     return(ContainsInterval(n, Interval.ToInterval(p, p), exact));
 }
 private static bool Contains <E, T>(IntervalTree <E, T> tree, Interval <E> target, IPredicate <T> containsTargetFunction)
     where E : IComparable <E>
     where T : IHasInterval <E>
 {
     return(Contains(tree.root, target, containsTargetFunction));
 }
        public virtual void TestGetOverlapping()
        {
            Interval <int> a        = Interval.ToInterval(249210699, 249212659);
            Interval <int> before   = Interval.ToInterval(249210000, 249210600);
            Interval <int> included = Interval.ToInterval(249210800, 249212000);
            Interval <int> after    = Interval.ToInterval(249213000, 249214000);
            IntervalTree <int, Interval <int> > tree = new IntervalTree <int, Interval <int> >();

            tree.Add(a);
            IList <Interval <int> > overlapping1 = tree.GetOverlapping(before);

            NUnit.Framework.Assert.IsTrue(overlapping1.IsEmpty());
            IList <Interval <int> > overlapping2 = tree.GetOverlapping(included);

            NUnit.Framework.Assert.IsTrue(overlapping2.Count == 1);
            IList <Interval <int> > overlapping3 = tree.GetOverlapping(after);

            NUnit.Framework.Assert.IsTrue(overlapping3.IsEmpty());
            // Remove a
            tree.Remove(a);
            NUnit.Framework.Assert.IsTrue(tree.Count == 0);
            int n = 20000;

            // Add a bunch of interval before adding a
            for (int i = 0; i < n; i++)
            {
                int            x        = i;
                int            y        = i + 1;
                Interval <int> interval = Interval.ToInterval(x, y);
                tree.Add(interval);
            }
            tree.Add(a);
            overlapping1 = tree.GetOverlapping(before);
            NUnit.Framework.Assert.IsTrue(overlapping1.IsEmpty());
            overlapping2 = tree.GetOverlapping(included);
            NUnit.Framework.Assert.IsTrue(overlapping2.Count == 1);
            overlapping3 = tree.GetOverlapping(after);
            NUnit.Framework.Assert.IsTrue(overlapping3.IsEmpty());
            NUnit.Framework.Assert.IsTrue(tree.Height() < 20);
            // Try balancing the tree
            //    System.out.println("Height is " + tree.height());
            tree.Check();
            tree.Balance();
            int height = tree.Height();

            NUnit.Framework.Assert.IsTrue(height < 20);
            tree.Check();
            overlapping1 = tree.GetOverlapping(before);
            NUnit.Framework.Assert.IsTrue(overlapping1.IsEmpty());
            overlapping2 = tree.GetOverlapping(included);
            NUnit.Framework.Assert.IsTrue(overlapping2.Count == 1);
            overlapping3 = tree.GetOverlapping(after);
            NUnit.Framework.Assert.IsTrue(overlapping3.IsEmpty());
            // Clear tree
            tree.Clear();
            NUnit.Framework.Assert.IsTrue(tree.Count == 0);
            // Add a bunch of random interval before adding a
            Random rand = new Random();
            IList <Interval <int> > list = new List <Interval <int> >(n + 1);

            for (int i_1 = 0; i_1 < n; i_1++)
            {
                int            x        = rand.NextInt();
                int            y        = rand.NextInt();
                Interval <int> interval = Interval.ToValidInterval(x, y);
                tree.Add(interval);
                list.Add(interval);
            }
            tree.Add(a);
            list.Add(a);
            overlapping1 = tree.GetOverlapping(before);
            CheckOverlapping(list, overlapping1, before);
            overlapping2 = tree.GetOverlapping(included);
            CheckOverlapping(list, overlapping2, included);
            overlapping3 = tree.GetOverlapping(after);
            CheckOverlapping(list, overlapping3, after);
        }