Java Array Sort addSortedUnique(T[] array, T object, int start)

Here you can find the source of addSortedUnique(T[] array, T object, int start)

Description

Adds an object to an array that presumably contains sorted elements, but only if it isn't found via binary search.

License

Open Source License

Parameter

Parameter Description
T the object type stored in the array that extends Comparable .
array the array to add an object to.
object the object to add.
start the index to add it to (the contents are replaced).

Exception

Parameter Description
NullPointerException if a comparison happens on a null object at some point.

Return

the final index in the array of the added object, or -1 if not added.

Declaration

public static <T extends Comparable<T>> int addSortedUnique(T[] array, T object, int start) 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright (c) 2009-2016 Black Rook Software
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the GNU Lesser Public License v2.1
 * which accompanies this distribution, and is available at
 * http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html
 ******************************************************************************/

import java.util.Arrays;

import java.util.Comparator;

public class Main {
    /**/*from   ww  w .  j  a v a  2s  .  c o m*/
     * Adds an object to an array that presumably contains sorted elements, but only if it isn't found via binary search.
     * An object is added at some point in the array, and the element is shifted down to an appropriate
     * position according to the object's {@link Comparable#compareTo(Object)} function.
     * @param <T> the object type stored in the array that extends {@link Comparable}.
     * @param array the array to add an object to.
     * @param object the object to add.
     * @param start the index to add it to (the contents are replaced).
     * @return the final index in the array of the added object, or -1 if not added.
     * @throws NullPointerException if a comparison happens on a null object at some point.
     * @see Comparable#compareTo(Object)
     * @see #addSorted(Comparable[], Comparable, int)
     * @since 2.21.0 
     */
    public static <T extends Comparable<T>> int addSortedUnique(T[] array, T object, int start) {
        if (Arrays.binarySearch(array, 0, start, object) < 0)
            return addSorted(array, object, start);
        else
            return -1;
    }

    /**
     * Adds an object to an array that presumably contains sorted elements, but only if it isn't found via binary search.
     * An object is added at some point in the array, and the element is shifted down to an appropriate
     * position according to the object's {@link Comparable#compareTo(Object)} function.
     * @param <T> the object type stored in the arrays.
     * @param array the array to add an object to.
     * @param object the object to add.
     * @param start the index to add it to (the contents are replaced).
     * @param comparator the comparator to use for comparisons.
     * @return the final index in the array of the added object, or -1 if not added.
     * @throws NullPointerException if a comparison happens on a null object at some point.
     * @see Comparable#compareTo(Object)
     * @see #addSorted(Object[], Object, int, Comparator)
     * @since 2.21.0 
     */
    public static <T> int addSortedUnique(T[] array, T object, int start, Comparator<T> comparator) {
        if (Arrays.binarySearch(array, 0, start, object, comparator) < 0)
            return addSorted(array, object, start, comparator);
        else
            return -1;
    }

    /**
     * Adds an object to an array that presumably contains sorted elements.
     * An object is added at some point in the array, and the element is shifted down to an appropriate
     * position according to the object's {@link Comparable#compareTo(Object)} function.
     * @param <T> the object type stored in the array that extends {@link Comparable}.
     * @param array the array to add an object to.
     * @param object the object to add.
     * @param start the index to add it to (the contents are replaced).
     * @return the final index in the array of the added object.
     * @throws NullPointerException if a comparison happens on a null object at some point.
     * @see Comparable#compareTo(Object)
     * @see #sortFrom(Comparable[], int)
     * @since 2.21.0 
     */
    public static <T extends Comparable<T>> int addSorted(T[] array, T object, int start) {
        array[start] = object;
        return sortFrom(array, start);
    }

    /**
     * Adds an object to an array that presumably contains sorted elements.
     * An object is added at some point in the array, and the element is shifted down to an appropriate
     * position according to the object's {@link Comparable#compareTo(Object)} function.
     * @param <T> the object type stored in the arrays.
     * @param array the array to add an object to.
     * @param object the object to add.
     * @param start the index to add it to (the contents are replaced).
     * @param comparator the comparator to use for comparisons.
     * @return the final index in the array of the added object.
     * @throws NullPointerException if a comparison happens on a null object at some point.
     * @see Comparable#compareTo(Object)
     * @see #sortFrom(Object[], int, Comparator)
     * @since 2.21.0 
     */
    public static <T> int addSorted(T[] array, T object, int start, Comparator<T> comparator) {
        array[start] = object;
        return sortFrom(array, start, comparator);
    }

    /**
     * Shifts an object to an appropriate position according to the object's {@link Comparable#compareTo(Object)} function.
     * @param <T> the object type stored in the array that extends {@link Comparable}.
     * @param array the array to shift the contents of.
     * @param index the index to add it to (the contents are replaced).
     * @return the final index in the array of the sorted object.
     * @since 2.21.0 
     */
    public static <T extends Comparable<T>> int sortFrom(T[] array, int index) {
        while (index > 0 && array[index].compareTo(array[index - 1]) < 0) {
            arraySwap(array, index, index - 1);
            index--;
        }
        return index;
    }

    /**
     * Shifts an object to an appropriate position according to the provided <code>comparator</code> function.
     * @param <T> the object type stored in the arrays.
     * @param array the array to shift the contents of.
     * @param index the index to add it to (the contents are replaced).
     * @param comparator the comparator to use.
     * @return the final index in the array of the sorted object.
     * @since 2.21.0 
     */
    public static <T> int sortFrom(T[] array, int index, Comparator<? super T> comparator) {
        while (index > 0 && comparator.compare(array[index], array[index - 1]) < 0) {
            arraySwap(array, index, index - 1);
            index--;
        }
        return index;
    }

    /**
     * Swaps the contents of two indices of an array.
     * @param <T> the object type stored in the array.
     * @param array the input array.
     * @param a the first index.
     * @param b the second index.
     * @since 2.21.0
     */
    public static <T> void arraySwap(T[] array, int a, int b) {
        T temp = array[a];
        array[a] = array[b];
        array[b] = temp;
    }
}

Related

  1. addSorted(int[] array, int[] newValues)
  2. addToSortedIntArray(int[] a, int value)
  3. bitReversalSort(final short[] real)
  4. calculatePValueForDataPoint(double dataPoint, double[] sortedNullHypothesisSample)
  5. collectionToSortedArray(Collection objects)