-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathArray.h
More file actions
75 lines (61 loc) · 2.7 KB
/
Array.h
File metadata and controls
75 lines (61 loc) · 2.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#ifndef Rcpp__Array__h
#define Rcpp__Array__h
namespace Rcpp{
template <int N, int RTYPE, typename Storage = PreserveStorage>
class Array {
public:
typedef Vector<RTYPE,Storage> Vec;
typedef typename Vec::Proxy Proxy;
Array( SEXP x ) : index(), data(x) {
IntegerVector dim = attr(data, "dim") ;
if( dim.size() != N ) stop("incompatible dimensions") ;
std::copy( dim.begin(), dim.end(), index.begin() ) ;
}
template <
typename... Args,
typename = typename std::enable_if< ValidIndexArgs<N,Args...>() >::type
>
Array( Args... args ) :
index({ static_cast<size_t>(args)... }),
data(index.prod())
{
attr(data, "dim") = index ;
}
template <
typename... Args,
typename = typename std::enable_if< ValidIndexArgs<N,Args...>() >::type
>
Proxy operator()( Args&&... args){
return data[ index( std::forward<Args>(args)... ) ];
}
template <
typename... Args,
typename = typename std::enable_if< ValidIndexArgs<N,Args...>() >::type
>
const Proxy operator()( Args... args) const {
return data[ index.get_index( args... ) ];
}
inline operator SEXP() const { return data ; }
inline operator SEXP(){ return data ; }
template <typename T>
Array& fill( T value ){
std::fill( data.begin(), data.end(), value ) ;
return *this ;
}
inline int get_dim(int i) const {
return index.get_dim(i) ;
}
private:
Index<N> index ;
Vec data ;
} ;
template <int N, typename Storage = PreserveStorage> using NumericArray = Array<N, REALSXP, Storage> ;
template <int N, typename Storage = PreserveStorage> using DoubleArray = Array<N, REALSXP, Storage> ;
template <int N, typename Storage = PreserveStorage> using IntegerArray = Array<N, INTSXP , Storage> ;
template <int N, typename Storage = PreserveStorage> using StringArray = Array<N, STRSXP , Storage> ;
template <int N, typename Storage = PreserveStorage> using CharacterArray = Array<N, STRSXP , Storage> ;
template <int N, typename Storage = PreserveStorage> using LogicalArray = Array<N, LGLSXP , Storage> ;
template <int N, typename Storage = PreserveStorage> using RawArray = Array<N, RAWSXP , Storage> ;
template <int N, typename Storage = PreserveStorage> using ComplexArray = Array<N, CPLXSXP, Storage> ;
} // Rcpp
#endif