List of usage examples for java.util Arrays binarySearch
public static <T> int binarySearch(T[] a, T key, Comparator<? super T> c)
From source file:Main.java
public static void main(String[] args) { // initializing sorted short array Short shortArr[] = new Short[] { 1, 2, 15, 52, 110 }; // use comparator as null, sorting as natural ordering Comparator<Short> comp = null; // entering the value to be searched short searchVal = 15; int retVal = Arrays.binarySearch(shortArr, searchVal, comp); System.out.println("The index of element 15 is : " + retVal); }
From source file:AlphabeticSearch.java
public static void main(String[] args) { String[] sa = new String[] { "a", "c", "d" }; AlphabeticComparator comp = new AlphabeticComparator(); Arrays.sort(sa, comp);// w w w.ja v a 2 s .co m int index = Arrays.binarySearch(sa, sa[10], comp); System.out.println("Index = " + index); }
From source file:org.broadleafcommerce.common.extensibility.context.merge.handlers.AttributePreserveInsert.java
@Override public Node[] merge(List<Node> nodeList1, List<Node> nodeList2, List<Node> exhaustedNodes) { if (CollectionUtils.isEmpty(nodeList1) || CollectionUtils.isEmpty(nodeList2)) { return null; }// w w w .j a va 2s.co m Node node1 = nodeList1.get(0); Node node2 = nodeList2.get(0); NamedNodeMap attributes2 = node2.getAttributes(); Comparator<Object> nameCompare = new Comparator<Object>() { @Override public int compare(Object arg0, Object arg1) { return ((Node) arg0).getNodeName().compareTo(((Node) arg1).getNodeName()); } }; Node[] tempNodes = {}; tempNodes = exhaustedNodes.toArray(tempNodes); Arrays.sort(tempNodes, nameCompare); int length = attributes2.getLength(); for (int j = 0; j < length; j++) { Node temp = attributes2.item(j); int pos = Arrays.binarySearch(tempNodes, temp, nameCompare); if (pos < 0) { Attr clone = (Attr) temp.cloneNode(true); ((Element) node1).setAttributeNode((Attr) node1.getOwnerDocument().importNode(clone, true)); } } return null; }
From source file:org.apache.pig.backend.hadoop.executionengine.mapReduceLayer.partitioners.WeightedRangePartitioner.java
@SuppressWarnings("unchecked") @Override//from w ww . jav a 2 s . com public int getPartition(PigNullableWritable key, Writable value, int numPartitions) { if (comparator == null) { comparator = (RawComparator<PigNullableWritable>) PigMapReduce.sJobContext.getSortComparator(); } if (!weightedParts.containsKey(key)) { int index = Arrays.binarySearch(quantiles, key, comparator); if (index < 0) index = -index - 1; else index = index + 1; return Math.min(index, numPartitions - 1); } DiscreteProbabilitySampleGenerator gen = weightedParts.get(key); return gen.getNext(); }
From source file:net.darkmist.clf.Util.java
public static <T> T[] merge(T[] a, T[] b, T[] dest, Comparator<T> comparator) { int ai, bi, di; int dlen = a.length + b.length; if (dest.length < dlen) throw new IllegalArgumentException("Destination array is not large enough"); // check for simple case where one whole array goes before the other if (comparator.compare(a[a.length - 1], b[0]) <= 0) return concat(a, b, dest); if (comparator.compare(b[b.length - 1], a[0]) <= 0) return concat(b, a, dest); // binarySearch for the start of one in the other if (comparator.compare(a[0], b[0]) <= 0) { // a starts, figure out where b[0] is ai = Arrays.binarySearch(a, b[0], comparator); if (ai < 0) // ai = (-(insersion_point)-1) ai = -(ai + 1);/* w w w .ja v a 2 s . c o m*/ System.arraycopy(a, 0, dest, 0, ai); di = ai; bi = 0; } else { // b starts, figure out where a[0] is bi = Arrays.binarySearch(b, a[0], comparator); if (bi < 0) // bi = (-(insersion_point)-1) bi = -(bi + 1); System.arraycopy(b, 0, dest, 0, bi); di = bi; ai = 0; } if (logger.isDebugEnabled()) logger.debug("ai=" + ai + " bi=" + bi + " di=" + di); // merge until we run out of one for (; ai < a.length && bi < b.length; di++) { if (comparator.compare(a[ai], b[bi]) <= 0) dest[di] = a[ai++]; else dest[di] = b[bi++]; } // one array has been used up here...copy the rest if (ai < a.length) System.arraycopy(a, ai, dest, di, a.length - ai); else System.arraycopy(b, bi, dest, di, b.length - bi); return dest; }
From source file:edu.cornell.med.icb.goby.alignments.perms.PermutationReader.java
/** * Return the query index associated with a small index, or -1 if the association was not defined. * * @param smallIndex/* w w w .ja v a 2 s .com*/ * @return * @throws IOException */ @Override public int getQueryIndex(int smallIndex) throws IOException { // Util.invertPermutation() singletonQuery.firstSmallIndex = smallIndex; final int ip = Arrays.binarySearch(indexBlocks, singletonQuery, SMALL_INDEX_COMPARATOR); final Block block; if (ip >= 0) { block = indexBlocks[ip]; seek(block.offset + 4); final int first = dataInput.readInt(); assert first == smallIndex : "assertion failed at smallIndex=" + smallIndex; final int queryIndex = dataInput.readInt(); return queryIndex; } else { final int index = -(ip + 1); final int off = index - 1; if (off < 0) { return -1; } block = indexBlocks[off]; final int soffset = smallIndex - block.firstSmallIndex; if (soffset < 0 || block.firstSmallIndex + block.n < smallIndex) { // not in the block return -1; } seek(block.offset + 8 + soffset * 4L); final int queryIndex = dataInput.readInt(); return queryIndex; } }
From source file:org.broadleafcommerce.openadmin.dto.Entity.java
public Property findProperty(String name) { if (properties == null) { return null; }/*from w ww .j a v a2s . c om*/ Arrays.sort(properties, new Comparator<Property>() { @Override public int compare(Property o1, Property o2) { if (o1 == null && o2 == null) { return 0; } else if (o1 == null) { return 1; } else if (o2 == null) { return -1; } return o1.getName().compareTo(o2.getName()); } }); Property searchProperty = new Property(); searchProperty.setName(name); int index = Arrays.binarySearch(properties, searchProperty, new Comparator<Property>() { @Override public int compare(Property o1, Property o2) { if (o1 == null && o2 == null) { return 0; } else if (o1 == null) { return 1; } else if (o2 == null) { return -1; } return o1.getName().compareTo(o2.getName()); } }); if (index >= 0) { return properties[index]; } return null; }
From source file:org.apache.cocoon.util.IOUtils.java
/** * checks if the input string is a valid java keyword. * @return boolean true/false//from w w w. j a v a2 s. c o m */ private static boolean isJavaKeyword(String keyword) { return (Arrays.binarySearch(keywords, keyword, englishCollator) >= 0); }
From source file:org.broadleafcommerce.common.extensibility.context.merge.handlers.NodeReplaceInsert.java
protected boolean replaceNode(Node[] primaryNodes, Node testNode, final String attribute, List<Node> usedNodes) { if (testNode.getAttributes().getNamedItem(attribute) == null) { return false; }//from w ww. j a v a 2 s. c om //filter out primary nodes that don't have the attribute ArrayList<Node> filterList = new ArrayList<Node>(); for (int j = 0; j < primaryNodes.length; j++) { if (primaryNodes[j].getAttributes().getNamedItem(attribute) != null) { filterList.add(primaryNodes[j]); } } Node[] filtered = {}; filtered = filterList.toArray(filtered); Comparator<Node> idCompare = new Comparator<Node>() { public int compare(Node arg0, Node arg1) { Node id1 = arg0.getAttributes().getNamedItem(attribute); Node id2 = arg1.getAttributes().getNamedItem(attribute); String idVal1 = id1.getNodeValue(); String idVal2 = id2.getNodeValue(); return idVal1.compareTo(idVal2); } }; Arrays.sort(filtered, idCompare); int pos = Arrays.binarySearch(filtered, testNode, idCompare); if (pos >= 0) { Node newNode = filtered[pos].getOwnerDocument().importNode(testNode.cloneNode(true), true); filtered[pos].getParentNode().replaceChild(newNode, filtered[pos]); usedNodes.add(testNode); return true; } return false; }
From source file:org.apache.hadoop.hbase.client.TestAsyncNonMetaRegionLocator.java
private ServerName[] getLocations(byte[][] startKeys) { ServerName[] serverNames = new ServerName[startKeys.length]; TEST_UTIL.getHBaseCluster().getRegionServerThreads().stream().map(t -> t.getRegionServer()).forEach(rs -> { rs.getOnlineRegions(TABLE_NAME).forEach(r -> { serverNames[Arrays.binarySearch(startKeys, r.getRegionInfo().getStartKey(), Bytes::compareTo)] = rs .getServerName();/*from w w w . j av a 2s .c om*/ }); }); return serverNames; }