diff --git a/container/arraylist/js_arraylist.ts b/container/arraylist/js_arraylist.ts index 246a00df1be49329978da4129c8789b7f1194834..50f1229667a27ba534a49616e3943c0652c3cf44 100644 --- a/container/arraylist/js_arraylist.ts +++ b/container/arraylist/js_arraylist.ts @@ -12,40 +12,44 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -let flag = false; -let fastArrayList = undefined; -let arkPritvate = globalThis["ArkPrivate"] || undefined; +interface ArkPrivate { + ArrayList: number; + Load(key: number): Object; +} +let flag: boolean = false; +let fastArrayList: Object = undefined; +let arkPritvate: ArkPrivate = globalThis['ArkPrivate'] || undefined; if (arkPritvate !== undefined) { fastArrayList = arkPritvate.Load(arkPritvate.ArrayList); } else { flag = true; } -if (flag || fastArrayList == undefined) { +if (flag || fastArrayList === undefined) { class HandlerArrayList { - private isOutBounds(obj: ArrayList, prop: any) { - let index = Number.parseInt(prop); + private isOutBounds(obj: ArrayList, prop: any): void { + let index: number = Number.parseInt(prop); if (Number.isInteger(index)) { if (index < 0 || index >= obj.length) { - throw new RangeError("the index is out-of-bounds"); + throw new RangeError('the index is out-of-bounds'); } } } - get(obj: ArrayList, prop: any) { - if (typeof prop === "symbol") { + get(obj: ArrayList, prop: any): T { + if (typeof prop === 'symbol') { return obj[prop]; } this.isOutBounds(obj, prop); return obj[prop]; } - set(obj: ArrayList, prop: any, value: T) { - if (prop === "elementNum" || prop === "capacity") { + set(obj: ArrayList, prop: any, value: T): boolean { + if (prop === 'elementNum' || prop === 'capacity') { obj[prop] = value; return true; } - let index = Number.parseInt(prop); + let index: number = Number.parseInt(prop); if (Number.isInteger(index)) { if (index < 0 || index > obj.length) { - throw new RangeError("the index is out-of-bounds"); + throw new RangeError('the index is out-of-bounds'); } else { obj[index] = value; return true; @@ -53,38 +57,38 @@ if (flag || fastArrayList == undefined) { } return false; } - deleteProperty(obj: ArrayList, prop: any) { + deleteProperty(obj: ArrayList, prop: any): boolean { this.isOutBounds(obj, prop); - let index = Number.parseInt(prop); + let index: number = Number.parseInt(prop); if (index >= 0 && index < obj.length && Number.isInteger(index)) { obj.removeByIndex(index); return true; } return false; } - has(obj: ArrayList, prop: any) { + has(obj: ArrayList, prop: any): boolean { return obj.has(prop); } - ownKeys(obj: ArrayList) { - let keys = []; - for (let i = 0; i < obj.length; i++) { + ownKeys(obj: ArrayList): Array { + let keys: Array = []; + for (let i: number = 0; i < obj.length; i++) { keys.push(i.toString()); } return keys; } - defineProperty(obj: ArrayList, prop: any, desc: any) { + defineProperty(): boolean { return true; } - getOwnPropertyDescriptor(obj: ArrayList, prop: any) { + getOwnPropertyDescriptor(obj: ArrayList, prop: any): Object { this.isOutBounds(obj, prop); - let index = Number.parseInt(prop); + let index: number = 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 TypeError("Can setPrototype on ArrayList Object"); + setPrototypeOf(): T { + throw new TypeError('Can setPrototype on ArrayList Object'); } } interface IterableIterator { @@ -99,7 +103,7 @@ if (flag || fastArrayList == undefined) { constructor() { return new Proxy(this, new HandlerArrayList()); } - get length() { + get length(): number { return this.elementNum; } add(element: T): boolean { @@ -111,19 +115,19 @@ if (flag || fastArrayList == undefined) { } insert(element: T, index: number): void { if (index < 0 || index >= this.elementNum) { - throw new RangeError("the index is out-of-bounds"); + throw new RangeError('the index is out-of-bounds'); } if (this.isFull()) { this.resize(); } - for (let i = this.elementNum; i > index; i--) { + for (let i: number = this.elementNum; i > index; i--) { this[i] = this[i - 1]; } this[index] = element; this.elementNum++; } has(element: T): boolean { - for (let i = 0; i < this.elementNum; i++) { + for (let i: number = 0; i < this.elementNum; i++) { if (this[i] === element) { return true; } @@ -131,7 +135,7 @@ if (flag || fastArrayList == undefined) { return false; } getIndexOf(element: T): number { - for (let i = 0; i < this.elementNum; i++) { + for (let i: number = 0; i < this.elementNum; i++) { if (element === this[i]) { return i; } @@ -140,10 +144,10 @@ if (flag || fastArrayList == undefined) { } removeByIndex(index: number): T { if (index < 0 || index >= this.elementNum) { - throw new RangeError("the index 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++) { + let result: T = this[index]; + for (let i: number = index; i < this.elementNum - 1; i++) { this[i] = this[i + 1]; } this.elementNum--; @@ -151,8 +155,8 @@ if (flag || fastArrayList == undefined) { } remove(element: T): boolean { if (this.has(element)) { - let index = this.getIndexOf(element); - for (let i = index; i < this.elementNum - 1; i++) { + let index: number = this.getIndexOf(element); + for (let i: number = index; i < this.elementNum - 1; i++) { this[i] = this[i + 1]; } this.elementNum--; @@ -161,7 +165,7 @@ if (flag || fastArrayList == undefined) { return false; } getLastIndexOf(element: T): number { - for (let i = this.elementNum - 1; i >= 0; i--) { + for (let i: number = this.elementNum - 1; i >= 0; i--) { if (element === this[i]) { return i; } @@ -175,9 +179,9 @@ if (flag || fastArrayList == undefined) { if (fromIndex >= this.elementNum || fromIndex < 0 || toIndex < 0) { throw new RangeError(`the fromIndex or the toIndex is out-of-bounds`); } - toIndex = toIndex >= this.elementNum ? this.elementNum : toIndex; - let i = fromIndex; - for (let j = toIndex; j < this.elementNum; j++) { + toIndex = toIndex >= this.elementNum - 1 ? this.elementNum - 1 : toIndex; + let i: number = fromIndex; + for (let j: number = toIndex; j < this.elementNum; j++) { this[i] = this[j]; i++; } @@ -185,35 +189,35 @@ if (flag || fastArrayList == undefined) { } replaceAllElements(callbackfn: (value: T, index?: number, arrList?: ArrayList) => T, thisArg?: Object): void { - for (let i = 0; i < this.elementNum; i++) { + for (let i: number = 0; i < this.elementNum; i++) { this[i] = callbackfn.call(thisArg, this[i], i, this); } } forEach(callbackfn: (value: T, index?: number, arrList?: ArrayList) => void, thisArg?: Object): void { - for (let i = 0; i < this.elementNum; i++) { + for (let i: number = 0; i < this.elementNum; i++) { callbackfn.call(thisArg, this[i], i, this); } } sort(comparator?: (firstValue: T, secondValue: T) => number): void { - let isSort = true; + let isSort: boolean = true; if (comparator) { - for (let i = 0; i < this.elementNum; i++) { - for (let j = 0; j < this.elementNum - 1 - i; j++) { + for (let i: number = 0; i < this.elementNum; i++) { + for (let j: number = 0; j < this.elementNum - 1 - i; j++) { if (comparator(this[j], this[j + 1]) > 0) { isSort = false; - let temp = this[j]; + let temp: T = this[j]; this[j] = this[j + 1]; this[j + 1] = temp; } } } } else { - for (let i = 0; i < this.length - 1; i++) { - for (let j = 0; j < this.elementNum - 1 - i; j++) { + for (let i: number = 0; i < this.length - 1; i++) { + for (let j: number = 0; j < this.elementNum - 1 - i; j++) { if (this.asciSort(this[j], this[j + 1])) { isSort = false; - let temp = this[j]; + let temp: T = this[j]; this[j] = this[j + 1]; this[j + 1] = temp; } @@ -225,10 +229,10 @@ 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]")) { + 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) { @@ -246,8 +250,8 @@ if (flag || fastArrayList == undefined) { throw new RangeError(`the fromIndex or the toIndex is out-of-bounds`); } toIndex = toIndex >= this.elementNum ? this.elementNum : toIndex; - let arraylist = new ArrayList(); - for (let i = fromIndex; i < toIndex; i++) { + let arraylist: ArrayList = new ArrayList(); + for (let i: number = fromIndex; i < toIndex; i++) { arraylist.add(this[i]); } return arraylist; @@ -256,8 +260,8 @@ if (flag || fastArrayList == undefined) { this.elementNum = 0; } clone(): ArrayList { - let clone = new ArrayList(); - for (let i = 0; i < this.elementNum; i++) { + let clone: ArrayList = new ArrayList(); + for (let i: number = 0; i < this.elementNum; i++) { clone.add(this[i]); } return clone; @@ -266,8 +270,8 @@ if (flag || fastArrayList == undefined) { return this.capacity; } convertToArray(): Array { - let arr = []; - for (let i = 0; i < this.elementNum; i++) { + let arr: Array = []; + for (let i: number = 0; i < this.elementNum; i++) { arr[i] = this[i]; } return arr; @@ -279,7 +283,7 @@ if (flag || fastArrayList == undefined) { this.capacity = 1.5 * this.capacity; } isEmpty(): boolean { - return this.elementNum == 0; + return this.elementNum === 0; } increaseCapacityTo(newCapacity: number): void { if (newCapacity >= this.elementNum) { @@ -290,12 +294,14 @@ if (flag || fastArrayList == undefined) { this.capacity = this.elementNum; } [Symbol.iterator](): IterableIterator { - let count = 0; - let arraylist = this; + let count: number = 0; + let arraylist: ArrayList = this; return { next: function () { - let done = count >= arraylist.elementNum; - let value = !done ? arraylist[count++] : undefined; + let done: boolean = false; + let value: T = undefined; + done = count >= arraylist.elementNum; + value = done ? undefined : arraylist[count++]; return { done: done, value: value, diff --git a/container/arraylist/native_module_arraylist.cpp b/container/arraylist/native_module_arraylist.cpp index c99767fdf17e2c478a62bbf8fe0f73219b7db4d9..16dbc8f92ee0e0d7dab38e5e453312a1969e37de 100644 --- a/container/arraylist/native_module_arraylist.cpp +++ b/container/arraylist/native_module_arraylist.cpp @@ -53,7 +53,7 @@ static napi_module arrayListModule = { .nm_flags = 0, .nm_filename = nullptr, .nm_register_func = ArrayListInit, - .nm_modname = "ArrayList", + .nm_modname = "util.ArrayList", .nm_priv = ((void *)0), .reserved = {0}, }; @@ -62,4 +62,4 @@ extern "C" __attribute__((constructor)) void RegisterModule() { napi_module_register(&arrayListModule); } -} +} // namespace OHOS::Util diff --git a/container/deque/js_deque.ts b/container/deque/js_deque.ts index bfcc52b909b8100376beb75d0c11eded92229781..5e18ffd857cbbd2a6b9ce6ff8bf2de820259845b 100644 --- a/container/deque/js_deque.ts +++ b/container/deque/js_deque.ts @@ -12,40 +12,44 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -let flag = false; -let fastDeque = undefined; -let arkPritvate = globalThis["ArkPrivate"] || undefined; +interface ArkPrivate { + Deque: number; + Load(key: number): Object; +} +let flag: boolean = false; +let fastDeque: Object = undefined; +let arkPritvate: ArkPrivate = globalThis['ArkPrivate'] || undefined; if (arkPritvate !== undefined) { fastDeque = arkPritvate.Load(arkPritvate.Deque); } else { flag = true; } -if (flag || fastDeque == undefined) { +if (flag || fastDeque === undefined) { class HandlerDeque { - private isOutBounds(obj: Deque, prop: any) { - let index = Number.parseInt(prop); + private isOutBounds(prop: any): void { + let index: number = Number.parseInt(prop); if (Number.isInteger(index)) { if (index < 0) { - throw new RangeError("the index is out-of-bounds"); + throw new RangeError('the index is out-of-bounds'); } } } get(obj: Deque, prop: any): T { - if (typeof prop === "symbol") { + if (typeof prop === 'symbol') { return obj[prop]; } - this.isOutBounds(obj, prop); + this.isOutBounds(prop); 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; } - let index = Number(prop); + let index: number = Number(prop); if (Number.isInteger(index)) { if (index < 0) { - throw new RangeError("index is out-of-bounds"); + throw new RangeError('index is out-of-bounds'); } else { obj[index] = value; return true; @@ -53,29 +57,29 @@ if (flag || fastDeque == undefined) { } return false; } - has(obj: Deque, prop: any) { + has(obj: Deque, prop: any): boolean { return obj.has(prop); } - ownKeys(obj: Deque) { - let keys = []; - for (let i = 0; i < obj.length; i++) { + ownKeys(obj: Deque): Array { + let keys: Array = []; + for (let i: number = 0; i < obj.length; i++) { keys.push(i.toString()); } return keys; } - defineProperty(obj: Deque, prop: any, desc: any) { + defineProperty(): boolean { return true; } - getOwnPropertyDescriptor(obj: Deque, prop: any) { - this.isOutBounds(obj, prop); - let index = Number.parseInt(prop); + getOwnPropertyDescriptor(obj: Deque, prop: any): Object { + this.isOutBounds(prop); + let index: number = Number.parseInt(prop); if (index >= 0 && Number.isInteger(index)) { return Object.getOwnPropertyDescriptor(obj, prop); } return } - setPrototypeOf(obj: any, prop: any): any { - throw new RangeError("Can setPrototype on Deque Object"); + setPrototypeOf(): T { + throw new RangeError('Can setPrototype on Deque Object'); } } interface IterableIterator { @@ -94,8 +98,9 @@ if (flag || fastDeque == undefined) { this.rear = 0; return new Proxy(this, new HandlerDeque()); } - get length() { - let result = (this.rear - this.front + this.capacity) % this.capacity; + get length(): number { + let result: number = 0; + result = (this.rear - this.front + this.capacity) % this.capacity; return result; } insertFront(element: T): void { @@ -125,9 +130,9 @@ if (flag || fastDeque == undefined) { return this[this.rear - 1]; } has(element: T): boolean { - let result = false; - this.forEach(function (value, index) { - if (value == element) { + let result: boolean = false; + this.forEach(function (value) { + if (value === element) { result = true; } }); @@ -137,7 +142,8 @@ if (flag || fastDeque == undefined) { if (this.isEmpty()) { return undefined; } - let result = this[this.front]; + let result: T = undefined; + result = this[this.front]; this.front = (this.front + 1) % (this.capacity + 1); return result; } @@ -145,14 +151,15 @@ if (flag || fastDeque == undefined) { if (this.isEmpty()) { return undefined; } - let result = this[this.rear - 1]; + let result: T = undefined; + 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 k: number = 0; + let i: number = this.front; while (true) { callbackfn.call(thisArg, this[i], k, this); i = (i + 1) % this.capacity; @@ -163,9 +170,9 @@ if (flag || fastDeque == undefined) { } } private increaseCapacity(): void { - let count = 0; - let arr = []; - let length = this.length; + let count: number = 0; + let arr: Array = []; + let length: number = this.length; while (true) { arr[count++] = this[this.front]; this.front = (this.front + 1) % this.capacity; @@ -173,7 +180,7 @@ if (flag || fastDeque == undefined) { break; } } - for (let i = 0; i < length; i++) { + for (let i: number = 0; i < length; i++) { this[i] = arr[i]; } this.capacity = 2 * this.capacity; @@ -184,15 +191,17 @@ if (flag || fastDeque == undefined) { return (this.rear + 1) % this.capacity === this.front; } private isEmpty(): boolean { - return this.length == 0; + return this.length === 0; } [Symbol.iterator](): IterableIterator { - let deque = this; - let count = deque.front; + let deque: Deque = this; + let count: number = deque.front; return { next: function () { - let done = count == deque.rear; - let value = !done ? deque[count] : undefined; + let done: boolean = false; + let value: T = undefined; + done = count === deque.rear; + value = done ? undefined : deque[count]; 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 246d6bff48c6de6709daa498f2d0dfb219ea1693..c3a50a9ea6b9aa43818f3f1c698b4f3575c85ee1 100644 --- a/container/deque/native_module_deque.cpp +++ b/container/deque/native_module_deque.cpp @@ -53,7 +53,7 @@ static napi_module dequeModule = { .nm_flags = 0, .nm_filename = nullptr, .nm_register_func = DequeInit, - .nm_modname = "Deque", + .nm_modname = "util.Deque", .nm_priv = ((void *)0), .reserved = {0}, }; @@ -62,4 +62,4 @@ extern "C" __attribute__((constructor)) void RegisterModule() { napi_module_register(&dequeModule); } -} +} // namespace OHOS::Util diff --git a/container/hashmap/js_hashmap.ts b/container/hashmap/js_hashmap.ts index ffb3e861c2570c55b689907c982625db478fa5ee..3986d8fb1154e0fd47c5595855a81cf5f2db95bf 100644 --- a/container/hashmap/js_hashmap.ts +++ b/container/hashmap/js_hashmap.ts @@ -13,18 +13,20 @@ * limitations under the License. */ declare function requireNapi(s: string): any; - -let flag = false; -let fastHashMap = undefined; -let arkPritvate = globalThis["ArkPrivate"] || undefined; +interface ArkPrivate { + HashMap: number; + Load(key: number): Object; +} +let flag: boolean = false; +let fastHashMap: Object = undefined; +let arkPritvate: ArkPrivate = globalThis['ArkPrivate'] || undefined; if (arkPritvate !== undefined) { fastHashMap = arkPritvate.Load(arkPritvate.HashMap); } else { flag = true; } - if (flag || fastHashMap === undefined) { - const HashMapAbility = requireNapi("util.struct"); + let HashMapAbility: any = requireNapi('util.struct'); interface IterableIterator { next: () => { value: T | undefined; @@ -39,14 +41,14 @@ if (flag || fastHashMap === undefined) { } return false; } - defineProperty(target: HashMap, p: any): boolean { - throw new Error("Can't define Property on HashMap Object"); + defineProperty(): boolean { + throw new Error(`Can't define Property on HashMap Object`); } - deleteProperty(target: HashMap, p: any): boolean { - throw new Error("Can't delete Property on HashMap Object"); + deleteProperty(): boolean { + throw new Error(`Can't delete Property on HashMap Object`); } - setPrototypeOf(target: HashMap, p: any): boolean { - throw new Error("Can't set Prototype on HashMap Object"); + setPrototypeOf(): boolean { + throw new Error(`Can't set Prototype on HashMap Object`); } } class HashMap extends HashMapAbility.DictionaryClass { @@ -54,7 +56,7 @@ if (flag || fastHashMap === undefined) { super(); return new Proxy(this, new HandlerHashMap()); } - get length() { + get length(): number { return this.memberNumber; } isEmpty(): boolean { @@ -70,11 +72,12 @@ if (flag || fastHashMap === undefined) { return this.getValueByKey(key); } setAll(map: HashMap): void { - if(!(map instanceof HashMap)) { - throw new TypeError("Incoming object is not JSAPIHashMap"); + if (!(map instanceof HashMap)) { + throw new TypeError('Incoming object is not JSAPIHashMap'); } - let memebers = map.keyValueArray; - for (let i = 0; i < memebers.length; i++) { + let memebers: Array = []; + memebers = map.keyValueArray; + for (let i: number = 0; i < memebers.length; i++) { this.put(memebers[i].key, memebers[i].value); } } @@ -82,19 +85,21 @@ if (flag || fastHashMap === undefined) { return super.put(key, value); } remove(key: K): V { - let result = this.removeMember(key); + let result: V = this.removeMember(key); return result; } clear(): void { super.clear(); } keys(): IterableIterator { - let data = this; - let count = 0; + let data: HashMap = this; + let count: number = 0; return { next: function () { - let done = count >= data.memberNumber; - let value = !done ? data.keyValueArray[count].key : undefined; + let done: boolean = false; + let value: K = undefined; + done = count >= data.memberNumber; + value = done ? undefined : data.keyValueArray[count].key; count++; return { done: done, @@ -104,12 +109,14 @@ if (flag || fastHashMap === undefined) { }; } values(): IterableIterator { - let data = this; - let count = 0; + let data: HashMap = this; + let count: number = 0; return { next: function () { - let done = count >= data.memberNumber; - let value = !done ? data.keyValueArray[count].value : undefined; + let done: boolean = false; + let value: V = undefined; + done = count >= data.memberNumber; + value = done ? undefined : data.keyValueArray[count].value; count++; return { done: done, @@ -123,18 +130,21 @@ if (flag || fastHashMap === undefined) { } forEach(callbackfn: (value?: V, key?: K, map?: HashMap) => void, thisArg?: Object): void { - let tagetArray = this.keyValueArray; - for (let i = 0; i < tagetArray.length; i++) { + let tagetArray: Array = []; + tagetArray = this.keyValueArray; + for (let i: number = 0; i < tagetArray.length; i++) { callbackfn.call(thisArg, tagetArray[i].value, tagetArray[i].key, this); } } entries(): IterableIterator<[K, V]> { - let data = this; - let count = 0; + let data: HashMap = this; + let count: number = 0; return { next: function () { - let done = count >= data.memberNumber; - let value = !done ? data.keyValueArray[count].entry() : undefined; + let done: boolean = false; + let value: [K, V] = undefined; + done = count >= data.memberNumber; + value = done ? undefined : data.keyValueArray[count].entry(); count++; return { done: done, @@ -144,19 +154,7 @@ if (flag || fastHashMap === undefined) { }; } [Symbol.iterator](): IterableIterator<[K, V]> { - let data = this; - let count = 0; - return { - next: function () { - let done = count >= data.memberNumber; - let value = !done ? data.keyValueArray[count].entry() : undefined; - count++; - return { - done: done, - value: value, - }; - }, - }; + return this.entries(); } } Object.freeze(HashMap); diff --git a/container/hashmap/native_module_hashmap.cpp b/container/hashmap/native_module_hashmap.cpp index d2d0058470fc36bd97b20baa4b56cb5b48370fc3..801a501423ec3f4e9f9bc2b98f3c1544ec7cf912 100644 --- a/container/hashmap/native_module_hashmap.cpp +++ b/container/hashmap/native_module_hashmap.cpp @@ -55,7 +55,7 @@ static napi_module hashMapModule = { .nm_flags = 0, .nm_filename = nullptr, .nm_register_func = HashMapInit, - .nm_modname = "HashMap", + .nm_modname = "util.HashMap", .nm_priv = ((void*)0), .reserved = { 0 }, }; @@ -64,4 +64,4 @@ extern "C" __attribute__ ((constructor)) void RegisterModule() { napi_module_register(&hashMapModule); } -} +} // namespace OHOS::Util diff --git a/container/hashset/js_hashset.ts b/container/hashset/js_hashset.ts index 880f33d07495c8403a0ded9b3ff919e653146cd4..541fd7a0154725ba1a2d44d0b75c9d1e72bdabe2 100644 --- a/container/hashset/js_hashset.ts +++ b/container/hashset/js_hashset.ts @@ -14,18 +14,20 @@ */ declare function requireNapi(s: string): any; - -let flag = false; -let fastHashSet = undefined; -let arkPritvate = globalThis["ArkPrivate"] || undefined; +interface ArkPrivate { + HashSet: number; + Load(key: number): Object; +} +let flag: boolean = false; +let fastHashSet: Object = undefined; +let arkPritvate: ArkPrivate = globalThis['ArkPrivate'] || undefined; if (arkPritvate !== undefined) { fastHashSet = arkPritvate.Load(arkPritvate.HashSet); } else { flag = true; } - if (flag || fastHashSet === undefined) { - const HashSetAbility = requireNapi("util.struct"); + let HashSetAbility: any = requireNapi('util.struct'); interface IterableIterator { next: () => { value: T | undefined; @@ -40,14 +42,14 @@ if (flag || fastHashSet === undefined) { } return false; } - defineProperty(target: HashSet, p: any): boolean { - throw new Error("Can't define Property on HashSet Object"); + defineProperty(): boolean { + throw new Error(`Can't define Property on HashSet Object`); } - deleteProperty(target: HashSet, p: any): boolean { - throw new Error("Can't delete Property on HashSet Object"); + deleteProperty(): boolean { + throw new Error(`Can't delete Property on HashSet Object`); } - setPrototypeOf(target: HashSet, p: any): boolean { - throw new Error("Can't set Prototype on HashSet Object"); + setPrototypeOf(): boolean { + throw new Error(`Can't set Prototype on HashSet Object`); } } class HashSet extends HashSetAbility.DictionaryClass { @@ -55,7 +57,7 @@ if (flag || fastHashSet === undefined) { super(); return new Proxy(this, new HandlerHashSet()); } - get length() { + get length(): number { return this.memberNumber; } isEmpty(): boolean { @@ -65,11 +67,15 @@ if (flag || fastHashSet === undefined) { return this.hasKey(value); } add(value: T): boolean { - if (this.has(value)) return false; + if (this.has(value)) { + return false; + } return this.put(value); } remove(value: T): boolean { - if (this.removeMember(value) !== undefined) return true; + if (this.removeMember(value) !== undefined) { + return true; + } return false; } clear(): void { @@ -77,18 +83,21 @@ if (flag || fastHashSet === undefined) { } forEach(callbackfn: (value?: T, key?: T, set?: HashSet) => void, thisArg?: Object): void { - let tagetArray = this.keyValueArray; - for (let i = 0; i < tagetArray.length; i++) { + let tagetArray: Array = []; + tagetArray = this.keyValueArray; + for (let i: number = 0; i < tagetArray.length; i++) { callbackfn.call(thisArg, tagetArray[i].key, tagetArray[i].key, this); } } values(): IterableIterator { - let data = this; - let count = 0; + let data: HashSet = this; + let count: number = 0; return { next: function () { - let done = count >= data.memberNumber; - let value = !done ? data.keyValueArray[count].key : undefined; + let done: boolean = false; + let value: T = undefined; + done = count >= data.memberNumber; + value = done ? undefined : data.keyValueArray[count].key; count++; return { done: done, @@ -98,12 +107,14 @@ if (flag || fastHashSet === undefined) { }; } entries(): IterableIterator<[T, T]> { - let data = this; - let count = 0; + let data: HashSet = this; + let count: number = 0; return { next: function () { - let done = count >= data.memberNumber; - let value = !done ? data.keyValueArray[count].entry() : undefined; + let done: boolean = false; + let value: [T, T] = undefined; + done = count >= data.memberNumber; + value = done ? undefined : data.keyValueArray[count].entry(); count++; return { done: done, @@ -113,19 +124,7 @@ if (flag || fastHashSet === undefined) { }; } [Symbol.iterator](): IterableIterator { - let data = this; - let count = 0; - return { - next: function () { - let done = count >= data.memberNumber; - let value = !done ? data.keyValueArray[count].key : undefined; - count++; - return { - done: done, - value: value, - }; - }, - }; + return this.values(); } } Object.freeze(HashSet); diff --git a/container/hashset/native_module_hashset.cpp b/container/hashset/native_module_hashset.cpp index 6c1c48579da52e52053d4d6aa1a97819ecfb5d26..ac42e25878b9caeb226406ce7eb4cd19783080e5 100644 --- a/container/hashset/native_module_hashset.cpp +++ b/container/hashset/native_module_hashset.cpp @@ -55,7 +55,7 @@ static napi_module hashSetModule = { .nm_flags = 0, .nm_filename = nullptr, .nm_register_func = HashSetInit, - .nm_modname = "HashSet", + .nm_modname = "util.HashSet", .nm_priv = ((void*)0), .reserved = { 0 }, }; @@ -64,4 +64,4 @@ extern "C" __attribute__ ((constructor)) void RegisterModule() { napi_module_register(&hashSetModule); } -} +} // namespace OHOS::Util diff --git a/container/lightweightmap/js_lightweightmap.ts b/container/lightweightmap/js_lightweightmap.ts index f8be0e56275a0b519aad8a0b83aa6d54bf432878..970447188cc0fc3f59e4ede50f03378a2dc79607 100644 --- a/container/lightweightmap/js_lightweightmap.ts +++ b/container/lightweightmap/js_lightweightmap.ts @@ -13,18 +13,20 @@ * limitations under the License. */ declare function requireNapi(s: string): any; - -let flag = false; -let fastLightWeightMap = undefined; -let arkPritvate = globalThis["ArkPrivate"] || undefined; +interface ArkPrivate { + LightWeightMap: number; + Load(key: number): Object; +} +let flag: boolean = false; +let fastLightWeightMap: Object = undefined; +let arkPritvate: ArkPrivate = globalThis['ArkPrivate'] || undefined; if (arkPritvate !== undefined) { fastLightWeightMap = arkPritvate.Load(arkPritvate.LightWeightMap); } else { flag = true; } - if (flag || fastLightWeightMap === undefined) { - const LightWeightAbility = requireNapi("util.struct"); + const LightWeightAbility = requireNapi('util.struct'); interface IterableIterator { next: () => { value: T | undefined; @@ -39,14 +41,14 @@ if (flag || fastLightWeightMap === undefined) { } return false; } - defineProperty(target: LightWeightMap, p: any): boolean { - throw new Error("Can't define Property on LightWeightMap Object"); + defineProperty(): boolean { + throw new Error(`Can't define Property on LightWeightMap Object`); } - deleteProperty(target: LightWeightMap, p: any): boolean { - throw new Error("Can't delete Property on LightWeightMap Object"); + deleteProperty(): boolean { + throw new Error(`Can't delete Property on LightWeightMap Object`); } - setPrototypeOf(target: LightWeightMap, p: any): boolean { - throw new Error("Can't set Prototype on LightWeightMap Object"); + setPrototypeOf(): boolean { + throw new Error(`Can't set Prototype on LightWeightMap Object`); } } class LightWeightMap extends LightWeightAbility.LightWeightClass { @@ -54,15 +56,17 @@ if (flag || fastLightWeightMap === undefined) { super(); return new Proxy(this, new HandlerLightWeightMap()); } - get length() { + get length(): number { return this.memberNumber; } hasAll(map: LightWeightMap): boolean { - if(!(map instanceof LightWeightMap)) { - throw new TypeError("map is not JSAPILightWeightMap"); + if (!(map instanceof LightWeightMap)) { + throw new TypeError('map is not JSAPILightWeightMap'); + } + if (map.memberNumber > this.memberNumber) { + return false; } - if (map.memberNumber > this.memberNumber) return false; - if (LightWeightAbility.isIncludeToArray(this.keyValueStringArray(), map.keyValueStringArray()) ) { + if (LightWeightAbility.isIncludeToArray(this.keyValueStringArray(), map.keyValueStringArray())) { return true; } return false; @@ -74,18 +78,20 @@ 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"); + if (typeof minimumCapacity !== 'number') { + throw new TypeError('the size is not integer'); } super.ensureCapacity(minimumCapacity); } entries(): IterableIterator<[K, V]> { - let data = this; - let count = 0; + let data: LightWeightMap = this; + let count: number = 0; return { next: function () { - let done = count >= data.memberNumber; - let value = !done ? [data.members.keys[count], data.members.values[count]] as [K, V] : undefined; + let done: boolean = false; + let value: [K, V] = undefined; + done = count >= data.memberNumber; + value = done ? undefined : [data.members.keys[count], data.members.values[count]] as [K, V]; count++; return { done: done, @@ -95,7 +101,8 @@ if (flag || fastLightWeightMap === undefined) { }; } get(key: K): V { - let index = this.getIndexByKey(key); + let index: number = 0; + index = this.getIndexByKey(key); return this.members.values[index]; } getIndexOfKey(key: K): number { @@ -108,18 +115,20 @@ if (flag || fastLightWeightMap === undefined) { return this.memberNumber === 0; } getKeyAt(index: number): K { - if (typeof index !== "number") { - throw new TypeError("the index is not integer"); + if (typeof index !== 'number') { + throw new TypeError('the index is not integer'); } return this.members.keys[index]; } keys(): IterableIterator { - let data = this; - let count = 0; + let data: LightWeightMap = this; + let count: number = 0; return { next: function () { - let done = count >= data.memberNumber; - let value = !done ? data.members.keys[count] : undefined; + let done: boolean = false; + let value: K = undefined; + done = count >= data.memberNumber; + value = done ? undefined : data.members.keys[count]; count++; return { done: done, @@ -129,8 +138,8 @@ if (flag || fastLightWeightMap === undefined) { }; } setAll(map: LightWeightMap): void { - if(!(map instanceof LightWeightMap)) { - throw new TypeError("Incoming object is not JSAPILightWeightMap"); + if (!(map instanceof LightWeightMap)) { + throw new TypeError('Incoming object is not JSAPILightWeightMap'); } if (this.memberNumber === 0) { this.members.hashs = map.members.hashs.slice(); @@ -138,7 +147,7 @@ if (flag || fastLightWeightMap === undefined) { this.members.values = map.members.values.slice(); this.memberNumber = map.memberNumber; } else { - for (let i = 0; i < map.memberNumber; i++) { + for (let i: number = 0; i < map.memberNumber; i++) { this.addmember(map.members.keys[i], map.members.values[i]); } } @@ -151,7 +160,9 @@ if (flag || fastLightWeightMap === undefined) { return this.deletemember(key); } removeAt(index: number): boolean { - if (index > this.memberNumber--) return false; + if (index > this.memberNumber--) { + return false; + } this.members.hashs.splice(index, 1); this.members.values.splice(index, 1); this.members.keys.splice(index, 1); @@ -168,49 +179,41 @@ if (flag || fastLightWeightMap === undefined) { } } setValueAt(index: number, newValue: V): boolean { - if (index > this.memberNumber || this.members.values[index] === undefined) return false; + if (index > this.memberNumber || this.members.values[index] === undefined) { + return false; + } this.members.values[index] = newValue; return true; } forEach(callbackfn: (value?: V, key?: K, map?: LightWeightMap) => void, thisArg?: Object): void { - let data = this; - for (let i = 0; i < data.memberNumber; i++) { + let data: LightWeightMap = this; + for (let i: number = 0; i < data.memberNumber; i++) { callbackfn.call(thisArg, data.members.values[i], data.members.keys[i], data); } } [Symbol.iterator](): IterableIterator<[K, V]> { - let data = this; - let count = 0; - return { - next: function () { - let done = count >= data.memberNumber; - let value = !done ? [data.members.keys[count], data.members.values[count]] as [K, V] : undefined; - count++; - return { - done: done, - value: value, - }; - }, - }; + return this.entries(); } toString(): string { - let result = new Array(); - for (let i = 0; i < this.memberNumber; i++) { - result.push(this.members.keys[i] + ":" + this.members.values[i]); + let result: string[] = []; + for (let i: number = 0; i < this.memberNumber; i++) { + result.push(this.members.keys[i] + ':' + this.members.values[i]); } - return result.join(","); + return result.join(','); } getValueAt(index: number): V { return this.members.values[index]; } values(): IterableIterator { - let data = this; - let count = 0; + let data: LightWeightMap = this; + let count: number = 0; return { next: function () { - let done = count >= data.memberNumber; - let value = !done ? data.members.values[count] : undefined; + let done: boolean = false; + let value: V = undefined; + done = count >= data.memberNumber; + value = done ? undefined : data.members.values[count]; count++; return { done: done, diff --git a/container/lightweightmap/native_module_lightweightmap.cpp b/container/lightweightmap/native_module_lightweightmap.cpp index e24270c272e19f72efe0fa4d06ec6f26dd46120b..32712670c73c4a81928fc712bf8283dc1fec3ffd 100644 --- a/container/lightweightmap/native_module_lightweightmap.cpp +++ b/container/lightweightmap/native_module_lightweightmap.cpp @@ -56,7 +56,7 @@ static napi_module lightWeightMapModule = { .nm_flags = 0, .nm_filename = nullptr, .nm_register_func = LightWeightMapInit, - .nm_modname = "LightWeightMap", + .nm_modname = "util.LightWeightMap", .nm_priv = ((void*)0), .reserved = { 0 }, }; @@ -65,4 +65,4 @@ extern "C" __attribute__ ((constructor)) void RegisterModule() { napi_module_register(&lightWeightMapModule); } -} +} // namespace OHOS::Util diff --git a/container/lightweightset/js_lightweightset.ts b/container/lightweightset/js_lightweightset.ts index 9e39294da3da896359d653bfee9e89c4e43fb0b5..7f97abe4110a7aacbeee26783c899389b9f87549 100644 --- a/container/lightweightset/js_lightweightset.ts +++ b/container/lightweightset/js_lightweightset.ts @@ -13,18 +13,20 @@ * limitations under the License. */ declare function requireNapi(s: string): any; - -let flag = false; -let fastLightWeightSet = undefined; -let arkPritvate = globalThis["ArkPrivate"] || undefined; +interface ArkPrivate { + LightWeightSet: number; + Load(key: number): Object; +} +let flag: boolean = false; +let fastLightWeightSet: Object = undefined; +let arkPritvate: ArkPrivate = globalThis['ArkPrivate'] || undefined; if (arkPritvate !== undefined) { fastLightWeightSet = arkPritvate.Load(arkPritvate.LightWeightSet); } else { flag = true; } - if (flag || fastLightWeightSet === undefined) { - const LightWeightAbility = requireNapi("util.struct"); + const LightWeightAbility = requireNapi('util.struct'); interface IterableIterator { next: () => { value: T | undefined; @@ -39,14 +41,14 @@ if (flag || fastLightWeightSet === undefined) { } return false; } - defineProperty(target: LightWeightSet, p: any): boolean { - throw new Error("Can't define Property on LightWeightSet Object"); + defineProperty(): boolean { + throw new Error(`Can't define Property on LightWeightSet Object`); } - deleteProperty(target: LightWeightSet, p: any): boolean { - throw new Error("Can't delete Property on LightWeightSet Object"); + deleteProperty(): boolean { + throw new Error(`Can't delete Property on LightWeightSet Object`); } - setPrototypeOf(target: LightWeightSet, p: any): boolean { - throw new Error("Can't set Prototype on LightWeightSet Object"); + setPrototypeOf(): boolean { + throw new Error(`Can't set Prototype on LightWeightSet Object`); } } class LightWeightSet extends LightWeightAbility.LightWeightClass { @@ -54,30 +56,34 @@ if (flag || fastLightWeightSet === undefined) { super(); return new Proxy(this, new HandlerLightWeightSet()); } - get length() { + get length(): number { return this.memberNumber; } add(obj: T): boolean { - if (this.members.keys.indexOf(obj) > 0) return false; + if (this.members.keys.indexOf(obj) > 0) { + return false; + } this.addmember(obj); return true; } addAll(set: LightWeightSet): boolean { - if(!(set instanceof LightWeightSet)) { - throw new TypeError("Incoming object is not JSAPILightWeightSet"); + if (!(set instanceof LightWeightSet)) { + throw new TypeError('Incoming object is not JSAPILightWeightSet'); } - let change = false; - if (set.memberNumber == 0) { + let change: boolean = false; + if (set.memberNumber === 0) { change = false; } else { - for (let i = 0; i < set.memberNumber; i++) { + for (let i: number = 0; i < set.memberNumber; i++) { change = this.add(set.members.keys[i]) || change; } - } + } return change; } hasAll(set: LightWeightSet): boolean { - if (set.memberNumber > this.memberNumber) return false; + if (set.memberNumber > this.memberNumber) { + return false; + } if (LightWeightAbility.isIncludeToArray(this.members.keys, set.members.keys)) { return true; } @@ -87,10 +93,15 @@ if (flag || fastLightWeightSet === undefined) { return this.members.keys.indexOf(key) > -1; } equal(obj: Object): boolean { - if (this.memberNumber === 0) return false; - if(obj instanceof LightWeightSet) + 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; + } + if (JSON.stringify(obj) === JSON.stringify(this.members.keys)) { + return true; + } return false; } increaseCapacityTo(minimumCapacity: number): void { @@ -106,7 +117,9 @@ if (flag || fastLightWeightSet === undefined) { return super.deletemember(key); } removeAt(index: number): boolean { - if (index > this.memberNumber--) return false; + if (index > this.memberNumber--) { + return false; + } this.members.hashs.splice(index, 1); this.members.values.splice(index, 1); this.members.keys.splice(index, 1); @@ -124,18 +137,20 @@ if (flag || fastLightWeightSet === undefined) { } forEach(callbackfn: (value?: T, key?: T, set?: LightWeightSet) => void, thisArg?: Object): void { - let data = this; - for (let i = 0; i < data.memberNumber; i++) { + let data: LightWeightSet = this; + for (let i: number = 0; i < data.memberNumber; i++) { callbackfn.call(thisArg, data.members.keys[i], data.members.keys[i], data); } } [Symbol.iterator](): IterableIterator { - let data = this; - let count = 0; + let data: LightWeightSet = this; + let count: number = 0; return { next: function () { - let done = count >= data.memberNumber; - let value = !done ? data.members.keys[count] : undefined; + let done: boolean = false; + let value: T = undefined; + done = count >= data.memberNumber; + value = done ? undefined : data.members.keys[count]; count++; return { done: done, @@ -145,7 +160,7 @@ if (flag || fastLightWeightSet === undefined) { }; } toString(): string { - return this.members.keys.join(","); + return this.members.keys.join(','); } toArray(): Array { return this.members.keys.slice(); @@ -157,13 +172,16 @@ if (flag || fastLightWeightSet === undefined) { return this.members.keys.values() as IterableIterator; } entries(): IterableIterator<[T, T]> { - let data = this; - let count = 0; + let data: LightWeightSet = this; + let count: number = 0; return { next: function () { - let done = count >= data.memberNumber; - let tempValue = data.members.keys[count]; - let value = !done ? ([tempValue, tempValue] as [T, T]) : undefined; + let done: boolean = false; + let value: [T, T] = undefined; + let tempValue: T = undefined; + done = count >= data.memberNumber; + tempValue = data.members.keys[count]; + value = done ? undefined : ([tempValue, tempValue] as [T, T]); count++; return { done: done, diff --git a/container/lightweightset/native_module_lightweightset.cpp b/container/lightweightset/native_module_lightweightset.cpp index 0d84dbb222c321dcdadc7d4c1cfcd5bd06397354..ff95359a2d575ef063aa52d15ece2e7803b5c2dc 100644 --- a/container/lightweightset/native_module_lightweightset.cpp +++ b/container/lightweightset/native_module_lightweightset.cpp @@ -56,7 +56,7 @@ static napi_module lightWeightSetModule = { .nm_flags = 0, .nm_filename = nullptr, .nm_register_func = LightWeightSetInit, - .nm_modname = "LightWeightSet", + .nm_modname = "util.LightWeightSet", .nm_priv = ((void*)0), .reserved = { 0 }, }; @@ -65,4 +65,4 @@ extern "C" __attribute__ ((constructor)) void RegisterModule() { napi_module_register(&lightWeightSetModule); } -} +} // namespace OHOS::Util diff --git a/container/linkedlist/js_linkedlist.ts b/container/linkedlist/js_linkedlist.ts index 3a51a94a3e74b405986b4aba7aa1f9129c37604a..9ede2de1a3104e9ed78021974a8abaae974c1a6e 100644 --- a/container/linkedlist/js_linkedlist.ts +++ b/container/linkedlist/js_linkedlist.ts @@ -12,44 +12,48 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -let flag = false; -let fastLinkedList = undefined; -let arkPritvate = globalThis["ArkPrivate"] || undefined; +interface ArkPrivate { + LinkedList: number; + Load(key: number): Object; +} +let flag: boolean = false; +let fastLinkedList: Object = undefined; +let arkPritvate: ArkPrivate = globalThis['ArkPrivate'] || undefined; if (arkPritvate !== undefined) { fastLinkedList = arkPritvate.Load(arkPritvate.LinkedList); } else { flag = true; } -if (flag || fastLinkedList == undefined) { +if (flag || fastLinkedList === undefined) { class HandlerLinkedList { - get(obj: LinkedList, prop: any, receiver: any) { - if (typeof prop === "symbol") { + get(obj: LinkedList, prop: any): T { + if (typeof prop === 'symbol') { return obj[prop]; } - let index = Number.parseInt(prop); - let length = obj.length; + let index: number = Number.parseInt(prop); + let length: number = obj.length; if (Number.isInteger(index)) { if (index < 0 || index >= length) { - throw new RangeError("the index is out-of-bounds"); + throw new RangeError('the index is out-of-bounds'); } return obj.get(index); } return obj[prop]; } - set(obj: LinkedList, prop: any, value: any) { - if (prop === "elementNum" || - prop === "capacity" || - prop === "head" || - prop == "next" || - prop == "tail") { + set(obj: LinkedList, prop: any, value: any): boolean { + if (prop === 'elementNum' || + prop === 'capacity' || + prop === 'head' || + prop === 'next' || + prop === 'tail') { obj[prop] = value; return true; } - let index = Number.parseInt(prop); + let index: number = Number.parseInt(prop); if (Number.isInteger(index)) { - let length = obj.length; + let length: number = obj.length; if (index < 0 || index >= length) { - throw new RangeError("the index is out-of-bounds"); + throw new RangeError('the index is out-of-bounds'); } else { obj.set(index, value); return true; @@ -57,45 +61,45 @@ if (flag || fastLinkedList == undefined) { } return false; } - deleteProperty(obj: LinkedList, prop: any) { - let index = Number.parseInt(prop); + deleteProperty(obj: LinkedList, prop: any): boolean { + let index: number = Number.parseInt(prop); if (Number.isInteger(index)) { - let length = obj.length; + let length: number = obj.length; if (index < 0 || index >= length) { - throw new RangeError("the index is out-of-bounds"); + throw new RangeError('the index is out-of-bounds'); } obj.removeByIndex(index); return true; } return false; } - has(obj: LinkedList, prop: any) { + has(obj: LinkedList, prop: any): boolean { return obj.has(prop); } - ownKeys(obj: LinkedList) { - let keys = []; - let length = obj.length; + ownKeys(obj: LinkedList): Array { + let keys: Array = []; + let length: number = obj.length; for (let i = 0; i < length; i++) { keys.push(i.toString()); } return keys; } - defineProperty(obj: LinkedList, prop: any, desc: any) { + defineProperty(): boolean { return true; } - getOwnPropertyDescriptor(obj: LinkedList, prop: any) { - let index = Number.parseInt(prop); + getOwnPropertyDescriptor(obj: LinkedList, prop: any): Object { + let index: number = Number.parseInt(prop); if (Number.isInteger(index)) { - let length = obj.length; + let length: number = obj.length; if (index < 0 || index >= length) { - throw new RangeError("the index is 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"); + setPrototypeOf(): T { + throw new Error('Can setPrototype on LinkedList Object'); } } interface IterableIterator { @@ -119,24 +123,20 @@ if (flag || fastLinkedList == undefined) { private tail?: NodeObj; private elementNum: number; private capacity: number; - 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()); } - get length() { + get length(): number { return this.elementNum; } private getNode(index: number): NodeObj | undefined { if (index >= 0 && index < this.elementNum) { - let current = this.head; - for (let i = 0; i < index; i++) { + let current: NodeObj = this.head; + for (let i: number = 0; i < index; i++) { if (current !== undefined) { current = current.next; } @@ -147,8 +147,8 @@ if (flag || fastLinkedList == undefined) { } get(index: number): T { if (index >= 0 && index < this.elementNum) { - let current = this.head; - for (let i = 0; i < index && current != undefined; i++) { + let current: NodeObj = this.head; + for (let i: number = 0; i < index && current !== undefined; i++) { current = current.next; } return current.element; @@ -156,11 +156,11 @@ if (flag || fastLinkedList == undefined) { return undefined; } add(element: T): boolean { - let node = new NodeObj(element); - if (this.head == undefined) { + let node: NodeObj = new NodeObj(element); + if (this.head === undefined) { this.head = this.tail = node; } else { - let current = this.head; + let current: NodeObj = this.head; while (current.next !== undefined) { current = current.next; } @@ -170,7 +170,7 @@ if (flag || fastLinkedList == undefined) { return true; } addFirst(element: T): void { - let node = new NodeObj(element); + let node: NodeObj = new NodeObj(element); if (this.elementNum === 0) { this.head = this.tail = node; } else { @@ -181,7 +181,7 @@ if (flag || fastLinkedList == undefined) { } removeFirst(): T { if (this.head !== undefined) { - let result = this.head.element; + let result: T = this.head.element; this.removeByIndex(0); return result; } @@ -189,7 +189,7 @@ if (flag || fastLinkedList == undefined) { } removeLast(): T { if (this.tail !== undefined) { - let result = this.tail.element; + let result: T = this.tail.element; this.removeByIndex(this.elementNum - 1); return result; } @@ -205,8 +205,8 @@ if (flag || fastLinkedList == undefined) { if (this.head.element === element) { return true; } - let current = this.head; - while (current.next != undefined) { + let current: NodeObj = this.head; + while (current.next !== undefined) { current = current.next; if (current.element === element) { return true; @@ -216,8 +216,8 @@ if (flag || fastLinkedList == undefined) { return false; } getIndexOf(element: T): number { - for (let i = 0; i < this.elementNum; i++) { - const curNode = this.getNode(i); + for (let i: number = 0; i < this.elementNum; i++) { + let curNode: NodeObj = this.getNode(i); if (curNode !== undefined && curNode.element === element) { return i; } @@ -225,8 +225,8 @@ if (flag || fastLinkedList == undefined) { return -1; } getLastIndexOf(element: T): number { - for (let i = this.elementNum - 1; i >= 0; i--) { - const curNode = this.getNode(i); + for (let i: number = this.elementNum - 1; i >= 0; i--) { + let curNode: NodeObj = this.getNode(i); if (curNode !== undefined && curNode.element === element) { return i; } @@ -235,22 +235,22 @@ if (flag || fastLinkedList == undefined) { } removeByIndex(index: number): T { if (index >= 0 && index < this.elementNum) { - let current = this.head; + let current: NodeObj = this.head; if (index === 0 && current !== undefined) { this.head = current.next; this.head.prev = undefined; - if (this.elementNum == 1) { + if (this.elementNum === 1) { this.head = this.tail = undefined; } - } else if (index == this.elementNum - 1) { + } else if (index === this.elementNum - 1) { current = this.getNode(index - 1); if (current !== undefined) { this.tail = current; current.next = undefined; } } else { - const prevNode = this.getNode(index - 1); - const nextNode = this.getNode(index + 1); + let prevNode: NodeObj = this.getNode(index - 1); + let nextNode: NodeObj = this.getNode(index + 1); if (prevNode !== undefined && nextNode !== undefined) { prevNode.next = nextNode; nextNode.prev = prevNode; @@ -261,7 +261,7 @@ if (flag || fastLinkedList == undefined) { return current.element; } } else { - throw new RangeError("the index is out-of-bounds"); + throw new RangeError('the index is out-of-bounds'); } } remove(element: T): boolean { @@ -269,7 +269,8 @@ if (flag || fastLinkedList == undefined) { return false; } if (this.has(element)) { - let index = this.getIndexOf(element); + let index: number = 0; + index = this.getIndexOf(element); this.removeByIndex(index); return true; } @@ -277,7 +278,8 @@ if (flag || fastLinkedList == undefined) { } removeFirstFound(element: T): boolean { if (this.has(element)) { - let index = this.getIndexOf(element); + let index: number = 0; + index = this.getIndexOf(element); this.removeByIndex(index); return true; } @@ -285,7 +287,8 @@ if (flag || fastLinkedList == undefined) { } removeLastFound(element: T): boolean { if (this.has(element)) { - let index = this.getLastIndexOf(element); + let index: number = 0; + index = this.getLastIndexOf(element); this.removeByIndex(index); return true; } @@ -305,8 +308,8 @@ if (flag || fastLinkedList == undefined) { } insert(index: number, element: T): void { if (index >= 0 && index <= this.elementNum) { - let newNode = new NodeObj(element); - let current = this.head; + let newNode: NodeObj = new NodeObj(element); + let current: NodeObj = this.head; if (index === 0) { if (this.head === undefined) { this.head = this.tail = newNode; @@ -316,10 +319,10 @@ if (flag || fastLinkedList == undefined) { this.head = newNode; } } else if (index === this.elementNum && this.elementNum !== 0) { - let prevNode = this.getNode(this.elementNum - 1); + let prevNode: NodeObj = this.getNode(this.elementNum - 1); prevNode.next = this.tail = newNode; } else { - const prevNode = this.getNode(index - 1); + let prevNode: NodeObj = this.getNode(index - 1); current = prevNode.next; newNode.next = current; prevNode.next = newNode; @@ -327,25 +330,26 @@ if (flag || fastLinkedList == undefined) { newNode.prev = prevNode; } } else { - throw new RangeError("the index is out-of-bounds"); + throw new RangeError('the index is out-of-bounds'); } this.elementNum++; } set(index: number, element: T): T { - const current = this.getNode(index); + let current: NodeObj = undefined; + current = this.getNode(index); current.element = element; return current.element; } convertToArray(): Array { let arr: Array = []; - let index = 0; + let index: number = 0; if (this.elementNum <= 0) { return arr; } if (this.head !== undefined) { - let current = this.head; + let current: NodeObj = this.head; arr[index] = this.head.element; - while (current.next != undefined) { + while (current.next !== undefined) { current = current.next; arr[++index] = current.element; } @@ -353,38 +357,40 @@ if (flag || fastLinkedList == undefined) { return arr; } clone(): LinkedList { - let clone = new LinkedList(); - let arr = this.convertToArray(); - for (let i = 0; i < arr.length; i++) { - let item = arr[i]; + let clone: LinkedList = new LinkedList(); + let arr: Array = this.convertToArray(); + for (let i: number = 0; i < arr.length; i++) { + let item: T = arr[i]; clone.add(item); } return clone; } private isEmpty(): boolean { - return this.elementNum == 0; + return this.elementNum === 0; } forEach(callbackfn: (value: T, index?: number, linkedList?: LinkedList) => void, thisArg?: Object): void { - let index = 0; + let index: number = 0; if (this.head !== undefined) { - let current = this.head; + let current: NodeObj = this.head; if (this.elementNum > 0) { callbackfn.call(thisArg, this.head.element, index, this); } - while (current.next != undefined) { + while (current.next !== undefined) { current = current.next; callbackfn.call(thisArg, current.element, ++index, this); } } } [Symbol.iterator](): IterableIterator { - let count = 0; - let linkedlist = this; + let count: number = 0; + let linkedlist: LinkedList = this; return { next: function () { - let done = count >= linkedlist.elementNum; - let value = !done ? linkedlist.getNode(count++).element : undefined; + let done: boolean = false; + let value: T = undefined; + done = count >= linkedlist.elementNum; + value = done ? undefined : linkedlist.getNode(count++).element; return { done: done, value: value, diff --git a/container/linkedlist/native_module_linkedlist.cpp b/container/linkedlist/native_module_linkedlist.cpp index 98ad00123a43f491b9934629f8773477ea00c496..387e27d3b92866ca9c8b7f6d5cd3b6f5180f6c1d 100644 --- a/container/linkedlist/native_module_linkedlist.cpp +++ b/container/linkedlist/native_module_linkedlist.cpp @@ -55,7 +55,7 @@ static napi_module linkedListModule = { .nm_flags = 0, .nm_filename = nullptr, .nm_register_func = LinkedListInit, - .nm_modname = "LinkedList", + .nm_modname = "util.LinkedList", .nm_priv = ((void *)0), .reserved = {0}, }; @@ -64,4 +64,4 @@ extern "C" __attribute__((constructor)) void RegisterModule() { napi_module_register(&linkedListModule); } -} +} // namespace OHOS::Util diff --git a/container/list/js_list.ts b/container/list/js_list.ts index 45703e6426119f33a250629739b7d76313e53e52..5eecd86201901fec0b47dbcecea7a18527aea17d 100644 --- a/container/list/js_list.ts +++ b/container/list/js_list.ts @@ -12,41 +12,45 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -let flag = false; -let fastList = undefined; -let arkPritvate = globalThis["ArkPrivate"] || undefined; +interface ArkPrivate { + List: number; + Load(key: number): Object; +} +let flag: boolean = false; +let fastList: Object = undefined; +let arkPritvate: ArkPrivate = globalThis['ArkPrivate'] || undefined; if (arkPritvate !== undefined) { fastList = arkPritvate.Load(arkPritvate.List); } else { flag = true; } -if (flag || fastList == undefined) { +if (flag || fastList === undefined) { class HandlerList { - get(obj: List, prop: any) { - if (typeof prop === "symbol") { + get(obj: List, prop: any): T { + if (typeof prop === 'symbol') { return obj[prop]; } - let index = Number.parseInt(prop); + let index: number = Number.parseInt(prop); if (Number.isInteger(index)) { if (index < 0 || index >= obj.length) { - throw new RangeError("the index is 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" || - prop === "capacity" || - prop === "head" || - prop == "next") { + set(obj: List, prop: any, value: T): boolean { + if (prop === 'elementNum' || + prop === 'capacity' || + prop === 'head' || + prop === 'next') { obj[prop] = value; return true; } - let index = Number.parseInt(prop); + let index: number = Number.parseInt(prop); if (Number.isInteger(index)) { if (index < 0 || index >= obj.length) { - throw new RangeError("the index is out-of-bounds"); + throw new RangeError('the index is out-of-bounds'); } else { obj.set(index, value); return true; @@ -54,42 +58,42 @@ if (flag || fastList == undefined) { } return false; } - deleteProperty(obj: List, prop: any) { - let index = Number.parseInt(prop); + deleteProperty(obj: List, prop: any): boolean { + let index: number = Number.parseInt(prop); if (Number.isInteger(index)) { if (index < 0 || index >= obj.length) { - throw new RangeError("the index is out-of-bounds"); + throw new RangeError('the index is out-of-bounds'); } obj.removeByIndex(index); return true; } return false; } - has(obj: List, prop: any) { + has(obj: List, prop: any): boolean { return obj.has(prop); } - ownKeys(obj: List) { - let keys = []; - for (let i = 0; i < obj.length; i++) { + ownKeys(obj: List): Array { + let keys: Array = []; + for (let i: number = 0; i < obj.length; i++) { keys.push(i.toString()); } return keys; } - defineProperty(obj: List, prop: any, desc: any) { + defineProperty(): boolean { return true; } - getOwnPropertyDescriptor(obj: List, prop: any) { - let index = Number.parseInt(prop); + getOwnPropertyDescriptor(obj: List, prop: any): Object { + let index: number = Number.parseInt(prop); if (Number.isInteger(index)) { if (index < 0 || index >= obj.length) { - throw new RangeError("the index is 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"); + setPrototypeOf(): T { + throw new Error('Can setPrototype on List Object'); } } interface IterableIterator { @@ -110,21 +114,19 @@ if (flag || fastList == undefined) { private head: NodeObj; private elementNum: number; private capacity: number; - private next?: NodeObj; constructor() { this.head = undefined; - this.next = undefined; this.elementNum = 0; this.capacity = 10; return new Proxy(this, new HandlerList()); } - get length() { + get length(): number { return this.elementNum; } private getNode(index: number): NodeObj | undefined { if (index >= 0 && index < this.elementNum) { - let current = this.head; - for (let i = 0; i < index; i++) { + let current: NodeObj = this.head; + for (let i: number = 0; i < index; i++) { if (current !== undefined) { current = current.next; } @@ -135,8 +137,8 @@ if (flag || fastList == undefined) { } get(index: number): T { if (index >= 0 && index < this.elementNum) { - let current = this.head; - for (let i = 0; i < index && current != undefined; i++) { + let current: NodeObj = this.head; + for (let i: number = 0; i < index && current != undefined; i++) { current = current.next; } return current.element; @@ -144,11 +146,11 @@ if (flag || fastList == undefined) { return undefined; } add(element: T): boolean { - let node = new NodeObj(element); - if (this.head == undefined) { + let node: NodeObj = new NodeObj(element); + if (this.head === undefined) { this.head = node; } else { - let current = this.head; + let current: NodeObj = this.head; while (current.next !== undefined) { current = current.next; } @@ -166,8 +168,8 @@ if (flag || fastList == undefined) { if (this.head.element === element) { return true; } - let current = this.head; - while (current.next != undefined) { + let current: NodeObj = this.head; + while (current.next !== undefined) { current = current.next; if (current.element === element) { return true; @@ -183,8 +185,8 @@ if (flag || fastList == undefined) { if (!(obj instanceof List)) { return false; } else { - let e1 = this.head; - let e2 = obj.head; + let e1: NodeObj = this.head; + let e2: NodeObj = obj.head; if (e1 !== undefined && e2 !== undefined) { while (e1.next !== undefined && e2.next !== undefined) { e1 = e1.next; @@ -204,8 +206,9 @@ if (flag || fastList == undefined) { } } getIndexOf(element: T): number { - for (let i = 0; i < this.elementNum; i++) { - const curNode = this.getNode(i); + for (let i: number = 0; i < this.elementNum; i++) { + let curNode: NodeObj = undefined; + curNode = this.getNode(i); if (curNode !== undefined && curNode.element === element) { return i; } @@ -213,8 +216,9 @@ if (flag || fastList == undefined) { return -1; } getLastIndexOf(element: T): number { - for (let i = this.elementNum - 1; i >= 0; i--) { - const curNode = this.getNode(i); + for (let i: number = this.elementNum - 1; i >= 0; i--) { + let curNode: NodeObj = undefined; + curNode = this.getNode(i); if (curNode !== undefined && curNode.element === element) { return i; } @@ -223,14 +227,15 @@ if (flag || fastList == undefined) { } removeByIndex(index: number): T { if (index < 0 || index >= this.elementNum) { - throw new RangeError("the index is out-of-bounds"); + throw new RangeError('the index is out-of-bounds'); } - let oldNode = this.head; + let oldNode: NodeObj = this.head; if (index === 0) { oldNode = this.head; this.head = oldNode && oldNode.next; } else { - let prevNode = this.getNode(index - 1); + let prevNode: NodeObj = undefined; + prevNode = this.getNode(index - 1); oldNode = prevNode.next; prevNode.next = oldNode.next; } @@ -239,7 +244,8 @@ if (flag || fastList == undefined) { } remove(element: T): boolean { if (this.has(element)) { - let index = this.getIndexOf(element); + let index: number = 0; + index = this.getIndexOf(element); this.removeByIndex(index); return true; } @@ -247,13 +253,13 @@ if (flag || fastList == undefined) { } replaceAllElements(callbackfn: (value: T, index?: number, list?: List) => T, thisArg?: Object): void { - let index = 0; + let index: number = 0; if (this.head !== undefined) { - let current = this.head; + let current: NodeObj = this.head; if (this.elementNum > 0) { this.getNode(index).element = callbackfn.call(thisArg, this.head.element, index, this); } - while (current.next != undefined) { + while (current.next !== undefined) { current = current.next; this.getNode(++index).element = callbackfn.call(thisArg, current.element, index, this); } @@ -263,29 +269,32 @@ if (flag || fastList == undefined) { if (this.isEmpty()) { return undefined; } - let element = this.head.element; + let element: T = this.head.element; return element; } getLast(): T { if (this.isEmpty()) { return undefined; } - let newNode = this.getNode(this.elementNum - 1); - let element = newNode.element; + let newNode: NodeObj = undefined; + newNode = this.getNode(this.elementNum - 1); + let element: T = 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"); + throw new RangeError('the index is out-of-bounds'); } - let newNode = new NodeObj(element); + let newNode: NodeObj = undefined; + newNode = new NodeObj(element); if (index >= 0 && index < this.elementNum) { if (index === 0) { - const current = this.head; + let current: NodeObj = this.head; newNode.next = current; this.head = newNode; } else { - const prevNode = this.getNode(index - 1); + let prevNode: NodeObj = undefined; + prevNode = this.getNode(index - 1); newNode.next = prevNode.next; prevNode.next = newNode; } @@ -294,21 +303,23 @@ 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"); + throw new RangeError('the index is out-of-bounds'); } - const current = this.getNode(index); + let current: NodeObj = undefined; + current = this.getNode(index); current.element = 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++) { + let isSort: boolean = true; + for (let i: number = 0; i < this.elementNum; i++) { + for (let j: number = 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; + let temp: T = undefined; + temp = this.getNode(j).element; this.getNode(j).element = this.getNode(j + 1).element; this.getNode(j + 1).element = temp; } @@ -326,9 +337,10 @@ if (flag || fastList == undefined) { throw new RangeError(`the fromIndex or the toIndex is out-of-bounds`); } toIndex = toIndex >= this.elementNum ? this.elementNum : toIndex; - let list = new List(); - for (let i = fromIndex; i < toIndex; i++) { - let element = this.getNode(i).element; + let list: List = new List(); + for (let i: number = fromIndex; i < toIndex; i++) { + let element: T = undefined; + element = this.getNode(i).element; list.add(element); if (element === undefined) { break; @@ -338,14 +350,14 @@ if (flag || fastList == undefined) { } convertToArray(): Array { let arr: Array = []; - let index = 0; + let index: number = 0; if (this.elementNum <= 0) { return arr; } if (this.head !== undefined) { - let current = this.head; + let current: NodeObj = this.head; arr[index] = this.head.element; - while (current.next != undefined) { + while (current.next !== undefined) { current = current.next; arr[++index] = current.element; } @@ -353,29 +365,31 @@ if (flag || fastList == undefined) { return arr; } isEmpty(): boolean { - return this.elementNum == 0; + return this.elementNum === 0; } forEach(callbackfn: (value: T, index?: number, list?: List) => void, thisArg?: Object): void { - let index = 0; + let index: number = 0; if (this.head !== undefined) { - let current = this.head; + let current: NodeObj = this.head; if (this.elementNum > 0) { callbackfn.call(thisArg, this.head.element, index, this); } - while (current.next != undefined) { + while (current.next !== undefined) { current = current.next; callbackfn.call(thisArg, current.element, ++index, this); } } } [Symbol.iterator](): IterableIterator { - let count = 0; - let list = this; + let count: number = 0; + let list: List = this; return { next: function () { - let done = count >= list.elementNum; - let value = !done ? list.getNode(count++).element : undefined; + let done: boolean = false; + let value: T = undefined; + done = count >= list.elementNum; + value = done ? undefined : list.getNode(count++).element; return { done: done, value: value, diff --git a/container/list/native_module_list.cpp b/container/list/native_module_list.cpp index 0ffbde0b14e533cbba32d22745b74ca6209f100a..d38bb05e3f3b28516ee147ffb5ec8d9bdfa0f761 100644 --- a/container/list/native_module_list.cpp +++ b/container/list/native_module_list.cpp @@ -53,7 +53,7 @@ static napi_module listModule = { .nm_flags = 0, .nm_filename = nullptr, .nm_register_func = ListInit, - .nm_modname = "List", + .nm_modname = "util.List", .nm_priv = ((void *)0), .reserved = {0}, }; @@ -61,4 +61,4 @@ extern "C" __attribute__((constructor)) void RegisterModule() { napi_module_register(&listModule); } -} +} // namespace OHOS::Util diff --git a/container/plainarray/js_plainarray.ts b/container/plainarray/js_plainarray.ts index 5c38c4d85a6aeee3b02e644aaa165fc70241c1e1..66af31f177539a7623fe807766f68a4b5a2a3e48 100644 --- a/container/plainarray/js_plainarray.ts +++ b/container/plainarray/js_plainarray.ts @@ -13,18 +13,20 @@ * limitations under the License. */ declare function requireNapi(s: string): any; - -let flag = false; -let fastPlainArray = undefined; -let arkPritvate = globalThis["ArkPrivate"] || undefined; +interface ArkPrivate { + PlainArray: number; + Load(key: number): Object; +} +let flag: boolean = false; +let fastPlainArray: Object = undefined; +let arkPritvate: ArkPrivate = globalThis['ArkPrivate'] || undefined; if (arkPritvate !== undefined) { fastPlainArray = arkPritvate.Load(arkPritvate.PlainArray); } else { flag = true; } - if (flag || fastPlainArray === undefined) { - const PlainAbility = requireNapi("util.struct"); + const PlainAbility = requireNapi('util.struct'); interface IterableIterator { next: () => { value: T | undefined; @@ -39,14 +41,14 @@ if (flag || fastPlainArray === undefined) { } return false; } - defineProperty(target: PlainArray, p: any): boolean { - throw new Error("Can't define Property on PlainArray Object"); + defineProperty(): boolean { + throw new Error(`Can't define Property on PlainArray Object`); } - deleteProperty(target: PlainArray, p: any): boolean { - throw new Error("Can't delete Property on PlainArray Object"); + deleteProperty(): boolean { + throw new Error(`Can't delete Property on PlainArray Object`); } - setPrototypeOf(target: PlainArray, p: any): boolean { - throw new Error("Can't set Prototype on PlainArray Object"); + setPrototypeOf(): boolean { + throw new Error(`Can't set Prototype on PlainArray Object`); } } class PlainArray extends PlainAbility.PlainArrayClass { @@ -54,12 +56,12 @@ if (flag || fastPlainArray === undefined) { super(); return new Proxy(this, new HandlerPlainArray()); } - get length() { + get length(): number { return this.memberNumber; } add(key: number, value: T): void { - if (typeof key !== "number") { - throw new TypeError("the index is not integer"); + if (typeof key !== 'number') { + throw new TypeError('the index is not integer'); } this.addmember(key, value); } @@ -71,30 +73,32 @@ if (flag || fastPlainArray === undefined) { } } clone(): PlainArray { - let clone = new PlainArray(); + let clone: PlainArray = new PlainArray(); clone.memberNumber = this.memberNumber; clone.members.keys = this.members.keys.slice(); clone.members.values = this.members.values.slice(); return clone; } has(key: number): boolean { - if (typeof key !== "number") { + 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"); + if (typeof key !== 'number') { + throw new TypeError('the index is not integer'); } - let index = this.binarySearchAtPlain(key); + let index: number = 0; + index = this.binarySearchAtPlain(key); return this.members.values[index]; } getIndexOfKey(key: number): number { - if (typeof key !== "number") { - throw new TypeError("the index is not integer"); + if (typeof key !== 'number') { + throw new TypeError('the index is not integer'); } - let result = this.binarySearchAtPlain(key); + let result: number = 0; + result = this.binarySearchAtPlain(key); return result < 0 ? -1 : result; } getIndexOfValue(value: T): number { @@ -104,73 +108,83 @@ if (flag || fastPlainArray === undefined) { return this.memberNumber === 0; } getKeyAt(index: number): number { - if (typeof index !== "number") { - throw new TypeError("the index is not integer"); + 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"); + if (typeof key !== 'number') { + throw new TypeError('the index is not integer'); + } + let index: number = 0; + index = this.binarySearchAtPlain(key); + if (index < 0) { + return result; } - let index = this.binarySearchAtPlain(key); - if (index < 0) return result; return this.deletemember(index); } removeAt(index: number): T { - if (typeof index !== "number") { - throw new TypeError("the index is not integer"); + if (typeof index !== 'number') { + throw new TypeError('the index is not integer'); } let result: any = undefined; - if (index >= this.memberNumber || index < 0) return result; + if (index >= this.memberNumber || index < 0) { + return result; + } return this.deletemember(index); } removeRangeFrom(index: number, size: number): number { - if (typeof index !== "number" || typeof size !== "number") { - throw new TypeError("the index is not integer"); + 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'); } - 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; + let safeSize: number = 0; + 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 (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 RangeError("the index is out-of-bounds"); + throw new RangeError('the index is out-of-bounds'); } } toString(): string { - let result = new Array(); - for (let i = 0; i < this.memberNumber; i++) { - result.push(this.members.keys[i] + ":" + this.members.values[i]); + let result: string[] = []; + for (let i: number = 0; i < this.memberNumber; i++) { + result.push(this.members.keys[i] + ':' + this.members.values[i]); } - return result.join(","); + return result.join(','); } getValueAt(index: number): T { - if (typeof index !== "number") { - throw new TypeError("the index is not integer"); + 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, thisArg?: Object): void { - for (let i = 0; i < this.memberNumber; i++) { + for (let i: number = 0; i < this.memberNumber; i++) { callbackfn.call(thisArg, this.members.values[i], this.members.keys[i]); } } [Symbol.iterator](): IterableIterator<[number, T]> { - let data = this; - let count = 0; + let data: PlainArray = this; + let count: number = 0; return { next: function () { - let done = count >= data.memberNumber; - let value = !done ? [data.members.keys[count], data.members.values[count]] as [number, T] : undefined; + let done: boolean = false; + let value: [number, T] = undefined; + done = count >= data.memberNumber; + value = done ? undefined : [data.members.keys[count], data.members.values[count]] as [number, T]; count++; return { done: done, diff --git a/container/plainarray/native_module_plainarray.cpp b/container/plainarray/native_module_plainarray.cpp index d0ce8bc83f37bb99ebe9267c60d03d025447867f..07d2227c427651b2fa08febe4f66dae8834655e5 100644 --- a/container/plainarray/native_module_plainarray.cpp +++ b/container/plainarray/native_module_plainarray.cpp @@ -56,7 +56,7 @@ static napi_module plainArrayModule = { .nm_flags = 0, .nm_filename = nullptr, .nm_register_func = PlainArrayInit, - .nm_modname = "PlainArray", + .nm_modname = "util.PlainArray", .nm_priv = ((void*)0), .reserved = { 0 }, }; @@ -65,4 +65,4 @@ extern "C" __attribute__ ((constructor)) void RegisterModule() { napi_module_register(&plainArrayModule); } -} +} // namespace OHOS::Util diff --git a/container/queue/js_queue.ts b/container/queue/js_queue.ts index 339dc071c27a9c0ded474adfc32ef38fdae85334..f0366682494eccc649ca06ba0db98a4921173331 100644 --- a/container/queue/js_queue.ts +++ b/container/queue/js_queue.ts @@ -12,65 +12,69 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -let flag = false; -let fastQueue = undefined; -let arkPritvate = globalThis["ArkPrivate"] || undefined; +interface ArkPrivate { + Queue: number; + Load(key: number): Object; +} +let flag: boolean = false; +let fastQueue: Object = undefined; +let arkPritvate: ArkPrivate = globalThis['ArkPrivate'] || undefined; if (arkPritvate !== undefined) { fastQueue = arkPritvate.Load(arkPritvate.Queue); } else { flag = true; } -if (flag || fastQueue == undefined) { +if (flag || fastQueue === undefined) { class HandlerQueue { - private isOutBounds(obj: Queue, prop: any) { - let index = Number.parseInt(prop); + private isOutBounds(obj: Queue, prop: any): void { + let index: number = Number.parseInt(prop); if (Number.isInteger(index)) { if (index < 0 || index > obj.length) { console.log(index, obj.length) - throw new RangeError("the index is out-of-bounds"); + throw new RangeError('the index is out-of-bounds'); } } } get(obj: Queue, prop: any): T { - if (typeof prop === "symbol") { + if (typeof prop === 'symbol') { return obj[prop]; } this.isOutBounds(obj, prop); 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; } this.isOutBounds(obj, prop); - let index = Number(prop); + let index: number = Number(prop); if (index >= 0 && index <= obj.length && Number.isInteger(index)) { - obj[index] = value; - return true; + obj[index] = value; + return true; } return false; } - ownKeys(obj: Queue) { - let keys = []; - for (let i = 0; i < obj.length; i++) { + ownKeys(obj: Queue): Array { + let keys: string[] = []; + for (let i: number = 0; i < obj.length; i++) { keys.push(i.toString()); } return keys; } - defineProperty(obj: Queue, prop: any, desc: any) { + defineProperty(): boolean { return true; } - getOwnPropertyDescriptor(obj: Queue, prop: any) { + getOwnPropertyDescriptor(obj: Queue, prop: any): Object { this.isOutBounds(obj, prop); - let index = Number.parseInt(prop); + let index: number = 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 RangeError("Can setPrototype on Queue Object"); + setPrototypeOf(): T { + throw new RangeError('Can setPrototype on Queue Object'); } } interface IterableIterator { @@ -89,7 +93,7 @@ if (flag || fastQueue == undefined) { this.rear = 0; return new Proxy(this, new HandlerQueue()); } - get length(){ + get length(): number { return this.rear - this.front; } add(element: T): boolean { @@ -110,19 +114,20 @@ if (flag || fastQueue == undefined) { if (this.isEmpty()) { return undefined; } - let result = this[this.front]; + let result: T = undefined; + 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 k: number = 0; + let i: number = this.front; if (this.isEmpty()) { return; } else { while (true) { - callbackfn.call(thisArg,this[i], k,this); + callbackfn.call(thisArg, this[i], k, this); i = (i + 1) % this.capacity; k++; if (i === this.rear) { @@ -135,15 +140,17 @@ if (flag || fastQueue == undefined) { return this.length === this.capacity; } private isEmpty(): boolean { - return this.length == 0; + return this.length === 0; } [Symbol.iterator](): IterableIterator { - let count = this.front; - let queue = this; + let count: number = this.front; + let queue: Queue = this; return { next: function () { - let done = count == queue.rear; - let value = !done ? queue[count] : undefined; + let done: boolean = false; + let value: T = undefined; + done = count === queue.rear; + value = done ? undefined : queue[count]; 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 7a053528c7890143b36d33519793a564d237833a..d26b752922dc127c07c2f122382cdad698f15b40 100644 --- a/container/queue/native_module_queue.cpp +++ b/container/queue/native_module_queue.cpp @@ -54,7 +54,7 @@ static napi_module queueModule = { .nm_flags = 0, .nm_filename = nullptr, .nm_register_func = QueueInit, - .nm_modname = "Queue", + .nm_modname = "util.Queue", .nm_priv = ((void *)0), .reserved = {0}, }; @@ -62,4 +62,4 @@ extern "C" __attribute__((constructor)) void RegisterModule() { napi_module_register(&queueModule); } -} +} // namespace OHOS::Util diff --git a/container/stack/js_stack.ts b/container/stack/js_stack.ts index 62ff2233d234243d1eadbd234ef0324e695a0070..3703d132c7e29ed33d1864586c43401fd0d1efbc 100644 --- a/container/stack/js_stack.ts +++ b/container/stack/js_stack.ts @@ -12,65 +12,69 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -let flag = false; -let fastStack = undefined; -let arkPritvate = globalThis["ArkPrivate"] || undefined; +interface ArkPrivate { + Stack: number; + Load(key: number): Object; +} +let flag: boolean = false; +let fastStack: Object = undefined; +let arkPritvate: ArkPrivate = globalThis['ArkPrivate'] || undefined; if (arkPritvate !== undefined) { fastStack = arkPritvate.Load(arkPritvate.Stack); } else { flag = true; } -if (flag || fastStack == undefined) { +if (flag || fastStack === undefined) { class HandlerStack { - private isOutBounds(obj: Stack, prop: any) { - let index = Number.parseInt(prop); + private isOutBounds(obj: Stack, prop: any): void { + let index: number = Number.parseInt(prop); if (Number.isInteger(index)) { if (index < 0 || index >= obj.length) { - throw new RangeError("the index is out-of-bounds"); + throw new RangeError('the index is out-of-bounds'); } } } - get(obj: Stack, prop: any) { - if (typeof prop === "symbol") { + get(obj: Stack, prop: any): T { + if (typeof prop === 'symbol') { return obj[prop]; } this.isOutBounds(obj, prop); return obj[prop]; } - set(obj: Stack, prop: any, value: T) { - if (prop === "elementNum" || prop === "capacity") { + set(obj: Stack, prop: any, value: T): boolean { + if (prop === 'elementNum' || prop === 'capacity') { obj[prop] = value; return true; } this.isOutBounds(obj, prop); - let index = Number.parseInt(prop); + let index: number = Number.parseInt(prop); if (index >= 0 && index < obj.length && Number.isInteger(index)) { obj[index] = value; return true; } return false; } - ownKeys(obj: Stack) { - let keys = []; - let length = obj.length; - for (let i = 0; i < length; i++) { + ownKeys(obj: Stack): Array { + let keys: string[] = []; + let length: number = obj.length; + for (let i: number = 0; i < length; i++) { keys.push(i.toString()); } return keys; } - defineProperty(obj: Stack, prop: any, desc: any) { + defineProperty() { return true; } getOwnPropertyDescriptor(obj: Stack, prop: any) { this.isOutBounds(obj, prop); - let index = Number.parseInt(prop); + let index: number = 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 RangeError("Can setPrototype on Stack Object"); + setPrototypeOf(): T { + throw new RangeError('Can setPrototype on Stack Object'); } } interface IterableIterator { @@ -85,7 +89,7 @@ if (flag || fastStack == undefined) { constructor() { return new Proxy(this, new HandlerStack()); } - get length() { + get length(): number { return this.elementNum; } push(item: T): T { @@ -99,7 +103,8 @@ if (flag || fastStack == undefined) { if (this.isEmpty()) { return undefined; } - let result = this[this.length - 1]; + let result: T = undefined; + result = this[this.length - 1]; this.elementNum--; return result; } @@ -110,7 +115,7 @@ if (flag || fastStack == undefined) { return this[this.length - 1]; } locate(element: T): number { - for (let i = 0; i < this.length; i++) { + for (let i: number = 0; i < this.length; i++) { if (this[i] === element) { return i; } @@ -118,11 +123,11 @@ if (flag || fastStack == undefined) { return -1; } isEmpty(): boolean { - return this.elementNum == 0; + return this.elementNum === 0; } forEach(callbackfn: (value: T, index?: number, stack?: Stack) => void, thisArg?: Object): void { - for (let i = 0; i < this.length; i++) { + for (let i: number = 0; i < this.length; i++) { callbackfn.call(thisArg, this[i], i, this); } } @@ -133,12 +138,14 @@ if (flag || fastStack == undefined) { this.capacity = 1.5 * this.capacity; } [Symbol.iterator](): IterableIterator { - let count = 0; - let stack = this; + let count: number = 0; + let stack: Stack = this; return { next: function () { - let done = count >= stack.elementNum; - let value = !done ? stack[count++] : undefined; + let done: boolean = false; + let value: T = undefined; + done = count >= stack.elementNum; + value = done ? undefined : stack[count++]; return { done: done, value: value, diff --git a/container/stack/native_module_stack.cpp b/container/stack/native_module_stack.cpp index 0acbe9e5db00a4f2dcf05f00ea1bfc8de171ba57..658c75861fb84206fb3482dcb3e4806477b99d2f 100644 --- a/container/stack/native_module_stack.cpp +++ b/container/stack/native_module_stack.cpp @@ -54,7 +54,7 @@ static napi_module stackModule = { .nm_flags = 0, .nm_filename = nullptr, .nm_register_func = StackInit, - .nm_modname = "Stack", + .nm_modname = "util.Stack", .nm_priv = ((void *)0), .reserved = {0}, }; @@ -63,4 +63,4 @@ extern "C" __attribute__((constructor)) void RegisterModule() { napi_module_register(&stackModule); } -} +} // namespace OHOS::Util diff --git a/container/struct/js_struct.ts b/container/struct/js_struct.ts index 751136a8393fae34aaa8059b4d5f3ed1ee82a5d2..ce69f8d2c9079bb515c766fecf2622bc8b73ed28 100644 --- a/container/struct/js_struct.ts +++ b/container/struct/js_struct.ts @@ -15,36 +15,36 @@ type CompFun = (firstValue: T, secondValue: T) => boolean; function hashCode(element: any): number { - let str = String(element); - let hash = 0; + let str: string = ''; + str = String(element); + let hash: number = 0; if (hash === 0 && str.length > 0) { - for (let i = 0; i < str.length; i++) { - let char = str.charCodeAt(i); + for (let i: number = 0; i < str.length; i++) { + let char: number = 0; + char = str.charCodeAt(i); hash = ((hash << 5) - hash) + char; hash = hash & hash; } } return hash; } - -function insert(a: Array, index: number, value: T) { - for (let i = a.length; i > index; i--) { +function insert(a: Array, index: number, value: T): void { + for (let i: number = a.length; i > index; i--) { a[i] = a[i - 1]; } a[index] = value; } - enum ComparResult { LESS_THAN = -1, BIGGER_THAN = 1, EQUALS = 0 } - -function compareToString(string1: String, string2: String) { - let len1 = string1.length; - let len2 = string2.length; - let lim = (len1 > len2 ? len2 : len1); - let k = 0; +function compareToString(string1: String, string2: String): number { + let len1: number = string1.length; + let len2: number = string2.length; + let lim: number = 0; + lim = (len1 > len2 ? len2 : len1); + let k: number = 0; while (k < lim) { if (string1.charCodeAt(k) === string2.charCodeAt(k)) { k++; @@ -55,41 +55,43 @@ function compareToString(string1: String, string2: String) { } return string1.charCodeAt(k) > string2.charCodeAt(k) ? ComparResult.BIGGER_THAN : ComparResult.LESS_THAN; } - throw new Error("this function run error"); + throw new Error('this compare function run error'); } - function currencyCompare(a: any, b: any, compareFn?: CompFun): number { - if (a === b) return ComparResult.EQUALS; + 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; } - if (typeof a === "number" && typeof b === "number") { + if (typeof a === 'number' && typeof b === 'number') { if (a > b) { return ComparResult.BIGGER_THAN; } else { return ComparResult.LESS_THAN; } - } else if (typeof a === "string" && typeof b === "string") { + } else if (typeof a === 'string' && typeof b === 'string') { return compareToString(a, b); - } else if (typeof a === "string" && typeof b === "number") { + } else if (typeof a === 'string' && typeof b === 'number') { return ComparResult.BIGGER_THAN; - } else if (typeof a === "number" && typeof b === "string") { + } else if (typeof a === 'number' && typeof b === 'string') { return ComparResult.LESS_THAN; } - throw new Error("This form of comparison is not supported"); + throw new Error('This form of comparison is not supported'); } - function isIncludeToArray(array1: Array, array2: Array): boolean { - let newlist = array1.filter((val) => { + let newList: number[] = []; + newList = array1.filter((val) => { return array2.indexOf(val) > -1; }) - if (newlist.length == array2.length) return true; + if (newList.length === array2.length) { + return true; + } return false; } - class Pair{ key: K; value: V; @@ -97,16 +99,13 @@ class Pair{ this.key = key; this.value = value; } - entry(): [K, V] { return [this.key, this.value]; } - - toString() { + toString(): string { return `[#${this.key}: ${this.value}]`; } } - class PlainArrayMembers { keys: Array; values: Array; @@ -122,9 +121,9 @@ class PlainArrayClass { this.memberNumber = 0; this.members = new PlainArrayMembers(); } - - protected addmember(key: number, value: T) { - let index = this.binarySearchAtPlain(key); + protected addmember(key: number, value: T): void { + let index: number = 0; + index = this.binarySearchAtPlain(key); if (index >= 0) { this.members.keys[index] = key; this.members.values[index] = value; @@ -135,20 +134,18 @@ class PlainArrayClass { this.memberNumber++; } } - protected deletemember(index: number, safeSize: number = 1): T { this.memberNumber -= safeSize; this.members.keys.splice(index, safeSize); let removeValue = this.members.values.splice(index, safeSize)[0]; return removeValue; } - protected binarySearchAtPlain(key: number, startIndex: number = 0, endIndex: number = this.memberNumber): number { - let low = startIndex; - let high = endIndex - 1; + let low: number = startIndex; + let high: number = endIndex - 1; while (low <= high) { - let mid = (low + high) >>> 1; - let midVal = this.members.keys[mid]; + let mid: number = (low + high) >>> 1; + let midVal: number = this.members.keys[mid]; if (midVal < key) { low = mid + 1; } else { @@ -161,7 +158,6 @@ class PlainArrayClass { return -(low + 1); } } - class LightWeightMembers { hashs: Array; keys: Array; @@ -180,10 +176,11 @@ class LightWeightClass { this.memberNumber = 0; this.members = new LightWeightMembers(); } - - protected addmember(key: K, value: V = key as unknown as V) { - let hash = hashCode(key); - let index = this.binarySearchAtLightWeight(hash); + protected addmember(key: K, value: V = key as unknown as V): void { + let hash: number = 0; + hash = hashCode(key); + let index: number = 0; + index = this.binarySearchAtLightWeight(hash); if (index >= 0) { this.members.keys[index] = key; this.members.values[index] = value; @@ -196,53 +193,59 @@ class LightWeightClass { } if (this.capacity < this.memberNumber) this.ensureCapacity(1); } - protected getIndexByKey(key: K): number { - let hash = hashCode(key); - let index = this.binarySearchAtLightWeight(hash); + let hash: number = 0; + hash = hashCode(key); + let index: number = 0; + 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) return result; + let index: number = 0; + index = this.getIndexByKey(key); + if (index < 0) { + return result; + } this.memberNumber--; this.members.hashs.splice(index, 1); this.members.keys.splice(index, 1); return this.members.values.splice(index, 1)[0]; } - - protected ensureCapacity(addCapacity: number = 1) { - let tempCapacity = this.capacity + addCapacity; + protected ensureCapacity(addCapacity: number = 1): void { + let tempCapacity: number = 0; + tempCapacity = this.capacity + addCapacity; while (this.capacity < tempCapacity) { this.capacity = 2 * this.capacity; } } - protected getIndex(key: K): number { - let hash = hashCode(key); - let index = this.binarySearchAtLightWeight(hash); - if (index < 0) index = ~index; + let hash: number = 0; + hash = hashCode(key); + let index: number = 0; + index = this.binarySearchAtLightWeight(hash); + if (index < 0) { + index = ~index; + } return index; } - protected keyValueStringArray(): Array { let resultArray: Array = []; - for (let i = 0; i < this.memberNumber; i++) { - resultArray.push(JSON.stringify(this.members.keys[i]) + ":" + JSON.stringify(this.members.values[i])); + for (let i: number = 0; i < this.memberNumber; i++) { + resultArray.push(JSON.stringify(this.members.keys[i]) + ':' + JSON.stringify(this.members.values[i])); } return resultArray; } - protected binarySearchAtLightWeight(hash: number, startIndex: number = 0, endIndex: number = this.memberNumber): number { - let low = startIndex; - let high = endIndex - 1; + let low: number = startIndex; + let high: number = endIndex - 1; while (low <= high) { - let mid = (low + high) >>> 1; - let midVal = this.members.hashs[mid]; + let mid: number = 0; + mid = (low + high) >>> 1; + let midVal: number = 0; + midVal = this.members.hashs[mid]; if (midVal < hash) { low = mid + 1; } else { @@ -255,7 +258,6 @@ class LightWeightClass { return -(low + 1); } } - type RBTreeNodeColor = 0 | 1; const BLACK = 0; const RED = 1; @@ -290,12 +292,11 @@ class RBTreeClass { this.isChange = true; this.treeNodeArray = []; } - - get keyValueArray() { - let result = this.recordByMinToMax(); + get keyValueArray(): Array> { + let result: Array> = []; + result = this.recordByMinToMax(); return result; } - addNode(key: K, value: V = key as unknown as V): RBTreeClass { if (this.root === undefined) { this.root = new RBTreeNode(key, value); @@ -307,7 +308,6 @@ class RBTreeClass { } return this; } - addProcess(key: K, value: V): RBTreeClass { let leafNode: RBTreeNode | undefined = this.root; let parentNode: RBTreeNode = this.root as RBTreeNode; @@ -336,21 +336,20 @@ class RBTreeClass { this.isChange = true; return this; } - removeNode(key: K): V | undefined { - const removeNode = this.getNode(key); + let removeNode: RBTreeNode | undefined = undefined; + removeNode = this.getNode(key); if (removeNode === undefined) { return undefined; } else { - let result = removeNode.value; + let result: V = removeNode.value; this.removeNodeProcess(removeNode); return result; } } - - removeNodeProcess(removeNode: RBTreeNode) { + removeNodeProcess(removeNode: RBTreeNode): void { if (removeNode.left !== undefined && removeNode.right !== undefined) { - let successor = removeNode.right; + let successor: RBTreeNode | undefined = removeNode.right; while (successor.left !== undefined) { successor = successor.left; } @@ -359,7 +358,8 @@ class RBTreeClass { this.removeNodeProcess(successor); // only once return; } else { // one or zero child - let child = (removeNode.left === undefined ? removeNode.right : removeNode.left); + let child: RBTreeNode | undefined = undefined; + child = (removeNode.left === undefined ? removeNode.right : removeNode.left); if (removeNode.parent === undefined) { // remove is root if (child === undefined) { this.root = undefined; @@ -394,22 +394,21 @@ class RBTreeClass { this.isChange = true; } } - getNode(key: K): RBTreeNode | undefined { - if (this.root === 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; + findNode = currencyCompare(findNode.key, key, this.compFun) === ComparResult.BIGGER_THAN ? + findNode.left : findNode.right; } return findNode; } - findNode(value: V): RBTreeNode | undefined { let tempNode: RBTreeNode | undefined = undefined; this.recordByMinToMax(); - for (let i = 0; i < this.memberNumber; i++) { + for (let i: number = 0; i < this.memberNumber; i++) { if (this.treeNodeArray[i].value === value) { tempNode = this.treeNodeArray[i]; break; @@ -417,7 +416,6 @@ class RBTreeClass { } return tempNode; } - firstNode(): RBTreeNode | undefined { let tempNode: RBTreeNode | undefined = this.root; while (tempNode !== undefined && tempNode.left !== undefined) { @@ -425,7 +423,6 @@ class RBTreeClass { } return tempNode; } - lastNode(): RBTreeNode | undefined { let tempNode: RBTreeNode | undefined = this.root; while (tempNode !== undefined && tempNode.right !== undefined) { @@ -433,36 +430,36 @@ class RBTreeClass { } return tempNode; } - isEmpty(): boolean { return this.root === undefined; } - - setAll(map: RBTreeClass) { - let tempArray = map.recordByMinToMax(); - for (let i = 0; i < map.memberNumber; i++) { + setAll(map: RBTreeClass): void { + let tempArray: RBTreeNode[] = []; + tempArray = map.recordByMinToMax(); + for (let i: number = 0; i < map.memberNumber; i++) { this.addNode(tempArray[i].key, tempArray[i].value); } } - - clearTree() { + clearTree(): void { this.root = undefined; this.memberNumber = 0; } - private recordByMinToMax(): Array> { - if (!this.isChange) return this.treeNodeArray; - let stack = []; + if (!this.isChange) { + return this.treeNodeArray; + } + let stack: Array = []; this.treeNodeArray = []; - let node = this.root; + let node: RBTreeNode | undefined = this.root; while (node != undefined || stack.length) { while (node != undefined) { stack.push(node); node = node.left; } let tempNode = stack.pop(); - if (tempNode === undefined || tempNode === undefined) - throw new Error("this function run error"); + if (tempNode === undefined) { + throw new Error('this function run error'); + } node = tempNode; this.treeNodeArray.push(node); node = node.right; @@ -471,13 +468,13 @@ class RBTreeClass { this.memberNumber = this.treeNodeArray.length; return this.treeNodeArray; } - private lRotate(datumPointNode: RBTreeNode): RBTreeClass { - let newTopNode = datumPointNode.right; - if (newTopNode === undefined) - throw new Error("[rotate right error]: the right child node of the base node === undefined") + let newTopNode: RBTreeNode | undefined = datumPointNode.right; + if (newTopNode === undefined) { + throw new Error('[rotate right error]: the right child node of the base node === undefined') + } datumPointNode.right = newTopNode.left; - datumPointNode.right !== undefined ? datumPointNode.right.parent = datumPointNode : ""; + datumPointNode.right !== undefined ? datumPointNode.right.parent = datumPointNode : ''; newTopNode.parent = datumPointNode.parent; if (datumPointNode.parent === undefined) { this.root = newTopNode; @@ -490,14 +487,13 @@ class RBTreeClass { newTopNode.left = datumPointNode; return this; } - private rRotate(datumPointNode: RBTreeNode): RBTreeClass { - const newTopNode = datumPointNode.left; + let newTopNode: RBTreeNode | undefined = datumPointNode.left; if (newTopNode === undefined) { - throw new Error("[rotate right error]: the left child node of the base node === undefined") + throw new Error('[rotate right error]: the left child node of the base node === undefined') } datumPointNode.left = newTopNode.right; - datumPointNode.left !== undefined ? datumPointNode.left.parent = datumPointNode : ""; + datumPointNode.left !== undefined ? datumPointNode.left.parent = datumPointNode : ''; newTopNode.parent = datumPointNode.parent if (datumPointNode.parent === undefined) { this.root = newTopNode; @@ -510,9 +506,8 @@ class RBTreeClass { newTopNode.right = datumPointNode; return this; } - private insertRebalance(fixNode: RBTreeNode): RBTreeClass { - let parentNode = fixNode.parent; + let parentNode: RBTreeNode | undefined = fixNode.parent; while (this.getColor(parentNode) === RED && parentNode !== undefined && parentNode.parent !== undefined) { @@ -560,16 +555,15 @@ class RBTreeClass { fixNode = grandpaNode; parentNode = fixNode.parent; } else { - throw new Error("Exceptions after adding") + throw new Error('Exceptions after adding') } } - this.root ? this.root.color = BLACK : ""; + this.root ? this.root.color = BLACK : ''; return this; } - - private deleteRebalance(fixNode: RBTreeNode) { + private deleteRebalance(fixNode: RBTreeNode): void { while (this.getColor(fixNode) === BLACK && fixNode !== this.root && fixNode.parent) { - let sibling: RBTreeNode | undefined; + let sibling: RBTreeNode | undefined = undefined; if (fixNode === fixNode.parent.left) { sibling = fixNode.parent.right; if (this.getColor(sibling) === RED) { @@ -608,7 +602,7 @@ class RBTreeClass { .lRotate(fixNode.parent); break; } else { - throw new Error("Adjust the error after the error is deleted") + throw new Error('Adjust the error after the error is deleted') } } else { sibling = fixNode.parent.left; @@ -649,27 +643,24 @@ class RBTreeClass { .rRotate(fixNode.parent); break; } else { - throw new Error("Adjust the error after the error is deleted") + throw new Error('Adjust the error after the error is deleted') } } } this.setColor(BLACK, fixNode) } - private getColor(node: RBTreeNode | undefined): RBTreeNodeColor { return node === undefined ? BLACK : node.color; } - private setColor(color: RBTreeNodeColor, node: RBTreeNode | undefined): RBTreeClass { if (node === undefined) { - throw new Error("Wrong color setting") + throw new Error('Wrong color setting') } else { node.color = color } return this; } } - const MAXcapacity = 1 << 30; const LOADER_FACTOR = 0.75; class DictionaryClass { @@ -686,14 +677,15 @@ class DictionaryClass { this.capacity = 16; } - get keyValueArray() { - let result = this.keyValues(); + get keyValueArray(): Pair[] { + let result: Pair[] = []; + result = this.keyValues(); return result; } - protected getHashIndex(key: K): number { - let h; - let hash = ((key === null) ? 0 : ((h = hashCode(key)) ^ (h >>> 16))); + let h: number = 0; + let hash: number = 0; + hash = ((key === null) ? 0 : ((h = hashCode(key)) ^ (h >>> 16))); if (this.expandCapacity()) { this.keyValues(); this.memberNumber = 0; @@ -704,34 +696,37 @@ class DictionaryClass { } this.memberNumber++; } - let n = this.power(this.capacity); + let n: number = 0; + n = this.power(this.capacity); return (n - 1) & hash; } - - private power(size: number) { - let n = 1; - let temp = size; + private power(size: number): number { + let n: number = 1; + let temp: number = size; while (temp >>> 1 != 1) { n++; temp = temp >>> 1; } return n; } - private keyValues(): Pair[] { - if (!this.isChange) return this.memberArray; + 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]]; + let keys: number[] = []; + keys = Object.keys(this.tableLink).map((item) => parseInt(item)); + for (let i: number = 0; i < keys.length; i++) { + let members: RBTreeClass | LinkedList> = undefined; + members = this.tableLink[keys[i]]; if (members instanceof RBTreeClass) { - let tempArray = members.keyValueArray; - for (let i = 0; i < members.memberNumber; i++) { + let tempArray: RBTreeNode[] = members.keyValueArray; + for (let i: number = 0; i < members.memberNumber; i++) { this.memberArray.push(new Pair(tempArray[i].key, tempArray[i].value)); } } else { if (members != undefined && !members.isEmpty()) { - let current = members.getHead(); + let current: Node> = members.getHead(); while (current != undefined) { this.memberArray.push(current.element); current = current.next; @@ -740,28 +735,30 @@ class DictionaryClass { } } this.memberNumber = this.memberArray.length; - let valuePairs = this.memberArray; + let valuePairs: Pair[] = this.memberArray; return valuePairs; } - - protected expandCapacity() { - let capacityChange = false; + protected expandCapacity(): boolean { + let capacityChange: boolean = 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 { - this.isChange = true; - if (!this.hasKey(key)) this.memberNumber++; - const position = this.getHashIndex(key); - let members = this.tableLink[position]; + if (!this.hasKey(key)) { + this.memberNumber++; + } + let position: number = 0; + position = this.getHashIndex(key); + let members: LinkedList> | RBTreeClass = undefined; + members = this.tableLink[position]; if (members instanceof LinkedList && members.count >= 8) { - let newElement = new RBTreeClass(); - let current = members.getHead(); + let newElement: RBTreeClass = new RBTreeClass(); + let current: Node> = undefined; + current = members.getHead(); while (current != null || current != undefined) { if (!(current.element instanceof Pair)) return false; newElement.addNode(current.element.key, current.element.value); @@ -775,7 +772,7 @@ class DictionaryClass { this.tableLink[position] = members; return true; } else { - if (this.tableLink[position] == undefined) { + if (this.tableLink[position] === undefined) { members = new LinkedList>(); } if (!this.replaceMember(key, value)) { @@ -785,12 +782,16 @@ class DictionaryClass { return true; } } - protected replaceMember(key: K, value: V = key as unknown as V): boolean { - const position = this.getHashIndex(key); - const members = this.tableLink[position] as LinkedList>; - if (members === null || members === undefined) return false; - let current = members.getHead(); + let position: number = 0; + position = this.getHashIndex(key); + let members: LinkedList> = undefined; + members = this.tableLink[position] as LinkedList>; + if (members === null || members === undefined) { + return false; + } + let current: Node> = undefined; + current = members.getHead(); while (current != undefined) { if (current.element.key === key) { current.element.value = value; @@ -800,18 +801,21 @@ class DictionaryClass { } return false; } - protected getValueByKey(key: K): V | undefined { - const position = this.getHashIndex(key); - const members = this.tableLink[position]; + let position: number = 0; + position = this.getHashIndex(key); + let members: LinkedList> | RBTreeClass = undefined; + members = this.tableLink[position]; if (members instanceof RBTreeClass) { - let resultNode = members.getNode(key); + let resultNode: RBTreeNode | undefined = undefined; + resultNode = members.getNode(key); if (resultNode === undefined) return undefined; return resultNode.value; } else { if (members != undefined && !members.isEmpty()) { members as LinkedList>; - let current = members.getHead(); + let current: Node> = undefined; + current = members.getHead(); while (current != undefined) { if (current.element.key === key) { return current.element.value; @@ -822,12 +826,13 @@ class DictionaryClass { } return undefined; } - protected removeMember(key: K): V | undefined { - const position = this.getHashIndex(key); - const members = this.tableLink[position]; + let position: number = 0; + position = this.getHashIndex(key); + let members: LinkedList> | RBTreeClass = undefined; + members = this.tableLink[position]; if (members instanceof RBTreeClass) { - let result = members.removeNode(key); + let result: V | undefined = members.removeNode(key); if (result != undefined) { this.isChange = true; this.memberNumber--; @@ -835,10 +840,11 @@ class DictionaryClass { } } else { if (members != undefined && !members.isEmpty()) { - let current = members.getHead(); + let current: Node> = undefined; + current = members.getHead(); while (current != undefined) { if (current.element.key === key) { - const result = current.element.value; + let result: V | undefined = current.element.value; members.remove(current.element); if (members.isEmpty()) { delete this.tableLink[position]; @@ -853,22 +859,25 @@ class DictionaryClass { } return undefined; } - protected clear() { this.tableLink = {}; this.memberNumber = 0; this.isChange = true; this.capacity = 16; } - protected hasKey(key: K): boolean { - const position = this.getHashIndex(key); - const members = this.tableLink[position]; - if (members === undefined || members === undefined) return false; + let position: number = 0; + position = this.getHashIndex(key); + let members: LinkedList> | RBTreeClass = undefined; + members = this.tableLink[position]; + if (members === undefined || members === undefined) { + return false; + } if (members instanceof RBTreeClass) { return members.getNode(key) !== undefined; } - let current = members.getHead(); + let current: Node> = undefined; + current = members.getHead(); while (current != undefined && current != undefined) { if (current.element.key === key) { return true; @@ -877,24 +886,23 @@ class DictionaryClass { } return false; } - protected setAll(map: DictionaryClass): void { - let memebers = map.keyValues(); - for (let i = 0; i < memebers.length; i++) { + let memebers: Pair[] = []; + memebers = map.keyValues(); + for (let i: number = 0; i < memebers.length; i++) { this.put(memebers[i].key, memebers[i].value); } } - protected Values(): V[] { - const values = []; - const valuePairs = this.keyValues(); - for (let i = 0; i < valuePairs.length; i++) { + let values: Array = []; + let valuePairs: Pair[] = []; + valuePairs = this.keyValues(); + for (let i: number = 0; i < valuePairs.length; i++) { values.push(valuePairs[i].value); } return values; } } - class Node { element: T; next: Node | undefined; @@ -912,11 +920,11 @@ class LinkedList { this.next = undefined; this.head = undefined; } - - push(element: T) { - const node = new Node(element); - let current; - if (this.head == undefined) { + push(element: T): void { + let node: Node = undefined; + node = new Node(element); + let current: undefined | Node; + if (this.head === undefined) { this.head = node; } else { current = this.head; @@ -927,14 +935,13 @@ class LinkedList { } this.count++; } - - removeAt(index: number) { + removeAt(index: number): T { if (index >= 0 && index < this.count) { - let current = this.head; + let current: Node = this.head; if (index === 0 && current != undefined) { this.head = current.next; } else { - const previous = this.getElementAt(index--); + let previous: Node = this.getElementAt(index--); if (previous !== undefined) { current = previous.next; previous.next = (current === undefined ? undefined : current.next); @@ -947,28 +954,27 @@ class LinkedList { } return undefined; } - - getElementAt(index: number) { + getElementAt(index: number): Node { if (index > 0 && index < this.count) { - let current = this.head; - for (let i = 0; i < index && current != undefined; i++) { + let current: Node = this.head; + for (let i: number = 0; i < index && current != undefined; i++) { current = current.next; } return current; } return undefined; } - - insert(element: T, index: number) { + insert(element: T, index: number): boolean { if (index >= 0 && index <= this.count) { - const node = new Node(element); + let node: Node = undefined; + node = new Node(element); if (index === 0) { node.next = this.head; this.head = node; } else { - const previous = this.getElementAt(index--); + let previous: Node = this.getElementAt(index--); if (previous === undefined) - throw new Error("data storage error"); + throw new Error('data storage error'); node.next = previous.next; previous.next = node; } @@ -977,10 +983,9 @@ class LinkedList { } return false; } - - indexOf(element: T, compareFn?: CompFun) { - let current = this.head; - for (let i = 0; i < this.count && current != undefined; i++) { + indexOf(element: T, compareFn?: CompFun): number { + let current: Node = this.head; + for (let i: number = 0; i < this.count && current != undefined; i++) { if (currencyCompare(element, current.element, compareFn) === ComparResult.EQUALS) { return i; } @@ -988,38 +993,34 @@ class LinkedList { } return -1; } - - remove(element: T, compareFn?: CompFun) { + remove(element: T, compareFn?: CompFun): void { this.removeAt(this.indexOf(element, compareFn)); } - - clear() { + clear(): void { this.head = undefined; this.count = 0; } - - isEmpty() { + isEmpty(): boolean { return this.count === 0; } - - getHead() { + getHead(): Node { return this.head; } - - toString() { - if (this.head == undefined) { - return ""; + toString(): string { + if (this.head === undefined) { + return ''; } - let objString = `${this.head.element}`; - let current = this.head.next; - for (let i = 1; i < this.count && current != undefined; i++) { + let objString: string = ''; + objString = `${this.head.element}`; + let current: Node = undefined; + current = this.head.next; + for (let i: number = 1; i < this.count && current != undefined; i++) { objString = `${objString}, ${current.element}`; current = current.next; } return objString; } } - export default { isIncludeToArray, LightWeightClass, diff --git a/container/struct/native_module_struct.cpp b/container/struct/native_module_struct.cpp index 084f1705b014ee7a5d5e5e9269834a17d907ffe7..7272752f5178c312b74fa5126df289837c3468b9 100644 --- a/container/struct/native_module_struct.cpp +++ b/container/struct/native_module_struct.cpp @@ -56,7 +56,7 @@ static napi_module structModule = { .nm_flags = 0, .nm_filename = nullptr, .nm_register_func = StructInit, - .nm_modname = "struct", + .nm_modname = "util.struct", .nm_priv = ((void*)0), .reserved = { 0 }, }; @@ -65,4 +65,4 @@ extern "C" __attribute__ ((constructor)) void RegisterModule() { napi_module_register(&structModule); } -} +} // namespace OHOS::Util diff --git a/container/treemap/js_treemap.ts b/container/treemap/js_treemap.ts index 1305aeb6e277c8d000101fc407d384eda622af22..05232e0ee9011f6445a1e5a929edd84cdc272e7a 100644 --- a/container/treemap/js_treemap.ts +++ b/container/treemap/js_treemap.ts @@ -13,18 +13,20 @@ * limitations under the License. */ declare function requireNapi(s: string): any; - -let flag = false; -let fastTreeMap = undefined; -let arkPritvate = globalThis["ArkPrivate"] || undefined; +interface ArkPrivate { + TreeMap: number; + Load(key: number): Object; +} +let flag: boolean = false; +let fastTreeMap: Object = undefined; +let arkPritvate: ArkPrivate = globalThis['ArkPrivate'] || undefined; if (arkPritvate !== undefined) { fastTreeMap = arkPritvate.Load(arkPritvate.TreeMap); } else { flag = true; } - if (flag || fastTreeMap === undefined) { - const RBTreeAbility = requireNapi("util.struct") + let RBTreeAbility = requireNapi('util.struct'); interface IterableIterator { next: () => { value: T | undefined; @@ -39,14 +41,14 @@ if (flag || fastTreeMap === undefined) { } return false; } - defineProperty(target: TreeMap, p: any): boolean { - throw new Error("Can't define Property on TreeMap Object"); + defineProperty(): boolean { + throw new Error(`Can't define Property on TreeMap Object`); } - deleteProperty(target: TreeMap, p: any): boolean { - throw new Error("Can't delete Property on TreeMap Object"); + deleteProperty(): boolean { + throw new Error(`Can't delete Property on TreeMap Object`); } - setPrototypeOf(target: TreeMap, p: any): boolean { - throw new Error("Can't set Prototype on TreeMap Object"); + setPrototypeOf(): boolean { + throw new Error(`Can't set Prototype on TreeMap Object`); } } class TreeMap { @@ -55,10 +57,10 @@ if (flag || fastTreeMap === undefined) { this.constitute = new RBTreeAbility.RBTreeClass(comparator); return new Proxy(this, new HandlerTreeMap()); } - get length() { + get length(): number { return this.constitute.memberNumber; } - isEmpty() { + isEmpty(): boolean { return this.constitute.memberNumber === 0; } hasKey(key: K): boolean { @@ -68,18 +70,24 @@ if (flag || fastTreeMap === undefined) { return this.constitute.findNode(value) !== undefined; } get(key: K): V { - let tempNode = this.constitute.getNode(key); - if (tempNode === undefined) return tempNode; + let tempNode: any = undefined; + tempNode = this.constitute.getNode(key); + if (tempNode === undefined) { + return tempNode; + } return tempNode.value; } getFirstKey(): K { - let tempNode = this.constitute.firstNode(); - if (tempNode === undefined) + let tempNode: any = undefined; + tempNode = this.constitute.firstNode(); + if (tempNode === undefined) { return tempNode; + } return tempNode.key; } getLastKey(): K { - let tempNode = this.constitute.lastNode(); + let tempNode: any = undefined; + tempNode = this.constitute.lastNode(); if (tempNode === undefined) return tempNode; return tempNode.key; @@ -97,36 +105,52 @@ if (flag || fastTreeMap === undefined) { this.constitute.clearTree(); } getLowerKey(key: K): K { - let result: any = undefined; - let tempNode = this.constitute.getNode(key); - if (tempNode === undefined) return tempNode; - if (tempNode.left !== undefined) return tempNode.left.key; - let node = tempNode; + let result: K | undefined = undefined; + let tempNode: any = undefined; + tempNode = this.constitute.getNode(key); + if (tempNode === undefined) { + return tempNode; + } + if (tempNode.left !== undefined) { + return tempNode.left.key; + } + let node: any = tempNode; while (node.parent !== undefined) { - if (node.parent.right === node) return node.parent.key; + if (node.parent.right === node) { + return node.parent.key; + } node = node.parent; } return result; } getHigherKey(key: K): K { - let result: any = undefined; - let tempNode = this.constitute.getNode(key); - if (tempNode === undefined) return tempNode; - if (tempNode.right !== undefined) return tempNode.right.key; - let node = tempNode; + let result: K | undefined = undefined; + let tempNode: any = undefined; + tempNode = this.constitute.getNode(key); + if (tempNode === undefined) { + return tempNode; + } + if (tempNode.right !== undefined) { + return tempNode.right.key; + } + let node: any = tempNode; while (node.parent !== undefined) { - if (node.parent.left === node) return node.parent.key; + if (node.parent.left === node) { + return node.parent.key; + } node = node.parent; } return result; } keys(): IterableIterator { - let data = this.constitute; - let count = 0; + let data: any = this.constitute; + let count: number = 0; return { next: function () { - let done = count >= data.memberNumber; - let value = !done ? data.keyValueArray[count].key : undefined; + let done: boolean = false; + let value: K = undefined; + done = count >= data.memberNumber; + value = done ? undefined : data.keyValueArray[count].key; count++; return { done: done, @@ -136,12 +160,14 @@ if (flag || fastTreeMap === undefined) { }; } values(): IterableIterator { - let data = this.constitute; - let count = 0; + let data: any = this.constitute; + let count: number = 0; return { next: function () { - let done = count >= data.memberNumber; - let value = !done ? data.keyValueArray[count].value : undefined; + let done: boolean = false; + let value: V = undefined; + done = count >= data.memberNumber; + value = done ? undefined : data.keyValueArray[count].value; count++; return { done: done, @@ -151,26 +177,31 @@ if (flag || fastTreeMap === undefined) { }; } replace(key: K, newValue: V): boolean { - let targetNode = this.constitute.getNode(key); - if (targetNode === undefined) return false; + let targetNode: any = 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 tagetArray = data.keyValueArray; - for (let i = 0; i < data.memberNumber; i++) { + let data: any = this.constitute; + let tagetArray: Array = []; + tagetArray = data.keyValueArray; + for (let i: number = 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 count = 0; + let data: any = this.constitute; + let count: number = 0; return { next: function () { - let done = count >= data.memberNumber; - let value = !done ? data.keyValueArray[count].entry() : undefined; + let done: boolean = false; + let value: [K, V] = undefined; + done = count >= data.memberNumber; + value = done ? undefined : data.keyValueArray[count].entry(); count++; return { done: done, @@ -180,19 +211,7 @@ if (flag || fastTreeMap === undefined) { }; } [Symbol.iterator](): IterableIterator<[K, V]> { - let data = this.constitute; - let count = 0; - return { - next: function () { - let done = count >= data.memberNumber; - let value = !done ? data.keyValueArray[count].entry() : undefined; - count++; - return { - done: done, - value: value, - }; - }, - }; + return this.entries(); } } Object.freeze(TreeMap); diff --git a/container/treemap/native_module_treemap.cpp b/container/treemap/native_module_treemap.cpp index c330c0c1edc4f4e268e8c84cbe1855ff35a4c741..003081fabba0bed7526eb49367742d7ae344717a 100644 --- a/container/treemap/native_module_treemap.cpp +++ b/container/treemap/native_module_treemap.cpp @@ -56,7 +56,7 @@ static napi_module treeMapModule = { .nm_flags = 0, .nm_filename = nullptr, .nm_register_func = TreeMapInit, - .nm_modname = "TreeMap", + .nm_modname = "util.TreeMap", .nm_priv = ((void*)0), .reserved = { 0 }, }; @@ -65,4 +65,4 @@ extern "C" __attribute__ ((constructor)) void RegisterModule() { napi_module_register(&treeMapModule); } -} +} // namespace OHOS::Util diff --git a/container/treeset/js_treeset.ts b/container/treeset/js_treeset.ts index 6ed0e9e4af264c7ff4ded5d932eb1e7c893aa3bb..6a3e608f33697ed22ab5bad768037892f7c211f4 100644 --- a/container/treeset/js_treeset.ts +++ b/container/treeset/js_treeset.ts @@ -13,18 +13,20 @@ * limitations under the License. */ declare function requireNapi(s: string): any; - -let flag = false; -let fastTreeSet = undefined; -let arkPritvate = globalThis["ArkPrivate"] || undefined; +interface ArkPrivate { + TreeSet: number; + Load(key: number): Object; +} +let flag: boolean = false; +let fastTreeSet: Object = undefined; +let arkPritvate: ArkPrivate = globalThis['ArkPrivate'] || undefined; if (arkPritvate !== undefined) { fastTreeSet = arkPritvate.Load(arkPritvate.TreeSet); } else { flag = true; } - if (flag || fastTreeSet === undefined) { - const RBTreeAbility = requireNapi("util.struct"); + const RBTreeAbility = requireNapi('util.struct'); interface IterableIterator { next: () => { value: T | undefined; @@ -39,14 +41,14 @@ if (flag || fastTreeSet === undefined) { } return false; } - defineProperty(target: TreeSet, p: any): boolean { - throw new Error("Can't define Property on TreeSet Object"); + defineProperty(): boolean { + throw new Error(`Can't define Property on TreeSet Object`); } - deleteProperty(target: TreeSet, p: any): boolean { - throw new Error("Can't delete Property on TreeSet Object"); + deleteProperty(): boolean { + throw new Error(`Can't delete Property on TreeSet Object`); } - setPrototypeOf(target: TreeSet, p: any): boolean { - throw new Error("Can't set Prototype on TreeSet Object"); + setPrototypeOf(): boolean { + throw new Error(`Can't set Prototype on TreeSet Object`); } } class TreeSet { @@ -55,7 +57,7 @@ if (flag || fastTreeSet === undefined) { this.constitute = new RBTreeAbility.RBTreeClass(comparator); return new Proxy(this, new HandlerTreeSet()); } - get length() { + get length(): number { return this.constitute.memberNumber; } isEmpty(): boolean { @@ -69,67 +71,94 @@ if (flag || fastTreeSet === undefined) { return true; } remove(value: T): boolean { - let result = this.constitute.removeNode(value); + let result: T = undefined; + result = this.constitute.removeNode(value); return result !== undefined; } clear() { this.constitute.clearTree(); } getFirstValue(): T { - let tempNode = this.constitute.firstNode(); - if (tempNode === undefined) return tempNode; + let tempNode: any = undefined; + tempNode = this.constitute.firstNode(); + if (tempNode === undefined) { + return tempNode; + } return tempNode.key; } getLastValue(): T { - let tempNode = this.constitute.lastNode(); - if (tempNode === undefined) return tempNode; + let tempNode: any = undefined; + tempNode = this.constitute.lastNode(); + 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) return tempNode; - if (tempNode.left !== undefined) return tempNode.left.key; - let node = tempNode; + let tempNode: any = undefined; + tempNode = this.constitute.getNode(key); + if (tempNode === undefined) { + return tempNode; + } + if (tempNode.left !== undefined) { + return tempNode.left.key; + } + let node: any = tempNode; while (node.parent !== undefined) { - if (node.parent.right === node) return node.parent.key; + if (node.parent.right === node) { + return node.parent.key; + } node = node.parent; // node.parent.left === node is true; } - return result; + return undefined; } getHigherValue(key: T): T { - let result: any = undefined; - let tempNode = this.constitute.getNode(key); - if (tempNode === undefined) return tempNode; - if (tempNode.right !== undefined) return tempNode.right.key; - let node = tempNode; + let tempNode: any = undefined; + tempNode = this.constitute.getNode(key); + if (tempNode === undefined) { + return tempNode; + } + if (tempNode.right !== undefined) { + return tempNode.right.key; + } + let node: any = tempNode; while (node.parent !== undefined) { - if (node.parent.left === node) return node.parent.key; + if (node.parent.left === node) { + return node.parent.key; + } node = node.parent; // node.parent.right === node is true; } - return result; + undefined; } popFirst(): T { - let firstNode = this.constitute.firstNode(); - if (firstNode === undefined) return firstNode; - let value = firstNode.value; + let firstNode: any = undefined; + firstNode = this.constitute.firstNode(); + if (firstNode === undefined) { + return firstNode; + } + let value: T = firstNode.value; this.constitute.removeNodeProcess(firstNode); return value; } popLast(): T { - let lastNode = this.constitute.lastNode(); - if (lastNode === undefined) return lastNode; - let value = lastNode.value; + let lastNode: any = undefined; + lastNode = this.constitute.lastNode(); + if (lastNode === undefined) { + return lastNode; + } + let value: T = lastNode.value; this.constitute.removeNodeProcess(lastNode); return value; } values(): IterableIterator { - let data = this.constitute; - let count = 0; + let data: any = this.constitute; + let count: number = 0; return { next: function () { - let done = count >= data.memberNumber; - let value = !done ? data.keyValueArray[count].value as T : undefined; + let done: boolean = false; + let value: T = undefined; + done = count >= data.memberNumber; + value = done ? undefined : data.keyValueArray[count].value as T; count++; return { done: done, @@ -140,19 +169,21 @@ if (flag || fastTreeSet === undefined) { } forEach(callbackfn: (value?: T, key?: T, set?: TreeSet) => void, thisArg?: Object): void { - let data = this.constitute; - let tagetArray = data.keyValueArray; - for (let i = 0; i < data.memberNumber; i++) { + let data: any = this.constitute; + let tagetArray: Array = data.keyValueArray; + for (let i: number = 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 count = 0; + let data: any = this.constitute; + let count: number = 0; return { next: function () { - let done = count >= data.memberNumber; - let value = !done ? data.keyValueArray[count].entry() : undefined; + let done: boolean = false; + let value: [T, T] = undefined; + done = count >= data.memberNumber; + value = done ? undefined : data.keyValueArray[count].entry(); count++; return { done: done, @@ -162,19 +193,7 @@ if (flag || fastTreeSet === undefined) { }; } [Symbol.iterator](): IterableIterator { - let data = this.constitute; - let count = 0; - return { - next: function () { - let done = count >= data.memberNumber; - let value = !done ? data.keyValueArray[count].key : undefined; - count++; - return { - done: done, - value: value, - }; - }, - }; + return this.values(); } } Object.freeze(TreeSet); diff --git a/container/treeset/native_module_treeset.cpp b/container/treeset/native_module_treeset.cpp index d416e5f98c976db9688d66e67577d14e098851bb..0a0c37a183e751f2ada6f8f5db726d7806bebe93 100644 --- a/container/treeset/native_module_treeset.cpp +++ b/container/treeset/native_module_treeset.cpp @@ -56,7 +56,7 @@ static napi_module treeSetModule = { .nm_flags = 0, .nm_filename = nullptr, .nm_register_func = TreeSetInit, - .nm_modname = "TreeSet", + .nm_modname = "util.TreeSet", .nm_priv = ((void*)0), .reserved = { 0 }, }; @@ -65,4 +65,4 @@ extern "C" __attribute__ ((constructor)) void RegisterModule() { napi_module_register(&treeSetModule); } -} +} // namespace OHOS::Util diff --git a/container/vector/js_vector.ts b/container/vector/js_vector.ts index feec128289aa4f28224dd9341e9542501cc51d2e..202fff939f1508ec2079ee0eed29a8e8587d8aab 100644 --- a/container/vector/js_vector.ts +++ b/container/vector/js_vector.ts @@ -12,40 +12,44 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -let flag = false; -let fastVector = undefined; -let arkPritvate = globalThis["ArkPrivate"] || undefined; +interface ArkPrivate { + Vector: number; + Load(key: number): Object; +} +let flag: boolean = false; +let fastVector: object = undefined; +let arkPritvate: ArkPrivate = globalThis['ArkPrivate'] || undefined; if (arkPritvate !== undefined) { fastVector = arkPritvate.Load(arkPritvate.Vector); } else { flag = true; } -if (flag || fastVector == undefined) { +if (flag || fastVector === undefined) { class HandlerVector { - private isOutBounds(obj: Vector, prop: any) { - let index = Number.parseInt(prop); + private isOutBounds(obj: Vector, prop: any): void { + let index: number = Number.parseInt(prop); if (Number.isInteger(index)) { if (index < 0 || index >= obj.length) { - throw new RangeError("the index is out-of-bounds"); + throw new RangeError('the index is out-of-bounds'); } } } - get(obj: Vector, prop: any) { - if (typeof prop === "symbol") { + get(obj: Vector, prop: any): T { + if (typeof prop === 'symbol') { return obj[prop]; } this.isOutBounds(obj, prop); return obj[prop]; } - set(obj: Vector, prop: any, value: T) { - if (prop === "elementNum" || prop === "capacity") { + set(obj: Vector, prop: any, value: T): boolean { + if (prop === 'elementNum' || prop === 'capacity') { obj[prop] = value; return true; } - let index = Number.parseInt(prop); + let index: number = Number.parseInt(prop); if (Number.isInteger(index)) { if (index < 0 || index > obj.length) { - throw new RangeError("the index is out-of-bounds"); + throw new RangeError('the index is out-of-bounds'); } else { obj[index] = value; return true; @@ -53,38 +57,38 @@ if (flag || fastVector == undefined) { } return false; } - deleteProperty(obj: Vector, prop: any) { + deleteProperty(obj: Vector, prop: any): boolean { this.isOutBounds(obj, prop); - let index = Number.parseInt(prop); + let index: number = Number.parseInt(prop); if (index >= 0 && index < obj.length && Number.isInteger(index)) { obj.removeByIndex(index); return true; } return false; } - has(obj: Vector, prop: any) { + has(obj: Vector, prop: any): boolean { return obj.has(prop); } - ownKeys(obj: Vector) { - let keys = []; - for (let i = 0; i < obj.length; i++) { + ownKeys(obj: Vector): Array { + let keys: string[] = []; + for (let i: number = 0; i < obj.length; i++) { keys.push(i.toString()); } return keys; } - defineProperty(obj: Vector, prop: any, desc: any) { + defineProperty(): boolean { return true; } - getOwnPropertyDescriptor(obj: Vector, prop: any) { + getOwnPropertyDescriptor(obj: Vector, prop: any): Object { this.isOutBounds(obj, prop); - let index = Number.parseInt(prop); + let index: number = 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 RangeError("Can setPrototype on Vector Object"); + setPrototypeOf(): T { + throw new RangeError('Can setPrototype on Vector Object'); } } interface IterableIterator { @@ -99,7 +103,7 @@ if (flag || fastVector == undefined) { constructor() { return new Proxy(this, new HandlerVector()); } - get length() { + get length(): number { return this.elementNum; } add(element: T): boolean { @@ -111,19 +115,19 @@ if (flag || fastVector == undefined) { } insert(element: T, index: number): void { if (index < 0 || index >= this.elementNum) { - throw new RangeError("the index is out-of-bounds"); + throw new RangeError('the index is out-of-bounds'); } if (this.isFull()) { this.resize(); } - for (let i = this.elementNum; i > index; i--) { + for (let i: number = this.elementNum; i > index; i--) { this[i] = this[i - 1]; } this[index] = element; this.elementNum++; } has(element: T): boolean { - for (let i = 0; i < this.elementNum; i++) { + for (let i: number = 0; i < this.elementNum; i++) { if (this[i] === element) { return true; } @@ -134,7 +138,7 @@ if (flag || fastVector == undefined) { return this[index]; } getIndexOf(element: T): number { - for (let i = 0; i < this.elementNum; i++) { + for (let i: number = 0; i < this.elementNum; i++) { if (element === this[i]) { return i; } @@ -149,17 +153,17 @@ if (flag || fastVector == undefined) { } set(index: number, element: T): T { if (index < 0 || index >= this.elementNum) { - throw new RangeError("the index is out-of-bounds"); + 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"); + throw new RangeError('the index is out-of-bounds'); } - let result = this[index]; - for (let i = index; i < this.elementNum - 1; i++) { + let result: T = this[index]; + for (let i: number = index; i < this.elementNum - 1; i++) { this[i] = this[i + 1]; } this.elementNum--; @@ -167,8 +171,8 @@ if (flag || fastVector == undefined) { } remove(element: T): boolean { if (this.has(element)) { - let index = this.getIndexOf(element); - for (let i = index; i < this.elementNum - 1; i++) { + let index: number = this.getIndexOf(element); + for (let i: number = index; i < this.elementNum - 1; i++) { this[i] = this[i + 1]; } this.elementNum--; @@ -183,7 +187,7 @@ if (flag || fastVector == undefined) { return this[this.elementNum - 1]; } getLastIndexOf(element: T): number { - for (let i = this.elementNum - 1; i >= 0; i--) { + for (let i: number = this.elementNum - 1; i >= 0; i--) { if (element === this[i]) { return i; } @@ -192,7 +196,7 @@ if (flag || fastVector == undefined) { } getLastIndexFrom(element: T, index: number): number { if (this.has(element)) { - for (let i = index; i >= 0; i--) { + for (let i: number = index; i >= 0; i--) { if (this[i] === element) { return i; } @@ -202,7 +206,7 @@ if (flag || fastVector == undefined) { } getIndexFrom(element: T, index: number): number { if (this.has(element)) { - for (let i = index; i < this.elementNum; i++) { + for (let i: number = index; i < this.elementNum; i++) { if (this[i] === element) { return i; } @@ -221,7 +225,7 @@ if (flag || fastVector == undefined) { throw new RangeError(`the fromIndex or the toIndex is out-of-bounds`); } toIndex = toIndex >= this.elementNum ? this.elementNum : toIndex; - let i = fromIndex; + let i: number = fromIndex; for (let j = toIndex; j < this.elementNum; j++) { this[i] = this[j]; i++; @@ -236,35 +240,35 @@ if (flag || fastVector == undefined) { } replaceAllElements(callbackfn: (value: T, index?: number, vector?: Vector) => T, thisArg?: Object): void { - for (let i = 0; i < this.elementNum; i++) { + for (let i: number = 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.elementNum; i++) { + for (let i: number = 0; i < this.elementNum; i++) { callbackfn.call(thisArg, this[i], i, this); } } sort(comparator?: (firstValue: T, secondValue: T) => number): void { - let isSort = true; + let isSort: boolean = true; if (comparator) { - for (let i = 0; i < this.elementNum; i++) { - for (let j = 0; j < this.elementNum - 1 - i; j++) { + for (let i: number = 0; i < this.elementNum; i++) { + for (let j: number = 0; j < this.elementNum - 1 - i; j++) { if (comparator(this[j], this[j + 1]) > 0) { isSort = false; - let temp = this[j]; + let temp: T = this[j]; this[j] = this[j + 1]; this[j + 1] = temp; } } } } else { - for (let i = 0; i < this.elementNum - 1; i++) { - for (let j = 0; j < this.elementNum - 1 - i; j++) { + for (let i: number = 0; i < this.elementNum - 1; i++) { + for (let j: number = 0; j < this.elementNum - 1 - i; j++) { if (this.asciSort(this[j], this[j + 1])) { isSort = false; - let temp = this[j]; + let temp: T = this[j]; this[j] = this[j + 1]; this[j + 1] = temp; } @@ -276,16 +280,16 @@ if (flag || fastVector == 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 + 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 } return false; } @@ -297,35 +301,36 @@ if (flag || fastVector == undefined) { 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(); - for (let i = fromIndex; i < toIndex; i++) { + let vector: Vector = new Vector(); + for (let i: number = fromIndex; i < toIndex; i++) { vector.add(this[i]); } return vector; } convertToArray(): Array { - let arr = []; - for (let i = 0; i < this.elementNum; i++) { + let arr: Array = []; + for (let i: number = 0; i < this.elementNum; i++) { arr[i] = this[i]; } return arr; } copyToArray(array: Array): void { - let arr = this.convertToArray(); - for (let i = 0; i < array.length; i++) { + let arr: Array = this.convertToArray(); + for (let i: number = 0; i < array.length; i++) { array[i] = arr[i]; } } toString(): string { - let str = `${this[0]}`; - for (let i = 1; i < this.elementNum; i++) { + let str: string = ''; + str = `${this[0]}`; + for (let i: number = 1; i < this.elementNum; i++) { str = `${str},${this[i]}`; } return str; } clone(): Vector { - let clone = new Vector(); - for (let i = 0; i < this.elementNum; i++) { + let clone: Vector = new Vector(); + for (let i: number = 0; i < this.elementNum; i++) { clone.add(this[i]); } return clone; @@ -348,15 +353,17 @@ if (flag || fastVector == undefined) { this.capacity = this.elementNum; } isEmpty(): boolean { - return this.elementNum == 0; + return this.elementNum === 0; } [Symbol.iterator](): IterableIterator { - let count = 0; - let vector = this; + let count: number = 0; + let vector: Vector = this; return { next: function () { - let done = count >= vector.elementNum; - let value = !done ? vector[count++] : undefined; + let done: boolean = false; + let value: T = undefined; + done = count >= vector.elementNum; + value = done ? undefined : vector[count++]; return { done: done, value: value, diff --git a/container/vector/native_module_vector.cpp b/container/vector/native_module_vector.cpp index 5974a8feb3294a09c6ac624c789ea0feb34d6db3..06c2dab1538f34d10ebc66858393cc3fefd473bc 100644 --- a/container/vector/native_module_vector.cpp +++ b/container/vector/native_module_vector.cpp @@ -53,7 +53,7 @@ static napi_module vectorModule = { .nm_flags = 0, .nm_filename = nullptr, .nm_register_func = VectorInit, - .nm_modname = "Vector", + .nm_modname = "util.Vector", .nm_priv = ((void *)0), .reserved = {0}, }; @@ -61,4 +61,4 @@ extern "C" __attribute__((constructor)) void RegisterModule() { napi_module_register(&vectorModule); } -} +} // namespace OHOS::Util