Rename ubiquitous 'data' to something more meaningful according to the context

This commit is contained in:
Tissevert 2022-08-06 20:15:53 +02:00
parent 5c0ef70220
commit ffebb1efb8
7 changed files with 32 additions and 32 deletions

View file

@ -21,14 +21,14 @@ CellSet.prototype.fromRectangle = function(definition) {
}; };
CellSet.prototype.fromIsochrome = function(definition) { CellSet.prototype.fromIsochrome = function(definition) {
var originColor = definition.data[definition.row][definition.column]; var originColor = definition.grid[definition.row][definition.column];
var queue = [{i: definition.row, j: definition.column}]; var queue = [{i: definition.row, j: definition.column}];
while(queue.length > 0) { while(queue.length > 0) {
var p = queue[0]; var p = queue[0];
this.add(p.i, p.j); this.add(p.i, p.j);
for(var d = -1; d < 2; d += 2) { for(var d = -1; d < 2; d += 2) {
[{i: p.i + d, j: p.j}, {i: p.i, j: p.j + d}].forEach( [{i: p.i + d, j: p.j}, {i: p.i, j: p.j + d}].forEach(
gateKeeper(this, definition.data, queue, originColor) gateKeeper(this, definition.grid, queue, originColor)
); );
} }
queue.shift(); queue.shift();
@ -90,11 +90,11 @@ function id(i, j) {
return i + ':' + j; return i + ':' + j;
} }
function gateKeeper(cellSet, data, queue, originColor) { function gateKeeper(cellSet, grid, queue, originColor) {
return function(p1) { return function(p1) {
if(p1.i >= 0 && p1.i < data.length && p1.j >= 0 && p1.j < data.length if(p1.i >= 0 && p1.i < grid.length && p1.j >= 0 && p1.j < grid.length
&& !cellSet.contains(p1.i, p1.j) && !cellSet.contains(p1.i, p1.j)
&& data[p1.i][p1.j] == originColor) { && grid[p1.i][p1.j] == originColor) {
queue.push(p1); queue.push(p1);
} }
} }

View file

@ -4,7 +4,7 @@ import {iter, square} from Grid.Util;
var grid = { var grid = {
element: document.getElementById('grid'), element: document.getElementById('grid'),
data: null, colors: null,
missing: null, missing: null,
size: null size: null
}; };
@ -35,11 +35,11 @@ function makeRow(config) {
} }
function clear() { function clear() {
grid.data = square(grid.size); grid.colors = square(grid.size);
grid.missing = CellSet.make( grid.missing = CellSet.make(
{type: 'rectangle', row: 0, column: 0, width: 8, height: 8} {type: 'rectangle', row: 0, column: 0, width: 8, height: 8}
); );
iter(grid.data, function(row, column) { iter(grid.colors, function(row, column) {
cell(row, column).className = ''; cell(row, column).className = '';
}); });
} }

View file

@ -9,14 +9,14 @@ return {
function colorize(row, column, color) { function colorize(row, column, color) {
var grid = Grid.get(); var grid = Grid.get();
grid.data[row][column] = color || Toolbox.color(); grid.colors[row][column] = color || Toolbox.color();
Grid.cell(row, column).className = 'color' + grid.data[row][column]; Grid.cell(row, column).className = 'color' + grid.colors[row][column];
grid.missing.remove(row, column); grid.missing.remove(row, column);
} }
function paint(row, column) { function paint(row, column) {
var cellSet = CellSet.make( var cellSet = CellSet.make(
{type: 'isochrome', row: row, column: column, data: Grid.get().data} {type: 'isochrome', row: row, column: column, grid: Grid.get().colors}
); );
cellSet.iter(colorize); cellSet.iter(colorize);
} }

View file

@ -46,5 +46,5 @@ function setGridData(data) {
} }
function save() { function save() {
File.save('data:text/json,' + JSON.stringify(Grid.get().data), "grid.json"); File.save('data:text/json,' + JSON.stringify(Grid.get().colors), "grid.json");
} }

View file

@ -8,5 +8,5 @@ return {
}; };
function onEnter() { function onEnter() {
console.log(Solver.step(Grid.get().data)); console.log(Solver.step(Grid.get().colors));
} }

View file

@ -1,5 +1,5 @@
function Encoder() { function Encoder() {
this.data = ''; this.buffer = '';
this.stack = 0; this.stack = 0;
this.size = 0; this.size = 0;
} }
@ -13,7 +13,7 @@ Encoder.prototype.push = function(one) {
}; };
Encoder.prototype.flush = function() { Encoder.prototype.flush = function() {
this.data = this.data + String.fromCharCode(this.stack); this.buffer = this.buffer + String.fromCharCode(this.stack);
this.stack = 0; this.stack = 0;
this.size = 0; this.size = 0;
}; };
@ -23,7 +23,7 @@ Encoder.prototype.output = function() {
this.push(0); this.push(0);
} }
this.flush(); this.flush();
return btoa(this.data); return btoa(this.buffer);
}; };
Encoder.prototype.variableLength3 = function(n) { Encoder.prototype.variableLength3 = function(n) {

View file

@ -4,29 +4,29 @@ return {
step: step step: step
}; };
function step(matrix) { function step(grid) {
var zones = getZones(matrix); var zones = getZones(grid);
var grid = getGrid(matrix.length); var lines = getLines(grid.length);
var rowClusters = checkRowsInclusions(matrix); var rowClusters = checkRowsInclusions(grid);
if(rowClusters.length > 0) { if(rowClusters.length > 0) {
rowClusters.forEach(function(rowCluster) { rowClusters.forEach(function(rowCluster) {
rowCluster.toClear = difference( rowCluster.toClear = difference(
rowCluster.colors.map(function(color) {return zones[color];}), rowCluster.colors.map(function(color) {return zones[color];}),
rowCluster.rows.map(function(row) {return grid.rows[row];}) rowCluster.rows.map(function(row) {return lines.rows[row];})
); );
}); });
} }
return rowClusters; return rowClusters;
} }
function getZones(matrix) { function getZones(grid) {
var zones = {}; var zones = {};
for(var row = 0; row < matrix.length; row++) { for(var row = 0; row < grid.length; row++) {
for(var column = 0; column < matrix[row].length; column++) { for(var column = 0; column < grid[row].length; column++) {
var color = matrix[row][column]; var color = grid[row][column];
if(zones[color] == undefined) { if(zones[color] == undefined) {
zones[color] = CellSet.make( zones[color] = CellSet.make(
{type: 'isochrome', row: row, column: column, data: matrix} {type: 'isochrome', row: row, column: column, grid: grid}
); );
} }
} }
@ -46,7 +46,7 @@ function line(type, size, i) {
} }
} }
function getGrid(size) { function getLines(size) {
var empty = Array.from({length: size}); var empty = Array.from({length: size});
return { return {
rows: empty.map(function(x, i) {return line('row', size, i);}), rows: empty.map(function(x, i) {return line('row', size, i);}),
@ -54,11 +54,11 @@ function getGrid(size) {
}; };
} }
function getColorsByRow(matrix) { function getColorsByRow(grid) {
var colorsByRow = []; var colorsByRow = [];
for(var row = 0; row < matrix.length; row++) { for(var row = 0; row < grid.length; row++) {
colorsByRow.push( colorsByRow.push(
quotient(matrix[row], function(c0, c1) {return c0 == c1;}).map( quotient(grid[row], function(c0, c1) {return c0 == c1;}).map(
function(colorClass) {return colorClass.specimen;} function(colorClass) {return colorClass.specimen;}
) )
); );
@ -66,8 +66,8 @@ function getColorsByRow(matrix) {
return colorsByRow; return colorsByRow;
} }
function checkRowsInclusions(matrix) { function checkRowsInclusions(grid) {
var colorsByRow = getColorsByRow(matrix); var colorsByRow = getColorsByRow(grid);
var colorSets = quotient(colorsByRow, sameColorsSet); var colorSets = quotient(colorsByRow, sameColorsSet);
return colorSets.reduce(function(commands, colorSet) { return colorSets.reduce(function(commands, colorSet) {
if(colorSet.occurrences.length == colorSet.specimen.length) { if(colorSet.occurrences.length == colorSet.specimen.length) {