MCPcopy Create free account
hub / github.com/RcppCore/Rcpp / GreedyVector

Class GreedyVector

inst/include/Rcpp/internal/GreedyVector.h:24–92  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

22
23 template <typename T, typename CLASS>
24 class GreedyVector {
25 public:
26 typedef typename std::vector<T>::iterator iterator;
27 typedef typename std::vector<T>::const_iterator const_iterator;
28
29 GreedyVector(SEXP vec) : v(0){
30 if (!Rf_isNumeric(vec) || Rf_isMatrix(vec) || Rf_isLogical(vec))
31 throw std::range_error("invalid numeric vector in constructor");
32 int len = Rf_length(vec);
33 if (len == 0)
34 throw std::range_error("null vector in constructor");
35 v.resize(len);
36 for (int i = 0; i < len; i++)
37 v[i] = T( static_cast<double>(REAL(vec)[i]));
38 }
39
40 GreedyVector(int n) : v(n){}
41
42 inline const T& operator()(int i) const{
43 return at(i) ;
44 }
45 inline T& operator()(int i){
46 return at(i) ;
47 }
48
49 inline const T& operator[](int i) const{
50 return at(i) ;
51 }
52 inline T& operator[](int i){
53 return at(i) ;
54 }
55
56 inline int size() const {
57 return (int)v.size();
58 }
59
60 inline iterator begin(){ return v.begin(); }
61 inline iterator end(){ return v.end(); }
62
63 inline const_iterator begin() const { return v.begin(); }
64 inline const_iterator end() const { return v.end(); }
65
66 inline operator SEXP() const {
67 return wrap( v ) ;
68 }
69
70 protected:
71 std::vector<T> v;
72
73 private:
74 const T& at(int i) const{
75 if (i < 0 || i >= static_cast<int>(v.size())) {
76 std::ostringstream oss;
77 oss << "subscript out of range: " << i;
78 throw std::range_error(oss.str());
79 }
80 return v[i];
81 }

Callers

nothing calls this directly

Calls 2

wrapFunction · 0.70
sizeMethod · 0.45

Tested by

no test coverage detected