diff --git a/container/arraylist/js_arraylist.ts b/container/arraylist/js_arraylist.ts index 50f1229667a27ba534a49616e3943c0652c3cf44..44d98494da098a4214a60be38d4be7ca946537b9 100644 --- a/container/arraylist/js_arraylist.ts +++ b/container/arraylist/js_arraylist.ts @@ -179,7 +179,11 @@ if (flag || fastArrayList === undefined) { if (fromIndex >= this.elementNum || fromIndex < 0 || toIndex < 0) { throw new RangeError(`the fromIndex or the toIndex is out-of-bounds`); } - toIndex = toIndex >= this.elementNum - 1 ? this.elementNum - 1 : toIndex; + if (toIndex === this.elementNum) { + toIndex = this.elementNum - 1; + } else if (toIndex > this.elementNum) { + toIndex = this.elementNum; + } let i: number = fromIndex; for (let j: number = toIndex; j < this.elementNum; j++) { this[i] = this[j]; @@ -280,7 +284,7 @@ if (flag || fastArrayList === undefined) { return this.elementNum === this.capacity; } private resize(): void { - this.capacity = 1.5 * this.capacity; + this.capacity = Math.floor(1.5 * this.capacity); } isEmpty(): boolean { return this.elementNum === 0; diff --git a/container/linkedlist/js_linkedlist.ts b/container/linkedlist/js_linkedlist.ts index 9ede2de1a3104e9ed78021974a8abaae974c1a6e..ab08773b9cc19f702f880451f3b97eccface7df8 100644 --- a/container/linkedlist/js_linkedlist.ts +++ b/container/linkedlist/js_linkedlist.ts @@ -79,7 +79,7 @@ if (flag || fastLinkedList === undefined) { ownKeys(obj: LinkedList): Array { let keys: Array = []; let length: number = obj.length; - for (let i = 0; i < length; i++) { + for (let i: number = 0; i < length; i++) { keys.push(i.toString()); } return keys; @@ -160,11 +160,8 @@ if (flag || fastLinkedList === undefined) { if (this.head === undefined) { this.head = this.tail = node; } else { - let current: NodeObj = this.head; - while (current.next !== undefined) { - current = current.next; - } - this.tail = current.next = node; + this.tail.next = node; + this.tail = node; } this.elementNum++; return true; diff --git a/container/list/js_list.ts b/container/list/js_list.ts index 5eecd86201901fec0b47dbcecea7a18527aea17d..3b3d32fb8041300edbcb87451c6a9817ce083f7d 100644 --- a/container/list/js_list.ts +++ b/container/list/js_list.ts @@ -43,7 +43,8 @@ if (flag || fastList === undefined) { if (prop === 'elementNum' || prop === 'capacity' || prop === 'head' || - prop === 'next') { + prop === 'next' || + prop === 'tail') { obj[prop] = value; return true; } @@ -114,10 +115,12 @@ if (flag || fastList === undefined) { private head: NodeObj; private elementNum: number; private capacity: number; + private tail: NodeObj; constructor() { this.head = undefined; this.elementNum = 0; this.capacity = 10; + this.tail = undefined; return new Proxy(this, new HandlerList()); } get length(): number { @@ -148,19 +151,17 @@ if (flag || fastList === undefined) { add(element: T): boolean { let node: NodeObj = new NodeObj(element); if (this.head === undefined) { - this.head = node; + this.head = this.tail = node; } else { - let current: NodeObj = this.head; - while (current.next !== undefined) { - current = current.next; - } - current.next = node; + this.tail.next = node; + this.tail = node; } this.elementNum++; return true; } clear(): void { this.head = undefined; + this.tail = undefined; this.elementNum = 0; } has(element: T): boolean { @@ -233,6 +234,12 @@ if (flag || fastList === undefined) { if (index === 0) { oldNode = this.head; this.head = oldNode && oldNode.next; + } else if (index === this.elementNum - 1) { + oldNode = this.getNode(index - 1); + if (oldNode !== undefined) { + this.tail = oldNode; + oldNode.next = undefined; + } } else { let prevNode: NodeObj = undefined; prevNode = this.getNode(index - 1); @@ -276,9 +283,7 @@ if (flag || fastList === undefined) { if (this.isEmpty()) { return undefined; } - let newNode: NodeObj = undefined; - newNode = this.getNode(this.elementNum - 1); - let element: T = newNode.element; + let element: T = this.tail.element; return element; } insert(element: T, index: number): void { @@ -292,6 +297,7 @@ if (flag || fastList === undefined) { let current: NodeObj = this.head; newNode.next = current; this.head = newNode; + this.tail = this.head.next; } else { let prevNode: NodeObj = undefined; prevNode = this.getNode(index - 1); diff --git a/container/plainarray/js_plainarray.ts b/container/plainarray/js_plainarray.ts index 66af31f177539a7623fe807766f68a4b5a2a3e48..e96c2fc67c9050b0c56e0f4312ef2f916908a771 100644 --- a/container/plainarray/js_plainarray.ts +++ b/container/plainarray/js_plainarray.ts @@ -59,10 +59,13 @@ if (flag || fastPlainArray === undefined) { get length(): number { return this.memberNumber; } - add(key: number, value: T): void { - if (typeof key !== 'number') { + private isInteger(key: number): void { + if (key % 1 !== 0) { throw new TypeError('the index is not integer'); } + } + add(key: number, value: T): void { + this.isInteger(key); this.addmember(key, value); } clear(): void { @@ -86,17 +89,13 @@ if (flag || fastPlainArray === undefined) { return this.binarySearchAtPlain(key) > -1; } get(key: number): T { - if (typeof key !== 'number') { - throw new TypeError('the index is not integer'); - } + this.isInteger(key); let index: number = 0; index = this.binarySearchAtPlain(key); return this.members.values[index]; } getIndexOfKey(key: number): number { - if (typeof key !== 'number') { - throw new TypeError('the index is not integer'); - } + this.isInteger(key); let result: number = 0; result = this.binarySearchAtPlain(key); return result < 0 ? -1 : result; @@ -108,16 +107,12 @@ if (flag || fastPlainArray === undefined) { return this.memberNumber === 0; } getKeyAt(index: number): number { - if (typeof index !== 'number') { - throw new TypeError('the index is not integer'); - } + this.isInteger(index); return this.members.keys[index]; } remove(key: number): T { let result: any = undefined; - if (typeof key !== 'number') { - throw new TypeError('the index is not integer'); - } + this.isInteger(key); let index: number = 0; index = this.binarySearchAtPlain(key); if (index < 0) { @@ -126,9 +121,7 @@ if (flag || fastPlainArray === undefined) { return this.deletemember(index); } removeAt(index: number): T { - if (typeof index !== 'number') { - throw new TypeError('the index is not integer'); - } + this.isInteger(index); let result: any = undefined; if (index >= this.memberNumber || index < 0) { return result; @@ -136,7 +129,7 @@ if (flag || fastPlainArray === undefined) { return this.deletemember(index); } removeRangeFrom(index: number, size: number): number { - if (typeof index !== 'number' || typeof size !== 'number') { + if (index % 1 !== 0 || size % 1 !== 0) { throw new TypeError('the index is not integer'); } if (index >= this.memberNumber || index < 0) { @@ -148,9 +141,7 @@ if (flag || fastPlainArray === undefined) { return safeSize; } setValueAt(index: number, value: T): void { - if (typeof index !== 'number') { - throw new TypeError('the index is not integer'); - } + this.isInteger(index); if (index >= 0 && index < this.memberNumber) { this.members.values[index] = value; } else { @@ -165,9 +156,7 @@ if (flag || fastPlainArray === undefined) { return result.join(','); } getValueAt(index: number): T { - if (typeof index !== 'number') { - throw new TypeError('the index is not integer'); - } + this.isInteger(index); return this.members.values[index]; } forEach(callbackfn: (value: T, index?: number, PlainArray?: PlainArray) => void, diff --git a/container/stack/js_stack.ts b/container/stack/js_stack.ts index 3703d132c7e29ed33d1864586c43401fd0d1efbc..52eb3c8c574776d138e4da857c4931a72672fbfd 100644 --- a/container/stack/js_stack.ts +++ b/container/stack/js_stack.ts @@ -135,7 +135,7 @@ if (flag || fastStack === undefined) { return this.elementNum === this.capacity; } private increaseCapacity(): void { - this.capacity = 1.5 * this.capacity; + this.capacity = Math.floor(1.5 * this.capacity); } [Symbol.iterator](): IterableIterator { let count: number = 0; diff --git a/container/vector/js_vector.ts b/container/vector/js_vector.ts index 202fff939f1508ec2079ee0eed29a8e8587d8aab..e4602e6fc95909cafee9e94f3deda875f5fbea0e 100644 --- a/container/vector/js_vector.ts +++ b/container/vector/js_vector.ts @@ -224,7 +224,11 @@ if (flag || fastVector === undefined) { if (fromIndex >= this.elementNum || fromIndex < 0 || toIndex < 0) { throw new RangeError(`the fromIndex or the toIndex is out-of-bounds`); } - toIndex = toIndex >= this.elementNum ? this.elementNum : toIndex; + if (toIndex === this.elementNum) { + toIndex = this.elementNum - 1; + } else if (toIndex > this.elementNum) { + toIndex = this.elementNum; + } let i: number = fromIndex; for (let j = toIndex; j < this.elementNum; j++) { this[i] = this[j]; @@ -315,9 +319,9 @@ if (flag || fastVector === undefined) { return arr; } copyToArray(array: Array): void { - let arr: Array = this.convertToArray(); - for (let i: number = 0; i < array.length; i++) { - array[i] = arr[i]; + let len: number = array.length; + for (let i: number = 0; i < this.length; i++) { + array[len + i] = this[i]; } } toString(): string {