aboutsummaryrefslogtreecommitdiff
path: root/examples/search.js
blob: d119fd3fbd52e0a61cb17c130cf587b015c22c93 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
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;