diff options
Diffstat (limited to 'examples')
-rw-r--r-- | examples/search.js | 30 |
1 files changed, 30 insertions, 0 deletions
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; |