Correct relevant interfaces on TS side

Description
    1. Fix some interface errors
    2. Correct some wrong parameters
    3. complete some missing interfaces
    4. Align with the ark side and throw exception description
    #I4X54A:Correct relevant interfaces on TS side

Signed-off-by: wangyong1995626wywz <wangyong237@huawei.com>
This commit is contained in:
wangyong1995626wywz
2022-03-08 17:11:16 +08:00
parent 5f68b2bfde
commit 4301d443af
30 changed files with 648 additions and 548 deletions
+44 -39
View File
@@ -22,16 +22,19 @@ if (arkPritvate !== undefined) {
}
if (flag || fastArrayList == undefined) {
class HandlerArrayList<T> {
private isOutBounds(obj: ArrayList<T>, prop: any) {
let index = Number.parseInt(prop);
if (Number.isInteger(index)) {
if (index < 0 || index >= obj.length) {
throw new RangeError("the index is out-of-bounds");
}
}
}
get(obj: ArrayList<T>, prop: any) {
if (typeof prop === "symbol") {
return obj[prop];
}
var index = Number.parseInt(prop);
if (Number.isInteger(index)) {
if (index < 0 || index >= obj.length) {
throw new Error("ArrayList: get out-of-bounds");
}
}
this.isOutBounds(obj, prop);
return obj[prop];
}
set(obj: ArrayList<T>, prop: any, value: T) {
@@ -39,10 +42,10 @@ if (flag || fastArrayList == undefined) {
obj[prop] = value;
return true;
}
var index = Number.parseInt(prop);
let index = Number.parseInt(prop);
if (Number.isInteger(index)) {
if (index < 0 || index > obj.length) {
throw new Error("ArrayList: set out-of-bounds");
throw new RangeError("the index is out-of-bounds");
} else {
obj[index] = value;
return true;
@@ -51,11 +54,9 @@ if (flag || fastArrayList == undefined) {
return false;
}
deleteProperty(obj: ArrayList<T>, prop: any) {
var index = Number.parseInt(prop);
if (Number.isInteger(index)) {
if (index < 0 || index >= obj.length) {
throw new Error("ArrayList: deleteProperty out-of-bounds");
}
this.isOutBounds(obj, prop);
let index = Number.parseInt(prop);
if (index >= 0 && index < obj.length && Number.isInteger(index)) {
obj.removeByIndex(index);
return true;
}
@@ -75,17 +76,15 @@ if (flag || fastArrayList == undefined) {
return true;
}
getOwnPropertyDescriptor(obj: ArrayList<T>, prop: any) {
var index = Number.parseInt(prop);
if (Number.isInteger(index)) {
if (index < 0 || index >= obj.length) {
throw new Error("ArrayList: getOwnPropertyDescriptor out-of-bounds");
}
this.isOutBounds(obj, prop);
let index = 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 Error("Can setPrototype on ArrayList Object");
throw new TypeError("Can setPrototype on ArrayList Object");
}
}
interface IterableIterator<T> {
@@ -111,6 +110,9 @@ if (flag || fastArrayList == undefined) {
return true;
}
insert(element: T, index: number): void {
if (index < 0 || index >= this.elementNum) {
throw new RangeError("the index is out-of-bounds");
}
if (this.isFull()) {
this.resize();
}
@@ -138,7 +140,7 @@ if (flag || fastArrayList == undefined) {
}
removeByIndex(index: number): T {
if (index < 0 || index >= this.elementNum) {
throw new Error("removeByIndex 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++) {
@@ -168,9 +170,12 @@ if (flag || fastArrayList == undefined) {
}
removeByRange(fromIndex: number, toIndex: number): void {
if (fromIndex >= toIndex) {
throw new Error(`fromIndex cannot be less than or equal to toIndex`);
throw new RangeError(`the fromIndex cannot be less than or equal to toIndex`);
}
toIndex = toIndex >= this.elementNum - 1 ? this.elementNum - 1 : toIndex;
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++) {
this[i] = this[j];
@@ -178,13 +183,13 @@ if (flag || fastArrayList == undefined) {
}
this.elementNum -= toIndex - fromIndex;
}
replaceAllElements(callbackfn: (value: T, index?: number, arraylist?: ArrayList<T>) => T,
replaceAllElements(callbackfn: (value: T, index?: number, arrList?: ArrayList<T>) => T,
thisArg?: Object): void {
for (let i = 0; i < this.elementNum; i++) {
this[i] = callbackfn.call(thisArg, this[i], i, this);
}
}
forEach(callbackfn: (value: T, index?: number, arraylist?: ArrayList<T>) => void,
forEach(callbackfn: (value: T, index?: number, arrList?: ArrayList<T>) => void,
thisArg?: Object): void {
for (let i = 0; i < this.elementNum; i++) {
callbackfn.call(thisArg, this[i], i, this);
@@ -204,7 +209,7 @@ if (flag || fastArrayList == undefined) {
}
}
} else {
for (var i = 0; i < this.length - 1; i++) {
for (let i = 0; i < this.length - 1; i++) {
for (let j = 0; j < this.elementNum - 1 - i; j++) {
if (this.asciSort(this[j], this[j + 1])) {
isSort = false;
@@ -221,26 +226,26 @@ 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]")) {
curElement = curElement.toString();
nextElement = nextElement.toString();
if(curElement > nextElement) {
return true;
}
return false
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;
}
subArrayList(fromIndex: number, toIndex: number): ArrayList<T> {
if (fromIndex >= toIndex) {
throw new Error(`fromIndex cannot be less than or equal to toIndex`);
throw new RangeError(`the fromIndex cannot be less than or equal to toIndex`);
}
if (fromIndex >= this.elementNum || fromIndex < 0 || toIndex < 0) {
throw new Error(`fromIndex or toIndex is out-of-bounds`);
throw new RangeError(`the fromIndex or the toIndex is out-of-bounds`);
}
toIndex = toIndex > this.elementNum ? this.elementNum - 1 : toIndex;
toIndex = toIndex >= this.elementNum ? this.elementNum : toIndex;
let arraylist = new ArrayList<T>();
for (let i = fromIndex; i < toIndex; i++) {
arraylist.add(this[i]);
@@ -289,8 +294,8 @@ if (flag || fastArrayList == undefined) {
let arraylist = this;
return {
next: function () {
var done = count >= arraylist.elementNum;
var value = !done ? arraylist[count++] : undefined;
let done = count >= arraylist.elementNum;
let value = !done ? arraylist[count++] : undefined;
return {
done: done,
value: value,
+35 -19
View File
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2021 Huawei Device Co., Ltd.
* 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
@@ -22,16 +22,19 @@ if (arkPritvate !== undefined) {
}
if (flag || fastDeque == undefined) {
class HandlerDeque<T> {
private isOutBounds(obj: Deque<T>, prop: any) {
let index = Number.parseInt(prop);
if (Number.isInteger(index)) {
if (index < 0) {
throw new RangeError("the index is out-of-bounds");
}
}
}
get(obj: Deque<T>, prop: any): T {
if (typeof prop === "symbol") {
return obj[prop];
}
var index = Number.parseInt(prop);
if (Number.isInteger(index)) {
if (index < 0) {
throw new Error("Deque: get out-of-bounds");
}
}
this.isOutBounds(obj, prop);
return obj[prop];
}
set(obj: Deque<T>, prop: any, value: T): boolean {
@@ -39,10 +42,10 @@ if (flag || fastDeque == undefined) {
obj[prop] = value;
return true;
}
var index = Number(prop);
let index = Number(prop);
if (Number.isInteger(index)) {
if (index < 0) {
throw new Error("Deque: set out-of-bounds");
throw new RangeError("index is out-of-bounds");
} else {
obj[index] = value;
return true;
@@ -64,17 +67,15 @@ if (flag || fastDeque == undefined) {
return true;
}
getOwnPropertyDescriptor(obj: Deque<T>, prop: any) {
var index = Number.parseInt(prop);
if (Number.isInteger(index)) {
if (index < 0 || index >= obj.length) {
throw new Error("Deque: getOwnPropertyDescriptor out-of-bounds");
}
this.isOutBounds(obj, prop);
let index = Number.parseInt(prop);
if (index >= 0 && Number.isInteger(index)) {
return Object.getOwnPropertyDescriptor(obj, prop);
}
return;
return
}
setPrototypeOf(obj: any, prop: any): any {
throw new Error("Can setPrototype on Deque Object");
throw new RangeError("Can setPrototype on Deque Object");
}
}
interface IterableIterator<T> {
@@ -93,7 +94,7 @@ if (flag || fastDeque == undefined) {
this.rear = 0;
return new Proxy(this, new HandlerDeque());
}
get length(){
get length() {
let result = (this.rear - this.front + this.capacity) % this.capacity;
return result;
}
@@ -112,9 +113,15 @@ if (flag || fastDeque == undefined) {
this.rear = (this.rear + 1) % (this.capacity + 1);
}
getFirst(): T {
if (this.isEmpty()) {
return undefined;
}
return this[this.front];
}
getLast(): T {
if (this.isEmpty()) {
return undefined;
}
return this[this.rear - 1];
}
has(element: T): boolean {
@@ -127,11 +134,17 @@ if (flag || fastDeque == undefined) {
return result;
}
popFirst(): T {
if (this.isEmpty()) {
return undefined;
}
let result = this[this.front];
this.front = (this.front + 1) % (this.capacity + 1);
return result;
}
popLast(): T {
if (this.isEmpty()) {
return undefined;
}
let result = this[this.rear - 1];
this.rear = (this.rear + this.capacity) % (this.capacity + 1);
return result;
@@ -170,13 +183,16 @@ if (flag || fastDeque == undefined) {
private isFull(): boolean {
return (this.rear + 1) % this.capacity === this.front;
}
private isEmpty(): boolean {
return this.length == 0;
}
[Symbol.iterator](): IterableIterator<T> {
let deque = this;
let count = deque.front;
return {
next: function () {
var done = count == deque.rear;
var value = !done ? deque[count] : undefined;
let done = count == deque.rear;
let value = !done ? deque[count] : undefined;
count = (count + 1) % deque.capacity;
return {
done: done,
+1 -1
View File
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2021 Huawei Device Co., Ltd.
* 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
+12 -12
View File
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2021 Huawei Device Co., Ltd.
* 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
@@ -70,6 +70,9 @@ 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");
}
let memebers = map.keyValueArray;
for (let i = 0; i < memebers.length; i++) {
this.put(memebers[i].key, memebers[i].value);
@@ -80,9 +83,6 @@ if (flag || fastHashMap === undefined) {
}
remove(key: K): V {
let result = this.removeMember(key);
if (result === undefined) {
throw new Error("The removed element does not exist in this container");
}
return result;
}
clear(): void {
@@ -93,8 +93,8 @@ if (flag || fastHashMap === undefined) {
let count = 0;
return {
next: function () {
var done = count >= data.memberNumber;
var value = !done ? data.keyValueArray[count].key : undefined;
let done = count >= data.memberNumber;
let value = !done ? data.keyValueArray[count].key : undefined;
count++;
return {
done: done,
@@ -108,8 +108,8 @@ if (flag || fastHashMap === undefined) {
let count = 0;
return {
next: function () {
var done = count >= data.memberNumber;
var value = !done ? data.keyValueArray[count].value : undefined;
let done = count >= data.memberNumber;
let value = !done ? data.keyValueArray[count].value : undefined;
count++;
return {
done: done,
@@ -133,8 +133,8 @@ if (flag || fastHashMap === undefined) {
let count = 0;
return {
next: function () {
var done = count >= data.memberNumber;
var value = !done ? data.keyValueArray[count].entry() : undefined;
let done = count >= data.memberNumber;
let value = !done ? data.keyValueArray[count].entry() : undefined;
count++;
return {
done: done,
@@ -148,8 +148,8 @@ if (flag || fastHashMap === undefined) {
let count = 0;
return {
next: function () {
var done = count >= data.memberNumber;
var value = !done ? data.keyValueArray[count].entry() : undefined;
let done = count >= data.memberNumber;
let value = !done ? data.keyValueArray[count].entry() : undefined;
count++;
return {
done: done,
+1 -1
View File
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2021 Huawei Device Co., Ltd.
* 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
+7 -7
View File
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2021 Huawei Device Co., Ltd.
* 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
@@ -87,8 +87,8 @@ if (flag || fastHashSet === undefined) {
let count = 0;
return {
next: function () {
var done = count >= data.memberNumber;
var value = !done ? data.keyValueArray[count].key : undefined;
let done = count >= data.memberNumber;
let value = !done ? data.keyValueArray[count].key : undefined;
count++;
return {
done: done,
@@ -102,8 +102,8 @@ if (flag || fastHashSet === undefined) {
let count = 0;
return {
next: function () {
var done = count >= data.memberNumber;
var value = !done ? data.keyValueArray[count].entry() : undefined;
let done = count >= data.memberNumber;
let value = !done ? data.keyValueArray[count].entry() : undefined;
count++;
return {
done: done,
@@ -117,8 +117,8 @@ if (flag || fastHashSet === undefined) {
let count = 0;
return {
next: function () {
var done = count >= data.memberNumber;
var value = !done ? data.keyValueArray[count].key : undefined;
let done = count >= data.memberNumber;
let value = !done ? data.keyValueArray[count].key : undefined;
count++;
return {
done: done,
+1 -1
View File
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2021 Huawei Device Co., Ltd.
* 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
+21 -9
View File
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2021 Huawei Device Co., Ltd.
* 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
@@ -58,6 +58,9 @@ if (flag || fastLightWeightMap === undefined) {
return this.memberNumber;
}
hasAll(map: LightWeightMap<K, V>): boolean {
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()) ) {
return true;
@@ -71,6 +74,9 @@ 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");
}
super.ensureCapacity(minimumCapacity);
}
entries(): IterableIterator<[K, V]> {
@@ -78,8 +84,8 @@ if (flag || fastLightWeightMap === undefined) {
let count = 0;
return {
next: function () {
var done = count >= data.memberNumber;
var value = !done ? [data.members.keys[count], data.members.values[count]] as [K, V] : undefined;
let done = count >= data.memberNumber;
let value = !done ? [data.members.keys[count], data.members.values[count]] as [K, V] : undefined;
count++;
return {
done: done,
@@ -102,6 +108,9 @@ if (flag || fastLightWeightMap === undefined) {
return this.memberNumber === 0;
}
getKeyAt(index: number): K {
if (typeof index !== "number") {
throw new TypeError("the index is not integer");
}
return this.members.keys[index];
}
keys(): IterableIterator<K> {
@@ -109,8 +118,8 @@ if (flag || fastLightWeightMap === undefined) {
let count = 0;
return {
next: function () {
var done = count >= data.memberNumber;
var value = !done ? data.members.keys[count] : undefined;
let done = count >= data.memberNumber;
let value = !done ? data.members.keys[count] : undefined;
count++;
return {
done: done,
@@ -120,6 +129,9 @@ if (flag || fastLightWeightMap === undefined) {
};
}
setAll(map: LightWeightMap<K, V>): void {
if(!(map instanceof LightWeightMap)) {
throw new TypeError("Incoming object is not JSAPILightWeightMap");
}
if (this.memberNumber === 0) {
this.members.hashs = map.members.hashs.slice();
this.members.keys = map.members.keys.slice();
@@ -172,8 +184,8 @@ if (flag || fastLightWeightMap === undefined) {
let count = 0;
return {
next: function () {
var done = count >= data.memberNumber;
var value = !done ? [data.members.keys[count], data.members.values[count]] as [K, V] : undefined;
let done = count >= data.memberNumber;
let value = !done ? [data.members.keys[count], data.members.values[count]] as [K, V] : undefined;
count++;
return {
done: done,
@@ -197,8 +209,8 @@ if (flag || fastLightWeightMap === undefined) {
let count = 0;
return {
next: function () {
var done = count >= data.memberNumber;
var value = !done ? data.members.values[count] : undefined;
let done = count >= data.memberNumber;
let value = !done ? data.members.values[count] : undefined;
count++;
return {
done: done,
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2021 Huawei Device Co., Ltd.
* 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
+11 -6
View File
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2021 Huawei Device Co., Ltd.
* 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
@@ -63,6 +63,9 @@ if (flag || fastLightWeightSet === undefined) {
return true;
}
addAll(set: LightWeightSet<T>): boolean {
if(!(set instanceof LightWeightSet)) {
throw new TypeError("Incoming object is not JSAPILightWeightSet");
}
let change = false;
if (set.memberNumber == 0) {
change = false;
@@ -85,6 +88,8 @@ if (flag || fastLightWeightSet === undefined) {
}
equal(obj: Object): boolean {
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;
return false;
}
@@ -129,8 +134,8 @@ if (flag || fastLightWeightSet === undefined) {
let count = 0;
return {
next: function () {
var done = count >= data.memberNumber;
var value = !done ? data.members.keys[count] : undefined;
let done = count >= data.memberNumber;
let value = !done ? data.members.keys[count] : undefined;
count++;
return {
done: done,
@@ -156,9 +161,9 @@ if (flag || fastLightWeightSet === undefined) {
let count = 0;
return {
next: function () {
var done = count >= data.memberNumber;
var tempValue = data.members.keys[count];
var value = !done ? ([tempValue, tempValue] as [T, T]) : undefined;
let done = count >= data.memberNumber;
let tempValue = data.members.keys[count];
let value = !done ? ([tempValue, tempValue] as [T, T]) : undefined;
count++;
return {
done: done,
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2021 Huawei Device Co., Ltd.
* 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
+145 -156
View File
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2021 Huawei Device Co., Ltd.
* 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
@@ -26,11 +26,11 @@ if (flag || fastLinkedList == undefined) {
if (typeof prop === "symbol") {
return obj[prop];
}
var index = Number.parseInt(prop);
let index = Number.parseInt(prop);
let length = obj.length;
if (Number.isInteger(index)) {
if (index < 0 || index >= length) {
throw new Error("LinkedList: get out-of-bounds");
throw new RangeError("the index is out-of-bounds");
}
return obj.get(index);
}
@@ -38,18 +38,18 @@ if (flag || fastLinkedList == undefined) {
}
set(obj: LinkedList<T>, prop: any, value: any) {
if (prop === "elementNum" ||
prop === "capacity" ||
prop === "head" ||
prop == "next" ||
prop == "tail" ) {
prop === "capacity" ||
prop === "head" ||
prop == "next" ||
prop == "tail") {
obj[prop] = value;
return true;
}
var index = Number.parseInt(prop);
let index = Number.parseInt(prop);
if (Number.isInteger(index)) {
let length = obj.length;
if (index < 0 || index >= length) {
throw new Error("LinkedList: set out-of-bounds");
throw new RangeError("the index is out-of-bounds");
} else {
obj.set(index, value);
return true;
@@ -58,11 +58,11 @@ if (flag || fastLinkedList == undefined) {
return false;
}
deleteProperty(obj: LinkedList<T>, prop: any) {
var index = Number.parseInt(prop);
let index = Number.parseInt(prop);
if (Number.isInteger(index)) {
let length = obj.length;
if (index < 0 || index >= length) {
throw new Error("LinkedList: deleteProperty out-of-bounds");
throw new RangeError("the index is out-of-bounds");
}
obj.removeByIndex(index);
return true;
@@ -73,9 +73,9 @@ if (flag || fastLinkedList == undefined) {
return obj.has(prop);
}
ownKeys(obj: LinkedList<T>) {
var keys = [];
let keys = [];
let length = obj.length;
for (var i = 0; i < length; i++) {
for (let i = 0; i < length; i++) {
keys.push(i.toString());
}
return keys;
@@ -84,18 +84,18 @@ if (flag || fastLinkedList == undefined) {
return true;
}
getOwnPropertyDescriptor(obj: LinkedList<T>, prop: any) {
var index = Number.parseInt(prop);
let index = Number.parseInt(prop);
if (Number.isInteger(index)) {
let length = obj.length;
if (index < 0 || index >= length) {
throw new Error("LinkedList: getOwnPropertyDescriptor 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");
throw new Error("Can setPrototype on LinkedList Object");
}
}
interface IterableIterator<T> {
@@ -105,52 +105,27 @@ if (flag || fastLinkedList == undefined) {
};
}
class NodeObj<T> {
/* If 'any' is changed to 'T' here, an error will be reported:
Type 'unknown' cannot be assigned to type 'T'. */
element: any;
next?: NodeObj<T> | null;
prev?: NodeObj<T> | null;
constructor(
element: any,
next?: NodeObj<T> | null,
prev?: NodeObj<T> | null
) {
element: T;
next?: NodeObj<T>;
prev?: NodeObj<T>;
constructor(element: T, next?: NodeObj<T>, prev?: NodeObj<T>) {
this.element = element;
this.next = next;
this.prev = prev;
}
}
class LinkIterator<T> {
/* If 'any' is changed to 'T' here, an error will be reported:
Property 'next' does not exist on type 'T' */
private linkNode: any;
constructor(linkNode: any) {
this.linkNode = linkNode;
}
hasNext(): boolean {
if (this.linkNode.next !== null) {
return true;
} else {
return false;
}
}
next(): NodeObj<T> {
this.linkNode = this.linkNode.next;
return this.linkNode;
}
prev(): NodeObj<T> {
this.linkNode = this.linkNode.prev;
return this.linkNode;
}
}
class LinkedList<T> {
private head?: any;
private tail?: any;
private elementNum : number;
private head?: NodeObj<T>;
private tail?: NodeObj<T>;
private elementNum: number;
private capacity: number;
constructor(head?: NodeObj<T>, tail?: NodeObj<T>) {
this.head = head || null;
this.tail = tail || null;
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());
@@ -158,37 +133,44 @@ if (flag || fastLinkedList == undefined) {
get length() {
return this.elementNum;
}
private getNode(index: number): NodeObj<T> {
let current = this.head;
for (let i = 0; i < index; i++) {
current = current["next"];
private getNode(index: number): NodeObj<T> | undefined {
if (index >= 0 && index < this.elementNum) {
let current = this.head;
for (let i = 0; i < index; i++) {
if (current !== undefined) {
current = current.next;
}
}
return current;
}
return current;
return undefined;
}
get(index: number): T {
let current = this.head;
for (let i = 0; i < index; i++) {
current = current["next"];
if (index >= 0 && index < this.elementNum) {
let current = this.head;
for (let i = 0; i < index && current != undefined; i++) {
current = current.next;
}
return current.element;
}
return current.element;
return undefined;
}
add(element: T): boolean {
if (this.elementNum === 0) {
let head = this.head;
let tail = this.tail;
this.head = this.tail = new NodeObj(element, head, tail);
let node = new NodeObj(element);
if (this.head == undefined) {
this.head = this.tail = node;
} else {
let prevNode = this.getNode(this.elementNum - 1);
prevNode.next = new NodeObj(element, prevNode["next"], this.tail);
let current = this.head;
while (current.next !== undefined) {
current = current.next;
}
this.tail = current.next = node;
}
this.elementNum++;
return true;
}
addFirst(element: T): void {
if (!element) {
throw new Error("element cannot be null");
}
let node = new NodeObj(element, this.head, this.tail);
let node = new NodeObj(element);
if (this.elementNum === 0) {
this.head = this.tail = node;
} else {
@@ -198,47 +180,45 @@ if (flag || fastLinkedList == undefined) {
this.elementNum++;
}
removeFirst(): T {
let result = this.getNode(0).element;
this.removeByIndex(0);
return result;
}
popFirst(): T {
let result = this.getNode(0).element;
this.removeByIndex(0);
return result;
}
popLast(): T {
let result = this.getNode(this.elementNum - 1).element;
this.removeByIndex(this.elementNum - 1);
return result;
if (this.head !== undefined) {
let result = this.head.element;
this.removeByIndex(0);
return result;
}
return undefined;
}
removeLast(): T {
let result = this.getNode(this.elementNum - 1).element;
this.removeByIndex(this.elementNum - 1);
return result;
if (this.tail !== undefined) {
let result = this.tail.element;
this.removeByIndex(this.elementNum - 1);
return result;
}
return undefined;
}
clear(): void {
this.head = null;
this.tail = null;
this.head = undefined;
this.tail = undefined;
this.elementNum = 0;
}
has(element: T): boolean {
if (this.head.element === element) {
return true;
}
const linkIterator = new LinkIterator<T>(this.head);
while (linkIterator.hasNext()) {
const newNode = linkIterator.next();
if (newNode.element === element) {
if (this.head !== undefined) {
if (this.head.element === element) {
return true;
}
let current = this.head;
while (current.next != undefined) {
current = current.next;
if (current.element === element) {
return true;
}
}
}
return false;
}
getIndexOf(element: T): number {
for (let i = 0; i < this.elementNum; i++) {
const curNode = this.getNode(i);
if (curNode.element === element) {
if (curNode !== undefined && curNode.element === element) {
return i;
}
}
@@ -247,39 +227,45 @@ if (flag || fastLinkedList == undefined) {
getLastIndexOf(element: T): number {
for (let i = this.elementNum - 1; i >= 0; i--) {
const curNode = this.getNode(i);
if (curNode.element === element) {
if (curNode !== undefined && curNode.element === element) {
return i;
}
}
return -1;
}
removeByIndex(index: number): T {
if (index < 0 || index >= this.elementNum) {
throw new Error("removeByIndex is out-of-bounds");
}
let current = this.head;
if (index === 0) {
this.head = current && current.next;
if (this.elementNum == 1) {
this.head = this.tail = null;
if (index >= 0 && index < this.elementNum) {
let current = this.head;
if (index === 0 && current !== undefined) {
this.head = current.next;
this.head.prev = undefined;
if (this.elementNum == 1) {
this.head = this.tail = undefined;
}
} else if (index == this.elementNum - 1) {
current = this.getNode(index - 1);
if (current !== undefined) {
this.tail = current;
current.next = undefined;
}
} else {
this.head.prev = null;
const prevNode = this.getNode(index - 1);
const nextNode = this.getNode(index + 1);
if (prevNode !== undefined && nextNode !== undefined) {
prevNode.next = nextNode;
nextNode.prev = prevNode;
}
}
if (current !== undefined) {
this.elementNum--;
return current.element;
}
} else if (index == this.elementNum - 1) {
current = this.getNode(index - 1);
this.tail = current;
current.next = null;
} else {
const prevNode = this.getNode(index - 1);
const nextNode = this.getNode(index + 1);
prevNode.next = nextNode;
nextNode.prev = prevNode;
throw new RangeError("the index is out-of-bounds");
}
this.elementNum--;
return current && current.element;
}
remove(element: T): boolean {
if(this.isEmpty()){
if (this.isEmpty()) {
return false;
}
if (this.has(element)) {
@@ -294,42 +280,44 @@ if (flag || fastLinkedList == undefined) {
let index = this.getIndexOf(element);
this.removeByIndex(index);
return true;
} else {
return false;
}
return false;
}
removeLastFound(element: T): boolean {
if (this.has(element)) {
let index = this.getLastIndexOf(element);
this.removeByIndex(index);
return true;
} else {
return false;
}
return false;
}
getFirst(): T {
let newNode = this.getNode(0);
let element = newNode.element;
return element;
if (this.head !== undefined) {
return this.head.element;
}
return undefined;
}
getLast(): T {
let newNode = this.getNode(this.elementNum - 1);
let element = newNode.element;
return element;
if (this.tail !== undefined) {
return this.tail.element;
}
return undefined;
}
insert(index: number, element: T): void {
if (index >= 0 && index < this.elementNum) {
if (index >= 0 && index <= this.elementNum) {
let newNode = new NodeObj(element);
let current = this.head;
if (index === 0) {
if (this.head === null) {
this.head = newNode;
this.tail = newNode;
if (this.head === undefined) {
this.head = this.tail = newNode;
} else {
newNode.next = this.head;
this.head.prev = newNode;
this.head = newNode;
}
} else if (index === this.elementNum && this.elementNum !== 0) {
let prevNode = this.getNode(this.elementNum - 1);
prevNode.next = this.tail = newNode;
} else {
const prevNode = this.getNode(index - 1);
current = prevNode.next;
@@ -338,18 +326,15 @@ if (flag || fastLinkedList == undefined) {
current.prev = newNode;
newNode.prev = prevNode;
}
} else if (index === this.elementNum) {
let prevNode = this.getNode(this.elementNum - 1);
prevNode.next = new NodeObj(element, prevNode["next"], this.tail);
} else {
throw new Error("index cannot Less than 0 and more than this length");
throw new RangeError("the index is out-of-bounds");
}
this.elementNum++;
}
set(index: number, element: T): T {
const current = this.getNode(index);
current.element = element;
return current && current.element;
return current.element;
}
convertToArray(): Array<T> {
let arr: Array<T> = [];
@@ -357,11 +342,13 @@ if (flag || fastLinkedList == undefined) {
if (this.elementNum <= 0) {
return arr;
}
arr[index] = this.head.element;
const linkIterator = new LinkIterator<T>(this.head);
while (linkIterator.hasNext()) {
const newNode = linkIterator.next();
arr[++index] = newNode.element;
if (this.head !== undefined) {
let current = this.head;
arr[index] = this.head.element;
while (current.next != undefined) {
current = current.next;
arr[++index] = current.element;
}
}
return arr;
}
@@ -369,7 +356,7 @@ if (flag || fastLinkedList == undefined) {
let clone = new LinkedList<T>();
let arr = this.convertToArray();
for (let i = 0; i < arr.length; i++) {
let item = arr[i]
let item = arr[i];
clone.add(item);
}
return clone;
@@ -377,16 +364,18 @@ if (flag || fastLinkedList == undefined) {
private isEmpty(): boolean {
return this.elementNum == 0;
}
forEach(callbackfn: (value: T,index?: number,linkedlist?: LinkedList<T>) => void,
forEach(callbackfn: (value: T, index?: number, linkedList?: LinkedList<T>) => void,
thisArg?: Object): void {
let index = 0;
const linkIterator = new LinkIterator<T>(this.head);
if (this.elementNum > 0) {
callbackfn.call(thisArg, this.head.element, index, this);
}
while (linkIterator.hasNext()) {
const newNode = linkIterator.next();
callbackfn.call(thisArg, newNode.element, ++index, this);
if (this.head !== undefined) {
let current = this.head;
if (this.elementNum > 0) {
callbackfn.call(thisArg, this.head.element, index, this);
}
while (current.next != undefined) {
current = current.next;
callbackfn.call(thisArg, current.element, ++index, this);
}
}
}
[Symbol.iterator](): IterableIterator<T> {
@@ -394,8 +383,8 @@ if (flag || fastLinkedList == undefined) {
let linkedlist = this;
return {
next: function () {
var done = count >= linkedlist.elementNum;
var value = !done ? linkedlist.getNode(count++).element : undefined;
let done = count >= linkedlist.elementNum;
let value = !done ? linkedlist.getNode(count++).element : undefined;
return {
done: done,
value: value,
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2021 Huawei Device Co., Ltd.
* 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
+124 -108
View File
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2021 Huawei Device Co., Ltd.
* 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
@@ -26,28 +26,27 @@ if (flag || fastList == undefined) {
if (typeof prop === "symbol") {
return obj[prop];
}
var index = Number.parseInt(prop);
let index = Number.parseInt(prop);
if (Number.isInteger(index)) {
if (index < 0 || index >= obj.length) {
throw new Error("List: get 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" ||
if (prop === "elementNum" ||
prop === "capacity" ||
prop === "head" ||
prop == "next") {
obj[prop] = value;
return true;
}
var index = Number.parseInt(prop);
let index = Number.parseInt(prop);
if (Number.isInteger(index)) {
if (index < 0 || index >= obj.length) {
throw new Error("List: set out-of-bounds");
throw new RangeError("the index is out-of-bounds");
} else {
obj.set(index, value);
return true;
@@ -56,10 +55,10 @@ if (flag || fastList == undefined) {
return false;
}
deleteProperty(obj: List<T>, prop: any) {
var index = Number.parseInt(prop);
let index = Number.parseInt(prop);
if (Number.isInteger(index)) {
if (index < 0 || index >= obj.length) {
throw new Error("List: deleteProperty out-of-bounds");
throw new RangeError("the index is out-of-bounds");
}
obj.removeByIndex(index);
return true;
@@ -70,8 +69,8 @@ if (flag || fastList == undefined) {
return obj.has(prop);
}
ownKeys(obj: List<T>) {
var keys = [];
for (var i = 0; i < obj.length; i++) {
let keys = [];
for (let i = 0; i < obj.length; i++) {
keys.push(i.toString());
}
return keys;
@@ -80,17 +79,17 @@ if (flag || fastList == undefined) {
return true;
}
getOwnPropertyDescriptor(obj: List<T>, prop: any) {
var index = Number.parseInt(prop);
let index = Number.parseInt(prop);
if (Number.isInteger(index)) {
if (index < 0 || index >= obj.length) {
throw new Error("List: getOwnPropertyDescriptor 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");
throw new Error("Can setPrototype on List Object");
}
}
interface IterableIterator<T> {
@@ -100,41 +99,21 @@ if (flag || fastList == undefined) {
};
}
class NodeObj<T> {
/* If 'any' is changed to 'T' here, an error will be reported:
Type 'unknown' cannot be assigned to type 'T'. */
element: any;
next?: NodeObj<T> | null;
constructor(element: any, next?: NodeObj<T> | null) {
element: T;
next?: NodeObj<T>;
constructor(element: T, next?: NodeObj<T>) {
this.element = element;
this.next = next;
}
}
class LinkIterator<T> {
/* If 'any' is changed to 'T' here, an error will be reported:
Property 'next' does not exist on type 'T' */
private linkNode: any;
constructor(linkNode: any) {
this.linkNode = linkNode;
}
hasNext(): boolean {
if (this.linkNode.next !== null) {
return true;
}
return false;
}
next(): NodeObj<T> {
this.linkNode = this.linkNode.next;
return this.linkNode;
}
}
class List<T> {
/* If 'any' is changed to 'NodeObj<T> | null ' here, an error will be reported:
Object may be 'null' */
private head: any;
private head: NodeObj<T>;
private elementNum: number;
private capacity: number;
constructor(head?: NodeObj<T>) {
this.head = head || null;
private next?: NodeObj<T>;
constructor() {
this.head = undefined;
this.next = undefined;
this.elementNum = 0;
this.capacity = 10;
return new Proxy(this, new HandlerList());
@@ -142,45 +121,58 @@ if (flag || fastList == undefined) {
get length() {
return this.elementNum;
}
private getNode(index: number): NodeObj<T> {
let current = this.head;
for (let i = 0; i < index; i++) {
current = current["next"];
private getNode(index: number): NodeObj<T> | undefined {
if (index >= 0 && index < this.elementNum) {
let current = this.head;
for (let i = 0; i < index; i++) {
if (current !== undefined) {
current = current.next;
}
}
return current;
}
return current;
return undefined;
}
get(index: number): T {
let current = this.head;
for (let i = 0; i < index; i++) {
current = current["next"];
if (index >= 0 && index < this.elementNum) {
let current = this.head;
for (let i = 0; i < index && current != undefined; i++) {
current = current.next;
}
return current.element;
}
return current.element;
return undefined;
}
add(element: T): boolean {
if (this.elementNum === 0) {
let head = this.head;
this.head = new NodeObj(element, head);
let node = new NodeObj(element);
if (this.head == undefined) {
this.head = node;
} else {
let prevNode = this.getNode(this.elementNum - 1);
prevNode.next = new NodeObj(element, prevNode["next"]);
let current = this.head;
while (current.next !== undefined) {
current = current.next;
}
current.next = node;
}
this.elementNum++;
return true;
}
clear(): void {
this.head = null;
this.head = undefined;
this.elementNum = 0;
}
has(element: T): boolean {
if (this.head.element === element) {
return true;
}
const linkIterator = new LinkIterator<T>(this.head);
while (linkIterator.hasNext()) {
const newNode = linkIterator.next();
if (newNode.element === element) {
if (this.head !== undefined) {
if (this.head.element === element) {
return true;
}
let current = this.head;
while (current.next != undefined) {
current = current.next;
if (current.element === element) {
return true;
}
}
}
return false;
}
@@ -191,23 +183,30 @@ if (flag || fastList == undefined) {
if (!(obj instanceof List)) {
return false;
} else {
let e1 = new LinkIterator<T>(this.head);
let e2 = new LinkIterator<T>(obj.head);
while (e1.hasNext() && e2.hasNext()) {
const newNode1 = e1.next();
const newNode2 = e2.next();
if (newNode1.element !== newNode2.element) {
return false;
let e1 = this.head;
let e2 = obj.head;
if (e1 !== undefined && e2 !== undefined) {
while (e1.next !== undefined && e2.next !== undefined) {
e1 = e1.next;
e2 = e2.next;
if (e1.element !== e2.element) {
return false;
}
}
return !(e1.next !== undefined || e2.next !== undefined);
} else if (e1 !== undefined && e2 === undefined) {
return false;
} else if (e1 === undefined && e2 !== undefined) {
return false;
} else {
return true;
}
return !(e1.hasNext() || e2.hasNext());
}
return true;
}
getIndexOf(element: T): number {
for (let i = 0; i < this.elementNum; i++) {
const curNode = this.getNode(i);
if (curNode.element === element) {
if (curNode !== undefined && curNode.element === element) {
return i;
}
}
@@ -216,7 +215,7 @@ if (flag || fastList == undefined) {
getLastIndexOf(element: T): number {
for (let i = this.elementNum - 1; i >= 0; i--) {
const curNode = this.getNode(i);
if (curNode.element === element) {
if (curNode !== undefined && curNode.element === element) {
return i;
}
}
@@ -224,7 +223,7 @@ if (flag || fastList == undefined) {
}
removeByIndex(index: number): T {
if (index < 0 || index >= this.elementNum) {
throw new Error("removeByIndex is out-of-bounds");
throw new RangeError("the index is out-of-bounds");
}
let oldNode = this.head;
if (index === 0) {
@@ -249,27 +248,36 @@ if (flag || fastList == undefined) {
replaceAllElements(callbackfn: (value: T, index?: number, list?: List<T>) => T,
thisArg?: Object): void {
let index = 0;
const linkIterator = new LinkIterator<T>(this.head);
if (this.elementNum > 0) {
const linkIterator = new LinkIterator<T>(this.head);
this.getNode(index).element = callbackfn.call(thisArg, this.head.element, index, this);
}
while (linkIterator.hasNext()) {
const newNode = linkIterator.next();
this.getNode(++index).element = callbackfn.call(thisArg, newNode.element, index, this);
if (this.head !== undefined) {
let current = this.head;
if (this.elementNum > 0) {
this.getNode(index).element = callbackfn.call(thisArg, this.head.element, index, this);
}
while (current.next != undefined) {
current = current.next;
this.getNode(++index).element = callbackfn.call(thisArg, current.element, index, this);
}
}
}
getFirst(): T {
let newNode = this.getNode(0);
let element = newNode.element;
if (this.isEmpty()) {
return undefined;
}
let element = this.head.element;
return element;
}
getLast(): T {
if (this.isEmpty()) {
return undefined;
}
let newNode = this.getNode(this.elementNum - 1);
let element = 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");
}
let newNode = new NodeObj(element);
if (index >= 0 && index < this.elementNum) {
if (index === 0) {
@@ -285,15 +293,20 @@ 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");
}
const current = this.getNode(index);
current.element = element;
return current && current.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++) {
if (comparator(this.getNode(j).element, this.getNode(j + 1).element) > 0) {
if (
comparator(this.getNode(j).element, this.getNode(j + 1).element) > 0
) {
isSort = false;
let temp = this.getNode(j).element;
this.getNode(j).element = this.getNode(j + 1).element;
@@ -307,17 +320,17 @@ if (flag || fastList == undefined) {
}
getSubList(fromIndex: number, toIndex: number): List<T> {
if (toIndex <= fromIndex) {
throw new Error("toIndex cannot be less than or equal to fromIndex");
throw new RangeError(`the toIndex cannot be less than or equal to fromIndex`);
}
if (fromIndex >= this.elementNum || fromIndex < 0 || toIndex < 0) {
throw new Error(`fromIndex or toIndex is out-of-bounds`);
throw new RangeError(`the fromIndex or the toIndex is out-of-bounds`);
}
toIndex = toIndex > this.elementNum ? this.elementNum - 1 : toIndex;
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;
list.add(element);
if (element === null) {
if (element === undefined) {
break;
}
}
@@ -329,11 +342,13 @@ if (flag || fastList == undefined) {
if (this.elementNum <= 0) {
return arr;
}
arr[index] = this.head.element;
const linkIterator = new LinkIterator<T>(this.head);
while (linkIterator.hasNext()) {
const newNode = linkIterator.next();
arr[++index] = newNode.element;
if (this.head !== undefined) {
let current = this.head;
arr[index] = this.head.element;
while (current.next != undefined) {
current = current.next;
arr[++index] = current.element;
}
}
return arr;
}
@@ -343,14 +358,15 @@ if (flag || fastList == undefined) {
forEach(callbackfn: (value: T, index?: number, list?: List<T>) => void,
thisArg?: Object): void {
let index = 0;
const linkIterator = new LinkIterator<T>(this.head);
if (this.elementNum > 0) {
const linkIterator = new LinkIterator<T>(this.head);
callbackfn.call(thisArg, this.head.element, index, this);
}
while (linkIterator.hasNext()) {
const newNode = linkIterator.next();
callbackfn.call(thisArg, newNode.element, ++index, this);
if (this.head !== undefined) {
let current = this.head;
if (this.elementNum > 0) {
callbackfn.call(thisArg, this.head.element, index, this);
}
while (current.next != undefined) {
current = current.next;
callbackfn.call(thisArg, current.element, ++index, this);
}
}
}
[Symbol.iterator](): IterableIterator<T> {
@@ -358,8 +374,8 @@ if (flag || fastList == undefined) {
let list = this;
return {
next: function () {
var done = count >= list.elementNum;
var value = !done ? list.getNode(count++).element : undefined;
let done = count >= list.elementNum;
let value = !done ? list.getNode(count++).element : undefined;
return {
done: done,
value: value,
@@ -371,4 +387,4 @@ if (flag || fastList == undefined) {
Object.freeze(List);
fastList = List;
}
export default fastList;
export default fastList;
+1 -1
View File
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2021 Huawei Device Co., Ltd.
* 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
+37 -8
View File
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2021 Huawei Device Co., Ltd.
* 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
@@ -59,7 +59,7 @@ if (flag || fastPlainArray === undefined) {
}
add(key: number, value: T): void {
if (typeof key !== "number") {
throw new Error("PlainArray's only number is allowed");
throw new TypeError("the index is not integer");
}
this.addmember(key, value);
}
@@ -78,13 +78,22 @@ if (flag || fastPlainArray === undefined) {
return clone;
}
has(key: number): boolean {
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");
}
let 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");
}
let result = this.binarySearchAtPlain(key);
return result < 0 ? -1 : result;
}
@@ -95,28 +104,45 @@ if (flag || fastPlainArray === undefined) {
return this.memberNumber === 0;
}
getKeyAt(index: number): number {
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");
}
let index = this.binarySearchAtPlain(key);
if (index < 0) throw new Error("element not in this plainarray");
if (index < 0) return result;
return this.deletemember(index);
}
removeAt(index: number): T {
if (index >= this.memberNumber || index < 0) throw new Error("index not in this plainarray range");
if (typeof index !== "number") {
throw new TypeError("the index is not integer");
}
let result: any = undefined;
if (index >= this.memberNumber || index < 0) return result;
return this.deletemember(index);
}
removeRangeFrom(index: number, size: number): number {
if (index >= this.memberNumber || index < 0) throw new Error("index not in this plainarray range");
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;
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 (index >= 0 && index < this.memberNumber) {
this.members.values[index] = value;
} else {
throw new Error("index Out Of Bounds");
throw new RangeError("the index is out-of-bounds");
}
}
toString(): string {
@@ -127,6 +153,9 @@ if (flag || fastPlainArray === undefined) {
return result.join(",");
}
getValueAt(index: number): T {
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,
@@ -140,8 +169,8 @@ if (flag || fastPlainArray === undefined) {
let count = 0;
return {
next: function () {
var done = count >= data.memberNumber;
var value = !done ? [data.members.keys[count], data.members.values[count]] as [number, T] : undefined;
let done = count >= data.memberNumber;
let value = !done ? [data.members.keys[count], data.members.values[count]] as [number, T] : undefined;
count++;
return {
done: done,
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2021 Huawei Device Co., Ltd.
* 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
+42 -30
View File
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2021 Huawei Device Co., Ltd.
* 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
@@ -22,16 +22,20 @@ if (arkPritvate !== undefined) {
}
if (flag || fastQueue == undefined) {
class HandlerQueue<T> {
private isOutBounds(obj: Queue<T>, prop: any) {
let index = 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");
}
}
}
get(obj: Queue<T>, prop: any): T {
if (typeof prop === "symbol") {
return obj[prop];
}
var index = Number.parseInt(prop);
if (Number.isInteger(index)) {
if (index < 0 || index > obj.length) {
throw new Error("Queue: get out-of-bounds");
}
}
this.isOutBounds(obj, prop);
return obj[prop];
}
set(obj: Queue<T>, prop: any, value: T): boolean {
@@ -39,20 +43,17 @@ if (flag || fastQueue == undefined) {
obj[prop] = value;
return true;
}
var index = Number(prop);
if (Number.isInteger(index)) {
if (index < 0 || index > obj.length) {
throw new Error("Queue: set out-of-bounds");
} else {
this.isOutBounds(obj, prop);
let index = Number(prop);
if (index >= 0 && index <= obj.length && Number.isInteger(index)) {
obj[index] = value;
return true;
}
}
return false;
}
ownKeys(obj: Queue<T>) {
var keys = [];
for (var i = 0; i < obj.length; i++) {
let keys = [];
for (let i = 0; i < obj.length; i++) {
keys.push(i.toString());
}
return keys;
@@ -61,17 +62,15 @@ if (flag || fastQueue == undefined) {
return true;
}
getOwnPropertyDescriptor(obj: Queue<T>, prop: any) {
var index = Number.parseInt(prop);
if (Number.isInteger(index)) {
if (index < 0 || index >= obj.length) {
throw new Error("Queue: getOwnPropertyDescriptor out-of-bounds");
}
this.isOutBounds(obj, prop);
let index = Number.parseInt(prop);
if (index >= 0 && index < obj.length && Number.isInteger(index)) {
return Object.getOwnPropertyDescriptor(obj, prop);
}
return;
return
}
setPrototypeOf(obj: any, prop: any): any {
throw new Error("Can setPrototype on Queue Object");
throw new RangeError("Can setPrototype on Queue Object");
}
}
interface IterableIterator<T> {
@@ -102,9 +101,15 @@ if (flag || fastQueue == undefined) {
return true;
}
getFirst(): T {
if (this.isEmpty()) {
return undefined;
}
return this[this.front];
}
pop(): T {
if (this.isEmpty()) {
return undefined;
}
let result = this[this.front];
this.front = (this.front + 1) % (this.capacity + 1);
return result;
@@ -113,25 +118,32 @@ if (flag || fastQueue == undefined) {
thisArg?: Object): void {
let k = 0;
let i = this.front;
while (true) {
callbackfn.call(thisArg,this[i], k,this);
i = (i + 1) % this.capacity;
k++;
if (i === this.rear) {
break;
if (this.isEmpty()) {
return;
} else {
while (true) {
callbackfn.call(thisArg,this[i], k,this);
i = (i + 1) % this.capacity;
k++;
if (i === this.rear) {
break;
}
}
}
}
private isFull(): boolean {
return this.length === this.capacity;
}
private isEmpty(): boolean {
return this.length == 0;
}
[Symbol.iterator](): IterableIterator<T> {
let count = this.front;
let queue = this;
return {
next: function () {
var done = count == queue.rear;
var value = !done ? queue[count] : undefined;
let done = count == queue.rear;
let value = !done ? queue[count] : undefined;
count = (count + 1) % queue.capacity;
return {
done: done,
+1 -1
View File
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2021 Huawei Device Co., Ltd.
* 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
+30 -29
View File
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2021 Huawei Device Co., Ltd.
* 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
@@ -22,17 +22,19 @@ if (arkPritvate !== undefined) {
}
if (flag || fastStack == undefined) {
class HandlerStack<T> {
private isOutBounds(obj: Stack<T>, prop: any) {
let index = 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) {
if (typeof prop === "symbol") {
return obj[prop];
}
var index = Number.parseInt(prop);
if (Number.isInteger(index)) {
let length = obj.length;
if (index < 0 || index >= length) {
throw new Error("Stack: get out-of-bounds");
}
}
this.isOutBounds(obj, prop);
return obj[prop];
}
set(obj: Stack<T>, prop: any, value: T) {
@@ -40,22 +42,18 @@ if (flag || fastStack == undefined) {
obj[prop] = value;
return true;
}
var index = Number.parseInt(prop);
if (Number.isInteger(index)) {
let length = obj.length;
if (index < 0 || index >= length) {
throw new Error("Stack: set out-of-bounds");
} else {
obj[index] = value;
return true;
}
this.isOutBounds(obj, prop);
let index = Number.parseInt(prop);
if (index >= 0 && index < obj.length && Number.isInteger(index)) {
obj[index] = value;
return true;
}
return false;
}
ownKeys(obj: Stack<T>) {
var keys = [];
let keys = [];
let length = obj.length;
for (var i = 0; i < length; i++) {
for (let i = 0; i < length; i++) {
keys.push(i.toString());
}
return keys;
@@ -64,18 +62,15 @@ if (flag || fastStack == undefined) {
return true;
}
getOwnPropertyDescriptor(obj: Stack<T>, prop: any) {
var index = Number.parseInt(prop);
if (Number.isInteger(index)) {
let length = obj.length;
if (index < 0 || index >= length) {
throw new Error("Stack: getOwnPropertyDescriptor out-of-bounds");
}
this.isOutBounds(obj, prop);
let index = Number.parseInt(prop);
if (index >= 0 && index < obj.length && Number.isInteger(index)) {
return Object.getOwnPropertyDescriptor(obj, prop);
}
return;
return
}
setPrototypeOf(obj: any, prop: any): any {
throw new Error("Can setPrototype on Stack Object");
throw new RangeError("Can setPrototype on Stack Object");
}
}
interface IterableIterator<T> {
@@ -101,11 +96,17 @@ if (flag || fastStack == undefined) {
return item;
}
pop(): T {
if (this.isEmpty()) {
return undefined;
}
let 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 {
@@ -136,8 +137,8 @@ if (flag || fastStack == undefined) {
let stack = this;
return {
next: function () {
var done = count >= stack.elementNum;
var value = !done ? stack[count++] : undefined;
let done = count >= stack.elementNum;
let value = !done ? stack[count++] : undefined;
return {
done: done,
value: value,
+1 -1
View File
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2021 Huawei Device Co., Ltd.
* 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
+39 -40
View File
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2021 Huawei Device Co., Ltd.
* 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
@@ -12,6 +12,8 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
type CompFun<T> = (firstValue: T, secondValue: T) => boolean;
function hashCode(element: any): number {
let str = String(element);
let hash = 0;
@@ -56,7 +58,7 @@ function compareToString(string1: String, string2: String) {
throw new Error("this function run error");
}
function currencyCompare(a: any, b: any, compareFn?: Function): number {
function currencyCompare(a: any, b: any, compareFn?: CompFun<any>): number {
if (a === b) return ComparResult.EQUALS;
if (a instanceof Pair && b instanceof Pair) {
return currencyCompare(a.key, b.key, compareFn);
@@ -198,14 +200,15 @@ class LightWeightClass<K, V> {
protected getIndexByKey(key: K): number {
let hash = hashCode(key);
let 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)
throw new Error("don't find the key in lightweight");
if (index < 0) return result;
this.memberNumber--;
this.members.hashs.splice(index, 1);
this.members.keys.splice(index, 1);
@@ -279,8 +282,8 @@ class RBTreeClass<K, V> {
public memberNumber: number;
private isChange: boolean;
private treeNodeArray: Array<RBTreeNode<K, V>>;
private compFun: Function | undefined;
constructor(comparator?: (firstValue: K, secondValue: K) => boolean, root?: RBTreeNode<K, V>) {
private compFun: CompFun<K> | undefined;
constructor(comparator?: CompFun<K>, root?: RBTreeNode<K, V>) {
this.root = root;
this.compFun = comparator;
this.memberNumber = 0;
@@ -751,39 +754,36 @@ class DictionaryClass<K, V> {
}
protected put(key: K, value: V = key as unknown as V): boolean {
if (key != undefined && value != undefined) {
this.isChange = true;
if (!this.hasKey(key)) this.memberNumber++;
const position = this.getHashIndex(key);
let members = this.tableLink[position];
if (members instanceof LinkedList && members.count >= 8) {
let newElement = new RBTreeClass<K, V>();
let current = members.getHead();
while (current != null || current != undefined) {
if (!(current.element instanceof Pair))
throw new Error("this hashtable member save error");
newElement.addNode(current.element.key, current.element.value);
current = current.next;
}
newElement.addNode(key, value);
this.tableLink[position] = newElement;
return true;
} else if (members instanceof RBTreeClass) {
members.addNode(key, value);
this.tableLink[position] = members;
return true;
} else {
if (this.tableLink[position] == undefined) {
members = new LinkedList<Pair<K, V>>();
}
if (!this.replaceMember(key, value)) {
members.push(new Pair(key, value));
this.tableLink[position] = members;
}
return true;
this.isChange = true;
if (!this.hasKey(key)) this.memberNumber++;
const position = this.getHashIndex(key);
let members = this.tableLink[position];
if (members instanceof LinkedList && members.count >= 8) {
let newElement = new RBTreeClass<K, V>();
let current = members.getHead();
while (current != null || current != undefined) {
if (!(current.element instanceof Pair)) return false;
newElement.addNode(current.element.key, current.element.value);
current = current.next;
}
newElement.addNode(key, value);
this.tableLink[position] = newElement;
return true;
} else if (members instanceof RBTreeClass) {
members.addNode(key, value);
this.tableLink[position] = members;
return true;
} else {
if (this.tableLink[position] == undefined) {
members = new LinkedList<Pair<K, V>>();
}
if (!this.replaceMember(key, value)) {
members.push(new Pair(key, value));
this.tableLink[position] = members;
}
return true;
}
return false;
}
protected replaceMember(key: K, value: V = key as unknown as V): boolean {
@@ -978,7 +978,7 @@ class LinkedList<T> {
return false;
}
indexOf(element: T, compareFn?: Function) {
indexOf(element: T, compareFn?: CompFun<T>) {
let current = this.head;
for (let i = 0; i < this.count && current != undefined; i++) {
if (currencyCompare(element, current.element, compareFn) === ComparResult.EQUALS) {
@@ -989,7 +989,7 @@ class LinkedList<T> {
return -1;
}
remove(element: T, compareFn?: Function) {
remove(element: T, compareFn?: CompFun<T>) {
this.removeAt(this.indexOf(element, compareFn));
}
@@ -1020,7 +1020,6 @@ class LinkedList<T> {
}
}
export default {
isIncludeToArray,
LightWeightClass,
+1 -1
View File
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2021 Huawei Device Co., Ltd.
* 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
+18 -19
View File
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2021 Huawei Device Co., Ltd.
* 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
@@ -69,20 +69,19 @@ if (flag || fastTreeMap === undefined) {
}
get(key: K): V {
let tempNode = this.constitute.getNode(key);
if (tempNode === undefined)
throw new Error("The node of this key does not exist in the tree");
if (tempNode === undefined) return tempNode;
return tempNode.value;
}
getFirstKey(): K {
let tempNode = this.constitute.firstNode();
if (tempNode === undefined)
throw new Error("don't find this key,this tree is empty");
return tempNode;
return tempNode.key;
}
getLastKey(): K {
let tempNode = this.constitute.lastNode();
if (tempNode === undefined)
throw new Error("don't find this key,this tree is empty");
return tempNode;
return tempNode.key;
}
setAll(map: TreeMap<K, V>) {
@@ -98,36 +97,36 @@ if (flag || fastTreeMap === undefined) {
this.constitute.clearTree();
}
getLowerKey(key: K): K {
let result: any = undefined;
let tempNode = this.constitute.getNode(key);
if (tempNode === undefined)
throw new Error("don't find this key,this node is undefine");
if (tempNode === undefined) return tempNode;
if (tempNode.left !== undefined) return tempNode.left.key;
let node = tempNode;
while (node.parent !== undefined) {
if (node.parent.right === node) return node.parent.key;
node = node.parent;
}
throw new Error("don't find a key meet the conditions");
return result;
}
getHigherKey(key: K): K {
let result: any = undefined;
let tempNode = this.constitute.getNode(key);
if (tempNode === undefined)
throw new Error("don't find this key,this node is undefine");
if (tempNode === undefined) return tempNode;
if (tempNode.right !== undefined) return tempNode.right.key;
let node = tempNode;
while (node.parent !== undefined) {
if (node.parent.left === node) return node.parent.key;
node = node.parent;
}
throw new Error("don't find a key meet the conditions");
return result;
}
keys(): IterableIterator<K> {
let data = this.constitute;
let count = 0;
return {
next: function () {
var done = count >= data.memberNumber;
var value = !done ? data.keyValueArray[count].key : undefined;
let done = count >= data.memberNumber;
let value = !done ? data.keyValueArray[count].key : undefined;
count++;
return {
done: done,
@@ -141,8 +140,8 @@ if (flag || fastTreeMap === undefined) {
let count = 0;
return {
next: function () {
var done = count >= data.memberNumber;
var value = !done ? data.keyValueArray[count].value : undefined;
let done = count >= data.memberNumber;
let value = !done ? data.keyValueArray[count].value : undefined;
count++;
return {
done: done,
@@ -170,8 +169,8 @@ if (flag || fastTreeMap === undefined) {
let count = 0;
return {
next: function () {
var done = count >= data.memberNumber;
var value = !done ? data.keyValueArray[count].entry() : undefined;
let done = count >= data.memberNumber;
let value = !done ? data.keyValueArray[count].entry() : undefined;
count++;
return {
done: done,
@@ -185,8 +184,8 @@ if (flag || fastTreeMap === undefined) {
let count = 0;
return {
next: function () {
var done = count >= data.memberNumber;
var value = !done ? data.keyValueArray[count].entry() : undefined;
let done = count >= data.memberNumber;
let value = !done ? data.keyValueArray[count].entry() : undefined;
count++;
return {
done: done,
+1 -1
View File
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2021 Huawei Device Co., Ltd.
* 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
+19 -23
View File
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2021 Huawei Device Co., Ltd.
* 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
@@ -77,63 +77,59 @@ if (flag || fastTreeSet === undefined) {
}
getFirstValue(): T {
let tempNode = this.constitute.firstNode();
if (tempNode === undefined)
throw new Error("don't find this key,this tree is empty");
if (tempNode === undefined) return tempNode;
return tempNode.key;
}
getLastValue(): T {
let tempNode = this.constitute.lastNode();
if (tempNode === undefined)
throw new Error("don't find this key,this tree is empty");
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)
throw new Error("don't find this key,this node is undefine");
if (tempNode === undefined) return tempNode;
if (tempNode.left !== undefined) return tempNode.left.key;
let node = tempNode;
while (node.parent !== undefined) {
if (node.parent.right === node) return node.parent.key;
node = node.parent; // node.parent.left === node is true;
}
throw new Error("don't find a key meet the conditions");
return result;
}
getHigherValue(key: T): T {
let result: any = undefined;
let tempNode = this.constitute.getNode(key);
if (tempNode === undefined)
throw new Error("don't find this key,this node is undefine");
if (tempNode === undefined) return tempNode;
if (tempNode.right !== undefined) return tempNode.right.key;
let node = tempNode;
while (node.parent !== undefined) {
if (node.parent.left === node) return node.parent.key;
node = node.parent; // node.parent.right === node is true;
}
throw new Error("don't find a key meet the conditions");
return result;
}
popFirst(): T {
let firstNode = this.constitute.firstNode();
if (firstNode === undefined)
throw new Error("don't find first node,this tree is empty");
if (firstNode === undefined) return firstNode;
let value = firstNode.value;
this.constitute.removeNodeProcess(firstNode);
return value as T;
return value;
}
popLast(): T {
let lastNode = this.constitute.lastNode();
if (lastNode === undefined)
throw new Error("don't find last node,this tree is empty");
if (lastNode === undefined) return lastNode;
let value = lastNode.value;
this.constitute.removeNodeProcess(lastNode);
return value as T;
return value;
}
values(): IterableIterator<T> {
let data = this.constitute;
let count = 0;
return {
next: function () {
var done = count >= data.memberNumber;
var value = !done ? data.keyValueArray[count].value as T : undefined;
let done = count >= data.memberNumber;
let value = !done ? data.keyValueArray[count].value as T : undefined;
count++;
return {
done: done,
@@ -155,8 +151,8 @@ if (flag || fastTreeSet === undefined) {
let count = 0;
return {
next: function () {
var done = count >= data.memberNumber;
var value = !done ? data.keyValueArray[count].entry() : undefined;
let done = count >= data.memberNumber;
let value = !done ? data.keyValueArray[count].entry() : undefined;
count++;
return {
done: done,
@@ -170,8 +166,8 @@ if (flag || fastTreeSet === undefined) {
let count = 0;
return {
next: function () {
var done = count >= data.memberNumber;
var value = !done ? data.keyValueArray[count].key : undefined;
let done = count >= data.memberNumber;
let value = !done ? data.keyValueArray[count].key : undefined;
count++;
return {
done: done,
+1 -1
View File
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2021 Huawei Device Co., Ltd.
* 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
+1 -1
View File
@@ -7,7 +7,7 @@
"outDir": "./jscode", /* Specify an output folder for all emitted files. */
"esModuleInterop": true,
"forceConsistentCasingInFileNames": true,
"strict": true,
// "strict": true,
"skipLibCheck": true,
"noImplicitThis": false,
"suppressImplicitAnyIndexErrors": true
+49 -28
View File
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2021 Huawei Device Co., Ltd.
* 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
@@ -22,16 +22,19 @@ if (arkPritvate !== undefined) {
}
if (flag || fastVector == undefined) {
class HandlerVector<T> {
private isOutBounds(obj: Vector<T>, prop: any) {
let index = Number.parseInt(prop);
if (Number.isInteger(index)) {
if (index < 0 || index >= obj.length) {
throw new RangeError("the index is out-of-bounds");
}
}
}
get(obj: Vector<T>, prop: any) {
if (typeof prop === "symbol") {
return obj[prop];
}
var index = Number.parseInt(prop);
if (Number.isInteger(index)) {
if (index < 0 || index >= obj.length) {
throw new Error("Vector: get out-of-bounds");
}
}
this.isOutBounds(obj, prop);
return obj[prop];
}
set(obj: Vector<T>, prop: any, value: T) {
@@ -42,7 +45,7 @@ if (flag || fastVector == undefined) {
let index = Number.parseInt(prop);
if (Number.isInteger(index)) {
if (index < 0 || index > obj.length) {
throw new Error("Vector: set out-of-bounds");
throw new RangeError("the index is out-of-bounds");
} else {
obj[index] = value;
return true;
@@ -51,11 +54,9 @@ if (flag || fastVector == undefined) {
return false;
}
deleteProperty(obj: Vector<T>, prop: any) {
var index = Number.parseInt(prop);
if (Number.isInteger(index)) {
if (index < 0 || index >= obj.length) {
throw new Error("vector: deleteProperty out-of-bounds");
}
this.isOutBounds(obj, prop);
let index = Number.parseInt(prop);
if (index >= 0 && index < obj.length && Number.isInteger(index)) {
obj.removeByIndex(index);
return true;
}
@@ -66,7 +67,7 @@ if (flag || fastVector == undefined) {
}
ownKeys(obj: Vector<T>) {
let keys = [];
for (var i = 0; i < obj.length; i++) {
for (let i = 0; i < obj.length; i++) {
keys.push(i.toString());
}
return keys;
@@ -75,17 +76,15 @@ if (flag || fastVector == undefined) {
return true;
}
getOwnPropertyDescriptor(obj: Vector<T>, prop: any) {
var index = Number.parseInt(prop);
if (Number.isInteger(index)) {
if (index < 0 || index >= obj.length) {
throw new Error("Vector: getOwnPropertyDescriptor out-of-bounds");
}
this.isOutBounds(obj, prop);
let index = 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 Error("Can setPrototype on Vector Object");
throw new RangeError("Can setPrototype on Vector Object");
}
}
interface IterableIterator<T> {
@@ -111,6 +110,9 @@ if (flag || fastVector == undefined) {
return true;
}
insert(element: T, index: number): void {
if (index < 0 || index >= this.elementNum) {
throw new RangeError("the index is out-of-bounds");
}
if (this.isFull()) {
this.resize();
}
@@ -140,12 +142,22 @@ if (flag || fastVector == undefined) {
return -1;
}
getFirstElement(): T {
if (this.isEmpty()) {
return undefined;
}
return this[0];
}
set(index: number, element: T): void {
set(index: number, element: T): T {
if (index < 0 || index >= this.elementNum) {
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");
}
let result = this[index];
for (let i = index; i < this.elementNum - 1; i++) {
this[i] = this[i + 1];
@@ -165,6 +177,9 @@ if (flag || fastVector == undefined) {
return false;
}
getLastElement(): T {
if (this.isEmpty()) {
return undefined;
}
return this[this.elementNum - 1];
}
getLastIndexOf(element: T): number {
@@ -200,9 +215,12 @@ if (flag || fastVector == undefined) {
}
removeByRange(fromIndex: number, toIndex: number): void {
if (fromIndex >= toIndex) {
throw new Error(`fromIndex cannot be less than or equal to toIndex`);
throw new RangeError(`the fromIndex cannot be less than or equal to toIndex`);
}
toIndex = toIndex >= this.length - 1 ? this.length - 1 : toIndex;
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++) {
this[i] = this[j];
@@ -211,6 +229,9 @@ if (flag || fastVector == undefined) {
this.elementNum -= toIndex - fromIndex;
}
setLength(newSize: number): void {
if (newSize < 0) {
throw new RangeError(`An incorrect size was set`);
}
this.elementNum = newSize;
}
replaceAllElements(callbackfn: (value: T, index?: number, vector?: Vector<T>) => T,
@@ -239,7 +260,7 @@ if (flag || fastVector == undefined) {
}
}
} else {
for (var i = 0; i < this.length - 1; i++) {
for (let i = 0; i < this.elementNum - 1; i++) {
for (let j = 0; j < this.elementNum - 1 - i; j++) {
if (this.asciSort(this[j], this[j + 1])) {
isSort = false;
@@ -270,10 +291,10 @@ if (flag || fastVector == undefined) {
}
subVector(fromIndex: number, toIndex: number): Vector<T> {
if (fromIndex >= toIndex) {
throw new Error(`fromIndex cannot be less than or equal to toIndex`);
throw new RangeError(`the fromIndex cannot be less than or equal to toIndex`);
}
if (fromIndex >= this.elementNum || fromIndex < 0 || toIndex < 0) {
throw new Error(`fromIndex or toIndex is out-of-bounds`);
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>();
@@ -334,8 +355,8 @@ if (flag || fastVector == undefined) {
let vector = this;
return {
next: function () {
var done = count >= vector.elementNum;
var value = !done ? vector[count++] : undefined;
let done = count >= vector.elementNum;
let value = !done ? vector[count++] : undefined;
return {
done: done,
value: value,
+1 -1
View File
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2021 Huawei Device Co., Ltd.
* 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