mirror of
https://github.com/storytold/gridcraft.git
synced 2026-11-08 03:22:59 +00:00
Tighten selection comment and test index arithmetic
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
4523b817ce
commit
8b330ac9e2
2 files changed
+5
-4
No files matched your search
@@ -39,9 +39,10 @@ pub(super) fn sorted(mut v: Vec<f64>) -> Vec<f64> {
|
||||
}
|
||||
|
||||
/// Sorts `v` if it is already monotone in `total_cmp` order (a descending slice is reversed) and
|
||||
/// says whether it did. One cheap pass that stops at the first out-of-order pair, so unordered
|
||||
/// data costs next to nothing. Needed because the std sort recognises ascending and descending
|
||||
/// runs in O(n), which a selection cannot beat.
|
||||
/// says whether it did. At most two passes, each stopping at the first out-of-order pair, so
|
||||
/// unordered data costs next to nothing. Needed because the std sort recognises ascending and
|
||||
/// descending runs in O(n) with a small constant; selection is O(n) too but measured 1.2-1.8x
|
||||
/// slower on such input.
|
||||
fn sorted_if_monotone(v: &mut [f64]) -> bool {
|
||||
if v.is_sorted_by(|a, b| a.total_cmp(b).is_le()) {
|
||||
return true;
|
||||
|
||||
@@ -269,7 +269,7 @@ fn selection_matches_full_sort_bit_for_bit() {
|
||||
// Few distinct values: many ties.
|
||||
(0, r) => (r % 3) as f64,
|
||||
// Specials, including both zeros and NaNs.
|
||||
(1, r) => specials[(next() as usize + r as usize) % specials.len()],
|
||||
(1, r) => specials[(next() as usize).wrapping_add(r as usize) % specials.len()],
|
||||
// Wide random range.
|
||||
(2, _) => (next() as i64 as f64) / 1e3,
|
||||
// Integers with some specials mixed in.
|
||||
|
||||
Reference in new issue
Block a user