Rcpp Version 1.0.9
MatrixBase.h
Go to the documentation of this file.
1 // -*- mode: C++; c-indent-level: 4; c-basic-offset: 4; indent-tabs-mode: nil; -*-
2 //
3 // MatrixBase.h: Rcpp R/C++ interface class library --
4 //
5 // Copyright (C) 2010 - 2016 Dirk Eddelbuettel and Romain Francois
6 // Copyright (C) 2016 Dirk Eddelbuettel and Romain Francois and Nathan Russell
7 //
8 // This file is part of Rcpp.
9 //
10 // Rcpp is free software: you can redistribute it and/or modify it
11 // under the terms of the GNU General Public License as published by
12 // the Free Software Foundation, either version 2 of the License, or
13 // (at your option) any later version.
14 //
15 // Rcpp is distributed in the hope that it will be useful, but
16 // WITHOUT ANY WARRANTY; without even the implied warranty of
17 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 // GNU General Public License for more details.
19 //
20 // You should have received a copy of the GNU General Public License
21 // along with Rcpp. If not, see <http://www.gnu.org/licenses/>.
22 
23 #ifndef Rcpp__vector__MatrixBase_h
24 #define Rcpp__vector__MatrixBase_h
25 
27 
28 namespace Rcpp{
29 
31  template <int RTYPE, bool na, typename MATRIX>
33  public:
34  struct r_type : traits::integral_constant<int,RTYPE>{} ;
35  struct r_matrix_interface {} ;
36  struct can_have_na : traits::integral_constant<bool,na>{} ;
38 
39  MATRIX& get_ref(){
40  return static_cast<MATRIX&>(*this) ;
41  }
42 
43  inline stored_type operator()( int i, int j) const {
44  return static_cast<const MATRIX*>(this)->operator()(i, j) ;
45  }
46 
47  inline R_xlen_t size() const { return static_cast<const MATRIX&>(*this).size() ; }
48  inline R_xlen_t nrow() const { return static_cast<const MATRIX&>(*this).nrow() ; }
49  inline R_xlen_t ncol() const { return static_cast<const MATRIX&>(*this).ncol() ; }
50 
51  static MATRIX eye(int n) {
52  const bool enabled =
56 
58  }
59 
60  static MATRIX ones(int n) {
61  const bool enabled =
65 
66  MATRIX res(n, n);
67  std::fill(
68  res.begin(), res.end(),
70  );
71  return res;
72  }
73 
74  static MATRIX zeros(int n) {
75  const bool enabled =
79 
80  return MATRIX(n, n);
81  }
82 
83  class iterator {
84  public:
86  typedef stored_type* pointer ;
87  typedef R_xlen_t difference_type ;
89  typedef std::random_access_iterator_tag iterator_category ;
90 
91  iterator( const MatrixBase& object_, R_xlen_t index_ ) :
92  object(object_), i(0), j(0), nr(object_.nrow()), nc(object_.ncol()) {
93 
94  update_index( index_) ;
95  }
96 
97  iterator( const iterator& other) :
98  object(other.object), i(other.i), j(other.j), nr(other.nr), nc(other.nc){}
99 
100  inline iterator& operator++(){
101  i++ ;
102  if( i == nr ){
103  j++ ;
104  i=0 ;
105  }
106  return *this ;
107  }
108  inline iterator operator++(int){
109  iterator orig(*this) ;
110  ++(*this) ;
111  return orig ;
112  }
113 
114  inline iterator& operator--(){
115  i-- ;
116  if( i == -1 ){
117  j-- ;
118  i = nr - 1 ;
119  }
120  return *this ;
121  }
122  inline iterator operator--(int){
123  iterator orig(*this) ;
124  --(*this) ;
125  return orig ;
126  }
127 
129  return iterator( object, i + (j*nr) + n ) ;
130  }
132  return iterator( object, i + (j*nr) - n ) ;
133  }
134 
136  update_index( i + j*nr + n );
137  return *this ;
138  }
140  update_index( i + j*nr - n );
141  return *this ;
142  }
143 
144  inline reference operator*() {
145  return object(i,j) ;
146  }
147  inline pointer operator->(){
148  return &object(i,j) ;
149  }
150 
151  inline bool operator==( const iterator& y) const {
152  return ( i == y.i && j == y.j ) ;
153  }
154  inline bool operator!=( const iterator& y) const {
155  return ( i != y.i || j != y.j ) ;
156  }
157  inline bool operator<( const iterator& other ) const {
158  return index() < other.index() ;
159  }
160  inline bool operator>( const iterator& other ) const {
161  return index() > other.index() ;
162  }
163  inline bool operator<=( const iterator& other ) const {
164  return index() <= other.index() ;
165  }
166  inline bool operator>=( const iterator& other ) const {
167  return index() >= other.index() ;
168  }
169 
170  inline difference_type operator-(const iterator& other) const {
171  return index() - other.index() ;
172  }
173 
174 
175  private:
177  int i, j ;
178  int nr, nc ;
179 
180  inline void update_index( int index_ ){
181  i = internal::get_line( index_, nr );
182  j = internal::get_column( index_, nr, i ) ;
183  }
184 
185  inline R_xlen_t index() const {
186  return i + nr * j ;
187  }
188 
189  } ;
190 
191  inline iterator begin() const { return iterator(*this, 0) ; }
192  inline iterator end() const { return iterator(*this, size() ) ; }
193 
194  } ;
195 
196 } // namespace Rcpp
197 #endif
iterator operator+(difference_type n) const
Definition: MatrixBase.h:128
iterator operator--(int)
Definition: MatrixBase.h:122
bool operator==(const iterator &y) const
Definition: MatrixBase.h:151
iterator operator++(int)
Definition: MatrixBase.h:108
iterator(const MatrixBase &object_, R_xlen_t index_)
Definition: MatrixBase.h:91
const MatrixBase & object
Definition: MatrixBase.h:176
std::random_access_iterator_tag iterator_category
Definition: MatrixBase.h:89
iterator(const iterator &other)
Definition: MatrixBase.h:97
bool operator>=(const iterator &other) const
Definition: MatrixBase.h:166
bool operator<(const iterator &other) const
Definition: MatrixBase.h:157
bool operator>(const iterator &other) const
Definition: MatrixBase.h:160
iterator & operator-=(difference_type n)
Definition: MatrixBase.h:139
bool operator<=(const iterator &other) const
Definition: MatrixBase.h:163
iterator operator-(difference_type n) const
Definition: MatrixBase.h:131
difference_type operator-(const iterator &other) const
Definition: MatrixBase.h:170
bool operator!=(const iterator &y) const
Definition: MatrixBase.h:154
void update_index(int index_)
Definition: MatrixBase.h:180
R_xlen_t index() const
Definition: MatrixBase.h:185
iterator & operator+=(difference_type n)
Definition: MatrixBase.h:135
iterator begin() const
Definition: MatrixBase.h:191
static MATRIX zeros(int n)
Definition: MatrixBase.h:74
stored_type operator()(int i, int j) const
Definition: MatrixBase.h:43
iterator end() const
Definition: MatrixBase.h:192
R_xlen_t nrow() const
Definition: MatrixBase.h:48
static MATRIX ones(int n)
Definition: MatrixBase.h:60
static MATRIX eye(int n)
Definition: MatrixBase.h:51
traits::storage_type< RTYPE >::type stored_type
Definition: MatrixBase.h:37
R_xlen_t ncol() const
Definition: MatrixBase.h:49
MATRIX & get_ref()
Definition: MatrixBase.h:39
R_xlen_t size() const
Definition: MatrixBase.h:47
int get_column(int index, int nr)
Definition: tools.h:32
int get_line(int index, int nr)
Definition: tools.h:28
Rcpp API.
Definition: algo.h:28
sugar::diag_result_type_trait< T >::type diag(const T &t)
Definition: diag.h:83