aboutsummaryrefslogtreecommitdiff
path: root/ui/src/base/binary_search.ts
blob: 72a16ce0b356166292134e7eed3bebd8e9f8533b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
// Copyright (C) 2018 The Android Open Source Project
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//      http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.


type Numbers = Float64Array|Uint32Array|number[];
type Range = [number, number];

function searchImpl(
    haystack: Numbers, needle: number, i: number, j: number): number {
  if (i === j) return -1;
  if (i + 1 === j) {
    return (needle >= haystack[i]) ? i : -1;
  }

  const mid = Math.floor((j - i) / 2) + i;
  const midValue = haystack[mid];
  if (needle < midValue) {
    return searchImpl(haystack, needle, i, mid);
  } else {
    return searchImpl(haystack, needle, mid, j);
  }
}

function searchRangeImpl(
    haystack: Numbers, needle: number, i: number, j: number): Range {
  if (i === j) return [i, j];
  if (i + 1 === j) {
    if (haystack[i] <= needle) {
      return [i, j];
    } else {
      return [i, i];
    }
  }

  const mid = Math.floor((j - i) / 2) + i;
  const midValue = haystack[mid];

  if (needle < midValue) {
    return searchRangeImpl(haystack, needle, i, mid);
  } else if (needle > midValue) {
    return searchRangeImpl(haystack, needle, mid, j);
  } else {
    while (haystack[i] !== needle) i++;
    while (haystack[j - 1] !== needle) j--;
    return [i, j];
  }
}

export function search(haystack: Numbers, needle: number): number {
  return searchImpl(haystack, needle, 0, haystack.length);
}

/**
 * Given a sorted array of numbers (|haystack|) and a |needle| return the
 * half open range [i, j) of indexes where |haystack| is equal to needle.
 */
export function searchEq(
    haystack: Numbers, needle: number, optRange?: Range): Range {
  const range = searchRange(haystack, needle, optRange);
  const [i, j] = range;
  if (haystack[i] === needle) return range;
  return [j, j];
}

/**
 * Given a sorted array of numbers (|haystack|) and a |needle| return the
 * smallest half open range [i, j) of indexes which contains |needle|.
 */
export function searchRange(
    haystack: Numbers, needle: number, optRange?: Range): Range {
  const [left, right] = optRange ? optRange : [0, haystack.length];
  return searchRangeImpl(haystack, needle, left, right);
}

/**
 * Given a sorted array of numbers (|haystack|) and a |needle| return a
 * pair of indexes [i, j] such that:
 * If there is at least one element in |haystack| smaller than |needle|
 * i is the index of the largest such number otherwise -1;
 * If there is at least one element in |haystack| larger than |needle|
 * j is the index of the smallest such element otherwise -1.
 *
 * So we try to get the indexes of the two data points around needle
 * or -1 if there is no such datapoint.
 */
export function searchSegment(haystack: Numbers, needle: number): Range {
  if (!haystack.length) return [-1, -1];

  const left = search(haystack, needle);
  if (left === -1) {
    return [left, 0];
  } else if (left + 1 === haystack.length) {
    return [left, -1];
  } else {
    return [left, left + 1];
  }
}