Example usage for java.util Collections binarySearch

List of usage examples for java.util Collections binarySearch

Introduction

In this page you can find the example usage for java.util Collections binarySearch.

Prototype

public static <T> int binarySearch(List<? extends Comparable<? super T>> list, T key) 

Source Link

Document

Searches the specified list for the specified object using the binary search algorithm.

Usage

From source file:com.jbombardier.console.results.SortedTreeList.java

public int addAndReturnIndex(T item) {
    int index = Collections.binarySearch(this, item);

    if (index < 0) {
        index = ~index;//from   w ww .j  a  v  a  2s  .  c  o  m
    }

    super.add(index, item);
    return index;
}

From source file:com.jbombardier.console.results.SortedTreeList.java

public boolean removeFast(T item) {

    int index = Collections.binarySearch(this, item);

    boolean removed;
    if (index < 0) {
        // This didn't exist
        removed = false;//ww  w. j a  v  a 2s .  co m
    } else {
        remove(index);
        removed = true;
    }

    return removed;
}

From source file:Main.java

/**
 * Returns the index of the largest value in an list that is less than (or optionally equal to)
 * a specified key./*  w  w  w . ja va  2s.  c  o m*/
 * <p>
 * The search is performed using a binary search algorithm, and so the list must be sorted.
 *
 * @param list The list to search.
 * @param key The key being searched for.
 * @param inclusive If the key is present in the list, whether to return the corresponding index.
 *     If false then the returned index corresponds to the largest value in the list that is
 *     strictly less than the key.
 * @param stayInBounds If true, then 0 will be returned in the case that the key is smaller than
 *     the smallest value in the list. If false then -1 will be returned.
 */
public static <T> int binarySearchFloor(List<? extends Comparable<? super T>> list, T key, boolean inclusive,
        boolean stayInBounds) {
    int index = Collections.binarySearch(list, key);
    index = index < 0 ? -(index + 2) : (inclusive ? index : (index - 1));
    return stayInBounds ? Math.max(0, index) : index;
}

From source file:Main.java

/**
 * Returns the index of the smallest value in an list that is greater than (or optionally equal
 * to) a specified key./*from   ww w  .  j a  v  a2  s  .  c  o  m*/
 * <p>
 * The search is performed using a binary search algorithm, and so the list must be sorted.
 *
 * @param list The list to search.
 * @param key The key being searched for.
 * @param inclusive If the key is present in the list, whether to return the corresponding index.
 *     If false then the returned index corresponds to the smallest value in the list that is
 *     strictly greater than the key.
 * @param stayInBounds If true, then {@code (list.size() - 1)} will be returned in the case that
 *     the key is greater than the largest value in the list. If false then {@code list.size()}
 *     will be returned.
 */
public static <T> int binarySearchCeil(List<? extends Comparable<? super T>> list, T key, boolean inclusive,
        boolean stayInBounds) {
    int index = Collections.binarySearch(list, key);
    index = index < 0 ? ~index : (inclusive ? index : (index + 1));
    return stayInBounds ? Math.min(list.size() - 1, index) : index;
}

From source file:de.dhke.projects.cutil.collections.frozen.FrozenSortedList.java

@Override
@SuppressWarnings("unchecked")
public boolean contains(Object o) {
    return Collections.binarySearch(_elements, (T) o) >= 0;
}

From source file:com.epam.dlab.mongo.ResourceItemList.java

/** Appends the resource to the list and returns it.
 * @param resourceId the resource id./*from   ww w.j  av  a  2s.co m*/
 * @param resourceName the user friendly name of resource.
 * @param type the type of resource.
 * @param user the name of user.
 * @param exploratoryName the name of exploratory.
 * @return Instance of the resource.
 */
public ResourceItem append(String resourceId, String resourceName, DlabResourceType type, String user,
        String exploratoryName) {
    ResourceItem item = new ResourceItem(resourceId, resourceName, type, user, exploratoryName);
    synchronized (this) {
        int index = Collections.binarySearch(list, item);
        if (index < 0) {
            index = -index;
            if (index > list.size()) {
                list.add(item);
            } else {
                list.add(index - 1, item);
            }
        } else {
            item = list.get(index);
        }
    }
    return item;
}

