Rcpp Version 1.0.9
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 const_iterator begin() const {
88  return const_start ;
89  }
90 
91  inline const_iterator end() const {
92  return const_start + n ;
93  }
94 
95  inline const_iterator cbegin() const {
96  return const_start ;
97  }
98 
99  inline const_iterator cend() const {
100  return const_start + n ;
101  }
102 
103  inline iterator begin(){
104  return start ;
105  }
106 
107  inline iterator end(){
108  return start + n ;
109  }
110 
111  inline int size() const {
112  return n ;
113  }
114 
115 private:
116  const int n ;
119 
120 } ;
121 
122 template <int RTYPE>
123 class ConstMatrixColumn : public VectorBase<RTYPE,true,ConstMatrixColumn<RTYPE> > {
124 public:
126  typedef typename MATRIX::const_Proxy const_Proxy ;
127  typedef typename MATRIX::value_type value_type ;
129 
130  ConstMatrixColumn( const MATRIX& parent, int i ) :
131  n(parent.nrow()),
132  const_start(parent.begin() + i *n)
133  {
134  if( i < 0 || i >= parent.ncol() ) {
135  const char* fmt = "Column index is out of bounds: "
136  "[index=%i; column extent=%i].";
137  throw index_out_of_bounds(fmt, i, parent.ncol()) ;
138  }
139  }
140 
142  n(other.n),
143  const_start(other.const_start) {}
144 
145  inline const_Proxy operator[]( int i ) const {
146  return const_start[i] ;
147  }
148 
149  inline const_iterator begin() const {
150  return const_start ;
151  }
152 
153  inline const_iterator end() const {
154  return const_start + n ;
155  }
156 
157  inline const_iterator cbegin() const {
158  return const_start ;
159  }
160 
161  inline const_iterator cend() const {
162  return const_start + n ;
163  }
164 
165  inline int size() const {
166  return n ;
167  }
168 
169 private:
170  const int n ;
172 
173 } ;
174 
175 }
176 #endif
MATRIX::const_iterator const_iterator
Definition: MatrixColumn.h:128
const_Proxy operator[](int i) const
Definition: MatrixColumn.h:145
const_iterator const_start
Definition: MatrixColumn.h:171
const_iterator cend() const
Definition: MatrixColumn.h:161
MATRIX::value_type value_type
Definition: MatrixColumn.h:127
const_iterator begin() const
Definition: MatrixColumn.h:149
const_iterator end() const
Definition: MatrixColumn.h:153
const_iterator cbegin() const
Definition: MatrixColumn.h:157
ConstMatrixColumn(const MATRIX &parent, int i)
Definition: MatrixColumn.h:130
ConstMatrixColumn(const ConstMatrixColumn &other)
Definition: MatrixColumn.h:141
Matrix< RTYPE > MATRIX
Definition: MatrixColumn.h:125
MATRIX::const_Proxy const_Proxy
Definition: MatrixColumn.h:126
const_iterator const_start
Definition: MatrixColumn.h:118
MatrixColumn & operator=(const Rcpp::VectorBase< RT, NA, T > &rhs)
Definition: MatrixColumn.h:67
Matrix< RTYPE > MATRIX
Definition: MatrixColumn.h:30
int size() const
Definition: MatrixColumn.h:111
const_iterator end() const
Definition: MatrixColumn.h:91
MatrixColumn(const MATRIX &parent, int i)
Definition: MatrixColumn.h:49
const_iterator cend() const
Definition: MatrixColumn.h:99
const_Proxy operator[](int i) const
Definition: MatrixColumn.h:83
MatrixColumn(MATRIX &parent, int i)
Definition: MatrixColumn.h:37
const_iterator cbegin() const
Definition: MatrixColumn.h:95
MATRIX::value_type value_type
Definition: MatrixColumn.h:33
MATRIX::const_Proxy const_Proxy
Definition: MatrixColumn.h:32
MATRIX::iterator iterator
Definition: MatrixColumn.h:34
MatrixColumn & operator=(const MatrixColumn &rhs)
Definition: MatrixColumn.h:73
MATRIX::Proxy Proxy
Definition: MatrixColumn.h:31
const_iterator begin() const
Definition: MatrixColumn.h:87
MATRIX::const_iterator const_iterator
Definition: MatrixColumn.h:35
MatrixColumn(const MatrixColumn &other)
Definition: MatrixColumn.h:61
Proxy operator[](int i)
Definition: MatrixColumn.h:79
VECTOR::const_iterator const_iterator
Definition: Matrix.h:45
VECTOR::Proxy Proxy
Definition: Matrix.h:48
VECTOR::const_Proxy const_Proxy
Definition: Matrix.h:49
VECTOR::iterator iterator
Definition: Matrix.h:44
VECTOR & get_ref()
Definition: VectorBase.h:37
traits::r_vector_proxy< RTYPE, PreserveStorage >::type value_type
Definition: Vector.h:45
Rcpp API.
Definition: algo.h:28
Environment_Impl parent() const
Definition: Environment.h:387
#define RCPP_LOOP_UNROLL(TARGET, SOURCE)
Definition: unroll.h:47