Rcpp Version 0.10.3
 All Classes Namespaces Files Functions Variables Typedefs Enumerator Friends Macros
divides.h
Go to the documentation of this file.
1 // -*- mode: C++; c-indent-level: 4; c-basic-offset: 4; tab-width: 8 -*-
2 //
3 // divides.h: Rcpp R/C++ interface class library -- operator-
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__divides_h
23 #define Rcpp__sugar__divides_h
24 
25 namespace Rcpp{
26 namespace sugar{
27 
28  template <int RTYPE, bool LHS_NA, typename LHS_T, bool RHS_NA, typename RHS_T >
29  class Divides_Vector_Vector : public Rcpp::VectorBase<RTYPE,true, Divides_Vector_Vector<RTYPE,LHS_NA,LHS_T,RHS_NA,RHS_T> > {
30  public:
36 
37  Divides_Vector_Vector( const LHS_TYPE& lhs_, const RHS_TYPE& rhs_ ) :
38  lhs(lhs_.get_ref()), rhs(rhs_.get_ref()) {}
39 
40  inline STORAGE operator[]( int i ) const {
41  STORAGE x = lhs[i] ;
42  if( Rcpp::traits::is_na<RTYPE>( x ) ) return x ;
43  STORAGE y = rhs[i] ;
44  return Rcpp::traits::is_na<RTYPE>( y ) ? y : ( x / y ) ;
45  }
46 
47  inline int size() const { return lhs.size() ; }
48 
49  private:
50  const LHS_EXT& lhs ;
51  const RHS_EXT& rhs ;
52  } ;
53  // RTYPE = REALSXP
54  template <bool LHS_NA, typename LHS_T, bool RHS_NA, typename RHS_T >
55  class Divides_Vector_Vector<REALSXP,LHS_NA,LHS_T,RHS_NA,RHS_T> :
56  public Rcpp::VectorBase<REALSXP,true, Divides_Vector_Vector<REALSXP,LHS_NA,LHS_T,RHS_NA,RHS_T> > {
57  public:
62 
63  Divides_Vector_Vector( const LHS_TYPE& lhs_, const RHS_TYPE& rhs_ ) :
64  lhs(lhs_.get_ref()), rhs(rhs_.get_ref()) {}
65 
66  inline double operator[]( int i ) const {
67  return lhs[i] / rhs[i] ;
68  }
69 
70  inline int size() const { return lhs.size() ; }
71 
72  private:
73  const LHS_EXT& lhs ;
74  const RHS_EXT& rhs ;
75  } ;
76 
77 
78  template <int RTYPE, typename LHS_T, bool RHS_NA, typename RHS_T >
79  class Divides_Vector_Vector<RTYPE,false,LHS_T,RHS_NA,RHS_T> : public Rcpp::VectorBase<RTYPE,true, Divides_Vector_Vector<RTYPE,false,LHS_T,RHS_NA,RHS_T> > {
80  public:
86 
87  Divides_Vector_Vector( const LHS_TYPE& lhs_, const RHS_TYPE& rhs_ ) :
88  lhs(lhs_.get_ref()), rhs(rhs_.get_ref()) {}
89 
90  inline STORAGE operator[]( int i ) const {
91  STORAGE y = rhs[i] ;
92  if( Rcpp::traits::is_na<RTYPE>( y ) ) return y ;
93  return lhs[i] / y ;
94  }
95 
96  inline int size() const { return lhs.size() ; }
97 
98  private:
99  const LHS_EXT& lhs ;
100  const RHS_EXT& rhs ;
101  } ;
102  // RTYPE = REALSXP
103  template <typename LHS_T, bool RHS_NA, typename RHS_T >
104  class Divides_Vector_Vector<REALSXP,false,LHS_T,RHS_NA,RHS_T> :
105  public Rcpp::VectorBase<REALSXP,true, Divides_Vector_Vector<REALSXP,false,LHS_T,RHS_NA,RHS_T> > {
106  public:
111 
112  Divides_Vector_Vector( const LHS_TYPE& lhs_, const RHS_TYPE& rhs_ ) :
113  lhs(lhs_.get_ref()), rhs(rhs_.get_ref()) {}
114 
115  inline double operator[]( int i ) const {
116  return lhs[i] / rhs[i] ;
117  }
118 
119  inline int size() const { return lhs.size() ; }
120 
121  private:
122  const LHS_EXT& lhs ;
123  const RHS_EXT& rhs ;
124  } ;
125 
126 
127  template <int RTYPE, bool LHS_NA, typename LHS_T, typename RHS_T >
128  class Divides_Vector_Vector<RTYPE,LHS_NA,LHS_T,false,RHS_T> :
129  public Rcpp::VectorBase<RTYPE,true, Divides_Vector_Vector<RTYPE,LHS_NA,LHS_T,false,RHS_T> > {
130  public:
136 
137  Divides_Vector_Vector( const LHS_TYPE& lhs_, const RHS_TYPE& rhs_ ) :
138  lhs(lhs_.get_ref()), rhs(rhs_.get_ref()) {}
139 
140  inline STORAGE operator[]( int i ) const {
141  STORAGE x = lhs[i] ;
142  if( Rcpp::traits::is_na<RTYPE>( x ) ) return x ;
143  return x / rhs[i] ;
144  }
145  inline int size() const { return lhs.size() ; }
146 
147  private:
148  const LHS_EXT& lhs ;
149  const RHS_EXT& rhs ;
150  } ;
151  // RTYPE = REALSXP
152  template <bool LHS_NA, typename LHS_T, typename RHS_T >
153  class Divides_Vector_Vector<REALSXP,LHS_NA,LHS_T,false,RHS_T> :
154  public Rcpp::VectorBase<REALSXP,true, Divides_Vector_Vector<REALSXP,LHS_NA,LHS_T,false,RHS_T> > {
155  public:
160 
161  Divides_Vector_Vector( const LHS_TYPE& lhs_, const RHS_TYPE& rhs_ ) :
162  lhs(lhs_.get_ref()), rhs(rhs_.get_ref()) {}
163 
164  inline double operator[]( int i ) const {
165  return lhs[i] / rhs[i] ;
166  }
167  inline int size() const { return lhs.size() ; }
168 
169  private:
170  const LHS_EXT& lhs ;
171  const RHS_EXT& rhs ;
172  } ;
173 
174 
175  template <int RTYPE, typename LHS_T, typename RHS_T >
176  class Divides_Vector_Vector<RTYPE,false,LHS_T,false,RHS_T> :
177  public Rcpp::VectorBase<RTYPE,false, Divides_Vector_Vector<RTYPE,false,LHS_T,false,RHS_T> > {
178  public:
184 
185  Divides_Vector_Vector( const LHS_TYPE& lhs_, const RHS_TYPE& rhs_ ) :
186  lhs(lhs_.get_ref()), rhs(rhs_.get_ref()) {}
187 
188  inline STORAGE operator[]( int i ) const {
189  return lhs[i] / rhs[i] ;
190  }
191 
192  inline int size() const { return lhs.size() ; }
193 
194  private:
195  const LHS_EXT& lhs ;
196  const RHS_EXT& rhs ;
197  } ;
198  // RTYPE : REALSXP
199  template <typename LHS_T, typename RHS_T >
200  class Divides_Vector_Vector<REALSXP,false,LHS_T,false,RHS_T> :
201  public Rcpp::VectorBase<REALSXP,false, Divides_Vector_Vector<REALSXP,false,LHS_T,false,RHS_T> > {
202  public:
207 
208  Divides_Vector_Vector( const LHS_TYPE& lhs_, const RHS_TYPE& rhs_ ) :
209  lhs(lhs_.get_ref()), rhs(rhs_.get_ref()) {}
210 
211  inline double operator[]( int i ) const {
212  return lhs[i] / rhs[i] ;
213  }
214 
215  inline int size() const { return lhs.size() ; }
216 
217  private:
218  const LHS_EXT& lhs ;
219  const RHS_EXT& rhs ;
220  } ;
221 
222 
223 
224 
225  template <int RTYPE, bool NA, typename T>
227  public Rcpp::VectorBase<RTYPE,true, Divides_Vector_Primitive<RTYPE,NA,T> > {
228  public:
232 
233  Divides_Vector_Primitive( const VEC_TYPE& lhs_, STORAGE rhs_ ) :
234  lhs(lhs_.get_ref()), rhs(rhs_), rhs_na( Rcpp::traits::is_na<RTYPE>(rhs_) ) {
235  }
236 
237  inline STORAGE operator[]( int i ) const {
238  if(rhs_na) return rhs ;
239  STORAGE x = lhs[i] ;
240  return Rcpp::traits::is_na<RTYPE>(x) ? x : (x / rhs) ;
241  }
242 
243  inline int size() const { return lhs.size() ; }
244 
245  private:
246  const VEC_EXT& lhs ;
248  bool rhs_na ;
249  } ;
250  // RTYPE : REALSXP
251  template <bool NA, typename T>
252  class Divides_Vector_Primitive<REALSXP,NA,T> :
253  public Rcpp::VectorBase<REALSXP,true, Divides_Vector_Primitive<REALSXP,NA,T> > {
254  public:
257 
258  Divides_Vector_Primitive( const VEC_TYPE& lhs_, double rhs_ ) :
259  lhs(lhs_.get_ref()), rhs(rhs_) {
260  }
261 
262  inline double operator[]( int i ) const {
263  return lhs[i] / rhs ;
264  }
265 
266  inline int size() const { return lhs.size() ; }
267 
268  private:
269  const VEC_EXT& lhs ;
270  double rhs ;
271  } ;
272 
273 
274 
275  template <int RTYPE, typename T>
276  class Divides_Vector_Primitive<RTYPE,false,T> :
277  public Rcpp::VectorBase<RTYPE,true, Divides_Vector_Primitive<RTYPE,false,T> > {
278  public:
282 
283  Divides_Vector_Primitive( const VEC_TYPE& lhs_, STORAGE rhs_ ) :
284  lhs(lhs_.get_ref()), rhs(rhs_), rhs_na( Rcpp::traits::is_na<RTYPE>(rhs_) ) {}
285 
286  inline STORAGE operator[]( int i ) const {
287  if( rhs_na ) return rhs ;
288  STORAGE x = lhs[i] ;
289  return Rcpp::traits::is_na<RTYPE>(x) ? x : (x / rhs) ;
290  }
291  inline int size() const { return lhs.size() ; }
292 
293  private:
294  const VEC_EXT& lhs ;
296  bool rhs_na ;
297  } ;
298  // RTYPE = REALSXP
299  template <typename T>
300  class Divides_Vector_Primitive<REALSXP,false,T> :
301  public Rcpp::VectorBase<REALSXP,true, Divides_Vector_Primitive<REALSXP,false,T> > {
302  public:
305 
306  Divides_Vector_Primitive( const VEC_TYPE& lhs_, double rhs_ ) :
307  lhs(lhs_), rhs(rhs_){}
308 
309  inline double operator[]( int i ) const {
310  return lhs[i] / rhs ;
311  }
312  inline int size() const { return lhs.size() ; }
313 
314  private:
315  const VEC_EXT& lhs ;
316  double rhs ;
317  } ;
318 
319 
320 
321  template <int RTYPE, bool NA, typename T>
323  public Rcpp::VectorBase<RTYPE,true, Divides_Primitive_Vector<RTYPE,NA,T> > {
324  public:
328 
329  Divides_Primitive_Vector( STORAGE lhs_, const VEC_TYPE& rhs_ ) :
330  lhs(lhs_), rhs(rhs_.get_ref()), lhs_na( Rcpp::traits::is_na<RTYPE>(lhs_) ) {}
331 
332  inline STORAGE operator[]( int i ) const {
333  if( lhs_na ) return lhs ;
334  STORAGE x = rhs[i] ;
335  return Rcpp::traits::is_na<RTYPE>(x) ? x : (lhs / x) ;
336  }
337  inline int size() const { return rhs.size() ; }
338  private:
340  const VEC_EXT& rhs ;
341  bool lhs_na ;
342  } ;
343  // RTYPE = REALSXP
344  template <bool NA, typename T>
345  class Divides_Primitive_Vector<REALSXP,NA,T> :
346  public Rcpp::VectorBase<REALSXP,true, Divides_Primitive_Vector<REALSXP,NA,T> > {
347  public:
350 
351  Divides_Primitive_Vector( double lhs_, const VEC_TYPE& rhs_ ) :
352  lhs(lhs_), rhs(rhs_.get_ref()) {}
353 
354  inline double operator[]( int i ) const {
355  return lhs / rhs[i] ;
356  }
357  inline int size() const { return rhs.size() ; }
358  private:
359  double lhs ;
360  const VEC_EXT& rhs ;
361  } ;
362 
363 
364 
365  template <int RTYPE, typename T>
366  class Divides_Primitive_Vector<RTYPE,false,T> :
367  public Rcpp::VectorBase<RTYPE,true, Divides_Primitive_Vector<RTYPE,false,T> > {
368  public:
372 
373  Divides_Primitive_Vector( STORAGE lhs_, const VEC_TYPE& rhs_ ) :
374  lhs(lhs_), rhs(rhs_.get_ref()), lhs_na( Rcpp::traits::is_na<RTYPE>(lhs_) ) {}
375 
376  inline STORAGE operator[]( int i ) const {
377  if( lhs_na ) return lhs ;
378  return lhs / rhs[i] ;
379  }
380  inline int size() const { return rhs.size() ; }
381 
382  private:
384  const VEC_EXT& rhs ;
385  bool lhs_na ;
386  } ;
387  // RTYPE = REALSXP
388  template <typename T>
389  class Divides_Primitive_Vector<REALSXP,false,T> :
390  public Rcpp::VectorBase<REALSXP,true, Divides_Primitive_Vector<REALSXP,false,T> > {
391  public:
394 
395  Divides_Primitive_Vector( double lhs_, const VEC_TYPE& rhs_ ) :
396  lhs(lhs_), rhs(rhs_.get_ref()) {}
397 
398  inline double operator[]( int i ) const {
399  return lhs / rhs[i] ;
400  }
401  inline int size() const { return rhs.size() ; }
402 
403  private:
404  double lhs ;
405  const VEC_EXT& rhs ;
406  } ;
407 
408 
409 }
410 }
411 
412 template <int RTYPE,bool NA, typename T>
415  const Rcpp::VectorBase<RTYPE,NA,T>& lhs,
417 ) {
419 }
420 
421 
422 template <int RTYPE,bool NA, typename T>
427 ) {
429 }
430 
431 template <int RTYPE,bool LHS_NA, typename LHS_T, bool RHS_NA, typename RHS_T>
433  RTYPE ,
434  LHS_NA, LHS_T,
435  RHS_NA, RHS_T
436  >
440 ) {
442  RTYPE,
443  LHS_NA,LHS_T,
444  RHS_NA,RHS_T
445  >( lhs, rhs ) ;
446 }
447 
448 #endif