Skip to content

Commit

Permalink
maptype: fix tslint issues, remove spread stmt, as needs helper in es5
Browse files Browse the repository at this point in the history
  • Loading branch information
adamhaile committed Sep 2, 2018
1 parent 5b88002 commit 2567f5a
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 46 deletions.
2 changes: 1 addition & 1 deletion src/expression.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ class Expression {
}

public toString(): string {
let result = this._terms._array.map(function(pair, idx) {
let result = this._terms.array.map(function(pair, idx) {
return (pair.second + "*" + pair.first.toString());
}).join(" + ");

Expand Down
97 changes: 52 additions & 45 deletions src/maptype.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,44 +9,44 @@
export interface IMap<T extends { id(): number }, U> extends IndexedMap<T, U> { }

export
function createMap<T extends { id() : number }, U>( compare: any ): IMap<T, U> {
function createMap<T extends { id(): number }, U>( compare: any ): IMap<T, U> {
return new IndexedMap<T, U>();
}

class IndexedMap<T extends { id() : number }, U> {
public _index = {} as { [ id : number ] : number | undefined };
public _array = [] as Pair<T, U>[];
class IndexedMap<T extends { id(): number }, U> {
public index = {} as { [ id: number ]: number | undefined };
public array = [] as Array<Pair<T, U>>;

/**
* Returns the number of items in the array.
*/
public size(): number {
return this._array.length;
return this.array.length;
}

/**
* Returns true if the array is empty.
*/
* Returns true if the array is empty.
*/
public empty(): boolean {
return this._array.length === 0;
return this.array.length === 0;
}

/**
* Returns the item at the given array index.
*
* @param index The integer index of the desired item.
*/
public itemAt(index: number): Pair<T, U> {
return this._array[index];
return this.array[index];
}

/**
* Returns true if the key is in the array, false otherwise.
*
* @param key The key to locate in the array.
*/
* Returns true if the key is in the array, false otherwise.
*
* @param key The key to locate in the array.
*/
public contains(key: T) {
return this._index[key.id()] !== undefined;
return this.index[key.id()] !== undefined;
}

/**
Expand All @@ -55,8 +55,8 @@ class IndexedMap<T extends { id() : number }, U> {
* @param key The key to locate in the array.
*/
public find(key: T) {
const i = this._index[key.id()];
return i === undefined ? undefined : this._array[i];
const i = this.index[key.id()];
return i === undefined ? undefined : this.array[i];
}

/**
Expand All @@ -69,14 +69,14 @@ class IndexedMap<T extends { id() : number }, U> {
* @param factory The function which creates the default value.
*/
public setDefault(key: T, factory: () => U): Pair<T, U> {
const i = this._index[key.id()];
const i = this.index[key.id()];
if (i === undefined) {
const pair = new Pair(key, factory());
this._index[key.id()] = this._array.length;
this._array.push(pair);
this.index[key.id()] = this.array.length;
this.array.push(pair);
return pair;
} else {
return this._array[i];
return this.array[i];
}
}

Expand All @@ -89,13 +89,13 @@ class IndexedMap<T extends { id() : number }, U> {
* @param value The value portion of the pair.
*/
public insert(key: T, value: U): Pair<T, U> {
const pair = new Pair(key, value),
i = this._index[key.id()];
const pair = new Pair(key, value);
const i = this.index[key.id()];
if (i === undefined) {
this._index[key.id()] = this._array.length;
this._array.push(pair);
this.index[key.id()] = this.array.length;
this.array.push(pair);
} else {
this._array[i] = pair;
this.array[i] = pair;
}
return pair;
}
Expand All @@ -106,14 +106,16 @@ class IndexedMap<T extends { id() : number }, U> {
* @param key The key to remove from the map.
*/
public erase(key: T): Pair<T, U> {
const i = this._index[key.id()];
if (i === undefined) return undefined;
this._index[key.id()] = undefined;
const pair = this._array[i],
last = this._array.pop();
const i = this.index[key.id()];
if (i === undefined) {
return undefined;
}
this.index[key.id()] = undefined;
const pair = this.array[i];
const last = this.array.pop();
if (pair !== last) {
this._array[i] = last;
this._index[last.first.id()] = i;
this.array[i] = last;
this.index[last.first.id()] = i;
}
return pair;
}
Expand All @@ -123,26 +125,31 @@ class IndexedMap<T extends { id() : number }, U> {
*/
public copy(): IndexedMap<T, U> {
const copy = new IndexedMap<T, U>();
copy._index = { ...this._index };
copy._array = this._array.map(p => p.copy());
for (let i = 0; i < this.array.length; i++) {
const pair = this.array[i].copy();
copy.array[i] = pair;
copy.index[pair.first.id()] = i;
}
return copy;
}
}

/**
* A class which defines a generic pair object.
*/
* A class which defines a generic pair object.
* @private
*/
// tslint:disable: max-classes-per-file
class Pair<T, U> {
/**
* Construct a new Pair object.
*
* @param first The first item of the pair.
* @param second The second item of the pair.
*/
* Construct a new Pair object.
*
* @param first The first item of the pair.
* @param second The second item of the pair.
*/
constructor(public first: T, public second: U) { }

/**
* Create a copy of the pair.
*/
* Create a copy of the pair.
*/
public copy() { return new Pair(this.first, this.second); }
}

0 comments on commit 2567f5a

Please sign in to comment.