From fe3024ca0f41d5921ff6467089eee276b153edf7 Mon Sep 17 00:00:00 2001
From: Alexander Putilin
Date: Thu, 6 May 2021 02:41:17 +0100
Subject: [PATCH 1/2] New demos
---
package.json | 2 +-
src/autogenerated/new_demos.html | 18 ++++++++++++++++
src/chapter1_simplified_hash.js | 30 +++++++++++++-------------
src/index.js | 2 ++
src/mustache/new_demos.json | 3 +++
src/new_demos.js | 36 ++++++++++++++++++++++++++++++++
webpack.dev.js | 4 ++++
webpack.prod.js | 4 ++++
8 files changed, 82 insertions(+), 17 deletions(-)
create mode 100644 src/autogenerated/new_demos.html
create mode 100644 src/mustache/new_demos.json
create mode 100644 src/new_demos.js
diff --git a/package.json b/package.json
index c13cfa4..6fb3679 100644
--- a/package.json
+++ b/package.json
@@ -9,7 +9,7 @@
"test:pyunit": "npm run extractcode && ./unittest_python.sh",
"test": "npm run jest && npm run test:pyunit && npm run test:pystress",
"build:ssr": "./ssr-all.sh",
- "update:html": "mkdir -p build && (for i in {chapter1,chapter2,chapter3,chapter4}; do mustache src/mustache/$i.json src/page.html.template > src/autogenerated/$i.html; done)",
+ "update:html": "mkdir -p build && (for i in {chapter1,chapter2,chapter3,chapter4,new_demos}; do mustache src/mustache/$i.json src/page.html.template > src/autogenerated/$i.html; done)",
"start": "webpack-dev-server --config webpack.dev.js --host 0.0.0.0",
"serve": "http-server -p 9090 dist/",
"build": "npm run build:ssr && webpack --config webpack.prod.js",
diff --git a/src/autogenerated/new_demos.html b/src/autogenerated/new_demos.html
new file mode 100644
index 0000000..1241b14
--- /dev/null
+++ b/src/autogenerated/new_demos.html
@@ -0,0 +1,18 @@
+
+
+
+ Inside python dict — an explorable explanation
+
+
+
+
+
+
+
+
+
+
+
+
diff --git a/src/chapter1_simplified_hash.js b/src/chapter1_simplified_hash.js
index f282b60..064d0cb 100644
--- a/src/chapter1_simplified_hash.js
+++ b/src/chapter1_simplified_hash.js
@@ -172,7 +172,7 @@ class SimplifiedInsertAll extends BreakpointFunction {
this._overwritten = [];
}
- run(_originalList, isBroken = false) {
+ run(_originalList, isBroken = false, overrideSize = null) {
this.fmtIsBroken = isBroken;
this.originalList = new ImmutableList(_originalList);
this.newList = new ImmutableList();
@@ -180,7 +180,13 @@ class SimplifiedInsertAll extends BreakpointFunction {
this.fmtMissingNumbers = new ImmutableList();
this.newListWithReplacements = new ImmutableList();
}
- const startSize = (isBroken ? 1 : 2) * this.originalList.size;
+ let startSize;
+
+ if (!overrideSize) {
+ startSize = (isBroken ? 1 : 2) * this.originalList.size;
+ } else {
+ overrideSize = startSize;
+ }
for (let i = 0; i < startSize; ++i) {
this.newList = this.newList.push(null);
if (isBroken) {
@@ -245,21 +251,15 @@ let formatSimplifiedInsertAllDescription = function(bp, prevBp) {
bp.number
}`;
case 'compute-idx':
- return `Compute the slot index: ${bp.newListIdx} == ${bp.number} % ${
- bp.newList.size
- }`;
+ return `Compute the slot index: ${bp.newListIdx} == ${bp.number} % ${bp.newList.size}`;
case 'check-collision':
return chapter1_2_FormatCheckCollision(bp.newList, bp.newListIdx, bp.fmtCollisionCount);
case 'next-idx':
- return `Keep probing, the next slot will be ${bp.newListIdx} == (${
- prevBp.newListIdx
- } + 1) % ${bp.newList.size}`;
+ return `Keep probing, the next slot will be ${bp.newListIdx} == (${prevBp.newListIdx} + 1) % ${bp.newList.size}`;
case 'assign-elem': {
const prevNumber = prevBp.newList.get(bp.newListIdx);
if (prevNumber != null) {
- return `Collision of ${bp.number} with ${prevNumber} in slot ${
- bp.newListIdx
- } - the number is overwritten`;
+ return `Collision of ${bp.number} with ${prevNumber} in slot ${bp.newListIdx} - the number is overwritten`;
} else {
return `Put ${bp.number} in slot ${bp.newListIdx}`;
}
@@ -351,9 +351,7 @@ class SimplifiedSearch extends BreakpointFunction {
let formatSimplifiedSearchDescription = function(bp) {
switch (bp.point) {
case 'compute-idx':
- return `Compute the slot index: ${bp.newListIdx} == ${bp.number} % ${
- bp.newList.size
- }`;
+ return `Compute the slot index: ${bp.newListIdx} == ${bp.number} % ${bp.newList.size}`;
case 'check-not-found':
return commonFormatCheckNotFound(bp.newList, bp.newListIdx, bp.fmtCollisionCount);
case 'check-found':
@@ -618,8 +616,8 @@ export class Chapter1_SimplifiedHash extends ChapterComponent {
Accessing a single element by index is very fast. Accessing only a few elements would be fast
- too. We don't want to be doing a linear scan over the whole list every time we look up a number, so
- we need to organize our data in a clever way.
+ too. We don't want to be doing a linear scan over the whole list every time we look up a number,
+ so we need to organize our data in a clever way.