diff --git a/container/arraylist/js_arraylist.ts b/container/arraylist/js_arraylist.ts index bd7368e..e22465c 100644 --- a/container/arraylist/js_arraylist.ts +++ b/container/arraylist/js_arraylist.ts @@ -35,7 +35,7 @@ if (flag || fastArrayList == undefined) { return obj[prop]; } set(obj: ArrayList, prop: any, value: T) { - if (prop === "_length" || prop === "_capacity") { + if (prop === "elementNum" || prop === "capacity") { obj[prop] = value; return true; } @@ -95,33 +95,33 @@ if (flag || fastArrayList == undefined) { }; } class ArrayList { - private _length: number = 0; - private _capacity: number = 10; + private elementNum: number = 0; + private capacity: number = 10; constructor() { return new Proxy(this, new HandlerArrayList()); } get length() { - return this._length; + return this.elementNum; } add(element: T): boolean { if (this.isFull()) { this.resize(); } - this[this._length++] = element; + this[this.elementNum++] = element; return true; } insert(element: T, index: number): void { if (this.isFull()) { this.resize(); } - for (let i = this._length; i > index; i--) { + for (let i = this.elementNum; i > index; i--) { this[i] = this[i - 1]; } this[index] = element; - this._length++; + this.elementNum++; } has(element: T): boolean { - for (let i = 0; i < this._length; i++) { + for (let i = 0; i < this.elementNum; i++) { if (this[i] === element) { return true; } @@ -129,32 +129,37 @@ if (flag || fastArrayList == undefined) { return false; } getIndexOf(element: T): number { - for (let i = 0; i < this._length; i++) { + for (let i = 0; i < this.elementNum; i++) { if (element === this[i]) { return i; } } return -1; } - removeByIndex(index: number): void { - for (let i = index; i < this._length - 1; i++) { + removeByIndex(index: number): T { + if (index < 0 || index >= this.elementNum) { + throw new Error("removeByIndex is out-of-bounds"); + } + let result = this[index]; + for (let i = index; i < this.elementNum - 1; i++) { this[i] = this[i + 1]; } - this._length--; + this.elementNum--; + return result; } remove(element: T): boolean { if (this.has(element)) { let index = this.getIndexOf(element); - for (let i = index; i < this._length - 1; i++) { + for (let i = index; i < this.elementNum - 1; i++) { this[i] = this[i + 1]; } - this._length--; + this.elementNum--; return true; } return false; } getLastIndexOf(element: T): number { - for (let i = this._length - 1; i >= 0; i--) { + for (let i = this.elementNum - 1; i >= 0; i--) { if (element === this[i]) { return i; } @@ -165,31 +170,31 @@ if (flag || fastArrayList == undefined) { if (fromIndex >= toIndex) { throw new Error(`fromIndex cannot be less than or equal to toIndex`); } - toIndex = toIndex >= this._length - 1 ? this._length - 1 : toIndex; + toIndex = toIndex >= this.elementNum - 1 ? this.elementNum - 1 : toIndex; let i = fromIndex; - for (let j = toIndex; j < this._length; j++) { + for (let j = toIndex; j < this.elementNum; j++) { this[i] = this[j]; i++; } - this._length -= toIndex - fromIndex; + this.elementNum -= toIndex - fromIndex; } replaceAllElements(callbackfn: (value: T, index?: number, arraylist?: ArrayList) => T, thisArg?: Object): void { - for (let i = 0; i < this._length; i++) { + for (let i = 0; i < this.elementNum; i++) { this[i] = callbackfn.call(thisArg, this[i], i, this); } } forEach(callbackfn: (value: T, index?: number, arraylist?: ArrayList) => void, thisArg?: Object): void { - for (let i = 0; i < this._length; i++) { + for (let i = 0; i < this.elementNum; i++) { callbackfn.call(thisArg, this[i], i, this); } } sort(comparator?: (firstValue: T, secondValue: T) => number): void { let isSort = true; if (comparator) { - for (let i = 0; i < this._length; i++) { - for (let j = 0; j < this._length - 1 - i; j++) { + for (let i = 0; i < this.elementNum; i++) { + for (let j = 0; j < this.elementNum - 1 - i; j++) { if (comparator(this[j], this[j + 1]) > 0) { isSort = false; let temp = this[j]; @@ -200,7 +205,7 @@ if (flag || fastArrayList == undefined) { } } else { for (var i = 0; i < this.length - 1; i++) { - for (let j = 0; j < this._length - 1 - i; j++) { + for (let j = 0; j < this.elementNum - 1 - i; j++) { if (this.asciSort(this[j], this[j + 1])) { isSort = false; let temp = this[j]; @@ -232,7 +237,10 @@ if (flag || fastArrayList == undefined) { if (fromIndex >= toIndex) { throw new Error(`fromIndex cannot be less than or equal to toIndex`); } - toIndex = toIndex >= this._length - 1 ? this._length - 1 : toIndex; + if (fromIndex >= this.elementNum || fromIndex < 0 || toIndex < 0) { + throw new Error(`fromIndex or toIndex is out-of-bounds`); + } + toIndex = toIndex > this.elementNum ? this.elementNum - 1 : toIndex; let arraylist = new ArrayList(); for (let i = fromIndex; i < toIndex; i++) { arraylist.add(this[i]); @@ -240,48 +248,48 @@ if (flag || fastArrayList == undefined) { return arraylist; } clear(): void { - this._length = 0; + this.elementNum = 0; } clone(): ArrayList { let clone = new ArrayList(); - for (let i = 0; i < this._length; i++) { + for (let i = 0; i < this.elementNum; i++) { clone.add(this[i]); } return clone; } getCapacity(): number { - return this._capacity; + return this.capacity; } convertToArray(): Array { let arr = []; - for (let i = 0; i < this._length; i++) { + for (let i = 0; i < this.elementNum; i++) { arr[i] = this[i]; } return arr; } private isFull(): boolean { - return this._length === this._capacity; + return this.elementNum === this.capacity; } private resize(): void { - this._capacity = 1.5 * this._capacity; + this.capacity = 1.5 * this.capacity; } isEmpty(): boolean { - return this._length == 0; + return this.elementNum == 0; } increaseCapacityTo(newCapacity: number): void { - if (newCapacity >= this._length) { - this._capacity = newCapacity; + if (newCapacity >= this.elementNum) { + this.capacity = newCapacity; } } trimToCurrentLength(): void { - this._capacity = this._length; + this.capacity = this.elementNum; } [Symbol.iterator](): IterableIterator { let count = 0; let arraylist = this; return { next: function () { - var done = count >= arraylist._length; + var done = count >= arraylist.elementNum; var value = !done ? arraylist[count++] : undefined; return { done: done, diff --git a/container/deque/js_deque.ts b/container/deque/js_deque.ts index f644914..7d75fc2 100644 --- a/container/deque/js_deque.ts +++ b/container/deque/js_deque.ts @@ -35,7 +35,7 @@ if (flag || fastDeque == undefined) { return obj[prop]; } set(obj: Deque, prop: any, value: T): boolean { - if (prop === "_front" || prop === "_capacity" || prop === "_rear") { + if (prop === "front" || prop === "capacity" || prop === "rear") { obj[prop] = value; return true; } @@ -84,38 +84,38 @@ if (flag || fastDeque == undefined) { }; } class Deque { - private _front: number; - private _capacity: number; - private _rear: number; + private front: number; + private capacity: number; + private rear: number; constructor() { - this._front = 0; - this._capacity = 8; - this._rear = 0; + this.front = 0; + this.capacity = 8; + this.rear = 0; return new Proxy(this, new HandlerDeque()); } get length(){ - let result = (this._rear - this._front + this._capacity) % this._capacity; + let result = (this.rear - this.front + this.capacity) % this.capacity; return result; } insertFront(element: T): void { if (this.isFull()) { this.increaseCapacity(); } - this._front = (this._front - 1 + this._capacity) % this._capacity; - this[this._front] = element; + this.front = (this.front - 1 + this.capacity) % this.capacity; + this[this.front] = element; } insertEnd(element: T): void { if (this.isFull()) { this.increaseCapacity(); } - this[this._rear] = element; - this._rear = (this._rear + 1) % (this._capacity + 1); + this[this.rear] = element; + this.rear = (this.rear + 1) % (this.capacity + 1); } getFirst(): T { - return this[this._front]; + return this[this.front]; } getLast(): T { - return this[this._rear - 1]; + return this[this.rear - 1]; } has(element: T): boolean { let result = false; @@ -127,24 +127,24 @@ if (flag || fastDeque == undefined) { return result; } popFirst(): T { - let result = this[this._front]; - this._front = (this._front + 1) % (this._capacity + 1); + let result = this[this.front]; + this.front = (this.front + 1) % (this.capacity + 1); return result; } popLast(): T { - let result = this[this._rear - 1]; - this._rear = (this._rear + this._capacity) % (this._capacity + 1); + let result = this[this.rear - 1]; + this.rear = (this.rear + this.capacity) % (this.capacity + 1); return result; } forEach(callbackfn: (value: T, index?: number, deque?: Deque) => void, thisArg?: Object): void { let k = 0; - let i = this._front; + let i = this.front; while (true) { callbackfn.call(thisArg, this[i], k, this); - i = (i + 1) % this._capacity; + i = (i + 1) % this.capacity; k++; - if (i === this._rear) { + if (i === this.rear) { break; } } @@ -154,30 +154,30 @@ if (flag || fastDeque == undefined) { let arr = []; let length = this.length; while (true) { - arr[count++] = this[this._front]; - this._front = (this._front + 1) % this._capacity; - if (this._front === this._rear) { + arr[count++] = this[this.front]; + this.front = (this.front + 1) % this.capacity; + if (this.front === this.rear) { break; } } for (let i = 0; i < length; i++) { this[i] = arr[i]; } - this._capacity = 2 * this._capacity; - this._front = 0; - this._rear = length; + this.capacity = 2 * this.capacity; + this.front = 0; + this.rear = length; } private isFull(): boolean { - return (this._rear + 1) % this._capacity === this._front; + return (this.rear + 1) % this.capacity === this.front; } [Symbol.iterator](): IterableIterator { let deque = this; - let count = deque._front; + let count = deque.front; return { next: function () { - var done = count == deque._rear; + var done = count == deque.rear; var value = !done ? deque[count] : undefined; - count = (count + 1) % deque._capacity; + count = (count + 1) % deque.capacity; return { done: done, value: value, diff --git a/container/hashmap/js_hashmap.ts b/container/hashmap/js_hashmap.ts index 4e2b84c..309878d 100644 --- a/container/hashmap/js_hashmap.ts +++ b/container/hashmap/js_hashmap.ts @@ -67,15 +67,12 @@ if (flag || fastHashMap === undefined) { return super.Values().indexOf(value) > -1; } get(key: K): V { - let result = super.getValueByKey(key); - if (result === undefined) - throw new Error("this hashmap don't find the key"); - return result; + return this.getValueByKey(key); } setAll(map: HashMap): void { - let memebers = this.keyValueArray; + let memebers = map.keyValueArray; for (let i = 0; i < memebers.length; i++) { - map.put(memebers[i].key, memebers[i].value); + this.put(memebers[i].key, memebers[i].value); } } set(key: K, value: V): Object { @@ -83,7 +80,7 @@ if (flag || fastHashMap === undefined) { } remove(key: K): V { let result = this.removeMember(key); - if (result === null) { + if (result === undefined) { throw new Error("The removed element does not exist in this container"); } return result; @@ -97,7 +94,8 @@ if (flag || fastHashMap === undefined) { return { next: function () { var done = count >= data.memberNumber; - var value = !done ? data.keyValueArray[count++].key : undefined; + var value = !done ? data.keyValueArray[count].key : undefined; + count++; return { done: done, value: value, @@ -111,7 +109,8 @@ if (flag || fastHashMap === undefined) { return { next: function () { var done = count >= data.memberNumber; - var value = !done ? data.keyValueArray[count++].value : undefined; + var value = !done ? data.keyValueArray[count].value : undefined; + count++; return { done: done, value: value, @@ -135,7 +134,8 @@ if (flag || fastHashMap === undefined) { return { next: function () { var done = count >= data.memberNumber; - var value = !done ? data.keyValueArray[count++].entry() : undefined; + var value = !done ? data.keyValueArray[count].entry() : undefined; + count++; return { done: done, value: value, @@ -149,7 +149,8 @@ if (flag || fastHashMap === undefined) { return { next: function () { var done = count >= data.memberNumber; - var value = !done ? data.keyValueArray[count++].entry() : undefined; + var value = !done ? data.keyValueArray[count].entry() : undefined; + count++; return { done: done, value: value, diff --git a/container/hashset/js_hashset.ts b/container/hashset/js_hashset.ts index 4438a65..a7def21 100644 --- a/container/hashset/js_hashset.ts +++ b/container/hashset/js_hashset.ts @@ -62,14 +62,14 @@ if (flag || fastHashSet === undefined) { return this.memberNumber === 0; } has(value: T): boolean { - return super.hasKey(value); + return this.hasKey(value); } add(value: T): boolean { if (this.has(value)) return false; return this.put(value); } remove(value: T): boolean { - if (this.removeMember(value) !== null) return true; + if (this.removeMember(value) !== undefined) return true; return false; } clear(): void { @@ -88,7 +88,8 @@ if (flag || fastHashSet === undefined) { return { next: function () { var done = count >= data.memberNumber; - var value = !done ? data.keyValueArray[count++].key : undefined; + var value = !done ? data.keyValueArray[count].key : undefined; + count++; return { done: done, value: value, @@ -102,7 +103,8 @@ if (flag || fastHashSet === undefined) { return { next: function () { var done = count >= data.memberNumber; - var value = !done ? data.keyValueArray[count++].entry() : undefined; + var value = !done ? data.keyValueArray[count].entry() : undefined; + count++; return { done: done, value: value, @@ -116,7 +118,8 @@ if (flag || fastHashSet === undefined) { return { next: function () { var done = count >= data.memberNumber; - var value = !done ? data.keyValueArray[count++].key : undefined; + var value = !done ? data.keyValueArray[count].key : undefined; + count++; return { done: done, value: value, diff --git a/container/lightweightmap/js_lightweightmap.ts b/container/lightweightmap/js_lightweightmap.ts index 7c429b5..e9e2e34 100644 --- a/container/lightweightmap/js_lightweightmap.ts +++ b/container/lightweightmap/js_lightweightmap.ts @@ -59,8 +59,7 @@ if (flag || fastLightWeightMap === undefined) { } hasAll(map: LightWeightMap): boolean { if (map.memberNumber > this.memberNumber) return false; - if (LightWeightAbility.isIncludeToArray(this.members.keys, map.members.keys) && - LightWeightAbility.isIncludeToArray(this.members.values, map.members.values)) { + if (LightWeightAbility.isIncludeToArray(this.keyValueStringArray(), map.keyValueStringArray()) ) { return true; } return false; @@ -121,14 +120,14 @@ if (flag || fastLightWeightMap === undefined) { }; } setAll(map: LightWeightMap): void { - if (map.memberNumber === 0) { - map.members.hashs = this.members.hashs.slice(); - map.members.keys = this.members.keys.slice(); - map.members.values = this.members.values.slice(); - map.memberNumber = this.memberNumber; + if (this.memberNumber === 0) { + this.members.hashs = map.members.hashs.slice(); + this.members.keys = map.members.keys.slice(); + this.members.values = map.members.values.slice(); + this.memberNumber = map.memberNumber; } else { - for (let i = 0; i < this.memberNumber; i++) { - map.addmember(this.members.keys[i], this.members.values[i]); + for (let i = 0; i < map.memberNumber; i++) { + this.addmember(map.members.keys[i], map.members.values[i]); } } } @@ -191,7 +190,7 @@ if (flag || fastLightWeightMap === undefined) { return result.join(","); } getValueAt(index: number): V { - return this.members.values[index] as V; + return this.members.values[index]; } values(): IterableIterator { let data = this; diff --git a/container/lightweightset/js_lightweightset.ts b/container/lightweightset/js_lightweightset.ts index d4cbe78..7e52388 100644 --- a/container/lightweightset/js_lightweightset.ts +++ b/container/lightweightset/js_lightweightset.ts @@ -65,17 +65,12 @@ if (flag || fastLightWeightSet === undefined) { addAll(set: LightWeightSet): boolean { let change = false; if (set.memberNumber == 0) { - set.memberNumber = this.memberNumber; - set.members.hashs = this.members.hashs.slice(); - set.members.keys = this.members.keys.slice(); - set.members.values = this.members.values.slice(); - set.memberNumber = this.memberNumber; - change = true; + change = false; } else { - for (let i = 0; i < this.memberNumber; i++) { - change = set.add(this.members.keys[i]) || change; + for (let i = 0; i < set.memberNumber; i++) { + change = this.add(set.members.keys[i]) || change; } - } + } return change; } hasAll(set: LightWeightSet): boolean { @@ -90,7 +85,7 @@ if (flag || fastLightWeightSet === undefined) { } equal(obj: Object): boolean { if (this.memberNumber === 0) return false; - if (obj.toString() === this.members.keys.toString()) return true; + if (JSON.stringify(obj) === JSON.stringify(this.members.keys)) return true; return false; } increaseCapacityTo(minimumCapacity: number): void { @@ -136,6 +131,7 @@ if (flag || fastLightWeightSet === undefined) { next: function () { var done = count >= data.memberNumber; var value = !done ? data.members.keys[count] : undefined; + count++; return { done: done, value: value, diff --git a/container/linkedlist/js_linkedlist.ts b/container/linkedlist/js_linkedlist.ts index fc78bea..a4bba10 100644 --- a/container/linkedlist/js_linkedlist.ts +++ b/container/linkedlist/js_linkedlist.ts @@ -32,26 +32,26 @@ if (flag || fastLinkedList == undefined) { if (index < 0 || index >= length) { throw new Error("LinkedList: get out-of-bounds"); } + return obj.get(index); } return obj[prop]; } set(obj: LinkedList, prop: any, value: any) { - if (prop === "_length" || - prop === "_capacity" || - prop === "_head" || + if (prop === "elementNum" || + prop === "capacity" || + prop === "head" || prop == "next" || - prop == "_tail" ) { + prop == "tail" ) { obj[prop] = value; return true; } - var index = Number.parseInt(prop); if (Number.isInteger(index)) { let length = obj.length; if (index < 0 || index >= length) { throw new Error("LinkedList: set out-of-bounds"); } else { - obj[index] = value; + obj.set(index, value); return true; } } @@ -144,58 +144,58 @@ if (flag || fastLinkedList == undefined) { } } class LinkedList { - private _head?: any; - private _tail?: any; - private _length : number; - private _capacity: number; - constructor(_head?: NodeObj, _tail?: NodeObj) { - this._head = _head || null; - this._tail = _tail || null; - this._length = 0; - this._capacity = 10; + private head?: any; + private tail?: any; + private elementNum : number; + private capacity: number; + constructor(head?: NodeObj, tail?: NodeObj) { + this.head = head || null; + this.tail = tail || null; + this.elementNum = 0; + this.capacity = 10; return new Proxy(this, new HandlerLinkedList()); } get length() { - return this._length; + return this.elementNum; } private getNode(index: number): NodeObj { - let current = this._head; + let current = this.head; for (let i = 0; i < index; i++) { current = current["next"]; } return current; } get(index: number): T { - let current = this._head; + let current = this.head; for (let i = 0; i < index; i++) { current = current["next"]; } return current.element; } add(element: T): boolean { - if (this._length === 0) { - let head = this._head; - let tail = this._tail; - this._head = this._tail = new NodeObj(element, head, tail); + if (this.elementNum === 0) { + let head = this.head; + let tail = this.tail; + this.head = this.tail = new NodeObj(element, head, tail); } else { - let prevNode = this.getNode(this._length - 1); - prevNode.next = new NodeObj(element, prevNode["next"], this._tail); + let prevNode = this.getNode(this.elementNum - 1); + prevNode.next = new NodeObj(element, prevNode["next"], this.tail); } - this._length++; + this.elementNum++; return true; } addFirst(element: T): void { if (!element) { throw new Error("element cannot be null"); } - let node = new NodeObj(element, this._head, this._tail); - if (this._length === 0) { - this._head = this._tail = node; + let node = new NodeObj(element, this.head, this.tail); + if (this.elementNum === 0) { + this.head = this.tail = node; } else { - node.next = this._head; - this._head = node; + node.next = this.head; + this.head = node; } - this._length++; + this.elementNum++; } removeFirst(): T { let result = this.getNode(0).element; @@ -208,25 +208,25 @@ if (flag || fastLinkedList == undefined) { return result; } popLast(): T { - let result = this.getNode(this._length - 1).element; - this.removeByIndex(this._length - 1); + let result = this.getNode(this.elementNum - 1).element; + this.removeByIndex(this.elementNum - 1); return result; } removeLast(): T { - let result = this.getNode(this._length - 1).element; - this.removeByIndex(this._length - 1); + let result = this.getNode(this.elementNum - 1).element; + this.removeByIndex(this.elementNum - 1); return result; } clear(): void { - this._head = null; - this._tail = null; - this._length = 0; + this.head = null; + this.tail = null; + this.elementNum = 0; } has(element: T): boolean { - if (this._head.element === element) { + if (this.head.element === element) { return true; } - const linkIterator = new LinkIterator(this._head); + const linkIterator = new LinkIterator(this.head); while (linkIterator.hasNext()) { const newNode = linkIterator.next(); if (newNode.element === element) { @@ -236,7 +236,7 @@ if (flag || fastLinkedList == undefined) { return false; } getIndexOf(element: T): number { - for (let i = 0; i < this._length; i++) { + for (let i = 0; i < this.elementNum; i++) { const curNode = this.getNode(i); if (curNode.element === element) { return i; @@ -245,7 +245,7 @@ if (flag || fastLinkedList == undefined) { return -1; } getLastIndexOf(element: T): number { - for (let i = this._length - 1; i >= 0; i--) { + for (let i = this.elementNum - 1; i >= 0; i--) { const curNode = this.getNode(i); if (curNode.element === element) { return i; @@ -254,17 +254,20 @@ if (flag || fastLinkedList == undefined) { return -1; } removeByIndex(index: number): T { - let current = this._head; + if (index < 0 || index >= this.elementNum) { + throw new Error("removeByIndex is out-of-bounds"); + } + let current = this.head; if (index === 0) { - this._head = current && current.next; - if (this._length == 1) { - this._head = this._tail = null; + this.head = current && current.next; + if (this.elementNum == 1) { + this.head = this.tail = null; } else { - this._head.prev = null; + this.head.prev = null; } - } else if (index == this._length - 1) { + } else if (index == this.elementNum - 1) { current = this.getNode(index - 1); - this._tail = current; + this.tail = current; current.next = null; } else { const prevNode = this.getNode(index - 1); @@ -272,10 +275,13 @@ if (flag || fastLinkedList == undefined) { prevNode.next = nextNode; nextNode.prev = prevNode; } - this._length--; + this.elementNum--; return current && current.element; } remove(element: T): boolean { + if(this.isEmpty()){ + return false; + } if (this.has(element)) { let index = this.getIndexOf(element); this.removeByIndex(index); @@ -307,22 +313,22 @@ if (flag || fastLinkedList == undefined) { return element; } getLast(): T { - let newNode = this.getNode(this._length - 1); + let newNode = this.getNode(this.elementNum - 1); let element = newNode.element; return element; } insert(index: number, element: T): void { - if (index >= 0 && index < this._length) { + if (index >= 0 && index < this.elementNum) { let newNode = new NodeObj(element); - let current = this._head; + let current = this.head; if (index === 0) { - if (this._head === null) { - this._head = newNode; - this._tail = newNode; + if (this.head === null) { + this.head = newNode; + this.tail = newNode; } else { - newNode.next = this._head; - this._head.prev = newNode; - this._head = newNode; + newNode.next = this.head; + this.head.prev = newNode; + this.head = newNode; } } else { const prevNode = this.getNode(index - 1); @@ -332,13 +338,13 @@ if (flag || fastLinkedList == undefined) { current.prev = newNode; newNode.prev = prevNode; } - } else if (index === this._length) { - let prevNode = this.getNode(this._length - 1); - prevNode.next = new NodeObj(element, prevNode["next"], this._tail); + } else if (index === this.elementNum) { + let prevNode = this.getNode(this.elementNum - 1); + prevNode.next = new NodeObj(element, prevNode["next"], this.tail); } else { throw new Error("index cannot Less than 0 and more than this length"); } - this._length++; + this.elementNum++; } set(index: number, element: T): T { const current = this.getNode(index); @@ -348,11 +354,11 @@ if (flag || fastLinkedList == undefined) { convertToArray(): Array { let arr: Array = []; let index = 0; - if (this._length <= 0) { + if (this.elementNum <= 0) { return arr; } - arr[index] = this._head.element; - const linkIterator = new LinkIterator(this._head); + arr[index] = this.head.element; + const linkIterator = new LinkIterator(this.head); while (linkIterator.hasNext()) { const newNode = linkIterator.next(); arr[++index] = newNode.element; @@ -368,12 +374,15 @@ if (flag || fastLinkedList == undefined) { } return clone; } + private isEmpty(): boolean { + return this.elementNum == 0; + } forEach(callbackfn: (value: T,index?: number,linkedlist?: LinkedList) => void, thisArg?: Object): void { let index = 0; - const linkIterator = new LinkIterator(this._head); - if (this._length > 0) { - callbackfn.call(thisArg, this._head.element, index, this); + const linkIterator = new LinkIterator(this.head); + if (this.elementNum > 0) { + callbackfn.call(thisArg, this.head.element, index, this); } while (linkIterator.hasNext()) { const newNode = linkIterator.next(); @@ -382,10 +391,10 @@ if (flag || fastLinkedList == undefined) { } [Symbol.iterator](): IterableIterator { let count = 0; - let linkedlist = this + let linkedlist = this; return { next: function () { - var done = count >= linkedlist._length; + var done = count >= linkedlist.elementNum; var value = !done ? linkedlist.getNode(count++).element : undefined; return { done: done, diff --git a/container/list/js_list.ts b/container/list/js_list.ts index 192526a..00b235a 100644 --- a/container/list/js_list.ts +++ b/container/list/js_list.ts @@ -31,14 +31,15 @@ if (flag || fastList == undefined) { if (index < 0 || index >= obj.length) { throw new Error("List: get out-of-bounds"); } + return obj.get(index); } return obj[prop]; } set(obj: List, prop: any, value: T) { if ( - prop === "_length" || - prop === "_capacity" || - prop === "_head" || + prop === "elementNum" || + prop === "capacity" || + prop === "head" || prop == "next") { obj[prop] = value; return true; @@ -48,7 +49,7 @@ if (flag || fastList == undefined) { if (index < 0 || index >= obj.length) { throw new Error("List: set out-of-bounds"); } else { - obj[index] = value; + obj.set(index, value); return true; } } @@ -129,52 +130,52 @@ if (flag || fastList == undefined) { class List { /* If 'any' is changed to 'NodeObj | null ' here, an error will be reported: Object may be 'null' */ - private _head: any; - private _length: number; - private _capacity: number; - constructor(_head?: NodeObj) { - this._head = _head || null; - this._length = 0; - this._capacity = 10; + private head: any; + private elementNum: number; + private capacity: number; + constructor(head?: NodeObj) { + this.head = head || null; + this.elementNum = 0; + this.capacity = 10; return new Proxy(this, new HandlerList()); } get length() { - return this._length; + return this.elementNum; } private getNode(index: number): NodeObj { - let current = this._head; + let current = this.head; for (let i = 0; i < index; i++) { current = current["next"]; } return current; } get(index: number): T { - let current = this._head; + let current = this.head; for (let i = 0; i < index; i++) { current = current["next"]; } return current.element; } add(element: T): boolean { - if (this._length === 0) { - let head = this._head; - this._head = new NodeObj(element, head); + if (this.elementNum === 0) { + let head = this.head; + this.head = new NodeObj(element, head); } else { - let prevNode = this.getNode(this._length - 1); + let prevNode = this.getNode(this.elementNum - 1); prevNode.next = new NodeObj(element, prevNode["next"]); } - this._length++; + this.elementNum++; return true; } clear(): void { - this._head = null; - this._length = 0; + this.head = null; + this.elementNum = 0; } has(element: T): boolean { - if (this._head.element === element) { + if (this.head.element === element) { return true; } - const linkIterator = new LinkIterator(this._head); + const linkIterator = new LinkIterator(this.head); while (linkIterator.hasNext()) { const newNode = linkIterator.next(); if (newNode.element === element) { @@ -190,8 +191,8 @@ if (flag || fastList == undefined) { if (!(obj instanceof List)) { return false; } else { - let e1 = new LinkIterator(this._head); - let e2 = new LinkIterator(obj._head); + let e1 = new LinkIterator(this.head); + let e2 = new LinkIterator(obj.head); while (e1.hasNext() && e2.hasNext()) { const newNode1 = e1.next(); const newNode2 = e2.next(); @@ -204,7 +205,7 @@ if (flag || fastList == undefined) { return true; } getIndexOf(element: T): number { - for (let i = 0; i < this._length; i++) { + for (let i = 0; i < this.elementNum; i++) { const curNode = this.getNode(i); if (curNode.element === element) { return i; @@ -213,7 +214,7 @@ if (flag || fastList == undefined) { return -1; } getLastIndexOf(element: T): number { - for (let i = this._length - 1; i >= 0; i--) { + for (let i = this.elementNum - 1; i >= 0; i--) { const curNode = this.getNode(i); if (curNode.element === element) { return i; @@ -222,16 +223,19 @@ if (flag || fastList == undefined) { return -1; } removeByIndex(index: number): T { - let oldNode = this._head; + if (index < 0 || index >= this.elementNum) { + throw new Error("removeByIndex is out-of-bounds"); + } + let oldNode = this.head; if (index === 0) { - oldNode = this._head; - this._head = oldNode && oldNode.next; + oldNode = this.head; + this.head = oldNode && oldNode.next; } else { let prevNode = this.getNode(index - 1); oldNode = prevNode.next; prevNode.next = oldNode.next; } - this._length--; + this.elementNum--; return oldNode && oldNode.element; } remove(element: T): boolean { @@ -245,10 +249,10 @@ if (flag || fastList == undefined) { replaceAllElements(callbackfn: (value: T, index?: number, list?: List) => T, thisArg?: Object): void { let index = 0; - const linkIterator = new LinkIterator(this._head); - if (this._length > 0) { - const linkIterator = new LinkIterator(this._head); - this.getNode(index).element = callbackfn.call(thisArg, this._head.element, index, this); + const linkIterator = new LinkIterator(this.head); + if (this.elementNum > 0) { + const linkIterator = new LinkIterator(this.head); + this.getNode(index).element = callbackfn.call(thisArg, this.head.element, index, this); } while (linkIterator.hasNext()) { const newNode = linkIterator.next(); @@ -261,23 +265,23 @@ if (flag || fastList == undefined) { return element; } getLast(): T { - let newNode = this.getNode(this._length - 1); + let newNode = this.getNode(this.elementNum - 1); let element = newNode.element; return element; } insert(element: T, index: number): void { let newNode = new NodeObj(element); - if (index >= 0 && index < this._length) { + if (index >= 0 && index < this.elementNum) { if (index === 0) { - const current = this._head; + const current = this.head; newNode.next = current; - this._head = newNode; + this.head = newNode; } else { const prevNode = this.getNode(index - 1); newNode.next = prevNode.next; prevNode.next = newNode; } - this._length++; + this.elementNum++; } } set(index: number, element: T): T { @@ -287,8 +291,8 @@ if (flag || fastList == undefined) { } sort(comparator: (firstValue: T, secondValue: T) => number): void { let isSort = true; - for (let i = 0; i < this._length; i++) { - for (let j = 0; j < this._length - 1 - i; j++) { + for (let i = 0; i < this.elementNum; i++) { + for (let j = 0; j < this.elementNum - 1 - i; j++) { if (comparator(this.getNode(j).element, this.getNode(j + 1).element) > 0) { isSort = false; let temp = this.getNode(j).element; @@ -305,7 +309,10 @@ if (flag || fastList == undefined) { if (toIndex <= fromIndex) { throw new Error("toIndex cannot be less than or equal to fromIndex"); } - toIndex = toIndex > this._length ? this._length - 1 : toIndex; + if (fromIndex >= this.elementNum || fromIndex < 0 || toIndex < 0) { + throw new Error(`fromIndex or toIndex is out-of-bounds`); + } + toIndex = toIndex > this.elementNum ? this.elementNum - 1 : toIndex; let list = new List(); for (let i = fromIndex; i < toIndex; i++) { let element = this.getNode(i).element; @@ -319,11 +326,11 @@ if (flag || fastList == undefined) { convertToArray(): Array { let arr: Array = []; let index = 0; - if (this._length <= 0) { + if (this.elementNum <= 0) { return arr; } - arr[index] = this._head.element; - const linkIterator = new LinkIterator(this._head); + arr[index] = this.head.element; + const linkIterator = new LinkIterator(this.head); while (linkIterator.hasNext()) { const newNode = linkIterator.next(); arr[++index] = newNode.element; @@ -331,15 +338,15 @@ if (flag || fastList == undefined) { return arr; } isEmpty(): boolean { - return this._length == 0; + return this.elementNum == 0; } forEach(callbackfn: (value: T, index?: number, list?: List) => void, thisArg?: Object): void { let index = 0; - const linkIterator = new LinkIterator(this._head); - if (this._length > 0) { - const linkIterator = new LinkIterator(this._head); - callbackfn.call(thisArg, this._head.element, index, this); + const linkIterator = new LinkIterator(this.head); + if (this.elementNum > 0) { + const linkIterator = new LinkIterator(this.head); + callbackfn.call(thisArg, this.head.element, index, this); } while (linkIterator.hasNext()) { const newNode = linkIterator.next(); @@ -348,10 +355,10 @@ if (flag || fastList == undefined) { } [Symbol.iterator](): IterableIterator { let count = 0; - let list = this + let list = this; return { next: function () { - var done = count >= list._length; + var done = count >= list.elementNum; var value = !done ? list.getNode(count++).element : undefined; return { done: done, diff --git a/container/plainarray/js_plainarray.ts b/container/plainarray/js_plainarray.ts index ff3a5dd..f1855af 100644 --- a/container/plainarray/js_plainarray.ts +++ b/container/plainarray/js_plainarray.ts @@ -82,7 +82,6 @@ if (flag || fastPlainArray === undefined) { } get(key: number): T { let index = this.binarySearchAtPlain(key); - if (index < 0) throw new Error("Key error found"); return this.members.values[index]; } getIndexOfKey(key: number): number { @@ -100,7 +99,7 @@ if (flag || fastPlainArray === undefined) { } remove(key: number): T { let index = this.binarySearchAtPlain(key); - if (index < 0) throw new Error(" element not in this plainarray"); + if (index < 0) throw new Error("element not in this plainarray"); return this.deletemember(index); } removeAt(index: number): T { @@ -109,7 +108,7 @@ if (flag || fastPlainArray === undefined) { } removeRangeFrom(index: number, size: number): number { if (index >= this.memberNumber || index < 0) throw new Error("index not in this plainarray range"); - let safeSize = (this.memberNumber - (index + size) < 0) ? this.memberNumber - size : size; + let safeSize = (this.memberNumber - (index + size) < 0) ? this.memberNumber - index : size; this.deletemember(index, safeSize); return safeSize; } diff --git a/container/queue/js_queue.ts b/container/queue/js_queue.ts index 1d5c060..71176b7 100644 --- a/container/queue/js_queue.ts +++ b/container/queue/js_queue.ts @@ -35,7 +35,7 @@ if (flag || fastQueue == undefined) { return obj[prop]; } set(obj: Queue, prop: any, value: T): boolean { - if (prop === "_front" || prop === "_capacity" || prop === "_rear") { + if (prop === "front" || prop === "capacity" || prop === "rear") { obj[prop] = value; return true; } @@ -81,58 +81,58 @@ if (flag || fastQueue == undefined) { }; } class Queue { - private _front: number; - private _capacity: number; - private _rear: number; + private front: number; + private capacity: number; + private rear: number; constructor() { - this._front = 0; - this._capacity = 8; - this._rear = 0; + this.front = 0; + this.capacity = 8; + this.rear = 0; return new Proxy(this, new HandlerQueue()); } get length(){ - return this._rear - this._front; + return this.rear - this.front; } add(element: T): boolean { if (this.isFull()) { this.increaseCapacity(); } - this[this._rear] = element; - this._rear = (this._rear + 1) % (this._capacity + 1); + this[this.rear] = element; + this.rear = (this.rear + 1) % (this.capacity + 1); return true; } getFirst(): T { - return this[this._front]; + return this[this.front]; } pop(): T { - let result = this[this._front]; - this._front = (this._front + 1) % (this._capacity + 1); + let result = this[this.front]; + this.front = (this.front + 1) % (this.capacity + 1); return result; } forEach(callbackfn: (value: T, index?: number, queue?: Queue) => void, thisArg?: Object): void { let k = 0; - let i = this._front; + let i = this.front; while (true) { callbackfn.call(thisArg,this[i], k,this); - i = (i + 1) % this._capacity; + i = (i + 1) % this.capacity; k++; - if (i === this._rear) { + if (i === this.rear) { break; } } } private isFull(): boolean { - return this.length === this._capacity; + return this.length === this.capacity; } [Symbol.iterator](): IterableIterator { - let count = this._front; + let count = this.front; let queue = this; return { next: function () { - var done = count == queue._rear; + var done = count == queue.rear; var value = !done ? queue[count] : undefined; - count = (count + 1) % queue._capacity; + count = (count + 1) % queue.capacity; return { done: done, value: value, @@ -141,7 +141,7 @@ if (flag || fastQueue == undefined) { }; } private increaseCapacity(): void { - this._capacity = 2 * this._capacity; + this.capacity = 2 * this.capacity; } } Object.freeze(Queue); diff --git a/container/stack/js_stack.ts b/container/stack/js_stack.ts index 799537a..3f27e5e 100644 --- a/container/stack/js_stack.ts +++ b/container/stack/js_stack.ts @@ -36,7 +36,7 @@ if (flag || fastStack == undefined) { return obj[prop]; } set(obj: Stack, prop: any, value: T) { - if (prop === "_length" || prop === "_capacity") { + if (prop === "elementNum" || prop === "capacity") { obj[prop] = value; return true; } @@ -85,24 +85,24 @@ if (flag || fastStack == undefined) { }; } class Stack { - private _length: number = 0; - private _capacity: number = 10; + private elementNum: number = 0; + private capacity: number = 10; constructor() { return new Proxy(this, new HandlerStack()); } get length() { - return this._length; + return this.elementNum; } push(item: T): T { if (this.isFull()) { this.increaseCapacity(); } - this[this._length++] = item; + this[this.elementNum++] = item; return item; } pop(): T { let result = this[this.length - 1]; - this._length--; + this.elementNum--; return result; } peek(): T { @@ -117,7 +117,7 @@ if (flag || fastStack == undefined) { return -1; } isEmpty(): boolean { - return this._length == 0; + return this.elementNum == 0; } forEach(callbackfn: (value: T, index?: number, stack?: Stack) => void, thisArg?: Object): void { @@ -126,17 +126,17 @@ if (flag || fastStack == undefined) { } } private isFull(): boolean { - return this._length === this._capacity; + return this.elementNum === this.capacity; } private increaseCapacity(): void { - this._capacity = 1.5 * this._capacity; + this.capacity = 1.5 * this.capacity; } [Symbol.iterator](): IterableIterator { let count = 0; let stack = this; return { next: function () { - var done = count >= stack._length; + var done = count >= stack.elementNum; var value = !done ? stack[count++] : undefined; return { done: done, diff --git a/container/struct/js_struct.ts b/container/struct/js_struct.ts index 3a7cd15..3f4e34d 100644 --- a/container/struct/js_struct.ts +++ b/container/struct/js_struct.ts @@ -58,6 +58,9 @@ function compareToString(string1: String, string2: String) { function currencyCompare(a: any, b: any, compareFn?: Function): number { if (a === b) return ComparResult.EQUALS; + if (a instanceof Pair && b instanceof Pair) { + return currencyCompare(a.key, b.key, compareFn); + } if (compareFn != undefined) { return compareFn(a, b) ? ComparResult.BIGGER_THAN : ComparResult.LESS_THAN; } @@ -73,8 +76,6 @@ function currencyCompare(a: any, b: any, compareFn?: Function): number { return ComparResult.BIGGER_THAN; } else if (typeof a === "number" && typeof b === "string") { return ComparResult.LESS_THAN; - } else if (a instanceof Pair && b instanceof Pair) { - return currencyCompare(a.key, b.key); } throw new Error("This form of comparison is not supported"); } @@ -122,7 +123,7 @@ class PlainArrayClass { protected addmember(key: number, value: T) { let index = this.binarySearchAtPlain(key); - if (index > 0) { + if (index >= 0) { this.members.keys[index] = key; this.members.values[index] = value; } else { @@ -181,7 +182,7 @@ class LightWeightClass { protected addmember(key: K, value: V = key as unknown as V) { let hash = hashCode(key); let index = this.binarySearchAtLightWeight(hash); - if (index > 0) { + if (index >= 0) { this.members.keys[index] = key; this.members.values[index] = value; } else { @@ -225,10 +226,10 @@ class LightWeightClass { return index; } - protected keyArray(): Array { - let resultArray: Array = []; + protected keyValueStringArray(): Array { + let resultArray: Array = []; for (let i = 0; i < this.memberNumber; i++) { - resultArray.push(this.members[i].key); + resultArray.push(JSON.stringify(this.members.keys[i]) + ":" + JSON.stringify(this.members.values[i])); } return resultArray; } @@ -252,20 +253,20 @@ class LightWeightClass { } } -type RBTreeNodeColor = "black" | "red"; -const BLACK = "black"; -const RED = "red"; +type RBTreeNodeColor = 0 | 1; +const BLACK = 0; +const RED = 1; class RBTreeNode extends Pair{ color: RBTreeNodeColor; - left: RBTreeNode | null; - right: RBTreeNode | null; - parent: RBTreeNode | null; + left: RBTreeNode | undefined; + right: RBTreeNode | undefined; + parent: RBTreeNode | undefined; constructor(key: K, value?: V, color: RBTreeNodeColor = RED, - parent: RBTreeNode | null = null, - left: RBTreeNode | null = null, - right: RBTreeNode | null = null) { + parent?: RBTreeNode, + left?: RBTreeNode, + right?: RBTreeNode) { super(key, value); this.color = color; this.left = left; @@ -274,15 +275,17 @@ class RBTreeNode extends Pair{ } } class RBTreeClass { - private _root: RBTreeNode | null; + private root: RBTreeNode | undefined; public memberNumber: number; - private _isChange: boolean; - private _treeArray: Array>; - constructor(root: RBTreeNode | null = null) { - this._root = root; + private isChange: boolean; + private treeNodeArray: Array>; + private compFun: Function | undefined; + constructor(comparator?: (firstValue: K, secondValue: K) => boolean, root?: RBTreeNode) { + this.root = root; + this.compFun = comparator; this.memberNumber = 0; - this._isChange = true; - this._treeArray = []; + this.isChange = true; + this.treeNodeArray = []; } get keyValueArray() { @@ -291,11 +294,11 @@ class RBTreeClass { } addNode(key: K, value: V = key as unknown as V): RBTreeClass { - if (this._root === null) { - this._root = new RBTreeNode(key, value) - this.setColor(this._root, BLACK); + if (this.root === undefined) { + this.root = new RBTreeNode(key, value); + this.setColor(BLACK, this.root); this.memberNumber++; - this._isChange = true; + this.isChange = true; } else { this.addProcess(key, value) } @@ -303,12 +306,12 @@ class RBTreeClass { } addProcess(key: K, value: V): RBTreeClass { - let leafNode: RBTreeNode | null = this._root; - let parentNode: RBTreeNode = this._root as RBTreeNode; + let leafNode: RBTreeNode | undefined = this.root; + let parentNode: RBTreeNode = this.root as RBTreeNode; let comp: number = 0; - while (leafNode !== null) { + while (leafNode !== undefined) { parentNode = leafNode; - comp = currencyCompare(leafNode.key, key); + comp = currencyCompare(leafNode.key, key, this.compFun); if (comp === 0) { leafNode.value = value; return this; @@ -327,14 +330,14 @@ class RBTreeClass { } this.insertRebalance(leafNode); this.memberNumber++; - this._isChange = true; + this.isChange = true; return this; } - removeNode(key: K): V | null { + removeNode(key: K): V | undefined { const removeNode = this.getNode(key); - if (removeNode === null) { - return null; + if (removeNode === undefined) { + return undefined; } else { let result = removeNode.value; this.removeNodeProcess(removeNode); @@ -343,126 +346,138 @@ class RBTreeClass { } removeNodeProcess(removeNode: RBTreeNode) { - if (removeNode.left !== null && removeNode.right !== null) { + if (removeNode.left !== undefined && removeNode.right !== undefined) { let successor = removeNode.right; - while (successor.left !== null) { + while (successor.left !== undefined) { successor = successor.left; } - removeNode = successor; + removeNode.key = successor.key; + removeNode.value = successor.value; + this.removeNodeProcess(successor); // only once + return; + } else { // one or zero child + let child = (removeNode.left === undefined ? removeNode.right : removeNode.left); + if (removeNode.parent === undefined) { // remove is root + if (child === undefined) { + this.root = undefined; + } else { + child.parent = undefined; + child.color = BLACK; + this.root = child; + } + } else { + if (child != undefined) { + // delete removeNode + if (removeNode.parent.left === removeNode) { + removeNode.parent.left = child; + } else { + removeNode.parent.right = child; + } + if (this.getColor(removeNode) === BLACK) { + this.deleteRebalance(child) + } + } else { + if (this.getColor(removeNode) === BLACK) { + this.deleteRebalance(removeNode) + } + if (removeNode.parent.left === removeNode) { + removeNode.parent.left = child; + } else { + removeNode.parent.right = child; + } + } + } + this.memberNumber--; + this.isChange = true; } - let replacementNode = (removeNode.right === null ? removeNode.left : removeNode.right); - if (replacementNode !== null) { - replacementNode.parent = removeNode.parent; - if (removeNode.parent === null) { - this._root = replacementNode; - } else if (removeNode === removeNode.parent.right) { - removeNode.parent.right = replacementNode; - } else if (removeNode === removeNode.parent.left) { - removeNode.parent.left = replacementNode; - } - if (this.getColor(removeNode) === BLACK) { - this.deleteRebalance(replacementNode) - } - } else if (removeNode.parent === null) { - // removeNode.right = null; removeNode.left = null - this._root = null; - } else { - if (this.getColor(removeNode) === BLACK) { - this.deleteRebalance(removeNode) - } - if (removeNode === removeNode.parent.left) { - removeNode.parent.left = null; - } else if (removeNode === removeNode.parent.right) { - removeNode.parent.right = null; - } - } - this.memberNumber--; - this._isChange = true; } - getNode(key: K): RBTreeNode | null { - if (this._root === null) - return null; - let removeNode: RBTreeNode | null = this._root; - while (removeNode !== null && removeNode.key !== key) { - removeNode = removeNode.key > key ? removeNode.left : removeNode.right; + getNode(key: K): RBTreeNode | undefined { + if (this.root === undefined) + return undefined; + let findNode: RBTreeNode | undefined = this.root; + while (findNode !== undefined && findNode.key !== key) { + findNode = currencyCompare(findNode.key, key, this.compFun) === ComparResult.BIGGER_THAN ? + findNode.left : findNode.right; } - return removeNode; + return findNode; } - findNode(value: V): RBTreeNode | null { - let tempNode: RBTreeNode | null = null; + findNode(value: V): RBTreeNode | undefined { + let tempNode: RBTreeNode | undefined = undefined; this.recordByMinToMax(); for (let i = 0; i < this.memberNumber; i++) { - if (this._treeArray[i].value === value) tempNode = this._treeArray[i]; - break; + if (this.treeNodeArray[i].value === value) { + tempNode = this.treeNodeArray[i]; + break; + } } return tempNode; } - firstNode(): RBTreeNode | null { - let tempNode: RBTreeNode | null = this._root; - while (tempNode !== null && tempNode.left !== null) { + firstNode(): RBTreeNode | undefined { + let tempNode: RBTreeNode | undefined = this.root; + while (tempNode !== undefined && tempNode.left !== undefined) { tempNode = tempNode.left; } return tempNode; } - lastNode(): RBTreeNode | null { - let tempNode: RBTreeNode | null = this._root; - while (tempNode !== null && tempNode.right !== null) { + lastNode(): RBTreeNode | undefined { + let tempNode: RBTreeNode | undefined = this.root; + while (tempNode !== undefined && tempNode.right !== undefined) { tempNode = tempNode.right; } return tempNode; } isEmpty(): boolean { - return this._root === null; + return this.root === undefined; } setAll(map: RBTreeClass) { - this.recordByMinToMax(); - for (let i = 0; i < this.memberNumber; i++) { - map.addNode(this._treeArray[i].key, this._treeArray[i].value); + let tempArray = map.recordByMinToMax(); + for (let i = 0; i < map.memberNumber; i++) { + this.addNode(tempArray[i].key, tempArray[i].value); } } clearTree() { - this._root = null; + this.root = undefined; this.memberNumber = 0; } private recordByMinToMax(): Array> { - if (!this._isChange) return this._treeArray; + if (!this.isChange) return this.treeNodeArray; let stack = []; - this._treeArray = []; - let node = this._root; - while (node != null || stack.length) { - while (node != null) { + this.treeNodeArray = []; + let node = this.root; + while (node != undefined || stack.length) { + while (node != undefined) { stack.push(node); node = node.left; } let tempNode = stack.pop(); - if (tempNode === undefined || tempNode === null) + if (tempNode === undefined || tempNode === undefined) throw new Error("this function run error"); node = tempNode; - this._treeArray.push(node); + this.treeNodeArray.push(node); node = node.right; } - this._isChange = false; - this.memberNumber = this._treeArray.length; - return this._treeArray; + this.isChange = false; + this.memberNumber = this.treeNodeArray.length; + return this.treeNodeArray; } private lRotate(datumPointNode: RBTreeNode): RBTreeClass { let newTopNode = datumPointNode.right; - if (newTopNode === null) - throw new Error("[rotate right error]: the right child node of the base node === null") + if (newTopNode === undefined) + throw new Error("[rotate right error]: the right child node of the base node === undefined") datumPointNode.right = newTopNode.left; - datumPointNode.right !== null ? datumPointNode.right.parent = datumPointNode : ""; + datumPointNode.right !== undefined ? datumPointNode.right.parent = datumPointNode : ""; newTopNode.parent = datumPointNode.parent; - if (datumPointNode.parent === null) { - this._root = newTopNode; + if (datumPointNode.parent === undefined) { + this.root = newTopNode; } else if (datumPointNode.parent.left === datumPointNode) { datumPointNode.parent.left = newTopNode; } else if (datumPointNode.parent.right === datumPointNode) { @@ -475,14 +490,14 @@ class RBTreeClass { private rRotate(datumPointNode: RBTreeNode): RBTreeClass { const newTopNode = datumPointNode.left; - if (newTopNode === null) { - throw new Error("[rotate right error]: the left child node of the base node === null") + if (newTopNode === undefined) { + throw new Error("[rotate right error]: the left child node of the base node === undefined") } datumPointNode.left = newTopNode.right; - datumPointNode.left !== null ? datumPointNode.left.parent = datumPointNode : ""; + datumPointNode.left !== undefined ? datumPointNode.left.parent = datumPointNode : ""; newTopNode.parent = datumPointNode.parent - if (datumPointNode.parent === null) { - this._root = newTopNode; + if (datumPointNode.parent === undefined) { + this.root = newTopNode; } else if (datumPointNode === datumPointNode.parent.left) { datumPointNode.parent.left = newTopNode; } else if (datumPointNode === datumPointNode.parent.right) { @@ -496,23 +511,23 @@ class RBTreeClass { private insertRebalance(fixNode: RBTreeNode): RBTreeClass { let parentNode = fixNode.parent; while (this.getColor(parentNode) === RED && - parentNode !== null && - parentNode.parent !== null) { + parentNode !== undefined && + parentNode.parent !== undefined) { let grandpaNode = parentNode && parentNode.parent; if (parentNode === grandpaNode.left && this.getColor(grandpaNode.right) === BLACK && fixNode === parentNode.left) { this - .setColor(parentNode, BLACK) - .setColor(grandpaNode, RED) + .setColor(BLACK, parentNode) + .setColor(RED, grandpaNode) .rRotate(grandpaNode) break; } else if (parentNode === grandpaNode.left && this.getColor(grandpaNode.right) === BLACK && fixNode === parentNode.right) { this - .setColor(fixNode, BLACK) - .setColor(grandpaNode, RED) + .setColor(BLACK, fixNode) + .setColor(RED, grandpaNode) .lRotate(parentNode) .rRotate(grandpaNode) break; @@ -520,8 +535,8 @@ class RBTreeClass { this.getColor(grandpaNode.left) === BLACK && fixNode === parentNode.left) { this - .setColor(fixNode, BLACK) - .setColor(grandpaNode, RED) + .setColor(BLACK, fixNode) + .setColor(RED, grandpaNode) .rRotate(parentNode) .lRotate(grandpaNode) break; @@ -529,64 +544,64 @@ class RBTreeClass { this.getColor(grandpaNode.left) === BLACK && fixNode === parentNode.right) { this - .setColor(parentNode, BLACK) - .setColor(grandpaNode, RED) + .setColor(BLACK, parentNode) + .setColor(RED, grandpaNode) .lRotate(grandpaNode) break; } else if ((parentNode === grandpaNode.right && this.getColor(grandpaNode.left) === RED) || (parentNode === grandpaNode.left && this.getColor(grandpaNode.right) === RED)) { this - .setColor(parentNode, BLACK) - .setColor(parentNode === grandpaNode.left ? grandpaNode.right : grandpaNode.left, BLACK) - .setColor(grandpaNode, RED) + .setColor(BLACK, parentNode) + .setColor(BLACK, parentNode === grandpaNode.left ? grandpaNode.right : grandpaNode.left) + .setColor(RED, grandpaNode) fixNode = grandpaNode; parentNode = fixNode.parent; } else { throw new Error("Exceptions after adding") } } - this._root ? this._root.color = BLACK : ""; + this.root ? this.root.color = BLACK : ""; return this; } private deleteRebalance(fixNode: RBTreeNode) { - while (this.getColor(fixNode) === BLACK && fixNode !== this._root && fixNode.parent) { - let sibling: RBTreeNode | null; + while (this.getColor(fixNode) === BLACK && fixNode !== this.root && fixNode.parent) { + let sibling: RBTreeNode | undefined; if (fixNode === fixNode.parent.left) { sibling = fixNode.parent.right; if (this.getColor(sibling) === RED) { this - .setColor(fixNode.parent, RED) - .setColor(sibling, BLACK) + .setColor(RED, fixNode.parent) + .setColor(BLACK, sibling) .lRotate(fixNode.parent) sibling = fixNode.parent.right; } - if (sibling === null) { - throw new Error('Error sibling node is null') + if (sibling === undefined) { + throw new Error('Error sibling node is undefined') } if (this.getColor(sibling.left) === BLACK && this.getColor(sibling.right) === BLACK) { - this.setColor(sibling, RED) + this.setColor(RED, sibling) fixNode = fixNode.parent } else if (this.getColor(sibling.left) === RED && this.getColor(sibling.right) === BLACK) { this - .setColor(sibling, RED) - .setColor(sibling.left, BLACK) + .setColor(RED, sibling) + .setColor(BLACK, sibling.left) .rRotate(sibling); sibling = fixNode.parent.right - if (sibling === null) { + if (sibling === undefined) { throw new Error('Error sibling node is empty') } this - .setColor(sibling, fixNode.parent.color) - .setColor(fixNode.parent, BLACK) - .setColor(sibling.right, BLACK) + .setColor(fixNode.parent.color, sibling) + .setColor(BLACK, fixNode.parent) + .setColor(BLACK, sibling.right) .lRotate(fixNode.parent); break; } else if (this.getColor(sibling.right) === RED) { this - .setColor(sibling, fixNode.parent.color) - .setColor(fixNode.parent, BLACK) - .setColor(sibling.right, BLACK) + .setColor(fixNode.parent.color, sibling) + .setColor(BLACK, fixNode.parent) + .setColor(BLACK, sibling.right) .lRotate(fixNode.parent); break; } else { @@ -596,38 +611,38 @@ class RBTreeClass { sibling = fixNode.parent.left; if (this.getColor(sibling) === RED) { this - .setColor(sibling, BLACK) - .setColor(fixNode.parent, RED) + .setColor(BLACK, sibling) + .setColor(RED, fixNode.parent) .rRotate(fixNode.parent); sibling = fixNode.parent.left; } - if (sibling === null) { - throw new Error('Error sibling node is null') + if (sibling === undefined) { + throw new Error('Error sibling node is undefined') } if (this.getColor(sibling.left) === BLACK && this.getColor(sibling.right) === BLACK) { this - .setColor(sibling, RED) + .setColor(RED, sibling) fixNode = fixNode.parent; } else if (this.getColor(sibling.left) === BLACK && this.getColor(sibling.right) === RED) { this - .setColor(sibling, RED) - .setColor(sibling.right, BLACK) + .setColor(RED, sibling) + .setColor(BLACK, sibling.right) .lRotate(sibling); sibling = fixNode.parent.left; - if (sibling === null) { + if (sibling === undefined) { throw new Error('Adjust the error after the error is deleted') } this - .setColor(sibling, fixNode.parent.color) - .setColor(fixNode.parent, BLACK) - .setColor(sibling.left, BLACK) + .setColor(fixNode.parent.color, sibling) + .setColor(BLACK, fixNode.parent) + .setColor(BLACK, sibling.left) .rRotate(fixNode.parent); break; } else if (this.getColor(sibling.left) === RED) { this - .setColor(sibling, fixNode.parent.color) - .setColor(fixNode.parent, BLACK) - .setColor(sibling.left, BLACK) + .setColor(fixNode.parent.color, sibling) + .setColor(BLACK, fixNode.parent,) + .setColor(BLACK, sibling.left) .rRotate(fixNode.parent); break; } else { @@ -635,15 +650,15 @@ class RBTreeClass { } } } - this.setColor(fixNode, BLACK) + this.setColor(BLACK, fixNode) } - private getColor(node: RBTreeNode | null): RBTreeNodeColor { - return node === null ? BLACK : node.color; + private getColor(node: RBTreeNode | undefined): RBTreeNodeColor { + return node === undefined ? BLACK : node.color; } - private setColor(node: RBTreeNode | null, color: RBTreeNodeColor): RBTreeClass { - if (node === null) { + private setColor(color: RBTreeNodeColor, node: RBTreeNode | undefined): RBTreeClass { + if (node === undefined) { throw new Error("Wrong color setting") } else { node.color = color @@ -652,20 +667,20 @@ class RBTreeClass { } } -const MAX_CAPACITY = 1 << 30; +const MAXcapacity = 1 << 30; const LOADER_FACTOR = 0.75; class DictionaryClass { - private _tableLink: { [hashIndex: number]: LinkedList> | RBTreeClass }; + private tableLink: { [hashIndex: number]: LinkedList> | RBTreeClass }; protected memberNumber: number; - private _isChange: boolean; - private _memberArray: Array>; - private _capacity: number; + private isChange: boolean; + private memberArray: Array>; + private capacity: number; constructor() { - this._tableLink = {}; + this.tableLink = {}; this.memberNumber = 0; - this._isChange = true; - this._memberArray = []; - this._capacity = 16; + this.isChange = true; + this.memberArray = []; + this.capacity = 16; } get keyValueArray() { @@ -676,8 +691,17 @@ class DictionaryClass { protected getHashIndex(key: K): number { let h; let hash = ((key === null) ? 0 : ((h = hashCode(key)) ^ (h >>> 16))); - this.expandCapacity(); - let n = this.power(this._capacity); + if (this.expandCapacity()) { + this.keyValues(); + this.memberNumber = 0; + this.tableLink = {}; + this.isChange = true; + for (let item of this.memberArray) { + this.put(item.key, item.value); + } + this.memberNumber++; + } + let n = this.power(this.capacity); return (n - 1) & hash; } @@ -692,43 +716,46 @@ class DictionaryClass { } private keyValues(): Pair[] { - if (!this._isChange) return this._memberArray; - this._memberArray = []; - const keys = Object.keys(this._tableLink).map((item) => parseInt(item)); + if (!this.isChange) return this.memberArray; + this.memberArray = []; + const keys = Object.keys(this.tableLink).map((item) => parseInt(item)); for (let i = 0; i < keys.length; i++) { - const members = this._tableLink[keys[i]]; + const members = this.tableLink[keys[i]]; if (members instanceof RBTreeClass) { let tempArray = members.keyValueArray; for (let i = 0; i < members.memberNumber; i++) { - this._memberArray.push(new Pair(tempArray[i].key, tempArray[i].value)); + this.memberArray.push(new Pair(tempArray[i].key, tempArray[i].value)); } } else { - if (members != null && !members.isEmpty()) { + if (members != undefined && !members.isEmpty()) { let current = members.getHead(); - while (current != null) { - this._memberArray.push(current.element); + while (current != undefined) { + this.memberArray.push(current.element); current = current.next; } } } } - this.memberNumber = this._memberArray.length; - let valuePairs = this._memberArray; + this.memberNumber = this.memberArray.length; + let valuePairs = this.memberArray; return valuePairs; } protected expandCapacity() { - while (this._capacity < this.memberNumber / LOADER_FACTOR && this._capacity < MAX_CAPACITY) { - this._capacity = 2 * this._capacity; + let capacityChange = false; + while (this.capacity < this.memberNumber / LOADER_FACTOR && this.capacity < MAXcapacity) { + this.capacity = 2 * this.capacity; + capacityChange = true; } + return capacityChange; } protected put(key: K, value: V = key as unknown as V): boolean { - if (key != null && value != null) { - this._isChange = true; - this.memberNumber++; + if (key != undefined && value != undefined) { + this.isChange = true; + if (!this.hasKey(key)) this.memberNumber++; const position = this.getHashIndex(key); - let members = this._tableLink[position]; + let members = this.tableLink[position]; if (members instanceof LinkedList && members.count >= 8) { let newElement = new RBTreeClass(); let current = members.getHead(); @@ -739,19 +766,19 @@ class DictionaryClass { current = current.next; } newElement.addNode(key, value); - this._tableLink[position] = newElement; + this.tableLink[position] = newElement; return true; } else if (members instanceof RBTreeClass) { members.addNode(key, value); - this._tableLink[position] = members; + this.tableLink[position] = members; return true; } else { - if (this._tableLink[position] == null) { + if (this.tableLink[position] == undefined) { members = new LinkedList>(); } if (!this.replaceMember(key, value)) { members.push(new Pair(key, value)); - this._tableLink[position] = members; + this.tableLink[position] = members; } return true; } @@ -761,10 +788,10 @@ class DictionaryClass { protected replaceMember(key: K, value: V = key as unknown as V): boolean { const position = this.getHashIndex(key); - const members = this._tableLink[position] as LinkedList>; + const members = this.tableLink[position] as LinkedList>; if (members === null || members === undefined) return false; let current = members.getHead(); - while (current != null || current != undefined) { + while (current != undefined) { if (current.element.key === key) { current.element.value = value; return true; @@ -776,16 +803,16 @@ class DictionaryClass { protected getValueByKey(key: K): V | undefined { const position = this.getHashIndex(key); - const members = this._tableLink[position]; + const members = this.tableLink[position]; if (members instanceof RBTreeClass) { let resultNode = members.getNode(key); - if (resultNode === null) return undefined; + if (resultNode === undefined) return undefined; return resultNode.value; } else { - if (members != null && !members.isEmpty()) { + if (members != undefined && !members.isEmpty()) { members as LinkedList>; let current = members.getHead(); - while (current != null) { + while (current != undefined) { if (current.element.key === key) { return current.element.value; } @@ -796,27 +823,27 @@ class DictionaryClass { return undefined; } - protected removeMember(key: K): V | null { + protected removeMember(key: K): V | undefined { const position = this.getHashIndex(key); - const members = this._tableLink[position]; + const members = this.tableLink[position]; if (members instanceof RBTreeClass) { let result = members.removeNode(key); - if (result != null) { - this._isChange = true; + if (result != undefined) { + this.isChange = true; this.memberNumber--; return result; } } else { - if (members != null && !members.isEmpty()) { + if (members != undefined && !members.isEmpty()) { let current = members.getHead(); - while (current != null) { + while (current != undefined) { if (current.element.key === key) { const result = current.element.value; members.remove(current.element); if (members.isEmpty()) { - delete this._tableLink[position]; + delete this.tableLink[position]; } - this._isChange = true; + this.isChange = true; this.memberNumber--; return result; } @@ -824,24 +851,25 @@ class DictionaryClass { } } } - return null; + return undefined; } protected clear() { - this._tableLink = {}; + this.tableLink = {}; this.memberNumber = 0; - this._isChange = true; + this.isChange = true; + this.capacity = 16; } protected hasKey(key: K): boolean { const position = this.getHashIndex(key); - const members = this._tableLink[position]; - if (members === null || members === undefined) return false; + const members = this.tableLink[position]; + if (members === undefined || members === undefined) return false; if (members instanceof RBTreeClass) { - return members.getNode(key) !== null; + return members.getNode(key) !== undefined; } let current = members.getHead(); - while (current != null && current != undefined) { + while (current != undefined && current != undefined) { if (current.element.key === key) { return true; } @@ -851,9 +879,9 @@ class DictionaryClass { } protected setAll(map: DictionaryClass): void { - let memebers = this.keyValues(); + let memebers = map.keyValues(); for (let i = 0; i < memebers.length; i++) { - map.put(memebers[i].key, memebers[i].value); + this.put(memebers[i].key, memebers[i].value); } } @@ -867,32 +895,32 @@ class DictionaryClass { } } -class Node{ +class Node { element: T; - next: Node | null; - constructor(element: T, next: Node | null = null) { + next: Node | undefined; + constructor(element: T, next?: Node) { this.element = element; this.next = next; } } class LinkedList { public count: number; - protected next: Node | null; - protected head: Node | null; + protected next: Node | undefined; + protected head: Node | undefined; constructor() { this.count = 0; - this.next = null; - this.head = null; + this.next = undefined; + this.head = undefined; } push(element: T) { const node = new Node(element); let current; - if (this.head == null) { + if (this.head == undefined) { this.head = node; } else { current = this.head; - while (current.next != null) { + while (current.next != undefined) { current = current.next; } current.next = node; @@ -903,16 +931,16 @@ class LinkedList { removeAt(index: number) { if (index >= 0 && index < this.count) { let current = this.head; - if (index === 0 && current != null) { + if (index === 0 && current != undefined) { this.head = current.next; } else { const previous = this.getElementAt(index--); - if (previous !== null) { + if (previous !== undefined) { current = previous.next; - previous.next = (current === null ? null : current.next); + previous.next = (current === undefined ? undefined : current.next); } } - if (current !== null) { + if (current !== undefined) { this.count--; return current.element; } @@ -923,12 +951,12 @@ class LinkedList { getElementAt(index: number) { if (index > 0 && index < this.count) { let current = this.head; - for (let i = 0; i < index && current != null; i++) { + for (let i = 0; i < index && current != undefined; i++) { current = current.next; } return current; } - return null; + return undefined; } insert(element: T, index: number) { @@ -939,7 +967,7 @@ class LinkedList { this.head = node; } else { const previous = this.getElementAt(index--); - if (previous === null) + if (previous === undefined) throw new Error("data storage error"); node.next = previous.next; previous.next = node; @@ -950,10 +978,10 @@ class LinkedList { return false; } - indexOf(element: T) { + indexOf(element: T, compareFn?: Function) { let current = this.head; - for (let i = 0; i < this.count && current != null; i++) { - if (currencyCompare(element, current.element)) { + for (let i = 0; i < this.count && current != undefined; i++) { + if (currencyCompare(element, current.element, compareFn) === ComparResult.EQUALS) { return i; } current = current.next; @@ -961,12 +989,12 @@ class LinkedList { return -1; } - remove(element: T) { - this.removeAt(this.indexOf(element)); + remove(element: T, compareFn?: Function) { + this.removeAt(this.indexOf(element, compareFn)); } clear() { - this.head = null; + this.head = undefined; this.count = 0; } @@ -979,12 +1007,12 @@ class LinkedList { } toString() { - if (this.head == null) { + if (this.head == undefined) { return ""; } let objString = `${this.head.element}`; let current = this.head.next; - for (let i = 1; i < this.count && current != null; i++) { + for (let i = 1; i < this.count && current != undefined; i++) { objString = `${objString}, ${current.element}`; current = current.next; } diff --git a/container/treemap/js_treemap.ts b/container/treemap/js_treemap.ts index 0b14f90..18d1204 100644 --- a/container/treemap/js_treemap.ts +++ b/container/treemap/js_treemap.ts @@ -50,81 +50,85 @@ if (flag || fastTreeMap === undefined) { } } class TreeMap { - private _constitute: any; - constructor() { - this._constitute = new RBTreeAbility.RBTreeClass(); + private constitute: any; + constructor(comparator?: (firstValue: K, secondValue: K) => boolean) { + this.constitute = new RBTreeAbility.RBTreeClass(comparator); return new Proxy(this, new HandlerTreeMap()); - } + } get length() { - return this._constitute.memberNumber; + return this.constitute.memberNumber; + } + isEmpty() { + return this.constitute.memberNumber === 0; } hasKey(key: K): boolean { - return this._constitute.getNode(key) !== null; + return this.constitute.getNode(key) !== undefined; } hasValue(value: V): boolean { - return this._constitute.findNode(value) !== null; + return this.constitute.findNode(value) !== undefined; } - get(key: K): V | null { - let tempNode = this._constitute.getNode(key); - if (tempNode === null) - return null; + get(key: K): V { + let tempNode = this.constitute.getNode(key); + if (tempNode === undefined) + throw new Error("The node of this key does not exist in the tree"); return tempNode.value; } getFirstKey(): K { - let tempNode = this._constitute.firstNode(); - if (tempNode === null) - throw new Error("don't find this key,this tree is null"); + let tempNode = this.constitute.firstNode(); + if (tempNode === undefined) + throw new Error("don't find this key,this tree is empty"); return tempNode.key; } getLastKey(): K { - let tempNode = this._constitute.lastNode(); - if (tempNode === null) - throw new Error("don't find this key,this tree is null"); + let tempNode = this.constitute.lastNode(); + if (tempNode === undefined) + throw new Error("don't find this key,this tree is empty"); return tempNode.key; } setAll(map: TreeMap) { - this._constitute.setAll(map._constitute); + this.constitute.setAll(map.constitute); } set(key: K, value: V): Object { - return this._constitute.addNode(key, value); + return this.constitute.addNode(key, value); } - remove(key: K): V | null { - return this._constitute.removeNode(key); + remove(key: K): V { + return this.constitute.removeNode(key); } clear() { - this._constitute.clearTree(); + this.constitute.clearTree(); } getLowerKey(key: K): K { - let tempNode = this._constitute.getNode(key); - if (tempNode === null) + let tempNode = this.constitute.getNode(key); + if (tempNode === undefined) throw new Error("don't find this key,this node is undefine"); - if (tempNode.left !== null) return tempNode.left.key; + if (tempNode.left !== undefined) return tempNode.left.key; let node = tempNode; - while (node.parent !== null) { + while (node.parent !== undefined) { if (node.parent.right === node) return node.parent.key; node = node.parent; } throw new Error("don't find a key meet the conditions"); } getHigherKey(key: K): K { - let tempNode = this._constitute.getNode(key); - if (tempNode === null) + let tempNode = this.constitute.getNode(key); + if (tempNode === undefined) throw new Error("don't find this key,this node is undefine"); - if (tempNode.right !== null) return tempNode.right.key; + if (tempNode.right !== undefined) return tempNode.right.key; let node = tempNode; - while (node.parent !== null) { + while (node.parent !== undefined) { if (node.parent.left === node) return node.parent.key; node = node.parent; } throw new Error("don't find a key meet the conditions"); } keys(): IterableIterator { - let data = this._constitute; - let i = 0; + let data = this.constitute; + let count = 0; return { next: function () { - var done = i >= data.memberNumber; - var value = !done ? data.keyValueArray[i++].key : undefined; + var done = count >= data.memberNumber; + var value = !done ? data.keyValueArray[count].key : undefined; + count++; return { done: done, value: value, @@ -133,12 +137,13 @@ if (flag || fastTreeMap === undefined) { }; } values(): IterableIterator { - let data = this._constitute; - let i = 0; + let data = this.constitute; + let count = 0; return { next: function () { - var done = i >= data.memberNumber; - var value = !done ? data.keyValueArray[i++].value as V : undefined; + var done = count >= data.memberNumber; + var value = !done ? data.keyValueArray[count].value : undefined; + count++; return { done: done, value: value, @@ -147,26 +152,27 @@ if (flag || fastTreeMap === undefined) { }; } replace(key: K, newValue: V): boolean { - let targetNode = this._constitute.getNode(key); - if (targetNode === null) return false; + let targetNode = this.constitute.getNode(key); + if (targetNode === undefined) return false; targetNode.value = newValue; return true; } forEach(callbackfn: (value?: V, key?: K, map?: TreeMap) => void, thisArg?: Object): void { - let data = this._constitute; + let data = this.constitute; let tagetArray = data.keyValueArray; for (let i = 0; i < data.memberNumber; i++) { callbackfn.call(thisArg, tagetArray[i].value as V, tagetArray[i].key); } } entries(): IterableIterator<[K, V]> { - let data = this._constitute; - let i = 0; + let data = this.constitute; + let count = 0; return { next: function () { - var done = i >= data.memberNumber; - var value = !done ? data.keyValueArray[i++].entry() : undefined; + var done = count >= data.memberNumber; + var value = !done ? data.keyValueArray[count].entry() : undefined; + count++; return { done: done, value: value, @@ -175,12 +181,13 @@ if (flag || fastTreeMap === undefined) { }; } [Symbol.iterator](): IterableIterator<[K, V]> { - let data = this._constitute; - let i = 0; + let data = this.constitute; + let count = 0; return { next: function () { - var done = i >= data.memberNumber; - var value = !done ? data.keyValueArray[i++].entry() : undefined; + var done = count >= data.memberNumber; + var value = !done ? data.keyValueArray[count].entry() : undefined; + count++; return { done: done, value: value, diff --git a/container/treeset/js_treeset.ts b/container/treeset/js_treeset.ts index 070c284..8a0e450 100644 --- a/container/treeset/js_treeset.ts +++ b/container/treeset/js_treeset.ts @@ -50,90 +50,91 @@ if (flag || fastTreeSet === undefined) { } } class TreeSet { - private _constitute: any; - constructor() { - this._constitute = new RBTreeAbility.RBTreeClass(); + private constitute: any; + constructor(comparator?: (firstValue: T, secondValue: T) => boolean) { + this.constitute = new RBTreeAbility.RBTreeClass(comparator); return new Proxy(this, new HandlerTreeSet()); } get length() { - return this._constitute.memberNumber; + return this.constitute.memberNumber; } isEmpty(): boolean { - return this._constitute.isEmpty(); + return this.constitute.isEmpty(); } has(value: T): boolean { - return this._constitute.getNode(value) !== null; + return this.constitute.getNode(value) !== undefined; } add(value: T): boolean { - this._constitute.addNode(value); + this.constitute.addNode(value); return true; } remove(value: T): boolean { - let result = this._constitute.removeNode(value); - return result !== null; + let result = this.constitute.removeNode(value); + return result !== undefined; } clear() { - this._constitute.clearTree(); + this.constitute.clearTree(); } getFirstValue(): T { - let tempNode = this._constitute.firstNode(); - if (tempNode === null) - throw new Error("don't find this key,this tree is null"); + let tempNode = this.constitute.firstNode(); + if (tempNode === undefined) + throw new Error("don't find this key,this tree is empty"); return tempNode.key; } getLastValue(): T { - let tempNode = this._constitute.lastNode(); - if (tempNode === null) - throw new Error("don't find this key,this tree is null"); + let tempNode = this.constitute.lastNode(); + if (tempNode === undefined) + throw new Error("don't find this key,this tree is empty"); return tempNode.key; } getLowerValue(key: T): T { - let tempNode = this._constitute.getNode(key); - if (tempNode === null) + let tempNode = this.constitute.getNode(key); + if (tempNode === undefined) throw new Error("don't find this key,this node is undefine"); - if (tempNode.left !== null) return tempNode.left.key; + if (tempNode.left !== undefined) return tempNode.left.key; let node = tempNode; - while (node.parent !== null) { + while (node.parent !== undefined) { if (node.parent.right === node) return node.parent.key; node = node.parent; // node.parent.left === node is true; } throw new Error("don't find a key meet the conditions"); } getHigherValue(key: T): T { - let tempNode = this._constitute.getNode(key); - if (tempNode === null) + let tempNode = this.constitute.getNode(key); + if (tempNode === undefined) throw new Error("don't find this key,this node is undefine"); - if (tempNode.right !== null) return tempNode.right.key; + if (tempNode.right !== undefined) return tempNode.right.key; let node = tempNode; - while (node.parent !== null) { + while (node.parent !== undefined) { if (node.parent.left === node) return node.parent.key; node = node.parent; // node.parent.right === node is true; } throw new Error("don't find a key meet the conditions"); } popFirst(): T { - let firstNode = this._constitute.firstNode(); - if (firstNode === null) + let firstNode = this.constitute.firstNode(); + if (firstNode === undefined) throw new Error("don't find first node,this tree is empty"); let value = firstNode.value; - this._constitute.removeNodeProcess(firstNode); + this.constitute.removeNodeProcess(firstNode); return value as T; } popLast(): T { - let lastNode = this._constitute.lastNode(); - if (lastNode === null) + let lastNode = this.constitute.lastNode(); + if (lastNode === undefined) throw new Error("don't find last node,this tree is empty"); let value = lastNode.value; - this._constitute.removeNodeProcess(lastNode); + this.constitute.removeNodeProcess(lastNode); return value as T; } values(): IterableIterator { - let data = this._constitute; + let data = this.constitute; let count = 0; return { next: function () { var done = count >= data.memberNumber; - var value = !done ? data.keyValueArray[count++].value as T : undefined; + var value = !done ? data.keyValueArray[count].value as T : undefined; + count++; return { done: done, value: value, @@ -143,19 +144,20 @@ if (flag || fastTreeSet === undefined) { } forEach(callbackfn: (value?: T, key?: T, set?: TreeSet) => void, thisArg?: Object): void { - let data = this._constitute; + let data = this.constitute; let tagetArray = data.keyValueArray; for (let i = 0; i < data.memberNumber; i++) { callbackfn.call(thisArg, tagetArray[i].value as T, tagetArray[i].key); } } entries(): IterableIterator<[T, T]> { - let data = this._constitute; + let data = this.constitute; let count = 0; return { next: function () { var done = count >= data.memberNumber; - var value = !done ? data.keyValueArray[count++].entry() : undefined; + var value = !done ? data.keyValueArray[count].entry() : undefined; + count++; return { done: done, value: value, @@ -164,12 +166,13 @@ if (flag || fastTreeSet === undefined) { }; } [Symbol.iterator](): IterableIterator { - let data = this._constitute; + let data = this.constitute; let count = 0; return { next: function () { var done = count >= data.memberNumber; - var value = !done ? data.keyValueArray[count++].key : undefined; + var value = !done ? data.keyValueArray[count].key : undefined; + count++; return { done: done, value: value, diff --git a/container/vector/js_vector.ts b/container/vector/js_vector.ts index a37fe7f..3434061 100644 --- a/container/vector/js_vector.ts +++ b/container/vector/js_vector.ts @@ -35,7 +35,7 @@ if (flag || fastVector == undefined) { return obj[prop]; } set(obj: Vector, prop: any, value: T) { - if (prop === "_length" || prop === "_capacity") { + if (prop === "elementNum" || prop === "capacity") { obj[prop] = value; return true; } @@ -95,33 +95,33 @@ if (flag || fastVector == undefined) { }; } class Vector { - private _length: number = 0; - private _capacity: number = 10; + private elementNum: number = 0; + private capacity: number = 10; constructor() { return new Proxy(this, new HandlerVector()); } get length() { - return this._length; + return this.elementNum; } add(element: T): boolean { if (this.isFull()) { this.resize(); } - this[this._length++] = element; + this[this.elementNum++] = element; return true; } insert(element: T, index: number): void { if (this.isFull()) { this.resize(); } - for (let i = this._length; i > index; i--) { + for (let i = this.elementNum; i > index; i--) { this[i] = this[i - 1]; } this[index] = element; - this._length++; + this.elementNum++; } has(element: T): boolean { - for (let i = 0; i < this._length; i++) { + for (let i = 0; i < this.elementNum; i++) { if (this[i] === element) { return true; } @@ -132,7 +132,7 @@ if (flag || fastVector == undefined) { return this[index]; } getIndexOf(element: T): number { - for (let i = 0; i < this._length; i++) { + for (let i = 0; i < this.elementNum; i++) { if (element === this[i]) { return i; } @@ -145,28 +145,30 @@ if (flag || fastVector == undefined) { set(index: number, element: T): void { this[index] = element; } - removeByIndex(index: number): void { - for (let i = index; i < this._length - 1; i++) { + removeByIndex(index: number): T { + let result = this[index]; + for (let i = index; i < this.elementNum - 1; i++) { this[i] = this[i + 1]; } - this._length--; + this.elementNum--; + return result; } remove(element: T): boolean { if (this.has(element)) { let index = this.getIndexOf(element); - for (let i = index; i < this._length - 1; i++) { + for (let i = index; i < this.elementNum - 1; i++) { this[i] = this[i + 1]; } - this._length--; + this.elementNum--; return true; } return false; } getLastElement(): T { - return this[this._length - 1]; + return this[this.elementNum - 1]; } getLastIndexOf(element: T): number { - for (let i = this._length - 1; i >= 0; i--) { + for (let i = this.elementNum - 1; i >= 0; i--) { if (element === this[i]) { return i; } @@ -185,7 +187,7 @@ if (flag || fastVector == undefined) { } getIndexFrom(element: T, index: number): number { if (this.has(element)) { - for (let i = index; i < this._length; i++) { + for (let i = index; i < this.elementNum; i++) { if (this[i] === element) { return i; } @@ -194,7 +196,7 @@ if (flag || fastVector == undefined) { return -1; } clear(): void { - this._length = 0; + this.elementNum = 0; } removeByRange(fromIndex: number, toIndex: number): void { if (fromIndex >= toIndex) { @@ -202,32 +204,32 @@ if (flag || fastVector == undefined) { } toIndex = toIndex >= this.length - 1 ? this.length - 1 : toIndex; let i = fromIndex; - for (let j = toIndex; j < this._length; j++) { + for (let j = toIndex; j < this.elementNum; j++) { this[i] = this[j]; i++; } - this._length -= toIndex - fromIndex; + this.elementNum -= toIndex - fromIndex; } setLength(newSize: number): void { - this._length = newSize; + this.elementNum = newSize; } replaceAllElements(callbackfn: (value: T, index?: number, vector?: Vector) => T, thisArg?: Object): void { - for (let i = 0; i < this._length; i++) { + for (let i = 0; i < this.elementNum; i++) { this[i] = callbackfn.call(thisArg, this[i], i, this); } } forEach(callbackfn: (value: T, index?: number, vector?: Vector) => void, thisArg?: Object): void { - for (let i = 0; i < this._length; i++) { + for (let i = 0; i < this.elementNum; i++) { callbackfn.call(thisArg, this[i], i, this); } } sort(comparator?: (firstValue: T, secondValue: T) => number): void { let isSort = true; if (comparator) { - for (let i = 0; i < this._length; i++) { - for (let j = 0; j < this._length - 1 - i; j++) { + for (let i = 0; i < this.elementNum; i++) { + for (let j = 0; j < this.elementNum - 1 - i; j++) { if (comparator(this[j], this[j + 1]) > 0) { isSort = false; let temp = this[j]; @@ -238,7 +240,7 @@ if (flag || fastVector == undefined) { } } else { for (var i = 0; i < this.length - 1; i++) { - for (let j = 0; j < this._length - 1 - i; j++) { + for (let j = 0; j < this.elementNum - 1 - i; j++) { if (this.asciSort(this[j], this[j + 1])) { isSort = false; let temp = this[j]; @@ -270,7 +272,10 @@ if (flag || fastVector == undefined) { if (fromIndex >= toIndex) { throw new Error(`fromIndex cannot be less than or equal to toIndex`); } - toIndex = toIndex >= this._length - 1 ? this._length - 1 : toIndex; + if (fromIndex >= this.elementNum || fromIndex < 0 || toIndex < 0) { + throw new Error(`fromIndex or toIndex is out-of-bounds`); + } + toIndex = toIndex >= this.elementNum - 1 ? this.elementNum - 1 : toIndex; let vector = new Vector(); for (let i = fromIndex; i < toIndex; i++) { vector.add(this[i]); @@ -279,7 +284,7 @@ if (flag || fastVector == undefined) { } convertToArray(): Array { let arr = []; - for (let i = 0; i < this._length; i++) { + for (let i = 0; i < this.elementNum; i++) { arr[i] = this[i]; } return arr; @@ -292,47 +297,44 @@ if (flag || fastVector == undefined) { } toString(): string { let str = `${this[0]}`; - for (let i = 1; i < this._length; i++) { + for (let i = 1; i < this.elementNum; i++) { str = `${str},${this[i]}`; } return str; } clone(): Vector { let clone = new Vector(); - for (let i = 0; i < this._length; i++) { - this.add(this[i]); + for (let i = 0; i < this.elementNum; i++) { + clone.add(this[i]); } return clone; } getCapacity(): number { - return this._capacity; + return this.capacity; } private isFull(): boolean { - return this._length === this._capacity; + return this.elementNum === this.capacity; } private resize(): void { - this._capacity = 2 * this._capacity; + this.capacity = 2 * this.capacity; } increaseCapacityTo(newCapacity: number): void { - if (newCapacity >= this._length) { - this._capacity = newCapacity; + if (newCapacity >= this.elementNum) { + this.capacity = newCapacity; } } trimToCurrentLength(): void { - this._capacity = this._length; - } - setSize(newSize: number): void { - this._length = newSize; + this.capacity = this.elementNum; } isEmpty(): boolean { - return this._length == 0; + return this.elementNum == 0; } [Symbol.iterator](): IterableIterator { let count = 0; - let vector = this + let vector = this; return { next: function () { - var done = count >= vector._length; + var done = count >= vector.elementNum; var value = !done ? vector[count++] : undefined; return { done: done,