Skip to content

Commit a6adc76

Browse files
committed
Fix build with rayon feature.
1 parent be42525 commit a6adc76

File tree

4 files changed

+92
-86
lines changed

4 files changed

+92
-86
lines changed

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "ndarray-slice"
3-
version = "0.5.0"
3+
version = "0.5.1"
44
rust-version = "1.85.0"
55
edition = "2024"
66
authors = ["Rouven Spreckels <rs@qu1x.dev>"]

RELEASES.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
# Version 0.5.1 (2025-02-22)
2+
3+
* Fix build with `rayon` feature.
4+
15
# Version 0.5.0 (2025-02-22)
26

37
* Rust Edition 2024.

src/lib.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1797,13 +1797,13 @@ where
17971797
let sz_u32 = mem::size_of::<(K, u32)>();
17981798
let sz_usize = mem::size_of::<(K, usize)>();
17991799

1800-
if sz_u8 < sz_u16 && len <= (std::u8::MAX as usize) {
1800+
if sz_u8 < sz_u16 && len <= (u8::MAX as usize) {
18011801
return sort_by_key!(u8);
18021802
}
1803-
if sz_u16 < sz_u32 && len <= (std::u16::MAX as usize) {
1803+
if sz_u16 < sz_u32 && len <= (u16::MAX as usize) {
18041804
return sort_by_key!(u16);
18051805
}
1806-
if sz_u32 < sz_usize && len <= (std::u32::MAX as usize) {
1806+
if sz_u32 < sz_usize && len <= (u32::MAX as usize) {
18071807
return sort_by_key!(u32);
18081808
}
18091809
sort_by_key!(usize)

src/par/merge_sort.rs

Lines changed: 84 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -295,7 +295,7 @@ where
295295
// }
296296
//}
297297

298-
impl<'a, T> Drop for MergeHole<'a, T> {
298+
impl<T> Drop for MergeHole<'_, T> {
299299
fn drop(&mut self) {
300300
// SAFETY: `T` is not a zero-sized type, and these are pointers into a slice's elements.
301301
unsafe {
@@ -389,86 +389,88 @@ where
389389
T: Send,
390390
F: Fn(&T, &T) -> bool,
391391
{
392-
// Very short runs are extended using insertion sort to span at least this many elements.
393-
const MIN_RUN: usize = 10;
394-
395-
let len = v.len();
396-
397-
// In order to identify natural runs in `v`, we traverse it backwards. That might seem like a
398-
// strange decision, but consider the fact that merges more often go in the opposite direction
399-
// (forwards). According to benchmarks, merging forwards is slightly faster than merging
400-
// backwards. To conclude, identifying runs by traversing backwards improves performance.
401-
let mut runs = vec![];
402-
let mut end = len;
403-
while end > 0 {
404-
// Find the next natural run, and reverse it if it's strictly descending.
405-
let mut start = end - 1;
406-
407-
if start > 0 {
408-
start -= 1;
409-
410-
let w = v.view();
411-
if is_less(w.uget(start + 1), w.uget(start)) {
412-
while start > 0 && is_less(w.uget(start), w.uget(start - 1)) {
413-
start -= 1;
414-
}
415-
416-
// If this descending run covers the whole slice, return immediately.
417-
if start == 0 && end == len {
418-
return MergesortResult::Descending;
392+
unsafe {
393+
// Very short runs are extended using insertion sort to span at least this many elements.
394+
const MIN_RUN: usize = 10;
395+
396+
let len = v.len();
397+
398+
// In order to identify natural runs in `v`, we traverse it backwards. That might seem like a
399+
// strange decision, but consider the fact that merges more often go in the opposite direction
400+
// (forwards). According to benchmarks, merging forwards is slightly faster than merging
401+
// backwards. To conclude, identifying runs by traversing backwards improves performance.
402+
let mut runs = vec![];
403+
let mut end = len;
404+
while end > 0 {
405+
// Find the next natural run, and reverse it if it's strictly descending.
406+
let mut start = end - 1;
407+
408+
if start > 0 {
409+
start -= 1;
410+
411+
let w = v.view();
412+
if is_less(w.uget(start + 1), w.uget(start)) {
413+
while start > 0 && is_less(w.uget(start), w.uget(start - 1)) {
414+
start -= 1;
415+
}
416+
417+
// If this descending run covers the whole slice, return immediately.
418+
if start == 0 && end == len {
419+
return MergesortResult::Descending;
420+
} else {
421+
reverse(v.slice_mut(s![start..end]));
422+
}
419423
} else {
420-
reverse(v.slice_mut(s![start..end]));
421-
}
422-
} else {
423-
while start > 0 && !is_less(w.uget(start), w.uget(start - 1)) {
424-
start -= 1;
424+
while start > 0 && !is_less(w.uget(start), w.uget(start - 1)) {
425+
start -= 1;
426+
}
427+
428+
// If this non-descending run covers the whole slice, return immediately.
429+
if end - start == len {
430+
return MergesortResult::NonDescending;
431+
}
425432
}
433+
}
426434

427-
// If this non-descending run covers the whole slice, return immediately.
428-
if end - start == len {
429-
return MergesortResult::NonDescending;
430-
}
435+
// Insert some more elements into the run if it's too short. Insertion sort is faster than
436+
// merge sort on short sequences, so this significantly improves performance.
437+
while start > 0 && end - start < MIN_RUN {
438+
start -= 1;
439+
insert_head(v.slice_mut(s![start..end]), &is_less);
431440
}
432-
}
433441

434-
// Insert some more elements into the run if it's too short. Insertion sort is faster than
435-
// merge sort on short sequences, so this significantly improves performance.
436-
while start > 0 && end - start < MIN_RUN {
437-
start -= 1;
438-
insert_head(v.slice_mut(s![start..end]), &is_less);
442+
// Push this run onto the stack.
443+
runs.push(Run {
444+
start,
445+
len: end - start,
446+
});
447+
end = start;
448+
449+
// Merge some pairs of adjacent runs to satisfy the invariants.
450+
while let Some(r) = collapse(&runs) {
451+
let left = runs[r + 1];
452+
let right = runs[r];
453+
merge(
454+
v.slice_mut(s![left.start..right.start + right.len]),
455+
left.len,
456+
buf,
457+
&is_less,
458+
);
459+
460+
runs[r] = Run {
461+
start: left.start,
462+
len: left.len + right.len,
463+
};
464+
runs.remove(r + 1);
465+
}
439466
}
440467

441-
// Push this run onto the stack.
442-
runs.push(Run {
443-
start,
444-
len: end - start,
445-
});
446-
end = start;
447-
448-
// Merge some pairs of adjacent runs to satisfy the invariants.
449-
while let Some(r) = collapse(&runs) {
450-
let left = runs[r + 1];
451-
let right = runs[r];
452-
merge(
453-
v.slice_mut(s![left.start..right.start + right.len]),
454-
left.len,
455-
buf,
456-
&is_less,
457-
);
468+
// Finally, exactly one run must remain in the stack.
469+
debug_assert!(runs.len() == 1 && runs[0].start == 0 && runs[0].len == len);
458470

459-
runs[r] = Run {
460-
start: left.start,
461-
len: left.len + right.len,
462-
};
463-
runs.remove(r + 1);
464-
}
471+
// The original order of the slice was neither non-descending nor descending.
472+
MergesortResult::Sorted
465473
}
466-
467-
// Finally, exactly one run must remain in the stack.
468-
debug_assert!(runs.len() == 1 && runs[0].start == 0 && runs[0].len == len);
469-
470-
// The original order of the slice was neither non-descending nor descending.
471-
MergesortResult::Sorted
472474
}
473475

474476
////////////////////////////////////////////////////////////////////////////
@@ -644,7 +646,7 @@ unsafe fn par_merge<T, F>(
644646
dest_start: usize,
645647
}
646648

647-
impl<'a, T> Drop for State<'a, T> {
649+
impl<T> Drop for State<'_, T> {
648650
fn drop(&mut self) {
649651
//let size = size_of::<T>();
650652
//let left_len = (self.left_end as usize - self.left_start as usize) / size;
@@ -792,7 +794,7 @@ unsafe fn recurse<T, F>(
792794
len: usize,
793795
}
794796

795-
impl<'a, T> Drop for CopyOnDrop<'a, T> {
797+
impl<T> Drop for CopyOnDrop<'_, T> {
796798
fn drop(&mut self) {
797799
unsafe {
798800
for i in 0..self.len {
@@ -922,8 +924,8 @@ mod test {
922924
use core::cmp::Ordering;
923925
use ndarray::{Array1, ArrayView1, s};
924926
use quickcheck_macros::quickcheck;
925-
use rand::distributions::Uniform;
926-
use rand::{Rng, thread_rng};
927+
use rand::distr::Uniform;
928+
use rand::{Rng, rng};
927929

928930
#[test]
929931
fn split() {
@@ -948,19 +950,19 @@ mod test {
948950
check(&[1, 2, 2, 2, 2, 3], &[]);
949951
check(&[], &[1, 2, 2, 2, 2, 3]);
950952

951-
let rng = &mut thread_rng();
953+
let rng = &mut rng();
952954

953955
for _ in 0..100 {
954-
let limit: u32 = rng.gen_range(1..21);
955-
let left_len: usize = rng.gen_range(0..20);
956-
let right_len: usize = rng.gen_range(0..20);
956+
let limit: u32 = rng.random_range(1..21);
957+
let left_len: usize = rng.random_range(0..20);
958+
let right_len: usize = rng.random_range(0..20);
957959

958960
let mut left = rng
959-
.sample_iter(&Uniform::new(0, limit))
961+
.sample_iter(&Uniform::new(0, limit).unwrap())
960962
.take(left_len)
961963
.collect::<Vec<_>>();
962964
let mut right = rng
963-
.sample_iter(&Uniform::new(0, limit))
965+
.sample_iter(&Uniform::new(0, limit).unwrap())
964966
.take(right_len)
965967
.collect::<Vec<_>>();
966968

0 commit comments

Comments
 (0)