Rcpp Version 1.0.9
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;
48  typedef typename std::iterator_traits<vector_iterator>::pointer pointer;
49  };
50 
52  {
54 
55  typedef int difference_type;
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 
104  return row[index] ;
105  }
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 
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:
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 
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 
264  return row[index] ;
265  }
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 
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
bool operator>=(const const_iterator &other)
Definition: MatrixRow.h:275
traits::r_vector_const_proxy< RTYPE >::type reference
Definition: MatrixRow.h:228
const_iterator & operator-=(difference_type n)
Definition: MatrixRow.h:261
difference_type operator-(const const_iterator &other)
Definition: MatrixRow.h:281
bool operator>(const const_iterator &other)
Definition: MatrixRow.h:273
const_iterator operator+(difference_type n) const
Definition: MatrixRow.h:256
const reference operator[](int i) const
Definition: MatrixRow.h:277
const_iterator operator++(int)
Definition: MatrixRow.h:240
const_iterator operator--(int)
Definition: MatrixRow.h:250
const_iterator(const const_iterator &other)
Definition: MatrixRow.h:233
traits::r_vector_const_proxy< RTYPE >::type value_type
Definition: MatrixRow.h:227
bool operator==(const const_iterator &other)
Definition: MatrixRow.h:270
std::random_access_iterator_tag iterator_category
Definition: MatrixRow.h:231
bool operator!=(const const_iterator &other)
Definition: MatrixRow.h:271
bool operator<(const const_iterator &other)
Definition: MatrixRow.h:272
difference_type operator-(const const_iterator &other) const
Definition: MatrixRow.h:258
const_iterator operator-(difference_type n) const
Definition: MatrixRow.h:257
bool operator<=(const const_iterator &other)
Definition: MatrixRow.h:274
const_iterator(const ConstMatrixRow &row_, int index_)
Definition: MatrixRow.h:234
const_iterator & operator+=(difference_type n)
Definition: MatrixRow.h:260
std::iterator_traits< vector_iterator >::pointer pointer
Definition: MatrixRow.h:229
const ConstMatrixRow & row
Definition: MatrixRow.h:286
traits::r_vector_iterator< RTYPE >::type vector_iterator
Definition: MatrixRow.h:224
Matrix< RTYPE > MATRIX
Definition: MatrixRow.h:218
MATRIX::value_type value_type
Definition: MatrixRow.h:220
const MATRIX & parent
Definition: MatrixRow.h:329
MATRIX::const_Proxy const_reference
Definition: MatrixRow.h:219
const_iterator begin() const
Definition: MatrixRow.h:316
ConstMatrixRow(const MATRIX &object, int i)
Definition: MatrixRow.h:292
const_reference operator[](int i) const
Definition: MatrixRow.h:312
MATRIX::const_iterator start
Definition: MatrixRow.h:330
const_iterator end() const
Definition: MatrixRow.h:320
ConstMatrixRow(const ConstMatrixRow &other)
Definition: MatrixRow.h:305
int size() const
Definition: MatrixRow.h:324
const_iterator iterator
Definition: MatrixRow.h:290
int get_parent_index(int i) const
Definition: MatrixRow.h:334
bool operator==(const iter_base &other)
Definition: MatrixRow.h:110
reference operator[](int i) const
Definition: MatrixRow.h:117
iter_base(const iter_base &other)
Definition: MatrixRow.h:73
iter_base & operator+=(difference_type n)
Definition: MatrixRow.h:100
TRAITS::vector_iterator vector_iterator
Definition: MatrixRow.h:64
iter_base & operator++()
Definition: MatrixRow.h:76
iter_base operator+(difference_type n) const
Definition: MatrixRow.h:96
std::random_access_iterator_tag iterator_category
Definition: MatrixRow.h:71
bool operator>=(const iter_base &other)
Definition: MatrixRow.h:115
TRAITS::reference reference
Definition: MatrixRow.h:68
iter_base(MatrixRow &row_, int index_)
Definition: MatrixRow.h:74
TRAITS::value_type value_type
Definition: MatrixRow.h:67
iter_base operator++(int)
Definition: MatrixRow.h:80
difference_type operator-(const iter_base &other) const
Definition: MatrixRow.h:98
iter_base & operator--()
Definition: MatrixRow.h:86
TRAITS::difference_type difference_type
Definition: MatrixRow.h:66
bool operator!=(const iter_base &other)
Definition: MatrixRow.h:111
bool operator<=(const iter_base &other)
Definition: MatrixRow.h:114
bool operator<(const iter_base &other)
Definition: MatrixRow.h:112
iter_base & operator-=(difference_type n)
Definition: MatrixRow.h:101
bool operator>(const iter_base &other)
Definition: MatrixRow.h:113
iter_base operator--(int)
Definition: MatrixRow.h:90
difference_type operator-(const iter_base &other)
Definition: MatrixRow.h:121
TRAITS::pointer pointer
Definition: MatrixRow.h:69
iter_base operator-(difference_type n) const
Definition: MatrixRow.h:97
const_iterator begin() const
Definition: MatrixRow.h:183
MATRIX::value_type value_type
Definition: MatrixRow.h:34
iterator begin()
Definition: MatrixRow.h:175
int get_parent_index(int i) const
Definition: MatrixRow.h:209
Matrix< RTYPE > MATRIX
Definition: MatrixRow.h:30
iter_base< iter_traits > iterator
Definition: MatrixRow.h:130
MatrixRow(MATRIX &object, int i)
Definition: MatrixRow.h:133
const_iterator cbegin() const
Definition: MatrixRow.h:191
MATRIX::Proxy Proxy
Definition: MatrixRow.h:31
MatrixRow & operator=(const Rcpp::VectorBase< RT, NA, T > &rhs)
Definition: MatrixRow.h:154
const_iterator cend() const
Definition: MatrixRow.h:195
iterator end()
Definition: MatrixRow.h:179
reference operator[](int i)
Definition: MatrixRow.h:167
MatrixRow(const MatrixRow &other)
Definition: MatrixRow.h:146
MATRIX::iterator start
Definition: MatrixRow.h:205
MATRIX & parent
Definition: MatrixRow.h:204
MatrixRow & operator=(const MatrixRow &rhs)
Definition: MatrixRow.h:161
MATRIX::Proxy reference
Definition: MatrixRow.h:32
iter_base< const_iter_traits > const_iterator
Definition: MatrixRow.h:131
int size() const
Definition: MatrixRow.h:199
const_iterator end() const
Definition: MatrixRow.h:187
reference operator[](int i) const
Definition: MatrixRow.h:171
MATRIX::const_Proxy const_reference
Definition: MatrixRow.h:33
int ncol() const
Definition: Matrix.h:94
VECTOR::const_iterator const_iterator
Definition: Matrix.h:45
VECTOR::Proxy Proxy
Definition: Matrix.h:48
int nrow() const
Definition: Matrix.h:97
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
#define RCPP_DEBUG_4(fmt, M1, M2, M3, M4)
Definition: debug.h:47
Rcpp API.
Definition: algo.h:28
traits::r_vector_const_iterator< RTYPE >::type vector_iterator
Definition: MatrixRow.h:53
traits::r_vector_proxy< RTYPE >::type value_type
Definition: MatrixRow.h:56
std::iterator_traits< vector_iterator >::pointer pointer
Definition: MatrixRow.h:58
traits::r_vector_proxy< RTYPE >::type value_type
Definition: MatrixRow.h:46
traits::r_vector_iterator< RTYPE >::type vector_iterator
Definition: MatrixRow.h:43
std::iterator_traits< vector_iterator >::pointer pointer
Definition: MatrixRow.h:48
const storage_type< RTYPE >::type * type
Definition: proxy.h:255
const storage_type< RTYPE >::type & type
Definition: proxy.h:234
storage_type< RTYPE >::type * type
Definition: proxy.h:251
storage_type< RTYPE >::type & type
Definition: proxy.h:217
#define RCPP_LOOP_UNROLL_LHSFUN(TARGET, FUN, SOURCE)
Definition: unroll.h:68