Rcpp Version 1.1.2
Loading...
Searching...
No Matches
minus.h
Go to the documentation of this file.
1// -*- mode: C++; c-indent-level: 4; c-basic-offset: 4; tab-width: 8 -*-
2//
3// minus.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__minus_h
24#define Rcpp__sugar__minus_h
25
26namespace Rcpp{
27namespace sugar{
28
29 template <int RTYPE, bool LHS_NA, typename LHS_T, bool RHS_NA, typename RHS_T >
31 public Rcpp::VectorBase<RTYPE,true, Minus_Vector_Vector<RTYPE,LHS_NA,LHS_T,RHS_NA,RHS_T> > {
32 public:
38
39 Minus_Vector_Vector( const LHS_TYPE& lhs_, const RHS_TYPE& rhs_ ) :
40 lhs(lhs_.get_ref()), rhs(rhs_.get_ref()) {}
41
42 inline STORAGE operator[]( R_xlen_t i ) const {
43 STORAGE x = lhs[i] ;
44 if( Rcpp::traits::is_na<RTYPE>( x ) ) return x ;
45 STORAGE y = rhs[i] ;
46 return Rcpp::traits::is_na<RTYPE>( y ) ? y : RCPP_SAFE_SUB(x, y);
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 >
57 class Minus_Vector_Vector<REALSXP,LHS_NA,LHS_T,RHS_NA,RHS_T> :
58 public Rcpp::VectorBase<REALSXP,true, Minus_Vector_Vector<REALSXP,LHS_NA,LHS_T,RHS_NA,RHS_T> > {
59 public:
64
65 Minus_Vector_Vector( const LHS_TYPE& lhs_, const RHS_TYPE& rhs_ ) :
66 lhs(lhs_.get_ref()), rhs(rhs_.get_ref()) {}
67
68 inline double operator[]( R_xlen_t i ) const {
69 return lhs[i] - rhs[i] ;
70 }
71
72 inline R_xlen_t size() const { return lhs.size() ; }
73
74 private:
75 const LHS_EXT& lhs ;
76 const RHS_EXT& rhs ;
77 } ;
78
79
80 template <int RTYPE, typename LHS_T, bool RHS_NA, typename RHS_T >
81 class Minus_Vector_Vector<RTYPE,false,LHS_T,RHS_NA,RHS_T> : public Rcpp::VectorBase<RTYPE,true, Minus_Vector_Vector<RTYPE,false,LHS_T,RHS_NA,RHS_T> > {
82 public:
88
89 Minus_Vector_Vector( const LHS_TYPE& lhs_, const RHS_TYPE& rhs_ ) :
90 lhs(lhs_.get_ref()), rhs(rhs_.get_ref()) {}
91
92 inline STORAGE operator[]( R_xlen_t i ) const {
93 STORAGE y = rhs[i] ;
94 if( Rcpp::traits::is_na<RTYPE>( y ) ) return y ;
95 return lhs[i] - y;
96 }
97
98 inline R_xlen_t size() const { return lhs.size() ; }
99
100 private:
101 const LHS_EXT& lhs ;
102 const RHS_EXT& rhs ;
103 } ;
104 // RTYPE = REALSXP
105 template <typename LHS_T, bool RHS_NA, typename RHS_T >
106 class Minus_Vector_Vector<REALSXP,false,LHS_T,RHS_NA,RHS_T> :
107 public Rcpp::VectorBase<REALSXP,true, Minus_Vector_Vector<REALSXP,false,LHS_T,RHS_NA,RHS_T> > {
108 public:
113
114 Minus_Vector_Vector( const LHS_TYPE& lhs_, const RHS_TYPE& rhs_ ) :
115 lhs(lhs_.get_ref()), rhs(rhs_.get_ref()) {}
116
117 inline double operator[]( R_xlen_t i ) const {
118 return lhs[i] - rhs[i] ;
119 }
120
121 inline R_xlen_t size() const { return lhs.size() ; }
122
123 private:
124 const LHS_EXT& lhs ;
125 const RHS_EXT& rhs ;
126 } ;
127
128
129 template <int RTYPE, bool LHS_NA, typename LHS_T, typename RHS_T >
130 class Minus_Vector_Vector<RTYPE,LHS_NA,LHS_T,false,RHS_T> : public Rcpp::VectorBase<RTYPE,true, Minus_Vector_Vector<RTYPE,LHS_NA,LHS_T,false,RHS_T> > {
131 public:
137
138 Minus_Vector_Vector( const LHS_TYPE& lhs_, const RHS_TYPE& rhs_ ) :
139 lhs(lhs_.get_ref()), rhs(rhs_.get_ref()) {}
140
141 inline STORAGE operator[]( R_xlen_t i ) const {
142 STORAGE x = lhs[i] ;
143 if( Rcpp::traits::is_na<RTYPE>( x ) ) return x ;
144 return x - rhs[i];
145 }
146
147 inline R_xlen_t size() const { return lhs.size() ; }
148
149 private:
150 const LHS_EXT& lhs ;
151 const RHS_EXT& rhs ;
152 } ;
153 // RTYPE = REALSXP
154 template <bool LHS_NA, typename LHS_T, typename RHS_T >
155 class Minus_Vector_Vector<REALSXP,LHS_NA,LHS_T,false,RHS_T> :
156 public Rcpp::VectorBase<REALSXP,true, Minus_Vector_Vector<REALSXP,LHS_NA,LHS_T,false,RHS_T> > {
157 public:
162
163 Minus_Vector_Vector( const LHS_TYPE& lhs_, const RHS_TYPE& rhs_ ) :
164 lhs(lhs_.get_ref()), rhs(rhs_.get_ref()) {}
165
166 inline double operator[]( R_xlen_t i ) const {
167 return lhs[i] - rhs[i] ;
168 }
169
170 inline R_xlen_t size() const { return lhs.size() ; }
171
172 private:
173 const LHS_EXT& lhs ;
174 const RHS_EXT& rhs ;
175 } ;
176
177
178 template <int RTYPE, typename LHS_T, typename RHS_T >
179 class Minus_Vector_Vector<RTYPE,false,LHS_T,false,RHS_T> :
180 public Rcpp::VectorBase<RTYPE,false, Minus_Vector_Vector<RTYPE,false,LHS_T,false,RHS_T> > {
181 public:
187
188 Minus_Vector_Vector( const LHS_TYPE& lhs_, const RHS_TYPE& rhs_ ) :
189 lhs(lhs_.get_ref()), rhs(rhs_.get_ref()) {}
190
191 inline STORAGE operator[]( R_xlen_t i ) const {
192 return lhs[i] - rhs[i];
193 }
194
195 inline R_xlen_t size() const { return lhs.size() ; }
196
197 private:
198 const LHS_EXT& lhs ;
199 const RHS_EXT& rhs ;
200 } ;
201 template <typename LHS_T, typename RHS_T >
202 class Minus_Vector_Vector<REALSXP,false,LHS_T,false,RHS_T> :
203 public Rcpp::VectorBase<REALSXP,false, Minus_Vector_Vector<REALSXP,false,LHS_T,false,RHS_T> > {
204 public:
209
210 Minus_Vector_Vector( const LHS_TYPE& lhs_, const RHS_TYPE& rhs_ ) :
211 lhs(lhs_.get_ref()), rhs(rhs_.get_ref()) {}
212
213 inline double operator[]( R_xlen_t i ) const {
214 return lhs[i] - rhs[i] ;
215 }
216
217 inline R_xlen_t size() const { return lhs.size() ; }
218
219 private:
220 const LHS_EXT& lhs ;
221 const RHS_EXT& rhs ;
222 } ;
223
224
225
226
227
228 template <int RTYPE, bool NA, typename T>
230 public Rcpp::VectorBase<RTYPE,true, Minus_Vector_Primitive<RTYPE,NA,T> > {
231 public:
235
237 lhs(lhs_.get_ref()), rhs(rhs_), rhs_na( Rcpp::traits::is_na<RTYPE>(rhs_) ) {}
238
239 inline STORAGE operator[]( R_xlen_t i ) const {
240 if( rhs_na ) return rhs ;
241 STORAGE x = lhs[i] ;
242 return Rcpp::traits::is_na<RTYPE>(x) ? x : RCPP_SAFE_SUB(x, rhs);
243 }
244
245 inline R_xlen_t size() const { return lhs.size() ; }
246
247 private:
248 const VEC_EXT& lhs ;
250 bool rhs_na ;
251 } ;
252 template <bool NA, typename T>
253 class Minus_Vector_Primitive<REALSXP,NA,T> :
254 public Rcpp::VectorBase<REALSXP,true, Minus_Vector_Primitive<REALSXP,NA,T> > {
255 public:
258
259 Minus_Vector_Primitive( const VEC_TYPE& lhs_, double rhs_ ) :
260 lhs(lhs_.get_ref()), rhs(rhs_){}
261
262 inline double operator[]( R_xlen_t i ) const {
263 return lhs[i] - rhs ;
264 }
265
266 inline R_xlen_t size() const { return lhs.size() ; }
267
268 private:
269 const VEC_EXT& lhs ;
270 double rhs ;
271 } ;
272
273
274 template <int RTYPE, typename T>
275 class Minus_Vector_Primitive<RTYPE,false,T> :
276 public Rcpp::VectorBase<RTYPE,true, Minus_Vector_Primitive<RTYPE,false,T> > {
277 public:
281
283 lhs(lhs_.get_ref()), rhs(rhs_), rhs_na( Rcpp::traits::is_na<RTYPE>(rhs_) ) {}
284
285 inline STORAGE operator[]( R_xlen_t i ) const {
286 if( rhs_na ) return rhs ;
287 STORAGE x = lhs[i] ;
288 return Rcpp::traits::is_na<RTYPE>(x) ? x : x - rhs;
289 }
290
291 inline R_xlen_t size() const { return lhs.size() ; }
292
293 private:
294 const VEC_EXT& lhs ;
296 bool rhs_na ;
297 } ;
298 template <typename T>
299 class Minus_Vector_Primitive<REALSXP,false,T> :
300 public Rcpp::VectorBase<REALSXP,true, Minus_Vector_Primitive<REALSXP,false,T> > {
301 public:
304
305 Minus_Vector_Primitive( const VEC_TYPE& lhs_, double rhs_ ) :
306 lhs(lhs_.get_ref()), rhs(rhs_){}
307
308 inline double operator[]( R_xlen_t i ) const {
309 return lhs[i] - rhs ;
310 }
311
312 inline R_xlen_t size() const { return lhs.size() ; }
313
314 private:
315 const VEC_EXT& lhs ;
316 double rhs ;
317 } ;
318
319
320
321
322
323
324 template <int RTYPE, bool NA, typename T>
326 public Rcpp::VectorBase<RTYPE,true, Minus_Primitive_Vector<RTYPE,NA,T> > {
327 public:
331
333 lhs(lhs_), rhs(rhs_.get_ref()), lhs_na( Rcpp::traits::is_na<RTYPE>(lhs_) ) {}
334
335 inline STORAGE operator[]( R_xlen_t i ) const {
336 if( lhs_na ) return lhs ;
337 return RCPP_SAFE_SUB(lhs, rhs[i]);
338 }
339 inline R_xlen_t size() const { return rhs.size() ; }
340
341 private:
343 const VEC_EXT& rhs ;
344 bool lhs_na ;
345 } ;
346 template <bool NA, typename T>
347 class Minus_Primitive_Vector<REALSXP,NA,T> :
348 public Rcpp::VectorBase<REALSXP,true, Minus_Primitive_Vector<REALSXP,NA,T> > {
349 public:
352
353 Minus_Primitive_Vector( double lhs_, const VEC_TYPE& rhs_ ) :
354 lhs(lhs_), rhs(rhs_.get_ref()){}
355
356 inline double operator[]( R_xlen_t i ) const {
357 return lhs - rhs[i] ;
358 }
359 inline R_xlen_t size() const { return rhs.size() ; }
360
361 private:
362 double lhs ;
363 const VEC_EXT& rhs ;
364 } ;
365
366
367
368 template <int RTYPE, typename T>
369 class Minus_Primitive_Vector<RTYPE,false,T> :
370 public Rcpp::VectorBase<RTYPE,true, Minus_Primitive_Vector<RTYPE,false,T> > {
371 public:
375
377 lhs(lhs_), rhs(rhs_.get_ref()), lhs_na( Rcpp::traits::is_na<RTYPE>(lhs_) ) {}
378
379 inline STORAGE operator[]( R_xlen_t i ) const {
380 if( lhs_na ) return lhs ;
381 return lhs - rhs[i];
382 }
383
384 inline R_xlen_t size() const { return rhs.size() ; }
385
386 private:
388 const VEC_EXT& rhs ;
389 bool lhs_na ;
390
391 } ;
392 template <typename T>
393 class Minus_Primitive_Vector<REALSXP,false,T> :
394 public Rcpp::VectorBase<REALSXP,true, Minus_Primitive_Vector<REALSXP,false,T> > {
395 public:
398
399 Minus_Primitive_Vector( double lhs_, const VEC_TYPE& rhs_ ) :
400 lhs(lhs_), rhs(rhs_.get_ref()){}
401
402 inline double operator[]( R_xlen_t i ) const {
403 return lhs - rhs[i] ;
404 }
405
406 inline R_xlen_t size() const { return rhs.size() ; }
407
408 private:
409 double lhs ;
410 const VEC_EXT& rhs ;
411 } ;
412
413
414}
415
416template <int RTYPE,bool NA, typename T, typename U>
419 const VectorBase<RTYPE,NA,T>& lhs,
420 const U &rhs
421) {
423}
424
425
426template <int RTYPE,bool NA, typename T, typename U>
427inline typename traits::enable_if<traits::is_convertible<typename traits::remove_const_and_reference<U>::type, typename traits::storage_type<RTYPE>::type>::value, sugar::Minus_Primitive_Vector< RTYPE , NA,T> >::type
429 const U &lhs,
430 const VectorBase<RTYPE,NA,T>& rhs
431) {
433}
434
435template <int RTYPE,bool LHS_NA, typename LHS_T, bool RHS_NA, typename RHS_T>
436inline sugar::Minus_Vector_Vector<
437 RTYPE ,
438 LHS_NA, LHS_T,
439 RHS_NA, RHS_T
440 >
444) {
446 RTYPE,
447 LHS_NA,LHS_T,
448 RHS_NA,RHS_T
449 >( lhs, rhs ) ;
450}
451
452}
453
454#endif
Minus_Vector_Vector< RTYPE, LHS_NA, LHS_T, RHS_NA, RHS_T > & get_ref()
Definition VectorBase.h:37
Minus_Primitive_Vector(double lhs_, const VEC_TYPE &rhs_)
Definition minus.h:353
Rcpp::traits::Extractor< REALSXP, NA, T >::type VEC_EXT
Definition minus.h:351
Rcpp::VectorBase< REALSXP, NA, T > VEC_TYPE
Definition minus.h:350
Minus_Primitive_Vector(double lhs_, const VEC_TYPE &rhs_)
Definition minus.h:399
Rcpp::VectorBase< REALSXP, false, T > VEC_TYPE
Definition minus.h:396
Rcpp::traits::Extractor< REALSXP, false, T >::type VEC_EXT
Definition minus.h:397
Minus_Primitive_Vector(STORAGE lhs_, const VEC_TYPE &rhs_)
Definition minus.h:376
Rcpp::traits::Extractor< RTYPE, false, T >::type VEC_EXT
Definition minus.h:374
traits::storage_type< RTYPE >::type STORAGE
Definition minus.h:373
Rcpp::VectorBase< RTYPE, false, T > VEC_TYPE
Definition minus.h:372
traits::storage_type< RTYPE >::type STORAGE
Definition minus.h:329
STORAGE operator[](R_xlen_t i) const
Definition minus.h:335
Minus_Primitive_Vector(STORAGE lhs_, const VEC_TYPE &rhs_)
Definition minus.h:332
Rcpp::traits::Extractor< RTYPE, NA, T >::type VEC_EXT
Definition minus.h:330
Rcpp::VectorBase< RTYPE, NA, T > VEC_TYPE
Definition minus.h:328
Rcpp::traits::Extractor< REALSXP, NA, T >::type VEC_EXT
Definition minus.h:257
Rcpp::VectorBase< REALSXP, NA, T > VEC_TYPE
Definition minus.h:256
Minus_Vector_Primitive(const VEC_TYPE &lhs_, double rhs_)
Definition minus.h:259
Rcpp::traits::Extractor< REALSXP, false, T >::type VEC_EXT
Definition minus.h:303
Minus_Vector_Primitive(const VEC_TYPE &lhs_, double rhs_)
Definition minus.h:305
Rcpp::VectorBase< REALSXP, false, T > VEC_TYPE
Definition minus.h:302
Minus_Vector_Primitive(const VEC_TYPE &lhs_, STORAGE rhs_)
Definition minus.h:282
Rcpp::VectorBase< RTYPE, false, T > VEC_TYPE
Definition minus.h:279
Rcpp::traits::Extractor< RTYPE, false, T >::type VEC_EXT
Definition minus.h:280
traits::storage_type< RTYPE >::type STORAGE
Definition minus.h:278
Rcpp::VectorBase< RTYPE, NA, T > VEC_TYPE
Definition minus.h:233
Minus_Vector_Primitive(const VEC_TYPE &lhs_, STORAGE rhs_)
Definition minus.h:236
STORAGE operator[](R_xlen_t i) const
Definition minus.h:239
Rcpp::traits::Extractor< RTYPE, NA, T >::type VEC_EXT
Definition minus.h:234
traits::storage_type< RTYPE >::type STORAGE
Definition minus.h:232
Minus_Vector_Vector(const LHS_TYPE &lhs_, const RHS_TYPE &rhs_)
Definition minus.h:65
Rcpp::VectorBase< REALSXP, RHS_NA, RHS_T > RHS_TYPE
Definition minus.h:61
Rcpp::traits::Extractor< REALSXP, RHS_NA, RHS_T >::type RHS_EXT
Definition minus.h:63
Rcpp::traits::Extractor< REALSXP, LHS_NA, LHS_T >::type LHS_EXT
Definition minus.h:62
Rcpp::VectorBase< REALSXP, LHS_NA, LHS_T > LHS_TYPE
Definition minus.h:60
Rcpp::traits::Extractor< REALSXP, false, RHS_T >::type RHS_EXT
Definition minus.h:161
Rcpp::traits::Extractor< REALSXP, LHS_NA, LHS_T >::type LHS_EXT
Definition minus.h:160
Minus_Vector_Vector(const LHS_TYPE &lhs_, const RHS_TYPE &rhs_)
Definition minus.h:163
Rcpp::VectorBase< REALSXP, LHS_NA, LHS_T > LHS_TYPE
Definition minus.h:158
Rcpp::VectorBase< REALSXP, RHS_NA, RHS_T > RHS_TYPE
Definition minus.h:110
Minus_Vector_Vector(const LHS_TYPE &lhs_, const RHS_TYPE &rhs_)
Definition minus.h:114
Rcpp::traits::Extractor< REALSXP, false, LHS_T >::type LHS_EXT
Definition minus.h:111
Rcpp::traits::Extractor< REALSXP, RHS_NA, RHS_T >::type RHS_EXT
Definition minus.h:112
Rcpp::traits::Extractor< REALSXP, false, RHS_T >::type RHS_EXT
Definition minus.h:208
Minus_Vector_Vector(const LHS_TYPE &lhs_, const RHS_TYPE &rhs_)
Definition minus.h:210
Rcpp::traits::Extractor< REALSXP, false, LHS_T >::type LHS_EXT
Definition minus.h:207
Rcpp::traits::Extractor< RTYPE, LHS_NA, LHS_T >::type LHS_EXT
Definition minus.h:135
Rcpp::traits::Extractor< RTYPE, false, RHS_T >::type RHS_EXT
Definition minus.h:136
Minus_Vector_Vector(const LHS_TYPE &lhs_, const RHS_TYPE &rhs_)
Definition minus.h:138
Rcpp::VectorBase< RTYPE, RHS_NA, RHS_T > RHS_TYPE
Definition minus.h:84
Minus_Vector_Vector(const LHS_TYPE &lhs_, const RHS_TYPE &rhs_)
Definition minus.h:89
Rcpp::traits::Extractor< RTYPE, false, LHS_T >::type LHS_EXT
Definition minus.h:86
Rcpp::traits::Extractor< RTYPE, RHS_NA, RHS_T >::type RHS_EXT
Definition minus.h:87
Minus_Vector_Vector(const LHS_TYPE &lhs_, const RHS_TYPE &rhs_)
Definition minus.h:188
Rcpp::traits::Extractor< RTYPE, false, RHS_T >::type RHS_EXT
Definition minus.h:186
Rcpp::traits::Extractor< RTYPE, false, LHS_T >::type LHS_EXT
Definition minus.h:185
R_xlen_t size() const
Definition minus.h:49
Minus_Vector_Vector(const LHS_TYPE &lhs_, const RHS_TYPE &rhs_)
Definition minus.h:39
traits::storage_type< RTYPE >::type STORAGE
Definition minus.h:35
Rcpp::VectorBase< RTYPE, LHS_NA, LHS_T > LHS_TYPE
Definition minus.h:33
Rcpp::traits::Extractor< RTYPE, RHS_NA, RHS_T >::type RHS_EXT
Definition minus.h:37
Rcpp::traits::Extractor< RTYPE, LHS_NA, LHS_T >::type LHS_EXT
Definition minus.h:36
Rcpp::VectorBase< RTYPE, RHS_NA, RHS_T > RHS_TYPE
Definition minus.h:34
STORAGE operator[](R_xlen_t i) const
Definition minus.h:42
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
double operator-(const Date &d1, const Date &d2)
Definition Date.h:164
static Na_Proxy NA
Definition Na_Proxy.h:52
#define RCPP_SAFE_SUB(a, b)
Definition safe_math.h:34