c++ - 为稀疏矩阵类编写 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();
两个问题:
- 编译器在上面指出的行抛出错误:
uninitialized reference member in 'class Rcpp::SparseMatrix&' [-fpermissive]
. 如何解决这个问题? - 如何
double value() { return parent.x[index]; };
重新工作以返回指向该值的指针而不是该值的副本?
关于SparseMatrix
类的一点上下文:就像dgCMatrix
R 中的 a 一样,这个类的对象SparseMatrix
由三个向量组成:
i
保存每个元素的行指针x
p
i
给出对应于每列开始的索引x
包含非零值
解决方案
感谢@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 进行微基准测试时,上述函数比RcppArmadillo
、RcppEigen
和等效函数更快!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());
推荐阅读
- mysql - MariaDB/MySQL 中的 CTE 中类似 PostgreSQL 的返回修改语句管道/使用
- javascript - 使用 vue.js 创建交互式 svg 地图
- elasticsearch - 在索引的文本字段中查找最常见的术语
- ionic-framework - 离子电容器 WebView:不接受来自根域的 Cookie
- c# - 使用高级 Ocelot 请求聚合时,聚合器得到空响应
- virtualization - 如何将 qm config 变成 qm create
- python - 无法使用 nginx 和 uwsgi 在服务器上运行 dash 应用程序
- nest-device-access - 什么 RTSP/RTSPS 客户端
- c# - Realm + RaisePropertyChanged 没有为带有代码的属性引发
- cplex - 如何解决 cplex 中的未知属性错误?