From 8b5b77f6a720e33644dbf1fd34457a176451755c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hanno=20J=2E=20G=C3=B6decke?= Date: Wed, 25 Sep 2024 16:18:39 +0200 Subject: [PATCH] code documentation --- src/libs/FastSearch.ts | 7 +++++++ src/libs/SuffixUkkonenTree.ts | 6 +++--- 2 files changed, 10 insertions(+), 3 deletions(-) diff --git a/src/libs/FastSearch.ts b/src/libs/FastSearch.ts index 1c32cdfd8148..91b297df3fee 100644 --- a/src/libs/FastSearch.ts +++ b/src/libs/FastSearch.ts @@ -14,6 +14,10 @@ type SearchableData = { toSearchableString: (data: T) => string; }; +/** + * Creates a new "FastSearch" instance. "FastSearch" uses a suffix tree to search for (sub-)strings in a list of strings. + * You can provide multiple datasets. The search results will be returned for each dataset. + */ function createFastSearch(dataSet: Array>) { // Create a numeric list for the suffix tree, and a look up indexes array Timing.start(CONST.TIMING.SEARCH_CONVERT_SEARCH_VALUES); @@ -39,6 +43,9 @@ function createFastSearch(dataSet: Array>) { tree.build(); Timing.end(CONST.TIMING.SEARCH_BUILD_TREE); + /** + * Searches for the given input and returns results for each dataset. + */ function search(searchInput: string): T[][] { const searchValueNumeric = stringToNumeric(cleanString(searchInput)); const result = tree.findSubstring(searchValueNumeric); diff --git a/src/libs/SuffixUkkonenTree.ts b/src/libs/SuffixUkkonenTree.ts index 7d0beeb1ea76..3b4c9dd1f842 100644 --- a/src/libs/SuffixUkkonenTree.ts +++ b/src/libs/SuffixUkkonenTree.ts @@ -1,7 +1,9 @@ /* eslint-disable no-continue */ /** - * TODO: quick explanation to how suffix ukkonen tree works: + * This implements a suffix tree using Ukkonen's algorithm. + * A good visualization to learn about the algorithm can be found here: https://brenden.github.io/ukkonen-animation/ + * Note: This implementation is optimized for performance, not necessarily for readability. */ const CHAR_CODE_A = 'a'.charCodeAt(0); @@ -224,8 +226,6 @@ function makeTree(numericSearchValues: number[]) { const rightRange = rightEdges[node] ?? defaultREdgeValue; const rangeLen = node === 0 ? 0 : rightRange - leftRange + 1; - // console.log('dfs', node, depth, leftRange, rightRange, rangeLen, searchString.length, searchString); - for (let i = 0; i < rangeLen && depth + i < searchString.length; i++) { if (searchString[depth + i] !== numericSearchValues[leftRange + i]) { return;