@zwa73/utils 1.0.14 → 1.0.16
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/UtilClass.d.ts +289 -125
- package/dist/UtilClass.js +354 -136
- package/dist/UtilDecorators.d.ts +16 -0
- package/dist/UtilDecorators.js +65 -0
- package/dist/UtilFfmpegTools.d.ts +7 -23
- package/dist/UtilFfmpegTools.js +36 -59
- package/dist/UtilFunctions.d.ts +42 -4
- package/dist/UtilFunctions.js +128 -47
- package/dist/index.d.ts +1 -0
- package/dist/index.js +1 -0
- package/package.json +1 -1
- package/src/UtilClass.ts +517 -232
- package/src/UtilDecorators.ts +59 -0
- package/src/UtilFfmpegTools.ts +36 -60
- package/src/UtilFunctions.ts +159 -73
- package/src/UtilInterfaces.ts +0 -2
- package/src/index.ts +2 -1
- package/test.js +10 -1
package/src/UtilClass.ts
CHANGED
|
@@ -1,59 +1,138 @@
|
|
|
1
|
-
type struct = number|string;
|
|
1
|
+
type struct = number | string;
|
|
2
|
+
|
|
3
|
+
/**遍历函数
|
|
4
|
+
*/
|
|
5
|
+
interface EachCallback<T> {
|
|
6
|
+
/**
|
|
7
|
+
* @param {T} value - 值
|
|
8
|
+
* @param {number} index - 下标
|
|
9
|
+
* @param {SList<T>} list - 数组
|
|
10
|
+
* @returns {void}
|
|
11
|
+
*/
|
|
12
|
+
(value: T, index: number, list: SList<T>): void;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
/**加工函数
|
|
16
|
+
*/
|
|
17
|
+
interface MapCallback<T, U> {
|
|
18
|
+
/**
|
|
19
|
+
* @param {T} value - 值
|
|
20
|
+
* @param {number} index - 下标
|
|
21
|
+
* @param {SList<T>} list - 数组
|
|
22
|
+
* @returns {U} - 返回值
|
|
23
|
+
*/
|
|
24
|
+
(value: T, index: number, list: SList<T>): U;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
/**过滤函数
|
|
28
|
+
*/
|
|
29
|
+
interface FiltCallback<T> {
|
|
30
|
+
/**
|
|
31
|
+
* @param {T} value - 值
|
|
32
|
+
* @param {number} index - 下标
|
|
33
|
+
* @param {SList<T>} list - 数组
|
|
34
|
+
* @returns {boolean} - 返回值
|
|
35
|
+
*/
|
|
36
|
+
(value: T, index: number, list: SList<T>): boolean;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
/**统计函数
|
|
40
|
+
*/
|
|
41
|
+
interface AggrCallback<T, U> {
|
|
42
|
+
/**
|
|
43
|
+
* @param {U} accumulator - 累加器
|
|
44
|
+
* @param {T} value - 值
|
|
45
|
+
* @param {number} index - 下标
|
|
46
|
+
* @param {SList<T>} list - 数组
|
|
47
|
+
* @returns {U} - 返回值
|
|
48
|
+
*/
|
|
49
|
+
(accumulator: U, value: T, index: number, list: SList<T>): U;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
/**缩减函数
|
|
53
|
+
*/
|
|
54
|
+
interface ReduceCallback<T> {
|
|
55
|
+
/**
|
|
56
|
+
* @param {T} previousValue - 上一个值/累加值
|
|
57
|
+
* @param {T} currentValue - 当前值
|
|
58
|
+
* @param {number} currentIndex - 当前下标
|
|
59
|
+
* @param {SList<T>} list - 数组
|
|
60
|
+
* @returns {T} - 返回值
|
|
61
|
+
*/
|
|
62
|
+
(
|
|
63
|
+
previousValue: T,
|
|
64
|
+
currentValue: T,
|
|
65
|
+
currentIndex: number,
|
|
66
|
+
list: SList<T>
|
|
67
|
+
): T;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
/**排序函数
|
|
71
|
+
*/
|
|
72
|
+
interface SortCallback<T> {
|
|
73
|
+
/**
|
|
74
|
+
* @param {T} a - 值a
|
|
75
|
+
* @param {T} b - 值b
|
|
76
|
+
* @returns {number} - 返回值
|
|
77
|
+
*/
|
|
78
|
+
(a: T, b: T): number;
|
|
79
|
+
}
|
|
2
80
|
|
|
3
81
|
export class SList<T> {
|
|
4
|
-
|
|
5
|
-
constructor(obj?:Array<T
|
|
6
|
-
if(typeof obj ==
|
|
7
|
-
|
|
8
|
-
else
|
|
9
|
-
this._arr = obj;
|
|
10
|
-
else
|
|
11
|
-
this._arr=[];
|
|
82
|
+
private _arr: Array<T>;
|
|
83
|
+
constructor(obj?: Array<T> | number) {
|
|
84
|
+
if (typeof obj == "number") this._arr = new Array<T>(obj);
|
|
85
|
+
else if (Array.isArray(obj)) this._arr = obj;
|
|
86
|
+
else this._arr = [];
|
|
12
87
|
}
|
|
13
|
-
|
|
14
|
-
|
|
88
|
+
/**返回数组长度
|
|
89
|
+
* @returns {number} - 长度
|
|
90
|
+
*/
|
|
91
|
+
size(): number {
|
|
15
92
|
return this._arr.length;
|
|
16
93
|
}
|
|
17
94
|
|
|
18
95
|
/**获取指定下标的元素
|
|
19
|
-
* @param index 下标
|
|
96
|
+
* @param {number} index - 下标
|
|
97
|
+
* @returns {T} - 目标元素
|
|
20
98
|
*/
|
|
21
|
-
get(index: number):T{
|
|
99
|
+
get(index: number): T {
|
|
22
100
|
return this._arr[index];
|
|
23
101
|
}
|
|
24
102
|
|
|
25
103
|
/**设置指定下标的元素
|
|
26
104
|
* 返回自身 改变自身
|
|
27
|
-
* @param index 下标
|
|
28
|
-
* @param value 值
|
|
29
|
-
* @returns 自身
|
|
105
|
+
* @param {number} index - 下标
|
|
106
|
+
* @param {T} value - 值
|
|
107
|
+
* @returns {SList<T>} - 自身
|
|
30
108
|
*/
|
|
31
|
-
set(index: number, value: T):SList<T>{
|
|
109
|
+
set(index: number, value: T): SList<T> {
|
|
32
110
|
this._arr[index] = value;
|
|
33
111
|
return this;
|
|
34
112
|
}
|
|
35
113
|
|
|
36
114
|
/**连接两个数组
|
|
37
|
-
* @param list 目标数组
|
|
38
|
-
* @returns 新数组
|
|
115
|
+
* @param {SList<T>} list - 目标数组
|
|
116
|
+
* @returns {SList<T>} - 新数组
|
|
39
117
|
*/
|
|
40
|
-
concat(list: SList<T>):SList<T>{
|
|
118
|
+
concat(list: SList<T>): SList<T> {
|
|
41
119
|
return new SList<T>(this._arr.concat(list._arr));
|
|
42
120
|
}
|
|
43
121
|
|
|
44
122
|
/**在数组末尾添加一个元素
|
|
45
123
|
* 返回自身 改变自身
|
|
46
|
-
* @param value 值
|
|
124
|
+
* @param {T} value - 值
|
|
125
|
+
* @returns {SList<T>} - 自身
|
|
47
126
|
*/
|
|
48
|
-
push(value: T):SList<T>{
|
|
127
|
+
push(value: T): SList<T> {
|
|
49
128
|
this._arr.push(value);
|
|
50
129
|
return this;
|
|
51
130
|
}
|
|
52
131
|
|
|
53
132
|
/**在数组末尾添加一个数组
|
|
54
133
|
* 改变自身
|
|
55
|
-
* @param list 目标数组
|
|
56
|
-
* @returns 自身
|
|
134
|
+
* @param {SList<T>} list - 目标数组
|
|
135
|
+
* @returns {SList<T>} - 自身
|
|
57
136
|
*/
|
|
58
137
|
pushList(list: SList<T>) {
|
|
59
138
|
this._arr = this._arr.concat(list._arr);
|
|
@@ -61,117 +140,155 @@ export class SList<T> {
|
|
|
61
140
|
}
|
|
62
141
|
|
|
63
142
|
/**截取从起始点到结束点之前的一段数组
|
|
64
|
-
* @param strat
|
|
65
|
-
* @param end 结束点
|
|
66
|
-
* @returns 新数组
|
|
143
|
+
* @param {number} strat - 起始点
|
|
144
|
+
* @param {number} end - 结束点
|
|
145
|
+
* @returns {SList<T>} - 新数组
|
|
67
146
|
*/
|
|
68
|
-
slice(strat: number, end: number):SList<T>{
|
|
147
|
+
slice(strat: number, end: number): SList<T> {
|
|
69
148
|
return new SList(this._arr.slice(strat, end));
|
|
70
149
|
}
|
|
71
150
|
|
|
72
151
|
/**翻转数组
|
|
73
152
|
* 改变自身
|
|
74
|
-
* @returns 自身
|
|
153
|
+
* @returns {SList<T>} - 自身
|
|
75
154
|
*/
|
|
76
|
-
reverse():SList<T>{
|
|
155
|
+
reverse(): SList<T> {
|
|
77
156
|
this._arr.reverse();
|
|
78
157
|
return this;
|
|
79
158
|
}
|
|
80
159
|
|
|
81
|
-
/**将SList转换为数组
|
|
82
|
-
|
|
160
|
+
/**将SList转换为数组
|
|
161
|
+
* @returns {Array<T>} - 数组
|
|
162
|
+
*/
|
|
163
|
+
toArray(): Array<T> {
|
|
83
164
|
return ([] as T[]).concat(this._arr);
|
|
84
165
|
}
|
|
85
166
|
|
|
167
|
+
/**将SList转换为SStream
|
|
168
|
+
* @param {number} concurrent - 并发数
|
|
169
|
+
* @returns {SStream<T>} - 流
|
|
170
|
+
*/
|
|
171
|
+
toSStream(concurrent?: number): SStream<T> {
|
|
172
|
+
return new SStream<T>(this._arr, concurrent);
|
|
173
|
+
}
|
|
174
|
+
|
|
86
175
|
/**返回指定元素在数组中首次出现的位置
|
|
87
|
-
* @param value 目标元素
|
|
176
|
+
* @param {T} value - 目标元素
|
|
177
|
+
* @returns {number} - 下标
|
|
88
178
|
*/
|
|
89
|
-
indexOf(value: T):number{
|
|
179
|
+
indexOf(value: T): number {
|
|
90
180
|
return this._arr.indexOf(value);
|
|
91
181
|
}
|
|
92
182
|
|
|
93
183
|
/**返回指定元素在数组中最后一次出现的位置
|
|
94
|
-
* @param value 目标元素
|
|
184
|
+
* @param {T} value - 目标元素
|
|
185
|
+
* @returns {number} - 下标
|
|
95
186
|
*/
|
|
96
|
-
lastIndexOf(value: T):number{
|
|
187
|
+
lastIndexOf(value: T): number {
|
|
97
188
|
return this._arr.lastIndexOf(value);
|
|
98
189
|
}
|
|
99
190
|
|
|
100
191
|
/**判断数组中是否包含指定元素
|
|
101
|
-
* @param value 目标元素
|
|
192
|
+
* @param {T} value - 目标元素
|
|
193
|
+
* @returns {boolean} - 是否包含
|
|
102
194
|
*/
|
|
103
|
-
contains(value: T):boolean{
|
|
195
|
+
contains(value: T): boolean {
|
|
104
196
|
return this._arr.includes(value);
|
|
105
197
|
}
|
|
106
198
|
|
|
107
|
-
/**获取迭代器
|
|
108
|
-
|
|
199
|
+
/**获取迭代器
|
|
200
|
+
* @returns {SIterator<T>} - 迭代器
|
|
201
|
+
*/
|
|
202
|
+
iterator(): SIterator<T> {
|
|
109
203
|
return new SIterator<T>(this);
|
|
110
204
|
}
|
|
111
205
|
|
|
112
|
-
/**平分数组
|
|
113
|
-
|
|
206
|
+
/**平分数组
|
|
207
|
+
* @param {number} count - 份数
|
|
208
|
+
* @param {"average" | "chunk"} mode - 模式 average:轮询平均分 chunk:切块均分
|
|
209
|
+
* @returns {SList<SList<T>>} - 新数组
|
|
210
|
+
*/
|
|
211
|
+
divide(
|
|
212
|
+
count: number,
|
|
213
|
+
mode: "average" | "chunk" = "average"
|
|
214
|
+
): SList<SList<T>> {
|
|
215
|
+
if (count <= 0) return new SList<SList<T>>();
|
|
216
|
+
if (count == 1) return new SList<SList<T>>([this.clone()]);
|
|
217
|
+
|
|
114
218
|
let result = new SList<SList<T>>();
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
219
|
+
switch (mode) {
|
|
220
|
+
//轮询平均分
|
|
221
|
+
default:
|
|
222
|
+
case "average":
|
|
223
|
+
for (let i = 0; i < count; i++) {
|
|
224
|
+
let clist = new SList<T>();
|
|
225
|
+
let size = this.size();
|
|
226
|
+
for (let j = i; j < size; j += count)
|
|
227
|
+
clist.push(this.get(j));
|
|
228
|
+
result.push(clist);
|
|
229
|
+
}
|
|
230
|
+
break;
|
|
231
|
+
//切块均分
|
|
232
|
+
case "chunk":
|
|
233
|
+
let chunkSize = Math.ceil(this.size() / count);
|
|
234
|
+
for (let i = 0; i < count; i++) {
|
|
235
|
+
let start = i * chunkSize;
|
|
236
|
+
let end = (i + 1) * chunkSize;
|
|
237
|
+
if (end > this.size()) end = this.size();
|
|
238
|
+
result.push(this.slice(start, end));
|
|
239
|
+
}
|
|
240
|
+
break;
|
|
122
241
|
}
|
|
123
242
|
return result;
|
|
124
243
|
}
|
|
125
244
|
|
|
126
|
-
/**从index开始删除count
|
|
245
|
+
/**从index开始删除count个元素,返回删除的元素数组
|
|
127
246
|
* 改变自身
|
|
128
|
-
* @param index 起始点
|
|
129
|
-
* @param count 数量
|
|
130
|
-
* @returns
|
|
247
|
+
* @param {number} index - 起始点
|
|
248
|
+
* @param {number} count - 数量
|
|
249
|
+
* @returns {SList<T>} - 删除的元素数组
|
|
131
250
|
*/
|
|
132
251
|
removeRange(index: number, count: number): SList<T> {
|
|
133
|
-
this._arr.splice(index, count);
|
|
134
|
-
return
|
|
252
|
+
let narr = this._arr.splice(index, count);
|
|
253
|
+
return new SList<T>(narr);
|
|
135
254
|
}
|
|
136
255
|
|
|
137
|
-
|
|
256
|
+
/**删除对应下标下的元素,返回删除的元素
|
|
138
257
|
* 改变自身
|
|
139
|
-
* @param i 下标
|
|
140
|
-
* @returns
|
|
258
|
+
* @param {number} i - 下标
|
|
259
|
+
* @returns {T|null} - 删除的元素
|
|
141
260
|
*/
|
|
142
|
-
remove(i: number):
|
|
261
|
+
remove(i: number): T|null {
|
|
143
262
|
if (i >= 0 && i < this.size())
|
|
144
|
-
this._arr.splice(i, 1);
|
|
145
|
-
return
|
|
263
|
+
return this._arr.splice(i, 1)[0];
|
|
264
|
+
return null;
|
|
146
265
|
}
|
|
147
266
|
|
|
148
|
-
|
|
267
|
+
/**删除第一个匹配的项目,返回自身
|
|
149
268
|
* 改变自身
|
|
150
|
-
* @param obj 需删除目标
|
|
151
|
-
* @returns 自身
|
|
269
|
+
* @param {T} obj - 需删除目标
|
|
270
|
+
* @returns {SList<T>} - 自身
|
|
152
271
|
*/
|
|
153
272
|
removeMember(obj: T): SList<T> {
|
|
154
273
|
let index = this.indexOf(obj);
|
|
155
|
-
if (index > -1)
|
|
156
|
-
this._arr.splice(index, 1);
|
|
274
|
+
if (index > -1) this.remove(index);
|
|
157
275
|
return this;
|
|
158
276
|
}
|
|
159
277
|
|
|
160
278
|
/**删除所有匹配的项目
|
|
161
279
|
* 改变自身
|
|
162
|
-
* @param obj 需删除目标
|
|
163
|
-
* @returns 自身
|
|
280
|
+
* @param {T} obj - 需删除目标
|
|
281
|
+
* @returns {SList<T>} - 自身
|
|
164
282
|
*/
|
|
165
283
|
removeAllMember(obj: T): SList<T> {
|
|
166
|
-
while (this.contains(obj))
|
|
167
|
-
this.removeMember(obj);
|
|
284
|
+
while (this.contains(obj)) this.removeMember(obj);
|
|
168
285
|
return this;
|
|
169
286
|
}
|
|
170
287
|
/**在这个下标对应的元素前添加一个元素
|
|
171
288
|
* 改变自身
|
|
172
|
-
* @param index 下标
|
|
173
|
-
* @param obj 添加对象
|
|
174
|
-
* @returns 自身
|
|
289
|
+
* @param {number} index - 下标
|
|
290
|
+
* @param {T} obj - 添加对象
|
|
291
|
+
* @returns {SList<T>} - 自身
|
|
175
292
|
*/
|
|
176
293
|
insert(index: number, obj: T): SList<T> {
|
|
177
294
|
this._arr.splice(index, 0, obj);
|
|
@@ -180,60 +297,79 @@ export class SList<T> {
|
|
|
180
297
|
|
|
181
298
|
/**在这个下标对应的元素前添加一组元素
|
|
182
299
|
* 改变自身
|
|
183
|
-
* @param index 下标
|
|
184
|
-
* @param obj 添加对象
|
|
185
|
-
* @returns 自身
|
|
300
|
+
* @param {number} index - 下标
|
|
301
|
+
* @param {SList<T>} obj - 添加对象
|
|
302
|
+
* @returns {SList<T>} - 自身
|
|
186
303
|
*/
|
|
187
304
|
insertRange(index: number, obj: SList<T>): SList<T> {
|
|
188
305
|
this._arr.splice(index, 0, ...obj._arr);
|
|
189
306
|
return this;
|
|
190
307
|
}
|
|
191
308
|
|
|
309
|
+
/**删除并返回最后一个元素
|
|
310
|
+
* 改变自身
|
|
311
|
+
* @returns {T | null} - 最后一个元素
|
|
312
|
+
*/
|
|
313
|
+
pop(): T | null {
|
|
314
|
+
let val = this._arr.pop();
|
|
315
|
+
return val == undefined ? null : val;
|
|
316
|
+
}
|
|
317
|
+
|
|
318
|
+
/**删除并返回第一个元素
|
|
319
|
+
* 改变自身
|
|
320
|
+
* @returns {T | null} - 第一个元素
|
|
321
|
+
*/
|
|
322
|
+
shift(): T | null {
|
|
323
|
+
let val = this._arr.shift();
|
|
324
|
+
return val == undefined ? null : val;
|
|
325
|
+
}
|
|
326
|
+
|
|
327
|
+
|
|
328
|
+
|
|
192
329
|
/**克隆数组
|
|
193
|
-
* @returns 新数组
|
|
330
|
+
* @returns {SList<T>} - 新数组
|
|
194
331
|
*/
|
|
195
332
|
clone(): SList<T> {
|
|
196
333
|
return new SList(([] as T[]).concat(this._arr));
|
|
197
334
|
}
|
|
198
335
|
|
|
199
336
|
/**判断数组是否为空
|
|
337
|
+
* @returns {boolean} - 是否为空
|
|
200
338
|
*/
|
|
201
339
|
isEmpty(): boolean {
|
|
202
|
-
return this.size()==0;
|
|
340
|
+
return this.size() == 0;
|
|
203
341
|
}
|
|
204
342
|
|
|
205
343
|
/**删除数组中的重复元素
|
|
206
344
|
* 改变自身
|
|
207
|
-
* @returns 自身
|
|
345
|
+
* @returns {SList<T>} - 自身
|
|
208
346
|
*/
|
|
209
|
-
removeDuplicates():SList<T> {
|
|
347
|
+
removeDuplicates(): SList<T> {
|
|
210
348
|
this._arr = Array.from(new Set(this._arr));
|
|
211
349
|
return this;
|
|
212
350
|
}
|
|
213
351
|
|
|
214
352
|
/**交集
|
|
215
|
-
* @param lists 数组列表
|
|
216
|
-
* @returns 新数组
|
|
353
|
+
* @param {SList<T>[]} lists - 数组列表
|
|
354
|
+
* @returns {SList<T>} - 新数组
|
|
217
355
|
*/
|
|
218
356
|
intersection(...lists: SList<T>[]): SList<T> {
|
|
219
357
|
let nlist = this.clone().removeDuplicates();
|
|
220
358
|
|
|
221
|
-
for(let list of lists)
|
|
222
|
-
nlist = list.filt( val => nlist.contains(val));
|
|
359
|
+
for (let list of lists) nlist = list.filt((val) => nlist.contains(val));
|
|
223
360
|
|
|
224
361
|
return nlist;
|
|
225
362
|
}
|
|
226
363
|
/**并集
|
|
227
|
-
* @param lists 数组列表
|
|
228
|
-
* @returns 新数组
|
|
364
|
+
* @param {SList<T>[]} lists - 数组列表
|
|
365
|
+
* @returns {SList<T>} - 新数组
|
|
229
366
|
*/
|
|
230
367
|
union(...lists: SList<T>[]): SList<T> {
|
|
231
368
|
let nlist = this.clone().removeDuplicates();
|
|
232
369
|
|
|
233
|
-
for(let list of lists){
|
|
234
|
-
list.each(
|
|
235
|
-
if(!nlist.contains(val))
|
|
236
|
-
nlist.push(val);
|
|
370
|
+
for (let list of lists) {
|
|
371
|
+
list.each((val) => {
|
|
372
|
+
if (!nlist.contains(val)) nlist.push(val);
|
|
237
373
|
});
|
|
238
374
|
}
|
|
239
375
|
|
|
@@ -241,16 +377,16 @@ export class SList<T> {
|
|
|
241
377
|
}
|
|
242
378
|
|
|
243
379
|
/**返回符合条件的成员组成的新数组
|
|
244
|
-
* @param func 条件函数
|
|
245
|
-
* @returns 新数组
|
|
380
|
+
* @param {FiltCallback<T>} func - 条件函数
|
|
381
|
+
* @returns {SList<T>} - 新数组
|
|
246
382
|
*/
|
|
247
|
-
filt(func:
|
|
383
|
+
filt(func: FiltCallback<T>): SList<T> {
|
|
248
384
|
let nlist = new SList<T>(this.size());
|
|
249
385
|
let length = 0;
|
|
250
386
|
let it = this.iterator();
|
|
251
387
|
while (it.hasNext()) {
|
|
252
388
|
let tmpObj = it.next();
|
|
253
|
-
if (func(tmpObj)) {
|
|
389
|
+
if (func(tmpObj, it.currIndex(), this)) {
|
|
254
390
|
nlist.set(length, tmpObj);
|
|
255
391
|
length++;
|
|
256
392
|
}
|
|
@@ -259,48 +395,63 @@ export class SList<T> {
|
|
|
259
395
|
}
|
|
260
396
|
|
|
261
397
|
/**遍历数组的每一个元素
|
|
262
|
-
* @param func
|
|
263
|
-
* @returns 自身
|
|
398
|
+
* @param {EachCallback<T>} func - 遍历函数 (value: T, index: number, list: SList<T>): void
|
|
399
|
+
* @returns {SList<T>} - 自身
|
|
264
400
|
*/
|
|
265
|
-
each(func:
|
|
401
|
+
each(func: EachCallback<T>): SList<T> {
|
|
266
402
|
let it = this.iterator();
|
|
267
|
-
while (it.hasNext())
|
|
268
|
-
func(it.next());
|
|
403
|
+
while (it.hasNext()) func(it.next(), it.currIndex(), this);
|
|
269
404
|
return this;
|
|
270
405
|
}
|
|
271
406
|
|
|
272
407
|
/**对数组的每一个元素进行加工,返回加工完成的成员组成的新数组
|
|
273
|
-
* @param func 加工函数
|
|
274
|
-
* @returns 新数组
|
|
408
|
+
* @param {MapCallback<T,O>} func - 加工函数 (value: T, index: number, list: SList<T>): O
|
|
409
|
+
* @returns {SList<O>} - 新数组
|
|
275
410
|
*/
|
|
276
|
-
map<O>(func:
|
|
411
|
+
map<O>(func: MapCallback<T, O>): SList<O> {
|
|
277
412
|
let nlist = new SList<O>(this.size());
|
|
278
413
|
let it = this.iterator();
|
|
279
414
|
while (it.hasNext())
|
|
280
|
-
nlist.set(it.nextIndex(), func(it.next()));
|
|
415
|
+
nlist.set(it.nextIndex(), func(it.next(), it.currIndex(), this));
|
|
281
416
|
return nlist;
|
|
282
417
|
}
|
|
283
418
|
|
|
284
419
|
/**对数组进行排序
|
|
285
420
|
* 如函数返回值大于 0 则将 x 排在 y 后面,小于 0 则将 x 排在 y 前面
|
|
286
421
|
* 改变自身
|
|
287
|
-
* @param func
|
|
288
|
-
* @returns 自身
|
|
422
|
+
* @param {SortCallback<T>} func - 排序函数 (a: T, b: T): number
|
|
423
|
+
* @returns {SList<T>} - 自身
|
|
289
424
|
*/
|
|
290
|
-
sort(func:
|
|
291
|
-
this._arr.sort(
|
|
425
|
+
sort(func: SortCallback<T>): SList<T> {
|
|
426
|
+
this._arr.sort(func);
|
|
292
427
|
return this;
|
|
293
428
|
}
|
|
294
|
-
/**对数组进行统计
|
|
295
|
-
*
|
|
296
|
-
* @param
|
|
297
|
-
* @
|
|
429
|
+
/**对数组进行统计 aggregate
|
|
430
|
+
* 遍历数组,并将每次遍历的结果与下一次遍历的元素一起传入函数,最后返回最后一次遍历的结果
|
|
431
|
+
* @param {O} init - 初始值
|
|
432
|
+
* @param {AggrCallback<T,O>} func - 统计函数 (value: T, accumulator: U, index: number, list: SList<T>): U
|
|
433
|
+
* @returns {O} - 统计结果
|
|
298
434
|
*/
|
|
299
|
-
public
|
|
435
|
+
public aggr<O>(init: O, func: AggrCallback<T, O>): O {
|
|
300
436
|
let it = this.iterator();
|
|
301
437
|
let tmpObj = init;
|
|
302
438
|
while (it.hasNext())
|
|
303
|
-
tmpObj = func(tmpObj, it.next());
|
|
439
|
+
tmpObj = func(tmpObj, it.next(), it.currIndex(), this);
|
|
440
|
+
return tmpObj;
|
|
441
|
+
}
|
|
442
|
+
|
|
443
|
+
/**对数组进行缩减
|
|
444
|
+
* 遍历数组,并将每次遍历的结果与下一次遍历的元素一起传入函数,最后返回最后一次遍历的结果
|
|
445
|
+
* @param {ReduceCallback<T>} func - 缩减函数 (previousValue: T, currentValue: T, currentIndex: number, array: SList<T>):T
|
|
446
|
+
* @param {T} init - 初始值
|
|
447
|
+
* @returns {T} - 缩减结果
|
|
448
|
+
*/
|
|
449
|
+
public reduce(func: ReduceCallback<T>, init?: T): T | null {
|
|
450
|
+
if (this.size() === 0) return null;
|
|
451
|
+
let it = this.iterator();
|
|
452
|
+
let tmpObj = init === undefined ? it.next() : init;
|
|
453
|
+
while (it.hasNext())
|
|
454
|
+
tmpObj = func(tmpObj, it.next(), it.currIndex(), this);
|
|
304
455
|
return tmpObj;
|
|
305
456
|
}
|
|
306
457
|
|
|
@@ -309,7 +460,7 @@ export class SList<T> {
|
|
|
309
460
|
let it = this.iterator();
|
|
310
461
|
|
|
311
462
|
return {
|
|
312
|
-
next(): { value: T|null; done: boolean } {
|
|
463
|
+
next(): { value: T | null; done: boolean } {
|
|
313
464
|
if (it.hasNext()) {
|
|
314
465
|
return { value: it.next(), done: false };
|
|
315
466
|
} else {
|
|
@@ -320,137 +471,248 @@ export class SList<T> {
|
|
|
320
471
|
}
|
|
321
472
|
}
|
|
322
473
|
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
474
|
+
interface SStreamOperation<T, U> {
|
|
475
|
+
(item: T): Promise<U>;
|
|
476
|
+
}
|
|
477
|
+
export class SStream<T> {
|
|
478
|
+
/**并发数*/
|
|
479
|
+
private _concurrent: number;
|
|
480
|
+
/**原始列表*/
|
|
481
|
+
private _slist: SList<T>;
|
|
482
|
+
/**加工函数列表*/
|
|
483
|
+
private _operation: Array<SStreamOperation<T, T>> = [];
|
|
484
|
+
constructor(slist: SList<T> | Array<T>, concurrent: number = 1) {
|
|
485
|
+
if (slist instanceof SList) this._slist = slist;
|
|
486
|
+
else if (Array.isArray(slist)) this._slist = new SList(slist);
|
|
487
|
+
else this._slist = new SList();
|
|
488
|
+
this._concurrent = concurrent;
|
|
489
|
+
}
|
|
490
|
+
|
|
491
|
+
/**映射加工
|
|
492
|
+
* @param {SStreamOperation<T,U>} operation - 加工函数
|
|
493
|
+
* @returns {SStream<U>} - 新流
|
|
494
|
+
*/
|
|
495
|
+
map<U>(operation: SStreamOperation<T, U>): SStream<U> {
|
|
496
|
+
this._operation.push(operation as any);
|
|
497
|
+
return this as any as SStream<U>;
|
|
498
|
+
}
|
|
499
|
+
/**遍历
|
|
500
|
+
* 返回自身
|
|
501
|
+
* @param {SStreamOperation<T,void>} operation - 遍历函数
|
|
502
|
+
* @returns {SStream<T>} - 自身
|
|
503
|
+
*/
|
|
504
|
+
each(operation: SStreamOperation<T, void>): SStream<T> {
|
|
505
|
+
let opera = async (item: T) => {
|
|
506
|
+
operation(item);
|
|
507
|
+
return item;
|
|
508
|
+
};
|
|
509
|
+
this._operation.push(opera);
|
|
510
|
+
return this;
|
|
511
|
+
}
|
|
512
|
+
|
|
513
|
+
//终结操作
|
|
514
|
+
/**应用加工
|
|
515
|
+
* @returns {SStream<T>} - 自身
|
|
516
|
+
*/
|
|
517
|
+
async appendOperations(): Promise<SStream<T>> {
|
|
518
|
+
if (this._operation.length == 0) return this;
|
|
519
|
+
|
|
520
|
+
let nlist = new SList<SList<T>>();
|
|
521
|
+
let promiseList: Array<Promise<any>> = [];
|
|
522
|
+
//均分处理
|
|
523
|
+
let sliceList = this._slist.divide(this._concurrent);
|
|
524
|
+
for (let i = 0; i < this._concurrent; i++) {
|
|
525
|
+
let subList = sliceList.get(i);
|
|
526
|
+
if (!subList) continue;
|
|
527
|
+
promiseList.push(
|
|
528
|
+
new Promise(async () => {
|
|
529
|
+
let result = new SList<T>();
|
|
530
|
+
for (let item of subList) {
|
|
531
|
+
if (!item) continue;
|
|
532
|
+
for (let operation of this._operation)
|
|
533
|
+
item = await operation(item);
|
|
534
|
+
result.push(item);
|
|
535
|
+
}
|
|
536
|
+
nlist.set(i, result);
|
|
537
|
+
})
|
|
538
|
+
);
|
|
539
|
+
}
|
|
540
|
+
await Promise.all(promiseList);
|
|
541
|
+
//拼接结果 轮询均分
|
|
542
|
+
let result = new SList<T>(this._slist.size());
|
|
543
|
+
for (let i = 0; i < this._concurrent; i++) {
|
|
544
|
+
let subList = nlist.get(i);
|
|
545
|
+
if (!subList) continue;
|
|
546
|
+
let subSize = subList.size();
|
|
547
|
+
for (let j = 0; j < subSize; j++)
|
|
548
|
+
result.set(i + j * this._concurrent, subList.get(j));
|
|
549
|
+
}
|
|
550
|
+
this._slist = result;
|
|
551
|
+
this._operation = [];
|
|
552
|
+
return this;
|
|
553
|
+
}
|
|
554
|
+
/**转换为SList
|
|
555
|
+
* @returns {SList<T>} - 数组
|
|
556
|
+
*/
|
|
557
|
+
async toSList(): Promise<SList<T>> {
|
|
558
|
+
await this.appendOperations();
|
|
559
|
+
return this._slist;
|
|
560
|
+
}
|
|
561
|
+
/**转换为数组
|
|
562
|
+
* @returns {Array<T>} - 数组
|
|
563
|
+
*/
|
|
564
|
+
async toArray(): Promise<Array<T>> {
|
|
565
|
+
await this.appendOperations();
|
|
566
|
+
return this._slist.toArray();
|
|
567
|
+
}
|
|
568
|
+
}
|
|
569
|
+
|
|
570
|
+
export class SIterator<T> {
|
|
571
|
+
private _index = -1;
|
|
572
|
+
private _list: SList<T>;
|
|
573
|
+
constructor(list: SList<T>) {
|
|
327
574
|
this._list = list;
|
|
328
575
|
}
|
|
329
576
|
/**判断还有没有下一个元素
|
|
577
|
+
* @returns {boolean} - 是否有下一个元素
|
|
330
578
|
*/
|
|
331
579
|
hasNext(): boolean {
|
|
332
580
|
return this._index < this.size() - 1 && this._index >= -1;
|
|
333
581
|
}
|
|
334
582
|
|
|
335
583
|
/**判断当前下标有无元素
|
|
584
|
+
* @returns {boolean} - 是否有当前元素
|
|
336
585
|
*/
|
|
337
586
|
hasCurr(): boolean {
|
|
338
587
|
return this._index < this.size() && this._index >= 0;
|
|
339
588
|
}
|
|
340
589
|
|
|
341
590
|
/**判断还有没有上一个元素
|
|
591
|
+
* @returns {boolean} - 是否有上一个元素
|
|
342
592
|
*/
|
|
343
593
|
hasPre(): boolean {
|
|
344
594
|
return this._index < this.size() + 1 && this._index >= 1;
|
|
345
595
|
}
|
|
346
596
|
|
|
347
597
|
/**返回下一个下标指向的数组内成员,然后下标自加1
|
|
598
|
+
* @returns {T} - 下一个成员
|
|
348
599
|
*/
|
|
349
600
|
next(): T {
|
|
350
|
-
if (this.hasNext())
|
|
351
|
-
return this._list.get(++this._index);
|
|
601
|
+
if (this.hasNext()) return this._list.get(++this._index);
|
|
352
602
|
else
|
|
353
|
-
throw
|
|
603
|
+
throw "一个 SIterator 迭代器的 next 函数出错,可能是在下标到底时还在尝试迭代";
|
|
354
604
|
}
|
|
355
605
|
|
|
356
606
|
/**返回上一个下标指向的数组内成员,然后下标自减1
|
|
607
|
+
* @returns {T} - 上一个成员
|
|
357
608
|
*/
|
|
358
609
|
pre(): T {
|
|
359
|
-
if (this.hasPre())
|
|
360
|
-
return this._list.get(--this._index);
|
|
610
|
+
if (this.hasPre()) return this._list.get(--this._index);
|
|
361
611
|
else
|
|
362
|
-
throw
|
|
612
|
+
throw "一个 SIterator 迭代器的 pre 函数出错,可能是在下标小于1时还在尝试迭代";
|
|
363
613
|
}
|
|
364
614
|
|
|
365
615
|
/**返回当前下标指向的数组内成员
|
|
616
|
+
* @returns {T} - 当前成员
|
|
366
617
|
*/
|
|
367
618
|
curr(): T {
|
|
368
|
-
if (this.hasCurr())
|
|
369
|
-
return this._list.get(this._index);
|
|
619
|
+
if (this.hasCurr()) return this._list.get(this._index);
|
|
370
620
|
else
|
|
371
|
-
throw
|
|
621
|
+
throw "一个 SIterator 迭代器的 curr 函数出错,可能是当前下标不包含元素/当前下标已越界";
|
|
372
622
|
}
|
|
373
623
|
|
|
374
624
|
/**返回遍历长度
|
|
375
|
-
* @returns 遍历长度
|
|
625
|
+
* @returns {number} - 遍历长度
|
|
376
626
|
*/
|
|
377
627
|
size(): number {
|
|
378
628
|
return this._list.size();
|
|
379
629
|
}
|
|
380
630
|
|
|
381
631
|
/**获取下个下标
|
|
382
|
-
* @returns 下个下标
|
|
632
|
+
* @returns {number} - 下个下标
|
|
383
633
|
*/
|
|
384
634
|
nextIndex(): number {
|
|
385
635
|
return this._index + 1;
|
|
386
636
|
}
|
|
387
637
|
|
|
388
638
|
/**获取当前下标
|
|
389
|
-
* @
|
|
639
|
+
* @return {number} - 当前下标
|
|
390
640
|
*/
|
|
391
641
|
currIndex(): number {
|
|
392
642
|
return this._index;
|
|
393
643
|
}
|
|
394
644
|
|
|
395
645
|
/**获取上个下标
|
|
396
|
-
* @returns 上个下标
|
|
646
|
+
* @returns {number} - 上个下标
|
|
397
647
|
*/
|
|
398
648
|
preIndex(): number {
|
|
399
649
|
return this._index - 1;
|
|
400
650
|
}
|
|
401
651
|
|
|
402
652
|
/**判断是否是最后一个
|
|
403
|
-
* @returns 是否是最后一个
|
|
653
|
+
* @returns {boolean} - 是否是最后一个
|
|
404
654
|
*/
|
|
405
655
|
isLast(): boolean {
|
|
406
656
|
return this._index >= this._list.size() - 1;
|
|
407
657
|
}
|
|
408
658
|
|
|
409
659
|
/**设置数组在迭代器当前下标中的内容
|
|
410
|
-
*
|
|
660
|
+
* 返回自身
|
|
661
|
+
* @param {T} val 要设置的内容
|
|
662
|
+
* @returns {void}
|
|
411
663
|
*/
|
|
412
|
-
set(val: T):
|
|
664
|
+
set(val: T): SIterator<T> {
|
|
413
665
|
this._list.set(this._index, val);
|
|
666
|
+
return this;
|
|
414
667
|
}
|
|
415
668
|
|
|
416
669
|
/**删除数组在当前下标的内容,然后将下标移至上一位,改变数组长度
|
|
670
|
+
* @returns {T} - 被删除的内容
|
|
417
671
|
*/
|
|
418
|
-
remove():
|
|
672
|
+
remove(): T {
|
|
673
|
+
let tmp = this._list.get(this._index);
|
|
419
674
|
this._list.remove(this._index);
|
|
675
|
+
this._index--;
|
|
676
|
+
return tmp;
|
|
420
677
|
}
|
|
421
678
|
|
|
422
679
|
/**在当前下标前插入元素,然后将下标移至原元素,即下一位,改变数组长度
|
|
423
|
-
*
|
|
680
|
+
* 返回自身
|
|
681
|
+
* @param {T} obj - 要插入的元素
|
|
682
|
+
* @returns {SIterator<T>} - 自身
|
|
424
683
|
*/
|
|
425
|
-
addPre(obj: T):
|
|
684
|
+
addPre(obj: T): SIterator<T> {
|
|
426
685
|
this._list.insert(this._index, obj);
|
|
427
686
|
this._index++;
|
|
687
|
+
return this;
|
|
428
688
|
}
|
|
429
689
|
|
|
430
690
|
/**在当前下标后插入元素,然后将下标移至新元素,即下一位,改变数组长度
|
|
431
|
-
*
|
|
691
|
+
* 返回自身
|
|
692
|
+
* @param {T} obj - 要插入的元素
|
|
693
|
+
* @returns {SIterator<T>} - 自身
|
|
432
694
|
*/
|
|
433
|
-
addNext(obj: T):
|
|
695
|
+
addNext(obj: T): SIterator<T> {
|
|
434
696
|
this._list.insert(this._index + 1, obj);
|
|
435
697
|
this._index++;
|
|
698
|
+
return this;
|
|
436
699
|
}
|
|
437
700
|
}
|
|
438
701
|
|
|
439
|
-
export class SEntry<K,V>{
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
constructor(key:K,value:V){
|
|
444
|
-
this._key = key;
|
|
445
|
-
this._value = value;
|
|
446
|
-
}
|
|
447
|
-
getKey(){
|
|
448
|
-
return this._key;
|
|
449
|
-
}
|
|
450
|
-
getValue(){
|
|
451
|
-
return this._value;
|
|
452
|
-
}
|
|
702
|
+
export class SEntry<K, V> {
|
|
703
|
+
private _key: K;
|
|
704
|
+
private _value: V;
|
|
453
705
|
|
|
706
|
+
constructor(key: K, value: V) {
|
|
707
|
+
this._key = key;
|
|
708
|
+
this._value = value;
|
|
709
|
+
}
|
|
710
|
+
getKey() {
|
|
711
|
+
return this._key;
|
|
712
|
+
}
|
|
713
|
+
getValue() {
|
|
714
|
+
return this._value;
|
|
715
|
+
}
|
|
454
716
|
|
|
455
717
|
//重载TypeScript操作符
|
|
456
718
|
get key(): K {
|
|
@@ -461,104 +723,105 @@ export class SEntry<K,V>{
|
|
|
461
723
|
return this._value;
|
|
462
724
|
}
|
|
463
725
|
}
|
|
464
|
-
export class SHashMap<K,V>{
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
|
|
726
|
+
export class SHashMap<K, V> {
|
|
727
|
+
private _map: Map<K, V> = new Map();
|
|
728
|
+
|
|
729
|
+
/**构造函数
|
|
730
|
+
* @param {Map<K, V>} [map] - 一个键值对集合
|
|
731
|
+
*/
|
|
732
|
+
constructor(map?: Map<K, V>) {
|
|
733
|
+
if (map == null) return;
|
|
469
734
|
let keys = map.keys();
|
|
470
|
-
for(let key of keys){
|
|
735
|
+
for (let key of keys) {
|
|
471
736
|
let value = map.get(key);
|
|
472
|
-
if(value!=null)
|
|
473
|
-
this.put(key,value);
|
|
737
|
+
if (value != null) this.put(key, value);
|
|
474
738
|
}
|
|
475
739
|
}
|
|
476
740
|
/**添加一个键值对
|
|
477
|
-
*
|
|
478
|
-
* @
|
|
741
|
+
* 返回自身
|
|
742
|
+
* @param {SEntry<K, V>} entry - 键值对
|
|
743
|
+
* @returns {SHashMap<K, V>} - 自身
|
|
479
744
|
*/
|
|
480
|
-
putEntry(entry: SEntry<K, V>): SHashMap<K,V> {
|
|
481
|
-
this._map.set(entry.getKey(),entry.getValue());
|
|
745
|
+
putEntry(entry: SEntry<K, V>): SHashMap<K, V> {
|
|
746
|
+
this._map.set(entry.getKey(), entry.getValue());
|
|
482
747
|
return this;
|
|
483
748
|
}
|
|
484
749
|
|
|
485
750
|
/**获取指定键的值
|
|
486
|
-
* @param key 键
|
|
487
|
-
* @returns 值
|
|
751
|
+
* @param {K} key - 键
|
|
752
|
+
* @returns {V | undefined} - 值
|
|
488
753
|
*/
|
|
489
754
|
get(key: K): V | undefined {
|
|
490
755
|
return this._map.get(key);
|
|
491
756
|
}
|
|
492
757
|
|
|
493
758
|
/**添加一个键值对
|
|
494
|
-
*
|
|
495
|
-
* @param
|
|
496
|
-
* @
|
|
759
|
+
* 返回自身
|
|
760
|
+
* @param {K} key - 键
|
|
761
|
+
* @param {V} value - 值
|
|
762
|
+
* @returns {SHashMap<K, V>} - 自身
|
|
497
763
|
*/
|
|
498
|
-
put(key: K, value: V): SHashMap<K,V> {
|
|
764
|
+
put(key: K, value: V): SHashMap<K, V> {
|
|
499
765
|
this._map.set(key, value);
|
|
500
766
|
return this;
|
|
501
767
|
}
|
|
502
768
|
|
|
503
769
|
/**判断是否存在指定键
|
|
504
|
-
* @param key 键
|
|
505
|
-
* @returns 是否存在
|
|
770
|
+
* @param {K} key - 键
|
|
771
|
+
* @returns {boolean} - 是否存在
|
|
506
772
|
*/
|
|
507
773
|
has(key: K): boolean {
|
|
508
774
|
return this._map.has(key);
|
|
509
775
|
}
|
|
510
776
|
|
|
511
777
|
/**获取所有键值对
|
|
512
|
-
* @returns 键值对列表
|
|
778
|
+
* @returns {SList<SEntry<K, V>>} - 键值对列表
|
|
513
779
|
*/
|
|
514
780
|
entrys(): SList<SEntry<K, V>> {
|
|
515
781
|
let list = new SList<SEntry<K, V>>();
|
|
516
|
-
for (const [key, value] of this._map)
|
|
517
|
-
list.push(new SEntry(key, value));
|
|
782
|
+
for (const [key, value] of this._map) list.push(new SEntry(key, value));
|
|
518
783
|
return list;
|
|
519
784
|
}
|
|
520
785
|
|
|
521
786
|
/**获取所有键
|
|
522
|
-
* @returns 键列表
|
|
787
|
+
* @returns {SList<K>} - 键列表
|
|
523
788
|
*/
|
|
524
789
|
keys(): SList<K> {
|
|
525
790
|
let list = new SList<K>();
|
|
526
791
|
let it = this._map.keys();
|
|
527
|
-
for(let key of it)
|
|
528
|
-
list.push(key);
|
|
792
|
+
for (let key of it) list.push(key);
|
|
529
793
|
return list;
|
|
530
794
|
}
|
|
531
795
|
|
|
532
796
|
/**获取所有值
|
|
533
|
-
* @returns 值列表
|
|
797
|
+
* @returns {SList<V>} - 值列表
|
|
534
798
|
*/
|
|
535
799
|
values(): SList<V> {
|
|
536
800
|
let list = new SList<V>();
|
|
537
801
|
let it = this._map.values();
|
|
538
|
-
for(let val of it)
|
|
539
|
-
list.push(val);
|
|
802
|
+
for (let val of it) list.push(val);
|
|
540
803
|
return list;
|
|
541
804
|
}
|
|
542
805
|
|
|
543
|
-
|
|
544
|
-
* @returns 迭代器
|
|
806
|
+
/**转换为entry数组,并获取迭代器
|
|
807
|
+
* @returns {SIterator<SEntry<K, V>>} - 迭代器
|
|
545
808
|
*/
|
|
546
809
|
iterator(): SIterator<SEntry<K, V>> {
|
|
547
810
|
return this.entrys().iterator();
|
|
548
811
|
}
|
|
549
812
|
|
|
550
813
|
/**删除指定键的键值对
|
|
551
|
-
* @param key 键
|
|
552
|
-
* @returns
|
|
814
|
+
* @param {K} key - 键
|
|
815
|
+
* @returns {V | undefined} - 被删除的值
|
|
553
816
|
*/
|
|
554
|
-
remove(key: K): V|undefined {
|
|
817
|
+
remove(key: K): V | undefined {
|
|
555
818
|
let out = this._map.get(key);
|
|
556
819
|
this._map.delete(key);
|
|
557
820
|
return out;
|
|
558
821
|
}
|
|
559
822
|
|
|
560
823
|
/**清空哈希表
|
|
561
|
-
* @returns 自身
|
|
824
|
+
* @returns {SHashMap<K, V>} - 自身
|
|
562
825
|
*/
|
|
563
826
|
clear(): SHashMap<K, V> {
|
|
564
827
|
this._map.clear();
|
|
@@ -566,115 +829,137 @@ export class SHashMap<K,V>{
|
|
|
566
829
|
}
|
|
567
830
|
|
|
568
831
|
/**判断哈希表是否为空
|
|
569
|
-
* @returns 是否为空
|
|
832
|
+
* @returns {boolean} - 是否为空
|
|
570
833
|
*/
|
|
571
834
|
isEmpty(): boolean {
|
|
572
835
|
return this.isEmpty();
|
|
573
836
|
}
|
|
574
837
|
|
|
575
838
|
/**判断是否存在指定值
|
|
576
|
-
* @param val 值
|
|
577
|
-
* @returns 是否存在
|
|
839
|
+
* @param {V} val - 值
|
|
840
|
+
* @returns {boolean} - 是否存在
|
|
578
841
|
*/
|
|
579
842
|
containsValue(val: V): boolean {
|
|
580
843
|
return this.values().contains(val);
|
|
581
844
|
}
|
|
582
845
|
|
|
583
846
|
/**判断是否存在指定键
|
|
584
|
-
* @param key 键
|
|
585
|
-
* @returns 是否存在
|
|
847
|
+
* @param {K} key - 键
|
|
848
|
+
* @returns {boolean} - 是否存在
|
|
586
849
|
*/
|
|
587
850
|
containsKey(key: K): boolean {
|
|
588
851
|
return this._map.has(key);
|
|
589
852
|
}
|
|
590
853
|
|
|
591
|
-
|
|
592
|
-
*
|
|
593
|
-
* @param
|
|
594
|
-
* @
|
|
854
|
+
/**加载指定键的值,若不存在则添加默认值
|
|
855
|
+
* 返回键值
|
|
856
|
+
* @param {K} key - 键
|
|
857
|
+
* @param {V} def - 默认值
|
|
858
|
+
* @returns {V} - 值
|
|
595
859
|
*/
|
|
596
860
|
load(key: K, def: V): V {
|
|
597
|
-
if (this.containsKey(key))
|
|
598
|
-
return this.get(key)!;
|
|
861
|
+
if (this.containsKey(key)) return this.get(key)!;
|
|
599
862
|
this.put(key, def);
|
|
600
863
|
return def;
|
|
601
864
|
}
|
|
602
865
|
|
|
603
866
|
/**合并另一个哈希表,可选择是否覆盖已有键值对
|
|
604
|
-
* @param nmap 另一个哈希表
|
|
605
|
-
* @param isCover 是否覆盖已有键值对,默认为 true
|
|
606
|
-
* @returns 自身
|
|
867
|
+
* @param {SHashMap<K, V>} nmap - 另一个哈希表
|
|
868
|
+
* @param {boolean} [isCover=true] - 是否覆盖已有键值对,默认为 true
|
|
869
|
+
* @returns {SHashMap<K, V>} - 自身
|
|
607
870
|
*/
|
|
608
871
|
merge(nmap: SHashMap<K, V>, isCover: boolean = true): SHashMap<K, V> {
|
|
609
872
|
var it = nmap.iterator();
|
|
610
873
|
while (it.hasNext()) {
|
|
611
|
-
if (isCover)
|
|
612
|
-
this.putEntry(it.next());
|
|
874
|
+
if (isCover) this.putEntry(it.next());
|
|
613
875
|
else {
|
|
614
876
|
var entry = it.next();
|
|
615
|
-
if (!this.containsKey(entry.getKey()))
|
|
616
|
-
this.putEntry(entry);
|
|
877
|
+
if (!this.containsKey(entry.getKey())) this.putEntry(entry);
|
|
617
878
|
}
|
|
618
879
|
}
|
|
619
880
|
return this;
|
|
620
881
|
}
|
|
621
882
|
|
|
622
883
|
/**获取哈希表大小(键的数量)
|
|
623
|
-
* @returns
|
|
884
|
+
* @returns {number} - 大小
|
|
624
885
|
*/
|
|
625
886
|
size(): number {
|
|
626
887
|
return this._map.size;
|
|
627
888
|
}
|
|
628
889
|
/**对哈希表的每一个键值对进行加工,返回加工完成的键值对组成的新哈希表
|
|
629
|
-
* @param func 加工函数
|
|
630
|
-
* @returns 新哈希表
|
|
890
|
+
* @param {MapCallback<SEntry<K, V>, SEntry<OK, OV>>} func - 加工函数
|
|
891
|
+
* @returns {SHashMap<OK, OV>} - 新哈希表
|
|
631
892
|
*/
|
|
632
|
-
map<OK
|
|
893
|
+
map<OK, OV>(
|
|
894
|
+
func: MapCallback<SEntry<K, V>, SEntry<OK, OV>>
|
|
895
|
+
): SHashMap<OK, OV> {
|
|
633
896
|
let nmap = new SHashMap<OK, OV>();
|
|
634
897
|
this.entrys()
|
|
635
|
-
.map(
|
|
636
|
-
.each(val=>nmap.putEntry(val));
|
|
898
|
+
.map(func)
|
|
899
|
+
.each((val) => nmap.putEntry(val));
|
|
637
900
|
return nmap;
|
|
638
901
|
}
|
|
639
902
|
|
|
640
903
|
/**返回符合条件的键值对组成的新哈希表
|
|
641
|
-
* @param func 条件函数
|
|
642
|
-
* @returns 新哈希表
|
|
904
|
+
* @param {FiltCallback<SEntry<K, V>>): boolean} func - 条件函数
|
|
905
|
+
* @returns {SHashMap<K, V>} - 新哈希表
|
|
643
906
|
*/
|
|
644
|
-
filt(func:
|
|
907
|
+
filt(func: FiltCallback<SEntry<K, V>>): SHashMap<K, V> {
|
|
645
908
|
let nmap = new SHashMap<K, V>();
|
|
646
909
|
this.entrys()
|
|
647
|
-
.filt(
|
|
648
|
-
.each(val=>nmap.putEntry(val));
|
|
910
|
+
.filt(func)
|
|
911
|
+
.each((val) => nmap.putEntry(val));
|
|
649
912
|
return nmap;
|
|
650
913
|
}
|
|
651
914
|
|
|
652
915
|
/**遍历哈希表的每一个键值对
|
|
653
|
-
* @param func
|
|
654
|
-
* @returns 自身
|
|
916
|
+
* @param {EachCallback<SEntry<K, V>>} func - 遍历函数
|
|
917
|
+
* @returns {SHashMap<K, V>} - 自身
|
|
655
918
|
*/
|
|
656
|
-
each(func:
|
|
919
|
+
each(func: EachCallback<SEntry<K, V>>): SHashMap<K, V> {
|
|
657
920
|
this.entrys().each(func);
|
|
658
921
|
return this;
|
|
659
922
|
}
|
|
660
923
|
|
|
661
|
-
|
|
662
|
-
*
|
|
663
|
-
* @param
|
|
664
|
-
* @
|
|
665
|
-
* @returns 结果
|
|
924
|
+
/**对哈希表进行统计
|
|
925
|
+
* @param {O} init - 初始值
|
|
926
|
+
* @param {AggrCallback<SEntry<K, V>,O>} func - 统计函数
|
|
927
|
+
* @returns {O} - 统计结果
|
|
666
928
|
*/
|
|
667
|
-
|
|
668
|
-
return this.entrys().
|
|
929
|
+
aggr<O>(init: O, func: AggrCallback<SEntry<K, V>, O>): O {
|
|
930
|
+
return this.entrys().aggr(init, func);
|
|
669
931
|
}
|
|
670
932
|
|
|
933
|
+
/**对哈希表进行缩减
|
|
934
|
+
* @param {AggrCallback<SEntry<K, V>,V>} func - 缩减函数
|
|
935
|
+
* @param {V} init - 初始值
|
|
936
|
+
* @returns {V} - 缩减结果
|
|
937
|
+
*/
|
|
938
|
+
reduce(func: AggrCallback<SEntry<K, V>, V>, init?: V): V | null {
|
|
939
|
+
if (this.isEmpty()) {
|
|
940
|
+
if (init === undefined) return null;
|
|
941
|
+
return init;
|
|
942
|
+
}
|
|
943
|
+
|
|
944
|
+
let entries = this.entrys();
|
|
945
|
+
let result: V;
|
|
946
|
+
if (init === undefined) {
|
|
947
|
+
result = entries.get(0).getValue();
|
|
948
|
+
entries.shift();
|
|
949
|
+
} else result = init;
|
|
950
|
+
|
|
951
|
+
entries.each((val, index, list) => {
|
|
952
|
+
result = func(result, val, index, list);
|
|
953
|
+
});
|
|
954
|
+
return result;
|
|
955
|
+
}
|
|
671
956
|
|
|
672
957
|
//重载TypeScript操作符
|
|
673
958
|
[Symbol.iterator]() {
|
|
674
959
|
let it = this.iterator();
|
|
675
960
|
|
|
676
961
|
return {
|
|
677
|
-
next(): { value: SEntry<K,V
|
|
962
|
+
next(): { value: SEntry<K, V> | null; done: boolean } {
|
|
678
963
|
if (it.hasNext()) {
|
|
679
964
|
return { value: it.next(), done: false };
|
|
680
965
|
} else {
|
|
@@ -685,8 +970,8 @@ export class SHashMap<K,V>{
|
|
|
685
970
|
}
|
|
686
971
|
}
|
|
687
972
|
|
|
688
|
-
class SKVC{
|
|
689
|
-
|
|
973
|
+
class SKVC {
|
|
974
|
+
private stringMap: SHashMap<string, string> = new SHashMap();
|
|
690
975
|
|
|
691
|
-
|
|
692
|
-
}
|
|
976
|
+
constructor() {}
|
|
977
|
+
}
|