Skip to content

Commit

Permalink
code documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
hannojg committed Sep 25, 2024
1 parent f7528f4 commit 8b5b77f
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 3 deletions.
7 changes: 7 additions & 0 deletions src/libs/FastSearch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ type SearchableData<T> = {
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<T>(dataSet: Array<SearchableData<T>>) {
// Create a numeric list for the suffix tree, and a look up indexes array
Timing.start(CONST.TIMING.SEARCH_CONVERT_SEARCH_VALUES);
Expand All @@ -39,6 +43,9 @@ function createFastSearch<T>(dataSet: Array<SearchableData<T>>) {
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);
Expand Down
6 changes: 3 additions & 3 deletions src/libs/SuffixUkkonenTree.ts
Original file line number Diff line number Diff line change
@@ -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);
Expand Down Expand Up @@ -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;
Expand Down

0 comments on commit 8b5b77f

Please sign in to comment.