Rcpp Version 0.12.12
MatrixColumn.h
Go to the documentation of this file.
1 // -*- mode: C++; c-indent-level: 4; c-basic-offset: 4; indent-tabs-mode: nil; -*-
2 //
3 // MatrixColumn.h: Rcpp R/C++ interface class library -- matrices column
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__MatrixColumn_h
23 #define Rcpp__vector__MatrixColumn_h
24 
25 namespace Rcpp{
26 
27 template <int RTYPE>
28 class MatrixColumn : public VectorBase<RTYPE,true,MatrixColumn<RTYPE> > {
29 public:
31  typedef typename MATRIX::Proxy Proxy ;
32  typedef typename MATRIX::const_Proxy const_Proxy ;
33  typedef typename MATRIX::value_type value_type ;
34  typedef typename MATRIX::iterator iterator ;
36 
37  MatrixColumn( MATRIX& parent, int i ) :
38  n(parent.nrow()),
39  start(parent.begin() + static_cast<R_xlen_t>(i) * n ),
40  const_start(const_cast<const MATRIX&>(parent).begin() + static_cast<R_xlen_t>(i) * n)
41  {
42  if( i < 0 || i >= parent.ncol() ) {
43  const char* fmt = "Column index is out of bounds: "
44  "[index=%i; column extent=%i].";
45  throw index_out_of_bounds(fmt, i, parent.ncol()) ;
46  }
47  }
48 
49  MatrixColumn( const MATRIX& parent, int i ) :
50  n(parent.nrow()),
51  start( const_cast<MATRIX&>(parent).begin() + static_cast<R_xlen_t>(i) * n ),
52  const_start(parent.begin() + static_cast<R_xlen_t>(i) * n)
53  {
54  if( i < 0 || i >= parent.ncol() ) {
55  const char* fmt = "Column index is out of bounds: "
56  "[index=%i; column extent=%i].";
57  throw index_out_of_bounds(fmt, i, parent.ncol()) ;
58  }
59  }
60 
61  MatrixColumn( const MatrixColumn& other ) :
62  n(other.n),
63  start(other.start),
64  const_start(other.const_start) {}
65 
66  template <int RT, bool NA, typename T>
68  const T& ref = rhs.get_ref() ;
70  return *this ;
71  }
72 
74  iterator rhs_start = rhs.start ;
75  RCPP_LOOP_UNROLL(start,rhs_start)
76  return *this ;
77  }
78 
79  inline Proxy operator[]( int i ){
80  return start[i] ;
81  }
82 
83  inline const_Proxy operator[]( int i ) const {
84  return const_start[i] ;
85  }
86 
87  inline iterator begin(){
88  return start ;
89  }
90 
91  inline const_iterator begin() const {
92  return const_start ;
93  }
94 
95  inline iterator end(){
96  return start + n ;
97  }
98 
99  inline const_iterator end() const {
100  return const_start + n ;
101  }
102 
103  inline int size() const {
104  return n ;
105  }
106 
107 private:
108  const int n ;
109  iterator start ;
110  const_iterator const_start ;
111 
112 } ;
113 
114 template <int RTYPE>
115 class ConstMatrixColumn : public VectorBase<RTYPE,true,ConstMatrixColumn<RTYPE> > {
116 public:
118  typedef typename MATRIX::const_Proxy const_Proxy ;
119  typedef typename MATRIX::value_type value_type ;
121 
122  ConstMatrixColumn( const MATRIX& parent, int i ) :
123  n(parent.nrow()),
124  const_start(parent.begin() + i *n)
125  {
126  if( i < 0 || i >= parent.ncol() ) {
127  const char* fmt = "Column index is out of bounds: "
128  "[index=%i; column extent=%i].";
129  throw index_out_of_bounds(fmt, i, parent.ncol()) ;
130  }
131  }
132 
134  n(other.n),
135  const_start(other.const_start) {}
136 
137  inline const_Proxy operator[]( int i ) const {
138  return const_start[i] ;
139  }
140 
141  inline const_iterator begin() const {
142  return const_start ;
143  }
144 
145  inline const_iterator end() const {
146  return const_start + n ;
147  }
148 
149  inline int size() const {
150  return n ;
151  }
152 
153 private:
154  const int n ;
155  const_iterator const_start ;
156 
157 } ;
158 
159 }
160 #endif
Proxy operator[](int i)
Definition: MatrixColumn.h:79
const_iterator end() const
Definition: MatrixColumn.h:145
MatrixColumn(const MATRIX &parent, int i)
Definition: MatrixColumn.h:49
MATRIX::const_Proxy const_Proxy
Definition: MatrixColumn.h:118
VECTOR::const_iterator const_iterator
Definition: Matrix.h:45
VECTOR::Proxy Proxy
Definition: Matrix.h:48
VECTOR & get_ref()
Definition: VectorBase.h:37
const_iterator const_start
Definition: MatrixColumn.h:110
#define RCPP_LOOP_UNROLL(TARGET, SOURCE)
Definition: unroll.h:47
Matrix< RTYPE > MATRIX
Definition: MatrixColumn.h:117
const_iterator end() const
Definition: MatrixColumn.h:99
traits::r_vector_proxy< RTYPE >::type value_type
Definition: Vector.h:46
MATRIX::value_type value_type
Definition: MatrixColumn.h:33
ConstMatrixColumn(const MATRIX &parent, int i)
Definition: MatrixColumn.h:122
MatrixColumn & operator=(const MatrixColumn &rhs)
Definition: MatrixColumn.h:73
MatrixColumn & operator=(const Rcpp::VectorBase< RT, NA, T > &rhs)
Definition: MatrixColumn.h:67
const_Proxy operator[](int i) const
Definition: MatrixColumn.h:83
MATRIX::Proxy Proxy
Definition: MatrixColumn.h:31
int size() const
Definition: MatrixColumn.h:103
MATRIX::value_type value_type
Definition: MatrixColumn.h:119
const_Proxy operator[](int i) const
Definition: MatrixColumn.h:137
const_iterator const_start
Definition: MatrixColumn.h:155
const_iterator begin() const
Definition: MatrixColumn.h:141
VECTOR::iterator iterator
Definition: Matrix.h:44
ConstMatrixColumn(const ConstMatrixColumn &other)
Definition: MatrixColumn.h:133
MatrixColumn(const MatrixColumn &other)
Definition: MatrixColumn.h:61
VECTOR::const_Proxy const_Proxy
Definition: Matrix.h:49
Matrix< RTYPE > MATRIX
Definition: MatrixColumn.h:30
MATRIX::const_iterator const_iterator
Definition: MatrixColumn.h:120
MatrixColumn(MATRIX &parent, int i)
Definition: MatrixColumn.h:37
Rcpp API.
Definition: algo.h:28
MATRIX::const_Proxy const_Proxy
Definition: MatrixColumn.h:32
Environment_Impl parent() const
Definition: Environment.h:387
MATRIX::const_iterator const_iterator
Definition: MatrixColumn.h:35
const_iterator begin() const
Definition: MatrixColumn.h:91
iterator begin()
Definition: MatrixColumn.h:87
MATRIX::iterator iterator
Definition: MatrixColumn.h:34
int ncol() const
Definition: Matrix.h:96