Esempio n. 1
0
        public void TestIntervalSetRandom()
        {
            var random = new Random(0);

            for (int i = 0; i < 1000; i++)
            {
                var dummySet = new IntervalSetDummy();
                var fastSet  = new IntervalSet <int>();

                for (int j = 0; j < 50; j++)
                {
                    int start    = random.Next(0, 9000);
                    int end      = start + random.Next(1, 300);
                    var interval = new Interval <int>(start, end);

                    dummySet.Add(interval);
                    fastSet.Add(interval);

                    int chkStart     = random.Next(0, 9000);
                    var chk          = new Interval <int>(chkStart, chkStart + 1);
                    var dummyOverlap = dummySet.FindOverlappingInterval(chk);
                    var fastOverlap  = fastSet.FindOverlappingInterval(chk);
                    Assert.AreEqual(dummyOverlap, fastOverlap);
                }

                Assert.AreEqual(fastSet.ToString(), dummySet.ToString());
            }
        }
Esempio n. 2
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)
        {
            if (interval.Start.CompareTo(modulo) >= 0 || interval.End.CompareTo(modulo) >= 0)
            {
                throw new ArgumentException();
            }
            if (interval.Start.CompareTo(interval.End) < 0)
            {
                return(internalSet.FindOverlappingInterval(interval));
            }

            var overlappingSet = internalSet.FindOverlappingInterval(new Interval <T>(interval.Start, modulo));

            if (overlappingSet != null)
            {
                return(overlappingSet);
            }
            overlappingSet = internalSet.FindOverlappingInterval(new Interval <T>(zero, interval.End));
            return(overlappingSet);
        }