Remove SortedUniqueList

This commit is contained in:
Ron Buckton 2018-11-09 14:32:31 -08:00
parent 151dc074a8
commit 6927bc74fd
2 changed files with 13 additions and 83 deletions

View file

@ -1061,10 +1061,10 @@ namespace ts {
/**
* Stable sort of an array. Elements equal to each other maintain their relative position in the array.
*/
export function stableSort<T>(array: ReadonlyArray<T>, comparer: Comparer<T>) {
export function stableSort<T>(array: ReadonlyArray<T>, comparer: Comparer<T>): SortedReadonlyArray<T> {
const indices = array.map((_, i) => i);
stableSortIndices(array, indices, comparer);
return indices.map(i => array[i]);
return indices.map(i => array[i]) as SortedArray<T> as SortedReadonlyArray<T>;
}
export function rangeEquals<T>(array1: ReadonlyArray<T>, array2: ReadonlyArray<T>, pos: number, end: number) {
@ -1156,7 +1156,7 @@ namespace ts {
* @param offset An offset into `array` at which to start the search.
*/
export function binarySearch<T, U>(array: ReadonlyArray<T>, value: T, keySelector: (v: T) => U, keyComparer: Comparer<U>, offset?: number): number {
return some(array) ? binarySearchKey(array, keySelector(value), keySelector, keyComparer, offset) : -1;
return binarySearchKey(array, keySelector(value), keySelector, keyComparer, offset);
}
/**
@ -2167,74 +2167,4 @@ namespace ts {
export function fill<T>(length: number, cb: (index: number) => T): T[] {
return new Array(length).fill(0).map((_, i) => cb(i));
}
/**
* A list of sorted and unique values. Optimized for best performance when items are added
* in the sort order.
*/
export class SortedUniqueList<T> implements Push<T> {
private relationalComparer: (a: T, b: T) => Comparison;
private equalityComparer: (a: T, b: T) => boolean;
private sortedAndUnique = true;
private copyOnWrite = false;
private unsafeArray: T[] = [];
private unsafeLast: T | undefined = undefined;
constructor(relationalComparer: Comparer<T>, equalityComparer: EqualityComparer<T>) {
this.relationalComparer = relationalComparer;
this.equalityComparer = equalityComparer;
}
get size() {
return this.unsafeArray.length;
}
get last() {
this.ensureSortedAndUnique();
return this.unsafeLast === undefined
? this.unsafeLast = lastOrUndefined(this.unsafeArray)
: this.unsafeLast;
}
push(...values: T[]) {
for (const value of values) {
if (this.sortedAndUnique) {
const last = this.last;
if (last === undefined || this.relationalComparer(value, last) > 0) {
this.unsafeAdd(value, /*sortedAndUnique*/ true);
continue;
}
if (this.equalityComparer(value, last)) {
continue;
}
}
this.unsafeAdd(value, /*sortedAndUnique*/ false);
}
}
toArray(): ReadonlyArray<T> {
this.ensureSortedAndUnique();
this.copyOnWrite = true;
return this.unsafeArray;
}
private unsafeAdd(value: T, sortedAndUnique: boolean) {
if (this.copyOnWrite || this.unsafeArray === emptyArray) {
this.unsafeArray = this.unsafeArray.slice();
this.copyOnWrite = false;
}
this.unsafeArray.push(value);
this.unsafeLast = sortedAndUnique ? value : undefined;
this.sortedAndUnique = sortedAndUnique;
}
private ensureSortedAndUnique() {
if (!this.sortedAndUnique) {
this.unsafeArray = deduplicateSorted(stableSort(this.unsafeArray, this.relationalComparer) as any as SortedReadonlyArray<T>, this.equalityComparer) as any as T[];
this.unsafeLast = undefined;
this.sortedAndUnique = true;
}
}
}
}

View file

@ -566,18 +566,18 @@ namespace ts {
&& value.sourcePosition !== undefined;
}
function sameMappedPosition<T extends MappedPosition>(left: T, right: T) {
function sameMappedPosition(left: MappedPosition, right: MappedPosition) {
return left.generatedPosition === right.generatedPosition
&& left.sourceIndex === right.sourceIndex
&& left.sourcePosition === right.sourcePosition;
}
function compareSourcePositions(left: SourceMappedPosition, right: SourceMappedPosition) {
return left.sourcePosition - right.sourcePosition;
return compareValues(left.sourceIndex, right.sourceIndex);
}
function compareGeneratedPositions(left: MappedPosition, right: MappedPosition) {
return left.generatedPosition - right.generatedPosition;
return compareValues(left.generatedPosition, right.generatedPosition);
}
function getSourcePositionOfMapping(value: SourceMappedPosition) {
@ -598,8 +598,8 @@ namespace ts {
const sourceFileCanonicalPaths = sourceFileAbsolutePaths.map(source => host.getCanonicalFileName(source) as Path);
const sourceToSourceIndexMap = createMapFromEntries(sourceFileCanonicalPaths.map((source, i) => [source, i] as [string, number]));
let decodedMappings: ReadonlyArray<MappedPosition> | undefined;
let generatedMappings: ReadonlyArray<MappedPosition> | undefined;
let sourceMappings: ReadonlyArray<ReadonlyArray<SourceMappedPosition>> | undefined;
let generatedMappings: SortedReadonlyArray<MappedPosition> | undefined;
let sourceMappings: ReadonlyArray<SortedReadonlyArray<SourceMappedPosition>> | undefined;
return {
getSourcePosition,
@ -648,25 +648,25 @@ namespace ts {
function getSourceMappings(sourceIndex: number) {
if (sourceMappings === undefined) {
const lists: SortedUniqueList<SourceMappedPosition>[] = [];
const lists: SourceMappedPosition[][] = [];
for (const mapping of getDecodedMappings()) {
if (!isSourceMappedPosition(mapping)) continue;
let list = lists[mapping.sourceIndex];
if (!list) lists[mapping.sourceIndex] = list = new SortedUniqueList(compareSourcePositions, sameMappedPosition);
if (!list) lists[mapping.sourceIndex] = list = [];
list.push(mapping);
}
sourceMappings = lists.map(list => list.toArray());
sourceMappings = lists.map(list => sortAndDeduplicate<SourceMappedPosition>(list, compareSourcePositions, sameMappedPosition));
}
return sourceMappings[sourceIndex];
}
function getGeneratedMappings() {
if (generatedMappings === undefined) {
const list = new SortedUniqueList(compareGeneratedPositions, sameMappedPosition);
const list: MappedPosition[] = [];
for (const mapping of getDecodedMappings()) {
list.push(mapping);
}
generatedMappings = list.toArray();
generatedMappings = sortAndDeduplicate(list, compareGeneratedPositions, sameMappedPosition);
}
return generatedMappings;
}