繁体   English   中英

尝试收集到向量中,但失败了,“无法在类型<…>的元素上建立收集”

[英]Trying to collect into vector but failing with “a collection cannot be built over elements of type <…>”

我试图找到矩阵的鞍点,借来的向量数组。 为此,鞍点是矩阵中的元素,该元素在其列中最小,在其行中最大,或者在其列中最大,在其行中最小。

use rayon::prelude::*;

pub fn find_saddle_points(input: &[Vec<u64>]) -> Vec<(usize, usize)> {
    let flattened_matrix: Vec<(&u64)> = input.into_par_iter().flatten().collect::<Vec<&u64>>();
    match flattened_matrix == vec![] { //Check for empty input
        true => vec![],
        false => {
            let num_rows: usize = input.len();
            let num_cols: usize = input[0].len();

            let row_minima: Vec<Option<&u64>> = (0_usize..(num_rows - 1_usize))
                .into_par_iter()
                .map(|row_index| input[row_index].iter().min())
                .collect::<Vec<Option<&u64>>>();

            let row_maxima: Vec<Option<&u64>> = (0_usize..(num_rows - 1_usize))
                .into_par_iter()
                .map(|row_index| input[row_index].iter().max())
                .collect::<Vec<Option<&u64>>>();

            let input_tranpose: Vec<Vec<u64>> = (0_usize..(num_cols - 1_usize)) //Transpose the matrix to make it easier to get column information
                .into_par_iter()
                .map(|col_index| {
                    (0_usize..(num_rows - 1_usize))
                        .map(|row_index| input[row_index][col_index])
                        .collect::<Vec<u64>>() //Individual column
                })
                .collect::<Vec<Vec<u64>>>();

            let col_minima: Vec<Option<&u64>> = (0_usize..(num_cols - 1_usize))
                .into_par_iter()
                .map(|col_index| input_tranpose[col_index].iter().min())
                .collect::<Vec<Option<&u64>>>();

            let col_maxima: Vec<Option<&u64>> = (0_usize..(num_cols - 1_usize))
                .into_par_iter()
                .map(|col_index| input_tranpose[col_index].iter().max())
                .collect::<Vec<Option<&u64>>>();

            //All fine up to this point

            (0_usize..(num_rows - 1_usize))
                .map(|row_index| {
                    (0_usize..(num_cols - 1_usize)).map(|col_index| {
                        match (Some(row_minima[row_index]) == Some(col_maxima[col_index])
                            || Some(row_maxima[row_index]) == Some(col_minima[col_index]))
                        {
                            true => Some((row_index, col_index)),
                            false => None,
                        }
                    })
                })
                .collect::<Vec<(usize, usize)>>()
        }
    }
}

错误在最终的.collect

error[E0277]: a collection of type `std::vec::Vec<(usize, usize)>` cannot be built from an iterator over elements of type `std::iter::Map<std::ops::Range<usize>, [closure@src/lib.rs:45:57: 52:22 row_minima:_, row_index:_, col_maxima:_, row_maxima:_, col_minima:_]>`
  --> src/lib.rs:54:18
   |
54 |                 .collect::<Vec<(usize, usize)>>()
   |                  ^^^^^^^ a collection of type `std::vec::Vec<(usize, usize)>` cannot be built from `std::iter::Iterator<Item=std::iter::Map<std::ops::Range<usize>, [closure@src/lib.rs:45:57: 52:22 row_minima:_, row_index:_, col_maxima:_, row_maxima:_, col_minima:_]>>`
   |
   = help: the trait `std::iter::FromIterator<std::iter::Map<std::ops::Range<usize>, [closure@src/lib.rs:45:57: 52:22 row_minima:_, row_index:_, col_maxima:_, row_maxima:_, col_minima:_]>>` is not implemented for `std::vec::Vec<(usize, usize)>`

我知道这是由于迭代器受到了不属于期望类型的collect函数引起的,但是对我来说这应该是正确的,并且我不清楚为什么不是这样。

聊天记录会指导您完成该问题的解决方案,因为它很复杂)

TL; DR使用Iterator::flat_mapIterator::filter_map来解决此问题。

(0_usize..(num_rows - 1_usize))
    .flat_map(|row_index| {
        (0_usize..(num_cols - 1_usize)).flat_map(|col_index| {
            match row_minima[row_index] == col_maxima[col_index]
                || row_maxima[row_index] == col_minima[col_index]
            {
                true => Some((row_index, col_index)),
                false => None,
            }
        })
    })
    .collect::<Vec<(usize, usize)>>()

请注意,该代码非常简单,但是显示了解决方案。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM