Пример #1
0
        public IntervalNode(List <Interval <T> > intervals)
        {
            if (intervals.Count == 0)
            {
                return;
            }

            this.Point = this.FindMedianEndpoint(intervals);

            this.LeftIntervals  = this.GetIntersectingIntervals(intervals).OrderBy(x => x.Min).ToList();
            this.RightIntervals = this.GetIntersectingIntervals(intervals).OrderBy(x => x.Max).ToList();

            this.MinPoint = this.LeftIntervals.First().Min.Value;
            this.MaxPoint = this.RightIntervals.Last().Max.Value;

            this.SpanningInterval = new Interval <T>(this.MinPoint, this.MaxPoint);

            //if i.min < v_pt then it goes to the left subtree
            var leftSegment  = this.GetLeftIntervals(intervals);
            var rightSegment = this.GetRightIntervals(intervals);

            if (leftSegment.Count > 0)
            {
                this.LeftNode = new IntervalNode <T>(leftSegment);
            }

            if (rightSegment.Count > 0)
            {
                this.RightNode = new IntervalNode <T>(rightSegment);
            }
        }
Пример #2
0
        protected void SearchInternal(IntervalNode <T> node, Interval <T> searchInterval, LinkedList <Interval <T> > retList)
        {
            if (null == node)
            {
                return;
            }

            if (node.SpanningInterval.Overlaps(searchInterval))
            {
                //if searchInterval.contains(node.v_pt)
                //then add every interval contained in this node to the result set then search left and right for further
                //overlapping intervals
                if (searchInterval.Contains(node.Point))
                {
                    foreach (var interval in node.LeftIntervals)
                    {
                        retList.AddLast(interval);
                    }

                    this.SearchInternal(node.LeftNode, searchInterval, retList);
                    this.SearchInternal(node.RightNode, searchInterval, retList);
                    return;
                }

                // point < searchInterval.left
                if (node.Point.CompareTo(searchInterval.Min.Value) < 0)
                {
                    // TODO: Could probably do a binary search to speed things up.

                    foreach (var interval in node.RightIntervals)
                    {
                        if (interval.Max.CompareTo(searchInterval.Min) >= 0)
                        {
                            retList.AddLast(interval);
                        }
                    }
                    return;
                }

                // point > searchInterval.right
                if (node.Point.CompareTo(searchInterval.Max.Value) > 0)
                {
                    foreach (var interval in node.LeftIntervals)
                    {
                        if (interval.Min.CompareTo(searchInterval.Max) <= 0)
                        {
                            retList.AddLast(interval);
                        }
                    }
                    return;
                }
            }

            //if v.pt < searchInterval.left
            //add intervals in v with v[i].right >= searchitnerval.left
            //L contains no overlaps
            //R May
            if (node.Point.CompareTo(searchInterval.Min.Value) < 0)
            {
                foreach (var interval in node.RightIntervals)
                {
                    if (interval.Max.CompareTo(searchInterval.Min) >= 0)
                    {
                        retList.AddLast(interval);
                    }
                    else
                    {
                        break;
                    }
                }
                this.SearchInternal(node.RightNode, searchInterval, retList);
                return;
            }

            //if v.pt > searchInterval.right
            //add intervals in v with [i].left <= searchitnerval.right
            //R contains no overlaps
            //L May
            if (node.Point.CompareTo(searchInterval.Max.Value) > 0)
            {
                foreach (var interval in node.LeftIntervals)
                {
                    if (interval.Min.CompareTo(searchInterval.Max) <= 0)
                    {
                        retList.AddLast(interval);
                    }
                    else
                    {
                        break;
                    }
                }

                this.SearchInternal(node.LeftNode, searchInterval, retList);
                return;
            }
        }
Пример #3
0
 public IntervalSkipList(List <Interval <T> > intervals)
 {
     this._head = new IntervalNode <T>(intervals);
 }