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