Skip to content

Commit

Permalink
rfks, gen test
Browse files Browse the repository at this point in the history
  • Loading branch information
erhant committed Jul 15, 2023
1 parent cf0329c commit aa3b788
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 69 deletions.
42 changes: 14 additions & 28 deletions src/generators.ts
Original file line number Diff line number Diff line change
@@ -1,35 +1,21 @@
/**
* Generate possible paths up to the given length (inclusive)
* @param len length of the path
* @returns list of paths
*/
export function pathGenerate(len: number): boolean[][] {
if (len !== 1) {
const a: boolean[][] = pathGenerate(len - 1);
for (let i = 0; i < a.length; ++i) {
if (a[i].length === len - 1) {
const x: boolean[] = JSON.parse(JSON.stringify(a[i]));
const y: boolean[] = JSON.parse(JSON.stringify(a[i]));
x.push(false);
y.push(true);
a.push(x);
a.push(y);
}
import {toBinary} from './util';

/** Generate possible paths of the given length. */
export function generatePaths(len: number): boolean[][] {
return Array.from({length: 1 << len}, (_, n) => {
const b = toBinary(BigInt(n));
if (b.length < len) {
return Array.from({length: len - b.length}, () => false).concat(b);
} else {
return b;
}
return a;
} else {
return [[false], [true]];
}
});
}

/**
* Generate possible prefixes that appear for paths up to the given length.
*
* In other words, prefix_generate(length) = all the prefixes that appear in PIPTree(length)
*/
export function prefixGenerate(l: number): number[][] {
/** Generate possible prefixes that appear for paths up to the given length. */
export function generatePrefixes(l: number): number[][] {
if (l !== 1) {
const a = prefixGenerate(l - 1);
const a = generatePrefixes(l - 1);
const b = [...a]; // clone deep
for (let i = 0; i < a.length; ++i) {
a[i].push(l - 1);
Expand Down
18 changes: 2 additions & 16 deletions src/piptree.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,7 @@
import {iterate} from './prefix';
import {fromPath, fromBinary, isPower2, toPath} from './util';

/**
* Finds the nature of a path.
*
* @param p path
* @param pf prefix
* @param rpf root prefix
* @returns nature
*/
/** Finds the nature of a path. */
export function findNature(p: boolean[], pf: number[], rpf: number): boolean {
const n: bigint = fromPath(p);
const iter_res: bigint = iterate(n, pf.concat(rpf + 1));
Expand All @@ -23,9 +16,6 @@ export function findNature(p: boolean[], pf: number[], rpf: number): boolean {
*
* NOTE: There could be a faster way to do this by just looking at the path. The number of 0s gives you the level. The direction can be read from the binary representation, [0, 1, 1] is like [R],
* [1, 1, 1] is [R, R], [1, 0, 1] is [L, R] etc.
*
* @param p path
* @returns list of directions
*/
export function getRootDirections(p: boolean[]): boolean[] {
const ans: boolean[] = [];
Expand All @@ -43,11 +33,7 @@ export function getRootDirections(p: boolean[]): boolean[] {
return ans;
}

/**
* Finds the prefix of a number using PIPTree properties.
* @param input an integer or a path
* @returns prefix
*/
/** Finds the prefix of a number using PIPTree properties. */
export function prefixFind(input: bigint | boolean[]): number[] {
// typechecking and assigning
let p: boolean[], n: bigint;
Expand Down
30 changes: 5 additions & 25 deletions src/util.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
/**
* Finds the first number that resides at the given path.
* @param p path
* @returns number
*/
/** Finds the first number that resides at the given path. */
export function fromPath(p: boolean[]): bigint {
p = p.map(b => !b); // flip
p.reverse(); // reverse
Expand All @@ -11,11 +7,7 @@ export function fromPath(p: boolean[]): bigint {
return n;
}

/**
* Obtain the shortest path to given number.
* @param n number
* @returns path
*/
/** Obtain the shortest path to given number. */
export function toPath(n: bigint): boolean[] {
n--; // decrement
let p = toBinary(n); // from binary
Expand All @@ -24,11 +16,7 @@ export function toPath(n: bigint): boolean[] {
return p;
}

/**
* Find the number from its binary representation.
* @param b binary representation
* @returns number
*/
/** Find the number from its binary representation. */
export function fromBinary(b: boolean[]): bigint {
let ans = 0n;
b.forEach(isSet => {
Expand All @@ -38,11 +26,7 @@ export function fromBinary(b: boolean[]): bigint {
return ans;
}

/**
* Obtain the binary representation of a number
* @param n number
* @returns binary representation
*/
/** Obtain the binary representation of a number. */
export function toBinary(n: bigint): boolean[] {
const ans: boolean[] = [];
while (n !== 0n) {
Expand All @@ -52,11 +36,7 @@ export function toBinary(n: bigint): boolean[] {
return ans;
}

/**
* Find if a given number is a power of two
* @param n number
* @returns `true` if number is a power of two
*/
/** Find if a given number is a power of two. */
export function isPower2(n: bigint): boolean {
if (n === 0n) {
return true;
Expand Down
18 changes: 18 additions & 0 deletions test/generators.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import {generatePaths} from '../src/generators';

describe('generators', () => {
it('should generate paths', () => {
const cases: {len: number; paths: boolean[][]}[] = [
{len: 1, paths: [[false], [true]]},
// prettier-ignore
{len: 2, paths: [[false, false], [false, true], [true, false], [true, true]]},
];
for (const test of cases) {
const paths = generatePaths(test.len);
expect(paths.length).toEqual(test.paths.length);
for (let i = 0; i < paths.length; i++) {
expect(paths[i]).toEqual(test.paths[i]);
}
}
});
});

0 comments on commit aa3b788

Please sign in to comment.