///////////////////////////////////////////////////// this code borrowed from stackoverflow: https://stackoverflow.com/questions/22697936/binary-search-in-javascript
185240
function binarySearch(arr, pred) {
    let begin   = -1;
    let end     = arr.length;

    while (1 + begin < end) {
        const mi = begin + ((end - begin) >> 1);

        if (pred(arr[mi]))
            end = mi;
        else
            begin = mi;
    }

    return end;
}
1245913
protected int binarySearch (int key)
    {
        int low = 0;
        int high = _size-1;

        while (low <= high) {
            int mid = (low + high) >>> 1;
            int midVal = _values[mid];

            if (midVal < key) {
                low = mid + 1;
            } else if (midVal > key) {
                high = mid - 1;
            } else {
                return mid; // key found
            }
        }

        return -(low + 1);  // key not found.
    }
970510
private static final int binarySearchForRootPrimaryNode(
            int[] rootPrimaryIndexes, int length, long[] nodes, long p) {
        if(length == 0) { return ~0; }
        int start = 0;
        int limit = length;
        for (;;) {
            int i = (int)(((long)start + (long)limit) / 2);
            long node = nodes[rootPrimaryIndexes[i]];
            long nodePrimary = node >>> 32;  // weight32FromNode(node)
            if (p == nodePrimary) {
                return i;
            } else if (p < nodePrimary) {
                if (i == start) {
                    return ~start;  // insert s before i
                }
                limit = i;
            } else {
                if (i == start) {
                    return ~(start + 1);  // insert s after i
                }
                start = i;
            }
        }
    }
994559
function sortedIndex(element) {
    /* jshint validthis:true */
    var index = binarySearch(toArray(this), element, this.comparator);

    if (index < 0) {
        // binarySearch decreases the negative index by one because otherwise the index 0 would stand for two results
        // @see https://github.com/darkskyapp/binary-search/issues/1
        return Math.abs(index) - 1;
    } else {
        return index;
    }
}
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
1139058
function binarySearch(arr, item, comparator) {
  var low = 0;
  var high = arr.length;
  var mid;
  while (low < high) {
    mid = (low + high) >>> 1;
    if (comparator(arr[mid], item) < 0) {
      low = mid + 1;
    } else {
      high = mid;
    }
  }
  return low;
}
500123
function isBinary(buffer) {
  var chunk = buffer.toString('utf8', 0, Math.min(buffer.length, 24));
  for (var i in chunk) {
    var charCode = chunk.charCodeAt(i);
    if (charCode == 65533 || charCode <= 8) {
      return true;
    }
  }
  return false;
}
1017556
function binToHex(binArray){
        var hexString = "0123456789abcdef",
            str = "",
            i;

        for(i = 0; i < binArray.length * 4; i++){
            str += hexString.charAt((binArray[i >> 2] >> ((3 - i % 4) * 8 + 4)) & 0xF) +
                    hexString.charAt((binArray[i >> 2] >> ((3 - i % 4) * 8  )) & 0xF);
        }

        return str;
    }
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);
  }
