Rcpp Version 0.12.12
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  class iterator {
37  public:
39 
40  typedef int difference_type ;
43  typedef typename std::iterator_traits<vector_iterator>::pointer pointer ;
44 
45  typedef std::random_access_iterator_tag iterator_category ;
46 
47  iterator( const iterator& other) : row(other.row), index(other.index){}
48  iterator( MatrixRow& row_, int index_ ) : row(row_), index(index_){}
49 
51  index++;
52  return *this ;
53  }
55  iterator orig(*this);
56  index++ ;
57  return orig ;
58  }
59 
61  index-- ;
62  return *this ;
63  }
65  iterator orig(*this);
66  index-- ;
67  return orig ;
68  }
69 
70  iterator operator+(difference_type n) const { return iterator( row, index + n ) ; }
71  iterator operator-(difference_type n) const { return iterator( row, index - n ) ; }
72  difference_type operator-(const iterator& other) const { return index - other.index ; }
73 
74  iterator& operator+=(difference_type n) { index += n ; return *this ;}
75  iterator& operator-=(difference_type n) { index -= n ; return *this ;}
76 
77  reference operator*() {
78  return row[index] ;
79  }
80  pointer operator->(){
81  return &row[index] ;
82  }
83 
84  bool operator==( const iterator& other) { return index == other.index ; }
85  bool operator!=( const iterator& other) { return index != other.index ; }
86  bool operator<( const iterator& other ) { return index < other.index ;}
87  bool operator>( const iterator& other ) { return index > other.index ;}
88  bool operator<=( const iterator& other ) { return index <= other.index ; }
89  bool operator>=( const iterator& other ) { return index >= other.index ; }
90 
91  inline reference operator[]( int i) const {
92  return row[ index + i ] ;
93  }
94 
95  difference_type operator-(const iterator& other) {
96  return index - other.index ;
97  }
98 
99  private:
101  int index ;
102  } ;
103 
104  MatrixRow( MATRIX& object, int i ) :
105  parent(object),
106  start(parent.begin() + i),
107  parent_nrow(parent.nrow()),
108  row(i)
109  {
110  if( i < 0 || i >= parent.nrow() ) {
111  const char* fmt = "Row index is out of bounds: "
112  "[index=%i; row extent=%i].";
113  throw index_out_of_bounds(fmt, i, parent.nrow()) ;
114  }
115  }
116 
117  MatrixRow( const MatrixRow& other ) :
118  parent(other.parent),
119  start(other.start),
120  parent_nrow(other.parent_nrow),
121  row(other.row)
122  {} ;
123 
124  template <int RT, bool NA, typename T>
126  int n = size() ;
127  const T& ref = rhs.get_ref() ;
129  return *this ;
130  }
131 
132  MatrixRow& operator=( const MatrixRow& rhs ){
133  int n = size() ;
135  return *this ;
136  }
137 
138  inline reference operator[]( int i ){
139  return start[ get_parent_index(i) ] ;
140  }
141 
142  inline reference operator[]( int i ) const {
143  return parent[ row + i * parent_nrow ] ;
144  }
145 
146  inline iterator begin(){
147  return iterator( *this, 0 ) ;
148  }
149 
150  inline iterator end(){
151  return iterator( *this, size() ) ;
152  }
153 
154  inline iterator begin() const {
155  return iterator( const_cast<MatrixRow&>(*this), 0 ) ;
156  }
157 
158  inline iterator end() const {
159  return iterator( const_cast<MatrixRow&>(*this), size() ) ;
160  }
161 
162  inline int size() const {
163  return parent.ncol() ;
164  }
165 
166 private:
167  MATRIX& parent;
170  int row ;
171 
172  inline int get_parent_index(int i) const {
173  RCPP_DEBUG_4( "MatrixRow<%d>::get_parent_index(int = %d), parent_nrow = %d >> %d\n", RTYPE, i, parent_nrow, i*parent_nrow )
174  return i * parent_nrow ;
175  }
176 } ;
177 
178 template <int RTYPE>
179 class ConstMatrixRow : public VectorBase< RTYPE, true, ConstMatrixRow<RTYPE> > {
180 public:
183  typedef typename MATRIX::value_type value_type ;
184 
186  public:
188 
189  typedef int difference_type ;
192  typedef typename std::iterator_traits<vector_iterator>::pointer pointer ;
193 
194  typedef std::random_access_iterator_tag iterator_category ;
195 
196  const_iterator( const const_iterator& other) : row(other.row), index(other.index){}
197  const_iterator( const ConstMatrixRow& row_, int index_ ) : row(row_), index(index_){}
198 
200  index++;
201  return *this ;
202  }
204  const_iterator orig(*this);
205  index++ ;
206  return orig ;
207  }
208 
210  index-- ;
211  return *this ;
212  }
214  const_iterator orig(*this);
215  index-- ;
216  return orig ;
217  }
218 
219  const_iterator operator+(difference_type n) const { return iterator( row, index + n ) ; }
220  const_iterator operator-(difference_type n) const { return iterator( row, index - n ) ; }
221  difference_type operator-(const const_iterator& other) const { return index - other.index ; }
222 
223  const_iterator& operator+=(difference_type n) { index += n ; return *this ;}
224  const_iterator& operator-=(difference_type n) { index -= n ; return *this ;}
225 
226  const reference operator*() {
227  return row[index] ;
228  }
229  const pointer operator->(){
230  return &row[index] ;
231  }
232 
233  bool operator==( const const_iterator& other) { return index == other.index ; }
234  bool operator!=( const const_iterator& other) { return index != other.index ; }
235  bool operator<( const const_iterator& other ) { return index < other.index ;}
236  bool operator>( const const_iterator& other ) { return index > other.index ;}
237  bool operator<=( const const_iterator& other ) { return index <= other.index ; }
238  bool operator>=( const const_iterator& other ) { return index >= other.index ; }
239 
240  inline const reference operator[]( int i) const {
241  return row[ index + i ] ;
242  }
243 
244  difference_type operator-(const const_iterator& other) {
245  return index - other.index ;
246  }
247 
248  private:
250  int index ;
251  } ;
252 
254 
255  ConstMatrixRow( const MATRIX& object, int i ) :
256  parent(object),
257  start(parent.begin() + i),
258  parent_nrow(parent.nrow()),
259  row(i)
260  {
261  if( i < 0 || i >= parent.nrow() ) {
262  const char* fmt = "Row index is out of bounds: "
263  "[index=%i; row extent=%i].";
264  throw index_out_of_bounds(fmt, i, parent.nrow()) ;
265  }
266  }
267 
268  ConstMatrixRow( const ConstMatrixRow& other ) :
269  parent(other.parent),
270  start(other.start),
271  parent_nrow(other.parent_nrow),
272  row(other.row)
273  {} ;
274 
275  inline const_reference operator[]( int i ) const {
276  return parent[ row + i * parent_nrow ] ;
277  }
278 
279  inline const_iterator begin() const {
280  return const_iterator( *this, 0 ) ;
281  }
282 
283  inline const_iterator end() const {
284  return const_iterator( *this, size() ) ;
285  }
286 
287  inline int size() const {
288  return parent.ncol() ;
289  }
290 
291 private:
292  const MATRIX& parent;
295  int row ;
296 
297  inline int get_parent_index(int i) const {
298  RCPP_DEBUG_4( "ConstMatrixRow<%d>::get_parent_index(int = %d), parent_nrow = %d >> %d\n", RTYPE, i, parent_nrow, i*parent_nrow )
299  return i * parent_nrow ;
300  }
301 } ;
302 }
303 
304 #endif
traits::r_vector_const_proxy< RTYPE >::type reference
Definition: MatrixRow.h:191
const MATRIX & parent
Definition: MatrixRow.h:292
const_iterator(const const_iterator &other)
Definition: MatrixRow.h:196
iterator end() const
Definition: MatrixRow.h:158
iterator & operator-=(difference_type n)
Definition: MatrixRow.h:75
bool operator>(const iterator &other)
Definition: MatrixRow.h:87
bool operator>=(const const_iterator &other)
Definition: MatrixRow.h:238
ConstMatrixRow(const MATRIX &object, int i)
Definition: MatrixRow.h:255
const_iterator(const ConstMatrixRow &row_, int index_)
Definition: MatrixRow.h:197
difference_type operator-(const const_iterator &other)
Definition: MatrixRow.h:244
bool operator==(const const_iterator &other)
Definition: MatrixRow.h:233
MATRIX::iterator start
Definition: MatrixRow.h:168
traits::r_vector_iterator< RTYPE >::type vector_iterator
Definition: MatrixRow.h:38
reference operator[](int i) const
Definition: MatrixRow.h:91
const_iterator operator++(int)
Definition: MatrixRow.h:203
const_iterator operator-(difference_type n) const
Definition: MatrixRow.h:220
MATRIX::value_type value_type
Definition: MatrixRow.h:34
MatrixRow & operator=(const Rcpp::VectorBase< RT, NA, T > &rhs)
Definition: MatrixRow.h:125
int nrow() const
Definition: Matrix.h:99
VECTOR::const_iterator const_iterator
Definition: Matrix.h:45
VECTOR::Proxy Proxy
Definition: Matrix.h:48
VECTOR & get_ref()
Definition: VectorBase.h:37
Matrix< RTYPE > MATRIX
Definition: MatrixRow.h:30
std::random_access_iterator_tag iterator_category
Definition: MatrixRow.h:194
bool operator!=(const const_iterator &other)
Definition: MatrixRow.h:234
bool operator<(const const_iterator &other)
Definition: MatrixRow.h:235
bool operator<=(const const_iterator &other)
Definition: MatrixRow.h:237
reference operator[](int i) const
Definition: MatrixRow.h:142
std::random_access_iterator_tag iterator_category
Definition: MatrixRow.h:45
const_iterator operator+(difference_type n) const
Definition: MatrixRow.h:219
MatrixRow & operator=(const MatrixRow &rhs)
Definition: MatrixRow.h:132
const ConstMatrixRow & row
Definition: MatrixRow.h:249
bool operator>(const const_iterator &other)
Definition: MatrixRow.h:236
const storage_type< RTYPE >::type & type
Definition: proxy.h:225
Matrix< RTYPE > MATRIX
Definition: MatrixRow.h:181
MatrixRow(const MatrixRow &other)
Definition: MatrixRow.h:117
iterator operator+(difference_type n) const
Definition: MatrixRow.h:70
const_iterator end() const
Definition: MatrixRow.h:283
iterator(const iterator &other)
Definition: MatrixRow.h:47
traits::r_vector_proxy< RTYPE >::type value_type
Definition: Vector.h:46
int get_parent_index(int i) const
Definition: MatrixRow.h:297
#define RCPP_LOOP_UNROLL_LHSFUN(TARGET, FUN, SOURCE)
Definition: unroll.h:68
iterator & operator--()
Definition: MatrixRow.h:60
iterator operator++(int)
Definition: MatrixRow.h:54
iterator begin()
Definition: MatrixRow.h:146
storage_type< RTYPE >::type & type
Definition: proxy.h:211
std::iterator_traits< vector_iterator >::pointer pointer
Definition: MatrixRow.h:192
traits::r_vector_const_proxy< RTYPE >::type value_type
Definition: MatrixRow.h:190
bool operator==(const iterator &other)
Definition: MatrixRow.h:84
const reference operator[](int i) const
Definition: MatrixRow.h:240
const_iterator begin() const
Definition: MatrixRow.h:279
MATRIX::value_type value_type
Definition: MatrixRow.h:183
bool operator!=(const iterator &other)
Definition: MatrixRow.h:85
bool operator>=(const iterator &other)
Definition: MatrixRow.h:89
bool operator<=(const iterator &other)
Definition: MatrixRow.h:88
traits::r_vector_proxy< RTYPE >::type reference
Definition: MatrixRow.h:42
const_iterator & operator+=(difference_type n)
Definition: MatrixRow.h:223
MATRIX::const_iterator start
Definition: MatrixRow.h:293
int size() const
Definition: MatrixRow.h:287
traits::r_vector_iterator< RTYPE >::type vector_iterator
Definition: MatrixRow.h:187
MATRIX & parent
Definition: MatrixRow.h:167
difference_type operator-(const iterator &other)
Definition: MatrixRow.h:95
bool operator<(const iterator &other)
Definition: MatrixRow.h:86
VECTOR::iterator iterator
Definition: Matrix.h:44
traits::r_vector_proxy< RTYPE >::type value_type
Definition: MatrixRow.h:41
iterator & operator+=(difference_type n)
Definition: MatrixRow.h:74
int get_parent_index(int i) const
Definition: MatrixRow.h:172
MATRIX::Proxy reference
Definition: MatrixRow.h:32
std::iterator_traits< vector_iterator >::pointer pointer
Definition: MatrixRow.h:43
const_reference operator[](int i) const
Definition: MatrixRow.h:275
VECTOR::const_Proxy const_Proxy
Definition: Matrix.h:49
MatrixRow(MATRIX &object, int i)
Definition: MatrixRow.h:104
const_iterator operator--(int)
Definition: MatrixRow.h:213
reference operator[](int i)
Definition: MatrixRow.h:138
iterator(MatrixRow &row_, int index_)
Definition: MatrixRow.h:48
const_iterator & operator-=(difference_type n)
Definition: MatrixRow.h:224
Rcpp API.
Definition: algo.h:28
MATRIX::const_Proxy const_reference
Definition: MatrixRow.h:33
ConstMatrixRow(const ConstMatrixRow &other)
Definition: MatrixRow.h:268
difference_type operator-(const iterator &other) const
Definition: MatrixRow.h:72
MATRIX::const_Proxy const_reference
Definition: MatrixRow.h:182
iterator begin() const
Definition: MatrixRow.h:154
#define RCPP_DEBUG_4(fmt, M1, M2, M3, M4)
Definition: debug.h:47
difference_type operator-(const const_iterator &other) const
Definition: MatrixRow.h:221
int size() const
Definition: MatrixRow.h:162
iterator operator--(int)
Definition: MatrixRow.h:64
const_iterator iterator
Definition: MatrixRow.h:253
storage_type< RTYPE >::type * type
Definition: proxy.h:239
iterator operator-(difference_type n) const
Definition: MatrixRow.h:71
iterator end()
Definition: MatrixRow.h:150
MATRIX::Proxy Proxy
Definition: MatrixRow.h:31
iterator & operator++()
Definition: MatrixRow.h:50
int ncol() const
Definition: Matrix.h:96