Java Array Intersect getNonIntersection(int[] interval, int[] intervalToRemove)

Here you can find the source of getNonIntersection(int[] interval, int[] intervalToRemove)

Description

Returns interval(s) of non-intersection area for interval1

License

Open Source License

Parameter

Parameter Description
interval a parameter
intervalToRemove a parameter

Declaration

public static List<int[]> getNonIntersection(int[] interval,
        int[] intervalToRemove) 

Method Source Code

//package com.java2s;
/*//  w w w  .j a v a2 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 {
    /**
     * 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. arrayInterp(double[] inputArray, double index)
  2. arraysIntersect(Object[] array1, Object[] array2)
  3. byteIntersection(byte[] a, byte[] b)
  4. getArrayIntersection(int a[], int b[])
  5. hasIntersection(String a1[], String a2[], int mode)
  6. intersect(boolean[] mask, int[] examples, boolean[] intersection)
  7. intersect(int[] sorted1, int[] sorted2)
  8. intersect(int[]... arrays)