diff options
author | Holden Rohrer <hr@hrhr.dev> | 2020-01-29 23:42:44 -0500 |
---|---|---|
committer | Holden Rohrer <hr@hrhr.dev> | 2020-01-29 23:45:18 -0500 |
commit | 1cc310e20dda4a378aac76b49288e6c8e2361e5e (patch) | |
tree | 776e45ba83a264ae4870b1bcf2138e8f7ff6f847 /tools | |
parent | a468d5783fe63f68e679dd7f51d670fc82905131 (diff) |
generalized tools/search model
search.js now provides for a .match and .unmatch method,
so it doesn't need to be called every time (in-line with the singleton
MetaSocket object model, meaning that callbacks are used and several
spaces can be matched at a time)
Diffstat (limited to 'tools')
-rw-r--r-- | tools/search.js | 23 |
1 files changed, 19 insertions, 4 deletions
diff --git a/tools/search.js b/tools/search.js index 7f681f0..78e0ee7 100644 --- a/tools/search.js +++ b/tools/search.js @@ -12,11 +12,13 @@ function getComp(index){ return (element,needle) => (element[index] - needle[index] || element[1-index] - needle[1-index]) } -function Search(searchBlock){ // searchBlock should be a Space object. +function Search(){ // searchBlock should be a Space object. this.tiles = {}; // Object which stores Spaces. this.sort = [[],[]]; // Vertically/horizontally sorted list of tiles for fast addition, deletion, and searching + this.spaces = []; + this.calls = []; - this.add = function(loc, space){ // loc should be [tileY,tileX] and space Space. + this.add = function(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 @@ -29,8 +31,10 @@ function Search(searchBlock){ // searchBlock should be a Space object. block.forEach( (tile) => { searchspace.comb( this.tiles[tile], comb.add ); }); - coords = searchspace.search(searchBlock); // According to space docs, [] on failure and a character location on success - return vec.add(coords, searchspace.loc); + for (let i=0; i<this.spaces.length; i++){ + let coords = searchspace.search(this.spaces[i]); + if (coords.length) this.calls[i](vec.add(coords, searchspace.loc), send, searchspace); + } } this.block = function(loc,inds,exclude){ @@ -79,6 +83,17 @@ function Search(searchBlock){ // searchBlock should be a Space object. else this.add(tile, tiles[tile]); } } + + this.match = function(space, call){ + this.spaces.push(space); + this.calls.push(call); + } + + this.unmatch = function(space){ + let ind = this.spaces.indexOf(space); + this.spaces.splice(ind, 1); + this.calls.splice(ind, 1); + } } module.exports = Search; |