malwoden
Version:
   
123 lines • 4.45 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.Table = void 0;
var Table = /** @class */ (function () {
function Table(width, height) {
this.items = [];
this.width = width;
this.height = height;
// ToDo - Initialize empty array
}
Table.prototype.fill = function (value) {
var size = this.width * this.height;
for (var i = 0; i < size; i++) {
this.items[i] = value;
}
};
Table.prototype.get = function (_a) {
var x = _a.x, y = _a.y;
if (!this.isInBounds({ x: x, y: y }))
return undefined;
var index = y * this.width + x;
return this.items[index];
};
Table.prototype.set = function (pos, item) {
if (this.isInBounds(pos) === false)
throw new Error(pos.x + ":" + pos.y + " is not in bounds");
var index = pos.y * this.width + pos.x;
if (item !== undefined)
this.items[index] = item;
else
delete this.items[index];
};
Table.prototype.clear = function (pos) {
var index = pos.y * this.width + pos.x;
delete this.items[index];
};
Table.prototype.isInBounds = function (_a) {
var x = _a.x, y = _a.y;
if (x < 0 || x >= this.width || y < 0 || y >= this.height) {
return false;
}
else {
return true;
}
};
Table.prototype.getNeighbors = function (pos, predicate, topology) {
var _this = this;
if (topology === void 0) { topology = "eight"; }
var neighbors = [];
neighbors.push({ x: pos.x + 1, y: pos.y });
neighbors.push({ x: pos.x, y: pos.y + -1 });
neighbors.push({ x: pos.x + -1, y: pos.y });
neighbors.push({ x: pos.x, y: pos.y + 1 });
if (topology === "eight") {
neighbors.push({ x: pos.x + 1, y: pos.y - 1 });
neighbors.push({ x: pos.x + -1, y: pos.y + -1 });
neighbors.push({ x: pos.x + -1, y: pos.y + 1 });
neighbors.push({ x: pos.x + 1, y: pos.y + 1 });
}
neighbors = neighbors.filter(function (v) { return _this.isInBounds(v); });
if (predicate) {
neighbors = neighbors.filter(function (v) { return predicate(v, _this.get(v)); });
}
return neighbors;
};
Table.prototype.floodFillSelect = function (pos, targetValue) {
if (!targetValue) {
targetValue = this.get(pos);
}
// If given a target value, must match start position
if (this.get(pos) !== targetValue) {
return [];
}
var horizon = [pos];
// "x:y", for quick indexing
var floodFill = new Set();
while (horizon.length) {
var point = horizon.shift();
var value = this.get(point);
// If not the right type of value, we're done.
if (value !== targetValue)
continue;
// If we've already marked this spot
if (floodFill.has(point.x + ":" + point.y))
continue;
// If it is the proper value, collect it
floodFill.add(point.x + ":" + point.y);
// The add it's neighbors to the search horizon
var neighbors = this.getNeighbors(point, undefined, "four");
horizon.push.apply(horizon, neighbors);
}
var fill = Array.from(floodFill.keys()).map(function (str) {
var _a = str.split(":"), x = _a[0], y = _a[1];
return {
x: Number.parseInt(x),
y: Number.parseInt(y),
};
});
return fill;
};
Table.prototype.filter = function (match) {
var matches = [];
for (var y = 0; y < this.height; y++) {
for (var x = 0; x < this.width; x++) {
if (match({ x: x, y: y }, this.get({ x: x, y: y }))) {
matches.push({ x: x, y: y });
}
}
}
return matches;
};
Table.prototype.clone = function () {
var t = new Table(this.width, this.height);
t.items = this.items.slice();
return t;
};
Table.prototype.isSameSize = function (other) {
return this.width === other.width && this.height === other.height;
};
return Table;
}());
exports.Table = Table;
//# sourceMappingURL=table.js.map