findMaxBy -> findLastMaxBy

This commit is contained in:
Henning Dieterichs 2021-12-08 12:51:54 +01:00
parent 983a71fad9
commit 137ea7552b
No known key found for this signature in database
GPG key ID: 771381EFFDB9EC06
3 changed files with 29 additions and 2 deletions

View file

@ -625,6 +625,24 @@ export function findMaxBy<T>(items: readonly T[], comparator: Comparator<T>): T
return max;
}
/**
* Returns the last item that is equal to or greater than every other item.
*/
export function findLastMaxBy<T>(items: readonly T[], comparator: Comparator<T>): T | undefined {
if (items.length === 0) {
return undefined;
}
let max = items[0];
for (let i = 1; i < items.length; i++) {
const item = items[i];
if (comparator(item, max) >= 0) {
max = item;
}
}
return max;
}
/**
* Returns the first item that is equal to or less than every other item.
*/

View file

@ -347,6 +347,15 @@ suite('Arrays', () => {
);
});
test('findLastMaxBy', () => {
const array = [{ v: 3 }, { v: 5 }, { v: 2 }, { v: 2 }, { v: 2 }, { v: 5 }];
assert.strictEqual(
array.indexOf(arrays.findLastMaxBy(array, arrays.compareBy(v => v.v, arrays.numberComparator))!),
5
);
});
test('findMinBy', () => {
const array = [{ v: 3 }, { v: 5 }, { v: 2 }, { v: 2 }, { v: 2 }, { v: 5 }];

View file

@ -3,7 +3,7 @@
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import { compareBy, findMaxBy, findMinBy } from 'vs/base/common/arrays';
import { compareBy, findLastMaxBy, findMinBy } from 'vs/base/common/arrays';
import { CursorContext, CursorState, PartialCursorState } from 'vs/editor/common/controller/cursorCommon';
import { Cursor } from 'vs/editor/common/controller/oneCursor';
import { Position } from 'vs/editor/common/core/position';
@ -78,7 +78,7 @@ export class CursorCollection {
}
public getBottomMostViewPosition(): Position {
return findMaxBy(
return findLastMaxBy(
this.cursors,
compareBy(c => c.viewState.position, Position.compare)
)!.viewState.position;