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 ++++++++++++++++++++++++++++++ package-lock.json | 5 +++++ package.json | 1 + 3 files changed, 36 insertions(+) create mode 100644 examples/search.js 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; diff --git a/package-lock.json b/package-lock.json index 5514db8..7ca9bdf 100644 --- a/package-lock.json +++ b/package-lock.json @@ -9,6 +9,11 @@ "resolved": "https://registry.npmjs.org/async-limiter/-/async-limiter-1.0.1.tgz", "integrity": "sha512-csOlWGAcRFJaI6m+F2WKdnMKr4HhdhFVBk0H/QbJFMCr+uO2kwohwXQPxw/9OCxp05r5ghVBFSyioixx3gfkNQ==" }, + "binary-search": { + "version": "1.3.6", + "resolved": "https://registry.npmjs.org/binary-search/-/binary-search-1.3.6.tgz", + "integrity": "sha512-nbE1WxOTTrUWIfsfZ4aHGYu5DOuNkbxGokjV6Z2kxfJK3uaAb8zNK1muzOeipoLHZjInT4Br88BHpzevc681xA==" + }, "events": { "version": "3.0.0", "resolved": "https://registry.npmjs.org/events/-/events-3.0.0.tgz", diff --git a/package.json b/package.json index d84fc9a..63cd0ce 100644 --- a/package.json +++ b/package.json @@ -17,6 +17,7 @@ }, "homepage": "https://github.com/feynmansfedora/ywot-clean#readme", "dependencies": { + "binary-search": "^1.3.6", "events": ">=3.0.0", "json": ">=9.0.6", "ws": ">=7.2.0" -- cgit