mirror of
https://github.com/drewcassidy/vector-victor.git
synced 2024-09-01 14:58:35 +00:00
Rename Index2D to MatrixIndex to match SliceIndex
Hopefully less confusing
This commit is contained in:
parent
ae1c3a9e4b
commit
8e498f661e
10
src/index.rs
10
src/index.rs
@ -34,7 +34,7 @@ assert_eq!(m[(0,0)], 1);
|
|||||||
assert_eq!(m[(1,1)], 5);
|
assert_eq!(m[(1,1)], 5);
|
||||||
assert_eq!(m[(2,1)], 8);
|
assert_eq!(m[(2,1)], 8);
|
||||||
``` */
|
``` */
|
||||||
pub trait Index2D: Copy + Debug {
|
pub trait MatrixIndex: Copy + Debug {
|
||||||
/** Convert an index to its 1-D linear interpretation, given the `width` and `height` of the
|
/** Convert an index to its 1-D linear interpretation, given the `width` and `height` of the
|
||||||
matrix being subscripted.
|
matrix being subscripted.
|
||||||
|
|
||||||
@ -43,7 +43,7 @@ pub trait Index2D: Copy + Debug {
|
|||||||
|
|
||||||
# Examples
|
# Examples
|
||||||
```
|
```
|
||||||
# use vector_victor::index::Index2D;
|
# use vector_victor::index::MatrixIndex;
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
(1usize,2usize).to_1d(3,3),
|
(1usize,2usize).to_1d(3,3),
|
||||||
Some(5),
|
Some(5),
|
||||||
@ -67,7 +67,7 @@ pub trait Index2D: Copy + Debug {
|
|||||||
|
|
||||||
# Examples
|
# Examples
|
||||||
```
|
```
|
||||||
# use vector_victor::index::Index2D;
|
# use vector_victor::index::MatrixIndex;
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
5usize.to_2d(3,3),
|
5usize.to_2d(3,3),
|
||||||
Some((1usize,2usize)),
|
Some((1usize,2usize)),
|
||||||
@ -80,7 +80,7 @@ pub trait Index2D: Copy + Debug {
|
|||||||
fn to_2d(self, height: usize, width: usize) -> Option<(usize, usize)>;
|
fn to_2d(self, height: usize, width: usize) -> Option<(usize, usize)>;
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Index2D for usize {
|
impl MatrixIndex for usize {
|
||||||
#[inline(always)]
|
#[inline(always)]
|
||||||
fn to_2d(self, height: usize, width: usize) -> Option<(usize, usize)> {
|
fn to_2d(self, height: usize, width: usize) -> Option<(usize, usize)> {
|
||||||
match self < (height * width) {
|
match self < (height * width) {
|
||||||
@ -90,7 +90,7 @@ impl Index2D for usize {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Index2D for (usize, usize) {
|
impl MatrixIndex for (usize, usize) {
|
||||||
#[inline(always)]
|
#[inline(always)]
|
||||||
fn to_2d(self, height: usize, width: usize) -> Option<(usize, usize)> {
|
fn to_2d(self, height: usize, width: usize) -> Option<(usize, usize)> {
|
||||||
match self.0 < height && self.1 < width {
|
match self.0 < height && self.1 < width {
|
||||||
|
14
src/lib.rs
14
src/lib.rs
@ -4,7 +4,7 @@
|
|||||||
|
|
||||||
extern crate core;
|
extern crate core;
|
||||||
|
|
||||||
use index::Index2D;
|
use index::MatrixIndex;
|
||||||
use num_traits::{Bounded, One, Zero};
|
use num_traits::{Bounded, One, Zero};
|
||||||
use std::cmp::min;
|
use std::cmp::min;
|
||||||
use std::fmt::Debug;
|
use std::fmt::Debug;
|
||||||
@ -314,7 +314,7 @@ impl<T: Copy, const M: usize, const N: usize> Matrix<T, M, N> {
|
|||||||
|
|
||||||
# Arguments
|
# Arguments
|
||||||
|
|
||||||
* `index`: a 1D or 2D index into the matrix. See [Index2D] for more information on matrix indexing.
|
* `index`: a 1D or 2D index into the matrix. See [MatrixIndex] for more information on matrix indexing.
|
||||||
|
|
||||||
# Examples
|
# Examples
|
||||||
|
|
||||||
@ -335,7 +335,7 @@ impl<T: Copy, const M: usize, const N: usize> Matrix<T, M, N> {
|
|||||||
``` */
|
``` */
|
||||||
#[inline]
|
#[inline]
|
||||||
#[must_use]
|
#[must_use]
|
||||||
pub fn get(&self, index: impl Index2D) -> Option<&T> {
|
pub fn get(&self, index: impl MatrixIndex) -> Option<&T> {
|
||||||
let (m, n) = index.to_2d(M, N)?;
|
let (m, n) = index.to_2d(M, N)?;
|
||||||
Some(&self.data[m][n])
|
Some(&self.data[m][n])
|
||||||
}
|
}
|
||||||
@ -348,7 +348,7 @@ impl<T: Copy, const M: usize, const N: usize> Matrix<T, M, N> {
|
|||||||
|
|
||||||
# Arguments
|
# Arguments
|
||||||
|
|
||||||
* `index`: a 1D or 2D index into the matrix. See [Index2D] for more information
|
* `index`: a 1D or 2D index into the matrix. See [MatrixIndex] for more information
|
||||||
on matrix indexing.
|
on matrix indexing.
|
||||||
|
|
||||||
# Examples
|
# Examples
|
||||||
@ -365,7 +365,7 @@ impl<T: Copy, const M: usize, const N: usize> Matrix<T, M, N> {
|
|||||||
``` */
|
``` */
|
||||||
#[inline]
|
#[inline]
|
||||||
#[must_use]
|
#[must_use]
|
||||||
pub fn get_mut(&mut self, index: impl Index2D) -> Option<&mut T> {
|
pub fn get_mut(&mut self, index: impl MatrixIndex) -> Option<&mut T> {
|
||||||
let (m, n) = index.to_2d(M, N)?;
|
let (m, n) = index.to_2d(M, N)?;
|
||||||
Some(&mut self.data[m][n])
|
Some(&mut self.data[m][n])
|
||||||
}
|
}
|
||||||
@ -602,7 +602,7 @@ impl<T: Copy, const M: usize, const N: usize> Matrix<T, M, N> {
|
|||||||
// Index
|
// Index
|
||||||
impl<I, T, const M: usize, const N: usize> Index<I> for Matrix<T, M, N>
|
impl<I, T, const M: usize, const N: usize> Index<I> for Matrix<T, M, N>
|
||||||
where
|
where
|
||||||
I: Index2D,
|
I: MatrixIndex,
|
||||||
T: Copy,
|
T: Copy,
|
||||||
{
|
{
|
||||||
type Output = T;
|
type Output = T;
|
||||||
@ -619,7 +619,7 @@ where
|
|||||||
// IndexMut
|
// IndexMut
|
||||||
impl<I, T, const M: usize, const N: usize> IndexMut<I> for Matrix<T, M, N>
|
impl<I, T, const M: usize, const N: usize> IndexMut<I> for Matrix<T, M, N>
|
||||||
where
|
where
|
||||||
I: Index2D,
|
I: MatrixIndex,
|
||||||
T: Copy,
|
T: Copy,
|
||||||
{
|
{
|
||||||
#[inline(always)]
|
#[inline(always)]
|
||||||
|
Loading…
Reference in New Issue
Block a user