-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathArray.h
More file actions
67 lines (55 loc) · 2.7 KB
/
Array.h
File metadata and controls
67 lines (55 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
#ifndef Rcpp__Array__h
#define Rcpp__Array__h
namespace Rcpp{
template <int N, int RTYPE, template <class> class StoragePolicy = PreserveStorage>
class Array {
public:
using Vec = Vector<RTYPE,StoragePolicy>;
using Proxy = typename Vec::Proxy;
using const_Proxy = typename Vec::const_Proxy;
Array( SEXP x ) : index(), data(x) {
IntegerVector dim = data.attr("dim") ;
if( dim.size() != N ) stop("incompatible dimensions") ;
for( int i=0; i<N; i++)
index[i] = dim[i] ;
}
template <
typename... Args,
typename = typename std::enable_if< ValidIndexArgs<N,Args...>() >::type
>
Array( Args... args ) :
index({ static_cast<size_t>(args)... }),
data(index.prod())
{
data.attr("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 ; }
private:
Index<N> index ;
Vec data ;
} ;
template <int N, template <class> class StoragePolicy = PreserveStorage> using NumericArray = Array<N, REALSXP, StoragePolicy> ;
template <int N, template <class> class StoragePolicy = PreserveStorage> using DoubleArray = Array<N, REALSXP, StoragePolicy> ;
template <int N, template <class> class StoragePolicy = PreserveStorage> using IntegerArray = Array<N, INTSXP , StoragePolicy> ;
template <int N, template <class> class StoragePolicy = PreserveStorage> using StringArray = Array<N, STRSXP , StoragePolicy> ;
template <int N, template <class> class StoragePolicy = PreserveStorage> using CharacterArray = Array<N, STRSXP , StoragePolicy> ;
template <int N, template <class> class StoragePolicy = PreserveStorage> using LogicalArray = Array<N, LGLSXP , StoragePolicy> ;
template <int N, template <class> class StoragePolicy = PreserveStorage> using RawArray = Array<N, RAWSXP , StoragePolicy> ;
template <int N, template <class> class StoragePolicy = PreserveStorage> using ComplexArray = Array<N, CPLXSXP, StoragePolicy> ;
} // Rcpp
#endif