Skip to content

Commit

Permalink
refactor: functions decs updated
Browse files Browse the repository at this point in the history
  • Loading branch information
petrlipatov committed Sep 3, 2024
1 parent 092f049 commit 61927a6
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 11 deletions.
14 changes: 7 additions & 7 deletions src/algorithms/bwt/bwtDecode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,16 @@ import { END_OF_STRING } from "./utils/constants";
/**
* Decodes a string that was encoded using the Burrows-Wheeler Transform (BWT).
*
* The function reconstructs the original string from its BWT representation by:
* 1. Create a createFrequencyMap for chars from input string.
*
* 2. Construct a mapping (`firstOccurrenceTable`) that indicates the starting position of each character in the sorted BWT string.
*
* 1. Building a frequency table of characters from the BWT string.
* 2. Creating a table that maps each character to its first occurrence in the sorted version of the BWT string.
* 3. Generating a `nextRow` table that helps in reconstructing the original string by indicating the position of each character in the sorted table.
* 4. Using the `nextRow` table and starting from the provided index to rebuild the original string in reverse order.
* 3. Compute the index of each character in the BWT string as it appears in the sorted BWT string.
*
* Instead of concatenating strings repeatedly, the function constructs the result using an array and then converts it to a string for improved performance.
* 4. Using the `charsIndexesInSortedInput` array, trace back from the given index to reconstruct the original string.
*
* @param {string} bwt - The Burrows-Wheeler Transform (BWT) encoded string to decode.
* @param {string} input - BWT encoded string to decode.
* @param {number} index - The index of the original string in the sorted permutation table.
* @returns {string} The decoded original string.
*/
Expand Down
4 changes: 2 additions & 2 deletions src/algorithms/bwt/bwtEncode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { END_OF_STRING } from "./utils/constants";
/**
* Encodes a given string using the Burrows-Wheeler Transform (BWT).
*
* The function generates all cyclic permutations of the input string, sorts them
* The function virtualizes all cyclic permutations of the input string, sorts them
* lexicographically, and constructs the BWT string from the last characters of each
* permutation. It also returns the index of the original string in the sorted list of permutations.
*
Expand All @@ -16,7 +16,7 @@ export function bwtEncode(input: string): { bwt: string; index: number } {

const permutationsIndexes = Array.from({ length }, (_, i) => i);

// we're sorting all permutations
// sorting all permutations
// using indexes
// without creating actual strings
permutationsIndexes.sort((a, b) => {
Expand Down
9 changes: 7 additions & 2 deletions src/tools/getByteLength.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
export function getByteLength(str: string): number {
/**
* Calculate byte size of a string
* @param {string} input - The string whose byte size is to be calculated.
* @returns {number} The number of bytes required to encode the string in UTF-8.
*/
export function getByteLength(input: string): number {
const encoder = new TextEncoder();
const encoded = encoder.encode(str);
const encoded = encoder.encode(input);
return encoded.length;
}

0 comments on commit 61927a6

Please sign in to comment.