Binary search to approach the boundary.
970744
def binary_search_batch(original_image, perturbed_images, decision_function,
                        shape, constraint, theta):
  

  # Compute distance between each of perturbed image and original image.
  dists_post_update = np.array([
      compute_distance(
          original_image,
          perturbed_image,
          constraint
      )
      for perturbed_image in perturbed_images])

  # Choose upper thresholds in binary searchs based on constraint.
  if constraint == 'linf':
    highs = dists_post_update
    # Stopping criteria.
    thresholds = np.minimum(dists_post_update * theta, theta)
  else:
    highs = np.ones(len(perturbed_images))
    thresholds = theta

  lows = np.zeros(len(perturbed_images))

  while np.max((highs - lows) / thresholds) > 1:
    # projection to mids.
    mids = (highs + lows) / 2.0
    mid_images = project(original_image, perturbed_images,
                         mids, shape, constraint)

    # Update highs and lows based on model decisions.
    decisions = decision_function(mid_images)
    lows = np.where(decisions == 0, mids, lows)
    highs = np.where(decisions == 1, mids, highs)

  out_images = project(original_image, perturbed_images,
                       highs, shape, constraint)

  # Compute distance of the output image to select the best choice.
  # (only used when stepsize_search is grid_search.)
  dists = np.array([
      compute_distance(
          original_image,
          out_image,
          constraint
      )
      for out_image in out_images])
  idx = np.argmin(dists)

  dist = dists_post_update[idx]
  out_image = out_images[idx]
  return out_image, dist
235942
function(elementA, elementB) {

        var isSuccessor = false;
        this.search(elementA, function(element) {
            if (element === elementB && element !== elementA) {
                isSuccessor = true;
                return false;
            }
        }, { outbound: true });
        return isSuccessor;
    }
73272
def bound_bboxes(bboxes):
    
    group_x0 = min(map(lambda l: l[x0], bboxes))
    group_y0 = min(map(lambda l: l[y0], bboxes))
    group_x1 = max(map(lambda l: l[x1], bboxes))
    group_y1 = max(map(lambda l: l[y1], bboxes))
    return (group_x0, group_y0, group_x1, group_y1)
356248
private int binaryFirstSearch() {

        int low     = 0;
        int high    = count;
        int mid     = 0;
        int compare = 0;
        int found   = count;

        while (low < high) {
            mid     = (low + high) / 2;
            compare = compare(mid);

            if (compare < 0) {
                high = mid;
            } else if (compare > 0) {
                low = mid + 1;
            } else {
                high  = mid;
                found = mid;
            }
        }

        return found == count ? -1
                              : found;
    }
1336490
def binary_search(pVector, pElement)
      pVector.bsearch { |obj| pElement.name <=> obj.name }
    end
440714
function () {
			var k, ent,
			hash = {
				max: { x: -Infinity, y: -Infinity },
				min: { x: Infinity, y: Infinity }
			},
			coords = {
				max: { x: -Infinity, y: -Infinity },
				min: { x: Infinity, y: Infinity }
			};

      //Using broad phase hash to speed up the computation of boundaries.
			for (var h in this.map) {
				if (!this.map[h].length) continue;

        //broad phase coordinate
				var i= h>>16,
					j=(h<<16)>>16;
				if (j<0) { i = i^-1 }
				if (i >= hash.max.x) {
					hash.max.x = i;
					for (k in this.map[h]) {
						ent = this.map[h][k];
						//make sure that this is a Crafty entity
						if (typeof ent == 'object' && 'requires' in ent) {
							coords.max.x = Math.max(coords.max.x, ent.x + ent.w);
						}
					}
				}
				if (i <= hash.min.x) {
					hash.min.x = i;
					for (k in this.map[h]) {
						ent = this.map[h][k];
						if (typeof ent == 'object' && 'requires' in ent) {
							coords.min.x = Math.min(coords.min.x, ent.x);
						}
					}
				}
				if (j >= hash.max.y) {
					hash.max.y = j;
					for (k in this.map[h]) {
						ent = this.map[h][k];
						if (typeof ent == 'object' && 'requires' in ent) {
							coords.max.y = Math.max(coords.max.y, ent.y + ent.h);
						}
					}
				}
				if (j <= hash.min.y) {
					hash.min.y = j;
					for (k in this.map[h]) {
						ent = this.map[h][k];
						if (typeof ent == 'object' && 'requires' in ent) {
							coords.min.y = Math.min(coords.min.y, ent.y);
						}
					}
				}
			}

			return coords;
		}
