代码之家  ›  专栏  ›  技术社区  ›  Stéphane Laurent

如何防止Rcpp更改R中的某些值?

  •  -1
  • Stéphane Laurent  · 技术社区  · 4 年前

    我有这个C++函数:

    std::vector<std::vector<size_t>> list_to_faces(const Rcpp::List L) {
      const size_t nfaces = L.size();
      std::vector<std::vector<size_t>> faces;
      faces.reserve(nfaces);
      for(size_t i = 0; i < nfaces; i++) {
        Rcpp::IntegerVector face_rcpp = Rcpp::as<Rcpp::IntegerVector>(L(i));
        face_rcpp = face_rcpp - 1;
        std::vector<size_t> face(face_rcpp.begin(), face_rcpp.end());
        faces.emplace_back(face);
      }
      return faces;
    }
    

    正如您所看到的,它以列表作为参数。在我的代码中,这是一个整数向量列表,函数将该列表转换为另一个对象, 在列表的每个向量减去1之后

    此R命令:

    mesh <- Mesh(vertices, faces, normals = FALSE)
    

    调用上面的C++函数,其中 faces 是有问题的列表。最初,这是以下列表:

    > faces
    [[1]]
    [1] 1 2 3 4 5
    
    [[2]]
    [1]  6  7  8  9 10
    
    [[3]]
    [1] 1 2 7 6
    
    [[4]]
    [1] 2 3 8 7
    
    [[5]]
    [1] 3 4 9 8
    
    [[6]]
    [1]  4  5 10  9
    
    [[7]]
    [1]  5  1  6 10
    

    但在我执行R命令后,列表变为:

    > faces
    [[1]]
    [1] 0 1 2 3 4
    
    [[2]]
    [1] 5 6 7 8 9
    
    [[3]]
    [1] 0 1 6 5
    
    [[4]]
    [1] 1 2 7 6
    
    [[5]]
    [1] 2 3 8 7
    
    [[6]]
    [1] 3 4 9 8
    
    [[7]]
    [1] 4 0 5 9
    

    也就是说,C++函数减去1的动作被传播到R。我该如何防止这种情况发生?

    0 回复  |  直到 4 年前