33
44namespace Rcpp {
55
6+ template <int RTYPE, typename Mat>
7+ class MatrixColumn : public VectorBase <RTYPE,true ,MatrixColumn<RTYPE,Mat>> {
8+ public:
9+ using iterator = typename Mat::iterator ;
10+ using Proxy = typename Mat::Proxy ;
11+
12+ MatrixColumn ( Mat& mat, int i) : start(mat.begin() + i * mat.nrow()), n(mat.nrow()){}
13+
14+ inline int size () const { return n ;}
15+ inline Proxy operator []( int i){ return *(start+i) ; }
16+
17+ private:
18+ typename Mat::iterator start ;
19+ int n ;
20+ };
21+
22+ template <int RTYPE, typename Mat>
23+ class const_MatrixColumn : public VectorBase <RTYPE,true ,MatrixColumn<RTYPE,Mat>> {
24+ public:
25+ using const_iterator = typename Mat::const_iterator ;
26+ using const_Proxy = typename Mat::const_Proxy ;
27+
28+ const_MatrixColumn ( const Mat& mat, int i) : start(mat.begin() + i * mat.nrow()), n(mat.nrow()){}
29+
30+ inline int size () const { return n ;}
31+ inline const_Proxy operator []( int i){ return *(start+i) ; }
32+
33+ private:
34+ typename Mat::const_iterator start ;
35+ int n ;
36+ };
37+
38+
39+ template <int RTYPE, typename Mat>
40+ class MatrixRow : public VectorBase <RTYPE,true ,MatrixRow<RTYPE,Mat>> {
41+ public:
42+ using iterator = typename Mat::iterator ;
43+ using Proxy = typename Mat::Proxy ;
44+
45+ MatrixRow ( Mat& mat, int i) : start(mat.begin() + i), n(mat.ncol()), nr(mat.nrow()){}
46+
47+ inline int size () const { return n ;}
48+ inline Proxy operator []( int i){ return *(start+i*nr) ; }
49+
50+ private:
51+ typename Mat::iterator start ;
52+ int n ;
53+ int nr ;
54+ };
55+
56+ template <int RTYPE, typename Mat>
57+ class const_MatrixRow : public VectorBase <RTYPE,true ,MatrixRow<RTYPE,Mat>> {
58+ public:
59+ using const_iterator = typename Mat::const_iterator ;
60+ using const_Proxy = typename Mat::const_Proxy ;
61+
62+ const_MatrixRow ( const Mat& mat, int i) : start(mat.begin() + i * mat.nrow()), n(mat.ncol()), nr(mat.nrow()){}
63+
64+ inline int size () const { return n ;}
65+ inline const_Proxy operator []( int i){ return *(start+i*nr) ; }
66+
67+ private:
68+ typename Mat::const_iterator start ;
69+ int n ;
70+ int nr ;
71+ };
72+
73+
74+
675 template <int RTYPE, template <class > class StoragePolicy = PreserveStorage>
7- class Matrix {
76+ class Matrix : public MatrixBase <RTYPE, true , Matrix<RTYPE,StoragePolicy> > {
877 private:
978 Vector<RTYPE,StoragePolicy> vec ;
1079 int * dims ;
@@ -14,7 +83,12 @@ namespace Rcpp{
1483 using const_Proxy = typename Vector<RTYPE,StoragePolicy>::const_Proxy ;
1584 using iterator = typename Vector<RTYPE,StoragePolicy>::iterator ;
1685 using const_iterator = typename Vector<RTYPE,StoragePolicy>::const_iterator ;
17-
86+
87+ using Column = MatrixColumn<RTYPE, Matrix> ;
88+ using const_Column = const_MatrixColumn<RTYPE, Matrix> ;
89+ using Row = MatrixRow<RTYPE, Matrix> ;
90+ using const_Row = const_MatrixRow<RTYPE, Matrix> ;
91+
1892 Matrix (int nr, int nc) : vec(nr, nc){
1993 set_dims (nr, nc) ;
2094 }
@@ -29,12 +103,9 @@ namespace Rcpp{
29103 dims = INTEGER (d) ;
30104 }
31105
32- inline int nrow () const {
33- return dims[0 ] ;
34- }
35- inline int ncol () const {
36- return dims[1 ] ;
37- }
106+ inline int nrow () const { return dims[0 ] ; }
107+ inline int ncol () const { return dims[1 ] ; }
108+ inline int size () const { return vec.size () ; }
38109
39110 inline iterator begin (){ return vec.begin () ; }
40111 inline iterator end (){ return vec.end (); }
@@ -45,6 +116,16 @@ namespace Rcpp{
45116 inline Proxy operator ()(int i, int j) { return vec[offset (i,j)] ; }
46117 inline const_Proxy operator ()(int i, int j) const { return vec[offset (i,j)] ; }
47118
119+ inline Column col (int i){ return Column (*this , i) ; }
120+ inline const_Column col (int i) const { return const_Column (*this , i) ; }
121+ inline Column operator ()(internal::NamedPlaceHolder, int i){ return col (i); }
122+ inline const_Column operator ()(internal::NamedPlaceHolder, int i) const { return col (i); }
123+
124+ inline Row row (int i){ return Row (*this , i) ; }
125+ inline const_Row row (int i) const { return const_Row (*this , i) ; }
126+ inline Column operator ()(int i, internal::NamedPlaceHolder){ return row (i); }
127+ inline const_Column operator ()(int i, internal::NamedPlaceHolder) const { return row (i); }
128+
48129 private:
49130
50131 inline void set_dims (int nr, int nc){
0 commit comments