mirror of
https://github.com/openharmony/js_api_module.git
synced 2026-07-21 06:15:27 -04:00
81209848f2
Signed-off-by: lengchangjing <lengchangjing@huawei.com>
161 lines
4.5 KiB
TypeScript
161 lines
4.5 KiB
TypeScript
/*
|
|
* Copyright (c) 2022 Huawei Device Co., Ltd.
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
* you may not use this file except in compliance with the License.
|
|
* You may obtain a copy of the License at
|
|
*
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
*
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
* See the License for the specific language governing permissions and
|
|
* limitations under the License.
|
|
*/
|
|
interface 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) {
|
|
class HandlerStack<T> {
|
|
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');
|
|
}
|
|
}
|
|
}
|
|
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): boolean {
|
|
if (prop === 'elementNum' || prop === 'capacity') {
|
|
obj[prop] = value;
|
|
return true;
|
|
}
|
|
this.isOutBounds(obj, 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>): 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() {
|
|
return true;
|
|
}
|
|
getOwnPropertyDescriptor(obj: Stack<T>, prop: any) {
|
|
this.isOutBounds(obj, prop);
|
|
let index: number = Number.parseInt(prop);
|
|
if (index >= 0 && index < obj.length && Number.isInteger(index)) {
|
|
return Object.getOwnPropertyDescriptor(obj, prop);
|
|
}
|
|
return
|
|
}
|
|
setPrototypeOf(): T {
|
|
throw new RangeError('Can setPrototype on Stack Object');
|
|
}
|
|
}
|
|
interface IterableIterator<T> {
|
|
next: () => {
|
|
value: T;
|
|
done: boolean;
|
|
};
|
|
}
|
|
class Stack<T> {
|
|
private elementNum: number = 0;
|
|
private capacity: number = 10;
|
|
constructor() {
|
|
return new Proxy(this, new HandlerStack());
|
|
}
|
|
get length(): number {
|
|
return this.elementNum;
|
|
}
|
|
push(item: T): T {
|
|
if (this.isFull()) {
|
|
this.increaseCapacity();
|
|
}
|
|
this[this.elementNum++] = item;
|
|
return item;
|
|
}
|
|
pop(): T {
|
|
if (this.isEmpty()) {
|
|
return undefined;
|
|
}
|
|
let result: T = undefined;
|
|
result = this[this.length - 1];
|
|
this.elementNum--;
|
|
return result;
|
|
}
|
|
peek(): T {
|
|
if (this.isEmpty()) {
|
|
return undefined;
|
|
}
|
|
return this[this.length - 1];
|
|
}
|
|
locate(element: T): number {
|
|
for (let i: number = 0; i < this.length; i++) {
|
|
if (this[i] === element) {
|
|
return i;
|
|
}
|
|
}
|
|
return -1;
|
|
}
|
|
isEmpty(): boolean {
|
|
return this.elementNum === 0;
|
|
}
|
|
forEach(callbackfn: (value: T, index?: number, stack?: Stack<T>) => void,
|
|
thisArg?: Object): void {
|
|
for (let i: number = 0; i < this.length; i++) {
|
|
callbackfn.call(thisArg, this[i], i, this);
|
|
}
|
|
}
|
|
private isFull(): boolean {
|
|
return this.elementNum === this.capacity;
|
|
}
|
|
private increaseCapacity(): void {
|
|
this.capacity = 1.5 * this.capacity;
|
|
}
|
|
[Symbol.iterator](): IterableIterator<T> {
|
|
let count: number = 0;
|
|
let stack: Stack<T> = this;
|
|
return {
|
|
next: function () {
|
|
let done: boolean = false;
|
|
let value: T = undefined;
|
|
done = count >= stack.elementNum;
|
|
value = done ? undefined : stack[count++];
|
|
return {
|
|
done: done,
|
|
value: value,
|
|
};
|
|
},
|
|
};
|
|
}
|
|
}
|
|
Object.freeze(Stack);
|
|
fastStack = Stack;
|
|
}
|
|
export default fastStack;
|