Seek an iterator to the desired position, using binary search.

@param index Index to search
@param iter Iterator
@param val Distance to search to
6872
protected static void binarySearch(ModifiableDoubleDBIDList index, DoubleDBIDListIter iter, double val) {
    // Binary search. TODO: move this into the DoubleDBIDList class.
    int left = 0, right = index.size();
    while(left < right) {
      final int mid = (left + right) >>> 1;
      final double curd = iter.seek(mid).doubleValue();
      if(val < curd) {
        right = mid;
      }
      else if(val > curd) {
        left = mid + 1;
      }
      else {
        left = mid;
        break;
      }
    }
    if(left >= index.size()) {
      --left;
    }
    iter.seek(left);
  }
759459
@Override
    public void seek(Range range, Collection<ByteSequence> columnFamilies, boolean inclusive) throws IOException {
        topKey = null;
        topValue = null;

        Key sk = range.getStartKey();

        if (sk != null
            && sk.getColumnFamilyData().length() == 0
            && sk.getColumnQualifierData().length() == 0
            && sk.getColumnVisibilityData().length() == 0
            && sk.getTimestamp() == Long.MAX_VALUE
            && !range.isStartKeyInclusive()) {
            // assuming that we are seeking using a key previously returned by this iterator therefore go the next row
            Key followingRowKey = sk.followingKey(PartialKey.ROW);
            if (range.getEndKey() != null && followingRowKey.compareTo(range.getEndKey()) > 0) {
                return;
            }

            range = new Range(sk.followingKey(PartialKey.ROW), true, range.getEndKey(), range.isEndKeyInclusive());
        }

        sourceIterator.seek(range, columnFamilies, inclusive);
        loadNext();
    }
546240
public static <E> E at(long index, Iterable<E> iterable) {
        dbc.precondition(iterable != null, "cannot call at with a null iterable");
        final Iterator<E> filtered = new FilteringIterator<E>(iterable.iterator(), new AtIndex<E>(index));
        return new FirstElement<E>().apply(filtered);
    }
1284662
public static <E> Optional<E> maybeAt(long index, Iterable<E> iterable) {
        dbc.precondition(iterable != null, "cannot call maybeAt with a null iterable");
        final Iterator<E> filtered = new FilteringIterator<E>(iterable.iterator(), new AtIndex<E>(index));
        return new MaybeFirstElement<E>().apply(filtered);
    }
472097
public static TypeAnnotationPosition
        methodThrows(final List<TypePathEntry> location,
                     final int type_index) {
        return methodThrows(location, null, type_index, -1);
    }
1315661
public void forEachIndexed(int from, int step,
                               @NotNull IndexedConsumer<? super T> action) {
        int index = from;
        while (iterator.hasNext()) {
            action.accept(index, iterator.next());
            index += step;
        }
    }
410329
private KNNHeap linearScan(Relation<? extends O> relation, DBIDIter iter, final O obj, KNNHeap heap) {
    final SquaredEuclideanDistanceFunction squared = SquaredEuclideanDistanceFunction.STATIC;
    double max = Double.POSITIVE_INFINITY;
    while(iter.valid()) {
      final double dist = squared.distance(obj, relation.get(iter));
      if(dist <= max) {
        max = heap.insert(dist, iter);
      }
      iter.advance();
    }
    return heap;
  }
737890
private void buildMatch(Object val)
    {
        if (val instanceof String[]) {
            _match = (String[]) val;
        }
        else {
            Iterator matchIterator = null;
            // this should return null, but we should handle it it does
            matchIterator = IteratorFactory.createIterator(val);
            if (matchIterator == null)
                matchIterator = IteratorFactory.EMPTY_ITERATOR;

            List matchList = new ArrayList();
            while (matchIterator.hasNext()) {
                Object o = matchIterator.next();
                if (o != null)
                    matchList.add(o.toString());
            }
            int size = matchList.size();

            _match = new String[size];
            for (int i = 0; i < size; i++) {
                _match[i] = matchList.get(i).toString();
            }
        }

        if (logger.isDebugEnabled()) {
            logger.debug("****** CheckboxGroup Matches ******");
            if (_match != null) {
                for (int i = 0; i < _match.length; i++) {
                    logger.debug(i + ": " + _match[i]);
                }
            }
        }
    }
344906
public synchronized void seek(long position) throws IOException {
    if (position < 0 || position >= count) {
      throw new IOException("cannot seek position " + position + " as it is out of bounds");
    }
    pos = (int) position;
  }
669388
@SuppressWarnings("unchecked")
    public static void setIndex(Object array, int index, Object val)
            throws ReflectError, UtilTargetError {
        try {
            val = Primitive.unwrap(val);
            if ( array instanceof List )
                ((List<Object>) array).set(index, val);
            else
                Array.set(array, index, val);
        }
        catch( IllegalArgumentException e1 ) {
            // fabricated array store exception
            throw new UtilTargetError(
                new ArrayStoreException( e1.getMessage() ) );
        } catch( IndexOutOfBoundsException e1 ) {
            int len = array instanceof List
                    ? ((List<?>) array).size()
                    : Array.getLength(array);
            throw new UtilTargetError("Index " + index
                    + " out-of-bounds for length " + len, e1);
        }
    }
636597
public void forEachIndexed(int from, int step,
                               @NotNull IndexedIntConsumer action) {
        int index = from;
        while (iterator.hasNext()) {
            action.accept(index, iterator.nextInt());
            index += step;
        }
    }
1093022
public static <E> Optional<E> maybeAt(long index, Iterator<E> iterator) {
        final Iterator<E> filtered = new FilteringIterator<E>(iterator, new AtIndex<E>(index));
        return new MaybeFirstElement<E>().apply(filtered);
    }
597657
@Override
    public double put(int idx, double val) {
	    double old = 0;
		int i = Arrays.binarySearch(indices, 0, used, idx);
		if (i >= 0) {
			// Just update the value.
		    old = values[i];
			values[i] = val;
			return old;
		} 
		int insertionPoint = -(i + 1);
		indices = insert(indices, insertionPoint, idx);
		values = insert(values, insertionPoint, val);
		used++;
		return old;
	}
1348337
@SuppressWarnings("unchecked")
  public <T extends Entry> T get(long index) {
    Entry entry = buffer[offset(index)];
    return entry != null && entry.getIndex() == index ? (T) entry.acquire() : null;
  }
931439
public static <T> T search(Iterable<T> iter, SearchFunction<T> fx) {
        for (final T t : iter) {
            if (fx.match(t)) return t;
        }
        return null;
    }
976088
public void putInt(int index, int v)
    {
        // Reversing the endian
        v = Integer.reverseBytes(v);
        unsafe.putInt(base, address + index, v);
    }
1336031
private void recordIndex(int index) {
        stateLock.writeLock().lock();
        try {
            int insertionPoint = -Arrays.binarySearch(effectiveIndex, index) - 1;
            System.arraycopy(effectiveIndex, insertionPoint, effectiveIndex, insertionPoint + 1, effectiveIndex.length - insertionPoint - 1);
            effectiveIndex[insertionPoint] = index;
        } finally {
            stateLock.writeLock().unlock();
        }
    }