Repository navigation
Refactored ks_twosample to take in two slices instead of a mutable owned Vec<f64> #406
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
c1b486c
e24648c
4f9b161
80497a3
68c13e1
b44e42c
ad3c2df
87053ea
d171c05
866c2fc
d2abefd
26896ae
9cc4816
6d6e4ba
c1314c1
85d4b5c
fcd44ba
ee4a8f3
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,118 @@ | ||
| use std::{ | ||
| cmp::Ordering, | ||
| ops::{Deref, Index}, | ||
| slice::SliceIndex, | ||
| }; | ||
|
|
||
| pub enum SortError { | ||
| NotSorted, | ||
| } | ||
|
|
||
| #[derive(Clone, Debug)] | ||
| pub enum Collection<'a, T> { | ||
| Ref(&'a [T]), | ||
| Owned(Vec<T>), | ||
| } | ||
|
|
||
| #[derive(Clone, Debug)] | ||
| pub struct SortedCollection<'a, T> { | ||
| sorted: Collection<'a, T>, | ||
| } | ||
|
|
||
| impl<'a, T> SortedCollection<'a, T> | ||
| where | ||
| T: Ord, | ||
| { | ||
| pub fn from_slice(coll: &'a [T]) -> Result<Self, SortError> { | ||
| match coll | ||
| .windows(2) | ||
| .all(|sl| sl[0].cmp(&sl[1]) == Ordering::Less) | ||
| { | ||
| true => Ok(Self { | ||
| sorted: Collection::Ref(coll), | ||
| }), | ||
| false => Err(SortError::NotSorted), | ||
| } | ||
| } | ||
| pub fn from_mut_vec(mut coll: Vec<T>) -> Self { | ||
| coll.sort(); | ||
| Self { | ||
| sorted: Collection::Owned(coll), | ||
| } | ||
| } | ||
|
coderabbitai[bot] marked this conversation as resolved.
|
||
| pub fn iter(&'a self) -> std::slice::Iter<'a, T> { | ||
| match self.sorted { | ||
| Collection::Ref(items) => items.iter(), | ||
| Collection::Owned(ref items) => items.iter(), | ||
| } | ||
|
Comment on lines
+43
to
+47
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🎯 Functional Correctness | 🟠 Major | ⚡ Quick win 🔎 Supported by static analysis🏁 Script executed: sed -n '1,120p' src/sorted_collection.rsRepository: statrs-dev/statrs Length of output: 1833 Do not bind
🤖 Prompt for AI Agents |
||
| } | ||
| } | ||
|
|
||
| impl<'a, T> TryFrom<&'a [T]> for SortedCollection<'a, T> | ||
| where | ||
| T: Ord, | ||
| { | ||
| type Error = SortError; | ||
|
|
||
| fn try_from(value: &'a [T]) -> Result<Self, Self::Error> { | ||
| Self::from_slice(value) | ||
| } | ||
| } | ||
|
|
||
| impl<'a, T> TryFrom<&'a Box<[T]>> for SortedCollection<'a, T> | ||
| where | ||
| T: Ord, | ||
| { | ||
| type Error = SortError; | ||
|
|
||
| fn try_from(value: &'a Box<[T]>) -> Result<Self, Self::Error> { | ||
| Self::from_slice(value.as_ref()) | ||
| } | ||
| } | ||
|
|
||
| impl<'a, T> AsRef<[T]> for SortedCollection<'a, T> { | ||
| fn as_ref(&self) -> &[T] { | ||
| match self.sorted { | ||
| Collection::Ref(items) => items, | ||
| Collection::Owned(ref items) => items.as_ref(), | ||
| } | ||
| } | ||
| } | ||
|
|
||
| pub struct SortedSlice<'a, T>(&'a [T]); | ||
|
|
||
| impl<'a, T> TryInto<SortedSlice<'a, T>> for &'a [T] | ||
| where | ||
| T: Ord, | ||
| { | ||
| type Error = SortError; | ||
|
|
||
| fn try_into(self) -> Result<SortedSlice<'a, T>, Self::Error> { | ||
| match self.is_sorted() { | ||
| true => Ok(SortedSlice(self)), | ||
| false => Err(SortError::NotSorted), | ||
| } | ||
| } | ||
| } | ||
|
|
||
| impl<'a, T, I> Index<I> for SortedSlice<'a, T> | ||
| where | ||
| T: Ord, | ||
| I: SliceIndex<[T]>, | ||
| { | ||
| type Output = <I as SliceIndex<[T]>>::Output; | ||
|
|
||
| fn index(&self, index: I) -> &Self::Output { | ||
| self.0.index(index) | ||
| } | ||
| } | ||
| impl<'a, T> Deref for SortedSlice<'a, T> | ||
| where | ||
| T: Ord, | ||
| { | ||
| type Target = [T]; | ||
|
|
||
| fn deref(&self) -> &Self::Target { | ||
| self.0 | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,38 @@ | ||
| use std::marker::PhantomData; | ||
|
|
||
| use crate::stats_tests::NaNPolicy; | ||
|
|
||
| pub trait SortedIterator { | ||
| fn sorted_iter(&self, policy: NaNPolicy) -> Sorted; | ||
| } | ||
|
|
||
| impl SortedIterator for Vec<f64> { | ||
| fn sorted_iter(&self, policy: NaNPolicy) -> Sorted { | ||
| Sorted::new(self, policy) | ||
| } | ||
| } | ||
|
|
||
| /// TODO iron out implementation details later because this is not optimal retrieval of sorted data | ||
| pub struct Sorted { | ||
| sorted_iter: std::vec::IntoIter<f64>, | ||
| policy: NaNPolicy, | ||
| } | ||
|
|
||
| impl Sorted { | ||
| pub fn new(data: &[f64], policy: NaNPolicy) -> Self { | ||
| let mut cloned = Vec::from(data); | ||
| cloned.sort_by(|a, b| a.total_cmp(b)); | ||
| Self { | ||
| sorted_iter: cloned.into_iter(), | ||
| policy, | ||
| } | ||
| } | ||
| } | ||
|
|
||
| impl Iterator for Sorted { | ||
| type Item = f64; | ||
|
|
||
| fn next(&mut self) -> Option<Self::Item> { | ||
| self.sorted_iter.next() | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,65 @@ | ||
| use std::{ | ||
| cmp::Ordering, | ||
| ops::{Deref, Index}, | ||
| slice::SliceIndex, | ||
| }; | ||
|
|
||
| #[derive(Copy, Clone, PartialEq, Eq, Debug, Hash)] | ||
| pub enum SortError { | ||
| NotSorted, | ||
| } | ||
| impl core::fmt::Display for SortError { | ||
| fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { | ||
| match self { | ||
| SortError::NotSorted => write!(f, "Sample is not sorted."), | ||
| } | ||
| } | ||
| } | ||
|
|
||
| pub struct SortedSlice<'a, T>(&'a [T]); | ||
|
|
||
| impl<'a, T> SortedSlice<'a, T> { | ||
| pub fn iter(&self) -> std::slice::Iter<'_, T> { | ||
| self.0.iter() | ||
| } | ||
| pub fn into_iter(self) -> std::slice::Iter<'a, T> { | ||
| self.0.into_iter() | ||
| } | ||
| } | ||
|
|
||
| impl<'a> TryInto<SortedSlice<'a, f64>> for &'a [f64] { | ||
| type Error = SortError; | ||
|
|
||
| fn try_into(self) -> Result<SortedSlice<'a, f64>, Self::Error> { | ||
| match ( | ||
| self.is_sorted_by(|a, b| a.total_cmp(b) != Ordering::Greater), | ||
| self.is_empty(), | ||
| ) { | ||
| (_, true) => Ok(SortedSlice(self)), | ||
| (true, false) => Ok(SortedSlice(self)), | ||
| (false, false) => Err(SortError::NotSorted), | ||
| } | ||
| } | ||
| } | ||
|
|
||
| impl<'a, T, I> Index<I> for SortedSlice<'a, T> | ||
| where | ||
| T: Ord, | ||
| I: SliceIndex<[T]>, | ||
| { | ||
| type Output = <I as SliceIndex<[T]>>::Output; | ||
|
|
||
| fn index(&self, index: I) -> &Self::Output { | ||
| self.0.index(index) | ||
| } | ||
| } | ||
| impl<'a, T> Deref for SortedSlice<'a, T> | ||
| where | ||
| T: Ord, | ||
| { | ||
| type Target = [T]; | ||
|
|
||
| fn deref(&self) -> &Self::Target { | ||
| self.0 | ||
| } | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🎯 Functional Correctness | 🟡 Minor | ⚡ Quick win
🔎 Supported by static analysis
🏁 Script executed:
Repository: statrs-dev/statrs
Length of output: 402
🏁 Script executed:
Repository: statrs-dev/statrs
Length of output: 50376
🌐 Web query:
Rust 1.89 std Result unwrap expect requires E Debug official documentation💡 Result:
In Rust 1.89, the std::result::Result::unwrap and std::result::Result::expect methods do indeed require the error type E to implement the std::fmt::Debug trait [1]. This requirement exists because both methods are designed to panic if the Result contains an Err variant [1]. When they panic, the methods include the value of the Err variant in the panic message to assist with debugging [1]. Consequently, the type E must implement Debug so that it can be formatted and displayed in the error message [1]. The function signatures in the official documentation for Rust 1.89 reflect this constraint [1]: pub fn expect(self, msg: &str) -> T where E: Debug pub fn unwrap(self) -> T where E: Debug Conversely, methods like Result::expect_err and Result::unwrap_err, which extract the error value when the result is an Err, require the success type T to implement the Debug trait instead, as they must format the Ok value if a panic occurs [1].
Citations:
Derive
DebugforSortError.SortedCollection::newreturnsResult<Self, SortError>. Itsunwrapandexpectmethods requireSortError: Debug. Add#[derive(Debug)].🤖 Prompt for AI Agents