From a2b575318dcb95e01a31111694d18462fb618d59 Mon Sep 17 00:00:00 2001 From: "Florine W. Dekker" Date: Thu, 7 Apr 2022 18:04:37 +0200 Subject: [PATCH] Add full-width canvas, and document PrimeMath --- Gruntfile.js | 2 +- package-lock.json | Bin 218005 -> 218005 bytes package.json | 6 +-- src/main/css/main.css | 3 ++ src/main/index.html | 15 +++----- src/main/js/PrimeMath.ts | 77 +++++++++++++++++++++++++++++++++++++++ 6 files changed, 90 insertions(+), 13 deletions(-) diff --git a/Gruntfile.js b/Gruntfile.js index bf1cbd9..8b97347 100644 --- a/Gruntfile.js +++ b/Gruntfile.js @@ -48,7 +48,7 @@ module.exports = grunt => { }, html: { files: ["src/main/**/*.html"], - tasks: ["copy:html"], + tasks: ["copy:html", "replace:dev"], }, ts: { files: ["src/main/**/*.ts"], diff --git a/package-lock.json b/package-lock.json index 13ad97a19cc3ab5ebc5d6bdc9315186b91ca12c8..4c0c5533617e704b7552f32f50e974688f06920e 100644 GIT binary patch delta 1034 zcmci9&ui0g7zS{@$>ye5L6phFg9^e%gr?2XCPQRR+U8e(EJ>TKy=CpPwn^G%ZP#Q6 z84hoPNSNP)pc@{B2qFpwbRt89T@?KbHqTz12ff;zz4!WFp7(h^Tf3pH-O#H$2%!9H zr!KMn*;2*SybN9sFYA=;BvYO#5muF~uJ94LG@tVd zVN0+qSBqwfnWoF~I&b>>qq)sPJ2sbu%vT#h%jb zO9qh^=GBFW)-G3^G>v7MRX)GQwCPHNuVG}AR2yAZ5G9RkD3XvcT&ADok{p{(YBuNA1oOFvXbn;rF*(7ENl4VE~3J(JK%{w l_xyUWy@@;?g?}RG^l;RV??;0h1Paa!7yP8P_@B6U;UD)XUnu|p delta 1051 zcmci9&ui0g7{_tGiE~p{L6pfv#0f*UcD7BMHo3^!G-=wjP1EMb(p%egYtyA`I@=^Y z$k_1YK_txYK~RT>A)-tKbD200VHb!0!5CgWs0Y2;dGMlFcYAm)A9%kuwmchKp6%NR zh&U4`&%^dxaKeFz2jFmb5*>T+6r4dKr1R>+7+l=}BhE|j@awP0`-ZdDCY>)!*PM=T zO48=-Qr4sdoY%rt+#45LO)I~cDDnPyx?lIl5(%BFRm!&A&zYJ$7mP7fE!pH3teh3l z*#_n%;v9v`mZUb!keKQ%i_u;kg8RVd%s#uiexSPtI@rUYaTySBdm6n6zrO%el8_at zu*CFPy-S)BCbK14R}`A8N@Y!3sLof-6>qX0W$F#yo3I0gRv}o)^#dx!8ue7bE-afA zO_n=yD`%;!L1%cIWVJuDP``tWxF0t`YXrq_;PBfOlz=4+9Ur7Qq`|Z(QevvR6b)o- zr4~u06_F5%YAuF`6`@AZn!re5Q^hf+yrSU}XAwjur*hO{oMx+hmhUi3u)`%Q38Lq> zsyRiL4618s;5m92zG=}b+daORo-1WVOvZbe zKHo}ovwb{i(MF~jt#gWiM^r1X`13+R>T(S|Z|HO?&0B=Vsx^B#g15O!xm{7hk+9xd z8QdBy?;|ru!QI)L=m#)JefBOg=3bgb1q80z;JW*12fVv=4DyF-NZL8jQ*T0u_49bt v2a`p(zX=}w4)A0hc{l`r1ktH~(o6q?UWXH(0p?DIP;mO#-MPQa?Q_2XCTn91 diff --git a/package.json b/package.json index 30e4708..86f2d96 100644 --- a/package.json +++ b/package.json @@ -24,9 +24,9 @@ "grunt-focus": "^1.0.0", "grunt-text-replace": "^0.4.0", "grunt-webpack": "^5.0.0", - "ts-loader": "^9.2.6", - "typescript": "^4.5.5", - "webpack": "^5.69.1", + "ts-loader": "^9.2.8", + "typescript": "^4.6.3", + "webpack": "^5.72.0", "webpack-cli": "^4.9.2" } } diff --git a/src/main/css/main.css b/src/main/css/main.css index e69de29..d521a21 100644 --- a/src/main/css/main.css +++ b/src/main/css/main.css @@ -0,0 +1,3 @@ +#canvasContainer { + margin: 2em; +} diff --git a/src/main/index.html b/src/main/index.html index 26c0913..0c0bb9a 100644 --- a/src/main/index.html +++ b/src/main/index.html @@ -46,16 +46,13 @@ + -
-
- -
- -
-
+
+
+
diff --git a/src/main/js/PrimeMath.ts b/src/main/js/PrimeMath.ts index bc239fa..adf41d9 100644 --- a/src/main/js/PrimeMath.ts +++ b/src/main/js/PrimeMath.ts @@ -1,17 +1,50 @@ +/** + * Exposes maths functions for prime numbers. + */ export interface PrimeMath { + /** + * Checks a number for primality. + * + * @param n the number to check for primality + * @return `true` if and only if `n` is prime + */ isPrime(n: number): boolean + /** + * Decomposes `n` into its prime factors. + * + * @param n the number to decompose + * @return the array of prime factors of `n` + */ decompose(n: number): number[] } +/** + * Simple implementation of `PrimeMath`. + */ export class SimplePrimeMath implements PrimeMath { + /** + * The singleton instance. + * + * @private + */ private static instance: SimplePrimeMath; + /** + * Constructs a new `SimplePrimeMath`. + * + * @private + */ private constructor() { // Do nothing } + /** + * Returns the singleton instance. + * + * @return the singleton instance + */ public static getInstance(): SimplePrimeMath { if (!SimplePrimeMath.instance) SimplePrimeMath.instance = new SimplePrimeMath(); @@ -52,18 +85,51 @@ export class SimplePrimeMath implements PrimeMath { } } +/** + * Implements `PrimeMath` such that all queries are cached so that subsequent queries are faster. + */ export class CachedPrimeMath implements PrimeMath { + /** + * The singleton instance. + * + * @private + */ private static instance: CachedPrimeMath; + /** + * The underlying `SimplePrimeMath` instance of which the answers are cached. + * + * @private + */ private static simple: SimplePrimeMath = SimplePrimeMath.getInstance(); + /** + * Cached outputs for `isPrime` invocations. + * + * @private + */ private isPrimeCache: Map = new Map(); + /** + * Cached outputs for `decomposeCache` invocations. + * + * @private + */ private decomposeCache: Map = new Map(); + /** + * Constructs a new instance. + * + * @private + */ private constructor() { // Do nothing } + /** + * Returns the singleton instance. + * + * @return the singleton instance + */ public static getInstance(): CachedPrimeMath { if (!CachedPrimeMath.instance) CachedPrimeMath.instance = new CachedPrimeMath(); @@ -81,6 +147,17 @@ export class CachedPrimeMath implements PrimeMath { } + /** + * Invokes `fun` given `input`, and stores its output in `cache`. + * + * If `fun(input)` has previously been invoked, then the previous output is returned immediately. + * + * @param input the input to pass to `fun` + * @param fun the function to invoke with `input` + * @param cache the cache to store the output of `fun` in, and to read previous outputs from + * @return the output of `fun(input)`, or the previously stored output if there is one + * @private + */ private static invokeCached(input: IN, fun: (_: IN) => OUT, cache: Map): OUT { if (cache.has(input)) return cache.get(input)!!;