示例#1
0
        /// <summary>
        /// Finds an interval in the set that <see cref="Interval{T}.Overlaps"/>
        /// with <paramref name="interval"/>. Returns null if no such interval is found.
        /// </summary>
        /// <remarks>
        /// The given interval must be non-empty for a match to be found.
        /// </remarks>
        public Interval <T> FindOverlappingInterval(Interval <T> interval)
        {
            var probe = intervals.FindStartNode(interval);

            if (probe != null && probe.Item.Overlaps(interval))
            {
                return(probe.Item);
            }
            probe = intervals.FindEndNode(interval);
            if (probe != null && probe.Item.Overlaps(interval))
            {
                return(probe.Item);
            }
            return(null);
        }
示例#2
0
        public void TestTreeSetStartEnd()
        {
            int[] contents = new[] { 2, 4, 7, 8, 12, 14, 16, 18, 19, 23, 27, 35, 37, 40 };
            var   treeSet  = new TreeSet <int>();

            for (int index = 0; index < contents.Length; index++)
            {
                treeSet.Add(contents[index]);
            }
            Assert.AreEqual(contents.Length, treeSet.Count);

            for (int i = 0; i < 50; i++)
            {
                int expectedStart = -1, expectedEnd = -1;
                int j = 0;
                while (j < contents.Length && contents[j] < i)
                {
                    j++;
                }
                if (j < contents.Length)
                {
                    expectedStart = contents[j];
                }
                j = contents.Length - 1;
                while (j >= 0 && contents[j] > i)
                {
                    j--;
                }
                if (j >= 0)
                {
                    expectedEnd = contents[j];
                }

                var startNode = treeSet.FindStartNode(i);
                var stopNode  = treeSet.FindEndNode(i);

                int actualStart = startNode == null ? -1 : startNode.Item;
                int actualEnd   = stopNode == null ? -1 : stopNode.Item;

                Assert.AreEqual(expectedStart, actualStart);
                Assert.AreEqual(expectedEnd, actualEnd);
            }

            for (int i = 0; i < 50; i++)
            {
                for (int j = 0; j < 50; j++)
                {
                    int actualCount = 0, last = -1;
                    foreach (int x in treeSet.CreateWindow(i, j))
                    {
                        Assert.IsTrue(x > last);
                        Assert.IsTrue(Array.IndexOf(contents, x) >= 0);
                        last = x;
                        actualCount++;
                    }
                    int expectedCount = 0;
                    foreach (int x in contents)
                    {
                        if (x >= i && x <= j)
                        {
                            expectedCount++;
                        }
                    }
                    Assert.AreEqual(expectedCount, actualCount);
                }
            }
        }