-
Notifications
You must be signed in to change notification settings - Fork 17
/
infinite-grid.js
81 lines (71 loc) · 1.64 KB
/
infinite-grid.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
class InfiniteGrid {
constructor({ defaultFactory = (x, y) => 0, string_map = {} } = {}) {
this.defaultFactory = defaultFactory;
this.string_map = string_map;
this.grid = new Map();
this.max_x = -Infinity;
this.min_x = Infinity;
this.max_y = -Infinity;
this.min_y = Infinity;
}
static toId(x, y) {
return `${x},${y}`;
}
set(x, y, value) {
if (typeof x !== 'number' || typeof y !== 'number') {
throw new Error(
`x and y must be numbers, got (${typeof x})${x} and (${typeof y})${y}`
);
}
if (x < this.min_x) this.min_x = x;
if (x > this.max_x) this.max_x = x;
if (y < this.min_y) this.min_y = y;
if (y > this.max_y) this.max_y = y;
const id = InfiniteGrid.toId(x, y);
this.grid.set(id, value);
}
get(x, y) {
const id = InfiniteGrid.toId(x, y);
if (!this.grid.has(id)) {
this.set(x, y, this.defaultFactory(x, y));
}
return this.grid.get(id);
}
toGrid() {
let grid = [];
for (let y = this.min_y; y <= this.max_y; y++) {
let row = [];
for (let x = this.min_x; x <= this.max_x; x++) {
let cell = this.get(x, y);
row.push(cell);
}
grid.push(row);
}
return grid;
}
sum() {
let sum = 0;
for (let value of this.grid.values()) {
sum += value;
}
return sum;
}
toString() {
let grid = this.toGrid();
let rows = '';
for (let y = 0; y < grid.length; y++) {
let row = '';
for (let x = 0; x < grid[y].length; x++) {
let cell = grid[y][x];
let cell_string =
cell in this.string_map ? this.string_map[cell] : String(cell);
row += cell_string;
}
rows += rows.length ? '\n' + row : row;
}
return rows;
}
}
module.exports = {
InfiniteGrid,
};