Skip to content

Commit 6927bc7

Browse files
committed
Remove SortedUniqueList
1 parent 151dc07 commit 6927bc7

File tree

2 files changed

+13
-83
lines changed

2 files changed

+13
-83
lines changed

src/compiler/core.ts

Lines changed: 3 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -1061,10 +1061,10 @@ namespace ts {
10611061
/**
10621062
* Stable sort of an array. Elements equal to each other maintain their relative position in the array.
10631063
*/
1064-
export function stableSort<T>(array: ReadonlyArray<T>, comparer: Comparer<T>) {
1064+
export function stableSort<T>(array: ReadonlyArray<T>, comparer: Comparer<T>): SortedReadonlyArray<T> {
10651065
const indices = array.map((_, i) => i);
10661066
stableSortIndices(array, indices, comparer);
1067-
return indices.map(i => array[i]);
1067+
return indices.map(i => array[i]) as SortedArray<T> as SortedReadonlyArray<T>;
10681068
}
10691069

10701070
export function rangeEquals<T>(array1: ReadonlyArray<T>, array2: ReadonlyArray<T>, pos: number, end: number) {
@@ -1156,7 +1156,7 @@ namespace ts {
11561156
* @param offset An offset into `array` at which to start the search.
11571157
*/
11581158
export function binarySearch<T, U>(array: ReadonlyArray<T>, value: T, keySelector: (v: T) => U, keyComparer: Comparer<U>, offset?: number): number {
1159-
return some(array) ? binarySearchKey(array, keySelector(value), keySelector, keyComparer, offset) : -1;
1159+
return binarySearchKey(array, keySelector(value), keySelector, keyComparer, offset);
11601160
}
11611161

11621162
/**
@@ -2167,74 +2167,4 @@ namespace ts {
21672167
export function fill<T>(length: number, cb: (index: number) => T): T[] {
21682168
return new Array(length).fill(0).map((_, i) => cb(i));
21692169
}
2170-
2171-
/**
2172-
* A list of sorted and unique values. Optimized for best performance when items are added
2173-
* in the sort order.
2174-
*/
2175-
export class SortedUniqueList<T> implements Push<T> {
2176-
private relationalComparer: (a: T, b: T) => Comparison;
2177-
private equalityComparer: (a: T, b: T) => boolean;
2178-
private sortedAndUnique = true;
2179-
private copyOnWrite = false;
2180-
private unsafeArray: T[] = [];
2181-
private unsafeLast: T | undefined = undefined;
2182-
2183-
constructor(relationalComparer: Comparer<T>, equalityComparer: EqualityComparer<T>) {
2184-
this.relationalComparer = relationalComparer;
2185-
this.equalityComparer = equalityComparer;
2186-
}
2187-
2188-
get size() {
2189-
return this.unsafeArray.length;
2190-
}
2191-
2192-
get last() {
2193-
this.ensureSortedAndUnique();
2194-
return this.unsafeLast === undefined
2195-
? this.unsafeLast = lastOrUndefined(this.unsafeArray)
2196-
: this.unsafeLast;
2197-
}
2198-
2199-
push(...values: T[]) {
2200-
for (const value of values) {
2201-
if (this.sortedAndUnique) {
2202-
const last = this.last;
2203-
if (last === undefined || this.relationalComparer(value, last) > 0) {
2204-
this.unsafeAdd(value, /*sortedAndUnique*/ true);
2205-
continue;
2206-
}
2207-
if (this.equalityComparer(value, last)) {
2208-
continue;
2209-
}
2210-
}
2211-
this.unsafeAdd(value, /*sortedAndUnique*/ false);
2212-
}
2213-
}
2214-
2215-
toArray(): ReadonlyArray<T> {
2216-
this.ensureSortedAndUnique();
2217-
this.copyOnWrite = true;
2218-
return this.unsafeArray;
2219-
}
2220-
2221-
private unsafeAdd(value: T, sortedAndUnique: boolean) {
2222-
if (this.copyOnWrite || this.unsafeArray === emptyArray) {
2223-
this.unsafeArray = this.unsafeArray.slice();
2224-
this.copyOnWrite = false;
2225-
}
2226-
2227-
this.unsafeArray.push(value);
2228-
this.unsafeLast = sortedAndUnique ? value : undefined;
2229-
this.sortedAndUnique = sortedAndUnique;
2230-
}
2231-
2232-
private ensureSortedAndUnique() {
2233-
if (!this.sortedAndUnique) {
2234-
this.unsafeArray = deduplicateSorted(stableSort(this.unsafeArray, this.relationalComparer) as any as SortedReadonlyArray<T>, this.equalityComparer) as any as T[];
2235-
this.unsafeLast = undefined;
2236-
this.sortedAndUnique = true;
2237-
}
2238-
}
2239-
}
22402170
}

src/compiler/sourcemap.ts

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -566,18 +566,18 @@ namespace ts {
566566
&& value.sourcePosition !== undefined;
567567
}
568568

569-
function sameMappedPosition<T extends MappedPosition>(left: T, right: T) {
569+
function sameMappedPosition(left: MappedPosition, right: MappedPosition) {
570570
return left.generatedPosition === right.generatedPosition
571571
&& left.sourceIndex === right.sourceIndex
572572
&& left.sourcePosition === right.sourcePosition;
573573
}
574574

575575
function compareSourcePositions(left: SourceMappedPosition, right: SourceMappedPosition) {
576-
return left.sourcePosition - right.sourcePosition;
576+
return compareValues(left.sourceIndex, right.sourceIndex);
577577
}
578578

579579
function compareGeneratedPositions(left: MappedPosition, right: MappedPosition) {
580-
return left.generatedPosition - right.generatedPosition;
580+
return compareValues(left.generatedPosition, right.generatedPosition);
581581
}
582582

583583
function getSourcePositionOfMapping(value: SourceMappedPosition) {
@@ -598,8 +598,8 @@ namespace ts {
598598
const sourceFileCanonicalPaths = sourceFileAbsolutePaths.map(source => host.getCanonicalFileName(source) as Path);
599599
const sourceToSourceIndexMap = createMapFromEntries(sourceFileCanonicalPaths.map((source, i) => [source, i] as [string, number]));
600600
let decodedMappings: ReadonlyArray<MappedPosition> | undefined;
601-
let generatedMappings: ReadonlyArray<MappedPosition> | undefined;
602-
let sourceMappings: ReadonlyArray<ReadonlyArray<SourceMappedPosition>> | undefined;
601+
let generatedMappings: SortedReadonlyArray<MappedPosition> | undefined;
602+
let sourceMappings: ReadonlyArray<SortedReadonlyArray<SourceMappedPosition>> | undefined;
603603

604604
return {
605605
getSourcePosition,
@@ -648,25 +648,25 @@ namespace ts {
648648

649649
function getSourceMappings(sourceIndex: number) {
650650
if (sourceMappings === undefined) {
651-
const lists: SortedUniqueList<SourceMappedPosition>[] = [];
651+
const lists: SourceMappedPosition[][] = [];
652652
for (const mapping of getDecodedMappings()) {
653653
if (!isSourceMappedPosition(mapping)) continue;
654654
let list = lists[mapping.sourceIndex];
655-
if (!list) lists[mapping.sourceIndex] = list = new SortedUniqueList(compareSourcePositions, sameMappedPosition);
655+
if (!list) lists[mapping.sourceIndex] = list = [];
656656
list.push(mapping);
657657
}
658-
sourceMappings = lists.map(list => list.toArray());
658+
sourceMappings = lists.map(list => sortAndDeduplicate<SourceMappedPosition>(list, compareSourcePositions, sameMappedPosition));
659659
}
660660
return sourceMappings[sourceIndex];
661661
}
662662

663663
function getGeneratedMappings() {
664664
if (generatedMappings === undefined) {
665-
const list = new SortedUniqueList(compareGeneratedPositions, sameMappedPosition);
665+
const list: MappedPosition[] = [];
666666
for (const mapping of getDecodedMappings()) {
667667
list.push(mapping);
668668
}
669-
generatedMappings = list.toArray();
669+
generatedMappings = sortAndDeduplicate(list, compareGeneratedPositions, sameMappedPosition);
670670
}
671671
return generatedMappings;
672672
}

0 commit comments

Comments
 (0)