@flighthq/math 0.1.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.
Files changed (80) hide show
  1. package/dist/angle.d.ts +13 -0
  2. package/dist/angle.d.ts.map +1 -0
  3. package/dist/angle.js +24 -0
  4. package/dist/angle.js.map +1 -0
  5. package/dist/clamp.d.ts +15 -0
  6. package/dist/clamp.d.ts.map +1 -0
  7. package/dist/clamp.js +23 -0
  8. package/dist/clamp.js.map +1 -0
  9. package/dist/comparison.d.ts +17 -0
  10. package/dist/comparison.d.ts.map +1 -0
  11. package/dist/comparison.js +26 -0
  12. package/dist/comparison.js.map +1 -0
  13. package/dist/constants.d.ts +11 -0
  14. package/dist/constants.d.ts.map +1 -0
  15. package/dist/constants.js +11 -0
  16. package/dist/constants.js.map +1 -0
  17. package/dist/hash.d.ts +35 -0
  18. package/dist/hash.d.ts.map +1 -0
  19. package/dist/hash.js +51 -0
  20. package/dist/hash.js.map +1 -0
  21. package/dist/index.d.ts +16 -0
  22. package/dist/index.d.ts.map +1 -0
  23. package/dist/index.js +16 -0
  24. package/dist/index.js.map +1 -0
  25. package/dist/interpolation.d.ts +27 -0
  26. package/dist/interpolation.d.ts.map +1 -0
  27. package/dist/interpolation.js +43 -0
  28. package/dist/interpolation.js.map +1 -0
  29. package/dist/interpolationAdvanced.d.ts +42 -0
  30. package/dist/interpolationAdvanced.d.ts.map +1 -0
  31. package/dist/interpolationAdvanced.js +71 -0
  32. package/dist/interpolationAdvanced.js.map +1 -0
  33. package/dist/nextPowerOfTwo.d.ts +22 -0
  34. package/dist/nextPowerOfTwo.d.ts.map +1 -0
  35. package/dist/nextPowerOfTwo.js +52 -0
  36. package/dist/nextPowerOfTwo.js.map +1 -0
  37. package/dist/numberTheory.d.ts +33 -0
  38. package/dist/numberTheory.d.ts.map +1 -0
  39. package/dist/numberTheory.js +62 -0
  40. package/dist/numberTheory.js.map +1 -0
  41. package/dist/random.d.ts +20 -0
  42. package/dist/random.d.ts.map +1 -0
  43. package/dist/random.js +27 -0
  44. package/dist/random.js.map +1 -0
  45. package/dist/randomDistributions.d.ts +105 -0
  46. package/dist/randomDistributions.d.ts.map +1 -0
  47. package/dist/randomDistributions.js +209 -0
  48. package/dist/randomDistributions.js.map +1 -0
  49. package/dist/randomRange.d.ts +18 -0
  50. package/dist/randomRange.d.ts.map +1 -0
  51. package/dist/randomRange.js +29 -0
  52. package/dist/randomRange.js.map +1 -0
  53. package/dist/rounding.d.ts +35 -0
  54. package/dist/rounding.d.ts.map +1 -0
  55. package/dist/rounding.js +53 -0
  56. package/dist/rounding.js.map +1 -0
  57. package/dist/scalar.d.ts +30 -0
  58. package/dist/scalar.d.ts.map +1 -0
  59. package/dist/scalar.js +42 -0
  60. package/dist/scalar.js.map +1 -0
  61. package/dist/statistics.d.ts +29 -0
  62. package/dist/statistics.d.ts.map +1 -0
  63. package/dist/statistics.js +67 -0
  64. package/dist/statistics.js.map +1 -0
  65. package/package.json +37 -0
  66. package/src/angle.test.ts +83 -0
  67. package/src/clamp.test.ts +61 -0
  68. package/src/comparison.test.ts +54 -0
  69. package/src/constants.test.ts +38 -0
  70. package/src/hash.test.ts +84 -0
  71. package/src/interpolation.test.ts +93 -0
  72. package/src/interpolationAdvanced.test.ts +122 -0
  73. package/src/nextPowerOfTwo.test.ts +84 -0
  74. package/src/numberTheory.test.ts +95 -0
  75. package/src/random.test.ts +32 -0
  76. package/src/randomDistributions.test.ts +348 -0
  77. package/src/randomRange.test.ts +99 -0
  78. package/src/rounding.test.ts +81 -0
  79. package/src/scalar.test.ts +58 -0
  80. package/src/statistics.test.ts +74 -0
