Rcpp Version 1.0.14
Loading...
Searching...
No Matches
times.h
Go to the documentation of this file.
1// -*- mode: C++; c-indent-level: 4; c-basic-offset: 4; tab-width: 4 -*-
2//
3// times.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__times_h
23#define Rcpp__sugar__times_h
24
25namespace Rcpp{
26namespace sugar{
27
28 template <int RTYPE, bool LHS_NA, typename LHS_T, bool RHS_NA, typename RHS_T >
29 class Times_Vector_Vector : public Rcpp::VectorBase<RTYPE, true , Times_Vector_Vector<RTYPE,LHS_NA,LHS_T,RHS_NA,RHS_T> > {
30 public:
34
37
41
42 inline STORAGE operator[]( R_xlen_t i ) const {
43 STORAGE lhs_ = lhs[i] ;
44 if( traits::is_na<RTYPE>(lhs_) ) return lhs_ ;
45 STORAGE rhs_ = rhs[i] ;
46 return traits::is_na<RTYPE>(rhs_) ? rhs_ : (lhs_ * rhs_) ;
47 }
48
49 inline R_xlen_t size() const { return lhs.size() ; }
50
51 private:
52 const LHS_EXT& lhs ;
53 const RHS_EXT& rhs ;
54 } ;
55 // RTYPE = REALSXP
56 template <bool LHS_NA, typename LHS_T, bool RHS_NA, typename RHS_T >
58 public Rcpp::VectorBase<REALSXP, true , Times_Vector_Vector<REALSXP,LHS_NA,LHS_T,RHS_NA,RHS_T> > {
59 public:
62
65
69
70 inline double operator[]( R_xlen_t i ) const {
71 return lhs[i] * rhs[i];
72 }
73
74 inline R_xlen_t size() const { return lhs.size() ; }
75
76 private:
77 const LHS_EXT& lhs ;
78 const RHS_EXT& rhs ;
79 } ;
80
81
82 // specialization LHS_NA = false
83 template <int RTYPE, typename LHS_T, bool RHS_NA, typename RHS_T >
84 class Times_Vector_Vector<RTYPE,false,LHS_T,RHS_NA,RHS_T> : public Rcpp::VectorBase<RTYPE,true, Times_Vector_Vector<RTYPE,false,LHS_T,RHS_NA,RHS_T> > {
85 public:
89
92
95
96 inline STORAGE operator[]( R_xlen_t i ) const {
97 STORAGE rhs_ = rhs[i] ;
98 if( traits::is_na<RTYPE>(rhs_) ) return rhs_ ;
99 return lhs[i] * rhs_ ;
100 }
101
102 inline R_xlen_t size() const { return lhs.size() ; }
103
104 private:
105 const LHS_EXT& lhs ;
106 const RHS_EXT& rhs ;
107 } ;
108 // RTYPE = REALSXP
109 template <typename LHS_T, bool RHS_NA, typename RHS_T >
111 public Rcpp::VectorBase<REALSXP,true, Times_Vector_Vector<REALSXP,false,LHS_T,RHS_NA,RHS_T> > {
112 public:
115
118
121
122 inline double operator[]( R_xlen_t i ) const {
123 return lhs[i] * rhs[i] ;
124 }
125
126 inline R_xlen_t size() const { return lhs.size() ; }
127
128 private:
129 const LHS_EXT& lhs ;
130 const RHS_EXT& rhs ;
131 } ;
132
133
134 // specialization for RHS_NA = false
135 template <int RTYPE, bool LHS_NA, typename LHS_T, typename RHS_T >
136 class Times_Vector_Vector<RTYPE,LHS_NA,LHS_T,false,RHS_T> : public Rcpp::VectorBase<RTYPE, true , Times_Vector_Vector<RTYPE,LHS_NA,LHS_T,false,RHS_T> > {
137 public:
141
144
147
148 inline STORAGE operator[]( R_xlen_t i ) const {
149 STORAGE lhs_ = lhs[i] ;
150 if( traits::is_na<RTYPE>(lhs_) ) return lhs_ ;
151 return lhs_ * rhs[i] ;
152 }
153
154 inline R_xlen_t size() const { return lhs.size() ; }
155
156 private:
157 const LHS_EXT& lhs ;
158 const RHS_EXT& rhs ;
159 } ;
160 // RTYPE = REALSXP
161 template <bool LHS_NA, typename LHS_T, typename RHS_T >
163 public Rcpp::VectorBase<REALSXP, true , Times_Vector_Vector<REALSXP,LHS_NA,LHS_T,false,RHS_T> > {
164 public:
167
170
173
174 inline double operator[]( R_xlen_t i ) const {
175 return lhs[i] * rhs[i] ;
176 }
177
178 inline R_xlen_t size() const { return lhs.size() ; }
179
180 private:
181 const LHS_EXT& lhs ;
182 const RHS_EXT& rhs ;
183 } ;
184
185 // specialization for RHS_NA = false and LHS_NA = false
186 template <int RTYPE, typename LHS_T, typename RHS_T >
187 class Times_Vector_Vector<RTYPE,false,LHS_T,false,RHS_T> : public Rcpp::VectorBase<RTYPE, false , Times_Vector_Vector<RTYPE,false,LHS_T,false,RHS_T> > {
188 public:
192
195
198
199 inline STORAGE operator[]( R_xlen_t i ) const {
200 return lhs[i] * rhs[i] ;
201 }
202
203 inline R_xlen_t size() const { return lhs.size() ; }
204
205 private:
206 const LHS_EXT& lhs ;
207 const RHS_EXT& rhs ;
208 } ;
209 // RTYPE = REALSXP
210 template <typename LHS_T, typename RHS_T >
212 public Rcpp::VectorBase<REALSXP, false , Times_Vector_Vector<REALSXP,false,LHS_T,false,RHS_T> > {
213 public:
216
219
222
223 inline double operator[]( R_xlen_t i ) const {
224 return lhs[i] * rhs[i] ;
225 }
226
227 inline R_xlen_t size() const { return lhs.size() ; }
228
229 private:
230 const LHS_EXT& lhs ;
231 const RHS_EXT& rhs ;
232 } ;
233
234
235 template <int RTYPE, bool NA, typename T>
236 class Times_Vector_Primitive : public Rcpp::VectorBase<RTYPE,true, Times_Vector_Primitive<RTYPE,NA,T> > {
237 public:
240
242
244 lhs(lhs_.get_ref()), rhs(rhs_), rhs_na( Rcpp::traits::is_na<RTYPE>(rhs_) )
245 {}
246
247 inline STORAGE operator[]( R_xlen_t i ) const {
248 if( rhs_na ) return rhs ;
249 STORAGE x = lhs[i] ;
250 return Rcpp::traits::is_na<RTYPE>(x) ? x : (x * rhs) ;
251 }
252
253 inline R_xlen_t size() const { return lhs.size() ; }
254
255 private:
256 const EXT& lhs ;
258 bool rhs_na ;
259 } ;
260 // RTYPE = REALSXP
261 template <bool NA, typename T>
263 public Rcpp::VectorBase<REALSXP,true, Times_Vector_Primitive<REALSXP,NA,T> > {
264 public:
267
269 lhs(lhs_.get_ref()), rhs(rhs_)
270 {}
271
272 inline double operator[]( R_xlen_t i ) const {
273 return rhs * lhs[i] ;
274 }
275
276 inline R_xlen_t size() const { return lhs.size() ; }
277
278 private:
279 const EXT& lhs ;
280 double rhs ;
281 } ;
282
283
284 template <int RTYPE, typename T>
285 class Times_Vector_Primitive<RTYPE,false,T> : public Rcpp::VectorBase<RTYPE,false, Times_Vector_Primitive<RTYPE,false,T> > {
286 public:
289
291
293 lhs(lhs_.get_ref()), rhs(rhs_), rhs_na( Rcpp::traits::is_na<RTYPE>(rhs_) ) {}
294
295 inline STORAGE operator[]( R_xlen_t i ) const {
296 return rhs_na ? rhs : (rhs * lhs[i] ) ;
297 }
298
299 inline R_xlen_t size() const { return lhs.size() ; }
300
301 private:
302 const EXT& lhs ;
304 bool rhs_na ;
305
306 } ;
307 // RTYPE = REALSXP
308 template <typename T>
310 public Rcpp::VectorBase<REALSXP,false, Times_Vector_Primitive<REALSXP,false,T> > {
311 public:
314
316 lhs(lhs_.get_ref()), rhs(rhs_) {}
317
318 inline double operator[]( R_xlen_t i ) const {
319 return rhs * lhs[i] ;
320 }
321
322 inline R_xlen_t size() const { return lhs.size() ; }
323
324 private:
325 const EXT& lhs ;
326 double rhs ;
327 } ;
328
329
330
331
332
333
334 // Vector * nona(primitive)
335 template <int RTYPE, bool NA, typename T>
336 class Times_Vector_Primitive_nona : public Rcpp::VectorBase<RTYPE,true, Times_Vector_Primitive_nona<RTYPE,NA,T> > {
337 public:
341
345
346 inline STORAGE operator[]( R_xlen_t i ) const {
347 STORAGE x = lhs[i] ;
348 return Rcpp::traits::is_na<RTYPE>(x) ? x : (x * rhs) ;
349 }
350
351 inline R_xlen_t size() const { return lhs.size() ; }
352
353 private:
354 const EXT& lhs ;
356
357 } ;
358 // RTYPE = REALSXP
359 template <bool NA, typename T>
361 public Rcpp::VectorBase<REALSXP,true, Times_Vector_Primitive_nona<REALSXP,NA,T> > {
362 public:
365
367 lhs(lhs_.get_ref()), rhs(rhs_)
368 {}
369
370 inline double operator[]( R_xlen_t i ) const {
371 return lhs[i] * rhs ;
372 }
373
374 inline R_xlen_t size() const { return lhs.size() ; }
375
376 private:
377 const EXT& lhs ;
378 double rhs ;
379 } ;
380
381
382 template <int RTYPE, typename T>
383 class Times_Vector_Primitive_nona<RTYPE,false,T> : public Rcpp::VectorBase<RTYPE,false, Times_Vector_Primitive_nona<RTYPE,false,T> > {
384 public:
387
389
392
393 inline STORAGE operator[]( R_xlen_t i ) const {
394 return rhs * lhs[i] ;
395 }
396
397 inline R_xlen_t size() const { return lhs.size() ; }
398
399 private:
400 const EXT& lhs ;
402
403 } ;
404 template <typename T>
406 public Rcpp::VectorBase<REALSXP,false, Times_Vector_Primitive_nona<REALSXP,false,T> > {
407 public:
410
413
414 inline double operator[]( R_xlen_t i ) const {
415 return rhs * lhs[i] ;
416 }
417
418 inline R_xlen_t size() const { return lhs.size() ; }
419
420 private:
421 const EXT& lhs ;
422 double rhs ;
423 } ;
424
425}
426
427template <int RTYPE,bool NA, typename T, typename U>
430 const VectorBase<RTYPE,NA,T>& lhs,
431 const U &rhs
432) {
434}
435
436
437template <int RTYPE,bool NA, typename T, typename U>
438inline typename traits::enable_if<traits::is_convertible<typename traits::remove_const_and_reference<U>::type, typename traits::storage_type<RTYPE>::type>::value, sugar::Times_Vector_Primitive< RTYPE , NA , T > >::type
440 const U &rhs,
441 const VectorBase<RTYPE,NA,T>& lhs
442) {
444}
445
446
447
448template <int RTYPE,bool NA, typename T, typename U>
449inline typename traits::enable_if<traits::is_convertible<typename traits::remove_const_and_reference<U>::type, typename traits::storage_type<RTYPE>::type>::value, sugar::Times_Vector_Primitive_nona<RTYPE,NA,T> >::type
451 const VectorBase<RTYPE,NA,T>& lhs,
452 const typename sugar::NonaPrimitive< U > &rhs
453) {
455}
456
457template <int RTYPE,bool NA, typename T, typename U>
458inline typename traits::enable_if<traits::is_convertible<typename traits::remove_const_and_reference<U>::type, typename traits::storage_type<RTYPE>::type>::value, sugar::Times_Vector_Primitive_nona< RTYPE , NA , T > >::type
460 const typename sugar::NonaPrimitive< U > &rhs,
461 const VectorBase<RTYPE,NA,T>& lhs
462) {
464}
465
466
467template <int RTYPE,bool LHS_NA, typename LHS_T, bool RHS_NA, typename RHS_T>
468inline sugar::Times_Vector_Vector<
469 RTYPE ,
470 LHS_NA, LHS_T,
472 >
476) {
478 RTYPE,
479 LHS_NA, LHS_T,
481 >( lhs, rhs ) ;
482}
483
484}
485
486#endif
Times_Vector_Vector< RTYPE, LHS_NA, LHS_T, RHS_NA, RHS_T > & get_ref()
Definition VectorBase.h:37
Rcpp::traits::Extractor< REALSXP, NA, T >::type EXT
Definition times.h:266
Rcpp::VectorBase< REALSXP, NA, T > VEC_TYPE
Definition times.h:265
Times_Vector_Primitive(const VEC_TYPE &lhs_, double rhs_)
Definition times.h:268
Rcpp::traits::Extractor< REALSXP, false, T >::type EXT
Definition times.h:313
Times_Vector_Primitive(const VEC_TYPE &lhs_, double rhs_)
Definition times.h:315
Rcpp::VectorBase< REALSXP, false, T > VEC_TYPE
Definition times.h:312
Rcpp::traits::Extractor< RTYPE, false, T >::type EXT
Definition times.h:290
traits::storage_type< RTYPE >::type STORAGE
Definition times.h:288
Rcpp::VectorBase< RTYPE, false, T > VEC_TYPE
Definition times.h:287
Times_Vector_Primitive(const VEC_TYPE &lhs_, STORAGE rhs_)
Definition times.h:292
Rcpp::VectorBase< REALSXP, NA, T > VEC_TYPE
Definition times.h:363
Rcpp::traits::Extractor< REALSXP, NA, T >::type EXT
Definition times.h:364
Times_Vector_Primitive_nona(const VEC_TYPE &lhs_, double rhs_)
Definition times.h:366
Times_Vector_Primitive_nona(const VEC_TYPE &lhs_, double rhs_)
Definition times.h:411
Rcpp::VectorBase< REALSXP, false, T > VEC_TYPE
Definition times.h:408
Rcpp::traits::Extractor< REALSXP, false, T >::type EXT
Definition times.h:409
traits::storage_type< RTYPE >::type STORAGE
Definition times.h:386
Times_Vector_Primitive_nona(const VEC_TYPE &lhs_, STORAGE rhs_)
Definition times.h:390
Rcpp::traits::Extractor< RTYPE, false, T >::type EXT
Definition times.h:388
Rcpp::VectorBase< RTYPE, false, T > VEC_TYPE
Definition times.h:385
Rcpp::traits::Extractor< RTYPE, NA, T >::type EXT
Definition times.h:340
STORAGE operator[](R_xlen_t i) const
Definition times.h:346
traits::storage_type< RTYPE >::type STORAGE
Definition times.h:339
Times_Vector_Primitive_nona(const VEC_TYPE &lhs_, STORAGE rhs_)
Definition times.h:342
Rcpp::VectorBase< RTYPE, NA, T > VEC_TYPE
Definition times.h:338
Rcpp::traits::Extractor< RTYPE, NA, T >::type EXT
Definition times.h:241
Rcpp::VectorBase< RTYPE, NA, T > VEC_TYPE
Definition times.h:238
traits::storage_type< RTYPE >::type STORAGE
Definition times.h:239
Times_Vector_Primitive(const VEC_TYPE &lhs_, STORAGE rhs_)
Definition times.h:243
STORAGE operator[](R_xlen_t i) const
Definition times.h:247
Rcpp::traits::Extractor< REALSXP, RHS_NA, RHS_T >::type RHS_EXT
Definition times.h:64
Times_Vector_Vector(const LHS_TYPE &lhs_, const RHS_TYPE &rhs_)
Definition times.h:66
Rcpp::traits::Extractor< REALSXP, LHS_NA, LHS_T >::type LHS_EXT
Definition times.h:63
Rcpp::VectorBase< REALSXP, RHS_NA, RHS_T > RHS_TYPE
Definition times.h:61
Rcpp::VectorBase< REALSXP, LHS_NA, LHS_T > LHS_TYPE
Definition times.h:60
Times_Vector_Vector(const LHS_TYPE &lhs_, const RHS_TYPE &rhs_)
Definition times.h:171
Rcpp::traits::Extractor< REALSXP, LHS_NA, LHS_T >::type LHS_EXT
Definition times.h:168
Rcpp::traits::Extractor< REALSXP, false, RHS_T >::type RHS_EXT
Definition times.h:169
Rcpp::VectorBase< REALSXP, LHS_NA, LHS_T > LHS_TYPE
Definition times.h:165
Rcpp::traits::Extractor< REALSXP, false, LHS_T >::type LHS_EXT
Definition times.h:116
Times_Vector_Vector(const LHS_TYPE &lhs_, const RHS_TYPE &rhs_)
Definition times.h:119
Rcpp::traits::Extractor< REALSXP, RHS_NA, RHS_T >::type RHS_EXT
Definition times.h:117
Rcpp::VectorBase< REALSXP, RHS_NA, RHS_T > RHS_TYPE
Definition times.h:114
Rcpp::traits::Extractor< REALSXP, false, LHS_T >::type LHS_EXT
Definition times.h:217
Rcpp::traits::Extractor< REALSXP, false, RHS_T >::type RHS_EXT
Definition times.h:218
Times_Vector_Vector(const LHS_TYPE &lhs_, const RHS_TYPE &rhs_)
Definition times.h:220
Times_Vector_Vector(const LHS_TYPE &lhs_, const RHS_TYPE &rhs_)
Definition times.h:145
Rcpp::traits::Extractor< RTYPE, false, RHS_T >::type RHS_EXT
Definition times.h:143
Rcpp::traits::Extractor< RTYPE, LHS_NA, LHS_T >::type LHS_EXT
Definition times.h:142
Times_Vector_Vector(const LHS_TYPE &lhs_, const RHS_TYPE &rhs_)
Definition times.h:93
Rcpp::VectorBase< RTYPE, RHS_NA, RHS_T > RHS_TYPE
Definition times.h:88
Rcpp::traits::Extractor< RTYPE, RHS_NA, RHS_T >::type RHS_EXT
Definition times.h:91
Rcpp::traits::Extractor< RTYPE, false, LHS_T >::type LHS_EXT
Definition times.h:90
Rcpp::traits::Extractor< RTYPE, false, LHS_T >::type LHS_EXT
Definition times.h:193
Rcpp::traits::Extractor< RTYPE, false, RHS_T >::type RHS_EXT
Definition times.h:194
Times_Vector_Vector(const LHS_TYPE &lhs_, const RHS_TYPE &rhs_)
Definition times.h:196
traits::storage_type< RTYPE >::type STORAGE
Definition times.h:31
R_xlen_t size() const
Definition times.h:49
Rcpp::VectorBase< RTYPE, RHS_NA, RHS_T > RHS_TYPE
Definition times.h:33
Rcpp::traits::Extractor< RTYPE, LHS_NA, LHS_T >::type LHS_EXT
Definition times.h:35
Rcpp::traits::Extractor< RTYPE, RHS_NA, RHS_T >::type RHS_EXT
Definition times.h:36
Times_Vector_Vector(const LHS_TYPE &lhs_, const RHS_TYPE &rhs_)
Definition times.h:38
STORAGE operator[](R_xlen_t i) const
Definition times.h:42
Rcpp::VectorBase< RTYPE, LHS_NA, LHS_T > LHS_TYPE
Definition times.h:32
Rcpp API.
Definition algo.h:28
sugar::IsNa< RTYPE, NA, T > is_na(const Rcpp::VectorBase< RTYPE, NA, T > &t)
Definition is_na.h:91
traits::enable_if< traits::is_convertible< typenametraits::remove_const_and_reference< U >::type, typenametraits::storage_type< RTYPE >::type >::value, sugar::Times_Vector_Primitive< RTYPE, NA, T > >::type operator*(const VectorBase< RTYPE, NA, T > &lhs, const U &rhs)
Definition times.h:429
T as(SEXP x)
Definition as.h:151
static Na_Proxy NA
Definition Na_Proxy.h:52