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