import { Tile } from './tile.matrix.set.js' ; const CHAR_0 = '0' . charCodeAt ( 0 ) ; const CHAR_1 = '1' . charCodeAt ( 0 ) ; const CHAR_2 = '2' . charCodeAt ( 0 ) ; const CHAR_3 = '3' . charCodeAt ( 0 ) ; export const QuadKey = { Keys : [ '0' , '1' , '2' , '3' ] , param qkA param qkB returns intersects ( qkA , qkB ) { const shortestLength = Math . min ( qkA . length , qkB . length ) ; return qkA . substr ( 0 , shortestLength ) === qkB . substr ( 0 , shortestLength ) ; } , param qkA param qkB commonPrefix ( qkA , qkB ) { const len = Math . min ( qkA . length , qkB . length ) ; let i = 0 ; for ( ; i < len ; ++ i ) if ( qkA [ i ] !== qkB [ i ] ) break ; return qkA . slice ( 0 , i ) ; } , example param qk children ( qk ) { return QuadKey . Keys . map ( ( c ) => qk + c ) ; } , example param qk parent ( qk ) { return qk . substr ( 0 , qk . length - 1 ) ; } , param quadKey toTile ( quadKey ) { let x = 0 ; let y = 0 ; const z = quadKey . length ; for ( let i = z ; i > 0 ; i -- ) { const mask = 1 << ( i - 1 ) ; const q = quadKey . charCodeAt ( z - i ) ; if ( q === CHAR_1 ) x |= mask ; if ( q === CHAR_2 ) y |= mask ; if ( q === CHAR_3 ) { x |= mask ; y |= mask ; } } return { x , y , z } ; } , param tile fromTile ( tile ) { const { x , y , z } = tile ; let quadKey = '' ; for ( let zI = z ; zI > 0 ; zI -- ) { let b = CHAR_0 ; const mask = 1 << ( zI - 1 ) ; if ( ( x & mask ) !== 0 ) b ++ ; if ( ( y & mask ) !== 0 ) b += 2 ; quadKey += String . fromCharCode ( b ) ; } return quadKey ; } , param a param b retun compareKeys ( a , b ) { return a === b ? 0 : a . length === b . length ? ( a < b ? - 1 : 1 ) : a . length - b . length ; } , } ;