Browse Source

SortUtils: fix termination condition & enforce unsigned value (#28411)

Guilherme Avila 1 year ago
parent
commit
016bdb5fe1
1 changed files with 8 additions and 5 deletions
  1. 8 5
      examples/jsm/utils/SortUtils.js

+ 8 - 5
examples/jsm/utils/SortUtils.js

@@ -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;
@@ -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;
@@ -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;