Rcpp Version 1.1.2
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 - 2025 Dirk Eddelbuettel and Romain Francois
6// Copyright (C) 2026 Dirk Eddelbuettel, Romain Francois and IƱaki Ucar
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__sugar__times_h
24#define Rcpp__sugar__times_h
25
26namespace Rcpp{
27namespace sugar{
28
29 template <int RTYPE, bool LHS_NA, typename LHS_T, bool RHS_NA, typename RHS_T >
30 class Times_Vector_Vector : public Rcpp::VectorBase<RTYPE, true , Times_Vector_Vector<RTYPE,LHS_NA,LHS_T,RHS_NA,RHS_T> > {
31 public:
35
38
39 Times_Vector_Vector( const LHS_TYPE& lhs_, const RHS_TYPE& rhs_ ) :
40 lhs(lhs_.get_ref()), rhs(rhs_.get_ref()) {
41 }
42
43 inline STORAGE operator[]( R_xlen_t i ) const {
44 STORAGE lhs_ = lhs[i] ;
45 if( traits::is_na<RTYPE>(lhs_) ) return lhs_ ;
46 STORAGE rhs_ = rhs[i] ;
47 return traits::is_na<RTYPE>(rhs_) ? rhs_ : RCPP_SAFE_MUL(lhs_, rhs_) ;
48 }
49
50 inline R_xlen_t size() const { return lhs.size() ; }
51
52 private:
53 const LHS_EXT& lhs ;
54 const RHS_EXT& rhs ;
55 } ;
56 // RTYPE = REALSXP
57 template <bool LHS_NA, typename LHS_T, bool RHS_NA, typename RHS_T >
58 class Times_Vector_Vector<REALSXP,LHS_NA,LHS_T,RHS_NA,RHS_T> :
59 public Rcpp::VectorBase<REALSXP, true , Times_Vector_Vector<REALSXP,LHS_NA,LHS_T,RHS_NA,RHS_T> > {
60 public:
63
66
67 Times_Vector_Vector( const LHS_TYPE& lhs_, const RHS_TYPE& rhs_ ) :
68 lhs(lhs_.get_ref()), rhs(rhs_.get_ref()) {
69 }
70
71 inline double operator[]( R_xlen_t i ) const {
72 return lhs[i] * rhs[i];
73 }
74
75 inline R_xlen_t size() const { return lhs.size() ; }
76
77 private:
78 const LHS_EXT& lhs ;
79 const RHS_EXT& rhs ;
80 } ;
81
82
83 // specialization LHS_NA = false
84 template <int RTYPE, typename LHS_T, bool RHS_NA, typename RHS_T >
85 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> > {
86 public:
90
93
94 Times_Vector_Vector( const LHS_TYPE& lhs_, const RHS_TYPE& rhs_ ) :
95 lhs(lhs_.get_ref()), rhs(rhs_.get_ref()){}
96
97 inline STORAGE operator[]( R_xlen_t i ) const {
98 STORAGE rhs_ = rhs[i] ;
99 if( traits::is_na<RTYPE>(rhs_) ) return rhs_ ;
100 return lhs[i] * rhs_;
101 }
102
103 inline R_xlen_t size() const { return lhs.size() ; }
104
105 private:
106 const LHS_EXT& lhs ;
107 const RHS_EXT& rhs ;
108 } ;
109 // RTYPE = REALSXP
110 template <typename LHS_T, bool RHS_NA, typename RHS_T >
111 class Times_Vector_Vector<REALSXP,false,LHS_T,RHS_NA,RHS_T> :
112 public Rcpp::VectorBase<REALSXP,true, Times_Vector_Vector<REALSXP,false,LHS_T,RHS_NA,RHS_T> > {
113 public:
116
119
120 Times_Vector_Vector( const LHS_TYPE& lhs_, const RHS_TYPE& rhs_ ) :
121 lhs(lhs_.get_ref()), rhs(rhs_.get_ref()){}
122
123 inline double operator[]( R_xlen_t i ) const {
124 return lhs[i] * rhs[i] ;
125 }
126
127 inline R_xlen_t size() const { return lhs.size() ; }
128
129 private:
130 const LHS_EXT& lhs ;
131 const RHS_EXT& rhs ;
132 } ;
133
134
135 // specialization for RHS_NA = false
136 template <int RTYPE, bool LHS_NA, typename LHS_T, typename RHS_T >
137 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> > {
138 public:
142
145
146 Times_Vector_Vector( const LHS_TYPE& lhs_, const RHS_TYPE& rhs_ ) :
147 lhs(lhs_.get_ref()), rhs(rhs_.get_ref()){}
148
149 inline STORAGE operator[]( R_xlen_t i ) const {
150 STORAGE lhs_ = lhs[i] ;
151 if( traits::is_na<RTYPE>(lhs_) ) return lhs_ ;
152 return lhs_ * rhs[i];
153 }
154
155 inline R_xlen_t size() const { return lhs.size() ; }
156
157 private:
158 const LHS_EXT& lhs ;
159 const RHS_EXT& rhs ;
160 } ;
161 // RTYPE = REALSXP
162 template <bool LHS_NA, typename LHS_T, typename RHS_T >
163 class Times_Vector_Vector<REALSXP,LHS_NA,LHS_T,false,RHS_T> :
164 public Rcpp::VectorBase<REALSXP, true , Times_Vector_Vector<REALSXP,LHS_NA,LHS_T,false,RHS_T> > {
165 public:
168
171
172 Times_Vector_Vector( const LHS_TYPE& lhs_, const RHS_TYPE& rhs_ ) :
173 lhs(lhs_.get_ref()), rhs(rhs_.get_ref()){}
174
175 inline double operator[]( R_xlen_t i ) const {
176 return lhs[i] * rhs[i] ;
177 }
178
179 inline R_xlen_t size() const { return lhs.size() ; }
180
181 private:
182 const LHS_EXT& lhs ;
183 const RHS_EXT& rhs ;
184 } ;
185
186 // specialization for RHS_NA = false and LHS_NA = false
187 template <int RTYPE, typename LHS_T, typename RHS_T >
188 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> > {
189 public:
193
196
197 Times_Vector_Vector( const LHS_TYPE& lhs_, const RHS_TYPE& rhs_ ) :
198 lhs(lhs_.get_ref()), rhs(rhs_.get_ref()){}
199
200 inline STORAGE operator[]( R_xlen_t i ) const {
201 return lhs[i] * rhs[i];
202 }
203
204 inline R_xlen_t size() const { return lhs.size() ; }
205
206 private:
207 const LHS_EXT& lhs ;
208 const RHS_EXT& rhs ;
209 } ;
210 // RTYPE = REALSXP
211 template <typename LHS_T, typename RHS_T >
212 class Times_Vector_Vector<REALSXP,false,LHS_T,false,RHS_T> :
213 public Rcpp::VectorBase<REALSXP, false , Times_Vector_Vector<REALSXP,false,LHS_T,false,RHS_T> > {
214 public:
217
220
221 Times_Vector_Vector( const LHS_TYPE& lhs_, const RHS_TYPE& rhs_ ) :
222 lhs(lhs_.get_ref()), rhs(rhs_.get_ref()){}
223
224 inline double operator[]( R_xlen_t i ) const {
225 return lhs[i] * rhs[i] ;
226 }
227
228 inline R_xlen_t size() const { return lhs.size() ; }
229
230 private:
231 const LHS_EXT& lhs ;
232 const RHS_EXT& rhs ;
233 } ;
234
235
236 template <int RTYPE, bool NA, typename T>
237 class Times_Vector_Primitive : public Rcpp::VectorBase<RTYPE,true, Times_Vector_Primitive<RTYPE,NA,T> > {
238 public:
241
243
245 lhs(lhs_.get_ref()), rhs(rhs_), rhs_na( Rcpp::traits::is_na<RTYPE>(rhs_) )
246 {}
247
248 inline STORAGE operator[]( R_xlen_t i ) const {
249 if( rhs_na ) return rhs ;
250 STORAGE x = lhs[i] ;
251 return Rcpp::traits::is_na<RTYPE>(x) ? x : RCPP_SAFE_MUL(x, rhs);
252 }
253
254 inline R_xlen_t size() const { return lhs.size() ; }
255
256 private:
257 const EXT& lhs ;
259 bool rhs_na ;
260 } ;
261 // RTYPE = REALSXP
262 template <bool NA, typename T>
263 class Times_Vector_Primitive<REALSXP,NA,T> :
264 public Rcpp::VectorBase<REALSXP,true, Times_Vector_Primitive<REALSXP,NA,T> > {
265 public:
268
269 Times_Vector_Primitive( const VEC_TYPE& lhs_, double rhs_ ) :
270 lhs(lhs_.get_ref()), rhs(rhs_)
271 {}
272
273 inline double operator[]( R_xlen_t i ) const {
274 return rhs * lhs[i] ;
275 }
276
277 inline R_xlen_t size() const { return lhs.size() ; }
278
279 private:
280 const EXT& lhs ;
281 double rhs ;
282 } ;
283
284
285 template <int RTYPE, typename T>
286 class Times_Vector_Primitive<RTYPE,false,T> : public Rcpp::VectorBase<RTYPE,false, Times_Vector_Primitive<RTYPE,false,T> > {
287 public:
290
292
294 lhs(lhs_.get_ref()), rhs(rhs_), rhs_na( Rcpp::traits::is_na<RTYPE>(rhs_) ) {}
295
296 inline STORAGE operator[]( R_xlen_t i ) const {
297 return rhs_na ? rhs : (rhs * lhs[i]);
298 }
299
300 inline R_xlen_t size() const { return lhs.size() ; }
301
302 private:
303 const EXT& lhs ;
305 bool rhs_na ;
306
307 } ;
308 // RTYPE = REALSXP
309 template <typename T>
310 class Times_Vector_Primitive<REALSXP,false,T> :
311 public Rcpp::VectorBase<REALSXP,false, Times_Vector_Primitive<REALSXP,false,T> > {
312 public:
315
316 Times_Vector_Primitive( const VEC_TYPE& lhs_, double rhs_ ) :
317 lhs(lhs_.get_ref()), rhs(rhs_) {}
318
319 inline double operator[]( R_xlen_t i ) const {
320 return rhs * lhs[i] ;
321 }
322
323 inline R_xlen_t size() const { return lhs.size() ; }
324
325 private:
326 const EXT& lhs ;
327 double rhs ;
328 } ;
329
330
331
332
333
334
335 // Vector * nona(primitive)
336 template <int RTYPE, bool NA, typename T>
337 class Times_Vector_Primitive_nona : public Rcpp::VectorBase<RTYPE,true, Times_Vector_Primitive_nona<RTYPE,NA,T> > {
338 public:
342
344 lhs(lhs_.get_ref()), rhs(rhs_)
345 {}
346
347 inline STORAGE operator[]( R_xlen_t i ) const {
348 STORAGE x = lhs[i] ;
349 return Rcpp::traits::is_na<RTYPE>(x) ? x : x * rhs;
350 }
351
352 inline R_xlen_t size() const { return lhs.size() ; }
353
354 private:
355 const EXT& lhs ;
357
358 } ;
359 // RTYPE = REALSXP
360 template <bool NA, typename T>
362 public Rcpp::VectorBase<REALSXP,true, Times_Vector_Primitive_nona<REALSXP,NA,T> > {
363 public:
366
367 Times_Vector_Primitive_nona( const VEC_TYPE& lhs_, double rhs_ ) :
368 lhs(lhs_.get_ref()), rhs(rhs_)
369 {}
370
371 inline double operator[]( R_xlen_t i ) const {
372 return lhs[i] * rhs ;
373 }
374
375 inline R_xlen_t size() const { return lhs.size() ; }
376
377 private:
378 const EXT& lhs ;
379 double rhs ;
380 } ;
381
382
383 template <int RTYPE, typename T>
384 class Times_Vector_Primitive_nona<RTYPE,false,T> : public Rcpp::VectorBase<RTYPE,false, Times_Vector_Primitive_nona<RTYPE,false,T> > {
385 public:
388
390
392 lhs(lhs_.get_ref()), rhs(rhs_) {}
393
394 inline STORAGE operator[]( R_xlen_t i ) const {
395 return rhs * lhs[i];
396 }
397
398 inline R_xlen_t size() const { return lhs.size() ; }
399
400 private:
401 const EXT& lhs ;
403
404 } ;
405 template <typename T>
406 class Times_Vector_Primitive_nona<REALSXP,false,T> :
407 public Rcpp::VectorBase<REALSXP,false, Times_Vector_Primitive_nona<REALSXP,false,T> > {
408 public:
411
412 Times_Vector_Primitive_nona( const VEC_TYPE& lhs_, double rhs_ ) :
413 lhs(lhs_.get_ref()), rhs(rhs_) {}
414
415 inline double operator[]( R_xlen_t i ) const {
416 return rhs * lhs[i] ;
417 }
418
419 inline R_xlen_t size() const { return lhs.size() ; }
420
421 private:
422 const EXT& lhs ;
423 double rhs ;
424 } ;
425
426}
427
428template <int RTYPE,bool NA, typename T, typename U>
431 const VectorBase<RTYPE,NA,T>& lhs,
432 const U &rhs
433) {
435}
436
437
438template <int RTYPE,bool NA, typename T, typename U>
439inline 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
441 const U &rhs,
442 const VectorBase<RTYPE,NA,T>& lhs
443) {
445}
446
447
448
449template <int RTYPE,bool NA, typename T, typename U>
450inline 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
452 const VectorBase<RTYPE,NA,T>& lhs,
453 const typename sugar::NonaPrimitive< U > &rhs
454) {
456}
457
458template <int RTYPE,bool NA, typename T, typename U>
459inline 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
461 const typename sugar::NonaPrimitive< U > &rhs,
462 const VectorBase<RTYPE,NA,T>& lhs
463) {
465}
466
467
468template <int RTYPE,bool LHS_NA, typename LHS_T, bool RHS_NA, typename RHS_T>
469inline sugar::Times_Vector_Vector<
470 RTYPE ,
471 LHS_NA, LHS_T,
472 RHS_NA, RHS_T
473 >
477) {
479 RTYPE,
480 LHS_NA, LHS_T,
481 RHS_NA, RHS_T
482 >( lhs, rhs ) ;
483}
484
485}
486
487#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:267
Rcpp::VectorBase< REALSXP, NA, T > VEC_TYPE
Definition times.h:266
Times_Vector_Primitive(const VEC_TYPE &lhs_, double rhs_)
Definition times.h:269
Rcpp::traits::Extractor< REALSXP, false, T >::type EXT
Definition times.h:314
Times_Vector_Primitive(const VEC_TYPE &lhs_, double rhs_)
Definition times.h:316
Rcpp::VectorBase< REALSXP, false, T > VEC_TYPE
Definition times.h:313
Rcpp::traits::Extractor< RTYPE, false, T >::type EXT
Definition times.h:291
traits::storage_type< RTYPE >::type STORAGE
Definition times.h:289
Rcpp::VectorBase< RTYPE, false, T > VEC_TYPE
Definition times.h:288
Times_Vector_Primitive(const VEC_TYPE &lhs_, STORAGE rhs_)
Definition times.h:293
Rcpp::VectorBase< REALSXP, NA, T > VEC_TYPE
Definition times.h:364
Rcpp::traits::Extractor< REALSXP, NA, T >::type EXT
Definition times.h:365
Times_Vector_Primitive_nona(const VEC_TYPE &lhs_, double rhs_)
Definition times.h:367
Times_Vector_Primitive_nona(const VEC_TYPE &lhs_, double rhs_)
Definition times.h:412
Rcpp::VectorBase< REALSXP, false, T > VEC_TYPE
Definition times.h:409
Rcpp::traits::Extractor< REALSXP, false, T >::type EXT
Definition times.h:410
traits::storage_type< RTYPE >::type STORAGE
Definition times.h:387
Times_Vector_Primitive_nona(const VEC_TYPE &lhs_, STORAGE rhs_)
Definition times.h:391
Rcpp::traits::Extractor< RTYPE, false, T >::type EXT
Definition times.h:389
Rcpp::VectorBase< RTYPE, false, T > VEC_TYPE
Definition times.h:386
Rcpp::traits::Extractor< RTYPE, NA, T >::type EXT
Definition times.h:341
STORAGE operator[](R_xlen_t i) const
Definition times.h:347
traits::storage_type< RTYPE >::type STORAGE
Definition times.h:340
Times_Vector_Primitive_nona(const VEC_TYPE &lhs_, STORAGE rhs_)
Definition times.h:343
Rcpp::VectorBase< RTYPE, NA, T > VEC_TYPE
Definition times.h:339
Rcpp::traits::Extractor< RTYPE, NA, T >::type EXT
Definition times.h:242
Rcpp::VectorBase< RTYPE, NA, T > VEC_TYPE
Definition times.h:239
traits::storage_type< RTYPE >::type STORAGE
Definition times.h:240
Times_Vector_Primitive(const VEC_TYPE &lhs_, STORAGE rhs_)
Definition times.h:244
STORAGE operator[](R_xlen_t i) const
Definition times.h:248
Rcpp::traits::Extractor< REALSXP, RHS_NA, RHS_T >::type RHS_EXT
Definition times.h:65
Times_Vector_Vector(const LHS_TYPE &lhs_, const RHS_TYPE &rhs_)
Definition times.h:67
Rcpp::traits::Extractor< REALSXP, LHS_NA, LHS_T >::type LHS_EXT
Definition times.h:64
Rcpp::VectorBase< REALSXP, RHS_NA, RHS_T > RHS_TYPE
Definition times.h:62
Rcpp::VectorBase< REALSXP, LHS_NA, LHS_T > LHS_TYPE
Definition times.h:61
Times_Vector_Vector(const LHS_TYPE &lhs_, const RHS_TYPE &rhs_)
Definition times.h:172
Rcpp::traits::Extractor< REALSXP, LHS_NA, LHS_T >::type LHS_EXT
Definition times.h:169
Rcpp::traits::Extractor< REALSXP, false, RHS_T >::type RHS_EXT
Definition times.h:170
Rcpp::VectorBase< REALSXP, LHS_NA, LHS_T > LHS_TYPE
Definition times.h:166
Rcpp::traits::Extractor< REALSXP, false, LHS_T >::type LHS_EXT
Definition times.h:117
Times_Vector_Vector(const LHS_TYPE &lhs_, const RHS_TYPE &rhs_)
Definition times.h:120
Rcpp::traits::Extractor< REALSXP, RHS_NA, RHS_T >::type RHS_EXT
Definition times.h:118
Rcpp::VectorBase< REALSXP, RHS_NA, RHS_T > RHS_TYPE
Definition times.h:115
Rcpp::traits::Extractor< REALSXP, false, LHS_T >::type LHS_EXT
Definition times.h:218
Rcpp::traits::Extractor< REALSXP, false, RHS_T >::type RHS_EXT
Definition times.h:219
Times_Vector_Vector(const LHS_TYPE &lhs_, const RHS_TYPE &rhs_)
Definition times.h:221
Times_Vector_Vector(const LHS_TYPE &lhs_, const RHS_TYPE &rhs_)
Definition times.h:146
Rcpp::traits::Extractor< RTYPE, false, RHS_T >::type RHS_EXT
Definition times.h:144
Rcpp::traits::Extractor< RTYPE, LHS_NA, LHS_T >::type LHS_EXT
Definition times.h:143
Times_Vector_Vector(const LHS_TYPE &lhs_, const RHS_TYPE &rhs_)
Definition times.h:94
Rcpp::VectorBase< RTYPE, RHS_NA, RHS_T > RHS_TYPE
Definition times.h:89
Rcpp::traits::Extractor< RTYPE, RHS_NA, RHS_T >::type RHS_EXT
Definition times.h:92
Rcpp::traits::Extractor< RTYPE, false, LHS_T >::type LHS_EXT
Definition times.h:91
Rcpp::traits::Extractor< RTYPE, false, LHS_T >::type LHS_EXT
Definition times.h:194
Rcpp::traits::Extractor< RTYPE, false, RHS_T >::type RHS_EXT
Definition times.h:195
Times_Vector_Vector(const LHS_TYPE &lhs_, const RHS_TYPE &rhs_)
Definition times.h:197
traits::storage_type< RTYPE >::type STORAGE
Definition times.h:32
R_xlen_t size() const
Definition times.h:50
Rcpp::VectorBase< RTYPE, RHS_NA, RHS_T > RHS_TYPE
Definition times.h:34
Rcpp::traits::Extractor< RTYPE, LHS_NA, LHS_T >::type LHS_EXT
Definition times.h:36
Rcpp::traits::Extractor< RTYPE, RHS_NA, RHS_T >::type RHS_EXT
Definition times.h:37
Times_Vector_Vector(const LHS_TYPE &lhs_, const RHS_TYPE &rhs_)
Definition times.h:39
STORAGE operator[](R_xlen_t i) const
Definition times.h:43
Rcpp::VectorBase< RTYPE, LHS_NA, LHS_T > LHS_TYPE
Definition times.h:33
traits used to dispatch wrap
Definition Date.h:27
bool is_na(typename storage_type< RTYPE >::type)
Definition is_na.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:430
static Na_Proxy NA
Definition Na_Proxy.h:52
#define RCPP_SAFE_MUL(a, b)
Definition safe_math.h:35