Rcpp Version 0.9.10
as_vector.h
Go to the documentation of this file.
00001 // -*- mode: C++; c-indent-level: 4; c-basic-offset: 4; tab-width: 8 -*-
00002 //
00003 // as_vector.h: Rcpp R/C++ interface class library -- as_vector( sugar matrix expression )
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__sugar__matrix_as_vector_h
00023 #define Rcpp__sugar__matrix_as_vector_h
00024 
00025 namespace Rcpp{ 
00026 namespace internal{
00027 
00028 template <int RTYPE, bool NA, typename T>
00029 inline Rcpp::Vector<RTYPE> 
00030 as_vector__impl( MatrixBase<RTYPE,NA,T>& t, Rcpp::traits::false_type ){
00031     T& ref = t.get_ref() ;
00032     int nc = ref.ncol(), nr = ref.nrow() ;
00033     Vector<RTYPE> out (nr*nc) ;
00034     int k =0; 
00035     for( int col_index=0; col_index<nc; col_index++)
00036         for( int row_index=0; row_index<nr; row_index++, k++)
00037             out[k] = ref( row_index, col_index ) ;
00038     
00039     return out ;
00040 }
00041 
00042 template <int RTYPE, bool NA, typename T>
00043 inline Rcpp::Vector<RTYPE> 
00044 as_vector__impl( MatrixBase<RTYPE,NA,T>& t, Rcpp::traits::true_type ){
00045     Matrix<RTYPE>& ref = t.get_ref() ;
00046     int size = ref.ncol()*ref.nrow() ;
00047     typename Rcpp::Vector<RTYPE>::iterator first(static_cast<const Rcpp::Vector<RTYPE>&>(ref).begin())  ;
00048     return Vector<RTYPE>(first, first+size );
00049 } 
00050 
00051 } // internal
00052  
00053 template <int RTYPE, bool NA, typename T>
00054 inline Rcpp::Vector<RTYPE> 
00055 as_vector( const MatrixBase<RTYPE,NA,T>& t ){
00056     return internal::as_vector__impl( const_cast< MatrixBase<RTYPE,NA,T>& >(t), typename Rcpp::traits::same_type< T , Matrix<RTYPE> >() ) ;
00057 }
00058 
00059 } // Rcpp
00060 #endif
00061 
 All Classes Namespaces Files Functions Variables Typedefs Enumerator Friends Defines