Changeset View
Standalone View
binaries/data/mods/public/maps/random/rmgen/BoolArray.js
// Boolean array wrapper. Makes use of bitwise operations to acces each bit of Uint16Array. | |||||
function BoolArray(size) | |||||
vladislavbelov: A comment starts with a capital letter and a space before. | |||||
Done Inline Actionsuint16_t I think. Stan: uint16_t I think. | |||||
Done Inline Actionsnani: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Uint16Array | |||||
{ | |||||
// 16 == 2^4 | |||||
// 15 == 0b1111 | |||||
Done Inline ActionsMath.ceil(size / 16) => (size + 15) >> 4. Also some of your lines have semicolons, some don't. Use the same style for whole file. I assume we usually use semicolon always. vladislavbelov: `Math.ceil(size / 16)` => `(size + 15) >> 4`.
Also some of your lines have semicolons, some… | |||||
// (size + 15) >> 4 == Math.ceil(size/16) | |||||
this.arr = new Uint16Array((size + 15) >> 4); | |||||
}; | |||||
// Set bit to 1 (true). | |||||
BoolArray.prototype.set = function(pos) | |||||
Done Inline Actionssize / 16 => size & 0xF. The same below. vladislavbelov: `size / 16` => `size & 0xF`. The same below. | |||||
{ | |||||
// 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; | |||||
Done Inline ActionsFollow the code convention for naming. vladislavbelov: Follow the code convention for naming. | |||||
}; | |||||
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]) | |||||
Done Inline Actions++i Stan: ++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) | |||||
StanUnsubmitted Not Done Inline ActionsNot sure but maybe those functions could use a slice, and array.some() Stan: Not sure but maybe those functions could use a slice, and array.some() | |||||
StanUnsubmitted Not Done Inline ActionsWonder if that's slower or faster but that function can be replaced by return this.arr.slice(a >> 4, b >> 4).some(bit => ~bit); and the function above by return this.arr.slice(a >> 4, b >> 4).some(bit => bit); Stan: Wonder if that's slower or faster but that function can be replaced by
```lang=js
return this. | |||||
lyvUnsubmitted Not Done Inline Actionsslice creates a new Array, so slower. Don't know by how much. lyv: `slice` creates a new Array, so slower. Don't know by how much. | |||||
naniAuthorUnsubmitted Done Inline ActionsSlice doesn't include last index and slice creates a little bit of overhead (not 100% sure), as this function is called a massive number of times is important to use just keep it down to a minimum. Also, as smiley says, slice "creates" (some kind of reference/pointer I suppose) a new subarray so it potentiates overhead. Thanks for the input anyway :) nani: Slice doesn't include last index
https://developer.mozilla.org/en… | |||||
StanUnsubmitted Not Done Inline ActionsYou're welcome, both codes looked a lot alike, so I was looking for a way to simplify them :) Stan: You're welcome, both codes looked a lot alike, so I was looking for a way to simplify them :) | |||||
{ | |||||
const start = a >> 4; | |||||
const end = b >> 4; | |||||
for (let i = start; i <= end; ++i) | |||||
if (~this.arr[i]) | |||||
Done Inline Actions++i Stan: ++i | |||||
Done Inline Actions(that's the coding convention we use, postfix incrementor only when the return value is read from) elexis: (that's the coding convention we use, postfix incrementor only when the return value is read… | |||||
return true; | |||||
return false; | |||||
}; | |||||
Done Inline Actions(missing semicolon, since L48 is an assignment,) elexis: (missing semicolon, since L48 is an assignment,) |
A comment starts with a capital letter and a space before.