lower-bound.js 646 B

123456789101112131415161718192021
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. // Port of lower_bound from https://en.cppreference.com/w/cpp/algorithm/lower_bound
  4. // Used to compute insertion index to keep queue sorted after insertion
  5. function lowerBound(array, value, comparator) {
  6. let first = 0;
  7. let count = array.length;
  8. while (count > 0) {
  9. const step = (count / 2) | 0;
  10. let it = first + step;
  11. if (comparator(array[it], value) <= 0) {
  12. first = ++it;
  13. count -= step + 1;
  14. }
  15. else {
  16. count = step;
  17. }
  18. }
  19. return first;
  20. }
  21. exports.default = lowerBound;