@@ -170,6 +170,127 @@ class MatrixRow : public VectorBase< RTYPE, true, MatrixRow<RTYPE> > {
170170 return i * parent_nrow ;
171171 }
172172} ;
173+
174+ template <int RTYPE>
175+ class ConstMatrixRow : public VectorBase < RTYPE, true , ConstMatrixRow<RTYPE> > {
176+ public:
177+ typedef Matrix<RTYPE> MATRIX ;
178+ typedef typename MATRIX::const_Proxy const_reference ;
179+ typedef typename MATRIX::value_type value_type ;
180+
181+ class const_iterator {
182+ public:
183+ typedef typename traits::r_vector_iterator<RTYPE>::type vector_iterator ;
184+
185+ typedef int difference_type ;
186+ typedef typename traits::r_vector_const_proxy<RTYPE>::type value_type ;
187+ typedef typename traits::r_vector_const_proxy<RTYPE>::type reference ;
188+ typedef typename std::iterator_traits<vector_iterator>::pointer pointer ;
189+
190+ typedef std::random_access_iterator_tag iterator_category ;
191+
192+ const_iterator ( const const_iterator& other) : row(other.row), index(other.index){}
193+ const_iterator ( const ConstMatrixRow& row_, int index_ ) : row(row_), index(index_){}
194+
195+ const_iterator& operator ++(){
196+ index++;
197+ return *this ;
198+ }
199+ const_iterator operator ++(int ) {
200+ const_iterator orig (*this );
201+ index++ ;
202+ return orig ;
203+ }
204+
205+ const_iterator& operator --(){
206+ index-- ;
207+ return *this ;
208+ }
209+ const_iterator operator --(int ){
210+ const_iterator orig (*this );
211+ index-- ;
212+ return orig ;
213+ }
214+
215+ const_iterator operator +(difference_type n) const { return iterator ( row, index + n ) ; }
216+ const_iterator operator -(difference_type n) const { return iterator ( row, index - n ) ; }
217+ difference_type operator -(const const_iterator& other) const { return index - other.index ; }
218+
219+ const_iterator& operator +=(difference_type n) { index += n ; return *this ;}
220+ const_iterator& operator -=(difference_type n) { index -= n ; return *this ;}
221+
222+ const reference operator *() {
223+ return row[index] ;
224+ }
225+ const pointer operator ->(){
226+ return &row[index] ;
227+ }
228+
229+ bool operator ==( const const_iterator& other) { return index == other.index ; }
230+ bool operator !=( const const_iterator& other) { return index != other.index ; }
231+ bool operator <( const const_iterator& other ) { return index < other.index ;}
232+ bool operator >( const const_iterator& other ) { return index > other.index ;}
233+ bool operator <=( const const_iterator& other ) { return index <= other.index ; }
234+ bool operator >=( const const_iterator& other ) { return index >= other.index ; }
235+
236+ inline const reference operator []( int i) const {
237+ return row[ index + i ] ;
238+ }
239+
240+ difference_type operator -(const const_iterator& other) {
241+ return index - other.index ;
242+ }
243+
244+ private:
245+ const ConstMatrixRow& row ;
246+ int index ;
247+ } ;
248+
249+ typedef const_iterator iterator;
250+
251+ ConstMatrixRow ( const MATRIX& object, int i ) :
252+ parent (object),
253+ start (parent.begin() + i),
254+ parent_nrow (parent.nrow()),
255+ row (i)
256+ {
257+ if ( i < 0 || i >= parent.nrow () ) throw index_out_of_bounds () ;
258+ }
259+
260+ ConstMatrixRow ( const ConstMatrixRow& other ) :
261+ parent (other.parent),
262+ start (other.start),
263+ parent_nrow (other.parent_nrow),
264+ row (other.row)
265+ {} ;
266+
267+ inline const_reference operator []( int i ) const {
268+ return parent[ row + i * parent_nrow ] ;
269+ }
270+
271+ inline const_iterator begin () const {
272+ return const_iterator ( *this , 0 ) ;
273+ }
274+
275+ inline const_iterator end () const {
276+ return const_iterator ( *this , size () ) ;
277+ }
278+
279+ inline int size () const {
280+ return parent.ncol () ;
281+ }
282+
283+ private:
284+ const MATRIX& parent;
285+ typename MATRIX::const_iterator start ;
286+ int parent_nrow ;
287+ int row ;
288+
289+ inline int get_parent_index (int i) const {
290+ RCPP_DEBUG_4 ( " ConstMatrixRow<%d>::get_parent_index(int = %d), parent_nrow = %d >> %d\n " , RTYPE, i, parent_nrow, i*parent_nrow )
291+ return i * parent_nrow ;
292+ }
293+ } ;
173294}
174295
175296#endif
0 commit comments