Program: 87565
Project: 87565
Buggy source:
private void computeNearestNeighbors(){
  final int defaultWidth=mMostCommonKeyWidth;
  final int keyCount=mKeys.length;
  final int gridSize=mGridNeighbors.length;
  final int threshold=(int)(defaultWidth * SEARCH_DISTANCE);
  final int thresholdSquared=threshold * threshold;
  final int fullGridWidth=mGridWidth * mCellWidth;
  final int fullGridHeight=mGridHeight * mCellHeight;
  final Key[] neighborsFlatBuffer=new Key[gridSize * keyCount];
  final int[] neighborCountPerCell=new int[gridSize];
  final int halfCellWidth=mCellWidth / 2;
  final int halfCellHeight=mCellHeight / 2;
  for (  final Key key : mKeys) {
    if (key.isSpacer())     continue;
    final int keyX=key.getX();
    final int keyY=key.getY();
    final int topPixelWithinThreshold=keyY - threshold;
    final int yDeltaToGrid=topPixelWithinThreshold % mCellHeight;
    final int yMiddleOfTopCell=topPixelWithinThreshold - yDeltaToGrid + halfCellHeight;
    final int yStart=Math.max(halfCellHeight,yMiddleOfTopCell + (yDeltaToGrid <= halfCellHeight ? 0 : mCellHeight));
    final int yEnd=Math.min(fullGridHeight,keyY + key.getHeight() + threshold);
    final int leftPixelWithinThreshold=keyX - threshold;
    final int xDeltaToGrid=leftPixelWithinThreshold % mCellWidth;
    final int xMiddleOfLeftCell=leftPixelWithinThreshold - xDeltaToGrid + halfCellWidth;
    final int xStart=Math.max(halfCellWidth,xMiddleOfLeftCell + (xDeltaToGrid <= halfCellWidth ? 0 : mCellWidth));
    final int xEnd=Math.min(fullGridWidth,keyX + key.getWidth() + threshold);
    int baseIndexOfCurrentRow=(yStart / mCellHeight) * mGridWidth + (xStart / mCellWidth);
    for (int centerY=yStart; centerY <= yEnd; centerY+=mCellHeight) {
      int index=baseIndexOfCurrentRow;
      for (int centerX=xStart; centerX <= xEnd; centerX+=mCellWidth) {
        if (index < neighborCountPerCell.length && key.squaredDistanceToEdge(centerX,centerY) < thresholdSquared) {
          neighborsFlatBuffer[index * keyCount + neighborCountPerCell[index]]=key;
          ++neighborCountPerCell[index];
        }
        ++index;
      }
      baseIndexOfCurrentRow+=mGridWidth;
    }
  }
  for (int i=0; i < gridSize; ++i) {
    final int base=i * keyCount;
    mGridNeighbors[i]=Arrays.copyOfRange(neighborsFlatBuffer,base,base + neighborCountPerCell[i]);
  }
}

Fixed version (ground truth):
private void computeNearestNeighbors(){
  final int defaultWidth=mMostCommonKeyWidth;
  final int keyCount=mKeys.length;
  final int gridSize=mGridNeighbors.length;
  final int threshold=(int)(defaultWidth * SEARCH_DISTANCE);
  final int thresholdSquared=threshold * threshold;
  final int lastPixelXCoordinate=mGridWidth * mCellWidth - 1;
  final int lastPixelYCoordinate=mGridHeight * mCellHeight - 1;
  final Key[] neighborsFlatBuffer=new Key[gridSize * keyCount];
  final int[] neighborCountPerCell=new int[gridSize];
  final int halfCellWidth=mCellWidth / 2;
  final int halfCellHeight=mCellHeight / 2;
  for (  final Key key : mKeys) {
    if (key.isSpacer())     continue;
    final int keyX=key.getX();
    final int keyY=key.getY();
    final int topPixelWithinThreshold=keyY - threshold;
    final int yDeltaToGrid=topPixelWithinThreshold % mCellHeight;
    final int yMiddleOfTopCell=topPixelWithinThreshold - yDeltaToGrid + halfCellHeight;
    final int yStart=Math.max(halfCellHeight,yMiddleOfTopCell + (yDeltaToGrid <= halfCellHeight ? 0 : mCellHeight));
    final int yEnd=Math.min(lastPixelYCoordinate,keyY + key.getHeight() + threshold);
    final int leftPixelWithinThreshold=keyX - threshold;
    final int xDeltaToGrid=leftPixelWithinThreshold % mCellWidth;
    final int xMiddleOfLeftCell=leftPixelWithinThreshold - xDeltaToGrid + halfCellWidth;
    final int xStart=Math.max(halfCellWidth,xMiddleOfLeftCell + (xDeltaToGrid <= halfCellWidth ? 0 : mCellWidth));
    final int xEnd=Math.min(lastPixelXCoordinate,keyX + key.getWidth() + threshold);
    int baseIndexOfCurrentRow=(yStart / mCellHeight) * mGridWidth + (xStart / mCellWidth);
    for (int centerY=yStart; centerY <= yEnd; centerY+=mCellHeight) {
      int index=baseIndexOfCurrentRow;
      for (int centerX=xStart; centerX <= xEnd; centerX+=mCellWidth) {
        if (key.squaredDistanceToEdge(centerX,centerY) < thresholdSquared) {
          neighborsFlatBuffer[index * keyCount + neighborCountPerCell[index]]=key;
          ++neighborCountPerCell[index];
        }
        ++index;
      }
      baseIndexOfCurrentRow+=mGridWidth;
    }
  }
  for (int i=0; i < gridSize; ++i) {
    final int base=i * keyCount;
    mGridNeighbors[i]=Arrays.copyOfRange(neighborsFlatBuffer,base,base + neighborCountPerCell[i]);
  }
}