aboutsummaryrefslogtreecommitdiff
path: root/examples
diff options
context:
space:
mode:
authorHolden Rohrer <holden.rohrer@gmail.com>2019-12-19 21:02:33 -0500
committerHolden Rohrer <holden.rohrer@gmail.com>2019-12-19 21:02:33 -0500
commited1603283db986d5838a7d3abefd7aac27ac70f9 (patch)
treea38a81b72ed7206de48033ea57857d101644ae2b /examples
parent411af8efb00bd40486c2c1610bce0729298cbac9 (diff)
added search.js for jarvis (untested)
Diffstat (limited to 'examples')
-rw-r--r--examples/search.js30
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;