Skip to content

Commit c9c562a

Browse files
committed
Reorganize and remove duplication
1 parent bb26ab5 commit c9c562a

19 files changed

Lines changed: 180 additions & 588 deletions
Lines changed: 8 additions & 229 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
/// <reference path="./functions.ts" />
2-
namespace core {
1+
namespace collections {
32
export interface SortOptions<T> {
43
comparer: (a: T, b: T) => number;
54
sort: "insertion" | "comparison";
@@ -43,16 +42,16 @@ namespace core {
4342
}
4443

4544
public has(key: K) {
46-
return binarySearch(this._keys, key, identity, this._comparer) >= 0;
45+
return ts.binarySearch(this._keys, key, ts.identity, this._comparer) >= 0;
4746
}
4847

4948
public get(key: K) {
50-
const index = binarySearch(this._keys, key, identity, this._comparer);
49+
const index = ts.binarySearch(this._keys, key, ts.identity, this._comparer);
5150
return index >= 0 ? this._values[index] : undefined;
5251
}
5352

5453
public set(key: K, value: V) {
55-
const index = binarySearch(this._keys, key, identity, this._comparer);
54+
const index = ts.binarySearch(this._keys, key, ts.identity, this._comparer);
5655
if (index >= 0) {
5756
this._values[index] = value;
5857
}
@@ -67,12 +66,12 @@ namespace core {
6766
}
6867

6968
public delete(key: K) {
70-
const index = binarySearch(this._keys, key, identity, this._comparer);
69+
const index = ts.binarySearch(this._keys, key, ts.identity, this._comparer);
7170
if (index >= 0) {
7271
this.writePreamble();
73-
removeAt(this._keys, index);
74-
removeAt(this._values, index);
75-
if (this._order) removeAt(this._order, index);
72+
ts.orderedRemoveItemAt(this._keys, index);
73+
ts.orderedRemoveItemAt(this._values, index);
74+
if (this._order) ts.orderedRemoveItemAt(this._order, index);
7675
this.writePostScript();
7776
return true;
7877
}
@@ -211,226 +210,6 @@ namespace core {
211210
}
212211
}
213212

214-
export class SortedSet<T> {
215-
private _comparer: (a: T, b: T) => number;
216-
private _values: T[] = [];
217-
private _order: number[] | undefined;
218-
private _version = 0;
219-
private _copyOnWrite = false;
220-
221-
constructor(comparer: ((a: T, b: T) => number) | SortOptions<T>, iterable?: Iterable<T>) {
222-
this._comparer = typeof comparer === "object" ? comparer.comparer : comparer;
223-
this._order = typeof comparer === "object" && comparer.sort === "insertion" ? [] : undefined;
224-
225-
if (iterable) {
226-
const iterator = getIterator(iterable);
227-
try {
228-
for (let i = nextResult(iterator); i; i = nextResult(iterator)) {
229-
const value = i.value;
230-
this.add(value);
231-
}
232-
}
233-
finally {
234-
closeIterator(iterator);
235-
}
236-
}
237-
}
238-
239-
public get size() {
240-
return this._values.length;
241-
}
242-
243-
public get comparer() {
244-
return this._comparer;
245-
}
246-
247-
public get [Symbol.toStringTag]() {
248-
return "SortedSet";
249-
}
250-
251-
public has(value: T) {
252-
return binarySearch(this._values, value, identity, this._comparer) >= 0;
253-
}
254-
255-
public add(value: T) {
256-
const index = binarySearch(this._values, value, identity, this._comparer);
257-
if (index < 0) {
258-
this.writePreamble();
259-
insertAt(this._values, ~index, value);
260-
if (this._order) insertAt(this._order, ~index, this._version);
261-
this.writePostScript();
262-
}
263-
return this;
264-
}
265-
266-
public delete(value: T) {
267-
const index = binarySearch(this._values, value, identity, this._comparer);
268-
if (index >= 0) {
269-
this.writePreamble();
270-
removeAt(this._values, index);
271-
if (this._order) removeAt(this._order, index);
272-
this.writePostScript();
273-
return true;
274-
}
275-
return false;
276-
}
277-
278-
public clear() {
279-
if (this.size > 0) {
280-
this.writePreamble();
281-
this._values.length = 0;
282-
if (this._order) this._order.length = 0;
283-
this.writePostScript();
284-
}
285-
}
286-
287-
public forEach(callback: (value: T, key: T, collection: this) => void, thisArg?: any) {
288-
const values = this._values;
289-
const indices = this.getIterationOrder();
290-
const version = this._version;
291-
this._copyOnWrite = true;
292-
try {
293-
if (indices) {
294-
for (const i of indices) {
295-
callback.call(thisArg, values[i], values[i], this);
296-
}
297-
}
298-
else {
299-
for (const value of values) {
300-
callback.call(thisArg, value, value, this);
301-
}
302-
}
303-
}
304-
finally {
305-
if (version === this._version) {
306-
this._copyOnWrite = false;
307-
}
308-
}
309-
}
310-
311-
public keys() {
312-
return this.values();
313-
}
314-
315-
public * values() {
316-
const values = this._values;
317-
const indices = this.getIterationOrder();
318-
const version = this._version;
319-
this._copyOnWrite = true;
320-
try {
321-
if (indices) {
322-
for (const i of indices) {
323-
yield values[i];
324-
}
325-
}
326-
else {
327-
for (const value of values) {
328-
yield value;
329-
}
330-
}
331-
}
332-
finally {
333-
if (version === this._version) {
334-
this._copyOnWrite = false;
335-
}
336-
}
337-
}
338-
339-
public * entries() {
340-
const values = this._values;
341-
const indices = this.getIterationOrder();
342-
const version = this._version;
343-
this._copyOnWrite = true;
344-
try {
345-
if (indices) {
346-
for (const i of indices) {
347-
yield [values[i], values[i]] as [T, T];
348-
}
349-
}
350-
else {
351-
for (const value of values) {
352-
yield [value, value] as [T, T];
353-
}
354-
}
355-
}
356-
finally {
357-
if (version === this._version) {
358-
this._copyOnWrite = false;
359-
}
360-
}
361-
}
362-
363-
public [Symbol.iterator]() {
364-
return this.values();
365-
}
366-
367-
private writePreamble() {
368-
if (this._copyOnWrite) {
369-
this._values = this._values.slice();
370-
if (this._order) this._order = this._order.slice();
371-
this._copyOnWrite = false;
372-
}
373-
}
374-
375-
private writePostScript() {
376-
this._version++;
377-
}
378-
379-
private getIterationOrder() {
380-
if (this._order) {
381-
const order = this._order;
382-
return this._order
383-
.map((_, i) => i)
384-
.sort((x, y) => order[x] - order[y]);
385-
}
386-
return undefined;
387-
}
388-
}
389-
390-
export function binarySearch<T, U>(array: ReadonlyArray<T>, value: T, keySelector: (v: T) => U, keyComparer: (a: U, b: U) => number, offset?: number): number {
391-
if (!array || array.length === 0) {
392-
return -1;
393-
}
394-
395-
let low = offset || 0;
396-
let high = array.length - 1;
397-
const key = keySelector(value);
398-
while (low <= high) {
399-
const middle = low + ((high - low) >> 1);
400-
const midKey = keySelector(array[middle]);
401-
const result = keyComparer(midKey, key);
402-
if (result < 0) {
403-
low = middle + 1;
404-
}
405-
else if (result > 0) {
406-
high = middle - 1;
407-
}
408-
else {
409-
return middle;
410-
}
411-
}
412-
413-
return ~low;
414-
}
415-
416-
export function removeAt<T>(array: T[], index: number): void {
417-
if (index < 0 || index >= array.length) {
418-
return;
419-
}
420-
else if (index === 0) {
421-
array.shift();
422-
}
423-
else if (index === array.length - 1) {
424-
array.pop();
425-
}
426-
else {
427-
for (let i = index; i < array.length - 1; i++) {
428-
array[i] = array[i + 1];
429-
}
430-
array.length--;
431-
}
432-
}
433-
434213
export function insertAt<T>(array: T[], index: number, value: T): void {
435214
if (index === 0) {
436215
array.unshift(value);

src/harness/compiler.ts

Lines changed: 8 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,6 @@
1-
/// <reference path="./harness.ts" />
2-
/// <reference path="./documents.ts" />
3-
/// <reference path="./core.ts" />
4-
/// <reference path="./vpath.ts" />
5-
/// <reference path="./vfs.ts" />
6-
/// <reference path="./utils.ts" />
7-
/// <reference path="./fakes.ts" />
8-
9-
// NOTE: The contents of this file are all exported from the namespace 'compiler'. This is to
10-
// support the eventual conversion of harness into a modular system.
11-
1+
/**
2+
* Test harness compiler functionality.
3+
*/
124
namespace compiler {
135
export interface Project {
146
file: string;
@@ -64,7 +56,7 @@ namespace compiler {
6456
public readonly maps: ReadonlyMap<string, documents.TextDocument>;
6557

6658
private _inputs: documents.TextDocument[] = [];
67-
private _inputsAndOutputs: core.SortedMap<string, CompilationOutput>;
59+
private _inputsAndOutputs: collections.SortedMap<string, CompilationOutput>;
6860

6961
constructor(host: fakes.CompilerHost, options: ts.CompilerOptions, program: ts.Program | undefined, result: ts.EmitResult | undefined, diagnostics: ts.Diagnostic[]) {
7062
this.host = host;
@@ -74,9 +66,9 @@ namespace compiler {
7466
this.options = program ? program.getCompilerOptions() : options;
7567

7668
// collect outputs
77-
const js = this.js = new core.SortedMap<string, documents.TextDocument>({ comparer: this.vfs.stringComparer, sort: "insertion" });
78-
const dts = this.dts = new core.SortedMap<string, documents.TextDocument>({ comparer: this.vfs.stringComparer, sort: "insertion" });
79-
const maps = this.maps = new core.SortedMap<string, documents.TextDocument>({ comparer: this.vfs.stringComparer, sort: "insertion" });
69+
const js = this.js = new collections.SortedMap<string, documents.TextDocument>({ comparer: this.vfs.stringComparer, sort: "insertion" });
70+
const dts = this.dts = new collections.SortedMap<string, documents.TextDocument>({ comparer: this.vfs.stringComparer, sort: "insertion" });
71+
const maps = this.maps = new collections.SortedMap<string, documents.TextDocument>({ comparer: this.vfs.stringComparer, sort: "insertion" });
8072
for (const document of this.host.outputs) {
8173
if (vpath.isJavaScript(document.file)) {
8274
js.set(document.file, document);
@@ -90,7 +82,7 @@ namespace compiler {
9082
}
9183

9284
// correlate inputs and outputs
93-
this._inputsAndOutputs = new core.SortedMap<string, CompilationOutput>({ comparer: this.vfs.stringComparer, sort: "insertion" });
85+
this._inputsAndOutputs = new collections.SortedMap<string, CompilationOutput>({ comparer: this.vfs.stringComparer, sort: "insertion" });
9486
if (program) {
9587
if (this.options.out || this.options.outFile) {
9688
const outFile = vpath.resolve(this.vfs.cwd(), this.options.outFile || this.options.out);

src/harness/compilerRunner.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -273,8 +273,8 @@ class CompilerTest {
273273
}
274274

275275
private makeUnitName(name: string, root: string) {
276-
const path = ts.toPath(name, root, core.identity);
277-
const pathStart = ts.toPath(Harness.IO.getCurrentDirectory(), "", core.identity);
276+
const path = ts.toPath(name, root, ts.identity);
277+
const pathStart = ts.toPath(Harness.IO.getCurrentDirectory(), "", ts.identity);
278278
return pathStart ? path.replace(pathStart, "/") : path;
279279
}
280280

src/harness/core.ts

Lines changed: 0 additions & 4 deletions
This file was deleted.

src/harness/core/comparers.ts

Lines changed: 0 additions & 43 deletions
This file was deleted.

src/harness/core/functions.ts

Lines changed: 0 additions & 3 deletions
This file was deleted.

0 commit comments

Comments
 (0)