-
Notifications
You must be signed in to change notification settings - Fork 47
More ergonomic slicing #252
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
Merged
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
ebca1cd
Add CsMatBase::view_mut
vbarrielle 312a090
Add sealed trait to abstract over Range types
vbarrielle d46c5a9
Change IndPtr::middle_slice to use the Range trait
vbarrielle 45a2bff
Add ergonomic middle_slice functions for `CsMatBase` variants
vbarrielle 8de1c4e
Deprecate middle_outer_views and replace its uses in the crate
vbarrielle 3ee7006
Fix clippy warnings about unwrap_or followed by function call
vbarrielle cd3b208
Implement Range for RangeTo{,Inclusive}
vbarrielle 1a8589b
Return usize in Range::start()
vbarrielle 30af437
Refactor to get IndPtrBase::middle_slice, IndPtrView::middle_slice_rbr
vbarrielle 89d0e39
Refactor to expose `slice_outer` on `CsMatBase`
vbarrielle 63d2efa
Detail the reborrowing policy in the guidelines
vbarrielle 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
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
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,70 @@ | ||
//! This module abstracts over ranges to allow functions in the crate to take | ||
//! ranges as input in an ergonomic way. It howvers seals this abstractions to | ||
//! leave full control in this crate. | ||
|
||
/// Abstract over `std::ops::{Range,RangeFrom,RangeTo,RangeFull}` | ||
pub trait Range { | ||
fn start(&self) -> usize; | ||
|
||
fn end(&self) -> Option<usize>; | ||
} | ||
|
||
impl Range for std::ops::Range<usize> { | ||
fn start(&self) -> usize { | ||
self.start | ||
} | ||
|
||
fn end(&self) -> Option<usize> { | ||
Some(self.end) | ||
} | ||
} | ||
|
||
impl Range for std::ops::RangeFrom<usize> { | ||
fn start(&self) -> usize { | ||
self.start | ||
} | ||
|
||
fn end(&self) -> Option<usize> { | ||
None | ||
} | ||
} | ||
|
||
impl Range for std::ops::RangeTo<usize> { | ||
fn start(&self) -> usize { | ||
0 | ||
} | ||
|
||
fn end(&self) -> Option<usize> { | ||
Some(self.end) | ||
} | ||
} | ||
|
||
impl Range for std::ops::RangeFull { | ||
fn start(&self) -> usize { | ||
0 | ||
} | ||
|
||
fn end(&self) -> Option<usize> { | ||
None | ||
} | ||
} | ||
|
||
impl Range for std::ops::RangeInclusive<usize> { | ||
fn start(&self) -> usize { | ||
*self.start() | ||
} | ||
|
||
fn end(&self) -> Option<usize> { | ||
Some(*self.end() + 1) | ||
} | ||
} | ||
|
||
impl Range for std::ops::RangeToInclusive<usize> { | ||
fn start(&self) -> usize { | ||
0 | ||
} | ||
|
||
fn end(&self) -> Option<usize> { | ||
Some(self.end + 1) | ||
} | ||
} |
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
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,108 @@ | ||
//! This module implementations to slice a matrix along the desired dimension. | ||
//! We're using a sealed trait to enable using ranges for an idiomatic API. | ||
|
||
use crate::range::Range; | ||
use crate::{CsMatBase, CsMatViewI, CsMatViewMutI, SpIndex}; | ||
use std::ops::{Deref, DerefMut}; | ||
|
||
impl<N, I: SpIndex, Iptr: SpIndex, IptrStorage, IStorage, DStorage> | ||
CsMatBase<N, I, IptrStorage, IStorage, DStorage, Iptr> | ||
where | ||
IptrStorage: Deref<Target = [Iptr]>, | ||
IStorage: Deref<Target = [I]>, | ||
DStorage: Deref<Target = [N]>, | ||
{ | ||
/// Slice the outer dimension of the matrix according to the specified | ||
/// range. | ||
pub fn slice_outer<S: Range>(&self, range: S) -> CsMatViewI<N, I, Iptr> { | ||
self.view().slice_outer_rbr(range) | ||
} | ||
} | ||
|
||
impl<N, I: SpIndex, Iptr: SpIndex, IptrStorage, IStorage, DStorage> | ||
CsMatBase<N, I, IptrStorage, IStorage, DStorage, Iptr> | ||
where | ||
IptrStorage: Deref<Target = [Iptr]>, | ||
IStorage: Deref<Target = [I]>, | ||
DStorage: DerefMut<Target = [N]>, | ||
{ | ||
/// Slice the outer dimension of the matrix according to the specified | ||
/// range. | ||
pub fn slice_outer_mut<S: Range>( | ||
&mut self, | ||
range: S, | ||
) -> CsMatViewMutI<N, I, Iptr> { | ||
let start = range.start(); | ||
let end = range.end().unwrap_or_else(|| self.outer_dims()); | ||
if end < start { | ||
panic!("Invalid view"); | ||
} | ||
let outer_inds_slice = self.indptr.outer_inds_slice(start, end); | ||
let (nrows, ncols) = match self.storage() { | ||
crate::CSR => ((end - start), self.ncols), | ||
crate::CSC => (self.nrows, (end - start)), | ||
}; | ||
CsMatViewMutI { | ||
nrows, | ||
ncols, | ||
storage: self.storage, | ||
indptr: self.indptr.middle_slice(range), | ||
indices: &self.indices[outer_inds_slice.clone()], | ||
data: &mut self.data[outer_inds_slice], | ||
} | ||
} | ||
} | ||
|
||
impl<'a, N, I, Iptr> crate::CsMatViewI<'a, N, I, Iptr> | ||
where | ||
I: crate::SpIndex, | ||
Iptr: crate::SpIndex, | ||
{ | ||
/// Slice the outer dimension of the matrix according to the specified | ||
/// range. | ||
pub fn slice_outer_rbr<S>( | ||
&self, | ||
range: S, | ||
) -> crate::CsMatViewI<'a, N, I, Iptr> | ||
where | ||
S: Range, | ||
{ | ||
let start = range.start(); | ||
let end = range.end().unwrap_or_else(|| self.outer_dims()); | ||
if end < start { | ||
panic!("Invalid view"); | ||
} | ||
let outer_inds_slice = self.indptr.outer_inds_slice(start, end); | ||
let (nrows, ncols) = match self.storage() { | ||
crate::CSR => ((end - start), self.ncols), | ||
crate::CSC => (self.nrows, (end - start)), | ||
}; | ||
crate::CsMatViewI { | ||
nrows, | ||
ncols, | ||
storage: self.storage, | ||
indptr: self.indptr.middle_slice_rbr(range), | ||
indices: &self.indices[outer_inds_slice.clone()], | ||
data: &self.data[outer_inds_slice], | ||
} | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use crate::CsMat; | ||
|
||
#[test] | ||
fn slice_outer() { | ||
let size = 11; | ||
let csr: CsMat<f64> = CsMat::eye(size); | ||
let sliced = csr.slice_outer(2..7); | ||
let mut iter = sliced.into_iter(); | ||
assert_eq!(iter.next().unwrap(), (&1., (0, 2))); | ||
assert_eq!(iter.next().unwrap(), (&1., (1, 3))); | ||
assert_eq!(iter.next().unwrap(), (&1., (2, 4))); | ||
assert_eq!(iter.next().unwrap(), (&1., (3, 5))); | ||
assert_eq!(iter.next().unwrap(), (&1., (4, 6))); | ||
assert!(iter.next().is_none()); | ||
} | ||
} |
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
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.