试图收集到载体,但未能与“收集不能超过类型的元素来构建”

问题描述 投票:0回答:1

我试图找到一个矩阵,向量的借用阵列的鞍点。对于这个目的,一个鞍点是,或者是在其列中的最小的,并在其行最大的,或在其列中的最大,并在其行中的最小的矩阵的元素。

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功能,但对我来说,应该是正确的,而我不是,为什么现在还不清楚。

rust rayon
1个回答
0
投票

The chat transcript将引导您完成这个问题的解决方案,因为它是一种复杂的)

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)>>()

请注意,此代码是非常unidiomatic,但显示的解决方案。

© www.soinside.com 2019 - 2024. All rights reserved.