Skip to content

Commit

Permalink
Move to uint8 array for skips
Browse files Browse the repository at this point in the history
  • Loading branch information
Jumbub committed Mar 25, 2022
1 parent b507963 commit 01c5c85
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 14 deletions.
6 changes: 3 additions & 3 deletions src/common/load.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Board, CELL_COLOR } from '../logic/board.js';
import { Board, CELL_COLOR, DONT_SKIP } from '../logic/board.js';

export const load = (board: Board, data: string) => {
const { input, output, inSkip, outSkip } = board;
Expand All @@ -9,8 +9,8 @@ export const load = (board: Board, data: string) => {
input.data[i] = output.data[i] = CELL_COLOR[state][i % 4];
});

inSkip.fill(false);
outSkip.fill(false);
inSkip.fill(DONT_SKIP);
outSkip.fill(DONT_SKIP);
};

export const match = ({ output }: Board, data: string) => {
Expand Down
18 changes: 10 additions & 8 deletions src/logic/board.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,17 @@ export type Board = {
input: ImageData;
output: ImageData;

inSkip: boolean[];
outSkip: boolean[];
inSkip: Skips;
outSkip: Skips;
};

export const ALIVE = true;
export const DEAD = false;

export const SKIP = true;
export const DONT_SKIP = false;
export type Skip = 1 | 0;
export type Skips = Uint8Array;
export const SKIP = 1;
export const DONT_SKIP = 0;

export const ALIVE_COLOR = [255, 255, 255, 255] as const;
export const DEAD_COLOR = [0, 0, 0, 255] as const;
Expand All @@ -24,7 +26,7 @@ export const newBoard = (viewWidth: number, viewHeight: number) => {
const height = viewHeight + 2;

const newImageData = () => new ImageData(width, height);
const newSkips = () => Array(getSkipI(width * height)).fill(false);
const newSkips = () => new Uint8Array(getSkipI(width * height)).fill(0);

const board: Board = {
input: newImageData(),
Expand All @@ -47,16 +49,16 @@ export const setCellFrom = (data: ImageData['data'], i: number, from: number) =>
return setCell(data, i, getCell(data, from));
};

export const getCell = (data: ImageData['data'], i: number): boolean => {
export const getCell = (data: ImageData['data'], i: number) => {
return data[i * 4] ? ALIVE : DEAD;
};

export const getSkipI = (i: number) => Math.floor(i / SKIP_MULTIPLYER);

export const setSkip = (skips: boolean[], i: number, value: boolean) => {
export const setSkip = (skips: Skips, i: number, value: Skip) => {
skips[getSkipI(i)] = value;
};

export const getSkip = (skips: boolean[], i: number) => {
export const getSkip = (skips: Skips, i: number) => {
return skips[getSkipI(i)];
};
6 changes: 3 additions & 3 deletions src/logic/next.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Board, DONT_SKIP, getCell, getSkip, setCell, setSkip, SKIP_MULTIPLYER } from './board.js';
import { Board, DONT_SKIP, getCell, getSkip, setCell, setSkip, SKIP, Skips, SKIP_MULTIPLYER } from './board.js';
import { LOOKUP } from './lookup.js';
import { pad } from './padding.js';

Expand All @@ -16,7 +16,7 @@ const isAlive = ({ data, width }: ImageData, i: number) => {
return LOOKUP[sum];
};

const revokeSkipForNeighbours = (i: number, outSkip: boolean[], width: number) => {
const revokeSkipForNeighbours = (i: number, outSkip: Skips, width: number) => {
for (let b = i - width - 1; b <= i + width - 1; b += width) {
for (let o = 0; o < 3; o++) {
setSkip(outSkip, b + o, DONT_SKIP);
Expand All @@ -32,7 +32,7 @@ export const next = (board: Board) => {

const endI = width * height;

outSkip.fill(true);
outSkip.fill(SKIP);

let i = 0;
while (i < endI) {
Expand Down

0 comments on commit 01c5c85

Please sign in to comment.