Skip to content

Commit 2c3af42

Browse files
committed
Remove Sort1dExt in favor of SliceExt.
Reduces worst-case complexity from O(n^2) to O(n log(n)).
1 parent f830c9e commit 2c3af42

File tree

7 files changed

+30
-352
lines changed

7 files changed

+30
-352
lines changed

Cargo.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "ndarray-stats"
3-
version = "0.5.1"
3+
version = "0.6.0"
44
authors = ["Jim Turner <[email protected]>", "LukeMathWalker <[email protected]>"]
55
edition = "2018"
66

@@ -17,6 +17,7 @@ categories = ["data-structures", "science"]
1717

1818
[dependencies]
1919
ndarray = "0.15.0"
20+
ndarray-slice = "0.1.1"
2021
noisy_float = "0.2.0"
2122
num-integer = "0.1"
2223
num-traits = "0.2"

benches/sort.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use criterion::{
22
black_box, criterion_group, criterion_main, AxisScale, BatchSize, Criterion, PlotConfiguration,
33
};
44
use ndarray::prelude::*;
5-
use ndarray_stats::Sort1dExt;
5+
use ndarray_slice::Slice1Ext;
66
use rand::prelude::*;
77

88
fn get_from_sorted_mut(c: &mut Criterion) {
@@ -19,7 +19,7 @@ fn get_from_sorted_mut(c: &mut Criterion) {
1919
|| Array1::from(data.clone()),
2020
|mut arr| {
2121
for &i in &indices {
22-
black_box(arr.get_from_sorted_mut(i));
22+
black_box(arr.select_nth_unstable(i));
2323
}
2424
},
2525
BatchSize::SmallInput,
@@ -42,7 +42,7 @@ fn get_many_from_sorted_mut(c: &mut Criterion) {
4242
b.iter_batched(
4343
|| Array1::from(data.clone()),
4444
|mut arr| {
45-
black_box(arr.get_many_from_sorted_mut(&indices));
45+
black_box(arr.select_many_nth_unstable(&indices));
4646
},
4747
BatchSize::SmallInput,
4848
)

src/lib.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@ pub use crate::entropy::EntropyExt;
3535
pub use crate::histogram::HistogramExt;
3636
pub use crate::maybe_nan::{MaybeNan, MaybeNanExt};
3737
pub use crate::quantile::{interpolate, Quantile1dExt, QuantileExt};
38-
pub use crate::sort::Sort1dExt;
3938
pub use crate::summary_statistics::SummaryStatisticsExt;
4039

4140
#[cfg(test)]
@@ -105,5 +104,4 @@ pub mod errors;
105104
pub mod histogram;
106105
mod maybe_nan;
107106
mod quantile;
108-
mod sort;
109107
mod summary_statistics;

src/quantile/mod.rs

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
use self::interpolate::{higher_index, lower_index, Interpolate};
2-
use super::sort::get_many_from_sorted_mut_unchecked;
32
use crate::errors::QuantileError;
43
use crate::errors::{EmptyInput, MinMaxError, MinMaxError::UndefinedOrder};
54
use crate::{MaybeNan, MaybeNanExt};
5+
use indexmap::IndexMap;
66
use ndarray::prelude::*;
77
use ndarray::{Data, DataMut, RemoveAxis, Zip};
8+
use ndarray_slice::Slice1Ext;
89
use noisy_float::types::N64;
910
use std::cmp;
1011

@@ -471,15 +472,20 @@ where
471472
searched_indexes.push(higher_index(q, axis_len));
472473
}
473474
}
474-
searched_indexes.sort();
475-
searched_indexes.dedup();
475+
let mut indexes = Array1::from_vec(searched_indexes);
476+
indexes.sort_unstable();
477+
let (indexes, _duplicates) = indexes.partition_dedup();
476478

477479
let mut results = Array::from_elem(results_shape, data.first().unwrap().clone());
478480
Zip::from(results.lanes_mut(axis))
479481
.and(data.lanes_mut(axis))
480482
.for_each(|mut results, mut data| {
481-
let index_map =
482-
get_many_from_sorted_mut_unchecked(&mut data, &searched_indexes);
483+
let (lower_values, _higher) = data.select_many_nth_unstable(&indexes);
484+
let index_map = indexes
485+
.iter()
486+
.copied()
487+
.zip(lower_values.iter().map(|(_lower, value)| (*value).clone()))
488+
.collect::<IndexMap<usize, A>>();
483489
for (result, &q) in results.iter_mut().zip(qs) {
484490
let lower = if I::needs_lower(q, axis_len) {
485491
Some(index_map[&lower_index(q, axis_len)].clone())

src/sort.rs

Lines changed: 0 additions & 298 deletions
This file was deleted.

0 commit comments

Comments
 (0)