Skip to content

Commit 87b7b1c

Browse files
authored
Merge branch 'master' into issue2855
2 parents f210a33 + 35b6299 commit 87b7b1c

File tree

2 files changed

+42
-35
lines changed

2 files changed

+42
-35
lines changed

src/frank.c

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,22 +3,26 @@
33
// #include <signal.h> // the debugging machinery + breakpoint aidee
44
// raise(SIGINT);
55

6-
SEXP dt_na(SEXP x, SEXP cols) {
7-
int n=0, elem;
8-
6+
SEXP dt_na(SEXP x, SEXP cols)
7+
{
98
if (!isNewList(x)) internal_error(__func__, "Argument '%s' to %s is type '%s' not '%s'", "x", "Cdt_na", type2char(TYPEOF(x)), "list"); // # nocov
109
if (!isInteger(cols)) internal_error(__func__, "Argument '%s' to %s is type '%s' not '%s'", "cols", "Cdt_na", type2char(TYPEOF(cols)), "integer"); // # nocov
11-
for (int i=0; i<LENGTH(cols); ++i) {
12-
elem = INTEGER(cols)[i];
13-
if (elem<1 || elem>LENGTH(x))
14-
error(_("Item %d of 'cols' is %d which is outside 1-based range [1,ncol(x)=%d]"), i+1, elem, LENGTH(x));
15-
if (!n) n = length(VECTOR_ELT(x, elem-1));
10+
11+
int n = 0;
12+
const int numCols = LENGTH(cols);
13+
const int* col_ints = INTEGER_RO(cols);
14+
for (int i = 0; i < numCols; i++) {
15+
const int elem = col_ints[i];
16+
if (elem < 1 || elem > LENGTH(x))
17+
error(_("Item %d of 'cols' is %d which is outside 1-based range [1,ncol(x)=%d]"), i + 1, elem, LENGTH(x));
18+
if (!n) n = length(VECTOR_ELT(x, elem - 1));
1619
}
1720
SEXP ans = PROTECT(allocVector(LGLSXP, n));
1821
int *ians = LOGICAL(ans);
19-
for (int i=0; i<n; ++i) ians[i]=0;
20-
for (int i=0; i<LENGTH(cols); ++i) {
21-
SEXP v = VECTOR_ELT(x, INTEGER(cols)[i]-1);
22+
memset(ians, 0, n * sizeof(int));
23+
24+
for (int i = 0; i < numCols; i++) {
25+
SEXP v = VECTOR_ELT(x, col_ints[i]-1);
2226
if (!length(v) || isList(v)) continue; // like stats:::na.omit.data.frame, skip pairlist columns
2327
if (n != length(v))
2428
error(_("Column %d of input list x is length %d, inconsistent with first column of that item which is length %d."), i+1,length(v),n);

src/idatetime.c

Lines changed: 27 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
#include "data.table.h"
22

3-
#define YEARS400 146097
4-
#define YEARS100 36524
5-
#define YEARS4 1461
6-
#define YEARS1 365
3+
static const int YEARS400 = 146097;
4+
static const int YEARS100 = 36524;
5+
static const int YEARS4 = 1461;
6+
static const int YEARS1 = 365;
77

88
typedef enum { YDAY, WDAY, MDAY, WEEK, MONTH, QUARTER, YEAR, YEARMON, YEARQTR} datetype;
99

@@ -124,37 +124,40 @@ void convertSingleDate(int x, datetype type, void *out)
124124
SEXP convertDate(SEXP x, SEXP type)
125125
{
126126
if (!isInteger(x)) error(_("x must be an integer vector"));
127-
const int *ix = INTEGER(x);
127+
const int *ix = INTEGER_RO(x);
128128
const int n = length(x);
129129
if (!isString(type) || length(type) != 1)
130130
internal_error(__func__, "invalid type for, should have been caught before"); // # nocov
131-
datetype ctype=0;
131+
datetype ctype = 0;
132132
bool ansint = true;
133-
if (!strcmp(CHAR(STRING_ELT(type, 0)), "yday")) ctype = YDAY;
134-
else if (!strcmp(CHAR(STRING_ELT(type, 0)), "wday")) ctype = WDAY;
135-
else if (!strcmp(CHAR(STRING_ELT(type, 0)), "mday")) ctype = MDAY;
136-
else if (!strcmp(CHAR(STRING_ELT(type, 0)), "week")) ctype = WEEK;
137-
else if (!strcmp(CHAR(STRING_ELT(type, 0)), "month")) ctype = MONTH;
138-
else if (!strcmp(CHAR(STRING_ELT(type, 0)), "quarter")) ctype = QUARTER;
139-
else if (!strcmp(CHAR(STRING_ELT(type, 0)), "year")) ctype = YEAR;
140-
else if (!strcmp(CHAR(STRING_ELT(type, 0)), "yearmon")) { ctype = YEARMON; ansint = false; }
141-
else if (!strcmp(CHAR(STRING_ELT(type, 0)), "yearqtr")) { ctype = YEARQTR; ansint = false; }
142-
else internal_error(__func__, "invalid type for, should have been caught before"); // # nocov
143-
144-
SEXP ans;
133+
134+
const char* ctype_str = CHAR(STRING_ELT(type, 0));
135+
if (!strcmp(ctype_str, "yday")) ctype = YDAY;
136+
else if (!strcmp(ctype_str, "wday")) ctype = WDAY;
137+
else if (!strcmp(ctype_str, "mday")) ctype = MDAY;
138+
else if (!strcmp(ctype_str, "week")) ctype = WEEK;
139+
else if (!strcmp(ctype_str, "month")) ctype = MONTH;
140+
else if (!strcmp(ctype_str, "quarter")) ctype = QUARTER;
141+
else if (!strcmp(ctype_str, "year")) ctype = YEAR;
142+
else if (!strcmp(ctype_str, "yearmon")) { ctype = YEARMON; ansint = false; }
143+
else if (!strcmp(ctype_str, "yearqtr")) { ctype = YEARQTR; ansint = false; }
144+
else internal_error(__func__, "invalid type, should have been caught before"); // # nocov
145+
145146
if (ansint) {
146-
ans = PROTECT(allocVector(INTSXP, n));
147+
SEXP ans = PROTECT(allocVector(INTSXP, n));
147148
int *ansp = INTEGER(ans);
148-
for (int i=0; i < n; ++i) {
149+
for (int i = 0; i < n; i++) {
149150
convertSingleDate(ix[i], ctype, &ansp[i]);
150151
}
152+
UNPROTECT(1);
153+
return ans;
151154
} else {
152-
ans = PROTECT(allocVector(REALSXP, n));
155+
SEXP ans = PROTECT(allocVector(REALSXP, n));
153156
double *ansp = REAL(ans);
154-
for (int i=0; i < n; ++i) {
157+
for (int i = 0; i < n; i++) {
155158
convertSingleDate(ix[i], ctype, &ansp[i]);
156159
}
160+
UNPROTECT(1);
161+
return ans;
157162
}
158-
UNPROTECT(1);
159-
return ans;
160163
}

0 commit comments

Comments
 (0)