@zwa73/utils 1.0.1 → 1.0.3

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.
@@ -0,0 +1,669 @@
1
+ type struct = number|string;
2
+
3
+ export class SList<T> {
4
+ private _arr:Array<T>;
5
+ constructor(obj?:Array<T>|number){
6
+ if(typeof obj == 'number')
7
+ this._arr=new Array<T>(obj);
8
+ else if(Array.isArray(obj))
9
+ this._arr = obj;
10
+ else
11
+ this._arr=[];
12
+ }
13
+ /**返回数组长度*/
14
+ size():number{
15
+ return this._arr.length;
16
+ }
17
+
18
+ /**获取指定下标的元素
19
+ * @param index 下标
20
+ */
21
+ get(index: number):T{
22
+ return this._arr[index];
23
+ }
24
+
25
+ /**设置指定下标的元素
26
+ * 返回自身 改变自身
27
+ * @param index 下标
28
+ * @param value 值
29
+ * @returns 自身
30
+ */
31
+ set(index: number, value: T):SList<T>{
32
+ this._arr[index] = value;
33
+ return this;
34
+ }
35
+
36
+ /**连接两个数组
37
+ * @param list 目标数组
38
+ * @returns 新数组
39
+ */
40
+ concat(list: SList<T>):SList<T>{
41
+ return new SList<T>(this._arr.concat(list._arr));
42
+ }
43
+
44
+ /**在数组末尾添加一个元素
45
+ * 返回自身 改变自身
46
+ * @param value 值
47
+ */
48
+ push(value: T):SList<T>{
49
+ this._arr.push(value);
50
+ return this;
51
+ }
52
+
53
+ /**在数组末尾添加一个数组
54
+ * 改变自身
55
+ * @param list 目标数组
56
+ * @returns 自身
57
+ */
58
+ pushArr(list: SList<T>) {
59
+ this._arr = this._arr.concat(list._arr);
60
+ return this;
61
+ }
62
+
63
+ /**截取从起始点到结束点之前的一段数组
64
+ * @param strat 开始点
65
+ * @param end 结束点
66
+ * @returns 新数组
67
+ */
68
+ slice(strat: number, end: number):SList<T>{
69
+ return new SList(this._arr.slice(strat, end));
70
+ }
71
+
72
+ /**翻转数组
73
+ * 改变自身
74
+ * @returns 自身
75
+ */
76
+ reverse():SList<T>{
77
+ this._arr.reverse();
78
+ return this;
79
+ }
80
+
81
+ /**将SList转换为数组 */
82
+ toArray():Array<T>{
83
+ return ([] as T[]).concat(this._arr);
84
+ }
85
+
86
+ /**返回指定元素在数组中首次出现的位置
87
+ * @param value 目标元素
88
+ */
89
+ indexOf(value: T):number{
90
+ return this._arr.indexOf(value);
91
+ }
92
+
93
+ /**返回指定元素在数组中最后一次出现的位置
94
+ * @param value 目标元素
95
+ */
96
+ lastIndexOf(value: T):number{
97
+ return this._arr.lastIndexOf(value);
98
+ }
99
+
100
+ /**判断数组中是否包含指定元素
101
+ * @param value 目标元素
102
+ */
103
+ contains(value: T):boolean{
104
+ return this._arr.includes(value);
105
+ }
106
+
107
+ /**获取迭代器 */
108
+ iterator():SIterator<T> {
109
+ return new SIterator<T>(this);
110
+ }
111
+
112
+ /**从index开始删除count个元素
113
+ * 改变自身
114
+ * @param index 起始点
115
+ * @param count 数量
116
+ * @returns 自身
117
+ */
118
+ removeRange(index: number, count: number): SList<T> {
119
+ this._arr.splice(index, count);
120
+ return this;
121
+ }
122
+
123
+ /**删除对应下标下的元素
124
+ * 改变自身
125
+ * @param i 下标
126
+ * @returns 自身
127
+ */
128
+ remove(i: number): SList<T> {
129
+ if (i >= 0 && i < this.size())
130
+ this._arr.splice(i, 1);
131
+ return this;
132
+ }
133
+
134
+ /**删除第一个匹配的项目(改变长度)
135
+ * 改变自身
136
+ * @param obj 需删除目标
137
+ * @returns 自身
138
+ */
139
+ removeMember(obj: T): SList<T> {
140
+ let index = this.indexOf(obj);
141
+ if (index > -1)
142
+ this._arr.splice(index, 1);
143
+ return this;
144
+ }
145
+
146
+ /**删除所有匹配的项目
147
+ * 改变自身
148
+ * @param obj 需删除目标
149
+ * @returns 自身
150
+ */
151
+ removeAllMember(obj: T): SList<T> {
152
+ while (this.contains(obj))
153
+ this.removeMember(obj);
154
+ return this;
155
+ }
156
+ /**在这个下标对应的元素前添加一个元素
157
+ * 改变自身
158
+ * @param index 下标
159
+ * @param obj 添加对象
160
+ * @returns 自身
161
+ */
162
+ insert(index: number, obj: T): SList<T> {
163
+ this._arr.splice(index, 0, obj);
164
+ return this;
165
+ }
166
+
167
+ /**在这个下标对应的元素前添加一组元素
168
+ * 改变自身
169
+ * @param index 下标
170
+ * @param obj 添加对象
171
+ * @returns 自身
172
+ */
173
+ insertRange(index: number, obj: SList<T>): SList<T> {
174
+ this._arr.splice(index, 0, ...obj._arr);
175
+ return this;
176
+ }
177
+
178
+ /**克隆数组
179
+ * @returns 新数组
180
+ */
181
+ clone(): SList<T> {
182
+ return new SList(([] as T[]).concat(this._arr));
183
+ }
184
+
185
+ /**判断数组是否为空
186
+ */
187
+ isEmpty(): boolean {
188
+ return this.size()==0;
189
+ }
190
+
191
+ /**删除数组中的重复元素
192
+ * 改变自身
193
+ * @returns 自身
194
+ */
195
+ removeDuplicates():SList<T> {
196
+ this._arr = Array.from(new Set(this._arr));
197
+ return this;
198
+ }
199
+
200
+ /**交集
201
+ * @param lists 数组列表
202
+ * @returns 新数组
203
+ */
204
+ intersection(...lists: SList<T>[]): SList<T> {
205
+ let nlist = this.clone().removeDuplicates();
206
+
207
+ for(let list of lists)
208
+ nlist = list.filt( val => nlist.contains(val));
209
+
210
+ return nlist;
211
+ }
212
+ /**并集
213
+ * @param lists 数组列表
214
+ * @returns 新数组
215
+ */
216
+ union(...lists: SList<T>[]): SList<T> {
217
+ let nlist = this.clone().removeDuplicates();
218
+
219
+ for(let list of lists){
220
+ list.each( (val) => {
221
+ if(!nlist.contains(val))
222
+ nlist.push(val);
223
+ });
224
+ }
225
+
226
+ return nlist;
227
+ }
228
+
229
+ /**返回符合条件的成员组成的新数组
230
+ * @param func 条件函数
231
+ * @returns 新数组
232
+ */
233
+ filt(func: (value: T) => boolean): SList<T> {
234
+ let nlist = new SList<T>(this.size());
235
+ let length = 0;
236
+ let it = this.iterator();
237
+ while (it.hasNext()) {
238
+ let tmpObj = it.next();
239
+ if (func(tmpObj)) {
240
+ nlist.set(length, tmpObj);
241
+ length++;
242
+ }
243
+ }
244
+ return nlist.slice(0, length);
245
+ }
246
+
247
+ /**遍历数组的每一个元素
248
+ * @param func 处理函数
249
+ * @returns 自身
250
+ */
251
+ each(func: (value: T) => void): SList<T> {
252
+ let it = this.iterator();
253
+ while (it.hasNext())
254
+ func(it.next());
255
+ return this;
256
+ }
257
+
258
+ /**对数组的每一个元素进行加工,返回加工完成的成员组成的新数组
259
+ * @param func 加工函数
260
+ * @returns 新数组
261
+ */
262
+ map<O>(func: (value: T) => O): SList<O> {
263
+ let nlist = new SList<O>(this.size());
264
+ let it = this.iterator();
265
+ while (it.hasNext())
266
+ nlist.set(it.nextIndex(), func(it.next()));
267
+ return nlist;
268
+ }
269
+
270
+ /**对数组进行排序
271
+ * 如函数返回值大于 0 则将 x 排在 y 后面,小于 0 则将 x 排在 y 前面
272
+ * 改变自身
273
+ * @param func 比较函数
274
+ * @returns 自身
275
+ */
276
+ sort(func: (a: T, b: T) => number): SList<T> {
277
+ this._arr.sort((x, y) => func(x, y));
278
+ return this;
279
+ }
280
+ /**对数组进行统计
281
+ * @param init 初始值
282
+ * @param func 统计函数
283
+ * @returns 结果
284
+ */
285
+ public stat<O>(init: O, func: (accumulator: O, currentValue: T) => O): O {
286
+ let it = this.iterator();
287
+ let tmpObj = init;
288
+ while (it.hasNext())
289
+ tmpObj = func(tmpObj, it.next());
290
+ return tmpObj;
291
+ }
292
+
293
+ //重载TypeScript操作符
294
+ [Symbol.iterator]() {
295
+ let it = this.iterator();
296
+
297
+ return {
298
+ next(): { value: T|null; done: boolean } {
299
+ if (it.hasNext()) {
300
+ return { value: it.next(), done: false };
301
+ } else {
302
+ return { value: null, done: true };
303
+ }
304
+ },
305
+ };
306
+ }
307
+ }
308
+
309
+ export class SIterator<T>{
310
+ private _index=-1;
311
+ private _list:SList<T>;
312
+ constructor(list:SList<T>){
313
+ this._list = list;
314
+ }
315
+ /**判断还有没有下一个元素
316
+ */
317
+ hasNext(): boolean {
318
+ return this._index < this.size() - 1 && this._index >= -1;
319
+ }
320
+
321
+ /**判断当前下标有无元素
322
+ */
323
+ hasCurr(): boolean {
324
+ return this._index < this.size() && this._index >= 0;
325
+ }
326
+
327
+ /**判断还有没有上一个元素
328
+ */
329
+ hasPre(): boolean {
330
+ return this._index < this.size() + 1 && this._index >= 1;
331
+ }
332
+
333
+ /**返回下一个下标指向的数组内成员,然后下标自加1
334
+ */
335
+ next(): T {
336
+ if (this.hasNext())
337
+ return this._list.get(++this._index);
338
+ else
339
+ throw ("一个 SIterator 迭代器的 next 函数出错,可能是在下标到底时还在尝试迭代");
340
+ }
341
+
342
+ /**返回上一个下标指向的数组内成员,然后下标自减1
343
+ */
344
+ pre(): T {
345
+ if (this.hasPre())
346
+ return this._list.get(--this._index);
347
+ else
348
+ throw ("一个 SIterator 迭代器的 pre 函数出错,可能是在下标小于1时还在尝试迭代");
349
+ }
350
+
351
+ /**返回当前下标指向的数组内成员
352
+ */
353
+ curr(): T {
354
+ if (this.hasCurr())
355
+ return this._list.get(this._index);
356
+ else
357
+ throw ("一个 SIterator 迭代器的 curr 函数出错,可能是当前下标不包含元素/当前下标已越界");
358
+ }
359
+
360
+ /**返回遍历长度
361
+ * @returns 遍历长度
362
+ */
363
+ size(): number {
364
+ return this._list.size();
365
+ }
366
+
367
+ /**获取下个下标
368
+ * @returns 下个下标
369
+ */
370
+ nextIndex(): number {
371
+ return this._index + 1;
372
+ }
373
+
374
+ /**获取当前下标
375
+ * @returns 当前下标
376
+ */
377
+ currIndex(): number {
378
+ return this._index;
379
+ }
380
+
381
+ /**获取上个下标
382
+ * @returns 上个下标
383
+ */
384
+ preIndex(): number {
385
+ return this._index - 1;
386
+ }
387
+
388
+ /**判断是否是最后一个
389
+ * @returns 是否是最后一个
390
+ */
391
+ isLast(): boolean {
392
+ return this._index >= this._list.size() - 1;
393
+ }
394
+
395
+ /**设置数组在迭代器当前下标中的内容
396
+ * @param val 要设置的值
397
+ */
398
+ set(val: T): void {
399
+ this._list.set(this._index, val);
400
+ }
401
+
402
+ /**删除数组在当前下标的内容,然后将下标移至上一位,改变数组长度
403
+ */
404
+ remove(): void {
405
+ this._list.remove(this._index);
406
+ }
407
+
408
+ /**在当前下标前插入元素,然后将下标移至原元素,即下一位,改变数组长度
409
+ * @param obj 要插入的元素
410
+ */
411
+ addPre(obj: T): void {
412
+ this._list.insert(this._index, obj);
413
+ this._index++;
414
+ }
415
+
416
+ /**在当前下标后插入元素,然后将下标移至新元素,即下一位,改变数组长度
417
+ * @param obj 要插入的元素
418
+ */
419
+ addNext(obj: T): void {
420
+ this._list.insert(this._index + 1, obj);
421
+ this._index++;
422
+ }
423
+ }
424
+
425
+ export class SEntry<K,V>{
426
+ private _key:K;
427
+ private _value:V;
428
+
429
+ constructor(key:K,value:V){
430
+ this._key = key;
431
+ this._value = value;
432
+ }
433
+ getKey(){
434
+ return this._key;
435
+ }
436
+ getValue(){
437
+ return this._value;
438
+ }
439
+
440
+
441
+ //重载TypeScript操作符
442
+ get key(): K {
443
+ return this.getKey();
444
+ }
445
+
446
+ get value(): V {
447
+ return this._value;
448
+ }
449
+ }
450
+ export class SHashMap<K,V>{
451
+ private _map:Map<K,V> = new Map();
452
+ constructor(){}
453
+ /**添加一个键值对
454
+ * @param entry 键值对
455
+ * @returns 自身
456
+ */
457
+ putEntry(entry: SEntry<K, V>): SHashMap<K,V> {
458
+ this._map.set(entry.getKey(),entry.getValue());
459
+ return this;
460
+ }
461
+
462
+ /**获取指定键的值
463
+ * @param key 键
464
+ * @returns 值
465
+ */
466
+ get(key: K): V | undefined {
467
+ return this._map.get(key);
468
+ }
469
+
470
+ /**添加一个键值对
471
+ * @param key 键
472
+ * @param value 值
473
+ * @returns 自身
474
+ */
475
+ put(key: K, value: V): SHashMap<K,V> {
476
+ this._map.set(key, value);
477
+ return this;
478
+ }
479
+
480
+ /**判断是否存在指定键
481
+ * @param key 键
482
+ * @returns 是否存在
483
+ */
484
+ has(key: K): boolean {
485
+ return this._map.has(key);
486
+ }
487
+
488
+ /**获取所有键值对
489
+ * @returns 键值对列表
490
+ */
491
+ entrys(): SList<SEntry<K, V>> {
492
+ let list = new SList<SEntry<K, V>>();
493
+ for (const [key, value] of this._map)
494
+ list.push(new SEntry(key, value));
495
+ return list;
496
+ }
497
+
498
+ /**获取所有键
499
+ * @returns 键列表
500
+ */
501
+ keys(): SList<K> {
502
+ let list = new SList<K>();
503
+ let it = this._map.keys();
504
+ for(let key of it)
505
+ list.push(key);
506
+ return list;
507
+ }
508
+
509
+ /**获取所有值
510
+ * @returns 值列表
511
+ */
512
+ values(): SList<V> {
513
+ let list = new SList<V>();
514
+ let it = this._map.values();
515
+ for(let val of it)
516
+ list.push(val);
517
+ return list;
518
+ }
519
+
520
+ /**获取迭代器
521
+ * @returns 迭代器
522
+ */
523
+ iterator(): SIterator<SEntry<K, V>> {
524
+ return this.entrys().iterator();
525
+ }
526
+
527
+ /**删除指定键的键值对
528
+ * @param key 键
529
+ * @returns 删除的值
530
+ */
531
+ remove(key: K): V|undefined {
532
+ let out = this._map.get(key);
533
+ this._map.delete(key);
534
+ return out;
535
+ }
536
+
537
+ /**清空哈希表
538
+ * @returns 自身
539
+ */
540
+ clear(): SHashMap<K, V> {
541
+ this._map.clear();
542
+ return this;
543
+ }
544
+
545
+ /**判断哈希表是否为空
546
+ * @returns 是否为空
547
+ */
548
+ isEmpty(): boolean {
549
+ return this.isEmpty();
550
+ }
551
+
552
+ /**判断是否存在指定值
553
+ * @param val 值
554
+ * @returns 是否存在
555
+ */
556
+ containsValue(val: V): boolean {
557
+ return this.values().contains(val);
558
+ }
559
+
560
+ /**判断是否存在指定键
561
+ * @param key 键
562
+ * @returns 是否存在
563
+ */
564
+ containsKey(key: K): boolean {
565
+ return this._map.has(key);
566
+ }
567
+
568
+ /**加载指定键的值,若不存在则添加默认值并返回默认值
569
+ * @param key 键
570
+ * @param def 默认值
571
+ * @returns 值或默认值
572
+ */
573
+ load(key: K, def: V): V {
574
+ if (this.containsKey(key))
575
+ return this.get(key)!;
576
+ this.put(key, def);
577
+ return def;
578
+ }
579
+
580
+ /**合并另一个哈希表,可选择是否覆盖已有键值对
581
+ * @param nmap 另一个哈希表
582
+ * @param isCover 是否覆盖已有键值对,默认为 true
583
+ * @returns 自身
584
+ */
585
+ merge(nmap: SHashMap<K, V>, isCover: boolean = true): SHashMap<K, V> {
586
+ var it = nmap.iterator();
587
+ while (it.hasNext()) {
588
+ if (isCover)
589
+ this.putEntry(it.next());
590
+ else {
591
+ var entry = it.next();
592
+ if (!this.containsKey(entry.getKey()))
593
+ this.putEntry(entry);
594
+ }
595
+ }
596
+ return this;
597
+ }
598
+
599
+ /**获取哈希表大小(键的数量)
600
+ * @returns 大小(键的数量)
601
+ */
602
+ size(): number {
603
+ return this._map.size;
604
+ }
605
+ /**对哈希表的每一个键值对进行加工,返回加工完成的键值对组成的新哈希表
606
+ * @param func 加工函数
607
+ * @returns 新哈希表
608
+ */
609
+ map<OK extends struct, OV>(func: (entry: SEntry<K, V>) => SEntry<OK, OV>): SHashMap<OK, OV> {
610
+ let nmap = new SHashMap<OK, OV>();
611
+ this.entrys()
612
+ .map(val=>func(val))
613
+ .each(val=>nmap.putEntry(val));
614
+ return nmap;
615
+ }
616
+
617
+ /**返回符合条件的键值对组成的新哈希表
618
+ * @param func 条件函数
619
+ * @returns 新哈希表
620
+ */
621
+ filt(func: (entry: SEntry<K, V>) => boolean): SHashMap<K, V> {
622
+ let nmap = new SHashMap<K, V>();
623
+ this.entrys()
624
+ .filt(val=>func(val))
625
+ .each(val=>nmap.putEntry(val));
626
+ return nmap;
627
+ }
628
+
629
+ /**遍历哈希表的每一个键值对
630
+ * @param func 处理函数
631
+ * @returns 自身
632
+ */
633
+ each(func: (entry: SEntry<K, V>) => void): SHashMap<K,V> {
634
+ this.entrys().each(func);
635
+ return this;
636
+ }
637
+
638
+ /**
639
+ * 对哈希表进行统计
640
+ * @param init 初始值
641
+ * @param func 统计函数
642
+ * @returns 结果
643
+ */
644
+ stat<O>(init: O, func: (accumulator: O, entry: SEntry<K, V>) => O): O {
645
+ return this.entrys().stat(init, func);
646
+ }
647
+
648
+
649
+ //重载TypeScript操作符
650
+ [Symbol.iterator]() {
651
+ let it = this.iterator();
652
+
653
+ return {
654
+ next(): { value: SEntry<K,V>|null; done: boolean } {
655
+ if (it.hasNext()) {
656
+ return { value: it.next(), done: false };
657
+ } else {
658
+ return { value: null, done: true };
659
+ }
660
+ },
661
+ };
662
+ }
663
+ }
664
+
665
+ class SKVC{
666
+ private stringMap:SHashMap<string,string> = new SHashMap();
667
+
668
+ constructor(){}
669
+ }