Rcpp Version 0.9.10
MatrixColumn.h
Go to the documentation of this file.
00001 // -*- mode: C++; c-indent-level: 4; c-basic-offset: 4; indent-tabs-mode: nil; -*-
00002 //
00003 // MatrixColumn.h: Rcpp R/C++ interface class library -- matrices column
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__MatrixColumn_h
00023 #define Rcpp__vector__MatrixColumn_h
00024    
00025 template <int RTYPE>
00026 class MatrixColumn : public VectorBase<RTYPE,true,MatrixColumn<RTYPE> > {
00027 public:
00028     typedef Matrix<RTYPE> MATRIX ;
00029     typedef typename MATRIX::Proxy Proxy ;
00030     typedef typename MATRIX::value_type value_type ;
00031     typedef typename MATRIX::iterator iterator ;
00032         
00033     MatrixColumn( MATRIX& object, int i ) : 
00034         parent(object), index(i), start(parent.begin() + i * parent.nrow() )
00035     {
00036         if( i < 0 || i >= parent.ncol() ) throw index_out_of_bounds() ;
00037     }
00038         
00039     MatrixColumn( const MatrixColumn& other ) : 
00040         parent(other.parent), index(other.index), start(other.start){} ;
00041         
00042     template <int RT, bool NA, typename T>
00043     MatrixColumn& operator=( const Rcpp::VectorBase<RT,NA,T>& rhs ){
00044         int n = size() ;
00045         const T& ref = rhs.get_ref() ;
00046         RCPP_LOOP_UNROLL(start,ref)
00047             return *this ;
00048     }
00049         
00050     MatrixColumn& operator=( const MatrixColumn& rhs ){
00051         int n = size() ;
00052         iterator rhs_start = rhs.start ; 
00053         RCPP_LOOP_UNROLL(start,rhs_start)
00054             return *this ;
00055     }
00056 
00057     inline Proxy operator[]( int i ){
00058         /* TODO: should we cache nrow and ncol */
00059         return start[i] ;
00060     }
00061         
00062     inline Proxy operator[]( int i ) const {
00063         /* TODO: should we cache nrow and ncol */
00064         return start[i] ;
00065     }
00066         
00067     inline iterator begin(){
00068         return start ;
00069     }
00070         
00071     inline iterator begin() const {
00072         return start ;
00073     }
00074         
00075     inline iterator end(){
00076         return start + parent.nrow() ;
00077     }
00078         
00079     inline iterator end() const {
00080         return start + parent.nrow() ;
00081     }
00082         
00083     inline int size() const {
00084         return parent.nrow() ;
00085     }
00086         
00087 private:
00088     MATRIX& parent; 
00089     int index ;     
00090     iterator start ;
00091 
00092 } ;
00093 
00094 #endif
 All Classes Namespaces Files Functions Variables Typedefs Enumerator Friends Defines