android/guava/src/com/google/common/collect/TopKSelector.java
public void offer(@NullableDecl T elem) {
    if (k == 0) {
      return;
    } else if (bufferSize == 0) {
      buffer[0] = elem;
      threshold = elem;
      bufferSize = 1;
    } else if (bufferSize < k) {
      buffer[bufferSize++] = elem;
      if (comparator.compare(elem, threshold) > 0) {
        threshold = elem;
      }
    } else if (comparator.compare(elem, threshold) < 0) {
      // Otherwise, we can ignore elem; we've seen k better elements.
      buffer[bufferSize++] = elem;
      if (bufferSize == 2 * k) {
        trim();
      }
    }
  }
public void offer(@NullableDecl T elem) {
    if (k == 0) {
      return;
    } else if (bufferSize == 0) {
      buffer[0] = elem;
      threshold = elem;
      bufferSize = 1;
    } else if (bufferSize < k) {
      buffer[bufferSize++] = elem;
      if (comparator.compare(elem, threshold) > 0) {
        threshold = elem;
      }
    } else if (comparator.compare(elem, threshold) < 0) {
      // Otherwise, we can ignore elem; we've seen k better elements.
      buffer[bufferSize++] = elem;
      if (bufferSize == 2 * k) {
        trim();
      }
    }
  }
public void offer ( @ NullableDecl T elem ) { if ( k == 0 ) { return ; } else if ( bufferSize == 0 ) { buffer [ 0 ] = elem ; threshold = elem ; bufferSize = 1 ; } else if ( bufferSize < k ) { buffer [ bufferSize ++ ] = elem ; if ( comparator . compare ( elem , threshold ) > 0 ) { threshold = elem ; } } else if ( comparator . compare ( elem , threshold ) < 0 ) { // Otherwise, we can ignore elem; we've seen k better elements. buffer [ bufferSize ++ ] = elem ; if ( bufferSize == 2 * k ) { trim ( ) ; } } }
Adds {@code elem} as a candidate for the top {@code k} elements. This operation takes amortized
O(1) time.
Adds {