From ed1603283db986d5838a7d3abefd7aac27ac70f9 Mon Sep 17 00:00:00 2001 From: Holden Rohrer Date: Thu, 19 Dec 2019 21:02:33 -0500 Subject: added search.js for jarvis (untested) --- examples/search.js | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 examples/search.js (limited to 'examples') diff --git a/examples/search.js b/examples/search.js new file mode 100644 index 0000000..d119fd3 --- /dev/null +++ b/examples/search.js @@ -0,0 +1,30 @@ +// A search utility for arbitrary (new) tileUpdates. +// Provides `add` functionality, which auto-checks adjacency with other blocks. Subsequently, it searches them +// Provides `del` functionality, which should be used with auto-deletion +// As of now, it assumes that no search block is larger than 8x16, or the size of a block. + +const bs = require('binary-search'); + +function getComp(index){ + return (element,needle) => element[index] - needle[index]; +} + + +function Search(searchBlock){ // searchBlock should be a Space object. + this.tiles = {}; // Object which stores Spaces. + this.vertsort = []; // Vertically sorted list of tiles for fast addition, deletion, and searching + this.horisort = []; // Horizantally sorted list + + this.add = function(loc, space){ // loc should be [tileY,tileX] and space Space. + this.tiles[loc] = space; + vertsort.insert(loc, bs(vertsort, loc, getComp(0))); // TEST + horisort.insert(loc, bs(horisort, loc, getComp(1))); + } + + this.del = function(loc){ + delete tiles[loc]; + vertsort.remove(bs(vertsort, loc, getComp(0))); // TEST + horisort.remove(bs(horisort, loc, getComp(1))); + } +} +module.exports = Search; -- cgit