Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Basic cube representation types #3

Merged
merged 3 commits into from
Mar 13, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions src/lib/core/cube.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { CubeIndex } from "./cubeindex";
import { CubeOp } from "./cubeop";

export type FaceletNumber = number;

export interface Cube {
readonly dimension: number;
readonly positionOf: (FaceletNumber: number) => CubeIndex;
readonly occupantOf: (position: CubeIndex) => number;
readonly apply: (op: CubeOp) => Cube;
}
36 changes: 36 additions & 0 deletions src/lib/core/cubeindex.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
export type SlotNumber = number;

export interface CubeIndex {
readonly face: number;
readonly row: number;
readonly column: number;
}

export class IndexConverter {
readonly dimension: number;

static forDimension(dimension: number): IndexConverter {
return new IndexConverter(dimension);
}

fromSlotNumber(slotNumber: SlotNumber): CubeIndex {
const dimSquared = this.dimension ** 2;
return {
face: Math.trunc(slotNumber / dimSquared),
row: Math.trunc((slotNumber % dimSquared) / this.dimension),
column: slotNumber % this.dimension,
};
}

toSlotNumber(position: CubeIndex): SlotNumber {
return (
this.dimension ** 2 * position.face +
this.dimension * position.row +
position.column
);
}

private constructor(dimension: number) {
this.dimension = dimension;
}
}
6 changes: 6 additions & 0 deletions src/lib/core/cubeop.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { CubeIndex } from "./cubeindex";

export interface CubeOp {
readonly imageOf: (cubeIndex: CubeIndex, dimension: number) => CubeIndex;
readonly inverse: () => CubeOp;
}