-
Notifications
You must be signed in to change notification settings - Fork 120
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
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,76 @@ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| use std::cmp::Ordering; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+22
to
+38
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 Accept equal adjacent values, or enforce strict ordering in both constructors.
Proposed fix- .all(|sl| sl[0].cmp(&sl[1]) == Ordering::Less)
+ .all(|sl| sl[0].cmp(&sl[1]) != Ordering::Greater)📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
+39
to
+43
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(), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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() | ||
| } | ||
| } |
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