Java List Intersect removeIntersection(List intervals, List intervalsToRemove)

Here you can find the source of removeIntersection(List intervals, List intervalsToRemove)

Description

remove Intersection

License

Open Source License

Declaration

public static void removeIntersection(List<int[]> intervals,
            List<int[]> intervalsToRemove) 

Method Source Code

//package com.java2s;
/*/*from  ww  w  .ja v a  2 s. c o m*/
 * Copyright (c) 2008-2013 Maksim Khadkevich and Fondazione Bruno Kessler.
 *
 * This file is part of MART.
 * MART is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2, as published
 * by the Free Software Foundation.
 *
 * MART is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * You should have received a copy of the GNU General Public License
 * along with MART; if not, write to the Free Software Foundation,
 * Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

import java.util.*;

public class Main {
    public static void removeIntersection(List<int[]> intervals,
            List<int[]> intervalsToRemove) {
        List<int[]> newIntervals;
        boolean isChanged = true;

        while (isChanged) {
            isChanged = false;
            for (int[] intervalToRemove : intervalsToRemove) {
                for (int[] interval : intervals) {
                    if (intersect(intervalToRemove, interval)) {
                        newIntervals = getNonIntersection(interval,
                                getIntersection(interval, intervalToRemove));
                        intervals.remove(interval);
                        for (int[] newInterval : newIntervals) {
                            intervals.add(newInterval);
                        }
                        isChanged = true;
                        break;
                    }
                }
            }
        }
    }

    /**
     * returns true if 2 sections intersect
     *
     * @param start1 start1
     * @param end1   end1
     * @param start2 start2
     * @param end2   end2
     * @return if they intersect
     */
    public static boolean intersect(int start1, int end1, int start2,
            int end2) {
        return (end1 > start2) && (end2 > start1);
    }

    public static boolean intersect(int[] interval1, int[] interval2) {
        return intersect(interval1[0], interval1[1], interval2[0],
                interval2[1]);
    }

    /**
     * Returns interval(s) of non-intersection area for interval1
     *
     * @param interval
     * @param intervalToRemove
     * @return
     */
    public static List<int[]> getNonIntersection(int[] interval,
            int[] intervalToRemove) {
        List<int[]> outList = new ArrayList<int[]>();

        int[] intersection = getIntersection(interval, intervalToRemove);

        if (intersection[0] == interval[0]) {
            if (intersection[1] == interval[1]) {
                //There is no intervals for output
            } else {
                outList.add(new int[] { intersection[1] + 1, interval[1] });
            }
            return outList;
        }

        if (intersection[1] == interval[1]) {
            outList.add(new int[] { interval[0], intersection[0] - 1 });
        } else {
            //The intersection is inside the interval
            outList.add(new int[] { interval[0], intersection[0] - 1 });
            outList.add(new int[] { intersection[1] + 1, interval[1] });
        }
        return outList;
    }

    public static int[] getIntersection(int start1, int end1, int start2,
            int end2) {
        int start = Math.max(start1, start2);
        int end = Math.min(end1, end2);
        return new int[] { start, end };
    }

    public static int[] getIntersection(int[] interval1, int[] interval2) {
        return getIntersection(interval1[0], interval1[1], interval2[0],
                interval2[1]);
    }
}

Related

  1. intersection_type(List list1, List list2)
  2. intersectionInplace(List> sets)
  3. intersectList(List list1, List list2)
  4. intersectSortedAscending(List l1, List l2)
  5. isListIntersect(List list1, List list2)