-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathlengths.c
74 lines (55 loc) · 1.18 KB
/
lengths.c
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
#include <Rdefines.h>
SEXP
two(SEXP x, SEXP y)
{
double ans = 0;
int n = Rf_length(x); // Rf_xlength(x)
for(int i = 0; i < n; i++) {
ans += REAL(x)[i] + REAL(y)[i];
}
return(ScalarReal(ans));
}
// This version accesses 2*i of y for each iteration so y must have length >= 2*length(x)
SEXP
twoStride(SEXP x, SEXP y)
{
double ans = 0;
int n = Rf_length(x); // Rf_xlength(x)
for(int i = 0; i < n; i++) {
ans += REAL(x)[i] + REAL(y)[2*i];
}
return(ScalarReal(ans));
}
SEXP
foo(SEXP x, SEXP y)
{
int n = Rf_length(x);
double ans = 0.;
for(int i = 0; i < n; i++) {
ans += REAL(VECTOR_ELT(x, i))[0] + REAL(VECTOR_ELT(y, i))[0];
}
return(ScalarReal(ans));
}
SEXP
nchar(SEXP x, SEXP y)
{
int n = Rf_length(x);
double ans = 0.;
for(int i = 0; i < n; i++) {
ans += strlen(CHAR(STRING_ELT(x, i))) + strlen(CHAR(STRING_ELT(y, i)));
}
return(ScalarInteger(ans));
}
SEXP
matLoop(SEXP m)
{
int i, j;
double ans = 0.0;
int nrow = Rf_nrows(m), ncol = Rf_ncols(m);
for(i = 0; i < nrow; i++) {
for(j = 0; j < ncol ; j++) {
ans += REAL(m)[i*nrow + j];
}
}
return(ScalarReal(ans));
}