• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1// Copyright 2015 the V8 project authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5"use strict";
6
7function makeContainerPosVisible(container, pos) {
8  var height = container.offsetHeight;
9  var margin = Math.floor(height / 4);
10  if (pos < container.scrollTop + margin) {
11    pos -= margin;
12    if (pos < 0) pos = 0;
13    container.scrollTop = pos;
14    return;
15  }
16  if (pos > (container.scrollTop + 3 * margin)) {
17    pos = pos - 3 * margin;
18    if (pos < 0) pos = 0;
19    container.scrollTop = pos;
20  }
21}
22
23
24function lowerBound(a, value, compare, lookup) {
25  let first = 0;
26  let count = a.length;
27  while (count > 0) {
28    let step = Math.floor(count / 2);
29    let middle = first + step;
30    let middle_value = (lookup === undefined) ? a[middle] : lookup(a, middle);
31    let result = (compare === undefined) ? (middle_value < value) : compare(middle_value, value);
32    if (result) {
33      first = middle + 1;
34      count -= step + 1;
35    } else {
36      count = step;
37    }
38  }
39  return first;
40}
41
42
43function upperBound(a, value, compare, lookup) {
44  let first = 0;
45  let count = a.length;
46  while (count > 0) {
47    let step = Math.floor(count / 2);
48    let middle = first + step;
49    let middle_value = (lookup === undefined) ? a[middle] : lookup(a, middle);
50    let result = (compare === undefined) ? (value < middle_value) : compare(value, middle_value);
51    if (!result) {
52      first = middle + 1;
53      count -= step + 1;
54    } else {
55      count = step;
56    }
57  }
58  return first;
59}
60
61
62function sortUnique(arr, f) {
63  arr = arr.sort(f);
64  let ret = [arr[0]];
65  for (var i = 1; i < arr.length; i++) {
66    if (arr[i-1] !== arr[i]) {
67      ret.push(arr[i]);
68    }
69  }
70  return ret;
71}
72
73// Partial application without binding the receiver
74function partial(f) {
75  var arguments1 = Array.prototype.slice.call(arguments, 1);
76  return function() {
77    var arguments2 = Array.from(arguments);
78    f.apply(this, arguments1.concat(arguments2));
79  }
80}
81