1 // -*- mode: C++; c-indent-level: 4; c-basic-offset: 4; indent-tabs-mode: nil; -*-
2 /* :tabSize=4:indentSize=4:noTabs=false:folding=explicit:collapseFolds=1: */
3 //
4 // r_vector.h: Rcpp R/C++ interface class library -- information about R vectors
5 //
6 // Copyright (C) 2010 - 2017  Dirk Eddelbuettel and Romain Francois
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__internal__r_vector_h
24 #define Rcpp__internal__r_vector_h
25 
26 namespace Rcpp{
27 namespace internal{
28 
29 template <int RTYPE>
r_vector_start(SEXP x)30 typename Rcpp::traits::storage_type<RTYPE>::type* r_vector_start(SEXP x) {
31     typedef typename Rcpp::traits::storage_type<RTYPE>::type* pointer;
32     return reinterpret_cast<pointer>(dataptr(x));
33 }
34 
35 /**
36  * The value 0 statically casted to the appropriate type for
37  * the given SEXP type
38  */
39 template <int RTYPE,typename CTYPE>				// #nocov start
get_zero()40 inline CTYPE get_zero() {
41     return static_cast<CTYPE>(0);
42 }								// #nocov end
43 
44 
45 /**
46  * Specialization for Rcomplex
47  */
48 template<>
49 inline Rcomplex get_zero<CPLXSXP,Rcomplex>(){
50     Rcomplex x;
51     x.r = 0.0;
52     x.i = 0.0;
53     return x;
54 }
55 
56 /**
57  * Initializes a vector of the given SEXP type. The template fills the
58  * vector with the value 0 of the appropriate type, for example
59  * an INTSXP vector is initialized with (int)0, etc...
60  */
r_init_vector(SEXP x)61 template<int RTYPE> void r_init_vector(SEXP x) {		// #nocov start
62     typedef typename ::Rcpp::traits::storage_type<RTYPE>::type CTYPE;
63     CTYPE* start=r_vector_start<RTYPE>(x);
64     std::fill(start, start + Rf_xlength(x), get_zero<RTYPE,CTYPE>());
65 }								// #nocov end
66 /**
67  * Initializes a generic vector (VECSXP). Does nothing since
68  * R already initializes all elements to NULL
69  */
70 template<>
71 inline void r_init_vector<VECSXP>(SEXP /*x*/) {}
72 
73 /**
74  * Initializes an expression vector (EXPRSXP). Does nothing since
75  * R already initializes all elements to NULL
76  */
77 template<>
78 inline void r_init_vector<EXPRSXP>(SEXP /*x*/) {}
79 
80 /**
81  * Initializes a character vector (STRSXP). Does nothing since
82  * R already initializes all elements to ""
83  */
84 template<>
85 inline void r_init_vector<STRSXP>(SEXP /*x*/) {}
86 
87 
88 /**
89  * We do not allow List(RTYPE=VECSXP), RawVector(RTYPE=RAWSXP)
90  * or ExpressionVector(RTYPE=EXPRSXP) to be sorted, so it is
91  * desirable to issue a compiler error if user attempts to sort
92  * these types of Vectors.
93  *
94  * We declare a template class without defining the generic
95  * class body, but complete the definition in specialization
96  * of qualified Vector types. Hence when using this class
97  * on unqualified Vectors, the compiler will emit errors.
98  */
99 template<int RTYPE>
100 class Sort_is_not_allowed_for_this_type;
101 
102 /**
103  * Specialization for CPLXSXP, INTSXP, LGLSXP, REALSXP, and STRSXP
104  */
105 template<>
106 class Sort_is_not_allowed_for_this_type<CPLXSXP> {
107 public:
do_nothing()108     static void do_nothing() {}
109 };
110 
111 template<>
112 class Sort_is_not_allowed_for_this_type<INTSXP> {
113 public:
do_nothing()114     static void do_nothing() {}
115 };
116 
117 template<>
118 class Sort_is_not_allowed_for_this_type<LGLSXP> {
119 public:
do_nothing()120     static void do_nothing() {}
121 };
122 
123 template<>
124 class Sort_is_not_allowed_for_this_type<REALSXP> {
125 public:
do_nothing()126     static void do_nothing() {}
127 };
128 
129 template<>
130 class Sort_is_not_allowed_for_this_type<STRSXP> {
131 public:
do_nothing()132     static void do_nothing() {}
133 };
134 
135 
136 } // internal
137 } // Rcpp
138 
139 #endif
140