From db36e4680af51f83652a2c1c9de766b84cdbd03f Mon Sep 17 00:00:00 2001 From: Holden Rohrer Date: Fri, 17 Jul 2020 18:08:42 -0400 Subject: tools/search uses js class --- tools/search.js | 27 ++++++++++++++------------- 1 file changed, 14 insertions(+), 13 deletions(-) diff --git a/tools/search.js b/tools/search.js index eafb2bf..4901293 100644 --- a/tools/search.js +++ b/tools/search.js @@ -12,13 +12,14 @@ function getComp(index){ return (element,needle) => (element[index] - needle[index] || element[1-index] - needle[1-index]) } -function Search(){ // searchBlock should be a Space object. - this.tiles = {}; // Object which stores Spaces. - this.sort = [[],[]]; // Vertically/horizontally sorted list of indexed tiles for fast addition, deletion, and searching - this.spaces = []; // if spaces[i] is detected, - this.calls = []; // calls[i](coords, send, searchspace) - - this.add = function(loc, space, send){ // loc should be [tileY,tileX] and space Space. +class Search { + constructor(){ + this.tiles = {}; // Object which stores Spaces. + this.sort = [[],[]]; // Vertically/horizontally sorted list of indexed tiles for fast addition, deletion, and searching + this.spaces = []; // if spaces[i] is detected, + this.calls = []; // calls[i](coords, send, searchspace) + } + add(loc, space, send){ // loc should be [tileY,tileX] and space Space. this.tiles[loc] = space; let inds = Array(2); // Records horizontal and vertical indices for insertion. Then actually inserts the item. [0,1].forEach( ind => { // ind chooses y-or-x @@ -38,7 +39,7 @@ function Search(){ // searchBlock should be a Space object. } } - this.block = function(loc,inds,exclude){ + block(loc,inds,exclude){ if (! exclude) exclude = new Set(); // If not given, replace. let adjacent = new Set([loc]); exclude.add(loc); @@ -62,7 +63,7 @@ function Search(){ // searchBlock should be a Space object. return adjacent; } - this.del = function(loc){ + del(loc){ delete this.tiles[loc]; [0, 1].forEach( ind => { let theind = bs(this.sort[ind], loc, getComp(ind)); @@ -71,13 +72,13 @@ function Search(){ // searchBlock should be a Space object. }) } - this.has = function(loc){ + has(loc){ if (bs(this.sort[0], loc, getComp(0)) < 0) return false; // Could use ind = 1 just as well, but doesn't really matter // bs returns negative if not found. else return true; } - this.update = function(space){ // Must be a space w/ valid loc; allows overlay of some arbitrary text (in the space) mostly for recording updates from the server without directly cataloguing them + update(space){ // Must be a space w/ valid loc; allows overlay of some arbitrary text (in the space) mostly for recording updates from the server without directly cataloguing them if (space.loc.length === 0) throw "Update space must have valid loc"; let tiles = raster(space); // Returns a this.tiles-style binding: {'2,-3':Space object} for (let tile in tiles) { @@ -87,12 +88,12 @@ function Search(){ // searchBlock should be a Space object. } } - this.match = function(space, call){ + match(space, call){ this.spaces.push(space); this.calls.push(call); } - this.unmatch = function(space){ + unmatch(space){ let ind = this.spaces.indexOf(space); this.spaces.splice(ind, 1); this.calls.splice(ind, 1); -- cgit