|
1 | | -// -*- mode: C++; c-indent-level: 4; c-basic-offset: 4; tab-width: 8 -*- |
| 1 | +// -*- mode: C++; c-indent-level: 4; c-basic-offset: 4; indent-tabs-mode: nil; -*- |
2 | 2 | // |
3 | 3 | // lower_tri.h: Rcpp R/C++ interface class library -- lower.tri |
4 | 4 | // |
5 | | -// Copyright (C) 2010 - 2011 Dirk Eddelbuettel and Romain Francois |
| 5 | +// Copyright (C) 2010 - 2017 Dirk Eddelbuettel and Romain Francois |
| 6 | +// Copyright (C) 2017 Dirk Eddelbuettel, Romain Francois, and Nathan Russell |
6 | 7 | // |
7 | 8 | // This file is part of Rcpp. |
8 | 9 | // |
|
22 | 23 | #ifndef Rcpp__sugar__lower_tri_h |
23 | 24 | #define Rcpp__sugar__lower_tri_h |
24 | 25 |
|
25 | | -namespace Rcpp{ |
26 | | -namespace sugar{ |
| 26 | +namespace Rcpp { |
| 27 | +namespace sugar { |
27 | 28 |
|
28 | | -template <int RTYPE, bool LHS_NA, typename LHS_T> |
29 | | -class LowerTri : public VectorBase< |
30 | | - LGLSXP , |
31 | | - false , |
32 | | - LowerTri<RTYPE,LHS_NA,LHS_T> |
33 | | -> { |
| 29 | +template <int RTYPE, bool NA, typename T> |
| 30 | +class LowerTri : public MatrixBase<LGLSXP, false, LowerTri<RTYPE, NA, T> > { |
34 | 31 | public: |
35 | | - typedef Rcpp::MatrixBase<RTYPE,LHS_NA,LHS_T> LHS_TYPE ; |
| 32 | + typedef Rcpp::MatrixBase<RTYPE, NA, T> MatBase; |
36 | 33 |
|
37 | | - LowerTri( const LHS_TYPE& lhs, bool diag) : |
38 | | - nr( lhs.nrow() ), nc( lhs.ncol() ), |
39 | | - getter( diag ? (&LowerTri::get_diag_true) : (&LowerTri::get_diag_false) ){} |
| 34 | + LowerTri(const T& lhs, bool diag) |
| 35 | + : nr(lhs.nrow()), |
| 36 | + nc(lhs.ncol()), |
| 37 | + getter(diag ? (&LowerTri::get_diag_true) : (&LowerTri::get_diag_false)) |
| 38 | + {} |
40 | 39 |
|
41 | | - // inline int operator[]( int index ) const { |
42 | | - // int i = Rcpp::internal::get_line( index, nr ) ; |
43 | | - // int j = Rcpp::internal::get_column( index, nr, i ) ; |
44 | | - // return get(i,j) ; |
45 | | - // } |
46 | | - inline int operator()( int i, int j ) const { |
47 | | - return get(i,j) ; |
48 | | - } |
| 40 | + inline int operator()(int i, int j) const { return get(i, j); } |
49 | 41 |
|
50 | | - inline R_xlen_t size() const { return static_cast<R_xlen_t>(nr) * nc ; } |
51 | | - inline int nrow() const { return nr; } |
52 | | - inline int ncol() const { return nc; } |
| 42 | + inline R_xlen_t size() const { return static_cast<R_xlen_t>(nr) * nc; } |
| 43 | + inline int nrow() const { return nr; } |
| 44 | + inline int ncol() const { return nc; } |
53 | 45 |
|
54 | 46 | private: |
55 | | - int nr, nc ; |
56 | | - typedef bool (LowerTri::*Method)(int,int) ; |
| 47 | + typedef bool (LowerTri::*Method)(int, int) const; |
57 | 48 |
|
58 | | - Method getter ; |
59 | | - inline bool get_diag_true( int i, int j ){ |
60 | | - return i <= j ; |
61 | | - } |
62 | | - inline bool get_diag_false( int i, int j ){ |
63 | | - return i < j ; |
64 | | - } |
65 | | - inline bool get( int i, int j){ |
66 | | - return (this->*getter)(i, j ) ; |
67 | | - } |
| 49 | + int nr, nc; |
| 50 | + Method getter; |
68 | 51 |
|
69 | | -} ; |
| 52 | + inline bool get_diag_true(int i, int j) const { return i >= j; } |
| 53 | + |
| 54 | + inline bool get_diag_false(int i, int j) const { return i > j; } |
| 55 | + |
| 56 | + inline bool get(int i, int j) const { return (this->*getter)(i, j); } |
| 57 | +}; |
70 | 58 |
|
71 | 59 | } // sugar |
72 | 60 |
|
73 | | -template <int RTYPE, bool LHS_NA, typename LHS_T> |
74 | | -inline sugar::LowerTri<RTYPE,LHS_NA,LHS_T> |
75 | | -lower_tri( const Rcpp::MatrixBase<RTYPE,LHS_NA,LHS_T>& lhs, bool diag = false){ |
76 | | - return sugar::LowerTri<RTYPE,LHS_NA,LHS_T>( lhs, diag ) ; |
| 61 | +template <int RTYPE, bool NA, typename T> |
| 62 | +inline sugar::LowerTri<RTYPE, NA, T> |
| 63 | +lower_tri(const Rcpp::MatrixBase<RTYPE, NA, T>& lhs, bool diag = false) { |
| 64 | + return sugar::LowerTri<RTYPE, NA, T>(lhs, diag); |
77 | 65 | } |
78 | 66 |
|
79 | 67 | } // Rcpp |
80 | 68 |
|
81 | | -#endif |
| 69 | +#endif // Rcpp__sugar__lower_tri_h |
0 commit comments