@@ -5,6 +5,7 @@ const ARRAY_TYPES = [
55] ;
66
77/** @typedef {Int8ArrayConstructor | Uint8ArrayConstructor | Uint8ClampedArrayConstructor | Int16ArrayConstructor | Uint16ArrayConstructor | Int32ArrayConstructor | Uint32ArrayConstructor | Float32ArrayConstructor | Float64ArrayConstructor } TypedArrayConstructor */
8+ /** @typedef {Int8Array | Uint8Array | Uint8ClampedArray | Int16Array | Uint16Array | Int32Array | Uint32Array | Float32Array | Float64Array } TypedArray */
89
910const VERSION = 1 ; // serialized format version
1011const HEADER_SIZE = 8 ;
@@ -43,7 +44,7 @@ export default class KDBush {
4344 * @param {number } numItems
4445 * @param {number } [nodeSize=64] Size of the KD-tree node (64 by default).
4546 * @param {TypedArrayConstructor } [ArrayType=Float64Array] The array type used for coordinates storage (`Float64Array` by default).
46- * @param {ArrayBufferConstructor } [ArrayBufferType=ArrayBuffer] The array buffer type used for storage (`ArrayBuffer` by default).
47+ * @param {ArrayBufferConstructor | SharedArrayBufferConstructor } [ArrayBufferType=ArrayBuffer] The array buffer type used for storage (`ArrayBuffer` by default).
4748 * @param {ArrayBufferLike } [data] (For internal use only)
4849 */
4950 constructor ( numItems , nodeSize = 64 , ArrayType = Float64Array , ArrayBufferType = ArrayBuffer , data ) {
@@ -65,22 +66,26 @@ export default class KDBush {
6566
6667 if ( data ) { // reconstruct an index from a buffer
6768 this . data = data ;
68- this . ids = new this . IndexArrayType ( this . data , HEADER_SIZE , numItems ) ;
69- this . coords = new this . ArrayType ( this . data , HEADER_SIZE + idsByteSize + padCoords , numItems * 2 ) ;
69+ // @ts -expect-error TS can't handle SharedArrayBuffer overloads
70+ this . ids = new this . IndexArrayType ( data , HEADER_SIZE , numItems ) ;
71+ // @ts -expect-error TS can't handle SharedArrayBuffer overloads
72+ this . coords = new ArrayType ( data , HEADER_SIZE + idsByteSize + padCoords , numItems * 2 ) ;
7073 this . _pos = numItems * 2 ;
7174 this . _finished = true ;
7275
7376 } else { // initialize a new index
74- this . data = new ArrayBufferType ( HEADER_SIZE + coordsByteSize + idsByteSize + padCoords ) ;
75- this . ids = new this . IndexArrayType ( this . data , HEADER_SIZE , numItems ) ;
76- this . coords = new this . ArrayType ( this . data , HEADER_SIZE + idsByteSize + padCoords , numItems * 2 ) ;
77+ const data = this . data = new ArrayBufferType ( HEADER_SIZE + coordsByteSize + idsByteSize + padCoords ) ;
78+ // @ts -expect-error TS can't handle SharedArrayBuffer overloads
79+ this . ids = new this . IndexArrayType ( data , HEADER_SIZE , numItems ) ;
80+ // @ts -expect-error TS can't handle SharedArrayBuffer overloads
81+ this . coords = new ArrayType ( data , HEADER_SIZE + idsByteSize + padCoords , numItems * 2 ) ;
7782 this . _pos = 0 ;
7883 this . _finished = false ;
7984
8085 // set header
81- new Uint8Array ( this . data , 0 , 2 ) . set ( [ 0xdb , ( VERSION << 4 ) + arrayTypeIndex ] ) ;
82- new Uint16Array ( this . data , 2 , 1 ) [ 0 ] = nodeSize ;
83- new Uint32Array ( this . data , 4 , 1 ) [ 0 ] = numItems ;
86+ new Uint8Array ( data , 0 , 2 ) . set ( [ 0xdb , ( VERSION << 4 ) + arrayTypeIndex ] ) ;
87+ new Uint16Array ( data , 2 , 1 ) [ 0 ] = nodeSize ;
88+ new Uint32Array ( data , 4 , 1 ) [ 0 ] = numItems ;
8489 }
8590 }
8691
@@ -224,7 +229,7 @@ export default class KDBush {
224229
225230/**
226231 * @param {Uint16Array | Uint32Array } ids
227- * @param {InstanceType<TypedArrayConstructor> } coords
232+ * @param {TypedArray } coords
228233 * @param {number } nodeSize
229234 * @param {number } left
230235 * @param {number } right
@@ -248,7 +253,7 @@ function sort(ids, coords, nodeSize, left, right, axis) {
248253 * Custom Floyd-Rivest selection algorithm: sort ids and coords so that
249254 * [left..k-1] items are smaller than k-th item (on either x or y axis)
250255 * @param {Uint16Array | Uint32Array } ids
251- * @param {InstanceType<TypedArrayConstructor> } coords
256+ * @param {TypedArray } coords
252257 * @param {number } k
253258 * @param {number } left
254259 * @param {number } right
@@ -296,7 +301,7 @@ function select(ids, coords, k, left, right, axis) {
296301
297302/**
298303 * @param {Uint16Array | Uint32Array } ids
299- * @param {InstanceType<TypedArrayConstructor> } coords
304+ * @param {TypedArray } coords
300305 * @param {number } i
301306 * @param {number } j
302307 */
@@ -307,7 +312,7 @@ function swapItem(ids, coords, i, j) {
307312}
308313
309314/**
310- * @param {InstanceType<TypedArrayConstructor> } arr
315+ * @param {TypedArray } arr
311316 * @param {number } i
312317 * @param {number } j
313318 */
0 commit comments