diff --git a/container/BUILD.gn b/container/BUILD.gn new file mode 100644 index 0000000000000000000000000000000000000000..14bf38d43db13f0f36aa1dfc2082c034d5799b4b --- /dev/null +++ b/container/BUILD.gn @@ -0,0 +1,134 @@ +# Copyright (c) 2021 Huawei Device Co., Ltd. +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +import("//ark/ts2abc/ts2panda/ts2abc_config.gni") +import("//build/ohos.gni") +import("//build/ohos/ace/ace.gni") +import("//foundation/ace/ace_engine/ace_config.gni") + +container_names = [ + "arraylist", + "deque", + "queue", + "vector", + "linkedlist", + "list", + "stack", + "lightweightset", + "plainarray", +] + +# compile .ts to .js. +action("build_js_ts") { + script = "//base/compileruntime/js_util_module/container/build_ts_js.py" + args = [ + "--dst-file", + rebase_path(target_out_dir + "/"), + ] + depfile = "$target_gen_dir/$target_name.d" + outputs = [] + foreach(item, container_names) { + dep = target_out_dir + "/js_" + item + ".js" + outputs += [ dep ] + } +} + +# libs +template("container_lib") { + forward_variables_from(invoker, "*") + + name = target_name + base_output_path = get_label_info(":js_" + name, "target_out_dir") + js_container_obj_path = base_output_path + name + ".o" + gen_js_obj("js_" + name) { + input = "$target_out_dir/js_" + name + ".js" + output = js_container_obj_path + dep = ":build_js_ts" + } + + # compile .js to .abc. + action("gen_" + name + "_abc") { + visibility = [ ":*" ] + script = "//ark/ts2abc/ts2panda/scripts/generate_js_bytecode.py" + + args = [ + "--src-js", + rebase_path(target_out_dir + "/js_" + name + ".js"), + "--dst-file", + rebase_path(target_out_dir + "/" + name + ".abc"), + "--node", + rebase_path("${node_path}"), + "--frontend-tool-path", + rebase_path("${ts2abc_build_path}"), + "--node-modules", + rebase_path("${node_modules}"), + ] + deps = [ + ":build_js_ts", + "//ark/ts2abc/ts2panda:ark_ts2abc_build", + ] + + inputs = [ target_out_dir + "/js_" + name + ".js" ] + outputs = [ target_out_dir + "/" + name + ".abc" ] + } + + abc_output_path = get_label_info(":" + name + "_abc", "target_out_dir") + arraylist_abc_obj_path = abc_output_path + "/" + name + "_abc.o" + gen_js_obj(name + "_abc") { + input = "$target_out_dir/" + name + ".abc" + output = arraylist_abc_obj_path + dep = ":gen_" + target_name + } + + ohos_shared_library(name) { + include_dirs = [ + "//third_party/node/src", + "//foundation/ace/napi/interfaces/kits", + "//base/compileruntime/js_util_module/container/" + name, + ] + + sources = [ name + "/native_module_" + name + ".cpp" ] + + dep_abc = ":" + name + "_abc" + dep_js = ":js_" + name + deps = [ + "//base/compileruntime/js_util_module/container/:js_" + name, + "//foundation/ace/napi/:ace_napi", + "//utils/native/base:utils", + ] + deps += [ dep_abc ] + deps += [ dep_js ] + + if (is_standard_system) { + external_deps = [ "hiviewdfx_hilog_native:libhilog" ] + } else { + external_deps = [ "hilog:libhilog" ] + } + subsystem_name = "ccruntime" + part_name = "jsapi_util" + + relative_install_dir = "module" + } +} + +container_libs = [] +foreach(item, container_names) { + container_lib(item) { + } + dep = ":" + item + container_libs += [ dep ] +} + +group("container_packages") { + deps = container_libs +} diff --git a/container/arraylist/js_arraylist.ts b/container/arraylist/js_arraylist.ts new file mode 100644 index 0000000000000000000000000000000000000000..a591a5b8a00005ca2d9a89f537d8363e9132c469 --- /dev/null +++ b/container/arraylist/js_arraylist.ts @@ -0,0 +1,295 @@ +/* + * Copyright (c) 2021 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +interface IterableIterator { + next: () => { + value: T; + done: boolean; + }; +} +let flag = false; +let fastArrayList: any = undefined; +let arkPritvate = globalThis["ArkPrivate"] || undefined; +if (arkPritvate !== undefined) { + fastArrayList = arkPritvate.Load(arkPritvate.ArrayList); +} else { + flag = true; +} +if (flag) { + class handlerArrayList { + get(obj: ArrayList, prop: any) { + if (typeof prop === "symbol") { + return obj[prop]; + } + var index = Number.parseInt(prop); + if (Number.isInteger(index)) { + if (index < 0 || index >= obj.size()) { + throw new Error("ArrayList:get out-of-bounds"); + } + } + return obj[prop]; + } + set(obj: ArrayList, prop: any, value: T) { + if (prop === "length" || prop === "capacity") { + obj[prop] = value; + return true; + } + var index = Number.parseInt(prop); + if (Number.isInteger(index)) { + if (index < 0 || index > obj.size()) { + throw new Error("ArrayList:set out-of-bounds"); + } else { + obj[index] = value; + return true; + } + } + console.error("can not set" + prop); + return false; + } + deleteProperty(obj: ArrayList, prop: any) { + var index = Number.parseInt(prop); + if (Number.isInteger(index)) { + if (index < 0 || index >= obj.size()) { + throw new Error("ArrayList:deleteProperty out-of-bounds"); + } + obj.removeByIndex(index); + return true; + } + console.error("can not deleteProperty" + prop); + return false; + } + has(obj: ArrayList, prop: any) { + return obj.has(prop); + } + ownKeys(obj: ArrayList) { + var keys = []; + for (var i = 0; i < obj.size(); i++) { + keys.push(i.toString()); + } + return keys; + } + defineProperty(obj: ArrayList, prop: any, desc: any) { + console.error("Can not defineProperty on ArrayList Object."); + return true; + } + getOwnPropertyDescriptor(obj: ArrayList, prop: any) { + var index = Number.parseInt(prop); + if (Number.isInteger(index)) { + if (index < 0 || index >= obj.size()) { + throw new Error("ArrayList:getOwnPropertyDescriptor out-of-bounds"); + } + return Object.getOwnPropertyDescriptor(obj, prop); + } + return; + } + } + class ArrayList { + private length: number = 0; + private capacity: number = 10; + constructor() { + return new Proxy(this, new handlerArrayList()); + } + add(element: T): boolean { + if (this.isFull()) { + this.resize(); + } + this[this.length++] = element; + return true; + } + insert(element: T, index: number): void { + if (this.isFull()) { + this.resize(); + } + for (let i = this.length; i > index; i--) { + this[i] = this[i - 1]; + } + this[index] = element; + this.length++; + } + isEmpty(): boolean { + return this.length == 0; + } + has(element: T): boolean { + for (let i = 0; i < this.length; i++) { + if (this[i] === element) { + return true; + } + } + return false; + } + getIndexOf(element: T): number { + for (let i = 0; i < this.length; i++) { + if (element === this[i]) { + return i; + } + } + return -1; + } + + removeByIndex(index: number): void { + for (let i = index; i < this.length - 1; i++) { + this[i] = this[i + 1]; + } + this.length--; + } + remove(element: T): boolean { + if (this.has(element)) { + let index = this.getIndexOf(element); + for (let i = index; i < this.length - 1; i++) { + this[i] = this[i + 1]; + } + this.length--; + return true; + } + return false; + } + getLastIndexOf(element: T): number { + for (let i = this.length - 1; i >= 0; i--) { + if (element === this[i]) { + return i; + } + } + return -1; + } + removeByRange(fromIndex: number, toIndex: number): void { + if (fromIndex >= toIndex) { + throw new Error(`fromIndex cannot be less than or equal to toIndex`); + } + toIndex = toIndex >= this.length - 1 ? this.length - 1 : toIndex; + let arr = []; + let k = 0; + for (let i = 0; i < fromIndex; i++) { + arr[k] = this[i]; + k++; + } + for (let j = toIndex; j < this.length; j++) { + arr[k] = this[j]; + k++; + } + this.clear(); + for (let i = 0; i < arr.length; i++) { + this.add(arr[i]); + } + } + size(): number { + return this.length; + } + replaceAllElements(callbackfn: (value: T, index?: number, arraylist?: ArrayList) => T,thisArg?: Object): void + { + for (let i = 0; i < this.length; i++) { + this[i] = callbackfn(this[i], i, this); + } + } + forEach(callbackfn: (value: T, index?: number, arraylist?: ArrayList) => void,thisArg?: Object): void + { + for (let i = 0; i < this.length; i++) { + callbackfn(this[i], i); + } + } + sort(comparator: (firstValue: T, secondValue: T) => number): void { + let isSort = true; + for (let i = 0; i < this.length; i++) { + for (let j = 0; j < this.length - 1 - i; j++) { + if (comparator(this[j], this[j + 1]) > 0) { + isSort = false; + let temp = this[j]; + this[j] = this[j + 1]; + this[j + 1] = temp; + } + } + if (isSort) { + break; + } + } + } + subArrayList(fromIndex: number, toIndex: number): ArrayList + { + if (fromIndex >= toIndex) { + throw new Error(`fromIndex cannot be less than or equal to toIndex`); + } + toIndex = toIndex >= this.length - 1 ? this.length - 1 : toIndex; + let arr = []; + let k = 0; + for (let i = fromIndex; i < toIndex; i++) { + arr[k] = this[i]; + k++; + if (k > toIndex - fromIndex) { + break; + } + } + this.clear(); + for (let j = 0; j < arr.length; j++) { + this.add(arr[j]); + } + return this.clone(); + } + clear(): void { + for (let i = 0; i < this.length; i++) { + this[i] = null; + } + this.length = 0; + } + clone(): ArrayList { + let target = this; + let clone = Object.create(target); + for (let p in target) { + if (target.hasOwnProperty(p)) clone[p] = target[p]; + } + return clone; + } + getCapacity(): number { + return this.capacity; + } + convertToArray(): Array { + let arr = []; + for (let i = 0; i < this.length; i++) { + arr[i] = this[i]; + } + return arr; + } + isFull(): boolean { + return this.length === this.capacity; + } + resize(): void { + this.capacity = 1.5 * this.capacity; + } + increaseCapacityTo(newCapacity: number): void { + if(newCapacity >= this.length){ + this.capacity= newCapacity + } + } + trimToCurrentSize(): void { + this.capacity = this.length; + } + [Symbol.iterator](): IterableIterator { + let _this = this; + let i = 0; + return { + next: function () { + var done = i >= _this.length; + var value = !done ? _this[i++] : undefined; + return { + done: done, + value: value, + }; + }, + }; + } + } + Object.freeze(ArrayList); + fastArrayList = ArrayList; +} +export default { + ArrayList: fastArrayList, +}; diff --git a/container/arraylist/native_module_arraylist.cpp b/container/arraylist/native_module_arraylist.cpp new file mode 100644 index 0000000000000000000000000000000000000000..abdab08cd25eb4b567601dfd5cafb0ed2b93f3ba --- /dev/null +++ b/container/arraylist/native_module_arraylist.cpp @@ -0,0 +1,65 @@ +/* + * Copyright (c) 2021 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "utils/log.h" +#include "napi/native_api.h" +#include "napi/native_node_api.h" + +extern const char _binary_js_arraylist_js_start[]; +extern const char _binary_js_arraylist_js_end[]; +extern const char _binary_arraylist_abc_start[]; +extern const char _binary_arraylist_abc_end[]; + +namespace OHOS::Util { +static napi_value ArrayListInit(napi_env env, napi_value exports) +{ + return exports; +} +extern "C" +__attribute__((visibility("default"))) void NAPI_arraylist_GetJSCode(const char **buf, int *bufLen) +{ + if (buf != nullptr) { + *buf = _binary_js_arraylist_js_start; + } + if (bufLen != nullptr) { + *bufLen = _binary_js_arraylist_js_end - _binary_js_arraylist_js_start; + } +} +extern "C" +__attribute__((visibility("default"))) void NAPI_arraylist_GetABCCode(const char **buf, int *buflen) +{ + if (buf != nullptr) { + *buf = _binary_arraylist_abc_start; + } + if (buflen != nullptr) { + *buflen = _binary_arraylist_abc_end - _binary_arraylist_abc_start; + } +} + +static napi_module arrayListModule = { + .nm_version = 1, + .nm_flags = 0, + .nm_filename = nullptr, + .nm_register_func = ArrayListInit, + .nm_modname = "ArrayList", + .nm_priv = ((void *)0), + .reserved = {0}, +}; + +extern "C" __attribute__((constructor)) void RegisterModule() +{ + napi_module_register(&arrayListModule); +} +} \ No newline at end of file diff --git a/container/build_ts_js.py b/container/build_ts_js.py new file mode 100755 index 0000000000000000000000000000000000000000..4dbce9eadc2fc9d5af23088c12333e60b3983e75 --- /dev/null +++ b/container/build_ts_js.py @@ -0,0 +1,55 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- +# Copyright (c) 2021 Huawei Device Co., Ltd. +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +import os +import platform +import argparse +import subprocess + +def run_command(command): + print(" ".join(command)) + proc = subprocess.Popen(command, stdout=subprocess.PIPE, + stderr=subprocess.PIPE, universal_newlines=True) + out, err = proc.communicate() + if out != "": + print(out) + exit(1) + +if __name__ == '__main__': + + build_path = os.path.abspath(os.path.join(os.getcwd(), "../..")) + os.chdir("%s/base/compileruntime/js_util_module/container/" % build_path) + + parser = argparse.ArgumentParser() + parser.add_argument('--dst-file', + help='the converted target file') + input_arguments = parser.parse_args() + + node = '../../../../prebuilts/build-tools/common/nodejs/\ +node-v12.18.4-linux-x64/bin/node' + tsc = '../../../../ark/ts2abc/ts2panda/node_modules/typescript/bin/tsc' + cmd = [node, tsc] + run_command(cmd) + + for dirname in os.listdir("./jscode") : + filepath = os.path.join("./jscode", dirname) + for filename in os.listdir(filepath) : + dstpath = os.path.join(input_arguments.dst_file, filename) + srcpath = os.path.join(filepath, filename) + cmd = ['cp', "-r", srcpath, dstpath] + run_command(cmd) + + cmd = ['rm', "-rf", './jscode'] + run_command(cmd) + exit(0) diff --git a/container/deque/js_deque.ts b/container/deque/js_deque.ts new file mode 100644 index 0000000000000000000000000000000000000000..9198a32e3e5a6fd50272c2fa566e6ab05bc9abce --- /dev/null +++ b/container/deque/js_deque.ts @@ -0,0 +1,203 @@ +/* + * Copyright (c) 2021 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +interface IterableIterator { + next: () => { + value: T; + done: boolean; + }; +} +let flag = false; +let fastDeque: any = undefined; +let arkPritvate = globalThis["ArkPrivate"] || undefined; +if (arkPritvate !== undefined) { + fastDeque = arkPritvate.Load(arkPritvate.Deque); +} else { + flag = true; +} +if (flag) { + class handlerDeque { + get(obj: Deque, prop: any): T { + if (typeof prop === "symbol") { + return obj[prop]; + } + var index = Number.parseInt(prop); + if (Number.isInteger(index)) { + if (index < 0 || index >= obj.getCapacity()) { + throw new Error("Deque:get out-of-bounds"); + } + } + return obj[prop]; + } + set(obj: Deque, prop: any, value: T): boolean { + if (prop === "front" || prop === "capacity" || prop === "rear") { + obj[prop] = value; + return true; + } + var index = Number(prop); + if (Number.isInteger(index)) { + if (index < 0 || index > obj.getCapacity()) { + throw new Error("Deque:set out-of-bounds"); + } else { + obj[index] = value; + return true; + } + } + console.error("can not set" + prop); + return false; + } + has(obj: Deque, prop: any) { + return obj.has(prop); + } + ownKeys(obj: Deque) { + var keys = []; + for (var i = 0; i < obj.size(); i++) { + keys.push(i.toString()); + } + return keys; + } + defineProperty(obj: Deque, prop: any, desc: any) { + console.error("Can not defineProperty on Deque Object."); + return true; + } + getOwnPropertyDescriptor(obj: Deque, prop: any) { + var index = Number.parseInt(prop); + if (Number.isInteger(index)) { + if (index < 0 || index >= obj.size()) { + throw new Error("Deque:getOwnPropertyDescriptor out-of-bounds"); + } + return Object.getOwnPropertyDescriptor(obj, prop); + } + return; + } + } + class Deque { + private front: number; + private capacity: number; + private rear: number; + constructor() { + this.front = 0; + this.capacity = 8; + this.rear = 0; + return new Proxy(this, new handlerDeque()); + } + insertFront(element: T): void { + if (this.isFull()) { + this.increaseCapacityTo(); + } + this.front = (this.front - 1 + this.capacity) % this.capacity; + this[this.front] = element; + } + insertEnd(element: T): void { + if (this.isFull()) { + this.increaseCapacityTo(); + } + this[this.rear] = element; + this.rear = (this.rear + 1) % (this.capacity + 1); + } + getFirst(): T { + return this[this.front]; + } + getLast(): T { + return this[this.rear - 1]; + } + has(element: T): boolean { + let result = false; + this.forEach(function (value, index) { + if (value == element) { + result = true; + } + }); + return result; + } + popFirst(): T { + let result = this[this.front]; + this.front = (this.front + 1) % (this.capacity + 1); + return result; + } + popLast(): T { + let result = this[this.rear - 1]; + this.rear = (this.rear + this.capacity) % (this.capacity + 1); + return result; + } + forEach(callbackfn: (value: T, index?: number, deque?: Deque) => void,thisArg?: Object): void + { + let k = 0; + let i = this.front; + while (true) { + callbackfn(this[i], k); + i = (i + 1) % this.capacity; + k++; + if (i === this.rear) { + break; + } + } + } + isEmpty(): boolean { + return this.rear == this.front; + } + increaseCapacityTo(): void { + let k = 0; + let arr = []; + let len = this.size(); + while (true) { + arr[k++] = this[this.front]; + this.front = (this.front + 1) % this.capacity; + if (this.front === this.rear) { + break; + } + } + for (let i = 0; i < len; i++) { + this[i] = arr[i]; + } + this.capacity = 2 * this.capacity; + this.front = 0; + this.rear = len; + } + isFull(): boolean { + return (this.rear + 1) % this.capacity === this.front; + } + size(): number { + return (this.rear - this.front + this.capacity) % this.capacity; + } + + [Symbol.iterator](): IterableIterator { + let _this = this; + let i = _this.front; + return { + next: function () { + var done = i == _this.rear; + var value = !done ? _this[i] : undefined; + i = (i + 1) % _this.capacity; + return { + done: done, + value: value, + }; + }, + }; + } + getCapacity(): number { + return this.capacity; + } + trimToCurrentSize(): void { + let len = this.size(); + this.capacity = len; + } + } + Object.freeze(Deque); + fastDeque = Deque; +} +export default { + Deque: fastDeque, +}; diff --git a/container/deque/native_module_deque.cpp b/container/deque/native_module_deque.cpp new file mode 100644 index 0000000000000000000000000000000000000000..0e3c227bb8c49bdab86a9282d6c11499a7d4e8d0 --- /dev/null +++ b/container/deque/native_module_deque.cpp @@ -0,0 +1,65 @@ +/* + * Copyright (c) 2021 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "utils/log.h" +#include "napi/native_api.h" +#include "napi/native_node_api.h" + +extern const char _binary_js_deque_js_start[]; +extern const char _binary_js_deque_js_end[]; +extern const char _binary_deque_abc_start[]; +extern const char _binary_deque_abc_end[]; +namespace OHOS::Util { +static napi_value DequeInit(napi_env env, napi_value exports) +{ + return exports; +} +extern "C" +__attribute__((visibility("default"))) void NAPI_deque_GetJSCode(const char **buf, int *bufLen) +{ + if (buf != nullptr) { + *buf = _binary_js_deque_js_start; + } + + if (bufLen != nullptr) { + *bufLen = _binary_js_deque_js_end - _binary_js_deque_js_start; + } +} +extern "C" +__attribute__((visibility("default"))) void NAPI_deque_GetABCCode(const char **buf, int *buflen) +{ + if (buf != nullptr) { + *buf = _binary_deque_abc_start; + } + if (buflen != nullptr) { + *buflen = _binary_deque_abc_end - _binary_deque_abc_start; + } +} + +static napi_module dequeModule = { + .nm_version = 1, + .nm_flags = 0, + .nm_filename = nullptr, + .nm_register_func = DequeInit, + .nm_modname = "Deque", + .nm_priv = ((void *)0), + .reserved = {0}, +}; + +extern "C" __attribute__((constructor)) void RegisterModule() +{ + napi_module_register(&dequeModule); +} +} diff --git a/container/lightweightset/js_lightweightset.ts b/container/lightweightset/js_lightweightset.ts new file mode 100644 index 0000000000000000000000000000000000000000..49943c87f903cb67d97eaf4d656923721ca1a19e --- /dev/null +++ b/container/lightweightset/js_lightweightset.ts @@ -0,0 +1,22 @@ +/* + * Copyright (c) 2021 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +class LightweightSet { + constructor() { + } +} +export default { + LightweightSet : LightweightSet +} diff --git a/container/lightweightset/native_module_lightweightset.cpp b/container/lightweightset/native_module_lightweightset.cpp new file mode 100644 index 0000000000000000000000000000000000000000..0177d5fcd77a3e1d0122ede9221e030769d11367 --- /dev/null +++ b/container/lightweightset/native_module_lightweightset.cpp @@ -0,0 +1,78 @@ + /* + * Copyright (c) 2021 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "utils/log.h" +#include "napi/native_api.h" +#include "napi/native_node_api.h" + +extern const char _binary_js_lightweightset_js_start[]; +extern const char _binary_js_lightweightset_js_end[]; +extern const char _binary_lightweightset_abc_start[]; +extern const char _binary_lightweightset_abc_end[]; + +namespace OHOS::Util { +static napi_value LightweightSetInit(napi_env env, napi_value exports) +{ + // const char *lightweightSetClassName = "LightweightSet"; + // napi_value lightweightSetClass = nullptr; + // static napi_property_descriptor lightweightSetDesc[] = { + // }; + // NAPI_CALL(env, napi_define_class(env, lightweightSetClassName, strlen(lightweightSetClassName), LightweightSetConstructor, + // nullptr, sizeof(lightweightSetDesc) / sizeof(lightweightSetDesc[0]), lightweightSetDesc, + // &lightweightSetClass)); + // static napi_property_descriptor desc[] = { + // DECLARE_NAPI_PROPERTY("LightweightSet", lightweightSetClass) + // }; + // NAPI_CALL(env, napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc)); + return exports; +} + +extern "C" +__attribute__((visibility("default"))) void NAPI_lightweightset_GetJSCode(const char **buf, int *bufLen) +{ + if (buf != nullptr) { + *buf = _binary_js_lightweightset_js_start; + } + + if (bufLen != nullptr) { + *bufLen = _binary_js_lightweightset_js_end - _binary_js_lightweightset_js_start; + } +} +extern "C" +__attribute__((visibility("default"))) void NAPI_lightweightset_GetABCCode(const char** buf, int* buflen) +{ + if (buf != nullptr) { + *buf = _binary_lightweightset_abc_start; + } + if (buflen != nullptr) { + *buflen = _binary_lightweightset_abc_end - _binary_lightweightset_abc_start; + } +} + +static napi_module lightweightSetModule = { + .nm_version = 1, + .nm_flags = 0, + .nm_filename = nullptr, + .nm_register_func = LightweightSetInit, + .nm_modname = "LightweightSet", + .nm_priv = ((void*)0), + .reserved = { 0 }, +}; + +extern "C" __attribute__ ((constructor)) void RegisterModule() +{ + napi_module_register(&lightweightSetModule); +} +} // namespace diff --git a/container/linkedlist/js_linkedlist.ts b/container/linkedlist/js_linkedlist.ts new file mode 100644 index 0000000000000000000000000000000000000000..dfe19e671407a02707eb2aeda3b07c60608f6983 --- /dev/null +++ b/container/linkedlist/js_linkedlist.ts @@ -0,0 +1,412 @@ +/* + * Copyright (c) 2021 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +interface IterableIterator { + next: () => { + value: T; + done: boolean; + }; +} +let flag = false; +let fastLinkedList: any = undefined; +let arkPritvate = globalThis["ArkPrivate"] || undefined; +if (arkPritvate !== undefined) { + fastLinkedList = arkPritvate.Load(arkPritvate.LinkedList); +} else { + flag = true; +} +if (flag) { + class handlerLinkedList { + get (obj:LinkedList, prop: any, receiver: any) { + if (typeof prop === "symbol") { + return obj[prop]; + } + var index = Number.parseInt(prop); + if (Number.isInteger(index)) { + if (index < 0 || index >= obj.size()) { + throw new Error("LinkedList:get out-of-bounds"); + } + } + return obj[prop]; + } + set(obj: LinkedList, prop: any, value: any) { + if ( + prop === "length" || + prop === "capacity" || + prop === "head" || + prop == "next" || + prop == "tail" + ) { + obj[prop] = value; + return true; + } + + var index = Number.parseInt(prop); + if (Number.isInteger(index)) { + if (index < 0 || index >= obj.size()) { + throw new Error("LinkedList:set out-of-bounds"); + } else { + obj[index] = value; + return true; + } + } + console.error("can not set" + prop); + return false; + } + deleteProperty(obj: LinkedList, prop: any) { + var index = Number.parseInt(prop); + if (Number.isInteger(index)) { + if (index < 0 || index >= obj.size()) { + throw new Error("LinkedList:deleteProperty out-of-bounds"); + } + obj.removeByIndex(index); + return true; + } + console.error("can not deleteProperty" + prop); + return false; + } + has(obj: LinkedList, prop: any) { + return obj.has(prop); + } + ownKeys(obj: LinkedList) { + var keys = []; + for (var i = 0; i < obj.size(); i++) { + keys.push(i.toString()); + } + return keys; + } + defineProperty(obj: LinkedList, prop: any, desc: any) { + console.error("Can not defineProperty on LinkedList Object."); + return true; + } + getOwnPropertyDescriptor(obj: LinkedList, prop: any) { + var index = Number.parseInt(prop); + if (Number.isInteger(index)) { + if (index < 0 || index >= obj.size()) { + throw new Error("LinkedList:getOwnPropertyDescriptor out-of-bounds"); + } + return Object.getOwnPropertyDescriptor(obj, prop); + } + return; + } + }; + class NodeObj { + element: any; + next?: NodeObj | null; + prev?: NodeObj | null; + constructor( + element: any, + next?: NodeObj | null, + prev?: NodeObj | null + ) { + this.element = element; + this.next = next; + this.prev = prev; + } + } + class LinkIterator { + private linkNode: any; + constructor(linkNode: any) { + this.linkNode = linkNode; + } + hasNext(): boolean { + if (this.linkNode.next !== null) { + return true; + } else { + return false; + } + } + next(): NodeObj { + this.linkNode = this.linkNode.next; + return this.linkNode; + } + prev(): NodeObj { + this.linkNode = this.linkNode.prev; + return this.linkNode; + } + } + class LinkedList { + public head: any; + public tail: any; + public length: number; + private capacity: number; + constructor(head?: NodeObj, length?: number, tail?: NodeObj) { + this.head = head || null; + this.tail = tail || null; + this.length = length || 0; + this.capacity = 10; + return new Proxy(this, new handlerLinkedList()); + } + get(index: number): NodeObj { + let current = this.head; + for (let i = 0; i < index; i++) { + current = current["next"]; + } + return current; + } + add(element: T): boolean { + if (this.isFull()) { + this.increaseCapacityTo(); + } + if (this.length === 0) { + let head = this.head; + let tail = this.tail; + this.head = this.tail = new NodeObj(element, head, tail); + } else { + let prevNode = this.get(this.length - 1); + prevNode.next = new NodeObj(element, prevNode["next"], this.tail); + } + this.length++; + return true; + } + addFirst(element: T): void { + if (this.isFull()) { + this.increaseCapacityTo(); + } + if (!element) { + throw new Error("element cannot be null"); + } + let node = new NodeObj(element, this.head, this.tail); + if (this.length === 0) { + this.head = this.tail = node; + } else { + node.next = this.head; + this.head = node; + } + this.length++; + } + removeFirst(): T { + let result = this.get(0).element + this.removeByIndex(0); + return result; + } + popFirst(): T { + let result = this.get(0).element + this.removeByIndex(0); + return result; + } + popLast(): T { + let result = this.get(this.length - 1).element + this.removeByIndex(this.length - 1); + return result; + } + removeLast(): T { + let result = this.get(this.length - 1).element + this.removeByIndex(this.length - 1); + return result; + } + clear(): void { + this.head = null; + this.tail = null; + this.length = 0; + } + has(element: T): boolean { + if (this.head.element === element) { + return true; + } + const linkIterator = new LinkIterator(this.head); + while (linkIterator.hasNext()) { + const newNode = linkIterator.next(); + if (newNode.element === element) { + return true; + } + } + return false; + } + getIndexOf(element: T): number { + for (let i = 0; i < this.length; i++) { + const curNode = this.get(i); + if (curNode.element === element) { + return i; + } + } + return -1; + } + isEmpty(): boolean { + return this.length === 0; + } + getLastIndexOf(element: T): number { + for (let i = this.length - 1; i >= 0; i--) { + const curNode = this.get(i); + if (curNode.element === element) { + return i; + } + } + return -1; + } + removeByIndex(index: number): T { + let current = this.head; + if (index === 0) { + this.head = current && current.next; + if (this.length == 1) { + this.head = this.tail = null; + } else { + this.head.prev = null; + } + } else if (index == this.length - 1) { + current = this.get(index-1); + this.tail = current; + current.next = null; + } else { + const prevNode = this.get(index-1); + const nextNode = this.get(index+1); + prevNode.next = nextNode; + nextNode.prev = prevNode; + } + this.length--; + return current && current.element; + } + remove(element: T): boolean { + if (this.has(element)) { + let index = this.getIndexOf(element); + this.removeByIndex(index); + return true; + } + return false; + } + removeFirstFound(element: T): boolean { + if (this.has(element)) { + let index = this.getIndexOf(element); + this.removeByIndex(index); + return true; + } else { + return false; + } + } + removeLastFound(element: T): boolean { + if (this.has(element)) { + let index = this.getLastIndexOf(element); + this.removeByIndex(index); + return true; + } else { + return false; + } + } + getFirst(): T { + let newNode = this.get(0); + let element = newNode.element; + return element; + } + getLast(): T { + let newNode = this.get(this.length - 1); + let element = newNode.element; + return element; + } + insert(index: number, element: T): void { + if (this.isFull()) { + this.increaseCapacityTo(); + } + if (index >= 0 && index < this.length) { + let newNode = new NodeObj(element); + let current = this.head; + if (index === 0) { + if (this.head === null) { + this.head = newNode; + this.tail = newNode; + } else { + newNode.next = this.head; + this.head.prev = newNode; + this.head = newNode; + } + } else { + const prevNode = this.get(index - 1); + current = prevNode.next; + newNode.next = current; + prevNode.next = newNode; + current.prev = newNode; + newNode.prev = prevNode; + } + }else if(index ===this.length){ + let prevNode = this.get(this.length - 1); + prevNode.next = new NodeObj(element, prevNode["next"], this.tail); + }else{ + throw new Error("index cannot Less than 0 and more than this length"); + } + this.length++; + } + set(index: number, element: T): T { + const current = this.get(index); + current.element = element; + return current && current.element; + } + size(): number { + return this.length; + } + convertToArray(): Array { + let arr: Array = []; + let index = 0; + if (this.length <= 0) { + return arr; + } + arr[index] = this.head.element; + const linkIterator = new LinkIterator(this.head); + while (linkIterator.hasNext()) { + const newNode = linkIterator.next(); + arr[++index] = newNode.element; + } + return arr; + } + clone(): LinkedList { + let target = this; + let clone = Object.create(target); + for (let p in target) { + if (target.hasOwnProperty(p)) clone[p] = target[p]; + } + return clone; + } + getCapacity(): number { + return this.capacity; + } + increaseCapacityTo(): void { + this.capacity = 1.5 * this.capacity; + } + isFull(): boolean { + return this.length === this.capacity; + } + trimToCurrentSize(): void { + this.length = this.capacity; + } + forEach(callbackfn: (value: T, index?: number, linkedlist?: LinkedList) => void,thisArg?: Object): void + { + let index = 0; + const linkIterator = new LinkIterator(this.head); + if (this.length > 0) { + callbackfn(this.head.element, index); + } + while (linkIterator.hasNext()) { + const newNode = linkIterator.next(); + callbackfn(newNode.element, ++index); + } + } + [Symbol.iterator](): IterableIterator { + let _this = this; + let i = 0; + return { + next: function () { + var done = i >= _this.length; + var value = !done ? _this.get(i++).element : undefined; + return { + done: done, + value: value, + }; + }, + }; + } + } + Object.freeze(LinkedList); + fastLinkedList = LinkedList; +} +export default { + LinkedList: fastLinkedList, +}; diff --git a/container/linkedlist/native_module_linkedlist.cpp b/container/linkedlist/native_module_linkedlist.cpp new file mode 100644 index 0000000000000000000000000000000000000000..0b6c756c9e0bf68e9edf541d4f6a58cc2ffa9638 --- /dev/null +++ b/container/linkedlist/native_module_linkedlist.cpp @@ -0,0 +1,67 @@ +/* + * Copyright (c) 2021 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "utils/log.h" +#include "napi/native_api.h" +#include "napi/native_node_api.h" + +extern const char _binary_js_linkedlist_js_start[]; +extern const char _binary_js_linkedlist_js_end[]; +extern const char _binary_linkedlist_abc_start[]; +extern const char _binary_linkedlist_abc_end[]; + +namespace OHOS::Util { +static napi_value LinkedListInit(napi_env env, napi_value exports) +{ + return exports; +} + +extern "C" +__attribute__((visibility("default"))) void NAPI_linkedlist_GetJSCode(const char **buf, int *bufLen) +{ + if (buf != nullptr) { + *buf = _binary_js_linkedlist_js_start; + } + + if (bufLen != nullptr) { + *bufLen = _binary_js_linkedlist_js_end - _binary_js_linkedlist_js_start; + } +} +extern "C" +__attribute__((visibility("default"))) void NAPI_linkedlist_GetABCCode(const char **buf, int *buflen) +{ + if (buf != nullptr) { + *buf = _binary_linkedlist_abc_start; + } + if (buflen != nullptr) { + *buflen = _binary_linkedlist_abc_end - _binary_linkedlist_abc_start; + } +} + +static napi_module linkedListModule = { + .nm_version = 1, + .nm_flags = 0, + .nm_filename = nullptr, + .nm_register_func = LinkedListInit, + .nm_modname = "LinkedList", + .nm_priv = ((void *)0), + .reserved = {0}, +}; + +extern "C" __attribute__((constructor)) void RegisterModule() +{ + napi_module_register(&linkedListModule); +} +} diff --git a/container/list/js_list.ts b/container/list/js_list.ts new file mode 100644 index 0000000000000000000000000000000000000000..8873be2745891b837b80613328240de0a922ebac --- /dev/null +++ b/container/list/js_list.ts @@ -0,0 +1,385 @@ +/* + * Copyright (c) 2021 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +interface IterableIterator { + next: () => { + value: T; + done: boolean; + }; +} +let flag = false; +let fastList: any = undefined; +let arkPritvate = globalThis["ArkPrivate"] || undefined; +if (arkPritvate !== undefined) { + fastList = arkPritvate.Load(arkPritvate.List); +} else { + flag = true; +} +if (flag) { + class handlerList { + get(obj: List, prop: any) { + if (typeof prop === "symbol") { + return obj[prop]; + } + var index = Number.parseInt(prop); + if (Number.isInteger(index)) { + if (index < 0 || index >= obj.size()) { + throw new Error("ArrayList:get out-of-bounds"); + } + } + return obj[prop]; + } + set(obj: List, prop: any, value: T) { + if ( + prop === "length" || + prop === "capacity" || + prop === "head" || + prop == "next" + ) { + obj[prop] = value; + return true; + } + var index = Number.parseInt(prop); + if (Number.isInteger(index)) { + if (index < 0 || index >= obj.size()) { + throw new Error("List:set out-of-bounds"); + } else { + obj[index] = value; + return true; + } + } + console.error("can not set" + prop); + return false; + } + deleteProperty(obj: List, prop: any) { + var index = Number.parseInt(prop); + if (Number.isInteger(index)) { + if (index < 0 || index >= obj.size()) { + throw new Error("List:deleteProperty out-of-bounds"); + } + obj.removeByIndex(index); + return true; + } + console.error("can not deleteProperty" + prop); + return false; + } + has(obj: List, prop: any) { + return obj.has(prop); + } + ownKeys(obj: List) { + var keys = []; + for (var i = 0; i < obj.size(); i++) { + keys.push(i.toString()); + } + return keys; + } + defineProperty(obj: List, prop: any, desc: any) { + console.error("Can not defineProperty on List Object."); + return true; + } + getOwnPropertyDescriptor(obj: List, prop: any) { + var index = Number.parseInt(prop); + if (Number.isInteger(index)) { + if (index < 0 || index >= obj.size()) { + throw new Error("List:getOwnPropertyDescriptor out-of-bounds"); + } + return Object.getOwnPropertyDescriptor(obj, prop); + } + return; + } + } + class NodeObj { + element: any; + next?: NodeObj | null; + constructor(element: any, next?: NodeObj | null) { + this.element = element; + this.next = next; + } + } + class LinkIterator { + private linkNode: any; + constructor(linkNode: any) { + this.linkNode = linkNode; + } + hasNext(): boolean { + if (this.linkNode.next !== null) { + return true; + } else { + return false; + } + } + next(): NodeObj { + this.linkNode = this.linkNode.next; + return this.linkNode; + } + } + class List { + public head: any; + public length: number; + private capacity: number; + constructor(head?: NodeObj, length?: number) { + this.head = head || null; + this.length = length || 0; + this.capacity = 10; + return new Proxy(this, new handlerList()); + } + get(index: number): NodeObj { + let current = this.head; + for (let i = 0; i < index; i++) { + current = current["next"]; + } + return current; + } + add(element: T): boolean { + if (this.isFull()) { + this.increaseCapacityTo(); + } + if (this.length === 0) { + let head = this.head; + this.head = new NodeObj(element, head); + } else { + let prevNode = this.get(this.length - 1); + prevNode.next = new NodeObj(element, prevNode["next"]); + } + this.length++; + return true; + } + clear(): void { + this.head = null; + this.length = 0; + } + has(element: T): boolean { + if (this.head.element === element) { + return true; + } + const linkIterator = new LinkIterator(this.head); + while (linkIterator.hasNext()) { + const newNode = linkIterator.next(); + if (newNode.element === element) { + return true; + } + } + return false; + } + equals(obj: Object): boolean { + if (obj === this) { + return true; + } + if (!(obj instanceof List)) { + return false; + } else { + let e1 = new LinkIterator(this.head); + let e2 = new LinkIterator(obj.head); + while (e1.hasNext() && e2.hasNext()) { + const newNode1 = e1.next(); + const newNode2 = e2.next(); + if (newNode1.element !== newNode2.element) { + console.log("this" + newNode1.element); + console.log("obj" + newNode2.element); + return false; + } + } + return !(e1.hasNext() || e2.hasNext()); + } + return true; + } + getIndexOf(element: T): number { + for (let i = 0; i < this.length; i++) { + const curNode = this.get(i); + if (curNode.element === element) { + return i; + } + } + return -1; + } + isEmpty(): boolean { + return this.length === 0; + } + getLastIndexOf(element: T): number { + for (let i = this.length - 1; i >= 0; i--) { + const curNode = this.get(i); + if (curNode.element === element) { + return i; + } + } + return -1; + } + removeByIndex(index: number): T { + let oldNode = this.head; + if (index === 0) { + oldNode = this.head; + this.head = oldNode && oldNode.next; + } else { + let prevNode = this.get(index - 1); + oldNode = prevNode.next; + prevNode.next = oldNode.next; + } + this.length--; + return oldNode && oldNode.element; + } + remove(element: T): boolean { + if (this.has(element)) { + let index = this.getIndexOf(element); + this.removeByIndex(index); + return true; + } + return false; + } + replaceAllElements(callbackfn: (value: T, index?: number, list?: List) => T,thisArg?: Object): void + { + let index = 0; + const linkIterator = new LinkIterator(this.head); + if (this.length > 0) { + const linkIterator = new LinkIterator(this.head); + this.get(index).element = callbackfn(this.head.element, index); + } + while (linkIterator.hasNext()) { + const newNode = linkIterator.next(); + this.get(++index).element = callbackfn(newNode.element, index); + } + } + getFirst(): T { + let newNode = this.get(0); + let element = newNode.element; + return element; + } + getLast(): T { + let newNode = this.get(this.length - 1); + let element = newNode.element; + return element; + } + insert(element: T, index: number): void { + if (this.isFull()) { + this.increaseCapacityTo(); + } + let newNode = new NodeObj(element); + if (index >= 0 && index < this.length) { + if (index === 0) { + const current = this.head; + newNode.next = current; + this.head = newNode; + } else { + const prevNode = this.get(index - 1); + newNode.next = prevNode.next; + prevNode.next = newNode; + } + this.length++; + } + } + set(index: number, element: T): T { + const current = this.get(index); + current.element = element; + return current && current.element; + } + size(): number { + return this.length; + } + sort(comparator: (firstValue: T, secondValue: T) => number): void { + let isSort = true; + for (let i = 0; i < this.length; i++) { + for (let j = 0; j < this.length - 1 - i; j++) { + if (comparator(this.get(j).element, this.get(j + 1).element) > 0) { + isSort = false; + let temp = this.get(j).element; + this.get(j).element = this.get(j + 1).element; + this.get(j + 1).element = temp; + } + } + if (isSort) { + break; + } + } + } + getSubList(fromIndex: number, toIndex: number): List + { + if (toIndex <= fromIndex) { + throw new Error('toIndex cannot be less than or equal to fromIndex'); + } + toIndex = toIndex > this.length ? this.length : toIndex; + let arr = []; + for(let i=fromIndex;i { + let arr: Array = []; + let index = 0; + if (this.length <= 0) { + return arr; + } + arr[index] = this.head.element; + const linkIterator = new LinkIterator(this.head); + while (linkIterator.hasNext()) { + const newNode = linkIterator.next(); + arr[++index] = newNode.element; + } + return arr; + } + getCapacity(): number { + return this.capacity; + } + increaseCapacityTo(): void { + this.capacity = 1.5 * this.capacity; + } + isFull(): boolean { + return this.length === this.capacity; + } + trimToCurrentSize(): void { + this.capacity = this.length; + } + forEach(callbackfn: (value: T, index?: number, list?: List) => void,thisArg?: Object): void + { + let index = 0; + const linkIterator = new LinkIterator(this.head); + if (this.length > 0) { + const linkIterator = new LinkIterator(this.head); + callbackfn(this.head.element, index); + } + while (linkIterator.hasNext()) { + const newNode = linkIterator.next(); + callbackfn(newNode.element, ++index); + } + } + + [Symbol.iterator](): IterableIterator { + let _this = this; + let i = 0; + return { + next: function () { + var done = i >= _this.length; + var value = !done ? _this.get(i++).element : undefined; + return { + done: done, + value: value, + }; + }, + }; + } + } + Object.freeze(List); + fastList = List; +} +export default { + List: fastList, +}; diff --git a/container/list/native_module_list.cpp b/container/list/native_module_list.cpp new file mode 100644 index 0000000000000000000000000000000000000000..859a9dca2721937cc07632d37a7d8412a98d1a73 --- /dev/null +++ b/container/list/native_module_list.cpp @@ -0,0 +1,64 @@ +/* + * Copyright (c) 2021 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "utils/log.h" +#include "napi/native_api.h" +#include "napi/native_node_api.h" + +extern const char _binary_js_list_js_start[]; +extern const char _binary_js_list_js_end[]; +extern const char _binary_list_abc_start[]; +extern const char _binary_list_abc_end[]; +namespace OHOS::Util { +static napi_value ListInit(napi_env env, napi_value exports) +{ + return exports; +} +extern "C" +__attribute__((visibility("default"))) void NAPI_list_GetJSCode(const char **buf, int *bufLen) +{ + if (buf != nullptr) { + *buf = _binary_js_list_js_start; + } + + if (bufLen != nullptr) { + *bufLen = _binary_js_list_js_end - _binary_js_list_js_start; + } +} +extern "C" +__attribute__((visibility("default"))) void NAPI_list_GetABCCode(const char **buf, int *buflen) +{ + if (buf != nullptr) { + *buf = _binary_list_abc_start; + } + if (buflen != nullptr) { + *buflen = _binary_list_abc_end - _binary_list_abc_start; + } +} + +static napi_module listModule = { + .nm_version = 1, + .nm_flags = 0, + .nm_filename = nullptr, + .nm_register_func = ListInit, + .nm_modname = "List", + .nm_priv = ((void *)0), + .reserved = {0}, +}; +extern "C" __attribute__((constructor)) void RegisterModule() +{ + napi_module_register(&listModule); +} +} diff --git a/container/plainarray/js_plainarray.ts b/container/plainarray/js_plainarray.ts new file mode 100644 index 0000000000000000000000000000000000000000..3124afc000dd5b6a09295fd7b69a2b64d046b7c6 --- /dev/null +++ b/container/plainarray/js_plainarray.ts @@ -0,0 +1,23 @@ +/* + * Copyright (c) 2021 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +class PlainArray { + constructor() { + } +} + +export default { + PlainArray : PlainArray +} \ No newline at end of file diff --git a/container/plainarray/native_module_plainarray.cpp b/container/plainarray/native_module_plainarray.cpp new file mode 100644 index 0000000000000000000000000000000000000000..73f384612dd8d960e1139279372986382dfedebd --- /dev/null +++ b/container/plainarray/native_module_plainarray.cpp @@ -0,0 +1,78 @@ + /* + * Copyright (c) 2021 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "utils/log.h" +#include "napi/native_api.h" +#include "napi/native_node_api.h" + +extern const char _binary_js_plainarray_js_start[]; +extern const char _binary_js_plainarray_js_end[]; +extern const char _binary_plainarray_abc_start[]; +extern const char _binary_plainarray_abc_end[]; + +namespace OHOS::Util { +static napi_value PlainArrayInit(napi_env env, napi_value exports) +{ + // const char *plainArrayClassName = "PlainArray"; + // napi_value plainArrayClass = nullptr; + // static napi_property_descriptor plainArrayDesc[] = { + // }; + // NAPI_CALL(env, napi_define_class(env, plainArrayClassName, strlen(plainArrayClassName), PlainArrayConstructor, + // nullptr, sizeof(plainArrayDesc) / sizeof(plainArrayDesc[0]), plainArrayDesc, + // &plainArrayClass)); + // static napi_property_descriptor desc[] = { + // DECLARE_NAPI_PROPERTY("PlainArray", plainArrayClass) + // }; + // NAPI_CALL(env, napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc)); + return exports; +} + +extern "C" +__attribute__((visibility("default"))) void NAPI_plainarray_GetJSCode(const char **buf, int *bufLen) +{ + if (buf != nullptr) { + *buf = _binary_js_plainarray_js_start; + } + + if (bufLen != nullptr) { + *bufLen = _binary_js_plainarray_js_end - _binary_js_plainarray_js_start; + } +} +extern "C" +__attribute__((visibility("default"))) void NAPI_plainarray_GetABCCode(const char** buf, int* buflen) +{ + if (buf != nullptr) { + *buf = _binary_plainarray_abc_start; + } + if (buflen != nullptr) { + *buflen = _binary_plainarray_abc_end - _binary_plainarray_abc_start; + } +} + +static napi_module plainArrayModule = { + .nm_version = 1, + .nm_flags = 0, + .nm_filename = nullptr, + .nm_register_func = PlainArrayInit, + .nm_modname = "PlainArray", + .nm_priv = ((void*)0), + .reserved = { 0 }, +}; + +extern "C" __attribute__ ((constructor)) void RegisterModule() +{ + napi_module_register(&plainArrayModule); +} +} // namespace diff --git a/container/queue/js_queue.ts b/container/queue/js_queue.ts new file mode 100644 index 0000000000000000000000000000000000000000..38453e7d94397c1e6f16adf4a6ecd282cbe18489 --- /dev/null +++ b/container/queue/js_queue.ts @@ -0,0 +1,162 @@ +/* + * Copyright (c) 2021 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +interface IterableIterator { + next: () => { + value: T; + done: boolean; + }; +} +let flag = false; +let fastQueue: any = undefined; +let arkPritvate = globalThis["ArkPrivate"] || undefined; +if (arkPritvate !== undefined) { + fastQueue = arkPritvate.Load(arkPritvate.Queue); +} else { + flag = true; +} +if (flag) { + class handlerQueue { + get(obj: Queue, prop: any): T { + if (typeof prop === "symbol") { + return obj[prop]; + } + var index = Number.parseInt(prop); + if (Number.isInteger(index)) { + if (index < 0 || index >= obj.getCapacity()) { + throw new Error("Queue:get out-of-bounds"); + } + } + return obj[prop]; + } + set(obj: Queue, prop: any, value: T): boolean { + if (prop === "front" || prop === "capacity" || prop === "rear") { + obj[prop] = value; + return true; + } + var index = Number(prop); + if (Number.isInteger(index)) { + if (index < 0 || index > obj.getCapacity()) { + throw new Error("Queue:set out-of-bounds"); + } else { + obj[index] = value; + return true; + } + } + console.error("can not set" + prop); + return false; + } + + ownKeys(obj: Queue) { + var keys = []; + for (var i = 0; i < obj.size(); i++) { + keys.push(i.toString()); + } + return keys; + } + defineProperty(obj: Queue, prop: any, desc: any) { + console.error("Can not defineProperty on Queue Object."); + return true; + } + getOwnPropertyDescriptor(obj: Queue, prop: any) { + var index = Number.parseInt(prop); + if (Number.isInteger(index)) { + if (index < 0 || index >= obj.size()) { + throw new Error("Queue:getOwnPropertyDescriptor out-of-bounds"); + } + return Object.getOwnPropertyDescriptor(obj, prop); + } + return; + } + } + class Queue { + private front: number; + private capacity: number; + private rear: number; + constructor() { + this.front = 0; + this.capacity = 8; + this.rear = 0; + return new Proxy(this, new handlerQueue()); + } + add(element: T): boolean { + if (this.isFull()) { + this.increaseCapacityTo(); + } + this[this.rear] = element; + this.rear = (this.rear + 1) % (this.capacity + 1); + return true; + } + getFirst(): T { + return this[this.front]; + } + pop(): T { + let result = this[this.front]; + this.front = (this.front + 1) % (this.capacity + 1); + return result; + } + forEach(callbackfn: (value: T, index?: number, queue?: Queue) => void,thisArg?: Object): void + { + let k = 0; + let i = this.front; + while (true) { + callbackfn(this[i], k); + i = (i + 1) % this.capacity; + k++; + if (i === this.rear) { + break; + } + } + } + isEmpty(): boolean { + return this.rear == this.front; + } + isFull(): boolean { + return this.size() === this.capacity; + } + size(): number { + return this.rear - this.front; + } + [Symbol.iterator](): IterableIterator { + let _this = this; + let i = _this.front; + return { + next: function () { + var done = i == _this.rear; + var value = !done ? _this[i] : undefined; + i = (i + 1) % _this.capacity; + return { + done: done, + value: value, + }; + }, + }; + } + getCapacity(): number { + return this.capacity; + } + increaseCapacityTo(): void { + this.capacity = 2 * this.capacity; + } + trimToCurrentSize(): void { + let len = this.size(); + this.capacity = len; + } + } + Object.freeze(Queue); + fastQueue = Queue; +} +export default { + Queue: fastQueue, +}; diff --git a/container/queue/native_module_queue.cpp b/container/queue/native_module_queue.cpp new file mode 100644 index 0000000000000000000000000000000000000000..a564025f0d8cabd3c16965746738e79dd08ea532 --- /dev/null +++ b/container/queue/native_module_queue.cpp @@ -0,0 +1,65 @@ +/* + * Copyright (c) 2021 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "utils/log.h" +#include "napi/native_api.h" +#include "napi/native_node_api.h" + +extern const char _binary_js_queue_js_start[]; +extern const char _binary_js_queue_js_end[]; +extern const char _binary_queue_abc_start[]; +extern const char _binary_queue_abc_end[]; +namespace OHOS::Util { +static napi_value QueueInit(napi_env env, napi_value exports) +{ + return exports; +} +extern "C" +__attribute__((visibility("default"))) void NAPI_queue_GetJSCode(const char **buf, int *bufLen) +{ + if (buf != nullptr) { + *buf = _binary_js_queue_js_start; + } + + if (bufLen != nullptr) { + *bufLen = _binary_js_queue_js_end - _binary_js_queue_js_start; + } +} + +extern "C" +__attribute__((visibility("default"))) void NAPI_queue_GetABCCode(const char **buf, int *buflen) +{ + if (buf != nullptr) { + *buf = _binary_queue_abc_start; + } + if (buflen != nullptr) { + *buflen = _binary_queue_abc_end - _binary_queue_abc_start; + } +} + +static napi_module queueModule = { + .nm_version = 1, + .nm_flags = 0, + .nm_filename = nullptr, + .nm_register_func = QueueInit, + .nm_modname = "Queue", + .nm_priv = ((void *)0), + .reserved = {0}, +}; +extern "C" __attribute__((constructor)) void RegisterModule() +{ + napi_module_register(&queueModule); +} +} diff --git a/container/stack/js_stack.ts b/container/stack/js_stack.ts new file mode 100644 index 0000000000000000000000000000000000000000..1a68f9ebc238cdd9f57f7bcb34ad46e711f57b3d --- /dev/null +++ b/container/stack/js_stack.ts @@ -0,0 +1,155 @@ +/* + * Copyright (c) 2021 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +interface IterableIterator { + next: () => { + value: T; + done: boolean; + }; +} +let flag = false; +let fastStack: any = undefined; +let arkPritvate = globalThis["ArkPrivate"] || undefined; +if (arkPritvate !== undefined) { + fastStack = arkPritvate.Load(arkPritvate.Stack); +} else { + flag = true; +} +if (flag) { + class handlerStack { + get(obj: Stack, prop: any) { + if (typeof prop === "symbol") { + return obj[prop]; + } + var index = Number.parseInt(prop); + if (Number.isInteger(index)) { + if (index < 0 || index >= obj.size()) { + throw new Error("Stack:get out-of-bounds"); + } + } + return obj[prop]; + } + set(obj: Stack, prop: any, value: T) { + if (prop === "length" || prop === "capacity") { + obj[prop] = value; + return true; + } + var index = Number.parseInt(prop); + if (Number.isInteger(index)) { + if (index < 0 || index >= obj.size()) { + throw new Error("Stack:set out-of-bounds"); + } else { + obj[index] = value; + return true; + } + } + console.error("can not set" + prop); + return false; + } + ownKeys(obj: Stack) { + var keys = []; + for (var i = 0; i < obj.size(); i++) { + keys.push(i.toString()); + } + return keys; + } + defineProperty(obj: Stack, prop: any, desc: any) { + console.error("Can not defineProperty on Stack Object."); + return true; + } + getOwnPropertyDescriptor(obj: Stack, prop: any) { + var index = Number.parseInt(prop); + if (Number.isInteger(index)) { + if (index < 0 || index >= obj.size()) { + throw new Error("Stack:getOwnPropertyDescriptor out-of-bounds"); + } + return Object.getOwnPropertyDescriptor(obj, prop); + } + return; + } + } + class Stack { + private length: number = 0; + private capacity: number = 10; + constructor() { + return new Proxy(this, new handlerStack()); + } + push(item: T): T { + if (this.isFull()) { + this.increaseCapacityTo(); + } + this[this.length++] = item; + return item; + } + pop(): T { + let result = this[this.length - 1]; + this.length--; + return result; + } + peek(): T { + return this[this.length - 1]; + } + locate(element: T): number { + for (let i = 0; i < this.length; i++) { + if (this[i] === element) { + return i; + } + } + return -1; + } + isEmpty(): boolean { + return this.length == 0; + } + forEach(callbackfn: (value: T, index?: number, stack?: Stack) => void,thisArg?: Object): void + { + for (let i = 0; i < this.length; i++) { + callbackfn(this[i], i); + } + } + getCapacity(): number { + return this.capacity; + } + isFull(): boolean { + return this.length === this.capacity; + } + increaseCapacityTo(): void { + this.capacity = 1.5 * this.capacity; + } + trimToCurrentSize(): void { + this.capacity = this.length; + } + size(): number { + return this.length; + } + [Symbol.iterator](): IterableIterator { + let _this = this; + let i = 0; + return { + next: function () { + var done = i >= _this.length; + var value = !done ? _this[i++] : undefined; + return { + done: done, + value: value, + }; + }, + }; + } + } + Object.freeze(Stack); + fastStack = Stack; +} +export default { + Stack: fastStack, +}; diff --git a/container/stack/native_module_stack.cpp b/container/stack/native_module_stack.cpp new file mode 100644 index 0000000000000000000000000000000000000000..11a75d5a5961ceac49b5f97ca93e0e4972dd9fea --- /dev/null +++ b/container/stack/native_module_stack.cpp @@ -0,0 +1,66 @@ +/* + * Copyright (c) 2021 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "utils/log.h" +#include "napi/native_api.h" +#include "napi/native_node_api.h" + +extern const char _binary_js_stack_js_start[]; +extern const char _binary_js_stack_js_end[]; +extern const char _binary_stack_abc_start[]; +extern const char _binary_stack_abc_end[]; +namespace OHOS::Util { +static napi_value StackInit(napi_env env, napi_value exports) +{ + return exports; +} +extern "C" +__attribute__((visibility("default"))) void NAPI_stack_GetJSCode(const char **buf, int *bufLen) +{ + if (buf != nullptr) { + *buf = _binary_js_stack_js_start; + } + + if (bufLen != nullptr) { + *bufLen = _binary_js_stack_js_end - _binary_js_stack_js_start; + } +} + +extern "C" +__attribute__((visibility("default"))) void NAPI_stack_GetABCCode(const char **buf, int *buflen) +{ + if (buf != nullptr) { + *buf = _binary_stack_abc_start; + } + if (buflen != nullptr) { + *buflen = _binary_stack_abc_end - _binary_stack_abc_start; + } +} + +static napi_module stackModule = { + .nm_version = 1, + .nm_flags = 0, + .nm_filename = nullptr, + .nm_register_func = StackInit, + .nm_modname = "Stack", + .nm_priv = ((void *)0), + .reserved = {0}, +}; + +extern "C" __attribute__((constructor)) void RegisterModule() +{ + napi_module_register(&stackModule); +} +} diff --git a/container/tsconfig.json b/container/tsconfig.json new file mode 100644 index 0000000000000000000000000000000000000000..cab5e9dd7bce9384ebcb094233f0d39c8252dc60 --- /dev/null +++ b/container/tsconfig.json @@ -0,0 +1,16 @@ +{ + "compilerOptions": { + "target": "es6", + "module": "es6", + "rootDir": "./", + // "outFile": "./", /* Specify a file that bundles all outputs into one JavaScript file. If `declaration` is true, also designates a file that bundles all .d.ts output. */ + "outDir": "./jscode", /* Specify an output folder for all emitted files. */ + "esModuleInterop": true, + "forceConsistentCasingInFileNames": true, + "strict": true, + "skipLibCheck": true, + "noImplicitThis": false, + "suppressImplicitAnyIndexErrors": true + } +} + diff --git a/container/vector/js_vector.ts b/container/vector/js_vector.ts new file mode 100644 index 0000000000000000000000000000000000000000..96bcde2c17a5bcfb3941464981a976ee255311ed --- /dev/null +++ b/container/vector/js_vector.ts @@ -0,0 +1,329 @@ +interface IterableIterator { + next: () => { + value: T; + done: boolean; + }; +} +let flag = false; +let fastVector: any = undefined; +let arkPritvate = globalThis["ArkPrivate"] || undefined; +if (arkPritvate !== undefined) { + fastVector = arkPritvate.Load(arkPritvate.Vector); +} else { + flag = true; +} +if (flag) { + class handlerVector { + get(obj: Vector, prop: any) { + if (typeof prop === "symbol") { + return obj[prop]; + } + var index = Number.parseInt(prop); + if (Number.isInteger(index)) { + if (index < 0 || index >= obj.size()) { + throw new Error("Vector:get out-of-bounds"); + } + } + return obj[prop]; + } + set(obj: Vector, prop: any, value: T) { + if (prop === "length" || prop === "capacity") { + obj[prop] = value; + return true; + } + var index = Number.parseInt(prop); + if (Number.isInteger(index)) { + if (index < 0 || index > obj.size()) { + throw new Error("Vector:set out-of-bounds"); + } else { + obj[index] = value; + return true; + } + } + console.error("can not set" + prop); + return false; + } + deleteProperty(obj: Vector, prop: any) { + var index = Number.parseInt(prop); + if (Number.isInteger(index)) { + if (index < 0 || index >= obj.size()) { + throw new Error("vector:deleteProperty out-of-bounds"); + } + obj.removeByIndex(index); + return true; + } + console.error("can not deleteProperty" + prop); + return false; + } + has(obj: Vector, prop: any) { + return obj.has(prop); + } + ownKeys(obj: Vector) { + var keys = []; + for (var i = 0; i < obj.size(); i++) { + keys.push(i.toString()); + } + return keys; + } + defineProperty(obj: Vector, prop: any, desc: any) { + console.error("Can not defineProperty on Vector Object."); + return true; + } + getOwnPropertyDescriptor(obj: Vector, prop: any) { + var index = Number.parseInt(prop); + if (Number.isInteger(index)) { + if (index < 0 || index >= obj.size()) { + throw new Error("Vector:getOwnPropertyDescriptor out-of-bounds"); + } + return Object.getOwnPropertyDescriptor(obj, prop); + } + return; + } + } + class Vector { + private length: number = 0; + private capacity: number = 10; + constructor() { + return new Proxy(this, new handlerVector()); + } + add(element: T): boolean { + if (this.isFull()) { + this.resize(); + } + this[this.length++] = element; + return true; + } + insert(element: T, index: number): void { + if (this.isFull()) { + this.resize(); + } + for (let i = this.length; i > index; i--) { + this[i] = this[i - 1]; + } + this[index] = element; + this.length++; + } + size(): number { + return this.length; + } + has(element: T): boolean { + for (let i = 0; i < this.length; i++) { + if (this[i] === element) { + return true; + } + } + return false; + } + get(index: number): T { + return this[index]; + } + getIndexOf(element: T): number { + for (let i = 0; i < this.length; i++) { + if (element === this[i]) { + return i; + } + } + return -1; + } + getFirstElement(): T { + return this[0]; + } + set(index: number, element: T): void { + this[index] = element; + } + removeByIndex(index: number): void { + for (let i = index; i < this.length - 1; i++) { + this[i] = this[i + 1]; + } + this.length--; + } + remove(element: T): boolean { + if (this.has(element)) { + let index = this.getIndexOf(element); + for (let i = index; i < this.length - 1; i++) { + this[i] = this[i + 1]; + } + this.length--; + return true; + } + return false; + } + getLastElement(): T { + return this[this.length - 1]; + } + getLastIndexOf(element: T): number { + for (let i = this.length - 1; i >= 0; i--) { + if (element === this[i]) { + return i; + } + } + return -1; + } + getLastIndexOfFrom(element: T, index: number): number { + if (this.has(element)) { + for (let i = index; i >= 0; i--) { + if (this[i] === element) { + return i; + } + } + } + return -1; + } + getIndexOfFrom(element: T, index: number): number { + if (this.has(element)) { + for (let i = index; i < this.length; i++) { + if (this[i] === element) { + return i; + } + } + } + return -1; + } + clear(): void { + for (let i = 0; i < this.length; i++) { + this[i] = null; + } + this.length = 0; + } + removeByRange(fromIndex: number, toIndex: number): void { + if (fromIndex >= toIndex) { + throw new Error(`fromIndex cannot be less than or equal to toIndex`); + } + toIndex = toIndex >= this.length - 1 ? this.length - 1 : toIndex; + let arr = []; + let k = 0; + for (let i = 0; i < fromIndex; i++) { + arr[k] = this[i]; + k++; + } + for (let j = toIndex; j < this.length; j++) { + arr[k] = this[j]; + k++; + } + this.clear(); + for (let i = 0; i < arr.length; i++) { + this.add(arr[i]); + } + } + setLength(newSize: number): void { + this.length = newSize; + } + replaceAllElements(callbackfn: (value: T, index?: number, vector?: Vector) => T,thisArg?: Object): void + { + for (let i = 0; i < this.length; i++) { + this[i] = callbackfn(this[i], i, this); + } + } + forEach(callbackfn: (value: T, index?: number, vector?: Vector) => void,thisArg?: Object): void + { + for (let i = 0; i < this.length; i++) { + callbackfn(this[i], i, this); + } + } + sort(comparator: (firstValue: T, secondValue: T) => number): void { + let isSort = true; + for (let i = 0; i < this.length; i++) { + for (let j = 0; j < this.length - 1 - i; j++) { + if (comparator(this[j], this[j + 1]) > 0) { + isSort = false; + let temp = this[j]; + this[j] = this[j + 1]; + this[j + 1] = temp; + } + } + if (isSort) { + break; + } + } + } + subVector(fromIndex: number, toIndex: number): Vector + { + if (fromIndex >= toIndex) { + throw new Error(`fromIndex cannot be less than or equal to toIndex`); + } + toIndex = toIndex >= this.length - 1 ? this.length - 1 : toIndex; + let arr = []; + let k = 0; + for (let i = fromIndex; i < toIndex; i++) { + arr[k] = this[i]; + k++; + if (k > toIndex - fromIndex) { + break; + } + } + this.clear(); + for (let j = 0; j < arr.length; j++) { + this.add(arr[j]); + } + return this.clone(); + } + convertToArray(): Array { + let arr = []; + for (let i = 0; i < this.length; i++) { + arr[i] = this[i]; + } + return arr; + } + copyToArray(array: Array): void { + let arr = this.convertToArray(); + for (let i = 0; i < array.length; i++) { + array[i] = arr[i]; + } + } + toString(): string { + let str = `${this[0]}`; + for (let i = 1; i < this.length; i++) { + str = `${str},${this[i]}`; + } + return str; + } + clone(): Vector { + let target = this; + let clone = Object.create(target); + for (let p in target) { + if (target.hasOwnProperty(p)) clone[p] = target[p]; + } + return clone; + } + getCapacity(): number { + return this.capacity; + } + isFull(): boolean { + return this.length === this.capacity; + } + resize(): void { + this.capacity = 2 * this.capacity; + } + increaseCapacityTo(newCapacity: number): void { + if(newCapacity >= this.length){ + this.capacity= newCapacity + } + } + trimToCurrentSize(): void { + this.capacity = this.length; + } + setSize(newSize: number): void { + this.length = newSize; + } + [Symbol.iterator](): IterableIterator { + let _this = this; + let i = 0; + return { + next: function () { + var done = i >= _this.length; + var value = !done ? _this[i++] : undefined; + + return { + done: done, + value: value, + }; + }, + }; + } + } + Object.freeze(Vector); + fastVector = Vector; +} +export default { + Vector: fastVector, +}; diff --git a/container/vector/native_module_vector.cpp b/container/vector/native_module_vector.cpp new file mode 100644 index 0000000000000000000000000000000000000000..dfcba493fcbf8c4d1f5f06c21154a2d909f0eade --- /dev/null +++ b/container/vector/native_module_vector.cpp @@ -0,0 +1,64 @@ +/* + * Copyright (c) 2021 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "utils/log.h" +#include "napi/native_api.h" +#include "napi/native_node_api.h" + +extern const char _binary_js_vector_js_start[]; +extern const char _binary_js_vector_js_end[]; +extern const char _binary_vector_abc_start[]; +extern const char _binary_vector_abc_end[]; +namespace OHOS::Util { +static napi_value VectorInit(napi_env env, napi_value exports) +{ + return exports; +} +extern "C" +__attribute__((visibility("default"))) void NAPI_vector_GetJSCode(const char **buf, int *bufLen) +{ + if (buf != nullptr) { + *buf = _binary_js_vector_js_start; + } + + if (bufLen != nullptr) { + *bufLen = _binary_js_vector_js_end - _binary_js_vector_js_start; + } +} + +extern "C" +__attribute__((visibility("default"))) void NAPI_vector_GetABCCode(const char **buf, int *buflen) +{ + if (buf != nullptr) { + *buf = _binary_vector_abc_start; + } + if (buflen != nullptr) { + *buflen = _binary_vector_abc_end - _binary_vector_abc_start; + } +} +static napi_module vectorModule = { + .nm_version = 1, + .nm_flags = 0, + .nm_filename = nullptr, + .nm_register_func = VectorInit, + .nm_modname = "Vector", + .nm_priv = ((void *)0), + .reserved = {0}, +}; +extern "C" __attribute__((constructor)) void RegisterModule() +{ + napi_module_register(&vectorModule); +} +} diff --git a/ohos.build b/ohos.build index b85022b46f4aed35299b6ac2e4a7130135d8b852..64f3706a86de4ab5249e4325d9eae4f99b5f3337 100755 --- a/ohos.build +++ b/ohos.build @@ -7,7 +7,8 @@ "phone" ], "module_list": [ - "//base/compileruntime/js_util_module/util:util_packages" + "//base/compileruntime/js_util_module/util:util_packages", + "//base/compileruntime/js_util_module/container:container_packages" ], "inner_kits": [ ],