Skip to content

Commit 4e29b89

Browse files
committed
Use fixed lookup tables for birth and survival sets
1 parent 5265def commit 4e29b89

2 files changed

Lines changed: 25 additions & 21 deletions

File tree

src/core/World.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@ export enum DeltaType {
1616
export const worldContext = createContext<World>("world");
1717

1818
export class World {
19-
private _birthSet!: Set<number>;
20-
private _survivalSet!: Set<number>;
19+
private _birthTable: boolean[];
20+
private _survivalTable: boolean[];
2121

2222
private _neighborCountsStartState = new Map<number, number>();
2323
private _cellsStartState = new Map<number, Cell>();
@@ -44,7 +44,7 @@ export class World {
4444
@observable public accessor randomizeAverageDensity = 0.5;
4545

4646
constructor() {
47-
[this._birthSet, this._survivalSet] = parseRule(this.rule);
47+
[this._birthTable, this._survivalTable] = parseRule(this.rule);
4848

4949
this.setRule = this.setRule.bind(this);
5050
this.setRandomizeFieldSize = this.setRandomizeFieldSize.bind(this);
@@ -91,7 +91,7 @@ export class World {
9191

9292
@action
9393
public setRule(rule: Rule): void {
94-
[this._birthSet, this._survivalSet] = parseRule(rule);
94+
[this._birthTable, this._survivalTable] = parseRule(rule);
9595

9696
this.rule = rule;
9797

@@ -177,14 +177,14 @@ export class World {
177177
for (const [hash, cell] of this._cells) {
178178
const neighborCount = this._neighborCounts.get(hash);
179179

180-
if (!neighborCount || !this._survivalSet.has(neighborCount)) {
180+
if (!neighborCount || !this._survivalTable[neighborCount]) {
181181
cellsToKill.add(cell);
182182
}
183183
}
184184

185185
// Mark cells to spawn
186186
for (const [hash, count] of this._neighborCounts) {
187-
if (this._birthSet.has(count) && !this._cells.has(hash)) {
187+
if (this._birthTable[count] && !this._cells.has(hash)) {
188188
const cell = Cell.fromHash(hash);
189189
cellsToSpawn.add(cell);
190190
}

src/utils/RuleUtils.ts

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,23 +2,27 @@ import { Rule, ruleGroups, stylizedRuleNames } from "../core/Rules.ts";
22

33
export type RuleKey = keyof typeof Rule;
44

5-
export function parseRule(rule: Rule): [Set<number>, Set<number>] {
5+
export function parseRule(rule: Rule): [boolean[], boolean[]] {
66
const halves = rule.split("/");
77

8-
const birthSet = new Set(
9-
halves[0]
10-
.substring(1)
11-
.split("")
12-
.map((s) => parseInt(s, 10))
13-
);
14-
const survivalSet = new Set(
15-
halves[1]
16-
.substring(1)
17-
.split("")
18-
.map((s) => parseInt(s, 10))
19-
);
20-
21-
return [birthSet, survivalSet];
8+
const birthTable = Array.from({ length: 8 }, () => false);
9+
const survivalTable = Array.from({ length: 8 }, () => false);
10+
11+
halves[0]
12+
.substring(1)
13+
.split("")
14+
.map((s) => parseInt(s, 10))
15+
// oxlint-disable-next-line unicorn/no-array-for-each
16+
.forEach((index) => (birthTable[index] = true));
17+
18+
halves[1]
19+
.substring(1)
20+
.split("")
21+
.map((s) => parseInt(s, 10))
22+
// oxlint-disable-next-line unicorn/no-array-for-each
23+
.forEach((index) => (survivalTable[index] = true));
24+
25+
return [birthTable, survivalTable];
2226
}
2327

2428
function _convertToTitlecase(key: RuleKey): string {

0 commit comments

Comments
 (0)