|
Rcpp Version 0.9.10
|
00001 // -*- mode: C++; c-indent-level: 4; c-basic-offset: 4; indent-tabs-mode: nil; -*- 00002 // 00003 // MatrixRow.h: Rcpp R/C++ interface class library -- matrices row 00004 // 00005 // Copyright (C) 2010 - 2011 Dirk Eddelbuettel and Romain Francois 00006 // 00007 // This file is part of Rcpp. 00008 // 00009 // Rcpp is free software: you can redistribute it and/or modify it 00010 // under the terms of the GNU General Public License as published by 00011 // the Free Software Foundation, either version 2 of the License, or 00012 // (at your option) any later version. 00013 // 00014 // Rcpp is distributed in the hope that it will be useful, but 00015 // WITHOUT ANY WARRANTY; without even the implied warranty of 00016 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00017 // GNU General Public License for more details. 00018 // 00019 // You should have received a copy of the GNU General Public License 00020 // along with Rcpp. If not, see <http://www.gnu.org/licenses/>. 00021 00022 #ifndef Rcpp__vector__MatrixRow_h 00023 #define Rcpp__vector__MatrixRow_h 00024 00025 template <int RTYPE> 00026 class MatrixRow : public VectorBase< RTYPE, true, MatrixRow<RTYPE> > { 00027 public: 00028 typedef Matrix<RTYPE> MATRIX ; 00029 typedef typename MATRIX::Proxy Proxy ; 00030 typedef typename MATRIX::Proxy reference ; 00031 typedef typename MATRIX::value_type value_type ; 00032 00033 class iterator { 00034 public: 00035 typedef typename traits::r_vector_iterator<RTYPE>::type vector_iterator ; 00036 00037 typedef int difference_type ; 00038 typedef typename traits::r_vector_proxy<RTYPE>::type value_type ; 00039 typedef typename traits::r_vector_proxy<RTYPE>::type reference ; 00040 typedef typename std::iterator_traits<vector_iterator>::pointer pointer ; 00041 00042 typedef std::random_access_iterator_tag iterator_category ; 00043 00044 iterator( MatrixRow& row_, int index_ ) : row(row_), index(index_){} 00045 00046 iterator& operator++(){ 00047 index++; 00048 return *this ; 00049 } 00050 iterator& operator++(int) { 00051 index++; 00052 return *this ; 00053 } 00054 00055 iterator& operator--(){ 00056 index-- ; 00057 return *this ; 00058 } 00059 iterator& operator--(int){ 00060 index-- ; 00061 return *this ; 00062 } 00063 00064 iterator operator+(difference_type n) const { return iterator( row, index + n ) ; } 00065 iterator operator-(difference_type n) const { return iterator( row, index - n ) ; } 00066 difference_type operator-(const iterator& other) const { return index - other.index ; } 00067 00068 iterator& operator+=(difference_type n) { index += n ; return *this ;} 00069 iterator& operator-=(difference_type n) { index -= n ; return *this ;} 00070 00071 reference operator*() { 00072 return row[index] ; 00073 } 00074 pointer operator->(){ 00075 return &row[index] ; 00076 } 00077 00078 bool operator==( const iterator& other) { return index == other.index ; } 00079 bool operator!=( const iterator& other) { return index != other.index ; } 00080 bool operator<( const iterator& other ) { return index < other.index ;} 00081 bool operator>( const iterator& other ) { return index > other.index ;} 00082 bool operator<=( const iterator& other ) { return index <= other.index ; } 00083 bool operator>=( const iterator& other ) { return index >= other.index ; } 00084 00085 inline reference operator[]( int i){ 00086 return row[ index + i ] ; 00087 } 00088 00089 difference_type operator-(const iterator& other) { 00090 return index - other.index ; 00091 } 00092 00093 private: 00094 MatrixRow& row ; 00095 int index ; 00096 } ; 00097 00098 MatrixRow( MATRIX& object, int i ) : 00099 parent(object), 00100 start(parent.begin() + i), 00101 parent_nrow(parent.nrow()) 00102 { 00103 if( i < 0 || i >= parent.nrow() ) throw index_out_of_bounds() ; 00104 } 00105 00106 MatrixRow( const MatrixRow& other ) : 00107 parent(other.parent), 00108 start(other.start), 00109 parent_nrow(other.parent_nrow) 00110 {} ; 00111 00112 template <int RT, bool NA, typename T> 00113 MatrixRow& operator=( const Rcpp::VectorBase<RT,NA,T>& rhs ){ 00114 int n = size() ; 00115 const T& ref = rhs.get_ref() ; 00116 RCPP_LOOP_UNROLL_LHSFUN(start,get_parent_index,ref) 00117 return *this ; 00118 } 00119 00120 MatrixRow& operator=( const MatrixRow& rhs ){ 00121 int n = size() ; 00122 RCPP_LOOP_UNROLL_LHSFUN(start,get_parent_index,rhs) 00123 return *this ; 00124 } 00125 00126 inline reference operator[]( int i ){ 00127 return start[ get_parent_index(i) ] ; 00128 } 00129 00130 inline reference operator[]( int i ) const { 00131 return start[ get_parent_index(i) ] ; 00132 } 00133 00134 inline iterator begin(){ 00135 return iterator( *this, 0 ) ; 00136 } 00137 00138 inline iterator end(){ 00139 return iterator( *this, size() ) ; 00140 } 00141 00142 inline iterator begin() const { 00143 return iterator( const_cast<MatrixRow&>(*this), 0 ) ; 00144 } 00145 00146 inline iterator end() const { 00147 return iterator( const_cast<MatrixRow&>(*this), size() ) ; 00148 } 00149 00150 inline int size() const { 00151 return parent.ncol() ; 00152 } 00153 00154 private: 00155 MATRIX& parent; 00156 typename MATRIX::iterator start ; 00157 int parent_nrow ; 00158 00159 inline int get_parent_index(int i) const { return i * parent_nrow ; } 00160 } ; 00161 00162 #endif