1120172
function() {
    const buf8 = pslBuffer8;
    const buf32 = pslBuffer32;
    const iCharData = buf32[CHARDATA_PTR_SLOT];

    let iNode = pslBuffer32[RULES_PTR_SLOT];
    let cursorPos = -1;
    let iLabel = LABEL_INDICES_SLOT;

    // Label-lookup loop
    for (;;) {
        // Extract label indices
        const labelBeg = buf8[iLabel+1];
        const labelLen = buf8[iLabel+0] - labelBeg;
        // Match-lookup loop: binary search
        let r = buf32[iNode+0] >>> 16;
        if ( r === 0 ) { break; }
        const iCandidates = buf32[iNode+2];
        let l = 0;
        let iFound = 0;
        while ( l < r ) {
            const iCandidate = l + r >>> 1;
            const iCandidateNode = iCandidates + iCandidate + (iCandidate << 1);
            const candidateLen = buf32[iCandidateNode+0] & 0x000000FF;
            let d = labelLen - candidateLen;
            if ( d === 0 ) {
                const iCandidateChar = candidateLen <= 4
                    ? iCandidateNode + 1 << 2
                    : iCharData + buf32[iCandidateNode+1];
                for ( let i = 0; i < labelLen; i++ ) {
                    d = buf8[labelBeg+i] - buf8[iCandidateChar+i];
                    if ( d !== 0 ) { break; }
                }
            }
            if ( d < 0 ) {
                r = iCandidate;
            } else if ( d > 0 ) {
                l = iCandidate + 1;
            } else /* if ( d === 0 ) */ {
                iFound = iCandidateNode;
                break;
            }
        }
        // 2. If no rules match, the prevailing rule is "*".
        if ( iFound === 0 ) {
            if ( buf8[iCandidates + 1 << 2] !== 0x2A /* '*' */ ) { break; }
            iFound = iCandidates;
        }
        iNode = iFound;
        // 5. If the prevailing rule is a exception rule, modify it by
        //    removing the leftmost label.
        if ( (buf32[iNode+0] & 0x00000200) !== 0 ) {
            if ( iLabel > LABEL_INDICES_SLOT ) {
                return iLabel - 2;
            }
            break;
        }
        if ( (buf32[iNode+0] & 0x00000100) !== 0 ) {
            cursorPos = iLabel;
        }
        if ( labelBeg === 0 ) { break; }
        iLabel += 2;
    }

    return cursorPos;
}
732511
public static int getBiFrequency(String a, String b)
    {
        int idA = CoreDictionary.trie.exactMatchSearch(a);
        if (idA == -1)
        {
            return 0;
        }
        int idB = CoreDictionary.trie.exactMatchSearch(b);
        if (idB == -1)
        {
            return 0;
        }
        int index = binarySearch(pair, start[idA], start[idA + 1] - start[idA], idB);
        if (index < 0) return 0;
        index <<= 1;
        return pair[index + 1];
    }
386602
function searchAround (
    getValue, getConditionValue, value, conditionValue, i, arr
  ) {
    var start = i;
    var elm = arr[i];

    while (elm && (
      getValue(elm) !== value || getConditionValue(elm) !== conditionValue)
    ) {
      elm = arr[--i];
    }

    if (!elm || getValue(elm) !== value) {
      i = Math.min(arr.length, start + 1);
      elm = arr[i];
    }

    while (elm && (
      getValue(elm) !== value || getConditionValue(elm) !== conditionValue)
    ) {
      elm = arr[++i];
    }

    if (!elm || getValue(elm) !== value) {
      return -1;
    }

    return i;
  }
804000
function binarySearch(array, func) {
    // min is inclusive, max exclusive.
    let min = 0,
        max = array.length;

    while (min < max) {
        const mid = (min + max) >> 1;
        const res = func(array[mid]);
        if (res > 0) {
            // the element at 'mid' is too big; set it as the new max.
            max = mid;
        } else {
            // the element at 'mid' is too small. 'min' is inclusive, so +1.
            min = mid + 1;
        }
    }
    // presumably, min==max now.
    return min;
}
165342
function() {
        gobbleSpaces();
        var to_check = expr.substr(index, max_binop_len),
          tc_len = to_check.length;
        while (tc_len > 0) {
          if (binary_ops.hasOwnProperty(to_check)) {
            index += tc_len;
            return to_check;
          }
          to_check = to_check.substr(0, --tc_len);
        }
        return false;
      }
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;
    }
31726
static int binarySearch(final short[] a, final int fromIndex, final int toIndex, final short key) {
        if (N.isNullOrEmpty(a)) {
            return N.INDEX_NOT_FOUND;
        }

        return Arrays.binarySearch(a, fromIndex, toIndex, key);
    }
1223060
def binary_search(time, from = 0, to = nil, &block)
      to = private_rules.length - 1 if to.nil?

      return from if from == to

      mid = (from + to).div(2)

      unless yield(time, private_rules[mid])
        return binary_search(time, mid + 1, to, &block)
      end

      return mid if mid.zero?

      return mid unless yield(time, private_rules[mid - 1])

      binary_search(time, from, mid - 1, &block)
    end