From dd2b7787cdb7dcff0333b4ac0bdeed058636b387 Mon Sep 17 00:00:00 2001 From: wangyong Date: Sun, 16 Jan 2022 11:36:49 +0800 Subject: [PATCH] modified TS linear container class Signed-off-by: wangyong --- container/arraylist/js_arraylist.ts | 5 ++++- container/deque/js_deque.ts | 14 +++++++------- container/linkedlist/js_linkedlist.ts | 14 ++++++++------ container/list/js_list.ts | 8 +++++++- container/queue/js_queue.ts | 11 +++++++---- container/stack/js_stack.ts | 3 +++ container/vector/js_vector.ts | 12 +++++++++--- 7 files changed, 45 insertions(+), 22 deletions(-) diff --git a/container/arraylist/js_arraylist.ts b/container/arraylist/js_arraylist.ts index 7ab4948..bd7368e 100644 --- a/container/arraylist/js_arraylist.ts +++ b/container/arraylist/js_arraylist.ts @@ -265,12 +265,15 @@ if (flag || fastArrayList == undefined) { private resize(): void { this._capacity = 1.5 * this._capacity; } + isEmpty(): boolean { + return this._length == 0; + } increaseCapacityTo(newCapacity: number): void { if (newCapacity >= this._length) { this._capacity = newCapacity; } } - trimToCurrentSize(): void { + trimToCurrentLength(): void { this._capacity = this._length; } [Symbol.iterator](): IterableIterator { diff --git a/container/deque/js_deque.ts b/container/deque/js_deque.ts index c7f716f..f644914 100644 --- a/container/deque/js_deque.ts +++ b/container/deque/js_deque.ts @@ -73,8 +73,8 @@ if (flag || fastDeque == undefined) { } return; } - setPrototypeOf(obj: any, prop: any): T { - throw new Error("Can setPrototype on ArrayList Object"); + setPrototypeOf(obj: any, prop: any): any { + throw new Error("Can setPrototype on Deque Object"); } } interface IterableIterator { @@ -171,13 +171,13 @@ if (flag || fastDeque == undefined) { return (this._rear + 1) % this._capacity === this._front; } [Symbol.iterator](): IterableIterator { - let _this = this; - let count = _this._front; + let deque = this; + let count = deque._front; return { next: function () { - var done = count == _this._rear; - var value = !done ? _this[count] : undefined; - count = (count + 1) % _this._capacity; + var done = count == deque._rear; + var value = !done ? deque[count] : undefined; + count = (count + 1) % deque._capacity; return { done: done, value: value, diff --git a/container/linkedlist/js_linkedlist.ts b/container/linkedlist/js_linkedlist.ts index a5d27e6..fc78bea 100644 --- a/container/linkedlist/js_linkedlist.ts +++ b/container/linkedlist/js_linkedlist.ts @@ -36,12 +36,11 @@ if (flag || fastLinkedList == undefined) { return obj[prop]; } set(obj: LinkedList, prop: any, value: any) { - if ( - prop === "_length" || - prop === "_capacity" || - prop === "_head" || - prop == "next" || - prop == "_tail") { + if (prop === "_length" || + prop === "_capacity" || + prop === "_head" || + prop == "next" || + prop == "_tail" ) { obj[prop] = value; return true; } @@ -95,6 +94,9 @@ if (flag || fastLinkedList == undefined) { } return; } + setPrototypeOf(obj: any, prop: any): any { + throw new Error("Can setPrototype on LinkedList Object"); + } } interface IterableIterator { next: () => { diff --git a/container/list/js_list.ts b/container/list/js_list.ts index d6f9a24..192526a 100644 --- a/container/list/js_list.ts +++ b/container/list/js_list.ts @@ -88,6 +88,9 @@ if (flag || fastList == undefined) { } return; } + setPrototypeOf(obj: any, prop: any): any { + throw new Error("Can setPrototype on List Object"); + } } interface IterableIterator { next: () => { @@ -180,7 +183,7 @@ if (flag || fastList == undefined) { } return false; } - equals(obj: Object): boolean { + equal(obj: Object): boolean { if (obj === this) { return true; } @@ -327,6 +330,9 @@ if (flag || fastList == undefined) { } return arr; } + isEmpty(): boolean { + return this._length == 0; + } forEach(callbackfn: (value: T, index?: number, list?: List) => void, thisArg?: Object): void { let index = 0; diff --git a/container/queue/js_queue.ts b/container/queue/js_queue.ts index ab2759b..1d5c060 100644 --- a/container/queue/js_queue.ts +++ b/container/queue/js_queue.ts @@ -70,7 +70,9 @@ if (flag || fastQueue == undefined) { } return; } - + setPrototypeOf(obj: any, prop: any): any { + throw new Error("Can setPrototype on Queue Object"); + } } interface IterableIterator { next: () => { @@ -125,11 +127,12 @@ if (flag || fastQueue == undefined) { } [Symbol.iterator](): IterableIterator { let count = this._front; + let queue = this; return { next: function () { - var done = count == this._rear; - var value = !done ? this[count] : undefined; - count = (count + 1) % this._capacity; + var done = count == queue._rear; + var value = !done ? queue[count] : undefined; + count = (count + 1) % queue._capacity; return { done: done, value: value, diff --git a/container/stack/js_stack.ts b/container/stack/js_stack.ts index 87fa4b0..799537a 100644 --- a/container/stack/js_stack.ts +++ b/container/stack/js_stack.ts @@ -74,6 +74,9 @@ if (flag || fastStack == undefined) { } return; } + setPrototypeOf(obj: any, prop: any): any { + throw new Error("Can setPrototype on Stack Object"); + } } interface IterableIterator { next: () => { diff --git a/container/vector/js_vector.ts b/container/vector/js_vector.ts index a127633..a37fe7f 100644 --- a/container/vector/js_vector.ts +++ b/container/vector/js_vector.ts @@ -84,6 +84,9 @@ if (flag || fastVector == undefined) { } return; } + setPrototypeOf(obj: any, prop: any): any { + throw new Error("Can setPrototype on Vector Object"); + } } interface IterableIterator { next: () => { @@ -170,7 +173,7 @@ if (flag || fastVector == undefined) { } return -1; } - getLastIndexOfFrom(element: T, index: number): number { + getLastIndexFrom(element: T, index: number): number { if (this.has(element)) { for (let i = index; i >= 0; i--) { if (this[i] === element) { @@ -180,7 +183,7 @@ if (flag || fastVector == undefined) { } return -1; } - getIndexOfFrom(element: T, index: number): number { + getIndexFrom(element: T, index: number): number { if (this.has(element)) { for (let i = index; i < this._length; i++) { if (this[i] === element) { @@ -315,12 +318,15 @@ if (flag || fastVector == undefined) { this._capacity = newCapacity; } } - trimToCurrentSize(): void { + trimToCurrentLength(): void { this._capacity = this._length; } setSize(newSize: number): void { this._length = newSize; } + isEmpty(): boolean { + return this._length == 0; + } [Symbol.iterator](): IterableIterator { let count = 0; let vector = this