Index: binaries/data/mods/public/maps/random/rmgen/BoolArray.js =================================================================== --- binaries/data/mods/public/maps/random/rmgen/BoolArray.js +++ binaries/data/mods/public/maps/random/rmgen/BoolArray.js @@ -0,0 +1,57 @@ +// Boolean array wrapper. Makes use of bitwise operations to acces each bit of Uint16Array. +function BoolArray(size) +{ + // 16 == 2^4 + // 15 == 0b1111 + // (size + 15) >> 4 == Math.ceil(size/16) + this.arr = new Uint16Array((size + 15) >> 4); +}; + +// Set bit to 1 (true). +BoolArray.prototype.set = function(pos) +{ + // pos & 0xF == pos & 15 == pos % 16 + this.arr[pos >> 4] |= 1 << (pos & 0xF); +}; + +// Set bit to 0 (false). +BoolArray.prototype.clear = function(pos) +{ + this.arr[pos >> 4] &= ~(1 << (pos & 0xF)); +}; + +BoolArray.prototype.get = function(pos) +{ + return (this.arr[pos >> 4] >> (pos & 0xF)) & 1; +}; + +BoolArray.prototype.getByteBlock = function(pos) +{ + return this.arr[pos >> 4]; +}; + +// Returns an aproximated guess if some bits between a and b included are not set to zero. +// Can return false negative but not false positive. +BoolArray.prototype.isSomeNotZero = function(a, b) +{ + const start = a >> 4; + const end = b >> 4; + + for (let i = start; i <= end; ++i) + if (this.arr[i]) + return true; + return false; +}; + +// Returns an aproximated guess if some bits between a and b included are not set to one. +// Can return false negative but not false positive. +BoolArray.prototype.isSomeNotOne = function(a, b) +{ + const start = a >> 4; + const end = b >> 4; + + for (let i = start; i <= end; ++i) + if (~this.arr[i]) + return true; + return false; +};