1315190
def binary_search(data, target, lo=0, hi=None):
    
    hi = hi if hi is not None else len(data)
    mid = (lo + hi) // 2
    if hi < 2 or hi > len(data) or target > data[-1] or target < data[0]:
        return -1
    if data[mid] > target:
        return binary_search(data, target, lo=lo, hi=mid)
    elif data[mid] < target:
        return binary_search(data, target, lo=(mid + 1), hi=hi)
    elif data[mid] == target:
        return mid
672535
@Private static int binarySearch(IntList ascendingInts, int value) {
    for (int begin = 0, to = ascendingInts.size();;) {
      if (begin == to) return begin;
      int i = (begin + to) / 2;
      int x = ascendingInts.get(i);
      if (x == value) return i;
      else if (x > value) to = i;
      else begin = i + 1;
    }
  }
168576
function binarySearch (comparator, array, key) {
  var low = 0, high = array.length - 1, partition, compare;
  while (low <= high) {
    partition = Math.floor(low + (high - low) / 2);
    compare = comparator(key, array[partition]);
    if (compare < 0) high = partition - 1;
    else if (compare > 0) low = partition + 1;
    else return partition;
  }
  return low;
}
960375
protected int optimize( List<Point2D_I32> contour , int c0,int c1,int c2 ) {

		double bestDistance = computeCost(contour,c0,c1,c2,0);
		int bestIndex = 0;
		for( int i = -searchRadius; i <= searchRadius; i++ ) {
			if( i == 0 ) {
				// if it found a better point in the first half stop the search since that's probably the correct
				// direction.  Could be improved by remember past search direction
				if( bestIndex != 0 )
					break;
			} else {
				double found = computeCost(contour, c0, c1, c2, i);
				if (found < bestDistance) {
					bestDistance = found;
					bestIndex = i;
				}
			}
		}
		return CircularIndex.addOffset(c1, bestIndex, contour.size());
	}
1164072
private void findFullGridIntBounds() {
		int[] iIntBounds = getIntBounds(ibounds);

		bounds = new int[6];
		if (jbounds==null) {
			bounds = iIntBounds;
		} else {
			int[] jIntBounds = getIntBounds(jbounds);
			bounds[0] = Math.min(iIntBounds[0],jIntBounds[0]);
			bounds[1] = Math.min(iIntBounds[1],jIntBounds[1]);
			bounds[2] = Math.min(iIntBounds[2],jIntBounds[2]);
			bounds[3] = Math.max(iIntBounds[3],jIntBounds[3]);
			bounds[4] = Math.max(iIntBounds[4],jIntBounds[4]);
			bounds[5] = Math.max(iIntBounds[5],jIntBounds[5]);
		}
	}
196560
function binarySearchIndices( value ) {

			function binarySearch( start, end ) {

				// return closest larger index
				// if exact number is not found

				if ( end < start )
					return start;

				var mid = start + Math.floor( ( end - start ) / 2 );

				if ( cumulativeAreas[ mid ] > value ) {

					return binarySearch( start, mid - 1 );

				} else if ( cumulativeAreas[ mid ] < value ) {

					return binarySearch( mid + 1, end );

				} else {

					return mid;

				}

			}

			var result = binarySearch( 0, cumulativeAreas.length - 1 )
			return result;

		}
