Rcpp Version 0.12.14
diag.h
Go to the documentation of this file.
1 // -*- mode: C++; c-indent-level: 4; c-basic-offset: 4; tab-width: 8 -*-
2 //
3 // diag.h: Rcpp R/C++ interface class library -- diag
4 //
5 // Copyright (C) 2010 - 2011 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__diag_h
23 #define Rcpp__sugar__diag_h
24 
25 namespace Rcpp{
26 namespace sugar{
27 
28 template <int RTYPE, bool NA, typename T>
29 class Diag_Extractor : public Rcpp::VectorBase< RTYPE ,NA, Diag_Extractor<RTYPE,NA,T> > {
30 public:
33 
34  Diag_Extractor( const MAT_TYPE& object_ ) : object(object_), n(0) {
35  int nr = object.nrow() ;
36  int nc = object.ncol() ;
37  n = (nc < nr ) ? nc : nr ;
38  }
39 
40  inline STORAGE operator[]( int i ) const {
41  return object( i, i ) ;
42  }
43  inline R_xlen_t size() const { return n; }
44 
45 private:
46  const MAT_TYPE& object ;
47  R_xlen_t n ;
48 } ;
49 
50 
51 template <int RTYPE, bool NA, typename T>
52 class Diag_Maker : public Rcpp::MatrixBase< RTYPE ,NA, Diag_Maker<RTYPE,NA,T> > {
53 public:
56 
57  Diag_Maker( const VEC_TYPE& object_ ) : object(object_), n(object_.size()) {}
58 
59  inline STORAGE operator()( int i, int j ) const {
60  return (i==j) ? object[i] : 0 ;
61  }
62  inline R_xlen_t size() const { return static_cast<R_xlen_t>(n) * n; }
63  inline int ncol() const { return n; }
64  inline int nrow() const { return n; }
65 
66 private:
67  const VEC_TYPE& object ;
68  int n ;
69 } ;
70 
71 template <typename T> struct diag_result_type_trait{
72  typedef typename Rcpp::traits::if_<
77 } ;
78 
79 } // sugar
80 
81 template <typename T>
83 diag( const T& t ){
84  return typename sugar::diag_result_type_trait<T>::type( t ) ;
85 }
86 
87 
88 } // Rcpp
89 #endif
90 
Rcpp::MatrixBase< RTYPE, NA, T > MAT_TYPE
Definition: diag.h:31
const MAT_TYPE & object
Definition: diag.h:46
STORAGE operator[](int i) const
Definition: diag.h:40
Diag_Extractor(const MAT_TYPE &object_)
Definition: diag.h:34
R_xlen_t size() const
Definition: diag.h:43
int ncol() const
Definition: diag.h:63
STORAGE operator()(int i, int j) const
Definition: diag.h:59
Rcpp::traits::if_< Rcpp::traits::matrix_interface< T >::value, Diag_Extractor< T::r_type::value, T::can_have_na::value, T >, Diag_Maker< T::r_type::value, T::can_have_na::value, T > >::type type
Definition: diag.h:76
Rcpp::VectorBase< RTYPE, NA, T > VEC_TYPE
Definition: diag.h:54
const VEC_TYPE & object
Definition: diag.h:67
R_xlen_t size() const
Definition: diag.h:62
sugar::diag_result_type_trait< T >::type diag(const T &t)
Definition: diag.h:83
int nrow() const
Definition: diag.h:64
Rcpp::traits::storage_type< RTYPE >::type STORAGE
Definition: diag.h:32
Rcpp API.
Definition: algo.h:28
Rcpp::traits::storage_type< RTYPE >::type STORAGE
Definition: diag.h:55
Diag_Maker(const VEC_TYPE &object_)
Definition: diag.h:57