-
Notifications
You must be signed in to change notification settings - Fork 2
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
Added into_vec method for Matrix #3
base: dev
Are you sure you want to change the base?
Changes from 4 commits
13f44d2
b3a17e7
da1bc08
681a769
694f259
4319153
65528a8
c57c75a
0c82d17
3bf0aff
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 | ||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
@@ -2357,3 +2357,27 @@ impl<T> super::alias::Matrix1<T> { | |||||||||
scalar | ||||||||||
} | ||||||||||
} | ||||||||||
|
||||||||||
alexandruradovici marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||
/// Provides methods for transforming a matrix into a vector with different algorithms | ||||||||||
impl<T, R: Dim, C: Dim, S: Storage<T, R, C>> Matrix<T, R, C, S> | ||||||||||
where | ||||||||||
T: Clone, | ||||||||||
{ | ||||||||||
/// Converts matrix into a vector by concatenating rows | ||||||||||
pub fn into_vec(&self) -> Vec<T> { | ||||||||||
let (num_rows, num_columns) = self.shape(); | ||||||||||
let mut resulted_vector = Vec::with_capacity(num_rows * num_columns); | ||||||||||
|
||||||||||
for i in 0..num_rows { | ||||||||||
for j in 0..num_columns { | ||||||||||
unsafe { | ||||||||||
resulted_vector.push(self.get_unchecked((i, j)).clone()); | ||||||||||
} | ||||||||||
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.
Suggested change
Please explain why did you use 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. As the method suggests (get_unchecked), there is no warranty that (i, j) are in bounds, that the operation does not fail or that the operation is memory safe. Now, the methods called by resulted_vector are not unsafe. If the resulted_vector is immutable, then it will result in compilation error, not that the operation is not memory safe. If the push exceeds vector capacity, then Rust is designed to reallocate space. I will modify. 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. I observed that was the way transpose method access the elements of a matrix in matrix.rs so I wanted to maintain the format. 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. Please add a comment in the code explaining why this is used and why it is safe. 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. I've added two comments that should clarify the problem |
||||||||||
} | ||||||||||
} | ||||||||||
|
||||||||||
return resulted_vector; | ||||||||||
alexandruradovici marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||
} | ||||||||||
} | ||||||||||
|
||||||||||
|
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.
Contribution starts here