From source file:it.crs4.seal.recab.ArrayListVariantTable.java

public boolean isVariantLocation(String chr, long pos) {
    if (pos > Integer.MAX_VALUE)
        throw new RuntimeException("pos bigger than expected!  File a bug!!");

    ArrayList<Integer> list = data.get(chr);
    if (list != null)
        return Collections.binarySearch(list, (int) pos) >= 0;
    else/* ww  w  .  ja  va  2  s  .c  om*/
        return false;
}

From source file:com.florianmski.tracktoid.trakt.tasks.get.CalendarTask.java

@Override
protected boolean doTraktStuffInBackground() {
    //      showToast("Retrieving calendar...", Toast.LENGTH_SHORT);

    ArrayList<CalendarDate> calendarListShows;
    ArrayList<CalendarDate> calendarListPremieres = new ArrayList<CalendarDate>();
    ArrayList<CalendarDate> calendarListMyShows = new ArrayList<CalendarDate>();

    calendarListShows = (ArrayList<CalendarDate>) tm.calendarService().shows().fire();

    DatabaseWrapper dbw = new DatabaseWrapper(context);
    dbw.open();//from w w w  .  j  a v a2 s. c  o  m

    List<TvShow> shows = dbw.getShows();

    dbw.close();

    for (CalendarDate cd : calendarListShows) {
        CalendarDate calendarPremieres = new CalendarDate();
        CalendarDate calendarMyShows = new CalendarDate();

        calendarPremieres.date = cd.date;
        calendarMyShows.date = cd.date;

        List<CalendarTvShowEpisode> episodesPremieres = new ArrayList<CalendarTvShowEpisode>();
        List<CalendarTvShowEpisode> episodesMyShows = new ArrayList<CalendarTvShowEpisode>();

        for (CalendarTvShowEpisode e : cd.episodes) {
            int index = Collections.binarySearch(shows, e.show);

            if (e.episode.number == 1)
                episodesPremieres.add(e);
            if (index != -1 && index >= 0 && index < shows.size())
                episodesMyShows.add(e);
        }

        if (!episodesPremieres.isEmpty()) {
            calendarPremieres.episodes = episodesPremieres;
            calendarListPremieres.add(calendarPremieres);
        }

        if (!episodesMyShows.isEmpty()) {
            calendarMyShows.episodes = episodesMyShows;
            calendarListMyShows.add(calendarMyShows);
        }
    }

    calendars.add(calendarListPremieres);
    calendars.add(calendarListMyShows);
    calendars.add(calendarListShows);

    return true;
}

From source file:org.apache.usergrid.persistence.cassandra.SimpleIndexBucketLocatorImpl.java

/** Get the next token in the ring for this big int. */
private String getClosestToken(UUID entityId) {
    BigInteger location = new BigInteger(md5(bytes(entityId)));
    location = location.abs();//from  w  ww. ja  va 2s .c  o  m

    int index = Collections.binarySearch(buckets, location);

    if (index < 0) {
        index = (index + 1) * -1;
    }

    // mod if we need to wrap
    index = index % size;

    return bucketsString.get(index);
}

From source file:dk.statsbiblioteket.netark.dvenabler.wrapper.SortedDocValuesWrapper.java

@Override
public int getOrd(int docID) {
    tracker.ping(docID);/*from   w w  w.  ja va 2  s  . com*/

    try {
        String value = reader.document(docID, FIELDS).get(field.getName());
        if (value == null) {
            return -1;
        }
        int ord = Collections.binarySearch(values, new BytesRef(value));
        if (ord < 0) {
            throw new IllegalStateException("The ord for value '" + value + "' for docID " + docID
                    + " in field '" + field + "' could not be located but should always be present");
        }
        return ord;
    } catch (IOException e) {
        throw new RuntimeException("Unable to lookup docID=" + docID + ", field=" + field, e);
    }
}