diff --git a/container/arraylist/js_arraylist.ts b/container/arraylist/js_arraylist.ts index a0112fe3a4ffb591a7e9c8d112b6dfcfea5452fd..246a00df1be49329978da4129c8789b7f1194834 100644 --- a/container/arraylist/js_arraylist.ts +++ b/container/arraylist/js_arraylist.ts @@ -22,16 +22,19 @@ if (arkPritvate !== undefined) { } if (flag || fastArrayList == undefined) { class HandlerArrayList { - get(obj: ArrayList, prop: any) { - if (typeof prop === "symbol") { - return obj[prop]; - } - var index = Number.parseInt(prop); + private isOutBounds(obj: ArrayList, prop: any) { + let index = Number.parseInt(prop); if (Number.isInteger(index)) { if (index < 0 || index >= obj.length) { - throw new Error("ArrayList: get out-of-bounds"); + throw new RangeError("the index is out-of-bounds"); } } + } + get(obj: ArrayList, prop: any) { + if (typeof prop === "symbol") { + return obj[prop]; + } + this.isOutBounds(obj, prop); return obj[prop]; } set(obj: ArrayList, prop: any, value: T) { @@ -39,10 +42,10 @@ if (flag || fastArrayList == undefined) { obj[prop] = value; return true; } - var index = Number.parseInt(prop); + let index = Number.parseInt(prop); if (Number.isInteger(index)) { if (index < 0 || index > obj.length) { - throw new Error("ArrayList: set out-of-bounds"); + throw new RangeError("the index is out-of-bounds"); } else { obj[index] = value; return true; @@ -51,11 +54,9 @@ if (flag || fastArrayList == undefined) { return false; } deleteProperty(obj: ArrayList, prop: any) { - var index = Number.parseInt(prop); - if (Number.isInteger(index)) { - if (index < 0 || index >= obj.length) { - throw new Error("ArrayList: deleteProperty out-of-bounds"); - } + this.isOutBounds(obj, prop); + let index = Number.parseInt(prop); + if (index >= 0 && index < obj.length && Number.isInteger(index)) { obj.removeByIndex(index); return true; } @@ -75,17 +76,15 @@ if (flag || fastArrayList == undefined) { return true; } getOwnPropertyDescriptor(obj: ArrayList, prop: any) { - var index = Number.parseInt(prop); - if (Number.isInteger(index)) { - if (index < 0 || index >= obj.length) { - throw new Error("ArrayList: getOwnPropertyDescriptor out-of-bounds"); - } + this.isOutBounds(obj, prop); + let index = Number.parseInt(prop); + if (index >= 0 && index < obj.length && Number.isInteger(index)) { return Object.getOwnPropertyDescriptor(obj, prop); } return; } setPrototypeOf(obj: any, prop: any): T { - throw new Error("Can setPrototype on ArrayList Object"); + throw new TypeError("Can setPrototype on ArrayList Object"); } } interface IterableIterator { @@ -111,6 +110,9 @@ if (flag || fastArrayList == undefined) { return true; } insert(element: T, index: number): void { + if (index < 0 || index >= this.elementNum) { + throw new RangeError("the index is out-of-bounds"); + } if (this.isFull()) { this.resize(); } @@ -138,7 +140,7 @@ if (flag || fastArrayList == undefined) { } removeByIndex(index: number): T { if (index < 0 || index >= this.elementNum) { - throw new Error("removeByIndex is out-of-bounds"); + throw new RangeError("the index is out-of-bounds"); } let result = this[index]; for (let i = index; i < this.elementNum - 1; i++) { @@ -168,9 +170,12 @@ if (flag || fastArrayList == undefined) { } removeByRange(fromIndex: number, toIndex: number): void { if (fromIndex >= toIndex) { - throw new Error(`fromIndex cannot be less than or equal to toIndex`); + throw new RangeError(`the fromIndex cannot be less than or equal to toIndex`); } - toIndex = toIndex >= this.elementNum - 1 ? this.elementNum - 1 : toIndex; + if (fromIndex >= this.elementNum || fromIndex < 0 || toIndex < 0) { + throw new RangeError(`the fromIndex or the toIndex is out-of-bounds`); + } + toIndex = toIndex >= this.elementNum ? this.elementNum : toIndex; let i = fromIndex; for (let j = toIndex; j < this.elementNum; j++) { this[i] = this[j]; @@ -178,13 +183,13 @@ if (flag || fastArrayList == undefined) { } this.elementNum -= toIndex - fromIndex; } - replaceAllElements(callbackfn: (value: T, index?: number, arraylist?: ArrayList) => T, + replaceAllElements(callbackfn: (value: T, index?: number, arrList?: ArrayList) => T, thisArg?: Object): void { 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, + forEach(callbackfn: (value: T, index?: number, arrList?: ArrayList) => void, thisArg?: Object): void { for (let i = 0; i < this.elementNum; i++) { callbackfn.call(thisArg, this[i], i, this); @@ -204,7 +209,7 @@ if (flag || fastArrayList == undefined) { } } } else { - for (var i = 0; i < this.length - 1; i++) { + for (let i = 0; i < this.length - 1; i++) { for (let j = 0; j < this.elementNum - 1 - i; j++) { if (this.asciSort(this[j], this[j + 1])) { isSort = false; @@ -221,26 +226,26 @@ if (flag || fastArrayList == undefined) { } private asciSort(curElement: any, nextElement: any): boolean { if ((Object.prototype.toString.call(curElement) === "[object String]" || - Object.prototype.toString.call(curElement) === "[object Number]") && - (Object.prototype.toString.call(nextElement) === "[object String]" || - Object.prototype.toString.call(nextElement) === "[object Number]")) { - curElement = curElement.toString(); - nextElement = nextElement.toString(); - if(curElement > nextElement) { - return true; - } - return false + Object.prototype.toString.call(curElement) === "[object Number]") && + (Object.prototype.toString.call(nextElement) === "[object String]" || + Object.prototype.toString.call(nextElement) === "[object Number]")) { + curElement = curElement.toString(); + nextElement = nextElement.toString(); + if (curElement > nextElement) { + return true; + } + return false } return false; } subArrayList(fromIndex: number, toIndex: number): ArrayList { if (fromIndex >= toIndex) { - throw new Error(`fromIndex cannot be less than or equal to toIndex`); + throw new RangeError(`the fromIndex cannot be less than or equal to toIndex`); } if (fromIndex >= this.elementNum || fromIndex < 0 || toIndex < 0) { - throw new Error(`fromIndex or toIndex is out-of-bounds`); + throw new RangeError(`the fromIndex or the toIndex is out-of-bounds`); } - toIndex = toIndex > this.elementNum ? this.elementNum - 1 : toIndex; + toIndex = toIndex >= this.elementNum ? this.elementNum : toIndex; let arraylist = new ArrayList(); for (let i = fromIndex; i < toIndex; i++) { arraylist.add(this[i]); @@ -289,8 +294,8 @@ if (flag || fastArrayList == undefined) { let arraylist = this; return { next: function () { - var done = count >= arraylist.elementNum; - var value = !done ? arraylist[count++] : undefined; + let done = count >= arraylist.elementNum; + let value = !done ? arraylist[count++] : undefined; return { done: done, value: value, diff --git a/container/deque/js_deque.ts b/container/deque/js_deque.ts index 980507c28e383df6c20ff2fad442f852a69d1997..bfcc52b909b8100376beb75d0c11eded92229781 100644 --- a/container/deque/js_deque.ts +++ b/container/deque/js_deque.ts @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021 Huawei Device Co., Ltd. + * Copyright (c) 2022 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at @@ -22,16 +22,19 @@ if (arkPritvate !== undefined) { } if (flag || fastDeque == undefined) { class HandlerDeque { - get(obj: Deque, prop: any): T { - if (typeof prop === "symbol") { - return obj[prop]; - } - var index = Number.parseInt(prop); + private isOutBounds(obj: Deque, prop: any) { + let index = Number.parseInt(prop); if (Number.isInteger(index)) { if (index < 0) { - throw new Error("Deque: get out-of-bounds"); + throw new RangeError("the index is out-of-bounds"); } } + } + get(obj: Deque, prop: any): T { + if (typeof prop === "symbol") { + return obj[prop]; + } + this.isOutBounds(obj, prop); return obj[prop]; } set(obj: Deque, prop: any, value: T): boolean { @@ -39,10 +42,10 @@ if (flag || fastDeque == undefined) { obj[prop] = value; return true; } - var index = Number(prop); + let index = Number(prop); if (Number.isInteger(index)) { if (index < 0) { - throw new Error("Deque: set out-of-bounds"); + throw new RangeError("index is out-of-bounds"); } else { obj[index] = value; return true; @@ -64,17 +67,15 @@ if (flag || fastDeque == undefined) { return true; } getOwnPropertyDescriptor(obj: Deque, prop: any) { - var index = Number.parseInt(prop); - if (Number.isInteger(index)) { - if (index < 0 || index >= obj.length) { - throw new Error("Deque: getOwnPropertyDescriptor out-of-bounds"); - } + this.isOutBounds(obj, prop); + let index = Number.parseInt(prop); + if (index >= 0 && Number.isInteger(index)) { return Object.getOwnPropertyDescriptor(obj, prop); } - return; + return } setPrototypeOf(obj: any, prop: any): any { - throw new Error("Can setPrototype on Deque Object"); + throw new RangeError("Can setPrototype on Deque Object"); } } interface IterableIterator { @@ -93,7 +94,7 @@ if (flag || fastDeque == undefined) { this.rear = 0; return new Proxy(this, new HandlerDeque()); } - get length(){ + get length() { let result = (this.rear - this.front + this.capacity) % this.capacity; return result; } @@ -112,9 +113,15 @@ if (flag || fastDeque == undefined) { this.rear = (this.rear + 1) % (this.capacity + 1); } getFirst(): T { + if (this.isEmpty()) { + return undefined; + } return this[this.front]; } getLast(): T { + if (this.isEmpty()) { + return undefined; + } return this[this.rear - 1]; } has(element: T): boolean { @@ -127,11 +134,17 @@ if (flag || fastDeque == undefined) { return result; } popFirst(): T { + if (this.isEmpty()) { + return undefined; + } let result = this[this.front]; this.front = (this.front + 1) % (this.capacity + 1); return result; } popLast(): T { + if (this.isEmpty()) { + return undefined; + } let result = this[this.rear - 1]; this.rear = (this.rear + this.capacity) % (this.capacity + 1); return result; @@ -170,13 +183,16 @@ if (flag || fastDeque == undefined) { private isFull(): boolean { return (this.rear + 1) % this.capacity === this.front; } + private isEmpty(): boolean { + return this.length == 0; + } [Symbol.iterator](): IterableIterator { let deque = this; let count = deque.front; return { next: function () { - var done = count == deque.rear; - var value = !done ? deque[count] : undefined; + let done = count == deque.rear; + let value = !done ? deque[count] : undefined; count = (count + 1) % deque.capacity; return { done: done, diff --git a/container/deque/native_module_deque.cpp b/container/deque/native_module_deque.cpp index 70eea6ce1768ea506be5ea4eac639fe0d76e4259..246d6bff48c6de6709daa498f2d0dfb219ea1693 100644 --- a/container/deque/native_module_deque.cpp +++ b/container/deque/native_module_deque.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021 Huawei Device Co., Ltd. + * Copyright (c) 2022 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at diff --git a/container/hashmap/js_hashmap.ts b/container/hashmap/js_hashmap.ts index 542aad4d4d63d15b5adc05da031036fbccd6bd28..ffb3e861c2570c55b689907c982625db478fa5ee 100644 --- a/container/hashmap/js_hashmap.ts +++ b/container/hashmap/js_hashmap.ts @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021 Huawei Device Co., Ltd. + * Copyright (c) 2022 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at @@ -70,6 +70,9 @@ if (flag || fastHashMap === undefined) { return this.getValueByKey(key); } setAll(map: HashMap): void { + if(!(map instanceof HashMap)) { + throw new TypeError("Incoming object is not JSAPIHashMap"); + } let memebers = map.keyValueArray; for (let i = 0; i < memebers.length; i++) { this.put(memebers[i].key, memebers[i].value); @@ -80,9 +83,6 @@ if (flag || fastHashMap === undefined) { } remove(key: K): V { let result = this.removeMember(key); - if (result === undefined) { - throw new Error("The removed element does not exist in this container"); - } return result; } clear(): void { @@ -93,8 +93,8 @@ if (flag || fastHashMap === undefined) { let count = 0; return { next: function () { - var done = count >= data.memberNumber; - var value = !done ? data.keyValueArray[count].key : undefined; + let done = count >= data.memberNumber; + let value = !done ? data.keyValueArray[count].key : undefined; count++; return { done: done, @@ -108,8 +108,8 @@ if (flag || fastHashMap === undefined) { let count = 0; return { next: function () { - var done = count >= data.memberNumber; - var value = !done ? data.keyValueArray[count].value : undefined; + let done = count >= data.memberNumber; + let value = !done ? data.keyValueArray[count].value : undefined; count++; return { done: done, @@ -133,8 +133,8 @@ if (flag || fastHashMap === undefined) { let count = 0; return { next: function () { - var done = count >= data.memberNumber; - var value = !done ? data.keyValueArray[count].entry() : undefined; + let done = count >= data.memberNumber; + let value = !done ? data.keyValueArray[count].entry() : undefined; count++; return { done: done, @@ -148,8 +148,8 @@ if (flag || fastHashMap === undefined) { let count = 0; return { next: function () { - var done = count >= data.memberNumber; - var value = !done ? data.keyValueArray[count].entry() : undefined; + let done = count >= data.memberNumber; + let value = !done ? data.keyValueArray[count].entry() : undefined; count++; return { done: done, diff --git a/container/hashmap/native_module_hashmap.cpp b/container/hashmap/native_module_hashmap.cpp index 4892c1ca900acfc8a29e34d6c5d15b63021c7eb3..d2d0058470fc36bd97b20baa4b56cb5b48370fc3 100644 --- a/container/hashmap/native_module_hashmap.cpp +++ b/container/hashmap/native_module_hashmap.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021 Huawei Device Co., Ltd. + * Copyright (c) 2022 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at diff --git a/container/hashset/js_hashset.ts b/container/hashset/js_hashset.ts index 394affff5ab1e11f82e5bea58dd80c2fbc6c41a6..880f33d07495c8403a0ded9b3ff919e653146cd4 100644 --- a/container/hashset/js_hashset.ts +++ b/container/hashset/js_hashset.ts @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021 Huawei Device Co., Ltd. + * Copyright (c) 2022 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at @@ -87,8 +87,8 @@ if (flag || fastHashSet === undefined) { let count = 0; return { next: function () { - var done = count >= data.memberNumber; - var value = !done ? data.keyValueArray[count].key : undefined; + let done = count >= data.memberNumber; + let value = !done ? data.keyValueArray[count].key : undefined; count++; return { done: done, @@ -102,8 +102,8 @@ if (flag || fastHashSet === undefined) { let count = 0; return { next: function () { - var done = count >= data.memberNumber; - var value = !done ? data.keyValueArray[count].entry() : undefined; + let done = count >= data.memberNumber; + let value = !done ? data.keyValueArray[count].entry() : undefined; count++; return { done: done, @@ -117,8 +117,8 @@ if (flag || fastHashSet === undefined) { let count = 0; return { next: function () { - var done = count >= data.memberNumber; - var value = !done ? data.keyValueArray[count].key : undefined; + let done = count >= data.memberNumber; + let value = !done ? data.keyValueArray[count].key : undefined; count++; return { done: done, diff --git a/container/hashset/native_module_hashset.cpp b/container/hashset/native_module_hashset.cpp index c5d9cdf0f407ea76df485651bf1dbfef1a3d2e8d..6c1c48579da52e52053d4d6aa1a97819ecfb5d26 100644 --- a/container/hashset/native_module_hashset.cpp +++ b/container/hashset/native_module_hashset.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021 Huawei Device Co., Ltd. + * Copyright (c) 2022 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at diff --git a/container/lightweightmap/js_lightweightmap.ts b/container/lightweightmap/js_lightweightmap.ts index 72ba6c1b1bc178b805611065620c60d05b318d99..f8be0e56275a0b519aad8a0b83aa6d54bf432878 100644 --- a/container/lightweightmap/js_lightweightmap.ts +++ b/container/lightweightmap/js_lightweightmap.ts @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021 Huawei Device Co., Ltd. + * Copyright (c) 2022 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at @@ -58,6 +58,9 @@ if (flag || fastLightWeightMap === undefined) { return this.memberNumber; } hasAll(map: LightWeightMap): boolean { + if(!(map instanceof LightWeightMap)) { + throw new TypeError("map is not JSAPILightWeightMap"); + } if (map.memberNumber > this.memberNumber) return false; if (LightWeightAbility.isIncludeToArray(this.keyValueStringArray(), map.keyValueStringArray()) ) { return true; @@ -71,6 +74,9 @@ if (flag || fastLightWeightMap === undefined) { return this.members.values.indexOf(value) > -1; } increaseCapacityTo(minimumCapacity: number): void { + if (typeof minimumCapacity !== "number") { + throw new TypeError("the size is not integer"); + } super.ensureCapacity(minimumCapacity); } entries(): IterableIterator<[K, V]> { @@ -78,8 +84,8 @@ if (flag || fastLightWeightMap === undefined) { let count = 0; return { next: function () { - var done = count >= data.memberNumber; - var value = !done ? [data.members.keys[count], data.members.values[count]] as [K, V] : undefined; + let done = count >= data.memberNumber; + let value = !done ? [data.members.keys[count], data.members.values[count]] as [K, V] : undefined; count++; return { done: done, @@ -102,6 +108,9 @@ if (flag || fastLightWeightMap === undefined) { return this.memberNumber === 0; } getKeyAt(index: number): K { + if (typeof index !== "number") { + throw new TypeError("the index is not integer"); + } return this.members.keys[index]; } keys(): IterableIterator { @@ -109,8 +118,8 @@ if (flag || fastLightWeightMap === undefined) { let count = 0; return { next: function () { - var done = count >= data.memberNumber; - var value = !done ? data.members.keys[count] : undefined; + let done = count >= data.memberNumber; + let value = !done ? data.members.keys[count] : undefined; count++; return { done: done, @@ -120,6 +129,9 @@ if (flag || fastLightWeightMap === undefined) { }; } setAll(map: LightWeightMap): void { + if(!(map instanceof LightWeightMap)) { + throw new TypeError("Incoming object is not JSAPILightWeightMap"); + } if (this.memberNumber === 0) { this.members.hashs = map.members.hashs.slice(); this.members.keys = map.members.keys.slice(); @@ -172,8 +184,8 @@ if (flag || fastLightWeightMap === undefined) { let count = 0; return { next: function () { - var done = count >= data.memberNumber; - var value = !done ? [data.members.keys[count], data.members.values[count]] as [K, V] : undefined; + let done = count >= data.memberNumber; + let value = !done ? [data.members.keys[count], data.members.values[count]] as [K, V] : undefined; count++; return { done: done, @@ -197,8 +209,8 @@ if (flag || fastLightWeightMap === undefined) { let count = 0; return { next: function () { - var done = count >= data.memberNumber; - var value = !done ? data.members.values[count] : undefined; + let done = count >= data.memberNumber; + let value = !done ? data.members.values[count] : undefined; count++; return { done: done, diff --git a/container/lightweightmap/native_module_lightweightmap.cpp b/container/lightweightmap/native_module_lightweightmap.cpp index d828099982763d97c69837b307b4ef960f5e3fcc..e24270c272e19f72efe0fa4d06ec6f26dd46120b 100644 --- a/container/lightweightmap/native_module_lightweightmap.cpp +++ b/container/lightweightmap/native_module_lightweightmap.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021 Huawei Device Co., Ltd. + * Copyright (c) 2022 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at diff --git a/container/lightweightset/js_lightweightset.ts b/container/lightweightset/js_lightweightset.ts index 46c1e19a0ee06ac390fbded06826db6369b7820f..9e39294da3da896359d653bfee9e89c4e43fb0b5 100644 --- a/container/lightweightset/js_lightweightset.ts +++ b/container/lightweightset/js_lightweightset.ts @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021 Huawei Device Co., Ltd. + * Copyright (c) 2022 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at @@ -63,6 +63,9 @@ if (flag || fastLightWeightSet === undefined) { return true; } addAll(set: LightWeightSet): boolean { + if(!(set instanceof LightWeightSet)) { + throw new TypeError("Incoming object is not JSAPILightWeightSet"); + } let change = false; if (set.memberNumber == 0) { change = false; @@ -85,6 +88,8 @@ if (flag || fastLightWeightSet === undefined) { } equal(obj: Object): boolean { if (this.memberNumber === 0) return false; + if(obj instanceof LightWeightSet) + return JSON.stringify(obj.members.keys) === JSON.stringify(this.members.keys); if (JSON.stringify(obj) === JSON.stringify(this.members.keys)) return true; return false; } @@ -129,8 +134,8 @@ if (flag || fastLightWeightSet === undefined) { let count = 0; return { next: function () { - var done = count >= data.memberNumber; - var value = !done ? data.members.keys[count] : undefined; + let done = count >= data.memberNumber; + let value = !done ? data.members.keys[count] : undefined; count++; return { done: done, @@ -156,9 +161,9 @@ if (flag || fastLightWeightSet === undefined) { let count = 0; return { next: function () { - var done = count >= data.memberNumber; - var tempValue = data.members.keys[count]; - var value = !done ? ([tempValue, tempValue] as [T, T]) : undefined; + let done = count >= data.memberNumber; + let tempValue = data.members.keys[count]; + let value = !done ? ([tempValue, tempValue] as [T, T]) : undefined; count++; return { done: done, diff --git a/container/lightweightset/native_module_lightweightset.cpp b/container/lightweightset/native_module_lightweightset.cpp index 6d427d0aed8b611c7b01b0ccdbd61bd6cb96337c..0d84dbb222c321dcdadc7d4c1cfcd5bd06397354 100644 --- a/container/lightweightset/native_module_lightweightset.cpp +++ b/container/lightweightset/native_module_lightweightset.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021 Huawei Device Co., Ltd. + * Copyright (c) 2022 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at diff --git a/container/linkedlist/js_linkedlist.ts b/container/linkedlist/js_linkedlist.ts index 9f33e5bab224c123560aa6929c8b4d38e14a3a20..3a51a94a3e74b405986b4aba7aa1f9129c37604a 100644 --- a/container/linkedlist/js_linkedlist.ts +++ b/container/linkedlist/js_linkedlist.ts @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021 Huawei Device Co., Ltd. + * Copyright (c) 2022 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at @@ -26,11 +26,11 @@ if (flag || fastLinkedList == undefined) { if (typeof prop === "symbol") { return obj[prop]; } - var index = Number.parseInt(prop); + let index = Number.parseInt(prop); let length = obj.length; if (Number.isInteger(index)) { if (index < 0 || index >= length) { - throw new Error("LinkedList: get out-of-bounds"); + throw new RangeError("the index is out-of-bounds"); } return obj.get(index); } @@ -38,18 +38,18 @@ if (flag || fastLinkedList == undefined) { } set(obj: LinkedList, prop: any, value: any) { if (prop === "elementNum" || - prop === "capacity" || - prop === "head" || - prop == "next" || - prop == "tail" ) { + prop === "capacity" || + prop === "head" || + prop == "next" || + prop == "tail") { obj[prop] = value; return true; } - var index = Number.parseInt(prop); + let 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"); + throw new RangeError("the index is out-of-bounds"); } else { obj.set(index, value); return true; @@ -58,11 +58,11 @@ if (flag || fastLinkedList == undefined) { return false; } deleteProperty(obj: LinkedList, prop: any) { - var index = Number.parseInt(prop); + let index = Number.parseInt(prop); if (Number.isInteger(index)) { let length = obj.length; if (index < 0 || index >= length) { - throw new Error("LinkedList: deleteProperty out-of-bounds"); + throw new RangeError("the index is out-of-bounds"); } obj.removeByIndex(index); return true; @@ -73,9 +73,9 @@ if (flag || fastLinkedList == undefined) { return obj.has(prop); } ownKeys(obj: LinkedList) { - var keys = []; + let keys = []; let length = obj.length; - for (var i = 0; i < length; i++) { + for (let i = 0; i < length; i++) { keys.push(i.toString()); } return keys; @@ -84,18 +84,18 @@ if (flag || fastLinkedList == undefined) { return true; } getOwnPropertyDescriptor(obj: LinkedList, prop: any) { - var index = Number.parseInt(prop); + let index = Number.parseInt(prop); if (Number.isInteger(index)) { let length = obj.length; if (index < 0 || index >= length) { - throw new Error("LinkedList: getOwnPropertyDescriptor out-of-bounds"); + throw new RangeError("the index is out-of-bounds"); } return Object.getOwnPropertyDescriptor(obj, prop); } return; } setPrototypeOf(obj: any, prop: any): any { - throw new Error("Can setPrototype on LinkedList Object"); + throw new Error("Can setPrototype on LinkedList Object"); } } interface IterableIterator { @@ -105,52 +105,27 @@ if (flag || fastLinkedList == undefined) { }; } class NodeObj { - /* If 'any' is changed to 'T' here, an error will be reported: - Type 'unknown' cannot be assigned to type 'T'. */ - element: any; - next?: NodeObj | null; - prev?: NodeObj | null; - constructor( - element: any, - next?: NodeObj | null, - prev?: NodeObj | null - ) { + element: T; + next?: NodeObj; + prev?: NodeObj; + constructor(element: T, next?: NodeObj, prev?: NodeObj) { this.element = element; this.next = next; this.prev = prev; } } - class LinkIterator { - /* If 'any' is changed to 'T' here, an error will be reported: - Property 'next' does not exist on type 'T' */ - private linkNode: any; - constructor(linkNode: any) { - this.linkNode = linkNode; - } - hasNext(): boolean { - if (this.linkNode.next !== null) { - return true; - } else { - return false; - } - } - next(): NodeObj { - this.linkNode = this.linkNode.next; - return this.linkNode; - } - prev(): NodeObj { - this.linkNode = this.linkNode.prev; - return this.linkNode; - } - } class LinkedList { - private head?: any; - private tail?: any; - private elementNum : number; + private head?: NodeObj; + private tail?: NodeObj; + private elementNum: number; private capacity: number; - constructor(head?: NodeObj, tail?: NodeObj) { - this.head = head || null; - this.tail = tail || null; + private next?: NodeObj; + private prev?: NodeObj; + constructor() { + this.head = undefined; + this.tail = undefined; + this.next = undefined; + this.prev = undefined; this.elementNum = 0; this.capacity = 10; return new Proxy(this, new HandlerLinkedList()); @@ -158,37 +133,44 @@ if (flag || fastLinkedList == undefined) { get length() { return this.elementNum; } - private getNode(index: number): NodeObj { - let current = this.head; - for (let i = 0; i < index; i++) { - current = current["next"]; + private getNode(index: number): NodeObj | undefined { + if (index >= 0 && index < this.elementNum) { + let current = this.head; + for (let i = 0; i < index; i++) { + if (current !== undefined) { + current = current.next; + } + } + return current; } - return current; + return undefined; } get(index: number): T { - let current = this.head; - for (let i = 0; i < index; i++) { - current = current["next"]; + if (index >= 0 && index < this.elementNum) { + let current = this.head; + for (let i = 0; i < index && current != undefined; i++) { + current = current.next; + } + return current.element; } - return current.element; + return undefined; } add(element: T): boolean { - if (this.elementNum === 0) { - let head = this.head; - let tail = this.tail; - this.head = this.tail = new NodeObj(element, head, tail); + let node = new NodeObj(element); + if (this.head == undefined) { + this.head = this.tail = node; } else { - let prevNode = this.getNode(this.elementNum - 1); - prevNode.next = new NodeObj(element, prevNode["next"], this.tail); + let current = this.head; + while (current.next !== undefined) { + current = current.next; + } + this.tail = current.next = node; } 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); + let node = new NodeObj(element); if (this.elementNum === 0) { this.head = this.tail = node; } else { @@ -198,47 +180,45 @@ if (flag || fastLinkedList == undefined) { this.elementNum++; } removeFirst(): T { - let result = this.getNode(0).element; - this.removeByIndex(0); - return result; - } - popFirst(): T { - let result = this.getNode(0).element; - this.removeByIndex(0); - return result; - } - popLast(): T { - let result = this.getNode(this.elementNum - 1).element; - this.removeByIndex(this.elementNum - 1); - return result; + if (this.head !== undefined) { + let result = this.head.element; + this.removeByIndex(0); + return result; + } + return undefined; } removeLast(): T { - let result = this.getNode(this.elementNum - 1).element; - this.removeByIndex(this.elementNum - 1); - return result; + if (this.tail !== undefined) { + let result = this.tail.element; + this.removeByIndex(this.elementNum - 1); + return result; + } + return undefined; } clear(): void { - this.head = null; - this.tail = null; + this.head = undefined; + this.tail = undefined; this.elementNum = 0; } has(element: T): boolean { - if (this.head.element === element) { - return true; - } - const linkIterator = new LinkIterator(this.head); - while (linkIterator.hasNext()) { - const newNode = linkIterator.next(); - if (newNode.element === element) { + if (this.head !== undefined) { + if (this.head.element === element) { return true; } + let current = this.head; + while (current.next != undefined) { + current = current.next; + if (current.element === element) { + return true; + } + } } return false; } getIndexOf(element: T): number { for (let i = 0; i < this.elementNum; i++) { const curNode = this.getNode(i); - if (curNode.element === element) { + if (curNode !== undefined && curNode.element === element) { return i; } } @@ -247,39 +227,45 @@ if (flag || fastLinkedList == undefined) { getLastIndexOf(element: T): number { for (let i = this.elementNum - 1; i >= 0; i--) { const curNode = this.getNode(i); - if (curNode.element === element) { + if (curNode !== undefined && curNode.element === element) { return i; } } return -1; } removeByIndex(index: number): T { - 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.elementNum == 1) { - this.head = this.tail = null; + if (index >= 0 && index < this.elementNum) { + let current = this.head; + if (index === 0 && current !== undefined) { + this.head = current.next; + this.head.prev = undefined; + if (this.elementNum == 1) { + this.head = this.tail = undefined; + } + } else if (index == this.elementNum - 1) { + current = this.getNode(index - 1); + if (current !== undefined) { + this.tail = current; + current.next = undefined; + } } else { - this.head.prev = null; + const prevNode = this.getNode(index - 1); + const nextNode = this.getNode(index + 1); + if (prevNode !== undefined && nextNode !== undefined) { + prevNode.next = nextNode; + nextNode.prev = prevNode; + } + } + if (current !== undefined) { + this.elementNum--; + return current.element; } - } else if (index == this.elementNum - 1) { - current = this.getNode(index - 1); - this.tail = current; - current.next = null; } else { - const prevNode = this.getNode(index - 1); - const nextNode = this.getNode(index + 1); - prevNode.next = nextNode; - nextNode.prev = prevNode; + throw new RangeError("the index is out-of-bounds"); } - this.elementNum--; - return current && current.element; } remove(element: T): boolean { - if(this.isEmpty()){ + if (this.isEmpty()) { return false; } if (this.has(element)) { @@ -294,42 +280,44 @@ if (flag || fastLinkedList == undefined) { let index = this.getIndexOf(element); this.removeByIndex(index); return true; - } else { - return false; } + return false; } removeLastFound(element: T): boolean { if (this.has(element)) { let index = this.getLastIndexOf(element); this.removeByIndex(index); return true; - } else { - return false; } + return false; } getFirst(): T { - let newNode = this.getNode(0); - let element = newNode.element; - return element; + if (this.head !== undefined) { + return this.head.element; + } + return undefined; } getLast(): T { - let newNode = this.getNode(this.elementNum - 1); - let element = newNode.element; - return element; + if (this.tail !== undefined) { + return this.tail.element; + } + return undefined; } insert(index: number, element: T): void { - if (index >= 0 && index < this.elementNum) { + if (index >= 0 && index <= this.elementNum) { let newNode = new NodeObj(element); let current = this.head; if (index === 0) { - if (this.head === null) { - this.head = newNode; - this.tail = newNode; + if (this.head === undefined) { + this.head = this.tail = newNode; } else { newNode.next = this.head; this.head.prev = newNode; this.head = newNode; } + } else if (index === this.elementNum && this.elementNum !== 0) { + let prevNode = this.getNode(this.elementNum - 1); + prevNode.next = this.tail = newNode; } else { const prevNode = this.getNode(index - 1); current = prevNode.next; @@ -338,18 +326,15 @@ if (flag || fastLinkedList == undefined) { current.prev = newNode; newNode.prev = prevNode; } - } 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"); + throw new RangeError("the index is out-of-bounds"); } this.elementNum++; } set(index: number, element: T): T { const current = this.getNode(index); current.element = element; - return current && current.element; + return current.element; } convertToArray(): Array { let arr: Array = []; @@ -357,11 +342,13 @@ if (flag || fastLinkedList == undefined) { if (this.elementNum <= 0) { return arr; } - arr[index] = this.head.element; - const linkIterator = new LinkIterator(this.head); - while (linkIterator.hasNext()) { - const newNode = linkIterator.next(); - arr[++index] = newNode.element; + if (this.head !== undefined) { + let current = this.head; + arr[index] = this.head.element; + while (current.next != undefined) { + current = current.next; + arr[++index] = current.element; + } } return arr; } @@ -369,7 +356,7 @@ if (flag || fastLinkedList == undefined) { let clone = new LinkedList(); let arr = this.convertToArray(); for (let i = 0; i < arr.length; i++) { - let item = arr[i] + let item = arr[i]; clone.add(item); } return clone; @@ -377,16 +364,18 @@ if (flag || fastLinkedList == undefined) { private isEmpty(): boolean { return this.elementNum == 0; } - forEach(callbackfn: (value: T,index?: number,linkedlist?: LinkedList) => void, + forEach(callbackfn: (value: T, index?: number, linkedList?: LinkedList) => void, thisArg?: Object): void { let index = 0; - 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(); - callbackfn.call(thisArg, newNode.element, ++index, this); + if (this.head !== undefined) { + let current = this.head; + if (this.elementNum > 0) { + callbackfn.call(thisArg, this.head.element, index, this); + } + while (current.next != undefined) { + current = current.next; + callbackfn.call(thisArg, current.element, ++index, this); + } } } [Symbol.iterator](): IterableIterator { @@ -394,8 +383,8 @@ if (flag || fastLinkedList == undefined) { let linkedlist = this; return { next: function () { - var done = count >= linkedlist.elementNum; - var value = !done ? linkedlist.getNode(count++).element : undefined; + let done = count >= linkedlist.elementNum; + let value = !done ? linkedlist.getNode(count++).element : undefined; return { done: done, value: value, diff --git a/container/linkedlist/native_module_linkedlist.cpp b/container/linkedlist/native_module_linkedlist.cpp index b15a45afd29f7e691e2aa3f6402ca9a0150ece07..98ad00123a43f491b9934629f8773477ea00c496 100644 --- a/container/linkedlist/native_module_linkedlist.cpp +++ b/container/linkedlist/native_module_linkedlist.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021 Huawei Device Co., Ltd. + * Copyright (c) 2022 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at diff --git a/container/list/js_list.ts b/container/list/js_list.ts index 1aa704b5e10541d3329c2b442077d7c6e5cb3e3f..45703e6426119f33a250629739b7d76313e53e52 100644 --- a/container/list/js_list.ts +++ b/container/list/js_list.ts @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021 Huawei Device Co., Ltd. + * Copyright (c) 2022 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at @@ -26,28 +26,27 @@ if (flag || fastList == undefined) { if (typeof prop === "symbol") { return obj[prop]; } - var index = Number.parseInt(prop); + let index = Number.parseInt(prop); if (Number.isInteger(index)) { if (index < 0 || index >= obj.length) { - throw new Error("List: get out-of-bounds"); + throw new RangeError("the index is out-of-bounds"); } return obj.get(index); } return obj[prop]; } set(obj: List, prop: any, value: T) { - if ( - prop === "elementNum" || + if (prop === "elementNum" || prop === "capacity" || prop === "head" || prop == "next") { obj[prop] = value; return true; } - var index = Number.parseInt(prop); + let index = Number.parseInt(prop); if (Number.isInteger(index)) { if (index < 0 || index >= obj.length) { - throw new Error("List: set out-of-bounds"); + throw new RangeError("the index is out-of-bounds"); } else { obj.set(index, value); return true; @@ -56,10 +55,10 @@ if (flag || fastList == undefined) { return false; } deleteProperty(obj: List, prop: any) { - var index = Number.parseInt(prop); + let index = Number.parseInt(prop); if (Number.isInteger(index)) { if (index < 0 || index >= obj.length) { - throw new Error("List: deleteProperty out-of-bounds"); + throw new RangeError("the index is out-of-bounds"); } obj.removeByIndex(index); return true; @@ -70,8 +69,8 @@ if (flag || fastList == undefined) { return obj.has(prop); } ownKeys(obj: List) { - var keys = []; - for (var i = 0; i < obj.length; i++) { + let keys = []; + for (let i = 0; i < obj.length; i++) { keys.push(i.toString()); } return keys; @@ -80,17 +79,17 @@ if (flag || fastList == undefined) { return true; } getOwnPropertyDescriptor(obj: List, prop: any) { - var index = Number.parseInt(prop); + let index = Number.parseInt(prop); if (Number.isInteger(index)) { if (index < 0 || index >= obj.length) { - throw new Error("List: getOwnPropertyDescriptor out-of-bounds"); + throw new RangeError("the index is out-of-bounds"); } return Object.getOwnPropertyDescriptor(obj, prop); } return; } setPrototypeOf(obj: any, prop: any): any { - throw new Error("Can setPrototype on List Object"); + throw new Error("Can setPrototype on List Object"); } } interface IterableIterator { @@ -100,41 +99,21 @@ if (flag || fastList == undefined) { }; } class NodeObj { - /* If 'any' is changed to 'T' here, an error will be reported: - Type 'unknown' cannot be assigned to type 'T'. */ - element: any; - next?: NodeObj | null; - constructor(element: any, next?: NodeObj | null) { + element: T; + next?: NodeObj; + constructor(element: T, next?: NodeObj) { this.element = element; this.next = next; } } - class LinkIterator { - /* If 'any' is changed to 'T' here, an error will be reported: - Property 'next' does not exist on type 'T' */ - private linkNode: any; - constructor(linkNode: any) { - this.linkNode = linkNode; - } - hasNext(): boolean { - if (this.linkNode.next !== null) { - return true; - } - return false; - } - next(): NodeObj { - this.linkNode = this.linkNode.next; - return this.linkNode; - } - } class List { - /* If 'any' is changed to 'NodeObj | null ' here, an error will be reported: - Object may be 'null' */ - private head: any; + private head: NodeObj; private elementNum: number; private capacity: number; - constructor(head?: NodeObj) { - this.head = head || null; + private next?: NodeObj; + constructor() { + this.head = undefined; + this.next = undefined; this.elementNum = 0; this.capacity = 10; return new Proxy(this, new HandlerList()); @@ -142,45 +121,58 @@ if (flag || fastList == undefined) { get length() { return this.elementNum; } - private getNode(index: number): NodeObj { - let current = this.head; - for (let i = 0; i < index; i++) { - current = current["next"]; + private getNode(index: number): NodeObj | undefined { + if (index >= 0 && index < this.elementNum) { + let current = this.head; + for (let i = 0; i < index; i++) { + if (current !== undefined) { + current = current.next; + } + } + return current; } - return current; + return undefined; } get(index: number): T { - let current = this.head; - for (let i = 0; i < index; i++) { - current = current["next"]; + if (index >= 0 && index < this.elementNum) { + let current = this.head; + for (let i = 0; i < index && current != undefined; i++) { + current = current.next; + } + return current.element; } - return current.element; + return undefined; } add(element: T): boolean { - if (this.elementNum === 0) { - let head = this.head; - this.head = new NodeObj(element, head); + let node = new NodeObj(element); + if (this.head == undefined) { + this.head = node; } else { - let prevNode = this.getNode(this.elementNum - 1); - prevNode.next = new NodeObj(element, prevNode["next"]); + let current = this.head; + while (current.next !== undefined) { + current = current.next; + } + current.next = node; } this.elementNum++; return true; } clear(): void { - this.head = null; + this.head = undefined; this.elementNum = 0; } has(element: T): boolean { - if (this.head.element === element) { - return true; - } - const linkIterator = new LinkIterator(this.head); - while (linkIterator.hasNext()) { - const newNode = linkIterator.next(); - if (newNode.element === element) { + if (this.head !== undefined) { + if (this.head.element === element) { return true; } + let current = this.head; + while (current.next != undefined) { + current = current.next; + if (current.element === element) { + return true; + } + } } return false; } @@ -191,23 +183,30 @@ if (flag || fastList == undefined) { if (!(obj instanceof List)) { return false; } else { - 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(); - if (newNode1.element !== newNode2.element) { - return false; + let e1 = this.head; + let e2 = obj.head; + if (e1 !== undefined && e2 !== undefined) { + while (e1.next !== undefined && e2.next !== undefined) { + e1 = e1.next; + e2 = e2.next; + if (e1.element !== e2.element) { + return false; + } } + return !(e1.next !== undefined || e2.next !== undefined); + } else if (e1 !== undefined && e2 === undefined) { + return false; + } else if (e1 === undefined && e2 !== undefined) { + return false; + } else { + return true; } - return !(e1.hasNext() || e2.hasNext()); } - return true; } getIndexOf(element: T): number { for (let i = 0; i < this.elementNum; i++) { const curNode = this.getNode(i); - if (curNode.element === element) { + if (curNode !== undefined && curNode.element === element) { return i; } } @@ -216,7 +215,7 @@ if (flag || fastList == undefined) { getLastIndexOf(element: T): number { for (let i = this.elementNum - 1; i >= 0; i--) { const curNode = this.getNode(i); - if (curNode.element === element) { + if (curNode !== undefined && curNode.element === element) { return i; } } @@ -224,7 +223,7 @@ if (flag || fastList == undefined) { } removeByIndex(index: number): T { if (index < 0 || index >= this.elementNum) { - throw new Error("removeByIndex is out-of-bounds"); + throw new RangeError("the index is out-of-bounds"); } let oldNode = this.head; if (index === 0) { @@ -249,27 +248,36 @@ 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.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(); - this.getNode(++index).element = callbackfn.call(thisArg, newNode.element, index, this); + if (this.head !== undefined) { + let current = this.head; + if (this.elementNum > 0) { + this.getNode(index).element = callbackfn.call(thisArg, this.head.element, index, this); + } + while (current.next != undefined) { + current = current.next; + this.getNode(++index).element = callbackfn.call(thisArg, current.element, index, this); + } } } getFirst(): T { - let newNode = this.getNode(0); - let element = newNode.element; + if (this.isEmpty()) { + return undefined; + } + let element = this.head.element; return element; } getLast(): T { + if (this.isEmpty()) { + return undefined; + } let newNode = this.getNode(this.elementNum - 1); let element = newNode.element; return element; } insert(element: T, index: number): void { + if (index < 0 || index >= this.elementNum) { + throw new RangeError("the index is out-of-bounds"); + } let newNode = new NodeObj(element); if (index >= 0 && index < this.elementNum) { if (index === 0) { @@ -285,15 +293,20 @@ if (flag || fastList == undefined) { } } set(index: number, element: T): T { + if (index < 0 || index >= this.elementNum) { + throw new RangeError("the index is out-of-bounds"); + } const current = this.getNode(index); current.element = element; - return current && current.element; + return current.element; } sort(comparator: (firstValue: T, secondValue: T) => number): void { let isSort = true; 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) { + if ( + comparator(this.getNode(j).element, this.getNode(j + 1).element) > 0 + ) { isSort = false; let temp = this.getNode(j).element; this.getNode(j).element = this.getNode(j + 1).element; @@ -307,17 +320,17 @@ if (flag || fastList == undefined) { } getSubList(fromIndex: number, toIndex: number): List { if (toIndex <= fromIndex) { - throw new Error("toIndex cannot be less than or equal to fromIndex"); + throw new RangeError(`the toIndex cannot be less than or equal to fromIndex`); } if (fromIndex >= this.elementNum || fromIndex < 0 || toIndex < 0) { - throw new Error(`fromIndex or toIndex is out-of-bounds`); + throw new RangeError(`the fromIndex or the toIndex is out-of-bounds`); } - toIndex = toIndex > this.elementNum ? this.elementNum - 1 : toIndex; + toIndex = toIndex >= this.elementNum ? this.elementNum : toIndex; let list = new List(); for (let i = fromIndex; i < toIndex; i++) { let element = this.getNode(i).element; list.add(element); - if (element === null) { + if (element === undefined) { break; } } @@ -329,11 +342,13 @@ if (flag || fastList == undefined) { if (this.elementNum <= 0) { return arr; } - arr[index] = this.head.element; - const linkIterator = new LinkIterator(this.head); - while (linkIterator.hasNext()) { - const newNode = linkIterator.next(); - arr[++index] = newNode.element; + if (this.head !== undefined) { + let current = this.head; + arr[index] = this.head.element; + while (current.next != undefined) { + current = current.next; + arr[++index] = current.element; + } } return arr; } @@ -343,14 +358,15 @@ if (flag || fastList == undefined) { forEach(callbackfn: (value: T, index?: number, list?: List) => void, thisArg?: Object): void { let index = 0; - 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(); - callbackfn.call(thisArg, newNode.element, ++index, this); + if (this.head !== undefined) { + let current = this.head; + if (this.elementNum > 0) { + callbackfn.call(thisArg, this.head.element, index, this); + } + while (current.next != undefined) { + current = current.next; + callbackfn.call(thisArg, current.element, ++index, this); + } } } [Symbol.iterator](): IterableIterator { @@ -358,8 +374,8 @@ if (flag || fastList == undefined) { let list = this; return { next: function () { - var done = count >= list.elementNum; - var value = !done ? list.getNode(count++).element : undefined; + let done = count >= list.elementNum; + let value = !done ? list.getNode(count++).element : undefined; return { done: done, value: value, @@ -371,4 +387,4 @@ if (flag || fastList == undefined) { Object.freeze(List); fastList = List; } -export default fastList; \ No newline at end of file +export default fastList; diff --git a/container/list/native_module_list.cpp b/container/list/native_module_list.cpp index 379e8e4f66fd4a6e58a460d59f6a510645fd4161..0ffbde0b14e533cbba32d22745b74ca6209f100a 100644 --- a/container/list/native_module_list.cpp +++ b/container/list/native_module_list.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021 Huawei Device Co., Ltd. + * Copyright (c) 2022 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at diff --git a/container/plainarray/js_plainarray.ts b/container/plainarray/js_plainarray.ts index 6e0ae1a2dfefd24f68ec6b06e50af963aa9da629..5c38c4d85a6aeee3b02e644aaa165fc70241c1e1 100644 --- a/container/plainarray/js_plainarray.ts +++ b/container/plainarray/js_plainarray.ts @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021 Huawei Device Co., Ltd. + * Copyright (c) 2022 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at @@ -59,7 +59,7 @@ if (flag || fastPlainArray === undefined) { } add(key: number, value: T): void { if (typeof key !== "number") { - throw new Error("PlainArray's only number is allowed"); + throw new TypeError("the index is not integer"); } this.addmember(key, value); } @@ -78,13 +78,22 @@ if (flag || fastPlainArray === undefined) { return clone; } has(key: number): boolean { + if (typeof key !== "number") { + return false; + } return this.binarySearchAtPlain(key) > -1; } get(key: number): T { + if (typeof key !== "number") { + throw new TypeError("the index is not integer"); + } let index = this.binarySearchAtPlain(key); return this.members.values[index]; } getIndexOfKey(key: number): number { + if (typeof key !== "number") { + throw new TypeError("the index is not integer"); + } let result = this.binarySearchAtPlain(key); return result < 0 ? -1 : result; } @@ -95,28 +104,45 @@ if (flag || fastPlainArray === undefined) { return this.memberNumber === 0; } getKeyAt(index: number): number { + if (typeof index !== "number") { + throw new TypeError("the index is not integer"); + } return this.members.keys[index]; } remove(key: number): T { + let result: any = undefined; + if (typeof key !== "number") { + throw new TypeError("the index is not integer"); + } let index = this.binarySearchAtPlain(key); - if (index < 0) throw new Error("element not in this plainarray"); + if (index < 0) return result; return this.deletemember(index); } removeAt(index: number): T { - if (index >= this.memberNumber || index < 0) throw new Error("index not in this plainarray range"); + if (typeof index !== "number") { + throw new TypeError("the index is not integer"); + } + let result: any = undefined; + if (index >= this.memberNumber || index < 0) return result; return this.deletemember(index); } removeRangeFrom(index: number, size: number): number { - if (index >= this.memberNumber || index < 0) throw new Error("index not in this plainarray range"); + if (typeof index !== "number" || typeof size !== "number") { + throw new TypeError("the index is not integer"); + } + if (index >= this.memberNumber || index < 0) throw new RangeError("the index is out-of-bounds"); let safeSize = (this.memberNumber - (index + size) < 0) ? this.memberNumber - index : size; this.deletemember(index, safeSize); return safeSize; } setValueAt(index: number, value: T): void { + if (typeof index !== "number") { + throw new TypeError("the index is not integer"); + } if (index >= 0 && index < this.memberNumber) { this.members.values[index] = value; } else { - throw new Error("index Out Of Bounds"); + throw new RangeError("the index is out-of-bounds"); } } toString(): string { @@ -127,6 +153,9 @@ if (flag || fastPlainArray === undefined) { return result.join(","); } getValueAt(index: number): T { + if (typeof index !== "number") { + throw new TypeError("the index is not integer"); + } return this.members.values[index]; } forEach(callbackfn: (value: T, index?: number, PlainArray?: PlainArray) => void, @@ -140,8 +169,8 @@ if (flag || fastPlainArray === undefined) { let count = 0; return { next: function () { - var done = count >= data.memberNumber; - var value = !done ? [data.members.keys[count], data.members.values[count]] as [number, T] : undefined; + let done = count >= data.memberNumber; + let value = !done ? [data.members.keys[count], data.members.values[count]] as [number, T] : undefined; count++; return { done: done, diff --git a/container/plainarray/native_module_plainarray.cpp b/container/plainarray/native_module_plainarray.cpp index 3065c317268c39df72f0971d0493ea5a988094cc..d0ce8bc83f37bb99ebe9267c60d03d025447867f 100644 --- a/container/plainarray/native_module_plainarray.cpp +++ b/container/plainarray/native_module_plainarray.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021 Huawei Device Co., Ltd. + * Copyright (c) 2022 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at diff --git a/container/queue/js_queue.ts b/container/queue/js_queue.ts index 30a6a47574ccbcba43d7e11504070e0249a03ea4..339dc071c27a9c0ded474adfc32ef38fdae85334 100644 --- a/container/queue/js_queue.ts +++ b/container/queue/js_queue.ts @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021 Huawei Device Co., Ltd. + * Copyright (c) 2022 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at @@ -22,16 +22,20 @@ if (arkPritvate !== undefined) { } if (flag || fastQueue == undefined) { class HandlerQueue { - get(obj: Queue, prop: any): T { - if (typeof prop === "symbol") { - return obj[prop]; - } - var index = Number.parseInt(prop); + private isOutBounds(obj: Queue, prop: any) { + let index = Number.parseInt(prop); if (Number.isInteger(index)) { if (index < 0 || index > obj.length) { - throw new Error("Queue: get out-of-bounds"); + console.log(index, obj.length) + throw new RangeError("the index is out-of-bounds"); } } + } + get(obj: Queue, prop: any): T { + if (typeof prop === "symbol") { + return obj[prop]; + } + this.isOutBounds(obj, prop); return obj[prop]; } set(obj: Queue, prop: any, value: T): boolean { @@ -39,20 +43,17 @@ if (flag || fastQueue == undefined) { obj[prop] = value; return true; } - var index = Number(prop); - if (Number.isInteger(index)) { - if (index < 0 || index > obj.length) { - throw new Error("Queue: set out-of-bounds"); - } else { + this.isOutBounds(obj, prop); + let index = Number(prop); + if (index >= 0 && index <= obj.length && Number.isInteger(index)) { obj[index] = value; return true; - } } return false; } ownKeys(obj: Queue) { - var keys = []; - for (var i = 0; i < obj.length; i++) { + let keys = []; + for (let i = 0; i < obj.length; i++) { keys.push(i.toString()); } return keys; @@ -61,17 +62,15 @@ if (flag || fastQueue == undefined) { return true; } getOwnPropertyDescriptor(obj: Queue, prop: any) { - var index = Number.parseInt(prop); - if (Number.isInteger(index)) { - if (index < 0 || index >= obj.length) { - throw new Error("Queue: getOwnPropertyDescriptor out-of-bounds"); - } + this.isOutBounds(obj, prop); + let index = Number.parseInt(prop); + if (index >= 0 && index < obj.length && Number.isInteger(index)) { return Object.getOwnPropertyDescriptor(obj, prop); } - return; + return } setPrototypeOf(obj: any, prop: any): any { - throw new Error("Can setPrototype on Queue Object"); + throw new RangeError("Can setPrototype on Queue Object"); } } interface IterableIterator { @@ -102,9 +101,15 @@ if (flag || fastQueue == undefined) { return true; } getFirst(): T { + if (this.isEmpty()) { + return undefined; + } return this[this.front]; } pop(): T { + if (this.isEmpty()) { + return undefined; + } let result = this[this.front]; this.front = (this.front + 1) % (this.capacity + 1); return result; @@ -113,25 +118,32 @@ if (flag || fastQueue == undefined) { thisArg?: Object): void { let k = 0; let i = this.front; - while (true) { - callbackfn.call(thisArg,this[i], k,this); - i = (i + 1) % this.capacity; - k++; - if (i === this.rear) { - break; + if (this.isEmpty()) { + return; + } else { + while (true) { + callbackfn.call(thisArg,this[i], k,this); + i = (i + 1) % this.capacity; + k++; + if (i === this.rear) { + break; + } } } } private isFull(): boolean { return this.length === this.capacity; } + private isEmpty(): boolean { + return this.length == 0; + } [Symbol.iterator](): IterableIterator { let count = this.front; let queue = this; return { next: function () { - var done = count == queue.rear; - var value = !done ? queue[count] : undefined; + let done = count == queue.rear; + let value = !done ? queue[count] : undefined; count = (count + 1) % queue.capacity; return { done: done, diff --git a/container/queue/native_module_queue.cpp b/container/queue/native_module_queue.cpp index 7f49eabe732975af54779da6abcfb8d059852413..7a053528c7890143b36d33519793a564d237833a 100644 --- a/container/queue/native_module_queue.cpp +++ b/container/queue/native_module_queue.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021 Huawei Device Co., Ltd. + * Copyright (c) 2022 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at diff --git a/container/stack/js_stack.ts b/container/stack/js_stack.ts index 80efb4b1220aa2305f42b10d272c3b9e06a584a8..62ff2233d234243d1eadbd234ef0324e695a0070 100644 --- a/container/stack/js_stack.ts +++ b/container/stack/js_stack.ts @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021 Huawei Device Co., Ltd. + * Copyright (c) 2022 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at @@ -22,17 +22,19 @@ if (arkPritvate !== undefined) { } if (flag || fastStack == undefined) { class HandlerStack { + private isOutBounds(obj: Stack, prop: any) { + let index = Number.parseInt(prop); + if (Number.isInteger(index)) { + if (index < 0 || index >= obj.length) { + throw new RangeError("the index is out-of-bounds"); + } + } + } get(obj: Stack, prop: any) { if (typeof prop === "symbol") { return obj[prop]; } - var index = Number.parseInt(prop); - if (Number.isInteger(index)) { - let length = obj.length; - if (index < 0 || index >= length) { - throw new Error("Stack: get out-of-bounds"); - } - } + this.isOutBounds(obj, prop); return obj[prop]; } set(obj: Stack, prop: any, value: T) { @@ -40,22 +42,18 @@ if (flag || fastStack == undefined) { 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("Stack: set out-of-bounds"); - } else { - obj[index] = value; - return true; - } + this.isOutBounds(obj, prop); + let index = Number.parseInt(prop); + if (index >= 0 && index < obj.length && Number.isInteger(index)) { + obj[index] = value; + return true; } return false; } ownKeys(obj: Stack) { - var keys = []; + let keys = []; let length = obj.length; - for (var i = 0; i < length; i++) { + for (let i = 0; i < length; i++) { keys.push(i.toString()); } return keys; @@ -64,18 +62,15 @@ if (flag || fastStack == undefined) { return true; } getOwnPropertyDescriptor(obj: Stack, prop: any) { - var index = Number.parseInt(prop); - if (Number.isInteger(index)) { - let length = obj.length; - if (index < 0 || index >= length) { - throw new Error("Stack: getOwnPropertyDescriptor out-of-bounds"); - } + this.isOutBounds(obj, prop); + let index = Number.parseInt(prop); + if (index >= 0 && index < obj.length && Number.isInteger(index)) { return Object.getOwnPropertyDescriptor(obj, prop); } - return; + return } setPrototypeOf(obj: any, prop: any): any { - throw new Error("Can setPrototype on Stack Object"); + throw new RangeError("Can setPrototype on Stack Object"); } } interface IterableIterator { @@ -101,11 +96,17 @@ if (flag || fastStack == undefined) { return item; } pop(): T { + if (this.isEmpty()) { + return undefined; + } let result = this[this.length - 1]; this.elementNum--; return result; } peek(): T { + if (this.isEmpty()) { + return undefined; + } return this[this.length - 1]; } locate(element: T): number { @@ -136,8 +137,8 @@ if (flag || fastStack == undefined) { let stack = this; return { next: function () { - var done = count >= stack.elementNum; - var value = !done ? stack[count++] : undefined; + let done = count >= stack.elementNum; + let value = !done ? stack[count++] : undefined; return { done: done, value: value, diff --git a/container/stack/native_module_stack.cpp b/container/stack/native_module_stack.cpp index abd74f69177508da463cdda62f92dfb6beff7180..0acbe9e5db00a4f2dcf05f00ea1bfc8de171ba57 100644 --- a/container/stack/native_module_stack.cpp +++ b/container/stack/native_module_stack.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021 Huawei Device Co., Ltd. + * Copyright (c) 2022 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at diff --git a/container/struct/js_struct.ts b/container/struct/js_struct.ts index 3f4e34da79881c7bacee56f810b046041a7fa0e6..751136a8393fae34aaa8059b4d5f3ed1ee82a5d2 100644 --- a/container/struct/js_struct.ts +++ b/container/struct/js_struct.ts @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021 Huawei Device Co., Ltd. + * Copyright (c) 2022 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at @@ -12,6 +12,8 @@ * See the License for the specific language governing permissions and * limitations under the License. */ +type CompFun = (firstValue: T, secondValue: T) => boolean; + function hashCode(element: any): number { let str = String(element); let hash = 0; @@ -56,7 +58,7 @@ function compareToString(string1: String, string2: String) { throw new Error("this function run error"); } -function currencyCompare(a: any, b: any, compareFn?: Function): number { +function currencyCompare(a: any, b: any, compareFn?: CompFun): number { if (a === b) return ComparResult.EQUALS; if (a instanceof Pair && b instanceof Pair) { return currencyCompare(a.key, b.key, compareFn); @@ -198,14 +200,15 @@ class LightWeightClass { protected getIndexByKey(key: K): number { let hash = hashCode(key); let index = this.binarySearchAtLightWeight(hash); + // js ( A negative number indicates an inverted number in the array ) if (index < 0 || index >= this.memberNumber) return -1; return index; } protected deletemember(key: K): V { + let result: any = undefined; let index = this.getIndexByKey(key); - if (index < 0) - throw new Error("don't find the key in lightweight"); + if (index < 0) return result; this.memberNumber--; this.members.hashs.splice(index, 1); this.members.keys.splice(index, 1); @@ -279,8 +282,8 @@ class RBTreeClass { public memberNumber: number; private isChange: boolean; private treeNodeArray: Array>; - private compFun: Function | undefined; - constructor(comparator?: (firstValue: K, secondValue: K) => boolean, root?: RBTreeNode) { + private compFun: CompFun | undefined; + constructor(comparator?: CompFun, root?: RBTreeNode) { this.root = root; this.compFun = comparator; this.memberNumber = 0; @@ -751,39 +754,36 @@ class DictionaryClass { } protected put(key: K, value: V = key as unknown as V): boolean { - if (key != undefined && value != undefined) { - this.isChange = true; - if (!this.hasKey(key)) this.memberNumber++; - const position = this.getHashIndex(key); - let members = this.tableLink[position]; - if (members instanceof LinkedList && members.count >= 8) { - let newElement = new RBTreeClass(); - let current = members.getHead(); - while (current != null || current != undefined) { - if (!(current.element instanceof Pair)) - throw new Error("this hashtable member save error"); - newElement.addNode(current.element.key, current.element.value); - current = current.next; - } - newElement.addNode(key, value); - this.tableLink[position] = newElement; - return true; - } else if (members instanceof RBTreeClass) { - members.addNode(key, value); + + this.isChange = true; + if (!this.hasKey(key)) this.memberNumber++; + const position = this.getHashIndex(key); + let members = this.tableLink[position]; + if (members instanceof LinkedList && members.count >= 8) { + let newElement = new RBTreeClass(); + let current = members.getHead(); + while (current != null || current != undefined) { + if (!(current.element instanceof Pair)) return false; + newElement.addNode(current.element.key, current.element.value); + current = current.next; + } + newElement.addNode(key, value); + this.tableLink[position] = newElement; + return true; + } else if (members instanceof RBTreeClass) { + members.addNode(key, value); + this.tableLink[position] = members; + return true; + } else { + if (this.tableLink[position] == undefined) { + members = new LinkedList>(); + } + if (!this.replaceMember(key, value)) { + members.push(new Pair(key, value)); this.tableLink[position] = members; - return true; - } else { - if (this.tableLink[position] == undefined) { - members = new LinkedList>(); - } - if (!this.replaceMember(key, value)) { - members.push(new Pair(key, value)); - this.tableLink[position] = members; - } - return true; } + return true; } - return false; } protected replaceMember(key: K, value: V = key as unknown as V): boolean { @@ -978,7 +978,7 @@ class LinkedList { return false; } - indexOf(element: T, compareFn?: Function) { + indexOf(element: T, compareFn?: CompFun) { let current = this.head; for (let i = 0; i < this.count && current != undefined; i++) { if (currencyCompare(element, current.element, compareFn) === ComparResult.EQUALS) { @@ -989,7 +989,7 @@ class LinkedList { return -1; } - remove(element: T, compareFn?: Function) { + remove(element: T, compareFn?: CompFun) { this.removeAt(this.indexOf(element, compareFn)); } @@ -1020,7 +1020,6 @@ class LinkedList { } } - export default { isIncludeToArray, LightWeightClass, diff --git a/container/struct/native_module_struct.cpp b/container/struct/native_module_struct.cpp index ee65b63ed6480a021ef74c1f562aa94ab7c2a53d..084f1705b014ee7a5d5e5e9269834a17d907ffe7 100644 --- a/container/struct/native_module_struct.cpp +++ b/container/struct/native_module_struct.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021 Huawei Device Co., Ltd. + * Copyright (c) 2022 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at diff --git a/container/treemap/js_treemap.ts b/container/treemap/js_treemap.ts index 90ac9e3a495bf767134420f6b3bdcddf9e3b1885..1305aeb6e277c8d000101fc407d384eda622af22 100644 --- a/container/treemap/js_treemap.ts +++ b/container/treemap/js_treemap.ts @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021 Huawei Device Co., Ltd. + * Copyright (c) 2022 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at @@ -69,20 +69,19 @@ if (flag || fastTreeMap === undefined) { } 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"); + if (tempNode === undefined) return tempNode; return tempNode.value; } getFirstKey(): K { let tempNode = this.constitute.firstNode(); if (tempNode === undefined) - throw new Error("don't find this key,this tree is empty"); + return tempNode; return tempNode.key; } getLastKey(): K { let tempNode = this.constitute.lastNode(); if (tempNode === undefined) - throw new Error("don't find this key,this tree is empty"); + return tempNode; return tempNode.key; } setAll(map: TreeMap) { @@ -98,36 +97,36 @@ if (flag || fastTreeMap === undefined) { this.constitute.clearTree(); } getLowerKey(key: K): K { + let result: any = undefined; let tempNode = this.constitute.getNode(key); - if (tempNode === undefined) - throw new Error("don't find this key,this node is undefine"); + if (tempNode === undefined) return tempNode; if (tempNode.left !== undefined) return tempNode.left.key; let node = tempNode; 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"); + return result; } getHigherKey(key: K): K { + let result: any = undefined; let tempNode = this.constitute.getNode(key); - if (tempNode === undefined) - throw new Error("don't find this key,this node is undefine"); + if (tempNode === undefined) return tempNode; if (tempNode.right !== undefined) return tempNode.right.key; let node = tempNode; 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"); + return result; } keys(): IterableIterator { let data = this.constitute; let count = 0; return { next: function () { - var done = count >= data.memberNumber; - var value = !done ? data.keyValueArray[count].key : undefined; + let done = count >= data.memberNumber; + let value = !done ? data.keyValueArray[count].key : undefined; count++; return { done: done, @@ -141,8 +140,8 @@ if (flag || fastTreeMap === undefined) { let count = 0; return { next: function () { - var done = count >= data.memberNumber; - var value = !done ? data.keyValueArray[count].value : undefined; + let done = count >= data.memberNumber; + let value = !done ? data.keyValueArray[count].value : undefined; count++; return { done: done, @@ -170,8 +169,8 @@ if (flag || fastTreeMap === undefined) { let count = 0; return { next: function () { - var done = count >= data.memberNumber; - var value = !done ? data.keyValueArray[count].entry() : undefined; + let done = count >= data.memberNumber; + let value = !done ? data.keyValueArray[count].entry() : undefined; count++; return { done: done, @@ -185,8 +184,8 @@ if (flag || fastTreeMap === undefined) { let count = 0; return { next: function () { - var done = count >= data.memberNumber; - var value = !done ? data.keyValueArray[count].entry() : undefined; + let done = count >= data.memberNumber; + let value = !done ? data.keyValueArray[count].entry() : undefined; count++; return { done: done, diff --git a/container/treemap/native_module_treemap.cpp b/container/treemap/native_module_treemap.cpp index 430fca6f46475be3adbeb5b223051ea7936bd6c1..c330c0c1edc4f4e268e8c84cbe1855ff35a4c741 100644 --- a/container/treemap/native_module_treemap.cpp +++ b/container/treemap/native_module_treemap.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021 Huawei Device Co., Ltd. + * Copyright (c) 2022 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at diff --git a/container/treeset/js_treeset.ts b/container/treeset/js_treeset.ts index 8ce0e00c490413554a96587700a6a721333b3206..6ed0e9e4af264c7ff4ded5d932eb1e7c893aa3bb 100644 --- a/container/treeset/js_treeset.ts +++ b/container/treeset/js_treeset.ts @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021 Huawei Device Co., Ltd. + * Copyright (c) 2022 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at @@ -77,63 +77,59 @@ if (flag || fastTreeSet === undefined) { } getFirstValue(): T { let tempNode = this.constitute.firstNode(); - if (tempNode === undefined) - throw new Error("don't find this key,this tree is empty"); + if (tempNode === undefined) return tempNode; return tempNode.key; } getLastValue(): T { let tempNode = this.constitute.lastNode(); - if (tempNode === undefined) - throw new Error("don't find this key,this tree is empty"); + if (tempNode === undefined) return tempNode; return tempNode.key; } getLowerValue(key: T): T { + let result: any = undefined; let tempNode = this.constitute.getNode(key); - if (tempNode === undefined) - throw new Error("don't find this key,this node is undefine"); + if (tempNode === undefined) return tempNode; if (tempNode.left !== undefined) return tempNode.left.key; let node = tempNode; 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"); + return result; } getHigherValue(key: T): T { + let result: any = undefined; let tempNode = this.constitute.getNode(key); - if (tempNode === undefined) - throw new Error("don't find this key,this node is undefine"); + if (tempNode === undefined) return tempNode; if (tempNode.right !== undefined) return tempNode.right.key; let node = tempNode; 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"); + return result; } popFirst(): T { let firstNode = this.constitute.firstNode(); - if (firstNode === undefined) - throw new Error("don't find first node,this tree is empty"); + if (firstNode === undefined) return firstNode; let value = firstNode.value; this.constitute.removeNodeProcess(firstNode); - return value as T; + return value; } popLast(): T { let lastNode = this.constitute.lastNode(); - if (lastNode === undefined) - throw new Error("don't find last node,this tree is empty"); + if (lastNode === undefined) return lastNode; let value = lastNode.value; this.constitute.removeNodeProcess(lastNode); - return value as T; + return value; } values(): IterableIterator { 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; + let done = count >= data.memberNumber; + let value = !done ? data.keyValueArray[count].value as T : undefined; count++; return { done: done, @@ -155,8 +151,8 @@ if (flag || fastTreeSet === undefined) { let count = 0; return { next: function () { - var done = count >= data.memberNumber; - var value = !done ? data.keyValueArray[count].entry() : undefined; + let done = count >= data.memberNumber; + let value = !done ? data.keyValueArray[count].entry() : undefined; count++; return { done: done, @@ -170,8 +166,8 @@ if (flag || fastTreeSet === undefined) { let count = 0; return { next: function () { - var done = count >= data.memberNumber; - var value = !done ? data.keyValueArray[count].key : undefined; + let done = count >= data.memberNumber; + let value = !done ? data.keyValueArray[count].key : undefined; count++; return { done: done, diff --git a/container/treeset/native_module_treeset.cpp b/container/treeset/native_module_treeset.cpp index 769559e8cc72413577f59ff3fc4dddabb636aad5..d416e5f98c976db9688d66e67577d14e098851bb 100644 --- a/container/treeset/native_module_treeset.cpp +++ b/container/treeset/native_module_treeset.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021 Huawei Device Co., Ltd. + * Copyright (c) 2022 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at diff --git a/container/tsconfig.json b/container/tsconfig.json index cab5e9dd7bce9384ebcb094233f0d39c8252dc60..d1d5e1d3abdeeef664f1aba7a2bd982d29ee8488 100644 --- a/container/tsconfig.json +++ b/container/tsconfig.json @@ -7,7 +7,7 @@ "outDir": "./jscode", /* Specify an output folder for all emitted files. */ "esModuleInterop": true, "forceConsistentCasingInFileNames": true, - "strict": true, + // "strict": true, "skipLibCheck": true, "noImplicitThis": false, "suppressImplicitAnyIndexErrors": true diff --git a/container/vector/js_vector.ts b/container/vector/js_vector.ts index 28e946d9c769ded0640fc364985c5576db4b8242..feec128289aa4f28224dd9341e9542501cc51d2e 100644 --- a/container/vector/js_vector.ts +++ b/container/vector/js_vector.ts @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021 Huawei Device Co., Ltd. + * Copyright (c) 2022 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at @@ -22,16 +22,19 @@ if (arkPritvate !== undefined) { } if (flag || fastVector == undefined) { class HandlerVector { - get(obj: Vector, prop: any) { - if (typeof prop === "symbol") { - return obj[prop]; - } - var index = Number.parseInt(prop); + private isOutBounds(obj: Vector, prop: any) { + let index = Number.parseInt(prop); if (Number.isInteger(index)) { if (index < 0 || index >= obj.length) { - throw new Error("Vector: get out-of-bounds"); + throw new RangeError("the index is out-of-bounds"); } } + } + get(obj: Vector, prop: any) { + if (typeof prop === "symbol") { + return obj[prop]; + } + this.isOutBounds(obj, prop); return obj[prop]; } set(obj: Vector, prop: any, value: T) { @@ -42,7 +45,7 @@ if (flag || fastVector == undefined) { let index = Number.parseInt(prop); if (Number.isInteger(index)) { if (index < 0 || index > obj.length) { - throw new Error("Vector: set out-of-bounds"); + throw new RangeError("the index is out-of-bounds"); } else { obj[index] = value; return true; @@ -51,11 +54,9 @@ if (flag || fastVector == undefined) { return false; } deleteProperty(obj: Vector, prop: any) { - var index = Number.parseInt(prop); - if (Number.isInteger(index)) { - if (index < 0 || index >= obj.length) { - throw new Error("vector: deleteProperty out-of-bounds"); - } + this.isOutBounds(obj, prop); + let index = Number.parseInt(prop); + if (index >= 0 && index < obj.length && Number.isInteger(index)) { obj.removeByIndex(index); return true; } @@ -66,7 +67,7 @@ if (flag || fastVector == undefined) { } ownKeys(obj: Vector) { let keys = []; - for (var i = 0; i < obj.length; i++) { + for (let i = 0; i < obj.length; i++) { keys.push(i.toString()); } return keys; @@ -75,17 +76,15 @@ if (flag || fastVector == undefined) { return true; } getOwnPropertyDescriptor(obj: Vector, prop: any) { - var index = Number.parseInt(prop); - if (Number.isInteger(index)) { - if (index < 0 || index >= obj.length) { - throw new Error("Vector: getOwnPropertyDescriptor out-of-bounds"); - } + this.isOutBounds(obj, prop); + let index = Number.parseInt(prop); + if (index >= 0 && index < obj.length && Number.isInteger(index)) { return Object.getOwnPropertyDescriptor(obj, prop); } return; } setPrototypeOf(obj: any, prop: any): any { - throw new Error("Can setPrototype on Vector Object"); + throw new RangeError("Can setPrototype on Vector Object"); } } interface IterableIterator { @@ -111,6 +110,9 @@ if (flag || fastVector == undefined) { return true; } insert(element: T, index: number): void { + if (index < 0 || index >= this.elementNum) { + throw new RangeError("the index is out-of-bounds"); + } if (this.isFull()) { this.resize(); } @@ -140,12 +142,22 @@ if (flag || fastVector == undefined) { return -1; } getFirstElement(): T { + if (this.isEmpty()) { + return undefined; + } return this[0]; } - set(index: number, element: T): void { + set(index: number, element: T): T { + if (index < 0 || index >= this.elementNum) { + throw new RangeError("the index is out-of-bounds"); + } this[index] = element; + return this[index]; } removeByIndex(index: number): T { + if (index < 0 || index >= this.elementNum) { + throw new RangeError("the index is out-of-bounds"); + } let result = this[index]; for (let i = index; i < this.elementNum - 1; i++) { this[i] = this[i + 1]; @@ -165,6 +177,9 @@ if (flag || fastVector == undefined) { return false; } getLastElement(): T { + if (this.isEmpty()) { + return undefined; + } return this[this.elementNum - 1]; } getLastIndexOf(element: T): number { @@ -200,9 +215,12 @@ if (flag || fastVector == undefined) { } removeByRange(fromIndex: number, toIndex: number): void { if (fromIndex >= toIndex) { - throw new Error(`fromIndex cannot be less than or equal to toIndex`); + throw new RangeError(`the 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 RangeError(`the fromIndex or the toIndex is out-of-bounds`); + } + toIndex = toIndex >= this.elementNum ? this.elementNum : toIndex; let i = fromIndex; for (let j = toIndex; j < this.elementNum; j++) { this[i] = this[j]; @@ -211,6 +229,9 @@ if (flag || fastVector == undefined) { this.elementNum -= toIndex - fromIndex; } setLength(newSize: number): void { + if (newSize < 0) { + throw new RangeError(`An incorrect size was set`); + } this.elementNum = newSize; } replaceAllElements(callbackfn: (value: T, index?: number, vector?: Vector) => T, @@ -239,7 +260,7 @@ if (flag || fastVector == undefined) { } } } else { - for (var i = 0; i < this.length - 1; i++) { + for (let i = 0; i < this.elementNum - 1; i++) { for (let j = 0; j < this.elementNum - 1 - i; j++) { if (this.asciSort(this[j], this[j + 1])) { isSort = false; @@ -270,10 +291,10 @@ if (flag || fastVector == undefined) { } subVector(fromIndex: number, toIndex: number): Vector { if (fromIndex >= toIndex) { - throw new Error(`fromIndex cannot be less than or equal to toIndex`); + throw new RangeError(`the fromIndex cannot be less than or equal to toIndex`); } if (fromIndex >= this.elementNum || fromIndex < 0 || toIndex < 0) { - throw new Error(`fromIndex or toIndex is out-of-bounds`); + throw new RangeError(`the fromIndex or the toIndex is out-of-bounds`); } toIndex = toIndex >= this.elementNum - 1 ? this.elementNum - 1 : toIndex; let vector = new Vector(); @@ -334,8 +355,8 @@ if (flag || fastVector == undefined) { let vector = this; return { next: function () { - var done = count >= vector.elementNum; - var value = !done ? vector[count++] : undefined; + let done = count >= vector.elementNum; + let value = !done ? vector[count++] : undefined; return { done: done, value: value, diff --git a/container/vector/native_module_vector.cpp b/container/vector/native_module_vector.cpp index fb4088aedbadaa078d8b07ccfa7dd469e8a4cce5..5974a8feb3294a09c6ac624c789ea0feb34d6db3 100644 --- a/container/vector/native_module_vector.cpp +++ b/container/vector/native_module_vector.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021 Huawei Device Co., Ltd. + * Copyright (c) 2022 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at