Rcpp Version 1.0.9
SubMatrix.h
Go to the documentation of this file.
1 // -*- mode: C++; c-indent-level: 4; c-basic-offset: 4; tab-width: 8 -*-
2 //
3 // SubMatrix.h: Rcpp R/C++ interface class library -- sub matrices
4 //
5 // Copyright (C) 2010 - 2013 Dirk Eddelbuettel and Romain Francois
6 //
7 // This file is part of Rcpp.
8 //
9 // Rcpp is free software: you can redistribute it and/or modify it
10 // under the terms of the GNU General Public License as published by
11 // the Free Software Foundation, either version 2 of the License, or
12 // (at your option) any later version.
13 //
14 // Rcpp is distributed in the hope that it will be useful, but
15 // WITHOUT ANY WARRANTY; without even the implied warranty of
16 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 // GNU General Public License for more details.
18 //
19 // You should have received a copy of the GNU General Public License
20 // along with Rcpp. If not, see <http://www.gnu.org/licenses/>.
21 
22 #ifndef Rcpp__vector__SubMatrix_h
23 #define Rcpp__vector__SubMatrix_h
24 
25 namespace Rcpp{
26 
27 template <int RTYPE>
28 class SubMatrix : public Rcpp::MatrixBase< RTYPE, true, SubMatrix<RTYPE> > {
29 public:
32  typedef typename MATRIX::Proxy Proxy ;
33 
34  SubMatrix( MATRIX& m_, const Range& row_range_, const Range& col_range_ ) :
35  m(m_),
36  iter( static_cast< Vector<RTYPE>& >(m_).begin() + row_range_.get_start() + col_range_.get_start() * m_.nrow() ),
37  m_nr( m.nrow() ),
38  nc( col_range_.size() ),
39  nr( row_range_.size() )
40  {}
41 
42  inline R_xlen_t size() const { return ((R_xlen_t)ncol()) * nrow() ; }
43  inline int ncol() const { return nc ; }
44  inline int nrow() const { return nr ; }
45 
46  inline Proxy operator()(int i, int j) const {
47  return iter[ i + j*m_nr ] ;
48  }
49 
50  inline vec_iterator column_iterator( int j ) const { return iter + j*m_nr ; }
51 
52 private:
53  MATRIX& m ;
55  int m_nr, nc, nr ;
56 } ;
57 
58 template <int RTYPE, template <class> class StoragePolicy >
59 Matrix<RTYPE,StoragePolicy>::Matrix( const SubMatrix<RTYPE>& sub ) : VECTOR( Rf_allocMatrix( RTYPE, sub.nrow(), sub.ncol() )), nrows(sub.nrow()) {
60  int nc = sub.ncol() ;
61  iterator start = VECTOR::begin() ;
62  iterator rhs_it ;
63  for( int j=0; j<nc; j++){
64  rhs_it = sub.column_iterator(j) ;
65  for( int i=0; i<nrows; i++, ++start){
66  *start = rhs_it[i] ;
67  }
68  }
69 }
70 
71 template <int RTYPE, template <class> class StoragePolicy >
73  int nc = sub.ncol(), nr = sub.nrow() ;
74  if( nc != nrow() || nr != ncol() ){
75  nrows = nr ;
76  VECTOR::set__( Rf_allocMatrix( RTYPE, nr, nc ) ) ;
77  }
78  iterator start = VECTOR::begin() ;
79  iterator rhs_it ;
80  for( int j=0; j<nc; j++){
81  rhs_it = sub.column_iterator(j) ;
82  for( int i=0; i<nrows; i++, ++start){
83  *start = rhs_it[i] ;
84  }
85  }
86  return *this ;
87 }
88 
89 #undef RCPP_WRAP_SUBMATRIX
90 #define RCPP_WRAP_SUBMATRIX(RTYPE) \
91 template<> inline SEXP wrap< SubMatrix<RTYPE> >( \
92  const SubMatrix<RTYPE>& object \
93  ) { \
94  return Matrix<RTYPE>( object ) ; \
95  }
96 RCPP_WRAP_SUBMATRIX(REALSXP)
97 RCPP_WRAP_SUBMATRIX(INTSXP)
98 RCPP_WRAP_SUBMATRIX(LGLSXP)
99 RCPP_WRAP_SUBMATRIX(RAWSXP)
100 // RCPP_WRAP_SUBMATRIX(STRSXP)
101 // RCPP_WRAP_SUBMATRIX(VECSXP)
102 // RCPP_WRAP_SUBMATRIX(EXPRSXP)
103 #undef RCPP_WRAP_SUBMATRIX
104 
105 }
106 
107 #endif
#define RCPP_WRAP_SUBMATRIX(RTYPE)
Definition: SubMatrix.h:90
VECTOR::Proxy Proxy
Definition: Matrix.h:48
Matrix & operator=(const Matrix &other)
Definition: Matrix.h:83
int nrows
Definition: Matrix.h:29
VECTOR::iterator iterator
Definition: Matrix.h:44
Vector< RTYPE >::iterator vec_iterator
Definition: SubMatrix.h:31
Matrix< RTYPE > MATRIX
Definition: SubMatrix.h:30
int nrow() const
Definition: SubMatrix.h:44
R_xlen_t size() const
Definition: SubMatrix.h:42
Proxy operator()(int i, int j) const
Definition: SubMatrix.h:46
vec_iterator iter
Definition: SubMatrix.h:54
vec_iterator column_iterator(int j) const
Definition: SubMatrix.h:50
MATRIX::Proxy Proxy
Definition: SubMatrix.h:32
int ncol() const
Definition: SubMatrix.h:43
MATRIX & m
Definition: SubMatrix.h:53
SubMatrix(MATRIX &m_, const Range &row_range_, const Range &col_range_)
Definition: SubMatrix.h:34
traits::r_vector_iterator< RTYPE, StoragePolicy >::type iterator
Definition: Vector.h:46
iterator begin()
Definition: Vector.h:334
Rcpp API.
Definition: algo.h:28