Skip to content

Commit

Permalink
chore: descs added to exported functions
Browse files Browse the repository at this point in the history
  • Loading branch information
petrlipatov committed Sep 1, 2024
1 parent 0b9c189 commit 5daceba
Show file tree
Hide file tree
Showing 17 changed files with 185 additions and 20 deletions.
10 changes: 10 additions & 0 deletions src/algorithms/bwt/bwtDecode.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,15 @@
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 by progressively building up the rows of the sorted
* permutation table from the BWT string, and then retrieving the row at the specified index.
*
* @param {string} bwt - The Burrows-Wheeler Transform (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.
*/
export function bwtDecode(bwt: string, index: number): string {
const length = bwt.length;
const table = Array.from({ length }, () => "");
Expand Down
11 changes: 10 additions & 1 deletion src/algorithms/huffman/binaryDecoder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,16 @@ import {
getDecodedData,
} from "./utils/helpers";

export const huffmanBinaryDecode = (buffer: ArrayBuffer) => {
/**
* Decodes data encoded with the Huffman coding algorithm from a binary buffer.
*
* This function reads a binary buffer that contains Huffman-encoded data, decodes the Huffman tree,
* and then decodes the original data using the Huffman tree and the encoded binary data.
*
* @param {ArrayBuffer} buffer - The binary buffer containing the Huffman-encoded data.
* @returns {string} The decoded data from the Huffman-encoded binary buffer.
*/
export const huffmanBinaryDecode = (buffer: ArrayBuffer): string => {
const binaryReader = new BinaryReader(buffer);
const huffmanTree = decodeHuffmanTree(binaryReader);
const byteCountEncodedData = binaryReader.getUint32();
Expand Down
13 changes: 12 additions & 1 deletion src/algorithms/huffman/binaryEncoder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,18 @@ import {
} from "./utils/helpers";
import { createHuffmanTree } from "./utils/huffman-tree";

export const huffmanBinaryEncode = (input: string) => {
/**
* Encodes a string using Huffman coding and returns the binary encoded data as an ArrayBuffer.
*
* This function takes an input string, generates a frequency map, creates a Huffman tree,
* and produces a binary encoding of the input string based on the Huffman encoding scheme.
* The result is returned as an ArrayBuffer, which contains the encoded data along with
* the necessary metadata to decode it later.
*
* @param {string} input - The input string to encode using Huffman coding.
* @returns {ArrayBuffer} The binary encoded data in the form of an ArrayBuffer.
*/
export const huffmanBinaryEncode = (input: string): ArrayBuffer => {
const frequencyTable = createFrequencyMap(input);
const huffmanTree = createHuffmanTree(frequencyTable);
const encodingTable = createEncodingTable(frequencyTable, huffmanTree);
Expand Down
10 changes: 10 additions & 0 deletions src/algorithms/huffman/utils/huffman-tree.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,16 @@
import { PriorityQueue } from "./priority-queue";
import { FrequencyTable, HuffmanTree, IntermediaryNode, Node } from "./types";

/**
* Creates a Huffman Tree from a frequency table.
*
* This function constructs a Huffman Tree by taking a frequency table as input,
* where each entry represents a character and its frequency. It uses a priority queue
* to build the tree, combining the two least frequent nodes until a single tree remains.
*
* @param {FrequencyTable} frequencyTable - A table that maps characters to their frequencies.
* @returns {HuffmanTree} The constructed Huffman Tree.
*/
export const createHuffmanTree = (
frequencyTable: FrequencyTable
): HuffmanTree => {
Expand Down
12 changes: 11 additions & 1 deletion src/algorithms/lz77/binaryDecoder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,17 @@ import { BinaryReader } from "../../common/binary-reader";
import { NULL_UNICODE } from "./utils/constants";
import { addMatchedPattern } from "./utils/helpers";

export function lz77BinaryDecode(buffer: ArrayBuffer) {
/**
* Decodes a string encoded with the LZ77 compression algorithm from a binary buffer.
*
* This function reads a binary buffer containing LZ77-encoded data, which consists of offset-length pairs
* followed by literal characters. It reconstructs the original string by expanding the matched patterns
* and appending the literal characters.
*
* @param {ArrayBuffer} buffer - The binary buffer containing the LZ77-encoded data.
* @returns {string} The decoded original string.
*/
export function lz77BinaryDecode(buffer: ArrayBuffer): string {
const binaryReader = new BinaryReader(buffer);

let output = "";
Expand Down
18 changes: 17 additions & 1 deletion src/algorithms/lz77/binaryEncoder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,23 @@ import { lz77Encode } from "./encoder";
import { createArrayBuffer } from "./utils/helpers";
import { Options } from "./utils/types";

export function lz77BinaryEncode(input: string, options?: Partial<Options>) {
/**
* Encodes a string using the LZ77 compression algorithm and returns the encoded data as an ArrayBuffer.
*
* This function compresses the input string with the LZ77 algorithm, generating encoded data
* that includes offset-length pairs and literal characters. The result is returned as an ArrayBuffer,
* which can be used for storage or transmission.
*
* @param {string} input - The input string to encode using the LZ77 compression algorithm.
* @param {Partial<Options>} [options] - Optional settings to customize the compression behavior.
* - `searchBufferLength` (number): The maximum length of the search buffer.
* - `lookaheadLength` (number): The length of the lookahead buffer.
* @returns {ArrayBuffer} The binary encoded data in the form of an ArrayBuffer.
*/
export function lz77BinaryEncode(
input: string,
options?: Partial<Options>
): ArrayBuffer {
const encodedData = lz77Encode(input, options);
const buffer = createArrayBuffer(encodedData);
return buffer;
Expand Down
12 changes: 10 additions & 2 deletions src/algorithms/lz77/encoder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,17 @@ import { NULL_UNICODE } from "./utils/constants";
*
* `next-unmatched-symbol` -> The next symbol in the stream that does not participate in
* the current pattern match, which will be encoded as a literal.
*
* @param {string} input - The input string to encode using the LZ77 compression algorithm.
* @param {Partial<Options>} [options] - Optional settings to customize the compression behavior.
* - `searchBufferLength` (number): The maximum length of the search buffer.
* - `lookaheadLength` (number): The length of the lookahead buffer.
* @returns {EncodedArray} array of tuples [offset, length, next-unmatched-symbol][]
*/

export function lz77Encode(input: string, options?: Partial<Options>) {
export function lz77Encode(
input: string,
options?: Partial<Options>
): EncodedArray {
const output: EncodedArray = [];

let pointer = 0;
Expand Down
13 changes: 12 additions & 1 deletion src/algorithms/lzss/binaryDecoder.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,17 @@
import { BinaryReader } from "../../common/binary-reader";

export function lzssBinaryDecode(buffer: ArrayBuffer) {
/**
* Decodes a string encoded with the LZSS compression algorithm from a binary buffer.
*
* This function reads a binary buffer containing LZSS-encoded data, which includes a schema byte
* to indicate whether the following data is a literal character or a reference to a previously encoded
* substring (offset-length pair). The function reconstructs the original string by expanding the
* matched patterns and appending literal characters.
*
* @param {ArrayBuffer} buffer - The binary buffer containing the LZSS-encoded data.
* @returns {string} The decoded original string.
*/
export function lzssBinaryDecode(buffer: ArrayBuffer): string {
const binaryReader = new BinaryReader(buffer);
let output = "";

Expand Down
18 changes: 17 additions & 1 deletion src/algorithms/lzss/binaryEncoder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,23 @@ import { lzssEncode } from "./encoder";
import { createArrayBuffer } from "./utils/helpers";
import { Options } from "./utils/types";

export function lzssBinaryEncode(input: string, options?: Partial<Options>) {
/**
* Encodes a string using the LZSS compression algorithm and returns the encoded data as an ArrayBuffer.
*
* This function compresses the input string with the LZSS algorithm, generating encoded data
* that includes literals and offset-length pairs, along with a schema to indicate the format of the data.
* The result is returned as an ArrayBuffer, which can be used for storage or transmission.
*
* @param {string} input - The input string to encode using the LZSS compression algorithm.
* @param {Partial<Options>} [options] - Optional settings to customize the compression behavior.
* - `searchBufferLength` (number): The maximum length of the search buffer (default is typically defined elsewhere).
* - `lookaheadLength` (number): The length of the lookahead buffer (default is typically defined elsewhere).
* @returns {ArrayBuffer} The binary encoded data in the form of an ArrayBuffer.
*/
export function lzssBinaryEncode(
input: string,
options?: Partial<Options>
): ArrayBuffer {
const { data, length, schema } = lzssEncode(input, options);
const buffer = createArrayBuffer(data, length, schema);
return buffer;
Expand Down
28 changes: 22 additions & 6 deletions src/algorithms/lzss/encoder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,29 @@ import { MIN_MATCH_LENGTH } from "./utils/constants";
import { EncodedArray, Options } from "./utils/types";

/**
* LZSS encodes repeated patterns with tuples [offset, length]:
* `offset` -> The distance (in characters) that points back to the earlier
* occurrence of the repeated pattern.
* `length` -> The number of characters that match representing the length
* of the repeated sequence.
* Encodes a string using the LZSS compression algorithm.
*
* This function compresses the input string with the LZSS algorithm, generating encoded data
* that includes literals and offset-length pairs, along with a schema byte array that indicates
* the format of the encoded data. The schema helps in interpreting the encoded data during decompression.
*
* @param {string} input - The input string to encode using the LZSS compression algorithm.
* @param {Partial<Options>} [options] - Optional settings to customize the compression behavior.
* - `searchBufferLength` (number): The maximum length of the search buffer (default is typically defined elsewhere).
* - `lookaheadLength` (number): The length of the lookahead buffer (default is typically defined elsewhere).
* @returns {Object} An object containing the following properties:
* - `data` (EncodedArray): The encoded data, which includes literals and offset-length pairs.
* - `length` (number): The length of the encoded data.
* - `schema` (number[]): An array of bytes representing the schema for interpreting the encoded data.
*/
export function lzssEncode(input: string, options?: Partial<Options>) {
export function lzssEncode(
input: string,
options?: Partial<Options>
): {
data: EncodedArray;
length: number;
schema: number[];
} {
const encodedData: EncodedArray = [];
// `schema` is a byte array where each bit indicates type of encoded data.
// 0 -> represents a literal symbol;
Expand Down
3 changes: 0 additions & 3 deletions src/algorithms/lzss/index.ts

This file was deleted.

9 changes: 9 additions & 0 deletions src/algorithms/rle/rleDecoder.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
import { extractSymbolAndCount } from "./utils/helpers";

/**
* Decodes a string encoded with the Run-Length Encoding (RLE) algorithm.
*
* This function reconstructs the original string from its RLE-encoded form by expanding sequences
* of repeated characters based on the counts specified in the encoded string.
*
* @param {string} input - The RLE-encoded string to decode.
* @returns {string} The decoded original string.
*/
export function rleDecode(input: string): string {
if (input.length === 0) return "";

Expand Down
10 changes: 10 additions & 0 deletions src/algorithms/rle/rleEncoder.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,15 @@
import { SPECIAL_CHAR } from "./utils/constants";

/**
* Encodes a string using the Run-Length Encoding (RLE) algorithm.
*
* This function compresses the input string by replacing consecutive repeated characters with
* a single character followed by the count of repetitions. If there are numeric characters in the
* input, a special character is inserted to differentiate literal numbers from counts in the RLE output.
*
* @param {string} input - The input string to encode using the RLE algorithm.
* @returns {string} The RLE-encoded string.
*/
export function rleEncode(input: string): string {
if (input.length === 0) {
throw new Error("Input string cannot be empty.");
Expand Down
11 changes: 10 additions & 1 deletion src/algorithms/rutf8/binaryDecoder.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,15 @@
import { rutf8Decode } from "./rutf8Decoder";

export function rutf8BinaryDecode(input: ArrayBuffer) {
/**
* Decodes a UTF-8 encoded binary buffer into a string using the RUTF8 encoding scheme.
*
* This function first decodes the binary buffer into a UTF-8 string and then applies the RUTF8
* decoding to obtain the final decoded string.
*
* @param {ArrayBuffer} input - The binary buffer containing UTF-8 encoded data.
* @returns {string} The decoded string after applying RUTF8 decoding.
*/
export function rutf8BinaryDecode(input: ArrayBuffer): string {
const binaryEncoder = new TextDecoder();
const decodedFromBuffer = binaryEncoder.decode(input);
const decodedFromRutf = rutf8Decode(decodedFromBuffer);
Expand Down
11 changes: 10 additions & 1 deletion src/algorithms/rutf8/binaryEncoder.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,15 @@
import { rutf8Encode } from "./rutf8Encoder";

export function rutf8BinaryEncode(input: string) {
/**
* Encodes a string into a binary buffer using the RUTF8 encoding scheme followed by UTF-8 encoding.
*
* This function first encodes the input string using the RUTF8 encoding scheme and then converts
* the RUTF8-encoded string into a binary buffer using UTF-8 encoding.
*
* @param {string} input - The input string to encode using the RUTF8 encoding scheme.
* @returns {Uint8Array} The binary buffer containing the UTF-8 encoded RUTF8 data.
*/
export function rutf8BinaryEncode(input: string): Uint8Array {
const binaryEncoder = new TextEncoder();
const rutfEncoded = rutf8Encode(input);
const buffer = binaryEncoder.encode(rutfEncoded);
Expand Down
13 changes: 12 additions & 1 deletion src/algorithms/rutf8/rutf8Decoder.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,17 @@
import { asciiToRussianMap, russianToAsciiMap } from "./utils/constants";

export function rutf8Decode(text): string {
/**
* Decodes a RUTF8-encoded string into its original form by converting characters
* from the RUTF8 encoding scheme back to their corresponding characters in the original text.
*
* This function maps characters from RUTF8 encoding to their original representations
* using predefined mappings (`asciiToRussianMap` and `russianToAsciiMap`). Characters
* that do not have corresponding mappings are left unchanged.
*
* @param {string} text - The RUTF8-encoded string to decode.
* @returns {string} The decoded string, which is the original form before RUTF8 encoding.
*/
export function rutf8Decode(text: string): string {
const output: string[] = new Array(text.length);
let index = 0;
for (const char of text) {
Expand Down
3 changes: 3 additions & 0 deletions src/algorithms/rutf8/rutf8Encoder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ import { russianToAsciiMap, asciiToRussianMap } from "./utils/constants";
*
* This implementation is bidirectional, meaning it can encode text into a more compact ASCII representation
* and decode it back to the original Russian Unicode text.
*
* @param {string} text - The input string to encode using the RUTF8 encoding scheme.
* @returns {string} The RUTF8-encoded string.
*/
export function rutf8Encode(text): string {
const output: string[] = new Array(text.length);
Expand Down

0 comments on commit 5daceba

Please sign in to comment.