| 1 |
/*--------------------------------------------------------------------------------------------- |
| 2 |
* Copyright (c) Microsoft Corporation. All rights reserved. |
| 3 |
* Licensed under the MIT License. See License.txt in the project root for license information. |
| 4 |
*--------------------------------------------------------------------------------------------*/ |
| 5 |
|
| 6 |
import { Comparator } from './arrays'; |
| 7 |
|
| 8 |
export function findLast<T>(array: readonly T[], predicate: (item: T) => boolean): T | undefined { |
| 9 |
const idx = findLastIdx(array, predicate); |
| 10 |
if (idx === -1) { |
| 11 |
return undefined; |
| 12 |
} |
| 13 |
return array[idx]; |
| 14 |
} |
| 15 |
|
| 16 |
export function findLastIdx<T>(array: readonly T[], predicate: (item: T) => boolean, fromIndex = array.length - 1): number { |
| 17 |
for (let i = fromIndex; i >= 0; i--) { |
| 18 |
const element = array[i]; |
| 19 |
|
| 20 |
if (predicate(element)) { |
| 21 |
return i; |
| 22 |
} |
| 23 |
} |
| 24 |
|
| 25 |
return -1; |
| 26 |
} |
| 27 |
|
| 28 |
/** |
| 29 |
* Finds the last item where predicate is true using binary search. |
| 30 |
* `predicate` must be monotonous, i.e. `arr.map(predicate)` must be like `[true, ..., true, false, ..., false]`! |
| 31 |
* |
| 32 |
* @returns `undefined` if no item matches, otherwise the last item that matches the predicate. |
| 33 |
*/ |
| 34 |
export function findLastMonotonous<T>(array: readonly T[], predicate: (item: T) => boolean): T | undefined { |
| 35 |
const idx = findLastIdxMonotonous(array, predicate); |
| 36 |
return idx === -1 ? undefined : array[idx]; |
| 37 |
} |
| 38 |
|
| 39 |
/** |
| 40 |
* Finds the last item where predicate is true using binary search. |
| 41 |
* `predicate` must be monotonous, i.e. `arr.map(predicate)` must be like `[true, ..., true, false, ..., false]`! |
| 42 |
* |
| 43 |
* @returns `startIdx - 1` if predicate is false for all items, otherwise the index of the last item that matches the predicate. |
| 44 |
*/ |
| 45 |
export function findLastIdxMonotonous<T>(array: readonly T[], predicate: (item: T) => boolean, startIdx = 0, endIdxEx = array.length): number { |
| 46 |
let i = startIdx; |
| 47 |
let j = endIdxEx; |
| 48 |
while (i < j) { |
| 49 |
const k = Math.floor((i + j) / 2); |
| 50 |
if (predicate(array[k])) { |
| 51 |
i = k + 1; |
| 52 |
} else { |
| 53 |
j = k; |
| 54 |
} |
| 55 |
} |
| 56 |
return i - 1; |
| 57 |
} |
| 58 |
|
| 59 |
/** |
| 60 |
* Finds the first item where predicate is true using binary search. |
| 61 |
* `predicate` must be monotonous, i.e. `arr.map(predicate)` must be like `[false, ..., false, true, ..., true]`! |
| 62 |
* |
| 63 |
* @returns `undefined` if no item matches, otherwise the first item that matches the predicate. |
| 64 |
*/ |
| 65 |
export function findFirstMonotonous<T>(array: readonly T[], predicate: (item: T) => boolean): T | undefined { |
| 66 |
const idx = findFirstIdxMonotonousOrArrLen(array, predicate); |
| 67 |
return idx === array.length ? undefined : array[idx]; |
| 68 |
} |
| 69 |
|
| 70 |
/** |
| 71 |
* Finds the first item where predicate is true using binary search. |
| 72 |
* `predicate` must be monotonous, i.e. `arr.map(predicate)` must be like `[false, ..., false, true, ..., true]`! |
| 73 |
* |
| 74 |
* @returns `endIdxEx` if predicate is false for all items, otherwise the index of the first item that matches the predicate. |
| 75 |
*/ |
| 76 |
export function findFirstIdxMonotonousOrArrLen<T>(array: readonly T[], predicate: (item: T) => boolean, startIdx = 0, endIdxEx = array.length): number { |
| 77 |
let i = startIdx; |
| 78 |
let j = endIdxEx; |
| 79 |
while (i < j) { |
| 80 |
const k = Math.floor((i + j) / 2); |
| 81 |
if (predicate(array[k])) { |
| 82 |
j = k; |
| 83 |
} else { |
| 84 |
i = k + 1; |
| 85 |
} |
| 86 |
} |
| 87 |
return i; |
| 88 |
} |
| 89 |
|
| 90 |
export function findFirstIdxMonotonous<T>(array: readonly T[], predicate: (item: T) => boolean, startIdx = 0, endIdxEx = array.length): number { |
| 91 |
const idx = findFirstIdxMonotonousOrArrLen(array, predicate, startIdx, endIdxEx); |
| 92 |
return idx === array.length ? -1 : idx; |
| 93 |
} |
| 94 |
|
| 95 |
/** |
| 96 |
* Use this when |
| 97 |
* * You have a sorted array |
| 98 |
* * You query this array with a monotonous predicate to find the last item that has a certain property. |
| 99 |
* * You query this array multiple times with monotonous predicates that get weaker and weaker. |
| 100 |
*/ |
| 101 |
export class MonotonousArray<T> { |
| 102 |
public static assertInvariants = false; |
| 103 |
|
| 104 |
private _findLastMonotonousLastIdx = 0; |
| 105 |
private _prevFindLastPredicate: ((item: T) => boolean) | undefined; |
| 106 |
|
| 107 |
constructor(private readonly _array: readonly T[]) { |
| 108 |
} |
| 109 |
|
| 110 |
/** |
| 111 |
* The predicate must be monotonous, i.e. `arr.map(predicate)` must be like `[true, ..., true, false, ..., false]`! |
| 112 |
* For subsequent calls, current predicate must be weaker than (or equal to) the previous predicate, i.e. more entries must be `true`. |
| 113 |
*/ |
| 114 |
findLastMonotonous(predicate: (item: T) => boolean): T | undefined { |
| 115 |
if (MonotonousArray.assertInvariants) { |
| 116 |
if (this._prevFindLastPredicate) { |
| 117 |
for (const item of this._array) { |
| 118 |
if (this._prevFindLastPredicate(item) && !predicate(item)) { |
| 119 |
throw new Error('MonotonousArray: current predicate must be weaker than (or equal to) the previous predicate.'); |
| 120 |
} |
| 121 |
} |
| 122 |
} |
| 123 |
this._prevFindLastPredicate = predicate; |
| 124 |
} |
| 125 |
|
| 126 |
const idx = findLastIdxMonotonous(this._array, predicate, this._findLastMonotonousLastIdx); |
| 127 |
this._findLastMonotonousLastIdx = idx + 1; |
| 128 |
return idx === -1 ? undefined : this._array[idx]; |
| 129 |
} |
| 130 |
} |
| 131 |
|
| 132 |
/** |
| 133 |
* Returns the first item that is equal to or greater than every other item. |
| 134 |
*/ |
| 135 |
export function findFirstMax<T>(array: readonly T[], comparator: Comparator<T>): T | undefined { |
| 136 |
if (array.length === 0) { |
| 137 |
return undefined; |
| 138 |
} |
| 139 |
|
| 140 |
let max = array[0]; |
| 141 |
for (let i = 1; i < array.length; i++) { |
| 142 |
const item = array[i]; |
| 143 |
if (comparator(item, max) > 0) { |
| 144 |
max = item; |
| 145 |
} |
| 146 |
} |
| 147 |
return max; |
| 148 |
} |
| 149 |
|
| 150 |
/** |
| 151 |
* Returns the last item that is equal to or greater than every other item. |
| 152 |
*/ |
| 153 |
export function findLastMax<T>(array: readonly T[], comparator: Comparator<T>): T | undefined { |
| 154 |
if (array.length === 0) { |
| 155 |
return undefined; |
| 156 |
} |
| 157 |
|
| 158 |
let max = array[0]; |
| 159 |
for (let i = 1; i < array.length; i++) { |
| 160 |
const item = array[i]; |
| 161 |
if (comparator(item, max) >= 0) { |
| 162 |
max = item; |
| 163 |
} |
| 164 |
} |
| 165 |
return max; |
| 166 |
} |
| 167 |
|
| 168 |
/** |
| 169 |
* Returns the first item that is equal to or less than every other item. |
| 170 |
*/ |
| 171 |
export function findFirstMin<T>(array: readonly T[], comparator: Comparator<T>): T | undefined { |
| 172 |
return findFirstMax(array, (a, b) => -comparator(a, b)); |
| 173 |
} |
| 174 |
|
| 175 |
export function findMaxIdx<T>(array: readonly T[], comparator: Comparator<T>): number { |
| 176 |
if (array.length === 0) { |
| 177 |
return -1; |
| 178 |
} |
| 179 |
|
| 180 |
let maxIdx = 0; |
| 181 |
for (let i = 1; i < array.length; i++) { |
| 182 |
const item = array[i]; |
| 183 |
if (comparator(item, array[maxIdx]) > 0) { |
| 184 |
maxIdx = i; |
| 185 |
} |
| 186 |
} |
| 187 |
return maxIdx; |
| 188 |
} |
| 189 |
|
| 190 |
/** |
| 191 |
* Returns the first mapped value of the array which is not undefined. |
| 192 |
*/ |
| 193 |
export function mapFindFirst<T, R>(items: Iterable<T>, mapFn: (value: T) => R | undefined): R | undefined { |
| 194 |
for (const value of items) { |
| 195 |
const mapped = mapFn(value); |
| 196 |
if (mapped !== undefined) { |
| 197 |
return mapped; |
| 198 |
} |
| 199 |
} |
| 200 |
|
| 201 |
return undefined; |
| 202 |
} |