@tspro/ts-utils-lib 1.13.0 → 1.14.0

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/CHANGELOG.md CHANGED
@@ -1,5 +1,13 @@
1
1
  # Changelog
2
2
 
3
+ ## [1.14.0] - 2025-10-19
4
+ ### Added
5
+ - Map1, Map2 and Map3.getOrCreate() accepts value and creator.
6
+ - SignedIndexArray<ELEM>
7
+
8
+ ## Fixed
9
+ - Map1, Map2 and Map3.getOrCreate()
10
+
3
11
  ## [1.13.0] - 2025-10-18
4
12
  ### Added
5
13
  - Added even more functionality to Map1, Map2 and Map3.
package/dist/index.d.mts CHANGED
@@ -446,6 +446,48 @@ declare class SmallIntCache<V> {
446
446
  clear(): void;
447
447
  }
448
448
 
449
+ /**
450
+ * An array-like structure for signed indexes, including negatives.
451
+ */
452
+ declare class SignedIndexArray<EL> {
453
+ private posEl;
454
+ private hasPos;
455
+ private negEl;
456
+ private hasNeg;
457
+ private elCount;
458
+ private static toNegIndex;
459
+ constructor();
460
+ constructor(arr: SignedIndexArray<EL>);
461
+ constructor(entries: Iterable<[number, EL]>);
462
+ get size(): number;
463
+ has(id: number): boolean;
464
+ set(id: number, el: EL): void;
465
+ get(id: number): EL | undefined;
466
+ getOrDefault(id: number, defaultValue: EL): EL;
467
+ getOrCreate(id: number, value: EL): EL;
468
+ getOrCreate(id: number, creator: () => EL): EL;
469
+ delete(id: number): boolean;
470
+ clear(): void;
471
+ forEach(callbackfn: (el: EL, id: number, arr: SignedIndexArray<EL>) => void, thisArg?: any): void;
472
+ indices(): IterableIterator<number>;
473
+ indicesArray(): number[];
474
+ values(): IterableIterator<EL>;
475
+ valuesArray(): EL[];
476
+ entries(): IterableIterator<[number, EL]>;
477
+ entriesArray(): [number, EL][];
478
+ [Symbol.iterator](): Generator<[number, EL], void, any>;
479
+ clone(): SignedIndexArray<EL>;
480
+ merge(other: SignedIndexArray<EL>, conflictResolver?: (oldValue: EL, newValue: EL, id: number) => EL): this;
481
+ some(fn: (el: EL, id: number) => boolean): boolean;
482
+ every(fn: (value: EL, key1: number) => boolean): boolean;
483
+ filter(fn: (value: EL, key1: number) => boolean): SignedIndexArray<EL>;
484
+ reduce(fn: (acc: EL, el: EL, id: number) => EL): EL;
485
+ reduce<R>(fn: (acc: R, el: EL, id: number) => R, init: R): R;
486
+ mapToArray<R>(fn: (value: EL, key1: number) => R): R[];
487
+ map<R = EL>(fn: (value: EL, key1: number) => R): SignedIndexArray<R>;
488
+ toString(): string;
489
+ }
490
+
449
491
  declare class Map1<KEY1, VALUE> {
450
492
  private map1;
451
493
  constructor();
@@ -455,6 +497,7 @@ declare class Map1<KEY1, VALUE> {
455
497
  set(key1: KEY1, value: VALUE): VALUE;
456
498
  get(key1: KEY1): VALUE | undefined;
457
499
  getOrDefault(key1: KEY1, defaultValue: VALUE): VALUE;
500
+ getOrCreate(key1: KEY1, value: VALUE): VALUE;
458
501
  getOrCreate(key1: KEY1, creator: () => VALUE): VALUE;
459
502
  delete(key1: KEY1): boolean;
460
503
  clear(): void;
@@ -487,6 +530,7 @@ declare class Map2<KEY1, KEY2, VALUE> {
487
530
  set(key1: KEY1, key2: KEY2, value: VALUE): VALUE;
488
531
  get(key1: KEY1, key2: KEY2): VALUE | undefined;
489
532
  getOrDefault(key1: KEY1, key2: KEY2, defaultValue: VALUE): VALUE;
533
+ getOrCreate(key1: KEY1, key2: KEY2, value: VALUE): VALUE;
490
534
  getOrCreate(key1: KEY1, key2: KEY2, creator: () => VALUE): VALUE;
491
535
  delete(key1: KEY1): boolean;
492
536
  delete(key1: KEY1, key2: KEY2): boolean;
@@ -520,6 +564,7 @@ declare class Map3<KEY1, KEY2, KEY3, VALUE> {
520
564
  set(key1: KEY1, key2: KEY2, key3: KEY3, value: VALUE): VALUE;
521
565
  get(key1: KEY1, key2: KEY2, key3: KEY3): VALUE | undefined;
522
566
  getOrDefault(key1: KEY1, key2: KEY2, key3: KEY3, defaultValue: VALUE): VALUE;
567
+ getOrCreate(key1: KEY1, key2: KEY2, key3: KEY3, value: VALUE): VALUE;
523
568
  getOrCreate(key1: KEY1, key2: KEY2, key3: KEY3, creator: () => VALUE): VALUE;
524
569
  delete(key1: KEY1): boolean;
525
570
  delete(key1: KEY1, key2: KEY2): boolean;
@@ -546,4 +591,4 @@ declare class Map3<KEY1, KEY2, KEY3, VALUE> {
546
591
  toString(): string;
547
592
  }
548
593
 
549
- export { Assert, Cookies, Device, LRUCache, Map1, Map2, Map3, SmallIntCache, Stack, index as Utils, Vec2 };
594
+ export { Assert, Cookies, Device, LRUCache, Map1, Map2, Map3, SignedIndexArray, SmallIntCache, Stack, index as Utils, Vec2 };
package/dist/index.d.ts CHANGED
@@ -446,6 +446,48 @@ declare class SmallIntCache<V> {
446
446
  clear(): void;
447
447
  }
448
448
 
449
+ /**
450
+ * An array-like structure for signed indexes, including negatives.
451
+ */
452
+ declare class SignedIndexArray<EL> {
453
+ private posEl;
454
+ private hasPos;
455
+ private negEl;
456
+ private hasNeg;
457
+ private elCount;
458
+ private static toNegIndex;
459
+ constructor();
460
+ constructor(arr: SignedIndexArray<EL>);
461
+ constructor(entries: Iterable<[number, EL]>);
462
+ get size(): number;
463
+ has(id: number): boolean;
464
+ set(id: number, el: EL): void;
465
+ get(id: number): EL | undefined;
466
+ getOrDefault(id: number, defaultValue: EL): EL;
467
+ getOrCreate(id: number, value: EL): EL;
468
+ getOrCreate(id: number, creator: () => EL): EL;
469
+ delete(id: number): boolean;
470
+ clear(): void;
471
+ forEach(callbackfn: (el: EL, id: number, arr: SignedIndexArray<EL>) => void, thisArg?: any): void;
472
+ indices(): IterableIterator<number>;
473
+ indicesArray(): number[];
474
+ values(): IterableIterator<EL>;
475
+ valuesArray(): EL[];
476
+ entries(): IterableIterator<[number, EL]>;
477
+ entriesArray(): [number, EL][];
478
+ [Symbol.iterator](): Generator<[number, EL], void, any>;
479
+ clone(): SignedIndexArray<EL>;
480
+ merge(other: SignedIndexArray<EL>, conflictResolver?: (oldValue: EL, newValue: EL, id: number) => EL): this;
481
+ some(fn: (el: EL, id: number) => boolean): boolean;
482
+ every(fn: (value: EL, key1: number) => boolean): boolean;
483
+ filter(fn: (value: EL, key1: number) => boolean): SignedIndexArray<EL>;
484
+ reduce(fn: (acc: EL, el: EL, id: number) => EL): EL;
485
+ reduce<R>(fn: (acc: R, el: EL, id: number) => R, init: R): R;
486
+ mapToArray<R>(fn: (value: EL, key1: number) => R): R[];
487
+ map<R = EL>(fn: (value: EL, key1: number) => R): SignedIndexArray<R>;
488
+ toString(): string;
489
+ }
490
+
449
491
  declare class Map1<KEY1, VALUE> {
450
492
  private map1;
451
493
  constructor();
@@ -455,6 +497,7 @@ declare class Map1<KEY1, VALUE> {
455
497
  set(key1: KEY1, value: VALUE): VALUE;
456
498
  get(key1: KEY1): VALUE | undefined;
457
499
  getOrDefault(key1: KEY1, defaultValue: VALUE): VALUE;
500
+ getOrCreate(key1: KEY1, value: VALUE): VALUE;
458
501
  getOrCreate(key1: KEY1, creator: () => VALUE): VALUE;
459
502
  delete(key1: KEY1): boolean;
460
503
  clear(): void;
@@ -487,6 +530,7 @@ declare class Map2<KEY1, KEY2, VALUE> {
487
530
  set(key1: KEY1, key2: KEY2, value: VALUE): VALUE;
488
531
  get(key1: KEY1, key2: KEY2): VALUE | undefined;
489
532
  getOrDefault(key1: KEY1, key2: KEY2, defaultValue: VALUE): VALUE;
533
+ getOrCreate(key1: KEY1, key2: KEY2, value: VALUE): VALUE;
490
534
  getOrCreate(key1: KEY1, key2: KEY2, creator: () => VALUE): VALUE;
491
535
  delete(key1: KEY1): boolean;
492
536
  delete(key1: KEY1, key2: KEY2): boolean;
@@ -520,6 +564,7 @@ declare class Map3<KEY1, KEY2, KEY3, VALUE> {
520
564
  set(key1: KEY1, key2: KEY2, key3: KEY3, value: VALUE): VALUE;
521
565
  get(key1: KEY1, key2: KEY2, key3: KEY3): VALUE | undefined;
522
566
  getOrDefault(key1: KEY1, key2: KEY2, key3: KEY3, defaultValue: VALUE): VALUE;
567
+ getOrCreate(key1: KEY1, key2: KEY2, key3: KEY3, value: VALUE): VALUE;
523
568
  getOrCreate(key1: KEY1, key2: KEY2, key3: KEY3, creator: () => VALUE): VALUE;
524
569
  delete(key1: KEY1): boolean;
525
570
  delete(key1: KEY1, key2: KEY2): boolean;
@@ -546,4 +591,4 @@ declare class Map3<KEY1, KEY2, KEY3, VALUE> {
546
591
  toString(): string;
547
592
  }
548
593
 
549
- export { Assert, Cookies, Device, LRUCache, Map1, Map2, Map3, SmallIntCache, Stack, index as Utils, Vec2 };
594
+ export { Assert, Cookies, Device, LRUCache, Map1, Map2, Map3, SignedIndexArray, SmallIntCache, Stack, index as Utils, Vec2 };
package/dist/index.js CHANGED
@@ -1,4 +1,4 @@
1
- /* TsUtilsLib v1.13.0 | (c) 2023 PahkaSoft | Licensed under the MIT License */
1
+ /* TsUtilsLib v1.14.0 | (c) 2023 PahkaSoft | Licensed under the MIT License */
2
2
  "use strict";
3
3
  var __defProp = Object.defineProperty;
4
4
  var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
@@ -30,6 +30,7 @@ __export(index_exports, {
30
30
  Map1: () => Map1,
31
31
  Map2: () => Map2,
32
32
  Map3: () => Map3,
33
+ SignedIndexArray: () => SignedIndexArray,
33
34
  SmallIntCache: () => SmallIntCache,
34
35
  Stack: () => Stack,
35
36
  Utils: () => utils_exports,
@@ -1292,6 +1293,226 @@ var SmallIntCache = class {
1292
1293
  }
1293
1294
  };
1294
1295
 
1296
+ // src/core/signed-index-array.ts
1297
+ var SignedIndexArray = class _SignedIndexArray {
1298
+ constructor(entries) {
1299
+ // for indexes >= 0
1300
+ __publicField(this, "posEl");
1301
+ __publicField(this, "hasPos");
1302
+ // for indexes < 0
1303
+ __publicField(this, "negEl");
1304
+ __publicField(this, "hasNeg");
1305
+ // number of elems
1306
+ __publicField(this, "elCount");
1307
+ if (entries instanceof _SignedIndexArray) {
1308
+ this.negEl = entries.negEl.slice();
1309
+ this.hasNeg = entries.hasNeg.slice();
1310
+ this.posEl = entries.posEl.slice();
1311
+ this.hasPos = entries.hasPos.slice();
1312
+ this.elCount = entries.elCount;
1313
+ } else {
1314
+ this.negEl = [];
1315
+ this.hasNeg = [];
1316
+ this.posEl = [];
1317
+ this.hasPos = [];
1318
+ this.elCount = 0;
1319
+ if (entries) {
1320
+ for (const [id, el] of entries) {
1321
+ this.set(id, el);
1322
+ }
1323
+ }
1324
+ }
1325
+ }
1326
+ static toNegIndex(id) {
1327
+ return -id - 1;
1328
+ }
1329
+ get size() {
1330
+ return this.elCount;
1331
+ }
1332
+ has(id) {
1333
+ if (!isInteger(id)) {
1334
+ return false;
1335
+ } else if (id >= 0) {
1336
+ return this.hasPos[id] === true;
1337
+ } else {
1338
+ return this.hasNeg[_SignedIndexArray.toNegIndex(id)] === true;
1339
+ }
1340
+ }
1341
+ set(id, el) {
1342
+ if (!isInteger(id)) {
1343
+ throw new Error("Index must be an integer");
1344
+ } else if (id >= 0) {
1345
+ if (this.hasPos[id] !== true) this.elCount++;
1346
+ this.posEl[id] = el;
1347
+ this.hasPos[id] = true;
1348
+ } else {
1349
+ if (this.hasNeg[_SignedIndexArray.toNegIndex(id)] !== true) this.elCount++;
1350
+ this.negEl[_SignedIndexArray.toNegIndex(id)] = el;
1351
+ this.hasNeg[_SignedIndexArray.toNegIndex(id)] = true;
1352
+ }
1353
+ }
1354
+ get(id) {
1355
+ if (!isInteger(id)) {
1356
+ throw new Error("Index must be an integer");
1357
+ } else if (id >= 0) {
1358
+ return this.hasPos[id] ? this.posEl[id] : void 0;
1359
+ } else {
1360
+ return this.hasNeg[_SignedIndexArray.toNegIndex(id)] ? this.negEl[_SignedIndexArray.toNegIndex(id)] : void 0;
1361
+ }
1362
+ }
1363
+ getOrDefault(id, defaultValue) {
1364
+ return this.get(id) ?? defaultValue;
1365
+ }
1366
+ getOrCreate(id, creatorOrValue) {
1367
+ if (!this.has(id)) {
1368
+ const value = isFunction(creatorOrValue) ? creatorOrValue() : creatorOrValue;
1369
+ this.set(id, value);
1370
+ return value;
1371
+ }
1372
+ return this.get(id);
1373
+ }
1374
+ delete(id) {
1375
+ if (!isInteger(id)) return false;
1376
+ const isPos = id >= 0;
1377
+ const arr = isPos ? this.posEl : this.negEl;
1378
+ const has = isPos ? this.hasPos : this.hasNeg;
1379
+ const idx = isPos ? id : _SignedIndexArray.toNegIndex(id);
1380
+ if (!has[idx]) return false;
1381
+ arr[idx] = void 0;
1382
+ has[idx] = false;
1383
+ this.elCount--;
1384
+ return true;
1385
+ }
1386
+ clear() {
1387
+ this.negEl = [];
1388
+ this.hasNeg = [];
1389
+ this.posEl = [];
1390
+ this.hasPos = [];
1391
+ this.elCount = 0;
1392
+ }
1393
+ forEach(callbackfn, thisArg) {
1394
+ for (const [id, el] of this.entries()) {
1395
+ callbackfn.call(thisArg, el, id, this);
1396
+ }
1397
+ }
1398
+ indices() {
1399
+ function* gen(self) {
1400
+ for (let id = self.negEl.length - 1; id >= 0; id--) {
1401
+ if (self.hasNeg[id]) yield _SignedIndexArray.toNegIndex(id);
1402
+ }
1403
+ for (let id = 0; id < self.posEl.length; id++) {
1404
+ if (self.hasPos[id]) yield id;
1405
+ }
1406
+ }
1407
+ return gen(this);
1408
+ }
1409
+ indicesArray() {
1410
+ return [...this.indices()];
1411
+ }
1412
+ values() {
1413
+ function* gen(self) {
1414
+ for (let id = self.negEl.length - 1; id >= 0; id--) {
1415
+ if (self.hasNeg[id]) yield self.negEl[id];
1416
+ }
1417
+ for (let id = 0; id < self.posEl.length; id++) {
1418
+ if (self.hasPos[id]) yield self.posEl[id];
1419
+ }
1420
+ }
1421
+ return gen(this);
1422
+ }
1423
+ valuesArray() {
1424
+ return [...this.values()];
1425
+ }
1426
+ *entries() {
1427
+ for (let id = this.negEl.length - 1; id >= 0; id--) {
1428
+ if (this.hasNeg[id]) yield [_SignedIndexArray.toNegIndex(id), this.negEl[id]];
1429
+ }
1430
+ for (let id = 0; id < this.posEl.length; id++) {
1431
+ if (this.hasPos[id]) yield [id, this.posEl[id]];
1432
+ }
1433
+ }
1434
+ entriesArray() {
1435
+ return [...this.entries()];
1436
+ }
1437
+ *[Symbol.iterator]() {
1438
+ yield* this.entries();
1439
+ }
1440
+ clone() {
1441
+ return new _SignedIndexArray(this);
1442
+ }
1443
+ merge(other, conflictResolver) {
1444
+ for (const [id, value] of other.entries()) {
1445
+ if (this.has(id) && conflictResolver) {
1446
+ this.set(id, conflictResolver(this.get(id), value, id));
1447
+ } else {
1448
+ this.set(id, value);
1449
+ }
1450
+ }
1451
+ return this;
1452
+ }
1453
+ some(fn) {
1454
+ for (const [id, el] of this.entries()) {
1455
+ if (fn(el, id)) return true;
1456
+ }
1457
+ return false;
1458
+ }
1459
+ every(fn) {
1460
+ for (const [id, el] of this.entries()) {
1461
+ if (!fn(el, id)) return false;
1462
+ }
1463
+ return true;
1464
+ }
1465
+ filter(fn) {
1466
+ let result = new _SignedIndexArray();
1467
+ for (const [id, el] of this.entries()) {
1468
+ if (fn(el, id)) result.set(id, el);
1469
+ }
1470
+ return result;
1471
+ }
1472
+ reduce(fn, init) {
1473
+ let iterator = this.entries();
1474
+ let first = iterator.next();
1475
+ if (first.done) {
1476
+ if (arguments.length < 2) {
1477
+ throw new TypeError("Reduce of empty SignedIndexArray with no initial value!");
1478
+ }
1479
+ return init;
1480
+ }
1481
+ let acc;
1482
+ let start;
1483
+ if (arguments.length < 2) {
1484
+ acc = first.value[1];
1485
+ start = iterator.next();
1486
+ } else {
1487
+ acc = init;
1488
+ start = first;
1489
+ }
1490
+ for (let current = start; !current.done; current = iterator.next()) {
1491
+ const [id, el] = current.value;
1492
+ acc = fn(acc, el, id);
1493
+ }
1494
+ return acc;
1495
+ }
1496
+ mapToArray(fn) {
1497
+ let result = [];
1498
+ for (const [id, el] of this.entries()) {
1499
+ result.push(fn(el, id));
1500
+ }
1501
+ return result;
1502
+ }
1503
+ map(fn) {
1504
+ let result = new _SignedIndexArray();
1505
+ for (const [id, el] of this.entries()) {
1506
+ result.set(id, fn(el, id));
1507
+ }
1508
+ return result;
1509
+ }
1510
+ toString() {
1511
+ const entries = this.entriesArray().map(([id, el]) => `${id}: ${el}`).join(", ");
1512
+ return `SignedIndexArray[ ${entries} ]`;
1513
+ }
1514
+ };
1515
+
1295
1516
  // src/core/map.ts
1296
1517
  var Map1 = class _Map1 {
1297
1518
  constructor(entries) {
@@ -1311,12 +1532,13 @@ var Map1 = class _Map1 {
1311
1532
  getOrDefault(key1, defaultValue) {
1312
1533
  return this.get(key1) ?? defaultValue;
1313
1534
  }
1314
- getOrCreate(key1, creator) {
1315
- let value = this.get(key1);
1316
- if (!value) {
1317
- this.set(key1, value = creator());
1535
+ getOrCreate(key1, creatorOrValue) {
1536
+ if (!this.has(key1)) {
1537
+ const value = isFunction(creatorOrValue) ? creatorOrValue() : creatorOrValue;
1538
+ this.set(key1, value);
1539
+ return value;
1318
1540
  }
1319
- return value;
1541
+ return this.get(key1);
1320
1542
  }
1321
1543
  delete(key1) {
1322
1544
  return this.map1.delete(key1);
@@ -1440,12 +1662,13 @@ var Map2 = class _Map2 {
1440
1662
  getOrDefault(key1, key2, defaultValue) {
1441
1663
  return this.get(key1, key2) ?? defaultValue;
1442
1664
  }
1443
- getOrCreate(key1, key2, creator) {
1444
- let value = this.get(key1, key2);
1445
- if (!value) {
1446
- this.set(key1, key2, value = creator());
1665
+ getOrCreate(key1, key2, creatorOrValue) {
1666
+ if (!this.has(key1, key2)) {
1667
+ const value = isFunction(creatorOrValue) ? creatorOrValue() : creatorOrValue;
1668
+ this.set(key1, key2, value);
1669
+ return value;
1447
1670
  }
1448
- return value;
1671
+ return this.get(key1, key2);
1449
1672
  }
1450
1673
  delete(key1, key2) {
1451
1674
  if (key2 === void 0) return this.map1.delete(key1);
@@ -1616,12 +1839,13 @@ var Map3 = class _Map3 {
1616
1839
  getOrDefault(key1, key2, key3, defaultValue) {
1617
1840
  return this.get(key1, key2, key3) ?? defaultValue;
1618
1841
  }
1619
- getOrCreate(key1, key2, key3, creator) {
1620
- let value = this.get(key1, key2, key3);
1621
- if (!value) {
1622
- this.set(key1, key2, key3, value = creator());
1842
+ getOrCreate(key1, key2, key3, creatorOrValue) {
1843
+ if (!this.has(key1, key2, key3)) {
1844
+ const value = isFunction(creatorOrValue) ? creatorOrValue() : creatorOrValue;
1845
+ this.set(key1, key2, key3, value);
1846
+ return value;
1623
1847
  }
1624
- return value;
1848
+ return this.get(key1, key2, key3);
1625
1849
  }
1626
1850
  delete(key1, key2, key3) {
1627
1851
  if (key3 === void 0) {
@@ -1795,6 +2019,7 @@ var Map3 = class _Map3 {
1795
2019
  Map1,
1796
2020
  Map2,
1797
2021
  Map3,
2022
+ SignedIndexArray,
1798
2023
  SmallIntCache,
1799
2024
  Stack,
1800
2025
  Utils,
package/dist/index.mjs CHANGED
@@ -1,4 +1,4 @@
1
- /* TsUtilsLib v1.13.0 | (c) 2023 PahkaSoft | Licensed under the MIT License */
1
+ /* TsUtilsLib v1.14.0 | (c) 2023 PahkaSoft | Licensed under the MIT License */
2
2
  var __defProp = Object.defineProperty;
3
3
  var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
4
4
  var __export = (target, all) => {
@@ -1262,6 +1262,226 @@ var SmallIntCache = class {
1262
1262
  }
1263
1263
  };
1264
1264
 
1265
+ // src/core/signed-index-array.ts
1266
+ var SignedIndexArray = class _SignedIndexArray {
1267
+ constructor(entries) {
1268
+ // for indexes >= 0
1269
+ __publicField(this, "posEl");
1270
+ __publicField(this, "hasPos");
1271
+ // for indexes < 0
1272
+ __publicField(this, "negEl");
1273
+ __publicField(this, "hasNeg");
1274
+ // number of elems
1275
+ __publicField(this, "elCount");
1276
+ if (entries instanceof _SignedIndexArray) {
1277
+ this.negEl = entries.negEl.slice();
1278
+ this.hasNeg = entries.hasNeg.slice();
1279
+ this.posEl = entries.posEl.slice();
1280
+ this.hasPos = entries.hasPos.slice();
1281
+ this.elCount = entries.elCount;
1282
+ } else {
1283
+ this.negEl = [];
1284
+ this.hasNeg = [];
1285
+ this.posEl = [];
1286
+ this.hasPos = [];
1287
+ this.elCount = 0;
1288
+ if (entries) {
1289
+ for (const [id, el] of entries) {
1290
+ this.set(id, el);
1291
+ }
1292
+ }
1293
+ }
1294
+ }
1295
+ static toNegIndex(id) {
1296
+ return -id - 1;
1297
+ }
1298
+ get size() {
1299
+ return this.elCount;
1300
+ }
1301
+ has(id) {
1302
+ if (!isInteger(id)) {
1303
+ return false;
1304
+ } else if (id >= 0) {
1305
+ return this.hasPos[id] === true;
1306
+ } else {
1307
+ return this.hasNeg[_SignedIndexArray.toNegIndex(id)] === true;
1308
+ }
1309
+ }
1310
+ set(id, el) {
1311
+ if (!isInteger(id)) {
1312
+ throw new Error("Index must be an integer");
1313
+ } else if (id >= 0) {
1314
+ if (this.hasPos[id] !== true) this.elCount++;
1315
+ this.posEl[id] = el;
1316
+ this.hasPos[id] = true;
1317
+ } else {
1318
+ if (this.hasNeg[_SignedIndexArray.toNegIndex(id)] !== true) this.elCount++;
1319
+ this.negEl[_SignedIndexArray.toNegIndex(id)] = el;
1320
+ this.hasNeg[_SignedIndexArray.toNegIndex(id)] = true;
1321
+ }
1322
+ }
1323
+ get(id) {
1324
+ if (!isInteger(id)) {
1325
+ throw new Error("Index must be an integer");
1326
+ } else if (id >= 0) {
1327
+ return this.hasPos[id] ? this.posEl[id] : void 0;
1328
+ } else {
1329
+ return this.hasNeg[_SignedIndexArray.toNegIndex(id)] ? this.negEl[_SignedIndexArray.toNegIndex(id)] : void 0;
1330
+ }
1331
+ }
1332
+ getOrDefault(id, defaultValue) {
1333
+ return this.get(id) ?? defaultValue;
1334
+ }
1335
+ getOrCreate(id, creatorOrValue) {
1336
+ if (!this.has(id)) {
1337
+ const value = isFunction(creatorOrValue) ? creatorOrValue() : creatorOrValue;
1338
+ this.set(id, value);
1339
+ return value;
1340
+ }
1341
+ return this.get(id);
1342
+ }
1343
+ delete(id) {
1344
+ if (!isInteger(id)) return false;
1345
+ const isPos = id >= 0;
1346
+ const arr = isPos ? this.posEl : this.negEl;
1347
+ const has = isPos ? this.hasPos : this.hasNeg;
1348
+ const idx = isPos ? id : _SignedIndexArray.toNegIndex(id);
1349
+ if (!has[idx]) return false;
1350
+ arr[idx] = void 0;
1351
+ has[idx] = false;
1352
+ this.elCount--;
1353
+ return true;
1354
+ }
1355
+ clear() {
1356
+ this.negEl = [];
1357
+ this.hasNeg = [];
1358
+ this.posEl = [];
1359
+ this.hasPos = [];
1360
+ this.elCount = 0;
1361
+ }
1362
+ forEach(callbackfn, thisArg) {
1363
+ for (const [id, el] of this.entries()) {
1364
+ callbackfn.call(thisArg, el, id, this);
1365
+ }
1366
+ }
1367
+ indices() {
1368
+ function* gen(self) {
1369
+ for (let id = self.negEl.length - 1; id >= 0; id--) {
1370
+ if (self.hasNeg[id]) yield _SignedIndexArray.toNegIndex(id);
1371
+ }
1372
+ for (let id = 0; id < self.posEl.length; id++) {
1373
+ if (self.hasPos[id]) yield id;
1374
+ }
1375
+ }
1376
+ return gen(this);
1377
+ }
1378
+ indicesArray() {
1379
+ return [...this.indices()];
1380
+ }
1381
+ values() {
1382
+ function* gen(self) {
1383
+ for (let id = self.negEl.length - 1; id >= 0; id--) {
1384
+ if (self.hasNeg[id]) yield self.negEl[id];
1385
+ }
1386
+ for (let id = 0; id < self.posEl.length; id++) {
1387
+ if (self.hasPos[id]) yield self.posEl[id];
1388
+ }
1389
+ }
1390
+ return gen(this);
1391
+ }
1392
+ valuesArray() {
1393
+ return [...this.values()];
1394
+ }
1395
+ *entries() {
1396
+ for (let id = this.negEl.length - 1; id >= 0; id--) {
1397
+ if (this.hasNeg[id]) yield [_SignedIndexArray.toNegIndex(id), this.negEl[id]];
1398
+ }
1399
+ for (let id = 0; id < this.posEl.length; id++) {
1400
+ if (this.hasPos[id]) yield [id, this.posEl[id]];
1401
+ }
1402
+ }
1403
+ entriesArray() {
1404
+ return [...this.entries()];
1405
+ }
1406
+ *[Symbol.iterator]() {
1407
+ yield* this.entries();
1408
+ }
1409
+ clone() {
1410
+ return new _SignedIndexArray(this);
1411
+ }
1412
+ merge(other, conflictResolver) {
1413
+ for (const [id, value] of other.entries()) {
1414
+ if (this.has(id) && conflictResolver) {
1415
+ this.set(id, conflictResolver(this.get(id), value, id));
1416
+ } else {
1417
+ this.set(id, value);
1418
+ }
1419
+ }
1420
+ return this;
1421
+ }
1422
+ some(fn) {
1423
+ for (const [id, el] of this.entries()) {
1424
+ if (fn(el, id)) return true;
1425
+ }
1426
+ return false;
1427
+ }
1428
+ every(fn) {
1429
+ for (const [id, el] of this.entries()) {
1430
+ if (!fn(el, id)) return false;
1431
+ }
1432
+ return true;
1433
+ }
1434
+ filter(fn) {
1435
+ let result = new _SignedIndexArray();
1436
+ for (const [id, el] of this.entries()) {
1437
+ if (fn(el, id)) result.set(id, el);
1438
+ }
1439
+ return result;
1440
+ }
1441
+ reduce(fn, init) {
1442
+ let iterator = this.entries();
1443
+ let first = iterator.next();
1444
+ if (first.done) {
1445
+ if (arguments.length < 2) {
1446
+ throw new TypeError("Reduce of empty SignedIndexArray with no initial value!");
1447
+ }
1448
+ return init;
1449
+ }
1450
+ let acc;
1451
+ let start;
1452
+ if (arguments.length < 2) {
1453
+ acc = first.value[1];
1454
+ start = iterator.next();
1455
+ } else {
1456
+ acc = init;
1457
+ start = first;
1458
+ }
1459
+ for (let current = start; !current.done; current = iterator.next()) {
1460
+ const [id, el] = current.value;
1461
+ acc = fn(acc, el, id);
1462
+ }
1463
+ return acc;
1464
+ }
1465
+ mapToArray(fn) {
1466
+ let result = [];
1467
+ for (const [id, el] of this.entries()) {
1468
+ result.push(fn(el, id));
1469
+ }
1470
+ return result;
1471
+ }
1472
+ map(fn) {
1473
+ let result = new _SignedIndexArray();
1474
+ for (const [id, el] of this.entries()) {
1475
+ result.set(id, fn(el, id));
1476
+ }
1477
+ return result;
1478
+ }
1479
+ toString() {
1480
+ const entries = this.entriesArray().map(([id, el]) => `${id}: ${el}`).join(", ");
1481
+ return `SignedIndexArray[ ${entries} ]`;
1482
+ }
1483
+ };
1484
+
1265
1485
  // src/core/map.ts
1266
1486
  var Map1 = class _Map1 {
1267
1487
  constructor(entries) {
@@ -1281,12 +1501,13 @@ var Map1 = class _Map1 {
1281
1501
  getOrDefault(key1, defaultValue) {
1282
1502
  return this.get(key1) ?? defaultValue;
1283
1503
  }
1284
- getOrCreate(key1, creator) {
1285
- let value = this.get(key1);
1286
- if (!value) {
1287
- this.set(key1, value = creator());
1504
+ getOrCreate(key1, creatorOrValue) {
1505
+ if (!this.has(key1)) {
1506
+ const value = isFunction(creatorOrValue) ? creatorOrValue() : creatorOrValue;
1507
+ this.set(key1, value);
1508
+ return value;
1288
1509
  }
1289
- return value;
1510
+ return this.get(key1);
1290
1511
  }
1291
1512
  delete(key1) {
1292
1513
  return this.map1.delete(key1);
@@ -1410,12 +1631,13 @@ var Map2 = class _Map2 {
1410
1631
  getOrDefault(key1, key2, defaultValue) {
1411
1632
  return this.get(key1, key2) ?? defaultValue;
1412
1633
  }
1413
- getOrCreate(key1, key2, creator) {
1414
- let value = this.get(key1, key2);
1415
- if (!value) {
1416
- this.set(key1, key2, value = creator());
1634
+ getOrCreate(key1, key2, creatorOrValue) {
1635
+ if (!this.has(key1, key2)) {
1636
+ const value = isFunction(creatorOrValue) ? creatorOrValue() : creatorOrValue;
1637
+ this.set(key1, key2, value);
1638
+ return value;
1417
1639
  }
1418
- return value;
1640
+ return this.get(key1, key2);
1419
1641
  }
1420
1642
  delete(key1, key2) {
1421
1643
  if (key2 === void 0) return this.map1.delete(key1);
@@ -1586,12 +1808,13 @@ var Map3 = class _Map3 {
1586
1808
  getOrDefault(key1, key2, key3, defaultValue) {
1587
1809
  return this.get(key1, key2, key3) ?? defaultValue;
1588
1810
  }
1589
- getOrCreate(key1, key2, key3, creator) {
1590
- let value = this.get(key1, key2, key3);
1591
- if (!value) {
1592
- this.set(key1, key2, key3, value = creator());
1811
+ getOrCreate(key1, key2, key3, creatorOrValue) {
1812
+ if (!this.has(key1, key2, key3)) {
1813
+ const value = isFunction(creatorOrValue) ? creatorOrValue() : creatorOrValue;
1814
+ this.set(key1, key2, key3, value);
1815
+ return value;
1593
1816
  }
1594
- return value;
1817
+ return this.get(key1, key2, key3);
1595
1818
  }
1596
1819
  delete(key1, key2, key3) {
1597
1820
  if (key3 === void 0) {
@@ -1764,6 +1987,7 @@ export {
1764
1987
  Map1,
1765
1988
  Map2,
1766
1989
  Map3,
1990
+ SignedIndexArray,
1767
1991
  SmallIntCache,
1768
1992
  Stack,
1769
1993
  utils_exports as Utils,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tspro/ts-utils-lib",
3
- "version": "1.13.0",
3
+ "version": "1.14.0",
4
4
  "author": "PahkaSoft",
5
5
  "license": "MIT",
6
6
  "private": false,