-
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
Open
AshrafIbrahim03
wants to merge
6
commits into
statrs-dev:main
Choose a base branch
from
AshrafIbrahim03:main
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 4 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
c1b486c
Added sorted iterator
AshrafIbrahim03 e24648c
Added sorted collection validation
AshrafIbrahim03 4f9b161
Added iterator function to SortedCollection
AshrafIbrahim03 80497a3
Merge branch 'main' of github.com:statrs-dev/statrs
AshrafIbrahim03 68c13e1
Added convenience methods for instantiating SortedCollection
AshrafIbrahim03 b44e42c
Added convenience derives and conversion functions
AshrafIbrahim03 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| pub enum SortError { | ||
| NotSorted, | ||
| } | ||
|
|
||
| pub struct SortedCollection<'a, T> { | ||
| sorted: &'a [T], | ||
| } | ||
|
|
||
| impl<'a, T> SortedCollection<'a, T> | ||
| where | ||
| T: PartialOrd, | ||
| { | ||
| pub fn new(coll: &'a [T]) -> Result<Self, SortError> { | ||
| match coll.windows(2).all(|sl| sl[0] < sl[1]) { | ||
| true => Ok(Self { sorted: coll }), | ||
| false => Err(SortError::NotSorted), | ||
| } | ||
| } | ||
| pub fn iter(&self) -> std::slice::Iter<'a, T> { | ||
| self.sorted.iter() | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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() | ||
| } | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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