Ejemplo n.º 1
0
        private void SplitIntersection(Range <T> top, Range <T> intersected, out List <Range <T> > leftovers, out List <Range <T> > newRanges)
        {
            leftovers = new List <Range <T> >();
            newRanges = new List <Range <T> >();

            Range <T> left = new Range <T>
            {
                StartIndex = Math.Min(top.StartIndex, intersected.StartIndex),
                EndIndex   = Math.Max(top.StartIndex, intersected.StartIndex) - 1,
                Values     = new HashSet <T>()
            };

            Range <T> right = new Range <T>
            {
                StartIndex = Math.Min(top.EndIndex, intersected.EndIndex) + 1,
                EndIndex   = Math.Max(top.EndIndex, intersected.EndIndex),
                Values     = new HashSet <T>()
            };

            Range <T> intersection = new Range <T>
            {
                StartIndex = Math.Max(top.StartIndex, intersected.StartIndex),
                EndIndex   = Math.Min(top.EndIndex, intersected.EndIndex),
                Values     = new HashSet <T>()
            };

            //create intersection
            intersection.AddValues(top.Values);
            intersection.AddValues(intersected.Values);
            newRanges.Add(intersection);

            //top starts before
            if (top.StartIndex < intersected.StartIndex)
            {
                left.AddValues(top.Values);
                leftovers.Add(left);
            }
            //intersected starts before
            else if (intersected.StartIndex < top.StartIndex)
            {
                left.AddValues(intersected.Values);
                newRanges.Add(left);
            }

            //top ends after
            if (top.EndIndex > intersected.EndIndex)
            {
                right.AddValues(top.Values);
                leftovers.Add(right);
            }
            //intersected ends after
            else if (intersected.EndIndex > top.EndIndex)
            {
                right.AddValues(intersected.Values);
                newRanges.Add(right);
            }
        }