@yipe/dice 0.1.1 → 0.1.2

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/index.d.cts CHANGED
@@ -1,9 +1,24 @@
1
+ /**
2
+ * Simple LRU cache implementation
3
+ */
4
+ declare class LRUCache<K, V> {
5
+ private readonly maxSize;
6
+ private cache;
7
+ constructor(maxSize?: number);
8
+ get(key: K): V | undefined;
9
+ delete(key: K): void;
10
+ set(key: K, value: V): this;
11
+ clear(): void;
12
+ get size(): number;
13
+ has(key: K): boolean;
14
+ keys(): IterableIterator<K>;
15
+ values(): IterableIterator<V>;
16
+ }
17
+
1
18
  /** Mapping from outcome label to probability mass or damage attribution. */
2
19
  type OutcomeLabelMap = Partial<Record<string, number>>;
3
20
  /** Computational epsilon for pruning negligible probabilities. */
4
- declare const COMPUTATIONAL_EPS = 1e-40;
5
- /** Test tolerance for floating-point precision errors. */
6
- declare const TEST_EPS = 1e-10;
21
+ declare const EPS = 1e-12;
7
22
  /** A probability bin for a specific damage value. */
8
23
  interface Bin {
9
24
  /** Total probability mass at this damage value. */
@@ -604,6 +619,7 @@ type Snapshot = {
604
619
  outcomes: Map<OutcomeType, OutcomeSnapshot>;
605
620
  };
606
621
 
622
+ declare const pmfCache: LRUCache<string, PMF>;
607
623
  /**
608
624
  * Probability Mass Function for discrete damage distributions.
609
625
  *
@@ -730,6 +746,7 @@ declare class PMF {
730
746
  static mixN(weights: [number, PMF][]): PMF;
731
747
  private setPreservedProvidence;
732
748
  preservedProvidence(): boolean;
749
+ private getPowerCacheKey;
733
750
  /**
734
751
  * Efficiently computes this PMF convolved with itself `n` times.
735
752
  * Uses exponentiation by squaring to reduce total convolutions.
@@ -864,7 +881,6 @@ declare class PMF {
864
881
  };
865
882
  query(): DiceQuery;
866
883
  }
867
- declare function getPowerCacheKey(base: PMF, n: number, eps: number): string;
868
884
 
869
885
  /**
870
886
  * @internal
@@ -925,23 +941,6 @@ declare class Dice {
925
941
  }
926
942
  type _DiceInternal = never;
927
943
 
928
- /**
929
- * Simple LRU cache implementation
930
- */
931
- declare class LRUCache<K, V> {
932
- private readonly maxSize;
933
- private cache;
934
- constructor(maxSize?: number);
935
- get(key: K): V | undefined;
936
- delete(key: K): void;
937
- set(key: K, value: V): this;
938
- clear(): void;
939
- get size(): number;
940
- has(key: K): boolean;
941
- keys(): IterableIterator<K>;
942
- values(): IterableIterator<V>;
943
- }
944
-
945
944
  /** Enable or disable the internal parse cache. */
946
945
  declare function setCachingEnabled(enabled: boolean): void;
947
946
  /** Returns whether the internal parse cache is currently enabled. */
@@ -955,4 +954,4 @@ declare function clearParserCache(): void;
955
954
  */
956
955
  declare function parse(expression: string, n?: number): PMF;
957
956
 
958
- export { type Bin, COMPUTATIONAL_EPS, type DamageDistribution, Dice, DiceQuery, LRUCache, type OutcomeLabelMap, type OutcomeSnapshot, type OutcomeType, PMF, type Snapshot, TEST_EPS, type _DiceInternal, clearParserCache, getCachingEnabled, getPowerCacheKey, parse, setCachingEnabled };
957
+ export { type Bin, type DamageDistribution, Dice, DiceQuery, EPS, LRUCache, type OutcomeLabelMap, type OutcomeSnapshot, type OutcomeType, PMF, type Snapshot, type _DiceInternal, clearParserCache, getCachingEnabled, parse, pmfCache, setCachingEnabled };
package/dist/index.d.ts CHANGED
@@ -1,9 +1,24 @@
1
+ /**
2
+ * Simple LRU cache implementation
3
+ */
4
+ declare class LRUCache<K, V> {
5
+ private readonly maxSize;
6
+ private cache;
7
+ constructor(maxSize?: number);
8
+ get(key: K): V | undefined;
9
+ delete(key: K): void;
10
+ set(key: K, value: V): this;
11
+ clear(): void;
12
+ get size(): number;
13
+ has(key: K): boolean;
14
+ keys(): IterableIterator<K>;
15
+ values(): IterableIterator<V>;
16
+ }
17
+
1
18
  /** Mapping from outcome label to probability mass or damage attribution. */
2
19
  type OutcomeLabelMap = Partial<Record<string, number>>;
3
20
  /** Computational epsilon for pruning negligible probabilities. */
4
- declare const COMPUTATIONAL_EPS = 1e-40;
5
- /** Test tolerance for floating-point precision errors. */
6
- declare const TEST_EPS = 1e-10;
21
+ declare const EPS = 1e-12;
7
22
  /** A probability bin for a specific damage value. */
8
23
  interface Bin {
9
24
  /** Total probability mass at this damage value. */
@@ -604,6 +619,7 @@ type Snapshot = {
604
619
  outcomes: Map<OutcomeType, OutcomeSnapshot>;
605
620
  };
606
621
 
622
+ declare const pmfCache: LRUCache<string, PMF>;
607
623
  /**
608
624
  * Probability Mass Function for discrete damage distributions.
609
625
  *
@@ -730,6 +746,7 @@ declare class PMF {
730
746
  static mixN(weights: [number, PMF][]): PMF;
731
747
  private setPreservedProvidence;
732
748
  preservedProvidence(): boolean;
749
+ private getPowerCacheKey;
733
750
  /**
734
751
  * Efficiently computes this PMF convolved with itself `n` times.
735
752
  * Uses exponentiation by squaring to reduce total convolutions.
@@ -864,7 +881,6 @@ declare class PMF {
864
881
  };
865
882
  query(): DiceQuery;
866
883
  }
867
- declare function getPowerCacheKey(base: PMF, n: number, eps: number): string;
868
884
 
869
885
  /**
870
886
  * @internal
@@ -925,23 +941,6 @@ declare class Dice {
925
941
  }
926
942
  type _DiceInternal = never;
927
943
 
928
- /**
929
- * Simple LRU cache implementation
930
- */
931
- declare class LRUCache<K, V> {
932
- private readonly maxSize;
933
- private cache;
934
- constructor(maxSize?: number);
935
- get(key: K): V | undefined;
936
- delete(key: K): void;
937
- set(key: K, value: V): this;
938
- clear(): void;
939
- get size(): number;
940
- has(key: K): boolean;
941
- keys(): IterableIterator<K>;
942
- values(): IterableIterator<V>;
943
- }
944
-
945
944
  /** Enable or disable the internal parse cache. */
946
945
  declare function setCachingEnabled(enabled: boolean): void;
947
946
  /** Returns whether the internal parse cache is currently enabled. */
@@ -955,4 +954,4 @@ declare function clearParserCache(): void;
955
954
  */
956
955
  declare function parse(expression: string, n?: number): PMF;
957
956
 
958
- export { type Bin, COMPUTATIONAL_EPS, type DamageDistribution, Dice, DiceQuery, LRUCache, type OutcomeLabelMap, type OutcomeSnapshot, type OutcomeType, PMF, type Snapshot, TEST_EPS, type _DiceInternal, clearParserCache, getCachingEnabled, getPowerCacheKey, parse, setCachingEnabled };
957
+ export { type Bin, type DamageDistribution, Dice, DiceQuery, EPS, LRUCache, type OutcomeLabelMap, type OutcomeSnapshot, type OutcomeType, PMF, type Snapshot, type _DiceInternal, clearParserCache, getCachingEnabled, parse, pmfCache, setCachingEnabled };
package/dist/index.js CHANGED
@@ -45,8 +45,7 @@ var LRUCache = class {
45
45
  };
46
46
 
47
47
  // src/types.ts
48
- var COMPUTATIONAL_EPS = 1e-40;
49
- var TEST_EPS = 1e-10;
48
+ var EPS = 1e-12;
50
49
 
51
50
  // src/query.ts
52
51
  var _DiceQuery = class _DiceQuery {
@@ -1227,7 +1226,7 @@ var _DiceQuery = class _DiceQuery {
1227
1226
  *
1228
1227
  * Returns tuple: [pFirstNonSubset, pFirstSubset, pAnySuccess, pNone]
1229
1228
  */
1230
- firstSuccessSplit(successOutcome, subsetOutcome, eps = COMPUTATIONAL_EPS) {
1229
+ firstSuccessSplit(successOutcome, subsetOutcome, eps = EPS) {
1231
1230
  const pmfs = this.singles;
1232
1231
  if (!pmfs.length) {
1233
1232
  throw new Error("firstSuccessSplitFromPMFs: pmfs must be non-empty");
@@ -1276,10 +1275,9 @@ __publicField(_DiceQuery, "DEFAULT_OUTCOMES", [
1276
1275
  "missNone"
1277
1276
  ]);
1278
1277
  var DiceQuery = _DiceQuery;
1279
- var EPSILON = COMPUTATIONAL_EPS;
1280
1278
  var pmfCache = new LRUCache(1e3);
1281
1279
  var _PMF = class _PMF {
1282
- constructor(map = /* @__PURE__ */ new Map(), epsilon = EPSILON, normalized = false, identifier = `anon#${_PMF.__anonIdCounter++}`, _preservedProvidence = true) {
1280
+ constructor(map = /* @__PURE__ */ new Map(), epsilon = EPS, normalized = false, identifier = `anon#${_PMF.__anonIdCounter++}`, _preservedProvidence = true) {
1283
1281
  this.map = map;
1284
1282
  this.epsilon = epsilon;
1285
1283
  this.normalized = normalized;
@@ -1294,11 +1292,11 @@ var _PMF = class _PMF {
1294
1292
  __publicField(this, "_variance");
1295
1293
  __publicField(this, "_stdev");
1296
1294
  }
1297
- static empty(epsilon = EPSILON, identifier = "empty") {
1295
+ static empty(epsilon = EPS, identifier = "empty") {
1298
1296
  return new _PMF(/* @__PURE__ */ new Map(), epsilon, false, identifier);
1299
1297
  }
1300
1298
  // This has a single bin at value 0, mass of 1
1301
- static zero(epsilon = EPSILON) {
1299
+ static zero(epsilon = EPS) {
1302
1300
  const m = /* @__PURE__ */ new Map();
1303
1301
  m.set(0, { p: 1, count: { miss: 1 }, attr: {} });
1304
1302
  return new _PMF(m, epsilon, false, "zero");
@@ -1470,6 +1468,12 @@ var _PMF = class _PMF {
1470
1468
  preservedProvidence() {
1471
1469
  return this._preservedProvidence;
1472
1470
  }
1471
+ getPowerCacheKey(n, eps) {
1472
+ const id = this.identifier;
1473
+ let key = `${id}`;
1474
+ for (let i = 1; i < n; i++) key += `+${id}`;
1475
+ return `${key}@${eps}`;
1476
+ }
1473
1477
  /**
1474
1478
  * Efficiently computes this PMF convolved with itself `n` times.
1475
1479
  * Uses exponentiation by squaring to reduce total convolutions.
@@ -1486,7 +1490,7 @@ var _PMF = class _PMF {
1486
1490
  }
1487
1491
  if (n === 1) return this;
1488
1492
  const epsilon = eps ?? this.epsilon;
1489
- const key = getPowerCacheKey(this, n, epsilon);
1493
+ const key = this.getPowerCacheKey(n, epsilon);
1490
1494
  {
1491
1495
  const cached = pmfCache?.get(key);
1492
1496
  if (cached) return cached;
@@ -1885,7 +1889,7 @@ var _PMF = class _PMF {
1885
1889
  * - Similar PMF lists share common prefixes (A+B, (A+B)+C, etc.)
1886
1890
  * - Order-independent cache keys work better with consistent build patterns
1887
1891
  */
1888
- static convolveMany(pmfList, eps = EPSILON) {
1892
+ static convolveMany(pmfList, eps = EPS) {
1889
1893
  if (pmfList.length === 0) return _PMF.empty(eps);
1890
1894
  if (pmfList.length === 1) return pmfList[0];
1891
1895
  return _PMF.reduceConvolveLeft(pmfList, eps);
@@ -1900,7 +1904,7 @@ var _PMF = class _PMF {
1900
1904
  static fromJSON(jsonData) {
1901
1905
  return new _PMF(
1902
1906
  new Map(jsonData.bins),
1903
- EPSILON,
1907
+ EPS,
1904
1908
  !!jsonData.normalized,
1905
1909
  jsonData.identifier || "fromJSON"
1906
1910
  );
@@ -2112,12 +2116,6 @@ var _PMF = class _PMF {
2112
2116
  // Unique ID generator for anonymous PMFs to avoid cache key collisions
2113
2117
  __publicField(_PMF, "__anonIdCounter", 1);
2114
2118
  var PMF = _PMF;
2115
- function getPowerCacheKey(base, n, eps) {
2116
- const id = base.identifier;
2117
- let key = `${id}`;
2118
- for (let i = 1; i < n; i++) key += `+${id}`;
2119
- return `${key}@${eps}`;
2120
- }
2121
2119
 
2122
2120
  // src/dice.ts
2123
2121
  var Dice = class _Dice {
@@ -2433,7 +2431,7 @@ var Dice = class _Dice {
2433
2431
  * Convert dice to PMF using OutcomeType labels directly from damage distribution.
2434
2432
  * This is much cleaner than the original complex distribution conversion.
2435
2433
  */
2436
- toPMF(numEpsilon = COMPUTATIONAL_EPS) {
2434
+ toPMF(numEpsilon = EPS) {
2437
2435
  const total = this.total();
2438
2436
  if (total === 0) return PMF.empty(numEpsilon);
2439
2437
  this.ensureHitDistribution();
@@ -2946,6 +2944,6 @@ function parseOperation(s) {
2946
2944
  return;
2947
2945
  }
2948
2946
 
2949
- export { COMPUTATIONAL_EPS, Dice, DiceQuery, LRUCache, PMF, TEST_EPS, clearParserCache, getCachingEnabled, getPowerCacheKey, parse, setCachingEnabled };
2947
+ export { Dice, DiceQuery, EPS, LRUCache, PMF, clearParserCache, getCachingEnabled, parse, pmfCache, setCachingEnabled };
2950
2948
  //# sourceMappingURL=index.js.map
2951
2949
  //# sourceMappingURL=index.js.map