/
hamster75
/
Life
Обзор
Документация
Войти
/
hamster75
/
Life
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
6
CI/CD
Аналитика
Безопасность
master
js/grid.js
77 строк
1 KB
Dmitry Smirnov
large experiment
25 май 2026, 14:59
25 май 2026, 14:59
9ad2a72
Код
Авторство
О чём код?
export class Grid { constructor() { this._alive = new Set(); this.version = 0; } _key(x, y) { return (x + Grid.OFFSET) * Grid.RANGE + (y + Grid.OFFSET); } _decode(k) { const x = Math.trunc(k / Grid.RANGE) - Grid.OFFSET; const y = (k % Grid.RANGE) - Grid.OFFSET; return [x, y]; } isAlive(x, y) { return this._alive.has(this._key(x, y)); } set(x, y, val) { const k = this._key(x, y); if (val) this._alive.add(k); else this._alive.delete(k); this.version++; } toggle(x, y) { const k = this._key(x, y); if (this._alive.has(k)) this._alive.delete(k); else this._alive.add(k); this.version++; } clear() { this._alive.clear(); this.version++; } stamp(cells, ox, oy) { for (const [dx, dy] of cells) { const k = this._key(ox + dx, oy + dy); this._alive.add(k); } this.version++; } stampXor(cells, ox, oy) { for (const [dx, dy] of cells) { const k = this._key(ox + dx, oy + dy); if (this._alive.has(k)) this._alive.delete(k); else this._alive.add(k); } this.version++; } // Load a raw alive-set (e.g. from snapshot) without going through set(). loadAlive(aliveSet) { this._alive = new Set(aliveSet); this.version++; } *entries() { for (const k of this._alive) { yield this._decode(k); } } clone() { const g = new Grid(); g._alive = new Set(this._alive); return g; } } Grid.OFFSET = 1 << 25; Grid.RANGE = 1 << 26;