import { asInt } from "@thi.ng/api/typedarray" ; /**
 * Yields sequence of 2D grid coordinates along 2D Hilbert curve using
 * given `cols` and `rows` (each max. 32768 (2^15)).
 *
 * Ported & modified from original Java code by Christopher Kulla.
 *  https param cols param rows export function * hilbert2d ( cols , rows = cols ) { [ cols , rows ] = asInt ( cols , rows ) ; let hIndex = 0 ; let hOrder = 0 ; while ( ( 1 << hOrder < cols || 1 << hOrder < rows ) && hOrder < 15 ) hOrder ++ ; const numBuckets = 1 << ( 2 * hOrder ) ; for ( let i = 0 , n = cols * rows ; i < n ; i ++ ) { let hx , hy ; do { let s , t , comp , swap , cs , sr ; s = hIndex | ( 0x55555555 << ( 2 * hOrder ) ) ; sr = ( s >>> 1 ) & 0x55555555 ; cs = ( ( s & 0x55555555 ) + sr ) ^ 0x55555555 ; cs = cs ^ ( cs >>> 2 ) ; cs = cs ^ ( cs >>> 4 ) ; cs = cs ^ ( cs >>> 8 ) ; cs = cs ^ ( cs >>> 16 ) ; swap = cs & 0x55555555 ; comp = ( cs >>> 1 ) & 0x55555555 ; t = ( s & swap ) ^ comp ; s = s ^ sr ^ t ^ ( t << 1 ) ; s = s & ( ( 1 << ( 2 * hOrder ) ) - 1 ) ; t = ( s ^ ( s >>> 1 ) ) & 0x22222222 ; s = s ^ t ^ ( t << 1 ) ; t = ( s ^ ( s >>> 2 ) ) & 0x0c0c0c0c ; s = s ^ t ^ ( t << 2 ) ; t = ( s ^ ( s >>> 4 ) ) & 0x00f000f0 ; s = s ^ t ^ ( t << 4 ) ; t = ( s ^ ( s >>> 8 ) ) & 0x0000ff00 ; s = s ^ t ^ ( t << 8 ) ; hx = s >>> 16 ; hy = s & 0xffff ; hIndex ++ ; } while ( ( hx >= cols || hy >= rows || hx < 0 || hy < 0 ) && hIndex < numBuckets ) ; yield [ hx , hy ] ; } }