1326902
def _binary_search_intervals(cls, intervals, index):
        
        start = 0
        end = len(intervals) - 1
        while start <= end:
            middle_index = start + ((end - start) // 2)
            middle = intervals[middle_index]
            if (middle[0] <= index) and (index < middle[1]):
                return middle
            elif middle[0] > index:
                end = middle_index - 1
            else:
                start = middle_index + 1
        return None
795996
function ( geometry, n ) {

		var face, i,
			faces = geometry.faces,
			vertices = geometry.vertices,
			il = faces.length,
			totalArea = 0,
			cumulativeAreas = [],
			vA, vB, vC, vD;

		// precompute face areas

		for ( i = 0; i < il; i ++ ) {

			face = faces[ i ];

			if ( face instanceof THREE.Face3 ) {

				vA = vertices[ face.a ];
				vB = vertices[ face.b ];
				vC = vertices[ face.c ];

				face._area = THREE.GeometryUtils.triangleArea( vA, vB, vC );

			} else if ( face instanceof THREE.Face4 ) {

				vA = vertices[ face.a ];
				vB = vertices[ face.b ];
				vC = vertices[ face.c ];
				vD = vertices[ face.d ];

				face._area1 = THREE.GeometryUtils.triangleArea( vA, vB, vD );
				face._area2 = THREE.GeometryUtils.triangleArea( vB, vC, vD );

				face._area = face._area1 + face._area2;

			}

			totalArea += face._area;

			cumulativeAreas[ i ] = totalArea;

		}

		// binary search cumulative areas array

		function binarySearchIndices( value ) {

			function binarySearch( start, end ) {

				// return closest larger index
				// if exact number is not found

				if ( end < start )
					return start;

				var mid = start + Math.floor( ( end - start ) / 2 );

				if ( cumulativeAreas[ mid ] > value ) {

					return binarySearch( start, mid - 1 );

				} else if ( cumulativeAreas[ mid ] < value ) {

					return binarySearch( mid + 1, end );

				} else {

					return mid;

				}

			}

			var result = binarySearch( 0, cumulativeAreas.length - 1 )
			return result;

		}

		// pick random face weighted by face area

		var r, index,
			result = [];

		var stats = {};

		for ( i = 0; i < n; i ++ ) {

			r = THREE.GeometryUtils.random() * totalArea;

			index = binarySearchIndices( r );

			result[ i ] = THREE.GeometryUtils.randomPointInFace( faces[ index ], geometry, true );

			if ( ! stats[ index ] ) {

				stats[ index ] = 1;

			} else {

				stats[ index ] += 1;

			}

		}

		return result;

	}
1233551
def golden_search(f, a, b, xatol=1e-6, ftol=1e-8, expand_bounds=False):
    

    ratio = 2 / (1 + math.sqrt(5))

    if not expand_bounds:
        x0 = a
        x3 = b
    else:
        x0 = (ratio * a - (1 - ratio) * b) / (2*ratio - 1)
        x3 = (ratio * b - (1 - ratio) * a) / (2*ratio - 1)

    x1 = ratio * x0 + (1 - ratio) * x3
    x2 = (1 - ratio) * x0 + ratio * x3

    f1 = f(x1)
    f2 = f(x2)

    f0 = max(abs(f1), abs(f2))

    while True:
        if abs(x0 - x3) < xatol or abs(f1 - f2) < ftol*f0:
            break

        if f2 < f1:
            x0 = x1
            x1 = x2
            x2 = ratio * x1 + (1 - ratio) * x3
            f1 = f2
            f2 = f(x2)
        else:
            x3 = x2
            x2 = x1
            x1 = ratio * x2 + (1 - ratio) * x0
            f2 = f1
            f1 = f(x1)

    if f2 < f1:
        return x2
    else:
        return x1
943098
private boolean searchAll(Node<K, V> x, Interval1D<K> interval, List<Node<K, V>> toAppend) {
    boolean found1 = false;
    boolean found2 = false;
    boolean found3 = false;
    if (x == null) {
      return false;
    }
    if (interval.intersects(x.interval)) {
      toAppend.add(x);
      found1 = true;
    }

    if (x.left != null && x.left.max.compareTo(interval.lo) >= 0) {
      found2 = searchAll(x.left, interval, toAppend);
    }
    if (found2 || x.left == null || x.left.max.compareTo(interval.lo) < 0) {
      found3 = searchAll(x.right, interval, toAppend);
    }
    return found1 || found2 || found3;
  }
1371022
int binarySearch(int fromIndex, int toIndex, long key, long value) {
		if (ind.isUnique()) {
			return binarySearchUnique(fromIndex, toIndex, key);
		}
		return binarySearchNonUnique(fromIndex, toIndex, key, value);
	}