aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--examples/search.js66
1 files changed, 51 insertions, 15 deletions
diff --git a/examples/search.js b/examples/search.js
index 204dc00..9465e08 100644
--- a/examples/search.js
+++ b/examples/search.js
@@ -5,11 +5,24 @@
const bs = require('binary-search');
const Space = require('../space');
const comb = require('../utils/comb.js')
+const getdims = require('../utils/getdims.js');
function getComp(index){
return (element,needle) => element[index] - needle[index];
}
+function tileToChar(coord){
+ let cofactor = [8,16];
+ return [coord[0]*cofactor[0], coord[1]*cofactor[1]];
+}
+
+function coordmult(coord,factor){
+ return [coord[0]*factor, coord[1]*factor]
+}
+
+function coordSum(coord, coord){ // This needs to be generalized and standardized (vector arithmetic) in a utils or a library
+ return [coord[0]+coord[0], coord[1]+coord[1]];
+}
function Search(searchBlock){ // searchBlock should be a Space object.
this.tiles = {}; // Object which stores Spaces.
@@ -19,18 +32,24 @@ function Search(searchBlock){ // searchBlock should be a Space object.
this.tiles[loc] = space;
let inds = Array(2); // Records horizontal and vertical indices for insertion. Then actually inserts the item.
[null,null].forEach( (item, ind) => {
- inds[ind] = -bs(sort[ind], loc, getComp(ind))-1;
+ inds[ind] = -bs(this.sort[ind], loc, getComp(ind))-1;
if (inds[ind] < 0) throw "loc should not be a part of the list";
- sort[ind].splice(inds[ind], 0, loc);
+ this.sort[ind].splice(inds[ind], 0, loc);
});
- let block = this.block(inds);
+ let block = this.block(loc, inds);
let searchspace = new Space();
- block.forEach( (tile) => {searchspace.comb(tiles[tile],comb.add,[tile[0]*8,tile[1]*16])});
- return searchspace.search(searchBlock); // According to space docs, [] on failure and a character location on success
+ let root = tileToChar(getdims(Array.from(block))[0]);
+ //let root = [0,0]; //PLACEHOLDER: get upperleft-most tile in searchspace using getdims.
+ // searchspace.comb(this.tiles['2,2'], comb.add, [16,32]);
+ block.forEach( (tile) => {searchspace.comb(this.tiles[tile],comb.add,coordSum(tileToChar(tile),coordmult(root,-1)))});
+ coords = searchspace.search(searchBlock);
+ if (! coords) return coords; // If coords are empty (i.e. no match), return;
+ return [ coords[0] , coords[1] ];
+ //return searchspace.search(searchBlock); // According to space docs, [] on failure and a character location on success
}
- this.block = function(loc,inds,exclude=Set()){
- let adjacent = Set([loc]);
+ this.block = function(loc,inds,exclude=new Set()){
+ let adjacent = new Set([loc]);
exclude.add(loc);
[null,null].forEach( (item, ind) => {
// inds[ind] is address, so check neighbors on sort[ind] (up, then down)
@@ -38,21 +57,38 @@ function Search(searchBlock){ // searchBlock should be a Space object.
// on each, if sort[ind][otherind][1-ind] == sort[ind][inds[ind]][1-ind],
// add to adjacent and search it with exclude=adjacent. Add that to adjacent
let addr = inds[ind];
- for (let i = -1; i <= 1; i++) for (let j = 1; sort[ind][inds[ind]][ind] == sort[ind][inds[ind]+i*j][ind]; j++)
- if (! exclude.has(sort[ind][inds[ind]]) && sort[ind][inds[ind]][1-ind] == sort[inds[ind]+i*j][1-ind]){ // Make more readable
- adjacent.add(...this.block(sort[ind][inds[ind]+i*j], inds[ind+i*j], exclude=adjacent)); // Already includes itself
- }
- return adjacent;
+ for (let i = -1; i <= 1; i+=2) for (let j = 1; this.sort[ind][inds[ind]+i*j] && this.sort[ind][inds[ind]][ind] == this.sort[ind][inds[ind]+i*j][ind]; j++){
+ if (! exclude.has(this.sort[ind][inds[ind]]) && this.sort[ind][inds[ind]][1-ind] == this.sort[inds[ind]+i*j][1-ind]){ // Make more readable
+ adjacent.add(...this.block(this.sort[ind][inds[ind]+i*j], inds[ind+i*j], exclude=adjacent)); // Already includes itself
+ }}
});
+ return adjacent;
}
this.del = function(loc){
- delete tiles[loc];
+ delete this.tiles[loc];
[null,null].forEach( (item,ind) => {
- let theind = bs(sort[ind], loc, getComp(ind));
+ let theind = bs(this.sort[ind], loc, getComp(ind));
if (theind < 0) throw "loc must already be a part of the list";
- sort[ind].splice(bs(sort[ind], loc, getComp(ind)), 1);
+ this.sort[ind].splice(bs(this.sort[ind], loc, getComp(ind)), 1);
})
}
}
+
+let space = new Space();
+space.adhoc('jarvis');
+let test = new Search(space);
+let tile = new Space();
+tile.adhoc('\
+ \n\
+ jarvis \n\
+ \n\
+ \n\
+ \n\
+ \n\
+ \n\
+ \n\
+');
+console.log(test.add([2,2],tile));
+
module.exports = Search;