blob: 211a28b5ac87426b6eafcf7957b79c791b94ef56 [file] [log] [blame]
// 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 Numeric = number | bigint;
type Range = [number, number];
function searchImpl<T extends Numeric>(
haystack: ArrayLike<T>,
needle: T,
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<T extends Numeric>(
haystack: ArrayLike<T>,
needle: T,
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];
}
}
// Given a sorted array of numeric values |haystack| and a |needle|, return the
// index of the last element of |haystack| which is less than or equal to
// |needle|, or -1 if all elements of |haystack| are greater than |needle|.
export function search<T extends Numeric>(
haystack: ArrayLike<T>,
needle: T,
): number {
return searchImpl(haystack, needle, 0, haystack.length);
}
// Given a sorted array of numeric values (|haystack|) return the half open
// range [i, j) of indices where |haystack| is equal to needle.
export function searchEq<T extends Numeric>(
haystack: ArrayLike<T>,
needle: T,
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 numeric values (|haystack|) and a |needle| return the
// smallest half open range [i, j) of indexes which contains |needle|.
export function searchRange<T extends Numeric>(
haystack: ArrayLike<T>,
needle: T,
optRange?: Range,
): Range {
const [left, right] = optRange ? optRange : [0, haystack.length];
return searchRangeImpl(haystack, needle, left, right);
}
// Given a sorted array of numeric values (|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<T extends Numeric>(
haystack: ArrayLike<T>,
needle: T,
): 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];
}
}