Rcpp Version 0.10.3
 All Classes Namespaces Files Functions Variables Typedefs Enumerator Friends Macros
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 template <int RTYPE>
26 class MatrixRow : public VectorBase< RTYPE, true, MatrixRow<RTYPE> > {
27 public:
29  typedef typename MATRIX::Proxy Proxy ;
30  typedef typename MATRIX::Proxy reference ;
32  typedef typename MATRIX::value_type value_type ;
33 
34  class iterator {
35  public:
37 
38  typedef int difference_type ;
41  typedef typename std::iterator_traits<vector_iterator>::pointer pointer ;
42 
43  typedef std::random_access_iterator_tag iterator_category ;
44 
45  iterator( const iterator& other) : row(other.row), index(other.index){}
46  iterator( MatrixRow& row_, int index_ ) : row(row_), index(index_){}
47 
49  index++;
50  return *this ;
51  }
53  iterator orig(*this);
54  index++ ;
55  return orig ;
56  }
57 
59  index-- ;
60  return *this ;
61  }
63  iterator orig(*this);
64  index-- ;
65  return orig ;
66  }
67 
68  iterator operator+(difference_type n) const { return iterator( row, index + n ) ; }
69  iterator operator-(difference_type n) const { return iterator( row, index - n ) ; }
70  difference_type operator-(const iterator& other) const { return index - other.index ; }
71 
72  iterator& operator+=(difference_type n) { index += n ; return *this ;}
73  iterator& operator-=(difference_type n) { index -= n ; return *this ;}
74 
76  return row[index] ;
77  }
79  return &row[index] ;
80  }
81 
82  bool operator==( const iterator& other) { return index == other.index ; }
83  bool operator!=( const iterator& other) { return index != other.index ; }
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 
89  inline reference operator[]( int i) const {
90  return row[ index + i ] ;
91  }
92 
94  return index - other.index ;
95  }
96 
97  private:
99  int index ;
100  } ;
101 
102  MatrixRow( MATRIX& object, int i ) :
103  parent(object),
104  start(parent.begin() + i),
105  parent_nrow(parent.nrow()),
106  row(i)
107  {
108  if( i < 0 || i >= parent.nrow() ) throw index_out_of_bounds() ;
109  }
110 
111  MatrixRow( const MatrixRow& other ) :
112  parent(other.parent),
113  start(other.start),
114  parent_nrow(other.parent_nrow),
115  row(other.row)
116  {} ;
117 
118  template <int RT, bool NA, typename T>
120  int n = size() ;
121  const T& ref = rhs.get_ref() ;
123  return *this ;
124  }
125 
126  MatrixRow& operator=( const MatrixRow& rhs ){
127  int n = size() ;
129  return *this ;
130  }
131 
132  inline reference operator[]( int i ){
133  return start[ get_parent_index(i) ] ;
134  }
135 
136  inline reference operator[]( int i ) const {
137  return parent[ row + i * parent_nrow ] ;
138  }
139 
140  inline iterator begin(){
141  return iterator( *this, 0 ) ;
142  }
143 
144  inline iterator end(){
145  return iterator( *this, size() ) ;
146  }
147 
148  inline iterator begin() const {
149  return iterator( const_cast<MatrixRow&>(*this), 0 ) ;
150  }
151 
152  inline iterator end() const {
153  return iterator( const_cast<MatrixRow&>(*this), size() ) ;
154  }
155 
156  inline int size() const {
157  return parent.ncol() ;
158  }
159 
160 private:
164  int row ;
165 
166  inline int get_parent_index(int i) const {
167  RCPP_DEBUG_4( "MatrixRow<%d>[%p]::get_parent_index(%d) = %d", RTYPE, this, i, i*parent_nrow)
168  Rprintf( "MatrixRow::get_parent_index(int = %d), parent_nrow = %d >> %d\n", i, parent_nrow, i*parent_nrow ) ;
169  return i * parent_nrow ;
170  }
171 } ;
172 
173 #endif