From 0f758a9e9eb4fc50a0e6dbf979721a710ea1109d Mon Sep 17 00:00:00 2001 From: teddygroves Date: Mon, 18 Mar 2024 17:49:38 +0100 Subject: [PATCH] Update week 3 materials --- docs/index.html | 104 ++++-- docs/metropolis-hastings.html | 144 +++++--- docs/search.json | 183 +++++----- docs/site_libs/bootstrap/bootstrap.min.css | 2 +- docs/site_libs/quarto-nav/quarto-nav.js | 1 + docs/site_libs/quarto-search/quarto-search.js | 65 +++- docs/week1.html | 104 ++++-- docs/week2.html | 108 ++++-- docs/week3.html | 321 ++++++++++++------ materials/_quarto.yml | 4 +- materials/week3.qmd | 91 ++++- 11 files changed, 807 insertions(+), 320 deletions(-) diff --git a/docs/index.html b/docs/index.html index e559f34..d9396e1 100644 --- a/docs/index.html +++ b/docs/index.html @@ -2,7 +2,7 @@ - + @@ -281,6 +281,7 @@ const links = window.document.querySelectorAll("a"); for (let i = 0; i < links.length; i++) { if (links[i].href) { + links[i].dataset.originalHref = links[i].href; links[i].href = links[i].href.replace(/\/index\.html/, "/"); } } @@ -1036,6 +1037,18 @@ // create the index var fuseIndex = undefined; var shownWarning = false; + +// fuse index options +const kFuseIndexOptions = { + keys: [ + { name: "title", weight: 20 }, + { name: "section", weight: 20 }, + { name: "text", weight: 10 }, + ], + ignoreLocation: true, + threshold: 0.1, +}; + async function readSearchData() { // Initialize the search index on demand if (fuseIndex === undefined) { @@ -1046,17 +1059,7 @@ shownWarning = true; return; } - // create fuse index - const options = { - keys: [ - { name: "title", weight: 20 }, - { name: "section", weight: 20 }, - { name: "text", weight: 10 }, - ], - ignoreLocation: true, - threshold: 0.1, - }; - const fuse = new window.Fuse([], options); + const fuse = new window.Fuse([], kFuseIndexOptions); // fetch the main search.json const response = await fetch(offsetURL("search.json")); @@ -1587,8 +1590,34 @@ }); } -function fuseSearch(query, fuse, fuseOptions) { - return fuse.search(query, fuseOptions).map((result) => { +let subSearchTerm = undefined; +let subSearchFuse = undefined; +const kFuseMaxWait = 125; + +async function fuseSearch(query, fuse, fuseOptions) { + let index = fuse; + // Fuse.js using the Bitap algorithm for text matching which runs in + // O(nm) time (no matter the structure of the text). In our case this + // means that long search terms mixed with large index gets very slow + // + // This injects a subIndex that will be used once the terms get long enough + // Usually making this subindex is cheap since there will typically be + // a subset of results matching the existing query + if (subSearchFuse !== undefined && query.startsWith(subSearchTerm)) { + // Use the existing subSearchFuse + index = subSearchFuse; + } else if (subSearchFuse !== undefined) { + // The term changed, discard the existing fuse + subSearchFuse = undefined; + subSearchTerm = undefined; + } + + // Search using the active fuse + const then = performance.now(); + const resultsRaw = await index.search(query, fuseOptions); + const now = performance.now(); + + const results = resultsRaw.map((result) => { const addParam = (url, name, value) => { const anchorParts = url.split("#"); const baseUrl = anchorParts[0]; @@ -1605,6 +1634,17 @@ crumbs: result.item.crumbs, }; }); + + // If we don't have a subfuse and the query is long enough, go ahead + // and create a subfuse to use for subsequent queries + if (now - then > kFuseMaxWait && subSearchFuse === undefined) { + subSearchTerm = query; + subSearchFuse = new window.Fuse([], kFuseIndexOptions); + resultsRaw.forEach((rr) => { + subSearchFuse.add(rr.item); + }); + } + return results; } @@ -4607,7 +4647,7 @@ .bi-suitcase2::before { content: "\f902"; } .bi-vignette::before { content: "\f903"; } - + @@ -4621,7 +4661,7 @@ .bi-suitcase2::before { content: "\f902"; } .bi-vignette::before { content: "\f903"; } - + @@ -4641,7 +4681,7 @@ .bi-suitcase2::before { content: "\f902"; } .bi-vignette::before { content: "\f903"; } - + @@ -4641,7 +4681,7 @@ .bi-suitcase2::before { content: "\f902"; } .bi-vignette::before { content: "\f903"; } - + @@ -4643,7 +4683,7 @@ .bi-suitcase2::before { content: "\f902"; } .bi-vignette::before { content: "\f903"; } - +