Add full-width canvas, and document PrimeMath
This commit is contained in:
parent
ddb0a37534
commit
a2b575318d
|
@ -48,7 +48,7 @@ module.exports = grunt => {
|
||||||
},
|
},
|
||||||
html: {
|
html: {
|
||||||
files: ["src/main/**/*.html"],
|
files: ["src/main/**/*.html"],
|
||||||
tasks: ["copy:html"],
|
tasks: ["copy:html", "replace:dev"],
|
||||||
},
|
},
|
||||||
ts: {
|
ts: {
|
||||||
files: ["src/main/**/*.ts"],
|
files: ["src/main/**/*.ts"],
|
||||||
|
|
Binary file not shown.
|
@ -24,9 +24,9 @@
|
||||||
"grunt-focus": "^1.0.0",
|
"grunt-focus": "^1.0.0",
|
||||||
"grunt-text-replace": "^0.4.0",
|
"grunt-text-replace": "^0.4.0",
|
||||||
"grunt-webpack": "^5.0.0",
|
"grunt-webpack": "^5.0.0",
|
||||||
"ts-loader": "^9.2.6",
|
"ts-loader": "^9.2.8",
|
||||||
"typescript": "^4.5.5",
|
"typescript": "^4.6.3",
|
||||||
"webpack": "^5.69.1",
|
"webpack": "^5.72.0",
|
||||||
"webpack-cli": "^4.9.2"
|
"webpack-cli": "^4.9.2"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,3 @@
|
||||||
|
#canvasContainer {
|
||||||
|
margin: 2em;
|
||||||
|
}
|
|
@ -46,16 +46,13 @@
|
||||||
<input type="number" id="gridScale" min="1" value="15" />
|
<input type="number" id="gridScale" min="1" value="15" />
|
||||||
</form>
|
</form>
|
||||||
</div>
|
</div>
|
||||||
|
</section>
|
||||||
|
|
||||||
<div class="row">
|
<section> <!-- No `container` class, to allow use of whole width -->
|
||||||
<div class="column">
|
<div id="canvasContainer">
|
||||||
<!-- Canvas -->
|
<canvas id="canvas" class="invisible" width="1" height="1">
|
||||||
<div id="canvasContainer">
|
Your browser must support the <canvas> element to run this game.
|
||||||
<canvas id="canvas" class="invisible" width="1" height="1">
|
</canvas>
|
||||||
Your browser must support the <canvas> element to run this game.
|
|
||||||
</canvas>
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
</div>
|
</div>
|
||||||
</section>
|
</section>
|
||||||
</div>
|
</div>
|
||||||
|
|
|
@ -1,17 +1,50 @@
|
||||||
|
/**
|
||||||
|
* Exposes maths functions for prime numbers.
|
||||||
|
*/
|
||||||
export interface PrimeMath {
|
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
|
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[]
|
decompose(n: number): number[]
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Simple implementation of `PrimeMath`.
|
||||||
|
*/
|
||||||
export class SimplePrimeMath implements PrimeMath {
|
export class SimplePrimeMath implements PrimeMath {
|
||||||
|
/**
|
||||||
|
* The singleton instance.
|
||||||
|
*
|
||||||
|
* @private
|
||||||
|
*/
|
||||||
private static instance: SimplePrimeMath;
|
private static instance: SimplePrimeMath;
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Constructs a new `SimplePrimeMath`.
|
||||||
|
*
|
||||||
|
* @private
|
||||||
|
*/
|
||||||
private constructor() {
|
private constructor() {
|
||||||
// Do nothing
|
// Do nothing
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns the singleton instance.
|
||||||
|
*
|
||||||
|
* @return the singleton instance
|
||||||
|
*/
|
||||||
public static getInstance(): SimplePrimeMath {
|
public static getInstance(): SimplePrimeMath {
|
||||||
if (!SimplePrimeMath.instance)
|
if (!SimplePrimeMath.instance)
|
||||||
SimplePrimeMath.instance = new SimplePrimeMath();
|
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 {
|
export class CachedPrimeMath implements PrimeMath {
|
||||||
|
/**
|
||||||
|
* The singleton instance.
|
||||||
|
*
|
||||||
|
* @private
|
||||||
|
*/
|
||||||
private static instance: CachedPrimeMath;
|
private static instance: CachedPrimeMath;
|
||||||
|
/**
|
||||||
|
* The underlying `SimplePrimeMath` instance of which the answers are cached.
|
||||||
|
*
|
||||||
|
* @private
|
||||||
|
*/
|
||||||
private static simple: SimplePrimeMath = SimplePrimeMath.getInstance();
|
private static simple: SimplePrimeMath = SimplePrimeMath.getInstance();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Cached outputs for `isPrime` invocations.
|
||||||
|
*
|
||||||
|
* @private
|
||||||
|
*/
|
||||||
private isPrimeCache: Map<number, boolean> = new Map();
|
private isPrimeCache: Map<number, boolean> = new Map();
|
||||||
|
/**
|
||||||
|
* Cached outputs for `decomposeCache` invocations.
|
||||||
|
*
|
||||||
|
* @private
|
||||||
|
*/
|
||||||
private decomposeCache: Map<number, number[]> = new Map();
|
private decomposeCache: Map<number, number[]> = new Map();
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Constructs a new instance.
|
||||||
|
*
|
||||||
|
* @private
|
||||||
|
*/
|
||||||
private constructor() {
|
private constructor() {
|
||||||
// Do nothing
|
// Do nothing
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns the singleton instance.
|
||||||
|
*
|
||||||
|
* @return the singleton instance
|
||||||
|
*/
|
||||||
public static getInstance(): CachedPrimeMath {
|
public static getInstance(): CachedPrimeMath {
|
||||||
if (!CachedPrimeMath.instance)
|
if (!CachedPrimeMath.instance)
|
||||||
CachedPrimeMath.instance = new CachedPrimeMath();
|
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<IN, OUT>(input: IN, fun: (_: IN) => OUT, cache: Map<IN, OUT>): OUT {
|
private static invokeCached<IN, OUT>(input: IN, fun: (_: IN) => OUT, cache: Map<IN, OUT>): OUT {
|
||||||
if (cache.has(input))
|
if (cache.has(input))
|
||||||
return cache.get(input)!!;
|
return cache.get(input)!!;
|
||||||
|
|
Loading…
Reference in New Issue