Rcpp Version 1.1.2
Loading...
Searching...
No Matches
cumsum.h
Go to the documentation of this file.
1// -*- mode: C++; c-indent-level: 4; c-basic-offset: 4; tab-width: 8 -*-
2//
3// cumsum.h: Rcpp R/C++ interface class library -- cumsum
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__cumsum_h
24#define Rcpp__sugar__cumsum_h
25
26namespace Rcpp{
27namespace sugar{
28
29template <int RTYPE, bool NA, typename T>
30class Cumsum : public Lazy< Rcpp::Vector<RTYPE> , Cumsum<RTYPE,NA,T> > {
31public:
35
36 Cumsum( const VEC_TYPE& object_ ) : object(object_){}
37
38 VECTOR get() const {
39 R_xlen_t n = object.size() ;
41 STORAGE current = object[0] ;
42 if( Rcpp::traits::is_na<RTYPE>(current) )
43 return result ;
44 result[0] = current ;
45 for( R_xlen_t i=1; i<n; i++){
46 current = object[i] ;
47 if( Rcpp::traits::is_na<RTYPE>(current) )
48 return result ;
49 result[i] = RCPP_SAFE_ADD(result[i-1], current);
50 }
51 return result ;
52 }
53private:
55} ;
56
57} // sugar
58
59template <bool NA, typename T>
63
64template <bool NA, typename T>
68
69
70} // Rcpp
71#endif
72
R_xlen_t size() const
Definition Vector.h:274
Cumsum(const VEC_TYPE &object_)
Definition cumsum.h:36
VECTOR get() const
Definition cumsum.h:38
Rcpp::traits::storage_type< RTYPE >::type STORAGE
Definition cumsum.h:33
const VEC_TYPE & object
Definition cumsum.h:54
Rcpp::VectorBase< RTYPE, NA, T > VEC_TYPE
Definition cumsum.h:32
Rcpp::Vector< RTYPE > VECTOR
Definition cumsum.h:34
storage_type< RTYPE >::type get_na()
bool is_na(typename storage_type< RTYPE >::type)
Definition is_na.h:32
Rcpp API.
Definition algo.h:28
sugar::Cumsum< INTSXP, NA, T > cumsum(const VectorBase< INTSXP, NA, T > &t)
Definition cumsum.h:60
#define RCPP_SAFE_ADD(a, b)
Definition safe_math.h:33