首页 > 解决方案 > 为稀疏矩阵类编写 C++ 迭代器

问题描述

我试图让一个基本的常量前向迭代器在 C++ 中工作。

namespace Rcpp {
    class SparseMatrix {
    public:
        IntegerVector i, p;
        NumericVector x;
   
        int begin_col(int j) { return p[j]; };
        int end_col(int j) { return p[j + 1]; };
        
        class iterator {
        public:
            int index;
            iterator(SparseMatrix& g) : parent(g) {}
            iterator(int ind) { index = ind; };                       // ERROR!
            bool operator!=(int x) const { return index != x; };
            iterator operator++(int) { ++index; return (*this); };
            int row() { return parent.i[index]; };
            double value() { return parent.x[index]; };
        private:
            SparseMatrix& parent;
        };
    };    
}

我的意图是在类似于以下的上下文中使用迭代器:

// sum of values in column 7
Rcpp::SparseMatrix A(nrow, ncol, fill::random);
double sum = 0;
for(Rcpp::SparseMatrix::iterator it = A.begin_col(7); it != A.end_col(7); it++)
    sum += it.value();

两个问题:

  1. 编译器在上面指出的行抛出错误:uninitialized reference member in 'class Rcpp::SparseMatrix&' [-fpermissive]. 如何解决这个问题?
  2. 如何double value() { return parent.x[index]; };重新工作以返回指向该值的指针而不是该值的副本?

关于SparseMatrix类的一点上下文:就像dgCMatrixR 中的 a 一样,这个类的对象SparseMatrix由三个向量组成:

标签: c++iteratorsparse-matrixconst-iterator

解决方案


感谢@Evg,这是解决方案:

namespace Rcpp {
    class SparseMatrix {
    public:
        IntegerVector i, p;
        NumericVector x;
   
        class iterator {
        public:
            int index;
            iterator(SparseMatrix& g, int ind) : parent(g) { index = ind; }
            bool operator!=(iterator x) const { return index != x.index; };
            iterator& operator++() { ++index; return (*this); };
            int row() { return parent.i[index]; };
            double& value() { return parent.x[index]; };
        private:
            SparseMatrix& parent;
        };

        iterator begin_col(int j) { return iterator(*this, p[j]); };
        iterator end_col(int j) { return iterator(*this, p[j + 1]); };
    };    
}

例如,它可以用于计算 colSums:

//[[Rcpp::export]]
Rcpp::NumericVector Rcpp_colSums(Rcpp::SparseMatrix& A) {
    Rcpp::NumericVector sums(A.cols());
    for (int i = 0; i < A.cols(); ++i)
        for (Rcpp::SparseMatrix::iterator it = A.begin_col(i); it != A.end_col(i); it++)
            sums(i) += it.value();
    return sums;
}

而且,当从 R 进行微基准测试时,上述函数比RcppArmadilloRcppEigen和等效函数更快!R::Matrix

编辑:

上述语法的灵感来自犰狳。我开始意识到稍微不同的语法(涉及更少的结构)给出了一个类似于 Eigen 的迭代器:

class col_iterator {
    public:
      col_iterator(SparseMatrix& ptr, int col) : ptr(ptr) { indx = ptr.p[col]; max_index = ptr.p[col + 1]; }
      operator bool() const { return (indx != max_index); }
      col_iterator& operator++() { ++indx; return *this; }
      const double& value() const { return ptr.x[indx]; }
      int row() const { return ptr.i[indx]; }
    private:
      SparseMatrix& ptr;
      int indx, max_index;
    };

然后可以这样使用:

int col = 0;
for (Rcpp::SparseMatrix::col_iterator it(A, col); it; ++it)
     Rprintf("row: %3d, value: %10.2e", it.row(), it.value());

推荐阅读