Skip to content

Commit ec13079

Browse files
refactor(algorithms): Remove unused imports and optimize _log2 function
1 parent a6e53f9 commit ec13079

File tree

1 file changed

+6
-2
lines changed

1 file changed

+6
-2
lines changed

pkgs/collection/lib/src/algorithms.dart

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
/// A selection of data manipulation algorithms.
66
library;
77

8-
import 'dart:math' show Random, ln2, log;
8+
import 'dart:math' show Random;
99

1010
import 'utils.dart';
1111

@@ -535,7 +535,11 @@ void quickSortBy<E, K>(
535535
const int _pdqInsertionSortThreshold = 24;
536536

537537
/// Computes the base-2 logarithm of [n].
538-
int _log2(int n) => n == 0 ? 0 : (log(n) / ln2).floor();
538+
/// Computes the base-2 logarithm of [n].
539+
///
540+
/// Uses bitLength to compute the floor(log2(n)) efficiently. For n == 0
541+
/// we return 0.
542+
int _log2(int n) => n > 0 ? n.bitLength - 1 : 0;
539543

540544
/// Swaps the elements at positions [i] and [j] in [elements].
541545
void _pdqSwap<E>(List<E> elements, int i, int j) {

0 commit comments

Comments
 (0)