Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/rust/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Struct 多参数矩阵索引算子的实现_Struct_Rust - Fatal编程技术网

Struct 多参数矩阵索引算子的实现

Struct 多参数矩阵索引算子的实现,struct,rust,Struct,Rust,我正在尝试创建一个矩阵结构,我想覆盖索引操作符,以便使用矩阵样式的索引 例如: let m = Matrix { ... DATA ... } let entry = m[0,0] 我的结构如下所示: struct Matrix { cols: usize, rows: usize, data: Vec<f32> } 结构矩阵{ 科尔斯:使用, 行:usize, 数据:Vec } 我一直在看电影,但我不知道我如何才能做到这一点?此外,我还希望能够在每个维

我正在尝试创建一个
矩阵
结构,我想覆盖
索引
操作符,以便使用矩阵样式的索引

例如:

let m = Matrix { ... DATA ... }
let entry = m[0,0]
我的结构如下所示:

struct Matrix {
    cols: usize,
    rows: usize,
    data: Vec<f32>
}
结构矩阵{ 科尔斯:使用, 行:usize, 数据:Vec }
我一直在看电影,但我不知道我如何才能做到这一点?此外,我还希望能够在每个维度上获取范围等。简而言之,您不能这样做。定义如下:

pub trait Index<Idx: ?Sized> {
    type Output: ?Sized;
    fn index(&self, index: Idx) -> &Self::Output;
}
它会被称为

matrix[(0, 1)]
使用两个元素数组而不是元组。这可能更容易键入,因为只需按两次方括号即可:

impl std::ops::Index<[usize; 2]> for Matrix {
    type Output = f32;

    fn index(&self, idx: [usize; 2]) -> &f32 {
        // or as appropriate for row- or column-major data       
        &self.data[idx[0] * self.cols + idx[1]]
    }
}
impl std::ops::矩阵索引{
类型输出=f32;
fn索引(&self,idx:[usize;2])->&f32{
//或适用于行或列主数据
&self.data[idx[0]*self.cols+idx[1]]
}
}
它被称为矩阵[[0,1]]。重要的是,仍然只有一个值作为
索引的参数提供


根据需要对、和重复实施。这些都是单一类型,因此您可以像
matrix[5..]
那样调用它,不管这意味着什么。

另一种可能是使用2D数组样式索引,如
m[0][1]
。这绝对是可能的——在你的情况下甚至很容易。您的
索引
实现只需返回可再次索引的内容即可。代码:

use std::ops::Index;

struct Matrix {
    cols: usize,
    rows: usize,
    data: Vec<f32>
}

impl Index<usize> for Matrix {
    type Output = [f32];
    fn index(&self, index: usize) -> &Self::Output {
        &self.data[index * self.cols .. (index+1) * self.cols]
    }
}

fn main() {
    let m = Matrix {
        cols: 2,
        rows: 2,
        data: vec![1., 2., 3., 4.],
    };

    println!("{} {}", m[0][0], m[0][1]);
    println!("{} {}", m[1][0], m[1][1]);
}
使用std::ops::Index;
结构矩阵{
科尔斯:使用,
行:usize,
数据:Vec
}
矩阵的impl索引{
类型输出=[f32];
fn索引(&self,索引:usize)->&self::输出{
&self.data[index*self.cols..(index+1)*self.cols]
}
}
fn main(){
设m=矩阵{
科尔斯:2,
行:2,
数据:vec![1,2,3,4],
};
println!(“{}{}”,m[0][0],m[0][1]);
println!(“{}{}”,m[1][0],m[1][1]);
}

这种风格在Java和C等语言中更为常见。

使用数组而不是元组是我从中看到的一个很好的技巧<代码>矩阵[[0,1]]
看起来不错,而且更容易键入。@bluss-mmm,确实很整洁!你想让我把它添加到这里,还是你想有一个单独的答案?这里很好,我认为这只是一个小细节。@bluss:我想在那里做元组而不是数组。可能是我在那里的Python背景,其中
foo[bar,baz]
foo.\uuu getitem\uuuu((bar,baz))
。(Python对元组做了很多允许省略括号的事情,例如,
foo,bar=baz
foo=bar,baz
。Rust至少目前没有做任何事情。)@ChrisMorgan:数组作为索引的好处是,您可以依赖
[usize;N]
在通用代码中实现
AsRef
,以更轻松地支持多个维度。我认为,如果你想使用元组,你必须对维度做更多的特殊大小写。当然,如果您仅限于二维,这并没有什么区别。但是,在不同数量的维度之间保持一致会很好。(顺便说一句,我是《多阵列板条箱》的作者)这是一个怎样的矩阵?矩阵将有一个向量,而不仅仅是一个向量。
use std::ops::Index;

struct Matrix {
    cols: usize,
    rows: usize,
    data: Vec<f32>
}

impl Index<usize> for Matrix {
    type Output = [f32];
    fn index(&self, index: usize) -> &Self::Output {
        &self.data[index * self.cols .. (index+1) * self.cols]
    }
}

fn main() {
    let m = Matrix {
        cols: 2,
        rows: 2,
        data: vec![1., 2., 3., 4.],
    };

    println!("{} {}", m[0][0], m[0][1]);
    println!("{} {}", m[1][0], m[1][1]);
}