@@ -0,0 +1,13 @@
1
+ /** Convert `degrees` to radians. */
2
+ export declare function degToRad(degrees: number): number;
3
+ /** Return the shortest signed difference between angles `from` and `to` (radians).
4
+ *
5
+ * The result is in `(-π, π]`. Useful for rotating a value toward a target by
6
+ * the minimum arc.
7
+ */
8
+ export declare function deltaAngle(from: number, to: number): number;
9
+ /** Wrap `radians` to the range `[-π, π)`. */
10
+ export declare function normalizeAngle(radians: number): number;
11
+ /** Convert `radians` to degrees. */
12
+ export declare function radToDeg(radians: number): number;
13
+ //# sourceMappingURL=angle.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"angle.d.ts","sourceRoot":"","sources":["../src/angle.ts"],"names":[],"mappings":"AAEA,oCAAoC;AACpC,wBAAgB,QAAQ,CAAC,OAAO,EAAE,MAAM,GAAG,MAAM,CAEhD;AAED;;;;GAIG;AACH,wBAAgB,UAAU,CAAC,IAAI,EAAE,MAAM,EAAE,EAAE,EAAE,MAAM,GAAG,MAAM,CAG3D;AAED,6CAA6C;AAC7C,wBAAgB,cAAc,CAAC,OAAO,EAAE,MAAM,GAAG,MAAM,CAGtD;AAED,oCAAoC;AACpC,wBAAgB,QAAQ,CAAC,OAAO,EAAE,MAAM,GAAG,MAAM,CAEhD"}
package/dist/angle.js ADDED
@@ -0,0 +1,24 @@
1
+ import { DEG_TO_RAD, RAD_TO_DEG, TAU } from './constants';
2
+ /** Convert `degrees` to radians. */
3
+ export function degToRad(degrees) {
4
+ return degrees * DEG_TO_RAD;
5
+ }
6
+ /** Return the shortest signed difference between angles `from` and `to` (radians).
7
+ *
8
+ * The result is in `(-π, π]`. Useful for rotating a value toward a target by
9
+ * the minimum arc.
10
+ */
11
+ export function deltaAngle(from, to) {
12
+ const diff = (((to - from) % TAU) + TAU) % TAU;
13
+ return diff > Math.PI ? diff - TAU : diff;
14
+ }
15
+ /** Wrap `radians` to the range `[-π, π)`. */
16
+ export function normalizeAngle(radians) {
17
+ const wrapped = ((radians % TAU) + TAU) % TAU;
18
+ return wrapped >= Math.PI ? wrapped - TAU : wrapped;
19
+ }
20
+ /** Convert `radians` to degrees. */
21
+ export function radToDeg(radians) {
22
+ return radians * RAD_TO_DEG;
23
+ }
24
+ //# sourceMappingURL=angle.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"angle.js","sourceRoot":"","sources":["../src/angle.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,UAAU,EAAE,GAAG,EAAE,MAAM,aAAa,CAAC;AAE1D,oCAAoC;AACpC,MAAM,UAAU,QAAQ,CAAC,OAAe;IACtC,OAAO,OAAO,GAAG,UAAU,CAAC;AAC9B,CAAC;AAED;;;;GAIG;AACH,MAAM,UAAU,UAAU,CAAC,IAAY,EAAE,EAAU;IACjD,MAAM,IAAI,GAAG,CAAC,CAAC,CAAC,EAAE,GAAG,IAAI,CAAC,GAAG,GAAG,CAAC,GAAG,GAAG,CAAC,GAAG,GAAG,CAAC;IAC/C,OAAO,IAAI,GAAG,IAAI,CAAC,EAAE,CAAC,CAAC,CAAC,IAAI,GAAG,GAAG,CAAC,CAAC,CAAC,IAAI,CAAC;AAC5C,CAAC;AAED,6CAA6C;AAC7C,MAAM,UAAU,cAAc,CAAC,OAAe;IAC5C,MAAM,OAAO,GAAG,CAAC,CAAC,OAAO,GAAG,GAAG,CAAC,GAAG,GAAG,CAAC,GAAG,GAAG,CAAC;IAC9C,OAAO,OAAO,IAAI,IAAI,CAAC,EAAE,CAAC,CAAC,CAAC,OAAO,GAAG,GAAG,CAAC,CAAC,CAAC,OAAO,CAAC;AACtD,CAAC;AAED,oCAAoC;AACpC,MAAM,UAAU,QAAQ,CAAC,OAAe;IACtC,OAAO,OAAO,GAAG,UAAU,CAAC;AAC9B,CAAC"}
@@ -0,0 +1,15 @@
1
+ /** Clamp `value` to the inclusive range `[min, max]`.
2
+ *
3
+ * When `min > max` the result is `min`. NaN propagates unchanged.
4
+ */
5
+ export declare function clamp(value: number, min: number, max: number): number;
6
+ /** Return `true` if `value` is within the inclusive range `[min, max]`. */
7
+ export declare function inRange(value: number, min: number, max: number): boolean;
8
+ /** Clamp `value` to `[0, 1]`. Equivalent to `clamp(value, 0, 1)`.
9
+ *
10
+ * Named `saturate` after the GPU HLSL/GLSL convention. Follows GPU semantics:
11
+ * NaN returns 0 (unlike `clamp`, which propagates NaN). Safe in hot loops,
12
+ * no allocation.
13
+ */
14
+ export declare function saturate(value: number): number;
15
+ //# sourceMappingURL=clamp.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"clamp.d.ts","sourceRoot":"","sources":["../src/clamp.ts"],"names":[],"mappings":"AAAA;;;GAGG;AACH,wBAAgB,KAAK,CAAC,KAAK,EAAE,MAAM,EAAE,GAAG,EAAE,MAAM,EAAE,GAAG,EAAE,MAAM,GAAG,MAAM,CAErE;AAED,2EAA2E;AAC3E,wBAAgB,OAAO,CAAC,KAAK,EAAE,MAAM,EAAE,GAAG,EAAE,MAAM,EAAE,GAAG,EAAE,MAAM,GAAG,OAAO,CAExE;AAED;;;;;GAKG;AACH,wBAAgB,QAAQ,CAAC,KAAK,EAAE,MAAM,GAAG,MAAM,CAG9C"}
package/dist/clamp.js ADDED
@@ -0,0 +1,23 @@
1
+ /** Clamp `value` to the inclusive range `[min, max]`.
2
+ *
3
+ * When `min > max` the result is `min`. NaN propagates unchanged.
4
+ */
5
+ export function clamp(value, min, max) {
6
+ return value < min ? min : value > max ? max : value;
7
+ }
8
+ /** Return `true` if `value` is within the inclusive range `[min, max]`. */
9
+ export function inRange(value, min, max) {
10
+ return value >= min && value <= max;
11
+ }
12
+ /** Clamp `value` to `[0, 1]`. Equivalent to `clamp(value, 0, 1)`.
13
+ *
14
+ * Named `saturate` after the GPU HLSL/GLSL convention. Follows GPU semantics:
15
+ * NaN returns 0 (unlike `clamp`, which propagates NaN). Safe in hot loops,
16
+ * no allocation.
17
+ */
18
+ export function saturate(value) {
19
+ if (value !== value)
20
+ return 0;
21
+ return value < 0 ? 0 : value > 1 ? 1 : value;
22
+ }
23
+ //# sourceMappingURL=clamp.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"clamp.js","sourceRoot":"","sources":["../src/clamp.ts"],"names":[],"mappings":"AAAA;;;GAGG;AACH,MAAM,UAAU,KAAK,CAAC,KAAa,EAAE,GAAW,EAAE,GAAW;IAC3D,OAAO,KAAK,GAAG,GAAG,CAAC,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,KAAK,GAAG,GAAG,CAAC,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,KAAK,CAAC;AACvD,CAAC;AAED,2EAA2E;AAC3E,MAAM,UAAU,OAAO,CAAC,KAAa,EAAE,GAAW,EAAE,GAAW;IAC7D,OAAO,KAAK,IAAI,GAAG,IAAI,KAAK,IAAI,GAAG,CAAC;AACtC,CAAC;AAED;;;;;GAKG;AACH,MAAM,UAAU,QAAQ,CAAC,KAAa;IACpC,IAAI,KAAK,KAAK,KAAK;QAAE,OAAO,CAAC,CAAC;IAC9B,OAAO,KAAK,GAAG,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,KAAK,GAAG,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,KAAK,CAAC;AAC/C,CAAC"}
@@ -0,0 +1,17 @@
1
+ /** Return `true` if `|a - b| <= epsilon`.
2
+ *
3
+ * Uses an absolute epsilon, appropriate for values near zero. For magnitudes
4
+ * much larger than 1, use `approxEqualRelative`. Defaults to the SDK `EPSILON`.
5
+ */
6
+ export declare function approxEqual(a: number, b: number, epsilon?: number): boolean;
7
+ /** Return `true` if `a` and `b` agree to within a relative tolerance.
8
+ *
9
+ * Uses `|a - b| <= relativeEpsilon * max(|a|, |b|)`, which scales with
10
+ * magnitude and is appropriate for large numbers where absolute epsilon
11
+ * comparisons fail. For values near zero, falls back to `EPSILON`
12
+ * (absolute). Defaults to `EPSILON` for `relativeEpsilon`.
13
+ */
14
+ export declare function approxEqualRelative(a: number, b: number, relativeEpsilon?: number): boolean;
15
+ /** Return `true` if `|value| <= epsilon`. */
16
+ export declare function approxZero(value: number, epsilon?: number): boolean;
17
+ //# sourceMappingURL=comparison.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"comparison.d.ts","sourceRoot":"","sources":["../src/comparison.ts"],"names":[],"mappings":"AAEA;;;;GAIG;AACH,wBAAgB,WAAW,CAAC,CAAC,EAAE,MAAM,EAAE,CAAC,EAAE,MAAM,EAAE,OAAO,GAAE,MAAgB,GAAG,OAAO,CAEpF;AAED;;;;;;GAMG;AACH,wBAAgB,mBAAmB,CAAC,CAAC,EAAE,MAAM,EAAE,CAAC,EAAE,MAAM,EAAE,eAAe,GAAE,MAAgB,GAAG,OAAO,CAIpG;AAED,6CAA6C;AAC7C,wBAAgB,UAAU,CAAC,KAAK,EAAE,MAAM,EAAE,OAAO,GAAE,MAAgB,GAAG,OAAO,CAE5E"}
@@ -0,0 +1,26 @@
1
+ import { EPSILON } from './constants';
2
+ /** Return `true` if `|a - b| <= epsilon`.
3
+ *
4
+ * Uses an absolute epsilon, appropriate for values near zero. For magnitudes
5
+ * much larger than 1, use `approxEqualRelative`. Defaults to the SDK `EPSILON`.
6
+ */
7
+ export function approxEqual(a, b, epsilon = EPSILON) {
8
+ return Math.abs(a - b) <= epsilon;
9
+ }
10
+ /** Return `true` if `a` and `b` agree to within a relative tolerance.
11
+ *
12
+ * Uses `|a - b| <= relativeEpsilon * max(|a|, |b|)`, which scales with
13
+ * magnitude and is appropriate for large numbers where absolute epsilon
14
+ * comparisons fail. For values near zero, falls back to `EPSILON`
15
+ * (absolute). Defaults to `EPSILON` for `relativeEpsilon`.
16
+ */
17
+ export function approxEqualRelative(a, b, relativeEpsilon = EPSILON) {
18
+ const diff = Math.abs(a - b);
19
+ const largest = Math.max(Math.abs(a), Math.abs(b));
20
+ return diff <= Math.max(relativeEpsilon * largest, EPSILON);
21
+ }
22
+ /** Return `true` if `|value| <= epsilon`. */
23
+ export function approxZero(value, epsilon = EPSILON) {
24
+ return Math.abs(value) <= epsilon;
25
+ }
26
+ //# sourceMappingURL=comparison.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"comparison.js","sourceRoot":"","sources":["../src/comparison.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,EAAE,MAAM,aAAa,CAAC;AAEtC;;;;GAIG;AACH,MAAM,UAAU,WAAW,CAAC,CAAS,EAAE,CAAS,EAAE,UAAkB,OAAO;IACzE,OAAO,IAAI,CAAC,GAAG,CAAC,CAAC,GAAG,CAAC,CAAC,IAAI,OAAO,CAAC;AACpC,CAAC;AAED;;;;;;GAMG;AACH,MAAM,UAAU,mBAAmB,CAAC,CAAS,EAAE,CAAS,EAAE,kBAA0B,OAAO;IACzF,MAAM,IAAI,GAAG,IAAI,CAAC,GAAG,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC;IAC7B,MAAM,OAAO,GAAG,IAAI,CAAC,GAAG,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC,CAAC,EAAE,IAAI,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,CAAC;IACnD,OAAO,IAAI,IAAI,IAAI,CAAC,GAAG,CAAC,eAAe,GAAG,OAAO,EAAE,OAAO,CAAC,CAAC;AAC9D,CAAC;AAED,6CAA6C;AAC7C,MAAM,UAAU,UAAU,CAAC,KAAa,EAAE,UAAkB,OAAO;IACjE,OAAO,IAAI,CAAC,GAAG,CAAC,KAAK,CAAC,IAAI,OAAO,CAAC;AACpC,CAAC"}
@@ -0,0 +1,11 @@
1
+ /** Smallest positive number distinguishable from zero in floating-point comparisons. */
2
+ export declare const EPSILON = 0.000001;
3
+ /** Full circle in radians (2π). Prefer `TAU` over `2 * Math.PI` for clarity. */
4
+ export declare const TAU: number;
5
+ /** Half of π (π / 2). */
6
+ export declare const HALF_PI: number;
7
+ /** Multiply degrees by this constant to get radians. */
8
+ export declare const DEG_TO_RAD: number;
9
+ /** Multiply radians by this constant to get degrees. */
10
+ export declare const RAD_TO_DEG: number;
11
+ //# sourceMappingURL=constants.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"constants.d.ts","sourceRoot":"","sources":["../src/constants.ts"],"names":[],"mappings":"AAAA,wFAAwF;AACxF,eAAO,MAAM,OAAO,WAAO,CAAC;AAE5B,gFAAgF;AAChF,eAAO,MAAM,GAAG,QAAc,CAAC;AAE/B,yBAAyB;AACzB,eAAO,MAAM,OAAO,QAAc,CAAC;AAEnC,wDAAwD;AACxD,eAAO,MAAM,UAAU,QAAgB,CAAC;AAExC,wDAAwD;AACxD,eAAO,MAAM,UAAU,QAAgB,CAAC"}
@@ -0,0 +1,11 @@
1
+ /** Smallest positive number distinguishable from zero in floating-point comparisons. */
2
+ export const EPSILON = 1e-6;
3
+ /** Full circle in radians (2π). Prefer `TAU` over `2 * Math.PI` for clarity. */
4
+ export const TAU = Math.PI * 2;
5
+ /** Half of π (π / 2). */
6
+ export const HALF_PI = Math.PI / 2;
7
+ /** Multiply degrees by this constant to get radians. */
8
+ export const DEG_TO_RAD = Math.PI / 180;
9
+ /** Multiply radians by this constant to get degrees. */
10
+ export const RAD_TO_DEG = 180 / Math.PI;
11
+ //# sourceMappingURL=constants.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"constants.js","sourceRoot":"","sources":["../src/constants.ts"],"names":[],"mappings":"AAAA,wFAAwF;AACxF,MAAM,CAAC,MAAM,OAAO,GAAG,IAAI,CAAC;AAE5B,gFAAgF;AAChF,MAAM,CAAC,MAAM,GAAG,GAAG,IAAI,CAAC,EAAE,GAAG,CAAC,CAAC;AAE/B,yBAAyB;AACzB,MAAM,CAAC,MAAM,OAAO,GAAG,IAAI,CAAC,EAAE,GAAG,CAAC,CAAC;AAEnC,wDAAwD;AACxD,MAAM,CAAC,MAAM,UAAU,GAAG,IAAI,CAAC,EAAE,GAAG,GAAG,CAAC;AAExC,wDAAwD;AACxD,MAAM,CAAC,MAAM,UAAU,GAAG,GAAG,GAAG,IAAI,CAAC,EAAE,CAAC"}
package/dist/hash.d.ts ADDED
@@ -0,0 +1,35 @@
1
+ import type { RandomSource } from '@flighthq/types';
2
+ /** Create a `RandomSource` seeded from a deterministic hash of `x` and `y`.
3
+ *
4
+ * Connects the stateless hash layer to the seeded-PRNG layer — each grid
5
+ * cell gets an independent reproducible random stream.
6
+ *
7
+ * ```ts
8
+ * const rng = createRandomSourceFromHash(tileX, tileY);
9
+ * const treasure = rng() < 0.05; // 5% chance, reproducible per tile
10
+ * ```
11
+ */
12
+ export declare function createRandomSourceFromHash(x: number, y: number): RandomSource;
13
+ /** Stateless position-seeded hash over a 2D integer grid.
14
+ *
15
+ * Returns a deterministic `number` in `[0, 2³²)`. Suitable for procedural
16
+ * tile/cell content and gradient noise seed generation.
17
+ */
18
+ export declare function hash2D(x: number, y: number): number;
19
+ /** Stateless position-seeded hash over a 3D integer grid. */
20
+ export declare function hash3D(x: number, y: number, z: number): number;
21
+ /** Combine a running hash `seed` with a new `value` using a Murmur3-inspired mix.
22
+ *
23
+ * Used to build multi-dimensional hashes from `hashUint32`:
24
+ * ```ts
25
+ * const h = hashCombine(hashUint32(x), y);
26
+ * ```
27
+ */
28
+ export declare function hashCombine(seed: number, value: number): number;
29
+ /** Finalise an arbitrary 32-bit integer through the fmix32 finalizer (Murmur3).
30
+ *
31
+ * Produces a deterministic, well-distributed 32-bit unsigned integer from any
32
+ * input. Useful as a stateless hash function for individual values.
33
+ */
34
+ export declare function hashUint32(value: number): number;
35
+ //# sourceMappingURL=hash.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"hash.d.ts","sourceRoot":"","sources":["../src/hash.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,iBAAiB,CAAC;AAIpD;;;;;;;;;GASG;AACH,wBAAgB,0BAA0B,CAAC,CAAC,EAAE,MAAM,EAAE,CAAC,EAAE,MAAM,GAAG,YAAY,CAE7E;AAED;;;;GAIG;AACH,wBAAgB,MAAM,CAAC,CAAC,EAAE,MAAM,EAAE,CAAC,EAAE,MAAM,GAAG,MAAM,CAEnD;AAED,6DAA6D;AAC7D,wBAAgB,MAAM,CAAC,CAAC,EAAE,MAAM,EAAE,CAAC,EAAE,MAAM,EAAE,CAAC,EAAE,MAAM,GAAG,MAAM,CAE9D;AAED;;;;;;GAMG;AACH,wBAAgB,WAAW,CAAC,IAAI,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM,GAAG,MAAM,CAE/D;AAED;;;;GAIG;AACH,wBAAgB,UAAU,CAAC,KAAK,EAAE,MAAM,GAAG,MAAM,CAQhD"}
package/dist/hash.js ADDED
@@ -0,0 +1,51 @@
1
+ import { createRandomSource } from './random';
2
+ /** Create a `RandomSource` seeded from a deterministic hash of `x` and `y`.
3
+ *
4
+ * Connects the stateless hash layer to the seeded-PRNG layer — each grid
5
+ * cell gets an independent reproducible random stream.
6
+ *
7
+ * ```ts
8
+ * const rng = createRandomSourceFromHash(tileX, tileY);
9
+ * const treasure = rng() < 0.05; // 5% chance, reproducible per tile
10
+ * ```
11
+ */
12
+ export function createRandomSourceFromHash(x, y) {
13
+ return createRandomSource(hash2D(x, y));
14
+ }
15
+ /** Stateless position-seeded hash over a 2D integer grid.
16
+ *
17
+ * Returns a deterministic `number` in `[0, 2³²)`. Suitable for procedural
18
+ * tile/cell content and gradient noise seed generation.
19
+ */
20
+ export function hash2D(x, y) {
21
+ return hashCombine(hashUint32(x | 0), y | 0);
22
+ }
23
+ /** Stateless position-seeded hash over a 3D integer grid. */
24
+ export function hash3D(x, y, z) {
25
+ return hashCombine(hash2D(x, y), z | 0);
26
+ }
27
+ /** Combine a running hash `seed` with a new `value` using a Murmur3-inspired mix.
28
+ *
29
+ * Used to build multi-dimensional hashes from `hashUint32`:
30
+ * ```ts
31
+ * const h = hashCombine(hashUint32(x), y);
32
+ * ```
33
+ */
34
+ export function hashCombine(seed, value) {
35
+ return hashUint32(seed ^ (value + 0x9e3779b9 + (seed << 6) + (seed >> 2)));
36
+ }
37
+ /** Finalise an arbitrary 32-bit integer through the fmix32 finalizer (Murmur3).
38
+ *
39
+ * Produces a deterministic, well-distributed 32-bit unsigned integer from any
40
+ * input. Useful as a stateless hash function for individual values.
41
+ */
42
+ export function hashUint32(value) {
43
+ let h = value | 0;
44
+ h ^= h >>> 16;
45
+ h = Math.imul(h, 0x85ebca6b) | 0;
46
+ h ^= h >>> 13;
47
+ h = Math.imul(h, 0xc2b2ae35) | 0;
48
+ h ^= h >>> 16;
49
+ return h >>> 0;
50
+ }
51
+ //# sourceMappingURL=hash.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"hash.js","sourceRoot":"","sources":["../src/hash.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,kBAAkB,EAAE,MAAM,UAAU,CAAC;AAE9C;;;;;;;;;GASG;AACH,MAAM,UAAU,0BAA0B,CAAC,CAAS,EAAE,CAAS;IAC7D,OAAO,kBAAkB,CAAC,MAAM,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,CAAC;AAC1C,CAAC;AAED;;;;GAIG;AACH,MAAM,UAAU,MAAM,CAAC,CAAS,EAAE,CAAS;IACzC,OAAO,WAAW,CAAC,UAAU,CAAC,CAAC,GAAG,CAAC,CAAC,EAAE,CAAC,GAAG,CAAC,CAAC,CAAC;AAC/C,CAAC;AAED,6DAA6D;AAC7D,MAAM,UAAU,MAAM,CAAC,CAAS,EAAE,CAAS,EAAE,CAAS;IACpD,OAAO,WAAW,CAAC,MAAM,CAAC,CAAC,EAAE,CAAC,CAAC,EAAE,CAAC,GAAG,CAAC,CAAC,CAAC;AAC1C,CAAC;AAED;;;;;;GAMG;AACH,MAAM,UAAU,WAAW,CAAC,IAAY,EAAE,KAAa;IACrD,OAAO,UAAU,CAAC,IAAI,GAAG,CAAC,KAAK,GAAG,UAAU,GAAG,CAAC,IAAI,IAAI,CAAC,CAAC,GAAG,CAAC,IAAI,IAAI,CAAC,CAAC,CAAC,CAAC,CAAC;AAC7E,CAAC;AAED;;;;GAIG;AACH,MAAM,UAAU,UAAU,CAAC,KAAa;IACtC,IAAI,CAAC,GAAG,KAAK,GAAG,CAAC,CAAC;IAClB,CAAC,IAAI,CAAC,KAAK,EAAE,CAAC;IACd,CAAC,GAAG,IAAI,CAAC,IAAI,CAAC,CAAC,EAAE,UAAU,CAAC,GAAG,CAAC,CAAC;IACjC,CAAC,IAAI,CAAC,KAAK,EAAE,CAAC;IACd,CAAC,GAAG,IAAI,CAAC,IAAI,CAAC,CAAC,EAAE,UAAU,CAAC,GAAG,CAAC,CAAC;IACjC,CAAC,IAAI,CAAC,KAAK,EAAE,CAAC;IACd,OAAO,CAAC,KAAK,CAAC,CAAC;AACjB,CAAC"}
@@ -0,0 +1,16 @@
1
+ export * from './angle';
2
+ export * from './clamp';
3
+ export * from './comparison';
4
+ export * from './constants';
5
+ export * from './hash';
6
+ export * from './interpolation';
7
+ export * from './interpolationAdvanced';
8
+ export * from './nextPowerOfTwo';
9
+ export * from './numberTheory';
10
+ export * from './random';
11
+ export * from './randomDistributions';
12
+ export * from './randomRange';
13
+ export * from './rounding';
14
+ export * from './scalar';
15
+ export * from './statistics';
16
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,SAAS,CAAC;AACxB,cAAc,SAAS,CAAC;AACxB,cAAc,cAAc,CAAC;AAC7B,cAAc,aAAa,CAAC;AAC5B,cAAc,QAAQ,CAAC;AACvB,cAAc,iBAAiB,CAAC;AAChC,cAAc,yBAAyB,CAAC;AACxC,cAAc,kBAAkB,CAAC;AACjC,cAAc,gBAAgB,CAAC;AAC/B,cAAc,UAAU,CAAC;AACzB,cAAc,uBAAuB,CAAC;AACtC,cAAc,eAAe,CAAC;AAC9B,cAAc,YAAY,CAAC;AAC3B,cAAc,UAAU,CAAC;AACzB,cAAc,cAAc,CAAC"}
package/dist/index.js ADDED
@@ -0,0 +1,16 @@
1
+ export * from './angle';
2
+ export * from './clamp';
3
+ export * from './comparison';
4
+ export * from './constants';
5
+ export * from './hash';
6
+ export * from './interpolation';
7
+ export * from './interpolationAdvanced';
8
+ export * from './nextPowerOfTwo';
9
+ export * from './numberTheory';
10
+ export * from './random';
11
+ export * from './randomDistributions';
12
+ export * from './randomRange';
13
+ export * from './rounding';
14
+ export * from './scalar';
15
+ export * from './statistics';
16
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,SAAS,CAAC;AACxB,cAAc,SAAS,CAAC;AACxB,cAAc,cAAc,CAAC;AAC7B,cAAc,aAAa,CAAC;AAC5B,cAAc,QAAQ,CAAC;AACvB,cAAc,iBAAiB,CAAC;AAChC,cAAc,yBAAyB,CAAC;AACxC,cAAc,kBAAkB,CAAC;AACjC,cAAc,gBAAgB,CAAC;AAC/B,cAAc,UAAU,CAAC;AACzB,cAAc,uBAAuB,CAAC;AACtC,cAAc,eAAe,CAAC;AAC9B,cAAc,YAAY,CAAC;AAC3B,cAAc,UAAU,CAAC;AACzB,cAAc,cAAc,CAAC"}
@@ -0,0 +1,27 @@
1
+ /** Return the `t` value that maps to `value` in the range `[a, b]`.
2
+ *
3
+ * The inverse of `lerp`: `inverseLerp(lerp(a, b, t), a, b) === t` when
4
+ * `a !== b`. Returns `0` when `a === b` to avoid division by zero.
5
+ */
6
+ export declare function inverseLerp(a: number, b: number, value: number): number;
7
+ /** Linearly interpolate between `a` and `b` by factor `t`.
8
+ *
9
+ * `t = 0` returns `a`; `t = 1` returns `b`. `t` is not clamped — pass a
10
+ * saturated `t` for clamped behaviour. Safe in hot loops, no allocation.
11
+ */
12
+ export declare function lerp(a: number, b: number, t: number): number;
13
+ /** Linearly interpolate `value` from `[inMin, inMax]` into `[outMin, outMax]`.
14
+ *
15
+ * Equivalent to `lerp(outMin, outMax, inverseLerp(inMin, inMax, value))`.
16
+ * Input range is not clamped. Returns `outMin` when `inMin === inMax`.
17
+ */
18
+ export declare function remap(value: number, inMin: number, inMax: number, outMin: number, outMax: number): number;
19
+ /** Hermite interpolation between `edge0` and `edge1` with a smooth ease curve.
20
+ *
21
+ * Returns 0 for `x <= edge0`, 1 for `x >= edge1`, and smoothly interpolates
22
+ * in between using `3t² - 2t³`. This is the same `smoothstep` found in GLSL.
23
+ */
24
+ export declare function smoothStep(edge0: number, edge1: number, x: number): number;
25
+ /** Returns 0 if `x < edge`, otherwise 1. The scalar step function from GLSL. */
26
+ export declare function step(edge: number, x: number): number;
27
+ //# sourceMappingURL=interpolation.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"interpolation.d.ts","sourceRoot":"","sources":["../src/interpolation.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AACH,wBAAgB,WAAW,CAAC,CAAC,EAAE,MAAM,EAAE,CAAC,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM,GAAG,MAAM,CAGvE;AAED;;;;GAIG;AACH,wBAAgB,IAAI,CAAC,CAAC,EAAE,MAAM,EAAE,CAAC,EAAE,MAAM,EAAE,CAAC,EAAE,MAAM,GAAG,MAAM,CAE5D;AAED;;;;GAIG;AACH,wBAAgB,KAAK,CAAC,KAAK,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,GAAG,MAAM,CAIzG;AAED;;;;GAIG;AACH,wBAAgB,UAAU,CAAC,KAAK,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM,EAAE,CAAC,EAAE,MAAM,GAAG,MAAM,CAI1E;AAED,gFAAgF;AAChF,wBAAgB,IAAI,CAAC,IAAI,EAAE,MAAM,EAAE,CAAC,EAAE,MAAM,GAAG,MAAM,CAEpD"}
@@ -0,0 +1,43 @@
1
+ /** Return the `t` value that maps to `value` in the range `[a, b]`.
2
+ *
3
+ * The inverse of `lerp`: `inverseLerp(lerp(a, b, t), a, b) === t` when
4
+ * `a !== b`. Returns `0` when `a === b` to avoid division by zero.
5
+ */
6
+ export function inverseLerp(a, b, value) {
7
+ const range = b - a;
8
+ return range === 0 ? 0 : (value - a) / range;
9
+ }
10
+ /** Linearly interpolate between `a` and `b` by factor `t`.
11
+ *
12
+ * `t = 0` returns `a`; `t = 1` returns `b`. `t` is not clamped — pass a
13
+ * saturated `t` for clamped behaviour. Safe in hot loops, no allocation.
14
+ */
15
+ export function lerp(a, b, t) {
16
+ return a + (b - a) * t;
17
+ }
18
+ /** Linearly interpolate `value` from `[inMin, inMax]` into `[outMin, outMax]`.
19
+ *
20
+ * Equivalent to `lerp(outMin, outMax, inverseLerp(inMin, inMax, value))`.
21
+ * Input range is not clamped. Returns `outMin` when `inMin === inMax`.
22
+ */
23
+ export function remap(value, inMin, inMax, outMin, outMax) {
24
+ const inRange = inMax - inMin;
25
+ if (inRange === 0)
26
+ return outMin;
27
+ return outMin + ((value - inMin) / inRange) * (outMax - outMin);
28
+ }
29
+ /** Hermite interpolation between `edge0` and `edge1` with a smooth ease curve.
30
+ *
31
+ * Returns 0 for `x <= edge0`, 1 for `x >= edge1`, and smoothly interpolates
32
+ * in between using `3t² - 2t³`. This is the same `smoothstep` found in GLSL.
33
+ */
34
+ export function smoothStep(edge0, edge1, x) {
35
+ const t = (x - edge0) / (edge1 - edge0);
36
+ const s = t < 0 ? 0 : t > 1 ? 1 : t;
37
+ return s * s * (3 - 2 * s);
38
+ }
39
+ /** Returns 0 if `x < edge`, otherwise 1. The scalar step function from GLSL. */
40
+ export function step(edge, x) {
41
+ return x < edge ? 0 : 1;
42
+ }
43
+ //# sourceMappingURL=interpolation.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"interpolation.js","sourceRoot":"","sources":["../src/interpolation.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AACH,MAAM,UAAU,WAAW,CAAC,CAAS,EAAE,CAAS,EAAE,KAAa;IAC7D,MAAM,KAAK,GAAG,CAAC,GAAG,CAAC,CAAC;IACpB,OAAO,KAAK,KAAK,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,KAAK,GAAG,CAAC,CAAC,GAAG,KAAK,CAAC;AAC/C,CAAC;AAED;;;;GAIG;AACH,MAAM,UAAU,IAAI,CAAC,CAAS,EAAE,CAAS,EAAE,CAAS;IAClD,OAAO,CAAC,GAAG,CAAC,CAAC,GAAG,CAAC,CAAC,GAAG,CAAC,CAAC;AACzB,CAAC;AAED;;;;GAIG;AACH,MAAM,UAAU,KAAK,CAAC,KAAa,EAAE,KAAa,EAAE,KAAa,EAAE,MAAc,EAAE,MAAc;IAC/F,MAAM,OAAO,GAAG,KAAK,GAAG,KAAK,CAAC;IAC9B,IAAI,OAAO,KAAK,CAAC;QAAE,OAAO,MAAM,CAAC;IACjC,OAAO,MAAM,GAAG,CAAC,CAAC,KAAK,GAAG,KAAK,CAAC,GAAG,OAAO,CAAC,GAAG,CAAC,MAAM,GAAG,MAAM,CAAC,CAAC;AAClE,CAAC;AAED;;;;GAIG;AACH,MAAM,UAAU,UAAU,CAAC,KAAa,EAAE,KAAa,EAAE,CAAS;IAChE,MAAM,CAAC,GAAG,CAAC,CAAC,GAAG,KAAK,CAAC,GAAG,CAAC,KAAK,GAAG,KAAK,CAAC,CAAC;IACxC,MAAM,CAAC,GAAG,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC;IACpC,OAAO,CAAC,GAAG,CAAC,GAAG,CAAC,CAAC,GAAG,CAAC,GAAG,CAAC,CAAC,CAAC;AAC7B,CAAC;AAED,gFAAgF;AAChF,MAAM,UAAU,IAAI,CAAC,IAAY,EAAE,CAAS;IAC1C,OAAO,CAAC,GAAG,IAAI,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC;AAC1B,CAAC"}
@@ -0,0 +1,42 @@
1
+ /** Frame-rate-independent exponential decay toward `target`.
2
+ *
3
+ * Equivalent to `lerp(current, target, 1 - exp(-lambda * deltaTime))`.
4
+ * Higher `lambda` = faster convergence. This is the classic "damp" function
5
+ * from Freya Holmér / Game Math — it does not overshoot and is independent of
6
+ * frame rate. `deltaTime` should be in seconds.
7
+ *
8
+ * Returns `current` when `deltaTime <= 0` or `lambda <= 0`.
9
+ */
10
+ export declare function damp(current: number, target: number, lambda: number, deltaTime: number): number;
11
+ /** Linearly interpolate between two angles `a` and `b` (radians) by factor `t`,
12
+ * taking the shortest arc.
13
+ *
14
+ * `t` is not clamped. The result wraps naturally; there is no explicit
15
+ * `normalizeAngle` call on the output — callers that need the result in a
16
+ * specific range should normalize it themselves.
17
+ */
18
+ export declare function lerpAngle(a: number, b: number, t: number): number;
19
+ /** Move `current` toward `target` by at most `maxDelta`, without overshooting.
20
+ *
21
+ * `maxDelta` should be positive; passing a negative `maxDelta` moves away
22
+ * from `target` instead.
23
+ */
24
+ export declare function moveTowards(current: number, target: number, maxDelta: number): number;
25
+ /** Bounce `t` back and forth over `[0, length]` like a ping-pong ball.
26
+ *
27
+ * `pingPong(t, 1)` cycles: 0 → 1 → 0 → 1 → …
28
+ */
29
+ export declare function pingPong(t: number, length: number): number;
30
+ /** Wrap `t` over `[0, length)`.
31
+ *
32
+ * `repeat(1.6, 1)` → `0.6`. Analogous to Unity's `Mathf.Repeat`.
33
+ */
34
+ export declare function repeat(t: number, length: number): number;
35
+ /** Hermite interpolation with a smoother quintic ease curve (Ken Perlin).
36
+ *
37
+ * Uses `6t⁵ - 15t⁴ + 10t³` — has zero first- _and_ second-derivative at the
38
+ * edges, making it smoother than the cubic `smoothStep`. Commonly called
39
+ * `smootherstep`. Returns 0 for `x <= edge0`, 1 for `x >= edge1`.
40
+ */
41
+ export declare function smootherStep(edge0: number, edge1: number, x: number): number;
42
+ //# sourceMappingURL=interpolationAdvanced.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"interpolationAdvanced.d.ts","sourceRoot":"","sources":["../src/interpolationAdvanced.ts"],"names":[],"mappings":"AAAA;;;;;;;;GAQG;AACH,wBAAgB,IAAI,CAAC,OAAO,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,EAAE,SAAS,EAAE,MAAM,GAAG,MAAM,CAG/F;AAED;;;;;;GAMG;AACH,wBAAgB,SAAS,CAAC,CAAC,EAAE,MAAM,EAAE,CAAC,EAAE,MAAM,EAAE,CAAC,EAAE,MAAM,GAAG,MAAM,CAKjE;AAED;;;;GAIG;AACH,wBAAgB,WAAW,CAAC,OAAO,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,EAAE,QAAQ,EAAE,MAAM,GAAG,MAAM,CAIrF;AAED;;;GAGG;AACH,wBAAgB,QAAQ,CAAC,CAAC,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,GAAG,MAAM,CAK1D;AAED;;;GAGG;AACH,wBAAgB,MAAM,CAAC,CAAC,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,GAAG,MAAM,CAGxD;AAED;;;;;GAKG;AACH,wBAAgB,YAAY,CAAC,KAAK,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM,EAAE,CAAC,EAAE,MAAM,GAAG,MAAM,CAI5E"}
@@ -0,0 +1,71 @@
1
+ /** Frame-rate-independent exponential decay toward `target`.
2
+ *
3
+ * Equivalent to `lerp(current, target, 1 - exp(-lambda * deltaTime))`.
4
+ * Higher `lambda` = faster convergence. This is the classic "damp" function
5
+ * from Freya Holmér / Game Math — it does not overshoot and is independent of
6
+ * frame rate. `deltaTime` should be in seconds.
7
+ *
8
+ * Returns `current` when `deltaTime <= 0` or `lambda <= 0`.
9
+ */
10
+ export function damp(current, target, lambda, deltaTime) {
11
+ if (deltaTime <= 0 || lambda <= 0)
12
+ return current;
13
+ return target + (current - target) * Math.exp(-lambda * deltaTime);
14
+ }
15
+ /** Linearly interpolate between two angles `a` and `b` (radians) by factor `t`,
16
+ * taking the shortest arc.
17
+ *
18
+ * `t` is not clamped. The result wraps naturally; there is no explicit
19
+ * `normalizeAngle` call on the output — callers that need the result in a
20
+ * specific range should normalize it themselves.
21
+ */
22
+ export function lerpAngle(a, b, t) {
23
+ const TAU = Math.PI * 2;
24
+ let diff = (((b - a) % TAU) + TAU) % TAU;
25
+ if (diff > Math.PI)
26
+ diff -= TAU;
27
+ return a + diff * t;
28
+ }
29
+ /** Move `current` toward `target` by at most `maxDelta`, without overshooting.
30
+ *
31
+ * `maxDelta` should be positive; passing a negative `maxDelta` moves away
32
+ * from `target` instead.
33
+ */
34
+ export function moveTowards(current, target, maxDelta) {
35
+ const delta = target - current;
36
+ if (Math.abs(delta) <= maxDelta)
37
+ return target;
38
+ return current + Math.sign(delta) * maxDelta;
39
+ }
40
+ /** Bounce `t` back and forth over `[0, length]` like a ping-pong ball.
41
+ *
42
+ * `pingPong(t, 1)` cycles: 0 → 1 → 0 → 1 → …
43
+ */
44
+ export function pingPong(t, length) {
45
+ if (length <= 0)
46
+ return 0;
47
+ const cycle = 2 * length;
48
+ const mod = ((t % cycle) + cycle) % cycle;
49
+ return mod <= length ? mod : cycle - mod;
50
+ }
51
+ /** Wrap `t` over `[0, length)`.
52
+ *
53
+ * `repeat(1.6, 1)` → `0.6`. Analogous to Unity's `Mathf.Repeat`.
54
+ */
55
+ export function repeat(t, length) {
56
+ if (length <= 0)
57
+ return 0;
58
+ return ((t % length) + length) % length;
59
+ }
60
+ /** Hermite interpolation with a smoother quintic ease curve (Ken Perlin).
61
+ *
62
+ * Uses `6t⁵ - 15t⁴ + 10t³` — has zero first- _and_ second-derivative at the
63
+ * edges, making it smoother than the cubic `smoothStep`. Commonly called
64
+ * `smootherstep`. Returns 0 for `x <= edge0`, 1 for `x >= edge1`.
65
+ */
66
+ export function smootherStep(edge0, edge1, x) {
67
+ const t = (x - edge0) / (edge1 - edge0);
68
+ const s = t < 0 ? 0 : t > 1 ? 1 : t;
69
+ return s * s * s * (s * (s * 6 - 15) + 10);
70
+ }
71
+ //# sourceMappingURL=interpolationAdvanced.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"interpolationAdvanced.js","sourceRoot":"","sources":["../src/interpolationAdvanced.ts"],"names":[],"mappings":"AAAA;;;;;;;;GAQG;AACH,MAAM,UAAU,IAAI,CAAC,OAAe,EAAE,MAAc,EAAE,MAAc,EAAE,SAAiB;IACrF,IAAI,SAAS,IAAI,CAAC,IAAI,MAAM,IAAI,CAAC;QAAE,OAAO,OAAO,CAAC;IAClD,OAAO,MAAM,GAAG,CAAC,OAAO,GAAG,MAAM,CAAC,GAAG,IAAI,CAAC,GAAG,CAAC,CAAC,MAAM,GAAG,SAAS,CAAC,CAAC;AACrE,CAAC;AAED;;;;;;GAMG;AACH,MAAM,UAAU,SAAS,CAAC,CAAS,EAAE,CAAS,EAAE,CAAS;IACvD,MAAM,GAAG,GAAG,IAAI,CAAC,EAAE,GAAG,CAAC,CAAC;IACxB,IAAI,IAAI,GAAG,CAAC,CAAC,CAAC,CAAC,GAAG,CAAC,CAAC,GAAG,GAAG,CAAC,GAAG,GAAG,CAAC,GAAG,GAAG,CAAC;IACzC,IAAI,IAAI,GAAG,IAAI,CAAC,EAAE;QAAE,IAAI,IAAI,GAAG,CAAC;IAChC,OAAO,CAAC,GAAG,IAAI,GAAG,CAAC,CAAC;AACtB,CAAC;AAED;;;;GAIG;AACH,MAAM,UAAU,WAAW,CAAC,OAAe,EAAE,MAAc,EAAE,QAAgB;IAC3E,MAAM,KAAK,GAAG,MAAM,GAAG,OAAO,CAAC;IAC/B,IAAI,IAAI,CAAC,GAAG,CAAC,KAAK,CAAC,IAAI,QAAQ;QAAE,OAAO,MAAM,CAAC;IAC/C,OAAO,OAAO,GAAG,IAAI,CAAC,IAAI,CAAC,KAAK,CAAC,GAAG,QAAQ,CAAC;AAC/C,CAAC;AAED;;;GAGG;AACH,MAAM,UAAU,QAAQ,CAAC,CAAS,EAAE,MAAc;IAChD,IAAI,MAAM,IAAI,CAAC;QAAE,OAAO,CAAC,CAAC;IAC1B,MAAM,KAAK,GAAG,CAAC,GAAG,MAAM,CAAC;IACzB,MAAM,GAAG,GAAG,CAAC,CAAC,CAAC,GAAG,KAAK,CAAC,GAAG,KAAK,CAAC,GAAG,KAAK,CAAC;IAC1C,OAAO,GAAG,IAAI,MAAM,CAAC,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,KAAK,GAAG,GAAG,CAAC;AAC3C,CAAC;AAED;;;GAGG;AACH,MAAM,UAAU,MAAM,CAAC,CAAS,EAAE,MAAc;IAC9C,IAAI,MAAM,IAAI,CAAC;QAAE,OAAO,CAAC,CAAC;IAC1B,OAAO,CAAC,CAAC,CAAC,GAAG,MAAM,CAAC,GAAG,MAAM,CAAC,GAAG,MAAM,CAAC;AAC1C,CAAC;AAED;;;;;GAKG;AACH,MAAM,UAAU,YAAY,CAAC,KAAa,EAAE,KAAa,EAAE,CAAS;IAClE,MAAM,CAAC,GAAG,CAAC,CAAC,GAAG,KAAK,CAAC,GAAG,CAAC,KAAK,GAAG,KAAK,CAAC,CAAC;IACxC,MAAM,CAAC,GAAG,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC;IACpC,OAAO,CAAC,GAAG,CAAC,GAAG,CAAC,GAAG,CAAC,CAAC,GAAG,CAAC,CAAC,GAAG,CAAC,GAAG,EAAE,CAAC,GAAG,EAAE,CAAC,CAAC;AAC7C,CAAC"}
@@ -0,0 +1,22 @@
1
+ /** Return `true` if `n` is an integer power of two (i.e. 1, 2, 4, 8, …).
2
+ *
3
+ * Returns `false` for `n <= 0`.
4
+ */
5
+ export declare function isPowerOfTwo(n: number): boolean;
6
+ /** Round `value` up to the nearest multiple of `multiple`.
7
+ *
8
+ * `nextMultipleOf(7, 4)` → `8`. When `multiple <= 0` the result is `value`.
9
+ */
10
+ export declare function nextMultipleOf(value: number, multiple: number): number;
11
+ /** Round `n` up to the next power of two.
12
+ *
13
+ * Uses integer bit-twiddling for stability on large values. Returns `1` for
14
+ * `n <= 1`.
15
+ */
16
+ export declare function nextPowerOfTwo(n: number): number;
17
+ /** Round `n` down to the largest power of two that is `<= n`.
18
+ *
19
+ * `previousPowerOfTwo(6)` → `4`. Returns `1` for `n <= 1`.
20
+ */
21
+ export declare function previousPowerOfTwo(n: number): number;
22
+ //# sourceMappingURL=nextPowerOfTwo.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"nextPowerOfTwo.d.ts","sourceRoot":"","sources":["../src/nextPowerOfTwo.ts"],"names":[],"mappings":"AAAA;;;GAGG;AACH,wBAAgB,YAAY,CAAC,CAAC,EAAE,MAAM,GAAG,OAAO,CAE/C;AAED;;;GAGG;AACH,wBAAgB,cAAc,CAAC,KAAK,EAAE,MAAM,EAAE,QAAQ,EAAE,MAAM,GAAG,MAAM,CAItE;AAED;;;;GAIG;AACH,wBAAgB,cAAc,CAAC,CAAC,EAAE,MAAM,GAAG,MAAM,CAWhD;AAED;;;GAGG;AACH,wBAAgB,kBAAkB,CAAC,CAAC,EAAE,MAAM,GAAG,MAAM,CAUpD"}
@@ -0,0 +1,52 @@
1
+ /** Return `true` if `n` is an integer power of two (i.e. 1, 2, 4, 8, …).
2
+ *
3
+ * Returns `false` for `n <= 0`.
4
+ */
5
+ export function isPowerOfTwo(n) {
6
+ return n > 0 && (n & (n - 1)) === 0;
7
+ }
8
+ /** Round `value` up to the nearest multiple of `multiple`.
9
+ *
10
+ * `nextMultipleOf(7, 4)` → `8`. When `multiple <= 0` the result is `value`.
11
+ */
12
+ export function nextMultipleOf(value, multiple) {
13
+ if (multiple <= 0)
14
+ return value;
15
+ const remainder = value % multiple;
16
+ return remainder === 0 ? value : value + multiple - remainder;
17
+ }
18
+ /** Round `n` up to the next power of two.
19
+ *
20
+ * Uses integer bit-twiddling for stability on large values. Returns `1` for
21
+ * `n <= 1`.
22
+ */
23
+ export function nextPowerOfTwo(n) {
24
+ if (n <= 1)
25
+ return 1;
26
+ // Decrement so exact powers of two are unchanged, then propagate the
27
+ // highest set bit downward and add 1 to get the next power.
28
+ n = (n - 1) | 0;
29
+ n |= n >> 1;
30
+ n |= n >> 2;
31
+ n |= n >> 4;
32
+ n |= n >> 8;
33
+ n |= n >> 16;
34
+ return (n + 1) >>> 0;
35
+ }
36
+ /** Round `n` down to the largest power of two that is `<= n`.
37
+ *
38
+ * `previousPowerOfTwo(6)` → `4`. Returns `1` for `n <= 1`.
39
+ */
40
+ export function previousPowerOfTwo(n) {
41
+ if (n <= 1)
42
+ return 1;
43
+ // Fill all bits below the highest set bit, then shift right by 1.
44
+ n = n | 0;
45
+ n |= n >> 1;
46
+ n |= n >> 2;
47
+ n |= n >> 4;
48
+ n |= n >> 8;
49
+ n |= n >> 16;
50
+ return (n + 1) >> 1;
51
+ }
52
+ //# sourceMappingURL=nextPowerOfTwo.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"nextPowerOfTwo.js","sourceRoot":"","sources":["../src/nextPowerOfTwo.ts"],"names":[],"mappings":"AAAA;;;GAGG;AACH,MAAM,UAAU,YAAY,CAAC,CAAS;IACpC,OAAO,CAAC,GAAG,CAAC,IAAI,CAAC,CAAC,GAAG,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,KAAK,CAAC,CAAC;AACtC,CAAC;AAED;;;GAGG;AACH,MAAM,UAAU,cAAc,CAAC,KAAa,EAAE,QAAgB;IAC5D,IAAI,QAAQ,IAAI,CAAC;QAAE,OAAO,KAAK,CAAC;IAChC,MAAM,SAAS,GAAG,KAAK,GAAG,QAAQ,CAAC;IACnC,OAAO,SAAS,KAAK,CAAC,CAAC,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC,KAAK,GAAG,QAAQ,GAAG,SAAS,CAAC;AAChE,CAAC;AAED;;;;GAIG;AACH,MAAM,UAAU,cAAc,CAAC,CAAS;IACtC,IAAI,CAAC,IAAI,CAAC;QAAE,OAAO,CAAC,CAAC;IACrB,qEAAqE;IACrE,4DAA4D;IAC5D,CAAC,GAAG,CAAC,CAAC,GAAG,CAAC,CAAC,GAAG,CAAC,CAAC;IAChB,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;IACZ,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;IACZ,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;IACZ,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;IACZ,CAAC,IAAI,CAAC,IAAI,EAAE,CAAC;IACb,OAAO,CAAC,CAAC,GAAG,CAAC,CAAC,KAAK,CAAC,CAAC;AACvB,CAAC;AAED;;;GAGG;AACH,MAAM,UAAU,kBAAkB,CAAC,CAAS;IAC1C,IAAI,CAAC,IAAI,CAAC;QAAE,OAAO,CAAC,CAAC;IACrB,kEAAkE;IAClE,CAAC,GAAG,CAAC,GAAG,CAAC,CAAC;IACV,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;IACZ,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;IACZ,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;IACZ,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;IACZ,CAAC,IAAI,CAAC,IAAI,EAAE,CAAC;IACb,OAAO,CAAC,CAAC,GAAG,CAAC,CAAC,IAAI,CAAC,CAAC;AACtB,CAAC"}