we don't need an "alignment margin"; indices can't be larger than 32bit

as array indices can never be larger than 32 bit integers, the typed arrays always align correctly in the "data" array buffer (note that the boxes always come in multiples of four entries)

see https://github.com/mourner/flatbush/pull/11#discussion_r185742889
This commit is contained in:
Martin Raifer 2018-05-04 10:35:37 +02:00
parent 7ef566ef7e
commit 0f7d35c366

View File

@ -20,15 +20,12 @@ export default class Flatbush {
} while (n !== 1);
this.ArrayType = ArrayType || Float64Array;
this.IndexArrayType = Float64Array;
this.IndexArrayType = Uint32Array;
if (numNodes * 4 < Math.pow(2, 8)) {
this.IndexArrayType = Uint8Array;
} else if (numNodes * 4 < Math.pow(2, 16)) {
this.IndexArrayType = Uint16Array;
} else if (numNodes * 4 < Math.pow(2, 32)) {
this.IndexArrayType = Uint32Array;
}
const alignmentMargin = (this.IndexArrayType.BYTES_PER_ELEMENT - numNodes * 4 * this.ArrayType.BYTES_PER_ELEMENT % this.IndexArrayType.BYTES_PER_ELEMENT) % this.IndexArrayType.BYTES_PER_ELEMENT;
if (data) {
if (data instanceof ArrayBuffer) {
@ -38,7 +35,7 @@ export default class Flatbush {
}
this._boxes = new this.ArrayType(this.data, 0, numNodes * 4);
this._indices = new this.IndexArrayType(this.data,
numNodes * 4 * this.ArrayType.BYTES_PER_ELEMENT + alignmentMargin,
numNodes * 4 * this.ArrayType.BYTES_PER_ELEMENT,
numNodes);
this._numAdded = numItems;
@ -50,10 +47,10 @@ export default class Flatbush {
} else {
this.data = new ArrayBuffer(numNodes * 4 * this.ArrayType.BYTES_PER_ELEMENT +
alignmentMargin + numNodes * this.IndexArrayType.BYTES_PER_ELEMENT);
numNodes * this.IndexArrayType.BYTES_PER_ELEMENT);
this._boxes = new this.ArrayType(this.data, 0, numNodes * 4);
this._indices = new this.IndexArrayType(this.data,
numNodes * 4 * this.ArrayType.BYTES_PER_ELEMENT + alignmentMargin,
numNodes * 4 * this.ArrayType.BYTES_PER_ELEMENT,
numNodes);
this._numAdded = 0;
this._pos = 0;