Rcpp Version 1.0.0
MatrixRow.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 // MatrixRow.h: Rcpp R/C++ interface class library -- matrices row
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__MatrixRow_h
23 #define Rcpp__vector__MatrixRow_h
24 
25 namespace Rcpp{
26 
27 template <int RTYPE>
28 class MatrixRow : public VectorBase< RTYPE, true, MatrixRow<RTYPE> > {
29 public:
31  typedef typename MATRIX::Proxy Proxy ;
32  typedef typename MATRIX::Proxy reference ;
34  typedef typename MATRIX::value_type value_type ;
35 
36  // We now have the structure to correct const-correctness, but without
37  // major changes to the proxy mechanism, we cannot do it correctly. As
38  // a result, it turns out that both MatrixRow iterators are effectively
39  // const, but this has always been the way it is so we won't break any
40  // compatibility. TODO: Fix proxies and make this const correct.
41  struct iter_traits
42  {
44 
45  typedef int difference_type;
47  typedef value_type reference;
48  typedef typename std::iterator_traits<vector_iterator>::pointer pointer;
49  };
50 
52  {
54 
55  typedef int difference_type;
57  typedef value_type reference;
58  typedef typename std::iterator_traits<vector_iterator>::pointer pointer;
59  };
60 
61  template< typename TRAITS >
62  class iter_base {
63  public:
64  typedef typename TRAITS::vector_iterator vector_iterator;
65 
66  typedef typename TRAITS::difference_type difference_type ;
67  typedef typename TRAITS::value_type value_type ;
68  typedef typename TRAITS::reference reference ;
69  typedef typename TRAITS::pointer pointer ;
70 
71  typedef std::random_access_iterator_tag iterator_category ;
72 
73  iter_base( const iter_base& other) : row(other.row), index(other.index){}
74  iter_base( MatrixRow& row_, int index_ ) : row(row_), index(index_){}
75 
77  index++;
78  return *this ;
79  }
81  iterator orig(*this);
82  index++ ;
83  return orig ;
84  }
85 
87  index-- ;
88  return *this ;
89  }
91  iter_base orig(*this);
92  index-- ;
93  return orig ;
94  }
95 
96  iter_base operator+(difference_type n) const { return iter_base( row, index + n ) ; }
97  iter_base operator-(difference_type n) const { return iter_base( row, index - n ) ; }
98  difference_type operator-(const iter_base& other) const { return index - other.index ; }
99 
100  iter_base& operator+=(difference_type n) { index += n ; return *this ;}
101  iter_base& operator-=(difference_type n) { index -= n ; return *this ;}
102 
103  reference operator*() {
104  return row[index] ;
105  }
106  pointer operator->(){
107  return &row[index] ;
108  }
109 
110  bool operator==( const iter_base& other) { return index == other.index ; }
111  bool operator!=( const iter_base& other) { return index != other.index ; }
112  bool operator<( const iter_base& other ) { return index < other.index ;}
113  bool operator>( const iter_base& other ) { return index > other.index ;}
114  bool operator<=( const iter_base& other ) { return index <= other.index ; }
115  bool operator>=( const iter_base& other ) { return index >= other.index ; }
116 
117  inline reference operator[]( int i) const {
118  return row[ index + i ] ;
119  }
120 
121  difference_type operator-(const iter_base& other) {
122  return index - other.index ;
123  }
124 
125  private:
127  int index ;
128  };
129 
132 
133  MatrixRow( MATRIX& object, int i ) :
134  parent(object),
135  start(parent.begin() + i),
136  parent_nrow(parent.nrow()),
137  row(i)
138  {
139  if( i < 0 || i >= parent.nrow() ) {
140  const char* fmt = "Row index is out of bounds: "
141  "[index=%i; row extent=%i].";
142  throw index_out_of_bounds(fmt, i, parent.nrow()) ;
143  }
144  }
145 
146  MatrixRow( const MatrixRow& other ) :
147  parent(other.parent),
148  start(other.start),
149  parent_nrow(other.parent_nrow),
150  row(other.row)
151  {} ;
152 
153  template <int RT, bool NA, typename T>
155  int n = size() ;
156  const T& ref = rhs.get_ref() ;
158  return *this ;
159  }
160 
161  MatrixRow& operator=( const MatrixRow& rhs ){
162  int n = size() ;
164  return *this ;
165  }
166 
167  inline reference operator[]( int i ){
168  return start[ get_parent_index(i) ] ;
169  }
170 
171  inline reference operator[]( int i ) const {
172  return parent[ row + i * parent_nrow ] ;
173  }
174 
175  inline iterator begin(){
176  return iterator( *this, 0 ) ;
177  }
178 
179  inline iterator end(){
180  return iterator( *this, size() ) ;
181  }
182 
183  inline const_iterator begin() const {
184  return const_iterator( const_cast<MatrixRow&>(*this), 0 ) ;
185  }
186 
187  inline const_iterator end() const {
188  return const_iterator( const_cast<MatrixRow&>(*this), size() ) ;
189  }
190 
191  inline const_iterator cbegin() const {
192  return const_iterator( const_cast<MatrixRow&>(*this), 0 ) ;
193  }
194 
195  inline const_iterator cend() const {
196  return const_iterator( const_cast<MatrixRow&>(*this), size() ) ;
197  }
198 
199  inline int size() const {
200  return parent.ncol() ;
201  }
202 
203 private:
204  MATRIX& parent;
207  int row ;
208 
209  inline int get_parent_index(int i) const {
210  RCPP_DEBUG_4( "MatrixRow<%d>::get_parent_index(int = %d), parent_nrow = %d >> %d\n", RTYPE, i, parent_nrow, i*parent_nrow )
211  return i * parent_nrow ;
212  }
213 } ;
214 
215 template <int RTYPE>
216 class ConstMatrixRow : public VectorBase< RTYPE, true, ConstMatrixRow<RTYPE> > {
217 public:
220  typedef typename MATRIX::value_type value_type ;
221 
223  public:
225 
226  typedef int difference_type ;
229  typedef typename std::iterator_traits<vector_iterator>::pointer pointer ;
230 
231  typedef std::random_access_iterator_tag iterator_category ;
232 
233  const_iterator( const const_iterator& other) : row(other.row), index(other.index){}
234  const_iterator( const ConstMatrixRow& row_, int index_ ) : row(row_), index(index_){}
235 
237  index++;
238  return *this ;
239  }
241  const_iterator orig(*this);
242  index++ ;
243  return orig ;
244  }
245 
247  index-- ;
248  return *this ;
249  }
251  const_iterator orig(*this);
252  index-- ;
253  return orig ;
254  }
255 
256  const_iterator operator+(difference_type n) const { return iterator( row, index + n ) ; }
257  const_iterator operator-(difference_type n) const { return iterator( row, index - n ) ; }
258  difference_type operator-(const const_iterator& other) const { return index - other.index ; }
259 
260  const_iterator& operator+=(difference_type n) { index += n ; return *this ;}
261  const_iterator& operator-=(difference_type n) { index -= n ; return *this ;}
262 
263  const reference operator*() {
264  return row[index] ;
265  }
266  const pointer operator->(){
267  return &row[index] ;
268  }
269 
270  bool operator==( const const_iterator& other) { return index == other.index ; }
271  bool operator!=( const const_iterator& other) { return index != other.index ; }
272  bool operator<( const const_iterator& other ) { return index < other.index ;}
273  bool operator>( const const_iterator& other ) { return index > other.index ;}
274  bool operator<=( const const_iterator& other ) { return index <= other.index ; }
275  bool operator>=( const const_iterator& other ) { return index >= other.index ; }
276 
277  inline const reference operator[]( int i) const {
278  return row[ index + i ] ;
279  }
280 
281  difference_type operator-(const const_iterator& other) {
282  return index - other.index ;
283  }
284 
285  private:
287  int index ;
288  } ;
289 
291 
292  ConstMatrixRow( const MATRIX& object, int i ) :
293  parent(object),
294  start(parent.begin() + i),
295  parent_nrow(parent.nrow()),
296  row(i)
297  {
298  if( i < 0 || i >= parent.nrow() ) {
299  const char* fmt = "Row index is out of bounds: "
300  "[index=%i; row extent=%i].";
301  throw index_out_of_bounds(fmt, i, parent.nrow()) ;
302  }
303  }
304 
305  ConstMatrixRow( const ConstMatrixRow& other ) :
306  parent(other.parent),
307  start(other.start),
308  parent_nrow(other.parent_nrow),
309  row(other.row)
310  {} ;
311 
312  inline const_reference operator[]( int i ) const {
313  return parent[ row + i * parent_nrow ] ;
314  }
315 
316  inline const_iterator begin() const {
317  return const_iterator( *this, 0 ) ;
318  }
319 
320  inline const_iterator end() const {
321  return const_iterator( *this, size() ) ;
322  }
323 
324  inline int size() const {
325  return parent.ncol() ;
326  }
327 
328 private:
329  const MATRIX& parent;
332  int row ;
333 
334  inline int get_parent_index(int i) const {
335  RCPP_DEBUG_4( "ConstMatrixRow<%d>::get_parent_index(int = %d), parent_nrow = %d >> %d\n", RTYPE, i, parent_nrow, i*parent_nrow )
336  return i * parent_nrow ;
337  }
338 } ;
339 }
340 
341 #endif
traits::r_vector_const_proxy< RTYPE >::type reference
Definition: MatrixRow.h:228
const MATRIX & parent
Definition: MatrixRow.h:329
const_iterator(const const_iterator &other)
Definition: MatrixRow.h:233
bool operator==(const iter_base &other)
Definition: MatrixRow.h:110
std::iterator_traits< vector_iterator >::pointer pointer
Definition: MatrixRow.h:48
bool operator>=(const const_iterator &other)
Definition: MatrixRow.h:275
iter_base(MatrixRow &row_, int index_)
Definition: MatrixRow.h:74
ConstMatrixRow(const MATRIX &object, int i)
Definition: MatrixRow.h:292
const_iterator(const ConstMatrixRow &row_, int index_)
Definition: MatrixRow.h:234
difference_type operator-(const const_iterator &other)
Definition: MatrixRow.h:281
bool operator==(const const_iterator &other)
Definition: MatrixRow.h:270
MATRIX::iterator start
Definition: MatrixRow.h:205
TRAITS::pointer pointer
Definition: MatrixRow.h:69
const_iterator operator++(int)
Definition: MatrixRow.h:240
const_iterator end() const
Definition: MatrixRow.h:187
const_iterator begin() const
Definition: MatrixRow.h:183
MATRIX::value_type value_type
Definition: MatrixRow.h:34
MatrixRow & operator=(const Rcpp::VectorBase< RT, NA, T > &rhs)
Definition: MatrixRow.h:154
VECTOR::const_iterator const_iterator
Definition: Matrix.h:45
const_iterator cbegin() const
Definition: MatrixRow.h:191
VECTOR::Proxy Proxy
Definition: Matrix.h:48
VECTOR & get_ref()
Definition: VectorBase.h:37
traits::r_vector_proxy< RTYPE, StoragePolicy >::type value_type
Definition: Vector.h:46
Matrix< RTYPE > MATRIX
Definition: MatrixRow.h:30
int get_parent_index(int i) const
Definition: MatrixRow.h:334
std::random_access_iterator_tag iterator_category
Definition: MatrixRow.h:231
bool operator!=(const const_iterator &other)
Definition: MatrixRow.h:271
int size() const
Definition: MatrixRow.h:199
bool operator<(const const_iterator &other)
Definition: MatrixRow.h:272
bool operator<=(const const_iterator &other)
Definition: MatrixRow.h:274
std::iterator_traits< vector_iterator >::pointer pointer
Definition: MatrixRow.h:58
const_iterator operator-(difference_type n) const
Definition: MatrixRow.h:257
const_iterator begin() const
Definition: MatrixRow.h:316
TRAITS::reference reference
Definition: MatrixRow.h:68
reference operator[](int i) const
Definition: MatrixRow.h:171
MatrixRow & operator=(const MatrixRow &rhs)
Definition: MatrixRow.h:161
int nrow() const
Definition: Matrix.h:97
bool operator!=(const iter_base &other)
Definition: MatrixRow.h:111
const storage_type< RTYPE >::type & type
Definition: proxy.h:234
const ConstMatrixRow & row
Definition: MatrixRow.h:286
traits::r_vector_const_iterator< RTYPE >::type vector_iterator
Definition: MatrixRow.h:53
bool operator>(const const_iterator &other)
Definition: MatrixRow.h:273
difference_type operator-(const iter_base &other) const
Definition: MatrixRow.h:98
iter_base operator--(int)
Definition: MatrixRow.h:90
Matrix< RTYPE > MATRIX
Definition: MatrixRow.h:218
iter_base & operator-=(difference_type n)
Definition: MatrixRow.h:101
MatrixRow(const MatrixRow &other)
Definition: MatrixRow.h:146
difference_type operator-(const const_iterator &other) const
Definition: MatrixRow.h:258
#define RCPP_LOOP_UNROLL_LHSFUN(TARGET, FUN, SOURCE)
Definition: unroll.h:68
iter_base(const iter_base &other)
Definition: MatrixRow.h:73
const_iterator operator+(difference_type n) const
Definition: MatrixRow.h:256
iterator begin()
Definition: MatrixRow.h:175
int size() const
Definition: MatrixRow.h:324
std::iterator_traits< vector_iterator >::pointer pointer
Definition: MatrixRow.h:229
iter_base operator+(difference_type n) const
Definition: MatrixRow.h:96
traits::r_vector_const_proxy< RTYPE >::type value_type
Definition: MatrixRow.h:227
bool operator<=(const iter_base &other)
Definition: MatrixRow.h:114
iter_base operator++(int)
Definition: MatrixRow.h:80
bool operator>=(const iter_base &other)
Definition: MatrixRow.h:115
MATRIX::value_type value_type
Definition: MatrixRow.h:220
int get_parent_index(int i) const
Definition: MatrixRow.h:209
iter_base & operator++()
Definition: MatrixRow.h:76
iter_base & operator--()
Definition: MatrixRow.h:86
const_iterator & operator+=(difference_type n)
Definition: MatrixRow.h:260
MATRIX::const_iterator start
Definition: MatrixRow.h:330
TRAITS::value_type value_type
Definition: MatrixRow.h:67
const_reference operator[](int i) const
Definition: MatrixRow.h:312
reference operator[](int i) const
Definition: MatrixRow.h:117
difference_type operator-(const iter_base &other)
Definition: MatrixRow.h:121
storage_type< RTYPE >::type & type
Definition: proxy.h:217
traits::r_vector_iterator< RTYPE >::type vector_iterator
Definition: MatrixRow.h:224
MATRIX & parent
Definition: MatrixRow.h:204
int ncol() const
Definition: Matrix.h:94
VECTOR::iterator iterator
Definition: Matrix.h:44
storage_type< RTYPE >::type * type
Definition: proxy.h:251
iter_base< const_iter_traits > const_iterator
Definition: MatrixRow.h:131
TRAITS::difference_type difference_type
Definition: MatrixRow.h:66
MATRIX::Proxy reference
Definition: MatrixRow.h:32
VECTOR::const_Proxy const_Proxy
Definition: Matrix.h:49
MatrixRow(MATRIX &object, int i)
Definition: MatrixRow.h:133
const_iterator operator--(int)
Definition: MatrixRow.h:250
reference operator[](int i)
Definition: MatrixRow.h:167
const_iterator cend() const
Definition: MatrixRow.h:195
const reference operator[](int i) const
Definition: MatrixRow.h:277
const_iterator & operator-=(difference_type n)
Definition: MatrixRow.h:261
Rcpp API.
Definition: algo.h:28
MATRIX::const_Proxy const_reference
Definition: MatrixRow.h:33
ConstMatrixRow(const ConstMatrixRow &other)
Definition: MatrixRow.h:305
bool operator>(const iter_base &other)
Definition: MatrixRow.h:113
MATRIX::const_Proxy const_reference
Definition: MatrixRow.h:219
TRAITS::vector_iterator vector_iterator
Definition: MatrixRow.h:64
#define RCPP_DEBUG_4(fmt, M1, M2, M3, M4)
Definition: debug.h:47
iter_base & operator+=(difference_type n)
Definition: MatrixRow.h:100
traits::r_vector_proxy< RTYPE >::type value_type
Definition: MatrixRow.h:46
const storage_type< RTYPE >::type * type
Definition: proxy.h:255
const_iterator iterator
Definition: MatrixRow.h:290
bool operator<(const iter_base &other)
Definition: MatrixRow.h:112
traits::r_vector_iterator< RTYPE >::type vector_iterator
Definition: MatrixRow.h:43
traits::r_vector_proxy< RTYPE >::type value_type
Definition: MatrixRow.h:56
iter_base operator-(difference_type n) const
Definition: MatrixRow.h:97
iterator end()
Definition: MatrixRow.h:179
iter_base< iter_traits > iterator
Definition: MatrixRow.h:130
MATRIX::Proxy Proxy
Definition: MatrixRow.h:31
const_iterator end() const
Definition: MatrixRow.h:320
std::random_access_iterator_tag iterator_category
Definition: MatrixRow.h:71