mirror of
https://github.com/openharmony/js_util_module.git
synced 2026-07-19 18:23:35 -04:00
Add TS associative container
Signed-off-by: wangyong <wangyong237@huawei.com>
This commit is contained in:
@@ -24,6 +24,14 @@ container_names = [
|
||||
"linkedlist",
|
||||
"list",
|
||||
"stack",
|
||||
"struct",
|
||||
"treemap",
|
||||
"treeset",
|
||||
"hashmap",
|
||||
"hashset",
|
||||
"lightweightmap",
|
||||
"lightweightset",
|
||||
"plainarray",
|
||||
]
|
||||
|
||||
# compile .ts to .js.
|
||||
|
||||
@@ -0,0 +1,171 @@
|
||||
/*
|
||||
* 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.
|
||||
*/
|
||||
declare function requireNapi(s: string): any;
|
||||
|
||||
let flag = false;
|
||||
let fastHashMap = undefined;
|
||||
let arkPritvate = globalThis["ArkPrivate"] || undefined;
|
||||
if (arkPritvate !== undefined) {
|
||||
fastHashMap = arkPritvate.Load(arkPritvate.HashMap);
|
||||
} else {
|
||||
flag = true;
|
||||
}
|
||||
|
||||
if (flag || fastHashMap === undefined) {
|
||||
const HashMapAbility = requireNapi("struct");
|
||||
interface IterableIterator<T> {
|
||||
next: () => {
|
||||
value: T | undefined;
|
||||
done: boolean;
|
||||
};
|
||||
}
|
||||
|
||||
class HandlerHashMap<K, V> {
|
||||
set(target: HashMap<K, V>, p: any, value: any): boolean {
|
||||
if (p in target) {
|
||||
target[p] = value;
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
defineProperty(target: HashMap<K, V>, p: any): boolean {
|
||||
throw new Error("Can't defineProperty on HashMap Object");
|
||||
}
|
||||
deleteProperty(target: HashMap<K, V>, p: any): boolean {
|
||||
throw new Error("Can't deleteProperty on HashMap Object");
|
||||
}
|
||||
setPrototypeOf(target: HashMap<K, V>, p: any): boolean {
|
||||
throw new Error("Can't setPrototype on HashMap Object");
|
||||
}
|
||||
}
|
||||
|
||||
class HashMap<K, V> extends HashMapAbility.DictionaryClass<K, V> {
|
||||
constructor() {
|
||||
super();
|
||||
return new Proxy(this, new HandlerHashMap());
|
||||
}
|
||||
|
||||
get length() {
|
||||
return this.memberNumber;
|
||||
}
|
||||
isEmpty(): boolean {
|
||||
return this.memberNumber === 0;
|
||||
}
|
||||
hasKey(key: K): boolean {
|
||||
return super.hasKey(key);
|
||||
}
|
||||
hasValue(value: V): boolean {
|
||||
return super.Values().indexOf(value) > -1;
|
||||
}
|
||||
get(key: K): V {
|
||||
let result = super.getValueByKey(key);
|
||||
if (result === undefined)
|
||||
throw new Error("this hashmap don't find the key");
|
||||
return result;
|
||||
}
|
||||
setAll(map: HashMap<K, V>): void {
|
||||
let memebers = this.keyValueArray;
|
||||
for (let i = 0; i < memebers.length; i++) {
|
||||
map.put(memebers[i].key, memebers[i].value);
|
||||
}
|
||||
}
|
||||
set(key: K, value: V): Object {
|
||||
return super.put(key, value);
|
||||
}
|
||||
remove(key: K): V {
|
||||
let result = this.removeMember(key);
|
||||
if (result === null) {
|
||||
throw new Error("The removed element does not exist in this container");
|
||||
}
|
||||
return result;
|
||||
}
|
||||
clear(): void {
|
||||
super.clear();
|
||||
}
|
||||
keys(): IterableIterator<K> {
|
||||
let data = this;
|
||||
let count = 0;
|
||||
return {
|
||||
next: function () {
|
||||
var done = count >= data.memberNumber;
|
||||
var value = !done ? data.keyValueArray[count++].key : undefined;
|
||||
return {
|
||||
done: done,
|
||||
value: value,
|
||||
};
|
||||
},
|
||||
};
|
||||
}
|
||||
values(): IterableIterator<V> {
|
||||
let data = this;
|
||||
let count = 0;
|
||||
return {
|
||||
next: function () {
|
||||
var done = count >= data.memberNumber;
|
||||
var value = !done ? data.keyValueArray[count++].value : undefined;
|
||||
return {
|
||||
done: done,
|
||||
value: value,
|
||||
};
|
||||
},
|
||||
};
|
||||
}
|
||||
replace(key: K, newValue: V): boolean {
|
||||
return super.replaceMember(key, newValue);
|
||||
}
|
||||
forEach(callbackfn: (value?: V, key?: K, map?: HashMap<K, V>) => void,
|
||||
thisArg?: Object): void {
|
||||
let tagetArray = this.keyValueArray;
|
||||
for (let i = 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;
|
||||
return {
|
||||
next: function () {
|
||||
var done = count >= data.memberNumber;
|
||||
var value = !done ? data.keyValueArray[count++].entry() : undefined;
|
||||
return {
|
||||
done: done,
|
||||
value: value,
|
||||
};
|
||||
},
|
||||
};
|
||||
}
|
||||
[Symbol.iterator](): IterableIterator<[K, V]> {
|
||||
let data = this;
|
||||
let count = 0;
|
||||
return {
|
||||
next: function () {
|
||||
var done = count >= data.memberNumber;
|
||||
var value = !done ? data.keyValueArray[count++].entry() : undefined;
|
||||
return {
|
||||
done: done,
|
||||
value: value,
|
||||
};
|
||||
},
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
Object.freeze(HashMap);
|
||||
fastHashMap = HashMap;
|
||||
}
|
||||
|
||||
export default {
|
||||
HashMap: fastHashMap,
|
||||
};
|
||||
@@ -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_hashmap_js_start[];
|
||||
extern const char _binary_js_hashmap_js_end[];
|
||||
extern const char _binary_hashmap_abc_start[];
|
||||
extern const char _binary_hashmap_abc_end[];
|
||||
|
||||
namespace OHOS::Util {
|
||||
static napi_value HashMapInit(napi_env env, napi_value exports)
|
||||
{
|
||||
return exports;
|
||||
}
|
||||
|
||||
extern "C"
|
||||
__attribute__((visibility("default"))) void NAPI_hashmap_GetJSCode(const char **buf, int *bufLen)
|
||||
{
|
||||
if (buf != nullptr) {
|
||||
*buf = _binary_js_hashmap_js_start;
|
||||
}
|
||||
|
||||
if (bufLen != nullptr) {
|
||||
*bufLen = _binary_js_hashmap_js_end - _binary_js_hashmap_js_start;
|
||||
}
|
||||
}
|
||||
extern "C"
|
||||
__attribute__((visibility("default"))) void NAPI_hashmap_GetABCCode(const char** buf, int* buflen)
|
||||
{
|
||||
if (buf != nullptr) {
|
||||
*buf = _binary_hashmap_abc_start;
|
||||
}
|
||||
if (buflen != nullptr) {
|
||||
*buflen = _binary_hashmap_abc_end - _binary_hashmap_abc_start;
|
||||
}
|
||||
}
|
||||
|
||||
static napi_module hashMapModule = {
|
||||
.nm_version = 1,
|
||||
.nm_flags = 0,
|
||||
.nm_filename = nullptr,
|
||||
.nm_register_func = HashMapInit,
|
||||
.nm_modname = "HashMap",
|
||||
.nm_priv = ((void*)0),
|
||||
.reserved = { 0 },
|
||||
};
|
||||
|
||||
extern "C" __attribute__ ((constructor)) void RegisterModule()
|
||||
{
|
||||
napi_module_register(&hashMapModule);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,137 @@
|
||||
/*
|
||||
* 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.
|
||||
*/
|
||||
|
||||
declare function requireNapi(s: string): any;
|
||||
|
||||
let flag = false;
|
||||
let fastHashSet = undefined;
|
||||
let arkPritvate = globalThis["ArkPrivate"] || undefined;
|
||||
if (arkPritvate !== undefined) {
|
||||
fastHashSet = arkPritvate.Load(arkPritvate.HashSet);
|
||||
} else {
|
||||
flag = true;
|
||||
}
|
||||
if (flag || fastHashSet === undefined) {
|
||||
const HashSetAbility = requireNapi("struct");
|
||||
interface IterableIterator<T> {
|
||||
next: () => {
|
||||
value: T | undefined;
|
||||
done: boolean;
|
||||
};
|
||||
}
|
||||
|
||||
class HandlerHashSet<T> {
|
||||
set(target: HashSet<T>, p: any, value: any): boolean {
|
||||
if (p in target) {
|
||||
target[p] = value;
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
defineProperty(target: HashSet<T>, p: any): boolean {
|
||||
throw new Error("Can't defineProperty on HashMap Object");
|
||||
}
|
||||
deleteProperty(target: HashSet<T>, p: any): boolean {
|
||||
throw new Error("Can't deleteProperty on HashMap Object");
|
||||
}
|
||||
setPrototypeOf(target: HashSet<T>, p: any): boolean {
|
||||
throw new Error("Can't setPrototype on HashMap Object");
|
||||
}
|
||||
}
|
||||
|
||||
class HashSet<T> extends HashSetAbility.DictionaryClass<T, T> {
|
||||
constructor() {
|
||||
super();
|
||||
return new Proxy(this, new HandlerHashSet());
|
||||
}
|
||||
|
||||
get length() {
|
||||
return this.memberNumber;
|
||||
}
|
||||
isEmpty(): boolean {
|
||||
return this.memberNumber === 0;
|
||||
}
|
||||
has(value: T): boolean {
|
||||
return super.hasKey(value);
|
||||
}
|
||||
add(value: T): boolean {
|
||||
if (this.has(value)) return false;
|
||||
return this.put(value);
|
||||
}
|
||||
remove(value: T): boolean {
|
||||
if (this.removeMember(value) !== null) return true;
|
||||
return false;
|
||||
}
|
||||
clear(): void {
|
||||
super.clear();
|
||||
}
|
||||
forEach(callbackfn: (value?: T, key?: T, set?: HashSet<T>) => void,
|
||||
thisArg?: Object): void {
|
||||
let tagetArray = this.keyValueArray;
|
||||
for (let i = 0; i < tagetArray.length; i++) {
|
||||
callbackfn.call(thisArg, tagetArray[i].key, tagetArray[i].key, this);
|
||||
}
|
||||
}
|
||||
values(): IterableIterator<T> {
|
||||
let _this = this;
|
||||
let i = 0;
|
||||
return {
|
||||
next: function () {
|
||||
var done = i >= _this.memberNumber;
|
||||
var value = !done ? _this.keyValueArray[i++].key : undefined;
|
||||
return {
|
||||
done: done,
|
||||
value: value,
|
||||
};
|
||||
},
|
||||
};
|
||||
}
|
||||
entries(): IterableIterator<[T, T]> {
|
||||
let _this = this;
|
||||
let count = 0;
|
||||
return {
|
||||
next: function () {
|
||||
var done = count >= _this.memberNumber;
|
||||
var value = !done ? _this.keyValueArray[count++].entry() : undefined;
|
||||
return {
|
||||
done: done,
|
||||
value: value,
|
||||
};
|
||||
},
|
||||
};
|
||||
}
|
||||
[Symbol.iterator](): IterableIterator<T> {
|
||||
let _this = this;
|
||||
let count = 0;
|
||||
return {
|
||||
next: function () {
|
||||
var done = count >= _this.memberNumber;
|
||||
var value = !done ? _this.keyValueArray[count++].key : undefined;
|
||||
return {
|
||||
done: done,
|
||||
value: value,
|
||||
};
|
||||
},
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
Object.freeze(HashSet);
|
||||
fastHashSet = HashSet;
|
||||
}
|
||||
|
||||
export default {
|
||||
HashSet: fastHashSet,
|
||||
};
|
||||
@@ -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_hashset_js_start[];
|
||||
extern const char _binary_js_hashset_js_end[];
|
||||
extern const char _binary_hashset_abc_start[];
|
||||
extern const char _binary_hashset_abc_end[];
|
||||
|
||||
namespace OHOS::Util {
|
||||
static napi_value HashSetInit(napi_env env, napi_value exports)
|
||||
{
|
||||
return exports;
|
||||
}
|
||||
|
||||
extern "C"
|
||||
__attribute__((visibility("default"))) void NAPI_hashset_GetJSCode(const char **buf, int *bufLen)
|
||||
{
|
||||
if (buf != nullptr) {
|
||||
*buf = _binary_js_hashset_js_start;
|
||||
}
|
||||
|
||||
if (bufLen != nullptr) {
|
||||
*bufLen = _binary_js_hashset_js_end - _binary_js_hashset_js_start;
|
||||
}
|
||||
}
|
||||
extern "C"
|
||||
__attribute__((visibility("default"))) void NAPI_hashset_GetABCCode(const char** buf, int* buflen)
|
||||
{
|
||||
if (buf != nullptr) {
|
||||
*buf = _binary_hashset_abc_start;
|
||||
}
|
||||
if (buflen != nullptr) {
|
||||
*buflen = _binary_hashset_abc_end - _binary_hashset_abc_start;
|
||||
}
|
||||
}
|
||||
|
||||
static napi_module hashSetModule = {
|
||||
.nm_version = 1,
|
||||
.nm_flags = 0,
|
||||
.nm_filename = nullptr,
|
||||
.nm_register_func = HashSetInit,
|
||||
.nm_modname = "HashSet",
|
||||
.nm_priv = ((void*)0),
|
||||
.reserved = { 0 },
|
||||
};
|
||||
|
||||
extern "C" __attribute__ ((constructor)) void RegisterModule()
|
||||
{
|
||||
napi_module_register(&hashSetModule);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,221 @@
|
||||
/*
|
||||
* 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.
|
||||
*/
|
||||
declare function requireNapi(s: string): any;
|
||||
|
||||
let flag = false;
|
||||
let fastLightWeightMap = undefined;
|
||||
let arkPritvate = globalThis["ArkPrivate"] || undefined;
|
||||
if (arkPritvate !== undefined) {
|
||||
fastLightWeightMap = arkPritvate.Load(arkPritvate.LightWeightMap);
|
||||
} else {
|
||||
flag = true;
|
||||
}
|
||||
|
||||
if (flag || fastLightWeightMap === undefined) {
|
||||
const LightWeightAbility = requireNapi("struct");
|
||||
interface IterableIterator<T> {
|
||||
next: () => {
|
||||
value: T | undefined;
|
||||
done: boolean;
|
||||
};
|
||||
}
|
||||
|
||||
class HandlerLightWeightMap<K, V> {
|
||||
set(target: LightWeightMap<K, V>, p: any, value: any): boolean {
|
||||
if (p in target) {
|
||||
target[p] = value;
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
defineProperty(target: LightWeightMap<K, V>, p: any): boolean {
|
||||
throw new Error("Can't defineProperty on LightWeightMap Object");
|
||||
}
|
||||
deleteProperty(target: LightWeightMap<K, V>, p: any): boolean {
|
||||
throw new Error("Can't deleteProperty on LightWeightMap Object");
|
||||
}
|
||||
setPrototypeOf(target: LightWeightMap<K, V>, p: any): boolean {
|
||||
throw new Error("Can't setPrototype on LightWeightMap Object");
|
||||
}
|
||||
}
|
||||
|
||||
class LightWeightMap<K, V> extends LightWeightAbility.LightWeightClass<K, V> {
|
||||
constructor() {
|
||||
super();
|
||||
return new Proxy(this, new HandlerLightWeightMap());
|
||||
}
|
||||
|
||||
get length() {
|
||||
return this.memberNumber;
|
||||
}
|
||||
hasAll(map: LightWeightMap<K, V>): boolean {
|
||||
if (map.memberNumber > this.memberNumber) return false;
|
||||
if (LightWeightAbility.isIncludeToArray(this.members.keys, map.members.keys) &&
|
||||
LightWeightAbility.isIncludeToArray(this.members.values, map.members.values)) {
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
hasKey(key: K): boolean {
|
||||
return this.members.keys.indexOf(key) > -1;
|
||||
}
|
||||
hasValue(value: V): boolean {
|
||||
return this.members.values.indexOf(value) > -1;
|
||||
}
|
||||
increaseCapacityTo(minimumCapacity: number): void {
|
||||
super.ensureCapacity(minimumCapacity);
|
||||
}
|
||||
entries(): IterableIterator<[K, V]> {
|
||||
let data = this;
|
||||
let count = 0;
|
||||
return {
|
||||
next: function () {
|
||||
var done = count >= data.memberNumber;
|
||||
var value = !done ? [data.members.keys[count], data.members.values[count]] as [K, V] : undefined;
|
||||
count++;
|
||||
return {
|
||||
done: done,
|
||||
value: value,
|
||||
};
|
||||
},
|
||||
};
|
||||
}
|
||||
get(key: K): V {
|
||||
let index = this.getIndexByKey(key);
|
||||
return this.members.values[index];
|
||||
}
|
||||
getIndexOfKey(key: K): number {
|
||||
return this.getIndexByKey(key);
|
||||
}
|
||||
getIndexOfValue(value: V): number {
|
||||
return this.members.values.indexOf(value);
|
||||
}
|
||||
isEmpty(): boolean {
|
||||
return this.memberNumber === 0;
|
||||
}
|
||||
getKeyAt(index: number): K {
|
||||
return this.members.keys[index];
|
||||
}
|
||||
keys(): IterableIterator<K> {
|
||||
let data = this;
|
||||
let count = 0;
|
||||
return {
|
||||
next: function () {
|
||||
var done = count >= data.memberNumber;
|
||||
var value = !done ? data.members.keys[count] : undefined;
|
||||
count++;
|
||||
return {
|
||||
done: done,
|
||||
value: value,
|
||||
};
|
||||
},
|
||||
};
|
||||
}
|
||||
setAll(map: LightWeightMap<K, V>): void {
|
||||
if (map.memberNumber === 0) {
|
||||
map.members.hashs = this.members.hashs.slice();
|
||||
map.members.keys = this.members.keys.slice();
|
||||
map.members.values = this.members.values.slice();
|
||||
map.memberNumber = this.memberNumber;
|
||||
} else {
|
||||
for (let i = 0; i < this.memberNumber; i++) {
|
||||
map.addmember(this.members.keys[i], this.members.values[i]);
|
||||
}
|
||||
}
|
||||
}
|
||||
set(key: K, value: V): Object {
|
||||
this.addmember(key, value);
|
||||
return this;
|
||||
}
|
||||
remove(key: K): V {
|
||||
return this.deletemember(key);
|
||||
}
|
||||
removeAt(index: number): boolean {
|
||||
if (index > this.memberNumber--) return false;
|
||||
this.members.hashs.splice(index, 1);
|
||||
this.members.values.splice(index, 1);
|
||||
this.members.keys.splice(index, 1);
|
||||
this.memberNumber--;
|
||||
return true;
|
||||
}
|
||||
clear(): void {
|
||||
if (this.memberNumber != 0 || this.capacity > 8) {
|
||||
this.members.hashs = [];
|
||||
this.members.keys = [];
|
||||
this.members.values = [];
|
||||
this.memberNumber = 0;
|
||||
this.capacity = 8;
|
||||
}
|
||||
}
|
||||
setValueAt(index: number, newValue: V): boolean {
|
||||
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<K, V>) => void,
|
||||
thisArg?: Object): void {
|
||||
let _this = this;
|
||||
for (let i = 0; i < _this.memberNumber; i++) {
|
||||
callbackfn.call(thisArg, _this.members.values[i], _this.members.keys[i], _this);
|
||||
}
|
||||
}
|
||||
[Symbol.iterator](): IterableIterator<[K, V]> {
|
||||
let data = this;
|
||||
let count = 0;
|
||||
return {
|
||||
next: function () {
|
||||
var done = count >= data.memberNumber;
|
||||
var value = !done ? [data.members.keys[count], data.members.values[count]] as [K, V] : undefined;
|
||||
count++;
|
||||
return {
|
||||
done: done,
|
||||
value: value,
|
||||
};
|
||||
},
|
||||
};
|
||||
}
|
||||
toString(): string {
|
||||
let result = new Array<string>();
|
||||
for (let i = 0; i < this.memberNumber; i++) {
|
||||
result.push(this.members.keys[i] + ":" + this.members.values[i]);
|
||||
}
|
||||
return result.join(",");
|
||||
}
|
||||
getValueAt(index: number): V {
|
||||
return this.members.values[index] as V;
|
||||
}
|
||||
values(): IterableIterator<V> {
|
||||
let data = this;
|
||||
let count = 0;
|
||||
return {
|
||||
next: function () {
|
||||
var done = count >= data.memberNumber;
|
||||
var value = !done ? data.members.values[count] : undefined;
|
||||
count++;
|
||||
return {
|
||||
done: done,
|
||||
value: value,
|
||||
};
|
||||
},
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
Object.freeze(LightWeightMap);
|
||||
fastLightWeightMap = LightWeightMap;
|
||||
}
|
||||
export default {
|
||||
LightWeightMap: fastLightWeightMap,
|
||||
};
|
||||
@@ -0,0 +1,68 @@
|
||||
/*
|
||||
* 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_lightweightmap_js_start[];
|
||||
extern const char _binary_js_lightweightmap_js_end[];
|
||||
extern const char _binary_lightweightmap_abc_start[];
|
||||
extern const char _binary_lightweightmap_abc_end[];
|
||||
|
||||
namespace OHOS::Util {
|
||||
static napi_value LightWeightMapInit(napi_env env, napi_value exports)
|
||||
{
|
||||
return exports;
|
||||
}
|
||||
|
||||
extern "C"
|
||||
__attribute__((visibility("default"))) void NAPI_lightweightmap_GetJSCode(const char **buf, int *bufLen)
|
||||
{
|
||||
if (buf != nullptr) {
|
||||
*buf = _binary_js_lightweightmap_js_start;
|
||||
}
|
||||
|
||||
if (bufLen != nullptr) {
|
||||
*bufLen = _binary_js_lightweightmap_js_end - _binary_js_lightweightmap_js_start;
|
||||
}
|
||||
}
|
||||
|
||||
extern "C"
|
||||
__attribute__((visibility("default"))) void NAPI_lightweightmap_GetABCCode(const char** buf, int* buflen)
|
||||
{
|
||||
if (buf != nullptr) {
|
||||
*buf = _binary_lightweightmap_abc_start;
|
||||
}
|
||||
if (buflen != nullptr) {
|
||||
*buflen = _binary_lightweightmap_abc_end - _binary_lightweightmap_abc_start;
|
||||
}
|
||||
}
|
||||
|
||||
static napi_module lightWeightMapModule = {
|
||||
.nm_version = 1,
|
||||
.nm_flags = 0,
|
||||
.nm_filename = nullptr,
|
||||
.nm_register_func = LightWeightMapInit,
|
||||
.nm_modname = "LightWeightMap",
|
||||
.nm_priv = ((void*)0),
|
||||
.reserved = { 0 },
|
||||
};
|
||||
|
||||
extern "C" __attribute__ ((constructor)) void RegisterModule()
|
||||
{
|
||||
napi_module_register(&lightWeightMapModule);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,184 @@
|
||||
/*
|
||||
* 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.
|
||||
*/
|
||||
declare function requireNapi(s: string): any;
|
||||
|
||||
let flag = false;
|
||||
let fastLightWeightSet = undefined;
|
||||
let arkPritvate = globalThis["ArkPrivate"] || undefined;
|
||||
if (arkPritvate !== undefined) {
|
||||
fastLightWeightSet = arkPritvate.Load(arkPritvate.LightWeightSet);
|
||||
} else {
|
||||
flag = true;
|
||||
}
|
||||
|
||||
if (flag || fastLightWeightSet === undefined) {
|
||||
const LightWeightAbility = requireNapi("struct");
|
||||
interface IterableIterator<T> {
|
||||
next: () => {
|
||||
value: T | undefined;
|
||||
done: boolean;
|
||||
};
|
||||
}
|
||||
|
||||
class HandlerLightWeightSet<T> {
|
||||
set(target: LightWeightSet<T>, p: any, value: any): boolean {
|
||||
if (p in target) {
|
||||
target[p] = value;
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
defineProperty(target: LightWeightSet<T>, p: any): boolean {
|
||||
throw new Error("Can't LightWeightSet on HashMap Object");
|
||||
}
|
||||
deleteProperty(target: LightWeightSet<T>, p: any): boolean {
|
||||
throw new Error("Can't LightWeightSet on HashMap Object");
|
||||
}
|
||||
setPrototypeOf(target: LightWeightSet<T>, p: any): boolean {
|
||||
throw new Error("Can't LightWeightSet on HashMap Object");
|
||||
}
|
||||
}
|
||||
|
||||
class LightWeightSet<T> extends LightWeightAbility.LightWeightClass<T, T> {
|
||||
constructor() {
|
||||
super();
|
||||
return new Proxy(this, new HandlerLightWeightSet());
|
||||
}
|
||||
|
||||
get length() {
|
||||
return this.memberNumber;
|
||||
}
|
||||
add(obj: T): boolean {
|
||||
if (this.members.keys.indexOf(obj) > 0) return false;
|
||||
this.addmember(obj);
|
||||
return true;
|
||||
}
|
||||
addAll(set: LightWeightSet<T>): boolean {
|
||||
let change = false;
|
||||
if (set.memberNumber == 0) {
|
||||
set.memberNumber = this.memberNumber;
|
||||
set.members.hashs = this.members.hashs.slice();
|
||||
set.members.keys = this.members.keys.slice();
|
||||
set.members.values = this.members.values.slice();
|
||||
set.memberNumber = this.memberNumber;
|
||||
change = true;
|
||||
} else {
|
||||
for (let i = 0; i < this.memberNumber; i++) {
|
||||
change = set.add(this.members.keys[i]) || change;
|
||||
}
|
||||
}
|
||||
return change;
|
||||
}
|
||||
hasAll(set: LightWeightSet<T>): boolean {
|
||||
if (set.memberNumber > this.memberNumber) return false;
|
||||
if (LightWeightAbility.isIncludeToArray(this.members.keys, set.members.keys)) {
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
has(key: T): boolean {
|
||||
return this.members.keys.indexOf(key) > -1;
|
||||
}
|
||||
equal(obj: Object): boolean {
|
||||
if (this.memberNumber === 0) return false;
|
||||
if (obj.toString() === this.members.keys.toString()) return true;
|
||||
return false;
|
||||
}
|
||||
increaseCapacityTo(minimumCapacity: number): void {
|
||||
super.ensureCapacity(minimumCapacity);
|
||||
}
|
||||
getIndexOf(key: T): number {
|
||||
return super.getIndexByKey(key);
|
||||
}
|
||||
isEmpty(): boolean {
|
||||
return this.memberNumber === 0;
|
||||
}
|
||||
remove(key: T): T {
|
||||
return super.deletemember(key);
|
||||
}
|
||||
removeAt(index: number): boolean {
|
||||
if (index > this.memberNumber--) return false;
|
||||
this.members.hashs.splice(index, 1);
|
||||
this.members.values.splice(index, 1);
|
||||
this.members.keys.splice(index, 1);
|
||||
this.memberNumber--;
|
||||
return true;
|
||||
}
|
||||
clear(): void {
|
||||
if (this.memberNumber != 0 || this.capacity > 8) {
|
||||
this.members.hashs = [];
|
||||
this.members.keys = [];
|
||||
this.members.values = [];
|
||||
this.memberNumber = 0;
|
||||
this.capacity = 8;
|
||||
}
|
||||
}
|
||||
forEach(callbackfn: (value?: T, key?: T, set?: LightWeightSet<T>) => void,
|
||||
thisArg?: Object): void {
|
||||
let _this = this;
|
||||
for (let i = 0; i < _this.memberNumber; i++) {
|
||||
callbackfn.call(thisArg, _this.members.keys[i], _this.members.keys[i], _this);
|
||||
}
|
||||
}
|
||||
[Symbol.iterator](): IterableIterator<T> {
|
||||
let data = this;
|
||||
let count = 0;
|
||||
return {
|
||||
next: function () {
|
||||
var done = count >= data.memberNumber;
|
||||
var value = !done ? data.members.keys[count] : undefined;
|
||||
return {
|
||||
done: done,
|
||||
value: value,
|
||||
};
|
||||
},
|
||||
};
|
||||
}
|
||||
toString(): string {
|
||||
return this.members.keys.join(",");
|
||||
}
|
||||
toArray(): Array<T> {
|
||||
return this.members.keys.slice();
|
||||
}
|
||||
getValueAt(index: number): T {
|
||||
return this.members.keys[index];
|
||||
}
|
||||
values(): IterableIterator<T> {
|
||||
return this.members.keys.values() as IterableIterator<T>;
|
||||
}
|
||||
entries(): IterableIterator<[T, T]> {
|
||||
let data = this;
|
||||
let count = 0;
|
||||
return {
|
||||
next: function () {
|
||||
var done = count >= data.memberNumber;
|
||||
var tempValue = data.members.keys[count];
|
||||
var value = !done ? ([tempValue, tempValue] as [T, T]) : undefined;
|
||||
count++;
|
||||
return {
|
||||
done: done,
|
||||
value: value,
|
||||
};
|
||||
},
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
Object.freeze(LightWeightSet);
|
||||
fastLightWeightSet = LightWeightSet;
|
||||
}
|
||||
export default {
|
||||
LightWeightSet: fastLightWeightSet,
|
||||
};
|
||||
@@ -0,0 +1,68 @@
|
||||
/*
|
||||
* 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)
|
||||
{
|
||||
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);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,164 @@
|
||||
/*
|
||||
* 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.
|
||||
*/
|
||||
declare function requireNapi(s: string): any;
|
||||
|
||||
let flag = false;
|
||||
let fastPlainArray = undefined;
|
||||
let arkPritvate = globalThis["ArkPrivate"] || undefined;
|
||||
if (arkPritvate !== undefined) {
|
||||
fastPlainArray = arkPritvate.Load(arkPritvate.PlainArray);
|
||||
} else {
|
||||
flag = true;
|
||||
}
|
||||
|
||||
if (flag || fastPlainArray === undefined) {
|
||||
const PlainAbility = requireNapi("struct");
|
||||
interface IterableIterator<T> {
|
||||
next: () => {
|
||||
value: T | undefined;
|
||||
done: boolean;
|
||||
};
|
||||
}
|
||||
|
||||
class HandlerPlainArray<T> {
|
||||
set(target: PlainArray<T>, p: any, value: any): boolean {
|
||||
if (p in target) {
|
||||
target[p] = value;
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
defineProperty(target: PlainArray<T>, p: any): boolean {
|
||||
throw new Error("Can't PlainArray on HashMap Object");
|
||||
}
|
||||
deleteProperty(target: PlainArray<T>, p: any): boolean {
|
||||
throw new Error("Can't PlainArray on HashMap Object");
|
||||
}
|
||||
setPrototypeOf(target: PlainArray<T>, p: any): boolean {
|
||||
throw new Error("Can't PlainArray on HashMap Object");
|
||||
}
|
||||
}
|
||||
class PlainArray<T> extends PlainAbility.PlainArrayClass<T> {
|
||||
constructor() {
|
||||
super();
|
||||
return new Proxy(this, new HandlerPlainArray());
|
||||
}
|
||||
|
||||
get length() {
|
||||
return this.memberNumber;
|
||||
}
|
||||
add(key: number, value: T): void {
|
||||
if(typeof key !== "number") {
|
||||
throw new Error("PlainArray's only number is allowed");
|
||||
}
|
||||
this.addmember(key, value);
|
||||
}
|
||||
clear(): void {
|
||||
if (this.memberNumber != 0) {
|
||||
this.members.keys = [];
|
||||
this.members.values = [];
|
||||
this.memberNumber = 0;
|
||||
}
|
||||
}
|
||||
clone(): PlainArray<T> {
|
||||
let clone = new PlainArray<T>();
|
||||
clone.memberNumber = this.memberNumber;
|
||||
clone.members.keys = this.members.keys.slice();
|
||||
clone.members.values = this.members.values.slice();
|
||||
|
||||
return clone;
|
||||
}
|
||||
has(key: number): boolean {
|
||||
return this.binarySearch_Plain(key) > -1;
|
||||
}
|
||||
get(key: number): T {
|
||||
let index = this.binarySearch_Plain(key);
|
||||
if (index < 0) throw new Error("Key error found");
|
||||
return this.members.values[index];
|
||||
}
|
||||
getIndexOfKey(key: number): number {
|
||||
let result = this.binarySearch_Plain(key);
|
||||
return result < 0 ? -1 : result;
|
||||
}
|
||||
getIndexOfValue(value: T): number {
|
||||
return this.members.values.indexOf(value);
|
||||
}
|
||||
isEmpty(): boolean {
|
||||
return this.memberNumber === 0;
|
||||
}
|
||||
getKeyAt(index: number): number {
|
||||
return this.members.keys[index];
|
||||
}
|
||||
remove(key: number): T {
|
||||
let index = this.binarySearch_Plain(key);
|
||||
if (index < 0) throw new Error(" element not in this plainarray");
|
||||
return this.deletemember(index);
|
||||
}
|
||||
removeAt(index: number): T {
|
||||
if (index >= this.memberNumber || index < 0) throw new Error("index not in this plainarray range");
|
||||
return this.deletemember(index);
|
||||
}
|
||||
removeRangeFrom(index: number, size: number): number {
|
||||
if (index >= this.memberNumber || index < 0) throw new Error("index not in this plainarray range");
|
||||
let safeSize = (this.memberNumber - (index + size) < 0) ? this.memberNumber - size : size;
|
||||
this.deletemember(index, safeSize);
|
||||
return safeSize;
|
||||
}
|
||||
setValueAt(index: number, value: T): void {
|
||||
if (index >= 0 && index < this.memberNumber) {
|
||||
this.members.values[index] = value;
|
||||
} else {
|
||||
throw new Error("index Out Of Bounds");
|
||||
}
|
||||
}
|
||||
toString(): string {
|
||||
let result = new Array<string>();
|
||||
for (let i = 0; i < this.memberNumber; i++) {
|
||||
result.push(this.members.keys[i] + ":" + this.members.values[i]);
|
||||
}
|
||||
return result.join(",");
|
||||
}
|
||||
getValueAt(index: number): T {
|
||||
return this.members.values[index];
|
||||
}
|
||||
forEach(callbackfn: (value: T, index?: number, PlainArray?: PlainArray<T>) => void,
|
||||
thisArg?: Object): void {
|
||||
for (let i = 0; i < this.memberNumber; i++) {
|
||||
callbackfn.call(thisArg, this.members.values[i], this.members.keys[i]);
|
||||
}
|
||||
}
|
||||
[Symbol.iterator](): IterableIterator<[number, T]> {
|
||||
let _this = this;
|
||||
let count = 0;
|
||||
return {
|
||||
next: function () {
|
||||
var done = count >= _this.memberNumber;
|
||||
var value = !done ? [_this.members.keys[count], _this.members.values[count]] as [number, T] : undefined;
|
||||
count++;
|
||||
return {
|
||||
done: done,
|
||||
value: value,
|
||||
};
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
}
|
||||
Object.freeze(PlainArray);
|
||||
fastPlainArray = PlainArray;
|
||||
}
|
||||
export default {
|
||||
PlainArray: fastPlainArray,
|
||||
};
|
||||
@@ -0,0 +1,68 @@
|
||||
/*
|
||||
* 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)
|
||||
{
|
||||
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);
|
||||
}
|
||||
}
|
||||
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,68 @@
|
||||
/*
|
||||
* 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_struct_js_start[];
|
||||
extern const char _binary_js_struct_js_end[];
|
||||
extern const char _binary_struct_abc_start[];
|
||||
extern const char _binary_struct_abc_end[];
|
||||
|
||||
namespace OHOS::Util {
|
||||
static napi_value StructInit(napi_env env, napi_value exports)
|
||||
{
|
||||
return exports;
|
||||
}
|
||||
|
||||
extern "C"
|
||||
__attribute__((visibility("default"))) void NAPI_struct_GetJSCode(const char **buf, int *bufLen)
|
||||
{
|
||||
if (buf != nullptr) {
|
||||
*buf = _binary_js_struct_js_start;
|
||||
}
|
||||
|
||||
if (bufLen != nullptr) {
|
||||
*bufLen = _binary_js_struct_js_end - _binary_js_struct_js_start;
|
||||
}
|
||||
}
|
||||
|
||||
extern "C"
|
||||
__attribute__((visibility("default"))) void NAPI_struct_GetABCCode(const char** buf, int* buflen)
|
||||
{
|
||||
if (buf != nullptr) {
|
||||
*buf = _binary_struct_abc_start;
|
||||
}
|
||||
if (buflen != nullptr) {
|
||||
*buflen = _binary_struct_abc_end - _binary_struct_abc_start;
|
||||
}
|
||||
}
|
||||
|
||||
static napi_module structModule = {
|
||||
.nm_version = 1,
|
||||
.nm_flags = 0,
|
||||
.nm_filename = nullptr,
|
||||
.nm_register_func = StructInit,
|
||||
.nm_modname = "struct",
|
||||
.nm_priv = ((void*)0),
|
||||
.reserved = { 0 },
|
||||
};
|
||||
|
||||
extern "C" __attribute__ ((constructor)) void RegisterModule()
|
||||
{
|
||||
napi_module_register(&structModule);
|
||||
}
|
||||
}
|
||||
@@ -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.
|
||||
*/
|
||||
declare function requireNapi(s: string): any;
|
||||
|
||||
let flag = false;
|
||||
let fastTreeMap = undefined;
|
||||
let arkPritvate = globalThis["ArkPrivate"] || undefined;
|
||||
if (arkPritvate !== undefined) {
|
||||
fastTreeMap = arkPritvate.Load(arkPritvate.TreeMap);
|
||||
} else {
|
||||
flag = true;
|
||||
}
|
||||
if (flag || fastTreeMap === undefined) {
|
||||
const RBTreeAbility = requireNapi("struct")
|
||||
interface IterableIterator<T> {
|
||||
next: () => {
|
||||
value: T | undefined;
|
||||
done: boolean;
|
||||
};
|
||||
}
|
||||
|
||||
class HandlerTreeMap<K, V> {
|
||||
set(target: TreeMap<K, V>, p: any, value: any): boolean {
|
||||
if (p in target) {
|
||||
target[p] = value;
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
defineProperty(target: TreeMap<K, V>, p: any): boolean {
|
||||
throw new Error("Can't defineProperty on TreeMap Object");
|
||||
}
|
||||
deleteProperty(target: TreeMap<K, V>, p: any): boolean {
|
||||
throw new Error("Can't deleteProperty on TreeMap Object");
|
||||
}
|
||||
setPrototypeOf(target: TreeMap<K, V>, p: any): boolean {
|
||||
throw new Error("Can't setPrototype on TreeMap Object");
|
||||
}
|
||||
}
|
||||
|
||||
class TreeMap<K, V> {
|
||||
private _constitute: any;
|
||||
constructor() {
|
||||
this._constitute = new RBTreeAbility.RBTreeClass();
|
||||
return new Proxy(this, new HandlerTreeMap());
|
||||
}
|
||||
|
||||
get length() {
|
||||
return this._constitute.memberNumber;
|
||||
}
|
||||
hasKey(key: K): boolean {
|
||||
return this._constitute.getNode(key) !== null;
|
||||
}
|
||||
hasValue(value: V): boolean {
|
||||
return this._constitute.findNode(value) !== null;
|
||||
}
|
||||
get(key: K): V | null {
|
||||
let tempNode = this._constitute.getNode(key);
|
||||
if (tempNode === null)
|
||||
return null;
|
||||
return tempNode.value;
|
||||
}
|
||||
getFirstKey(): K {
|
||||
let tempNode = this._constitute.firstNode();
|
||||
if (tempNode === null)
|
||||
throw new Error("don't find this key,this tree is null");
|
||||
return tempNode.key;
|
||||
}
|
||||
getLastKey(): K {
|
||||
let tempNode = this._constitute.lastNode();
|
||||
if (tempNode === null)
|
||||
throw new Error("don't find this key,this tree is null");
|
||||
return tempNode.key;
|
||||
}
|
||||
setAll(map: TreeMap<K, V>) {
|
||||
this._constitute.setAll(map._constitute);
|
||||
}
|
||||
set(key: K, value: V): Object {
|
||||
// If the user enters key other than number and string, a comparison function is required
|
||||
return this._constitute.addNode(key, value);
|
||||
}
|
||||
remove(key: K): V | null {
|
||||
return this._constitute.removeNode(key);
|
||||
}
|
||||
clear() {
|
||||
this._constitute.clearTree();
|
||||
}
|
||||
getLowerKey(key: K): K {
|
||||
let tempNode = this._constitute.getNode(key);
|
||||
if (tempNode === null)
|
||||
throw new Error("don't find this key,this node is undefine");
|
||||
if (tempNode.left !== null) return tempNode.left.key;
|
||||
let node = tempNode;
|
||||
while (node.parent !== null) {
|
||||
if (node.parent.right === node) return node.parent.key;
|
||||
node = node.parent;
|
||||
}
|
||||
throw new Error("don't find a key meet the conditions");
|
||||
}
|
||||
getHigherKey(key: K): K {
|
||||
let tempNode = this._constitute.getNode(key);
|
||||
|
||||
if (tempNode === null)
|
||||
throw new Error("don't find this key,this node is undefine");
|
||||
if (tempNode.right !== null) return tempNode.right.key;
|
||||
let node = tempNode;
|
||||
while (node.parent !== null) {
|
||||
if (node.parent.left === node) return node.parent.key;
|
||||
node = node.parent;
|
||||
}
|
||||
|
||||
throw new Error("don't find a key meet the conditions");
|
||||
}
|
||||
keys(): IterableIterator<K> {
|
||||
let _this = this._constitute;
|
||||
let i = 0;
|
||||
return {
|
||||
next: function () {
|
||||
var done = i >= _this.memberNumber;
|
||||
var value = !done ? _this.keyValueArray[i++].key : undefined;
|
||||
return {
|
||||
done: done,
|
||||
value: value,
|
||||
};
|
||||
},
|
||||
};
|
||||
}
|
||||
values(): IterableIterator<V> {
|
||||
let _this = this._constitute;
|
||||
let i = 0;
|
||||
return {
|
||||
next: function () {
|
||||
var done = i >= _this.memberNumber;
|
||||
var value = !done ? _this.keyValueArray[i++].value as V : undefined;
|
||||
return {
|
||||
done: done,
|
||||
value: value,
|
||||
};
|
||||
},
|
||||
};
|
||||
}
|
||||
replace(key: K, newValue: V): boolean {
|
||||
let targetNode = this._constitute.getNode(key);
|
||||
if (targetNode === null) return false;
|
||||
targetNode.value = newValue;
|
||||
return true;
|
||||
}
|
||||
forEach(callbackfn: (value?: V, key?: K, map?: TreeMap<K, V>) => void,
|
||||
thisArg?: Object): void {
|
||||
let _this = this._constitute;
|
||||
let tagetArray = _this.keyValueArray;
|
||||
for (let i = 0; i < _this.memberNumber; i++) {
|
||||
callbackfn.call(thisArg, tagetArray[i].value as V, tagetArray[i].key);
|
||||
}
|
||||
}
|
||||
entries(): IterableIterator<[K, V]> {
|
||||
let _this = this._constitute;
|
||||
let i = 0;
|
||||
return {
|
||||
next: function () {
|
||||
var done = i >= _this.memberNumber;
|
||||
var value = !done ? _this.keyValueArray[i++].entry() : undefined;
|
||||
return {
|
||||
done: done,
|
||||
value: value,
|
||||
};
|
||||
},
|
||||
};
|
||||
}
|
||||
[Symbol.iterator](): IterableIterator<[K, V]> {
|
||||
let _this = this._constitute;
|
||||
let i = 0;
|
||||
return {
|
||||
next: function () {
|
||||
var done = i >= _this.memberNumber;
|
||||
var value = !done ? _this.keyValueArray[i++].entry() : undefined;
|
||||
return {
|
||||
done: done,
|
||||
value: value,
|
||||
};
|
||||
},
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
Object.freeze(TreeMap);
|
||||
fastTreeMap = TreeMap;
|
||||
}
|
||||
export default {
|
||||
TreeMap: fastTreeMap,
|
||||
};
|
||||
@@ -0,0 +1,68 @@
|
||||
/*
|
||||
* 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_treemap_js_start[];
|
||||
extern const char _binary_js_treemap_js_end[];
|
||||
extern const char _binary_treemap_abc_start[];
|
||||
extern const char _binary_treemap_abc_end[];
|
||||
|
||||
namespace OHOS::Util {
|
||||
static napi_value TreeMapInit(napi_env env, napi_value exports)
|
||||
{
|
||||
return exports;
|
||||
}
|
||||
|
||||
extern "C"
|
||||
__attribute__((visibility("default"))) void NAPI_treemap_GetJSCode(const char **buf, int *bufLen)
|
||||
{
|
||||
if (buf != nullptr) {
|
||||
*buf = _binary_js_treemap_js_start;
|
||||
}
|
||||
|
||||
if (bufLen != nullptr) {
|
||||
*bufLen = _binary_js_treemap_js_end - _binary_js_treemap_js_start;
|
||||
}
|
||||
}
|
||||
|
||||
extern "C"
|
||||
__attribute__((visibility("default"))) void NAPI_treemap_GetABCCode(const char** buf, int* buflen)
|
||||
{
|
||||
if (buf != nullptr) {
|
||||
*buf = _binary_treemap_abc_start;
|
||||
}
|
||||
if (buflen != nullptr) {
|
||||
*buflen = _binary_treemap_abc_end - _binary_treemap_abc_start;
|
||||
}
|
||||
}
|
||||
|
||||
static napi_module treeMapModule = {
|
||||
.nm_version = 1,
|
||||
.nm_flags = 0,
|
||||
.nm_filename = nullptr,
|
||||
.nm_register_func = TreeMapInit,
|
||||
.nm_modname = "TreeMap",
|
||||
.nm_priv = ((void*)0),
|
||||
.reserved = { 0 },
|
||||
};
|
||||
|
||||
extern "C" __attribute__ ((constructor)) void RegisterModule()
|
||||
{
|
||||
napi_module_register(&treeMapModule);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,194 @@
|
||||
/*
|
||||
* 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.
|
||||
*/
|
||||
declare function requireNapi(s: string): any;
|
||||
|
||||
let flag = false;
|
||||
let fastTreeSet = undefined;
|
||||
let arkPritvate = globalThis["ArkPrivate"] || undefined;
|
||||
if (arkPritvate !== undefined) {
|
||||
fastTreeSet = arkPritvate.Load(arkPritvate.TreeSet);
|
||||
} else {
|
||||
flag = true;
|
||||
}
|
||||
|
||||
if (flag || fastTreeSet === undefined) {
|
||||
const RBTreeAbility = requireNapi("struct");
|
||||
interface IterableIterator<T> {
|
||||
next: () => {
|
||||
value: T | undefined;
|
||||
done: boolean;
|
||||
};
|
||||
}
|
||||
|
||||
class HandlerTreeSet<T> {
|
||||
set(target: TreeSet<T>, p: any, value: any): boolean {
|
||||
if (p in target) {
|
||||
target[p] = value;
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
defineProperty(target: TreeSet<T>, p: any): boolean {
|
||||
throw new Error("Can't TreeSet on HashMap Object");
|
||||
}
|
||||
deleteProperty(target: TreeSet<T>, p: any): boolean {
|
||||
throw new Error("Can't TreeSet on HashMap Object");
|
||||
}
|
||||
setPrototypeOf(target: TreeSet<T>, p: any): boolean {
|
||||
throw new Error("Can't TreeSet on HashMap Object");
|
||||
}
|
||||
}
|
||||
|
||||
class TreeSet<T> {
|
||||
private _constitute: any;
|
||||
constructor() {
|
||||
this._constitute = new RBTreeAbility.RBTreeClass();
|
||||
return new Proxy(this, new HandlerTreeSet());
|
||||
}
|
||||
|
||||
get length() {
|
||||
return this._constitute.memberNumber;
|
||||
}
|
||||
isEmpty(): boolean {
|
||||
return this._constitute.isEmpty();
|
||||
}
|
||||
has(value: T): boolean {
|
||||
return this._constitute.getNode(value) !== null;
|
||||
}
|
||||
add(value: T): boolean {
|
||||
this._constitute.addNode(value);
|
||||
return true;
|
||||
}
|
||||
remove(value: T): boolean {
|
||||
let result = this._constitute.removeNode(value);
|
||||
return result !== null;
|
||||
}
|
||||
clear() {
|
||||
this._constitute.clearTree();
|
||||
}
|
||||
getFirstValue(): T {
|
||||
let tempNode = this._constitute.firstNode();
|
||||
if (tempNode === null)
|
||||
throw new Error("don't find this key,this tree is null");
|
||||
return tempNode.key;
|
||||
}
|
||||
getLastValue(): T {
|
||||
let tempNode = this._constitute.lastNode();
|
||||
if (tempNode === null)
|
||||
throw new Error("don't find this key,this tree is null");
|
||||
return tempNode.key;
|
||||
}
|
||||
getLowerValue(key: T): T {
|
||||
let tempNode = this._constitute.getNode(key);
|
||||
|
||||
if (tempNode === null)
|
||||
throw new Error("don't find this key,this node is undefine");
|
||||
if (tempNode.left !== null) return tempNode.left.key;
|
||||
let node = tempNode;
|
||||
while (node.parent !== null) {
|
||||
if (node.parent.right === node) return node.parent.key;
|
||||
node = node.parent; // node.parent.left === node is true;
|
||||
}
|
||||
|
||||
throw new Error("don't find a key meet the conditions");
|
||||
}
|
||||
getHigherValue(key: T): T {
|
||||
let tempNode = this._constitute.getNode(key);
|
||||
|
||||
if (tempNode === null)
|
||||
throw new Error("don't find this key,this node is undefine");
|
||||
if (tempNode.right !== null) return tempNode.right.key;
|
||||
let node = tempNode;
|
||||
while (node.parent !== null) {
|
||||
if (node.parent.left === node) return node.parent.key;
|
||||
node = node.parent; // node.parent.right === node is true;
|
||||
}
|
||||
|
||||
throw new Error("don't find a key meet the conditions");
|
||||
}
|
||||
popFirst(): T {
|
||||
let firstNode = this._constitute.firstNode();
|
||||
if (firstNode === null)
|
||||
throw new Error("don't find first node,this tree is empty");
|
||||
let value = firstNode.value;
|
||||
this._constitute.removeNodeProcess(firstNode);
|
||||
return value as T;
|
||||
}
|
||||
popLast(): T {
|
||||
let lastNode = this._constitute.lastNode();
|
||||
if (lastNode === null)
|
||||
throw new Error("don't find last node,this tree is empty");
|
||||
let value = lastNode.value;
|
||||
this._constitute.removeNodeProcess(lastNode);
|
||||
return value as T;
|
||||
}
|
||||
values(): IterableIterator<T> {
|
||||
let _this = this._constitute;
|
||||
let i = 0;
|
||||
return {
|
||||
next: function () {
|
||||
var done = i >= _this.memberNumber;
|
||||
var value = !done ? _this.keyValueArray[i++].value as T : undefined;
|
||||
return {
|
||||
done: done,
|
||||
value: value,
|
||||
};
|
||||
},
|
||||
};
|
||||
}
|
||||
forEach(callbackfn: (value?: T, key?: T, set?: TreeSet<T>) => void,
|
||||
thisArg?: Object): void {
|
||||
let _this = this._constitute;
|
||||
let tagetArray = _this.keyValueArray;
|
||||
for (let i = 0; i < _this.memberNumber; i++) {
|
||||
callbackfn.call(thisArg, tagetArray[i].value as T, tagetArray[i].key);
|
||||
}
|
||||
}
|
||||
entries(): IterableIterator<[T, T]> {
|
||||
let _this = this._constitute;
|
||||
let i = 0;
|
||||
return {
|
||||
next: function () {
|
||||
var done = i >= _this.memberNumber;
|
||||
var value = !done ? _this.keyValueArray[i++].entry() : undefined;
|
||||
return {
|
||||
done: done,
|
||||
value: value,
|
||||
};
|
||||
},
|
||||
};
|
||||
}
|
||||
[Symbol.iterator](): IterableIterator<T> {
|
||||
let _this = this._constitute;
|
||||
let i = 0;
|
||||
return {
|
||||
next: function () {
|
||||
var done = i >= _this.memberNumber;
|
||||
var value = !done ? _this.keyValueArray[i++].key : undefined;
|
||||
return {
|
||||
done: done,
|
||||
value: value,
|
||||
};
|
||||
},
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
Object.freeze(TreeSet);
|
||||
fastTreeSet = TreeSet;
|
||||
}
|
||||
export default {
|
||||
TreeSet: fastTreeSet,
|
||||
};
|
||||
@@ -0,0 +1,68 @@
|
||||
/*
|
||||
* 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_treeset_js_start[];
|
||||
extern const char _binary_js_treeset_js_end[];
|
||||
extern const char _binary_treeset_abc_start[];
|
||||
extern const char _binary_treeset_abc_end[];
|
||||
|
||||
namespace OHOS::Util {
|
||||
static napi_value TreeSetInit(napi_env env, napi_value exports)
|
||||
{
|
||||
return exports;
|
||||
}
|
||||
|
||||
extern "C"
|
||||
__attribute__((visibility("default"))) void NAPI_treeset_GetJSCode(const char **buf, int *bufLen)
|
||||
{
|
||||
if (buf != nullptr) {
|
||||
*buf = _binary_js_treeset_js_start;
|
||||
}
|
||||
|
||||
if (bufLen != nullptr) {
|
||||
*bufLen = _binary_js_treeset_js_end - _binary_js_treeset_js_start;
|
||||
}
|
||||
}
|
||||
|
||||
extern "C"
|
||||
__attribute__((visibility("default"))) void NAPI_treeset_GetABCCode(const char** buf, int* buflen)
|
||||
{
|
||||
if (buf != nullptr) {
|
||||
*buf = _binary_treeset_abc_start;
|
||||
}
|
||||
if (buflen != nullptr) {
|
||||
*buflen = _binary_treeset_abc_end - _binary_treeset_abc_start;
|
||||
}
|
||||
}
|
||||
|
||||
static napi_module treeSetModule = {
|
||||
.nm_version = 1,
|
||||
.nm_flags = 0,
|
||||
.nm_filename = nullptr,
|
||||
.nm_register_func = TreeSetInit,
|
||||
.nm_modname = "TreeSet",
|
||||
.nm_priv = ((void*)0),
|
||||
.reserved = { 0 },
|
||||
};
|
||||
|
||||
extern "C" __attribute__ ((constructor)) void RegisterModule()
|
||||
{
|
||||
napi_module_register(&treeSetModule);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user