Skip to content

Commit

Permalink
SortUtils: fix termination condition & enforce unsigned value (#28411)
Browse files Browse the repository at this point in the history
  • Loading branch information
sciecode committed May 19, 2024
1 parent a49326c commit 016bdb5
Showing 1 changed file with 8 additions and 5 deletions.
13 changes: 8 additions & 5 deletions examples/jsm/utils/SortUtils.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
// Hybrid radix sort from
// - https://gist.github.com/sciecode/93ed864dd77c5c8803c6a86698d68dab
// - https://github.com/mrdoob/three.js/pull/27202#issuecomment-1817640271
//
// expects unsigned 32b integer values

const POWER = 3;
const BIT_MAX = 32;
const BIN_BITS = 1 << POWER;
Expand Down Expand Up @@ -102,11 +105,11 @@ export const radixSort = ( arr, opt ) => {

for ( let j = start + 1; j < start + len; j ++ ) {

const p = a[ j ], t = get( p );
const p = a[ j ], t = get( p ) >>> 0;
let i = j;
while ( i > 0 ) {
while ( i > start ) {

if ( compare( get( a[ i - 1 ] ), t ) )
if ( compare( get( a[ i - 1 ] ) >>> 0, t ) )
a[ i ] = a[ -- i ];
else
break;
Expand Down Expand Up @@ -140,14 +143,14 @@ export const radixSort = ( arr, opt ) => {
bin.fill( 0 );

for ( let j = start; j < end; j ++ )
bin[ ( get( a[ j ] ) >> shift ) & BIN_MAX ] ++;
bin[ ( get( a[ j ] ) >>> shift ) & BIN_MAX ] ++;

accumulate( bin );

cache.set( bin );

for ( let j = end - 1; j >= start; j -- )
b[ start + -- bin[ ( get( a[ j ] ) >> shift ) & BIN_MAX ] ] = a[ j ];
b[ start + -- bin[ ( get( a[ j ] ) >>> shift ) & BIN_MAX ] ] = a[ j ];

if ( depth == ITERATIONS - 1 ) return;

Expand Down

0 comments on commit 016bdb5

Please sign in to comment.