private void SwapOut(T[] entries)
 {
     if (Median.CompareTo(entries[Left]) == 0)
     {
         Swap(entries, LeftMedian++, Left);
     }
     if (Median.CompareTo(entries[Right]) == 0)
     {
         Swap(entries, Right, RightMedian--);
     }
 }
        private void Partition(T[] entries)
        {
            int first;
            int last;

#if Tripartite
            LeftMedian  = first;
            RightMedian = last;
#endif
            while (true)
            {
                //[Assert]There exists some index >= Left where entries[index] >= Median
                //[Assert]There exists some index <= Right where entries[index] <= Median
                // So, there is no need for Left or Right bound checks
                while (Median.CompareTo(entries[Left]) > 0)
                {
                    Left++;
                }
                while (Median.CompareTo(entries[Right]) < 0)
                {
                    Right--;
                }

                //[Assert]entries[Right] <= Median <= entries[Left]
                if (Right <= Left)
                {
                    break;
                }

                Swap(entries, Left, Right);
                SwapOut(entries);
                Left++;
                Right--;
                //[Assert]entries[first:Left - 1] <= Median <= entries[Right + 1:last]
            }

            if (Left == Right)
            {
                Left++;
                Right--;
            }
            //[Assert]Right < Left
            SwapIn(entries, first, last);

            //[Assert]entries[first:Right] <= Median <= entries[Left:last]
            //[Assert]entries[Right + 1:Left - 1] == Median when non-empty
        }
Esempio n. 3
0
        /// <summary>
        /// Retrieves all intervals which lay within the search interval and adds them to the given list.
        /// </summary>
        /// <example>
        /// Given the following intervals [0]-[4] and the search interval [x]:
        /// [0] |-------|
        /// [1]     |-----|
        /// [2]        |----|
        /// [3]          |----|
        /// [4]           |-------|
        /// [x]     |---------|
        /// [1], [2], [3] would be included in the result set. [0], [4] would not be included.
        /// </example>
        /// <param name="start">The lower boundary of the search interval.</param>
        /// <param name="end">The upper bound of the search interval.</param>
        /// <param name="list">A list to which resulting intervals can be added. Items are added in no particular order!</param>
        public void GetBetween(T start, T end, List <IInterval <T> > list)
        {
            if (Left != null && Left.IntervalMax.CompareTo(start) >= 0)
            {
                Left.GetBetween(start, end, list);
            }
            if (Right != null && Right.IntervalMin.CompareTo(end) <= 0)
            {
                Right.GetBetween(start, end, list);
            }

            if (OverlappingByStart is null || OverlappingByEnd is null || Median.CompareTo(start) < 0 || Median.CompareTo(end) > 0)
            {
                return;
            }

            if (OverlappingMin.CompareTo(start) >= 0)
            {
                var index = 0;
                while (index < OverlappingByEnd.Count && OverlappingByEnd[index].End.CompareTo(end) <= 0)
                {
                    index++;
                }
                list.AddRange(OverlappingByEnd.GetRange(0, index));
            }
            else if (OverlappingMax.CompareTo(end) <= 0)
            {
                var index = OverlappingByStart.Count - 1;
                while (index >= 0 && OverlappingByStart[index].Start.CompareTo(start) >= 0)
                {
                    index--;
                }
                index++;
                list.AddRange(OverlappingByStart.GetRange(index, OverlappingByStart.Count - index));
            }
            else
            {
                list.AddRange(OverlappingByStart.Where(i => i.Start.CompareTo(start) >= 0 && i.End.CompareTo(end) <= 0));
            }
        }