Rcpp Version 1.0.9
as_vector.h
Go to the documentation of this file.
1 // -*- mode: C++; c-indent-level: 4; c-basic-offset: 4; tab-width: 8 -*-
2 //
3 // as_vector.h: Rcpp R/C++ interface class library -- as_vector( sugar matrix expression )
4 //
5 // Copyright (C) 2010 - 2014 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__sugar__matrix_as_vector_h
23 #define Rcpp__sugar__matrix_as_vector_h
24 
25 namespace Rcpp{
26 namespace internal{
27 
28 template <int RTYPE, bool NA, typename T>
31  T& ref = t.get_ref() ;
32  int nc = ref.ncol(), nr = ref.nrow() ;
33  Vector<RTYPE> out (static_cast<R_xlen_t>(nr) * nc) ;
34  R_xlen_t k =0;
35  for( int col_index=0; col_index<nc; col_index++)
36  for( int row_index=0; row_index<nr; row_index++, k++)
37  out[k] = ref( row_index, col_index ) ;
38 
39  return out ;
40 }
41 
42 template <int RTYPE, bool NA, typename T>
45  Matrix<RTYPE>& ref = t.get_ref() ;
46  R_xlen_t size = static_cast<R_xlen_t>(ref.ncol())*ref.nrow() ;
47  typename Rcpp::Vector<RTYPE>::const_iterator first(static_cast<const Rcpp::Vector<RTYPE>&>(ref).begin()) ;
48  return Vector<RTYPE>(first, first+size );
49 }
50 
51 } // internal
52 
53 template <int RTYPE, bool NA, typename T>
56  return internal::as_vector__impl( const_cast< MatrixBase<RTYPE,NA,T>& >(t), typename Rcpp::traits::same_type< T , Matrix<RTYPE> >() ) ;
57 }
58 
59 } // Rcpp
60 #endif
61 
MATRIX & get_ref()
Definition: MatrixBase.h:39
int ncol() const
Definition: Matrix.h:94
int nrow() const
Definition: Matrix.h:97
traits::r_vector_const_iterator< RTYPE, StoragePolicy >::type const_iterator
Definition: Vector.h:47
Rcpp::Vector< RTYPE > as_vector__impl(MatrixBase< RTYPE, NA, T > &t, Rcpp::traits::false_type)
Definition: as_vector.h:30
Rcpp API.
Definition: algo.h:28
Rcpp::Vector< RTYPE > as_vector(const MatrixBase< RTYPE, NA, T > &t)
Definition: as_vector.h:55