import { number } from "yargs" import { Position, TripleTable } from "./types" export function initializeTable(rows: number, columns: number, value: T): T[][] { return new Array(rows).fill(value).map(() => new Array(columns).fill(value)) } // like indexOf, but returns an array of all values. export function indicesOf(arr: Array, searchValue: T): Array { return arr.reduce((acc, val, index) => { if (val === searchValue) acc.push(index) return acc }, new Array()) } export function lastColumnOf(arr: T[][]): T[] { return arr.map((row: T[]) => row[row.length-1]) } export function lastRowOf(arr: T[][]): T[] { return arr[arr.length-1] } export function indicesInTable(arr: T[][], search: T): Position[] { return arr.reduce((acc: Position[], currentRow: T[], rowIndex: number) => { let colIndices = indicesOf(currentRow, search) for (const colIndex of colIndices) acc.push([rowIndex, colIndex]) return acc }, [] as Position[]) } /** * * @param lengthSeq1 Length of the first sequence * @param lengthSeq2 Length of the second sequence * @returns a table of triples where the first column is pointing up (i.e. [0, 0, 1]) and the first * row is pointing left (i.e. [1, 0, 0]). */ export function initializePointerTable(lengthSeq1: number, lengthSeq2: number): TripleTable { let P: TripleTable = initializeTable(lengthSeq1+1, lengthSeq2+1, [0, 0, 0]) for (let i=1; i number): number[] { return new Array(length).fill(0).map((v, i) => callback(i)) }